
    +h              	       6   S SK r S SKJr  S SKJrJrJr  S SKrS SK	r	S SK
Js  Jr  S SK	Jr  SSKJrJr  SSKJr  SSKJr  S	S
KJr  S\	R.                  S\S\	R.                  4S jrS5S\	R.                  S\S\S\	R.                  4S jjrS rS6S jrS\S\4S jrS rS r S\	R.                  4S jr!S\	R.                  S\\\\4   4S jr" " S S\RF                  5      r$\ " S S 5      5       r% " S! S"\RF                  5      r& " S# S$\RF                  5      r' " S% S&\RF                  5      r(\ " S' S(\5      5       r) " S) S*\RF                  5      r*\ " S+ S,\5      5       r+ " S- S.\\5      r, " S/ S0\RF                  5      r- " S1 S2\\5      r. " S3 S4\\5      r/g)7    N)	dataclass)DictOptionalTuple)nn   )ConfigMixinregister_to_config)
ModelMixin)
BaseOutput   )create_pan_cameraspmf	n_samplesreturnc           	      N   U R                   Gt p#nUS:X  d   e[        R                  " U R                  SU5      SS9n[        R                  " U[        R
                  " UR                   S   XR                  S95      nUR                  " / UQUPSP76 R                  SUS-
  5      $ )a  
Sample from the given discrete probability distribution with replacement.

The i-th bin is assumed to have mass pmf[i].

Args:
    pmf: [batch_size, *shape, n_samples, 1] where (pmf.sum(dim=-2) == 1).all()
    n_samples: number of samples

Return:
    indices sampled with replacement
r   dimr   device)shapetorchcumsumviewsearchsortedrandr   clamp)r   r   r   support_sizelast_dimcdfindss          ]/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/shap_e/renderer.py
sample_pmfr$      s     &)YY"U(q==
,,sxxL1q
9Cc5::ciilIjj#YZD99*e*Y**00L14DEE    xmin_degmax_degc                    X:X  a  U $ S[         R                  " XU R                  U R                  S9-  nU R                  Gt pEU R                  SSU5      UR                  SSS5      -  R
                  " / UQSP76 nUR                  S   XRU-
  -  :X  d   e[         R                  " Xf[        R                  S-  -   /SS9R                  5       n[         R                  " X/SS9$ )zr
Concatenate x and its positional encodings, following NeRF.

Reference: https://huggingface.co/papers/2210.04628
g       @)dtyper   r   r   )axisr   )r   aranger*   r   r   reshaper   catmathpisin)r&   r'   r(   scalesr   r   xbembs           r#   posenc_nerfr5   5   s     ELLRRF''KU
))B3
&++aQ"7
7	@	@	L%	L	LB88B<3G"34444
))Rdggm+,2
6
:
:
<C99aX2&&r%   c                     [        U SSS9$ )Nr      r'   r(   )r5   positions    r#   encode_positionr;   F   s    xB77r%   c                 \    Uc  [         R                  " [        U SSS95      $ [        USSS9$ )Nr      r8   )r   
zeros_liker5   )r:   	directions     r#   encode_directionr@   J   s1    Ha KLL9a;;r%   c                 &    U R                  SS5      $ )N.__)replacer&   s    r#   _sanitize_namerF   Q   s    99S$r%   c                    U R                  U5      u    pEX%-  n[        R                  " USS9n[        R                  " USSSS24   * 5      nS[        R                  " U* 5      -
  n	[        R                  " [        R                  " [        R
                  " USSS2SS24   5      USSS2SS24   * /SS95      n
X-  n[        R                  " X;-  SS9nX;U4$ )a  
Function integrating the model output.

Args:
    volume_range: Specifies the integral range [t0, t1]
    ts: timesteps
    density: torch.Tensor [batch_size, *shape, n_samples, 1]
    channels: torch.Tensor [batch_size, *shape, n_samples, n_channels]
returns:
    channels: integrated rgb output weights: torch.Tensor [batch_size, *shape, n_samples, 1] (density
    *transmittance)[i] weight for each rgb output at [..., i, :]. transmittance: transmittance of this volume
)
r   .r   N      ?r   )	partitionr   r   expr.   r>   sum)volume_rangetsdensitychannels_dtddensitymasstransmittancealphasTsweightss               r#   integrate_samplesrY   U   s      %%b)HAq|H<<b)DIItCQJ//0M599hY''F	599e..tC!QJ/?@4SbSRSCTBTU[]^	_B kG yy+4Hm++r%   c                 p   [         R                  " US-  U R                  R                  S9nX!-  n[         R                  " X!SS9U-  n[         R                  " X!S-  SS9U-  n[         R
                  " XTU/SS9nUR                  5       US-
  -  U R                  U R                  -
  -  U R                  -   $ )Nr   r   trunc)rounding_mode   r   r   )r   r,   bbox_minr   divstackfloatbbox_max)volume	grid_sizeindiceszsysxscombineds          r#   volume_query_pointsrj   w   s    ll9a<0F0FGG		B	7W	=		IB	7qL	@9	LB{{BB<Q/HNN	A.6??V__3TUX^XgXgggr%   uc                 P    [         R                  " U S:*  U S-  U S-   S-  S-  5      $ )Ng?ܵ?gףp=
)@g)\(?gzG?g333333@r   where)rk   s    r#   _convert_srgb_to_linearro      s+    ;;qG|QY!e)u1D0LMMr%   flat_cube_indicesrd   c                    US   S-
  US   -  US   -  nUnUS   US   S-
  -  US   -  nX$-   n[         R                  " U S S 2S4   US   -  US   -  U S S 2S4   US   -  -   U S S 2S4   -   U S S 2S4   US   -  US   -  U S S 2S4   S-   US   -  -   U S S 2S4   -   U S S 2S4   US   -  US   -  U S S 2S4   US   -  -   U S S 2S4   -   S-   U S S 2S4   US   -  US   -  U S S 2S4   S-   US   -  -   U S S 2S4   -   S-   UU S S 2S4   US   S-
  -  US   -  -   U S S 2S4   US   -  -   U S S 2S4   -   UU S S 2S4   S-   US   S-
  -  US   -  -   U S S 2S4   US   -  -   U S S 2S4   -   UU S S 2S4   US   S-
  -  US   -  -   U S S 2S4   US   -  -   U S S 2S4   -   S-   UU S S 2S4   S-   US   S-
  -  US   -  -   U S S 2S4   US   -  -   U S S 2S4   -   S-   UU S S 2S4   US   -  US   S-
  -  -   U S S 2S4   US   S-
  -  -   U S S 2S4   -   UU S S 2S4   S-   US   -  US   S-
  -  -   U S S 2S4   US   S-
  -  -   U S S 2S4   -   UU S S 2S4   US   -  US   S-
  -  -   U S S 2S4   S-   US   S-
  -  -   U S S 2S4   -   UU S S 2S4   S-   US   -  US   S-
  -  -   U S S 2S4   S-   US   S-
  -  -   U S S 2S4   -   /SS9$ )Nr   r   r]   r   r   )r   r`   )rp   rd   num_xsy_offsetnum_ysz_offsets         r#   _create_flat_edge_indicesrv      s    lQ)A,.1=FHq\Yq\A-.1=FH;; ad#il2Yq\A1%	!451%& ad#il2Yq\A A&*il:;1%& ad#il2Yq\A1%	!451%&  ad#il2Yq\A A&*il:;1%&  #AqD)Yq\A-=>1MN#AqD)IaL89 $AqD)* $QT*Q.9Q<!3CDyQR|ST#AqD)IaL89 $AqD)* #AqD)Yq\A-=>1MN#AqD)IaL89 $AqD)* 	 $QT*Q.9Q<!3CDyQR|ST#AqD)IaL89 $AqD)* 	 #AqD)IaL8IaL1<LMN#AqD)Yq\A-=>? $AqD)* $QT*Q.)A,>)A,QRBRST#AqD)Yq\A-=>? $AqD)* #AqD)IaL8IaL1<LMN$QT*Q.9Q<!3CDE $AqD)* $QT*Q.)A,>)A,QRBRST$QT*Q.9Q<!3CDE $AqD)*D	
J MG Gr%   c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )VoidNeRFModel   zX
Implements the default empty space model where all queries are rendered as background.
c                    > [         TU ]  5         [        R                  " [        R
                  " [        R                  " U5      5      R                  [        R                  S9U-  5      nU R                  SU5        g )Nr*   
background)super__init__r   	Parameterr   
from_numpynparraytofloat32register_buffer)selfr|   channel_scale	__class__s      r#   r~   VoidNeRFModel.__init__   s[    \\%"2"2288J3G"H"K"KRWR_R_"K"`cp"pq
\:6r%   c                 ,   U R                   S    R                  UR                  5      nUR                  S S nS/[	        U5      S-
  -  nUR                  S   n[
        R                  " UR                  " UR                  S   /UQUP76 / UQUP5      nU$ )Nr   r   r   )r|   r   r   r   lenr   broadcast_tor   )r   r:   r|   r   ones
n_channelss         r#   forwardVoidNeRFModel.forward   s    __T*--hoo>
s#sc%j1n%%%b)
''

8H8H8K(_d(_T^(_auchaujtauv
r%    )     o@)	__name__
__module____qualname____firstlineno____doc__r~   r   __static_attributes____classcell__r   s   @r#   rx   rx      s    7 r%   rx   c                   |    \ rS rSr% \R
                  \S'   \R
                  \S'   \R
                  \S'   S rS rSr	g)	VolumeRange   t0t1intersectedc                     U R                   R                  U R                  R                  s=:X  a  U R                  R                  :X  d   e   eg N)r   r   r   r   )r   s    r#   __post_init__VolumeRange.__post_init__   s7    ww}}G1A1A1G1GGGGGGr%   c                 n   USSS2SS24   USSS2SS24   -   S-  n[         R                  " U R                  SSSS24   U/SS9n[         R                  " X R                  SSSS24   /SS9nXC-
  nUR                  UR                  s=:X  a"  UR                  s=:X  a  UR                  :X  d   e   eX4U4$ )a*  
Partitions t0 and t1 into n_samples intervals.

Args:
    ts: [batch_size, *shape, n_samples, 1]

Return:

    lower: [batch_size, *shape, n_samples, 1] upper: [batch_size, *shape, n_samples, 1] delta: [batch_size,
    *shape, n_samples, 1]

where
    ts \in [lower, upper] deltas = upper - lower
.r   Nr         ?rH   r   )r   r.   r   r   r   )r   rN   midslowerupperdeltas         r#   rJ   VolumeRange.partition   s      3A:C"aK0C7		4773a<0$7R@		4dA!67R@{{ekkDU[[DBHHDDDDDU""r%   r   N)
r   r   r   r   r   Tensor__annotations__r   rJ   r   r   r%   r#   r   r      s+    H#r%   r   c                      ^  \ rS rSrSrSSS.S\S\4U 4S jjjr  SS	\R                  S
\R                  S\	\R                     4S jjr
SrU =r$ )BoundingBoxVolumei  z@
Axis-aligned bounding box defined by the two opposite corners.
        MbP?)min_distmin_t_ranger   r   c                `  > [         TU ]  5         X0l        X@l        [        R
                  " U5      U l        [        R
                  " U5      U l        [        R                  " U R                  U R                  /5      U l	        U R                  R                  S:X  d   eUS:  d   eUS:  d   eg)z
Args:
    bbox_min: the left/bottommost corner of the bounding box
    bbox_max: the other corner of the bounding box
    min_dist: all rays should start at least this distance away from the origin.
r]   r   r   N)r}   r~   r   r   r   tensorr^   rb   r`   bboxr   )r   r^   rb   r   r   r   s        r#   r~   BoundingBoxVolume.__init__  s     	 &X.X.KK >?	yy&(((3S   r%   originr?   t0_lowerc                    UR                   GtpVnS/[        U5      -  nU R                  R                  " S/UQSPSP76 R	                  UR
                  5      n	SS jn
U
" XSSSS24   -
  USSSS24   US9nUR                  SS	9R                  R                  S
SS9R                  R                  U R                  5      nUR                  SS	9R                  R                  S
SS9R                  nUR                   UR                   s=:X  a  U/UQSP7:X  d   e   eUb2  UR                   UR                   :X  d   e[        R                  " X5      nXR                  -   U:  n[        R                  " X[        R                  " U5      5      n[        R                  " X[        R                   " U5      5      n[#        XUS9$ )a'  
Args:
    origin: [batch_size, *shape, 3]
    direction: [batch_size, *shape, 3]
    t0_lower: Optional [batch_size, *shape, 1] lower bound of t0 when intersecting this volume.
    params: Optional meta parameters in case Volume is parametric
    epsilon: to stabilize calculations

Return:
    A tuple of (t0, t1, intersected) where each has a shape [batch_size, *shape, 1]. If a ray intersects with
    the volume, `o + td` is in the volume for all t in [t0, t1]. If the volume is bounded, t1 is guaranteed to
    be on the boundary of the volume.
r   r]   r   c                 F    U [         R                  " US:  X-
  X-   5      -  $ Nr   rm   )abepsilons      r#   _safe_divide1BoundingBoxVolume.intersect.<locals>._safe_divideE  s"    u{{1q5!+q{CCCr%   .N)r   rH   r   r   Tr   keepdim)r   r   r   )ư>)r   r   r   r   r   r   minvaluesmaxr   r   r   maximumr   rn   r>   	ones_liker   )r   r   r?   r   r   
batch_sizer   rQ   r   r   r   rN   r   r   r   s                  r#   	intersectBoundingBoxVolume.intersect,  s   * !'
AsSZyy~~a-$--1-00?	D $T1!55ydA7NX_` VVV^""&&2t&<CCII$--XVVV^""&&2t&<CCxx288>
'>U'>A'>>>>>>88x~~---r,B+++b0[[%*:*:2*>?[[%//"*=>b[AAr%   )r   rb   r^   r   r   )Nr   )r   r   r   r   r   ra   r~   r   r   r   r   r   r   r   s   @r#   r   r     sr     !!
 ! ! !< ,02B2B <<2B 5<<(	2B 2Br%   r   c                       \ rS rSrSrSS\4S jjr SS\R                  S\R                  S\	S\
S	\R                  4
S
 jjrSrg)StratifiedRaySampleria  zW
Instead of fixed intervals, a sample is drawn uniformly at random from each interval.

depth_modec                 4    Xl         U R                   S;   d   eg)zv
:param depth_mode: linear samples ts linearly in depth. harmonic ensures
    closer points are sampled more densely.
)linear	geometricharmonicNr   )r   r   s     r#   r~   StratifiedRaySampler.__init__f  s    
 %"EEEEr%   r   r   r   r   r   c                    S/[        UR                  5      S-
  -  n[        R                  " SSU5      R                  " / UQUP76 R                  UR                  5      R                  UR                  5      nU R                  S:X  a  USU-
  -  X&-  -   nOU R                  S:X  aW  UR                  U5      R                  5       SU-
  -  UR                  U5      R                  5       U-  -   R                  5       nOEU R                  S:X  a5  SSUR                  U5      -  SU-
  -  SUR                  U5      -  U-  -   -  nSUSSS	24   USS	S
24   -   -  n[        R                  " Xr/S
S9n[        R                  " X/S
S9n	[        R                  " S5        [        R                  " U5      n
XU	-
  U
-  -   nUR                  S
5      $ )
Args:
    t0: start time has shape [batch_size, *shape, 1]
    t1: finish time has shape [batch_size, *shape, 1]
    n_samples: number of ts to sample
Return:
    sampled ts of shape [batch_size, *shape, n_samples, 1]
r   r   r   rI   r   r   r   .Nr   r   )r   r   r   linspacer   r   r*   r   r   r   logrK   r.   manual_seed	rand_like	unsqueeze)r   r   r   r   r   r   rN   r   r   r   t_rands              r#   sampleStratifiedRaySampler.samplen  s    sc"((ma'(^^Aq),11C4CCFFrxxPSSTVT]T]^??h&sRx27*B__+((7#'')S2X6'9J9N9N9PSU9UUZZ\B__
* bhhw//38<sRXXgEV?VY[?[[\BbabkBsCRCxL01		4*"-		2*"-!$emv--||Br%   r   N)r   )r   )r   r   r   r   r   strr~   r   r   intra   r   r   r   r%   r#   r   r   a  s`    F3 F $ LL$  LL$  	$ 
 $  
$  $ r%   r   c                       \ rS rSrSr  SS\S\R                  S\R                  S\S\	4
S jjr
\R                  " 5       S	\R                  S
\R                  S\S\R                  4S j5       rSrg)ImportanceRaySampleri  zh
Given the initial estimate of densities, this samples more from regions/bins expected to have objects.
rM   rN   rX   	blur_poolalphac                     Xl         UR                  5       R                  5       U l        UR                  5       R                  5       U l        X@l        XPl        g)a5  
Args:
    volume_range: the range in which a ray intersects the given volume.
    ts: earlier samples from the coarse rendering step
    weights: discretized version of density * transmittance
    blur_pool: if true, use 2-tap max + 2-tap blur filter from mip-NeRF.
    alpha: small value to add to weights.
N)rM   clonedetachrN   rX   r   r   )r   rM   rN   rX   r   r   s         r#   r~   ImportanceRaySampler.__init__  s@      )((*##%}}--/"
r%   r   r   r   r   c           	         U R                   R                  U R                  5      u  pEnU R                  R                  GtpxpU R                  n
U R
                  (       ar  [        R                  " U
SSS2SS24   XSSS2SS24   /SS9n[        R                  " USSS2SS24   USSS2SS24   5      nSUSSS2SS24   USSS2SS24   -   -  n
XR                  -   n
XR                  SSS	9-  n[        X5      nUR                  U/UQUPSP7:X  d   eUS
:  R                  5       (       a  X:  R                  5       (       d   e[        R                  " UR                  UR                  S9n[        R                  " USU5      n[        R                  " USU5      nUUU-
  U-  -   n[        R                   " USS9R"                  nU$ )r   .Nr   r   rH   r   r   Tr   r   r   )rM   rJ   rN   r   rX   r   r   r.   r   r   rL   r$   allr   r   gathersortr   )r   r   r   r   r   r   rQ   r   r   n_coarse_samplesrX   paddedmaxesr   r"   r   lower_upper_rN   s                      r#   r   ImportanceRaySampler.sample  s    ++55dgg>a26''--/
,,,>>YYRaR
 3Wc23PQk>RSY[\FMM&crc1"5vc12qj7IJEU3Q;/%QR
2CCDGJJ&D99#)zzj?5?)?Q????	  d&=%B%B%D%DDDDJJt{{;eR.eR.v&00ZZ#**	r%   )r   r   rN   rM   rX   N)Fgh㈵>)r   r   r   r   r   r   r   r   boolra   r~   no_gradr   r   r   r   r%   r#   r   r     s      ! LL 	
  , ]]_ 5<< C ELL  r%   r   c                   ~    \ rS rSr% Sr\R                  \S'   \R                  \S'   \\	\R                  4   \S'   Sr
g)MeshDecoderOutputi  aT  
A 3D triangle mesh with optional data at the vertices and faces.

Args:
    verts (`torch.Tensor` of shape `(N, 3)`):
        array of vertext coordinates
    faces (`torch.Tensor` of shape `(N, 3)`):
        array of triangles, pointing to indices in verts.
    vertext_channels (Dict):
        vertext coordinates for each color channel
vertsfacesvertex_channelsr   N)r   r   r   r   r   r   r   r   r   r   r   r   r%   r#   r   r     s1    
 <<<<#u||+,,r%   r   c                   ~   ^  \ rS rSrSrU 4S jrS\R                  S\R                  S\R                  4S jrSr	U =r
$ )	MeshDecoderi  zT
Construct meshes from Signed distance functions (SDFs) using marching cubes method
c                    > [         TU ]  5         [        R                  " SSS[        R                  S9n[        R                  " SS[        R
                  S9nU R                  SU5        U R                  SU5        g )N      r   r{   casesmasks)r}   r~   r   zeroslongr   r   )r   r  r  r   s      r#   r~   MeshDecoder.__init__  s\    CAUZZ8C%**5We,We,r%   field	min_pointsizec           
      
   [        UR                  5      S:X  d   S5       eUR                  nU R                  R	                  U5      nU R
                  R	                  U5      nUR	                  U5      nUR	                  U5      nUR                  n[        R                  " U5      R	                  U5      nUS:  R	                  [        R                  5      n	U	SS2SS2SS24   U	SS2SS2SS24   S-  -  n	U	SS2SS2SS24   U	SS2SS2SS24   S-  -  n	U	SS2SS2SS24   U	SS2SS2SS24   S-  -  n	[        R                  " / UQSP7XAR                  S	.6n
[        R                  " US   XAR                  S	9SS2SS4   U
[        US   5      SS2SS2S4'   [        R                  " US   XAR                  S	9SS2S4   U
SS2[        US   5      SS2S4'   [        R                  " US   XAR                  S	9U
SS2SS2[        US   5      S4'   [        R                  " U
SS U
SS -   S-  R                  SS5      U
SS2SS24   U
SS2SS24   -   S-  R                  SS5      U
SS2SS2SS24   U
SS2SS2SS24   -   S-  R                  SS5      /SS
9n[        R                  " US   S-
  US   S-
  US   S-
  SU[        R                   S	9n[        R                  " US   S-
  US9SS2SS4   U[        US   S-
  5      SS2SS2S4'   [        R                  " US   S-
  US9SS2S4   USS2[        US   S-
  5      SS2S4'   [        R                  " US   S-
  US9USS2SS2[        US   S-
  5      S4'   UR                  SS5      n[#        X5      nU	R                  S5      R!                  5       nX_   nXo   n[        R$                  " USUR                  UR                  S   S5      5      R                  UR                  5      nUR                  SS5      UR                  S5         n[        R&                  " UR)                  S5      5      nUU   n[        R                  " [        U5      U[        R                   S	9n[        R                  " [        U5      U[        R                   S	9UU'   [        R$                  " USUR)                  S5      5      R                  UR                  5      n[        R*                  " U5      R	                  [        R                   5      n[        R,                  " U5      R	                  [        R                   5      nUUSS2S4   USS2S4   USS2S4   4   nUUSS2S4   USS2S4   USS2S4   4   nUR/                  5       US-
  -  U-  U-   nUR/                  5       US-
  -  U-  U-   nUUU-
  -  SS2S4   nUU-  SU-
  U-  -   n[1        UUSS9$ )a	  
For a signed distance field, produce a mesh using marching cubes.

:param field: a 3D tensor of field values, where negative values correspond
            to the outside of the shape. The dimensions correspond to the x, y, and z directions, respectively.
:param min_point: a tensor of shape [3] containing the point corresponding
                to (0, 0, 0) in the field.
:param size: a tensor of shape [3] containing the per-axis distance from the
            (0, 0, 0) field corner and the (-1, -1, -1) field corner.
r   zinput must be a 3D scalar fieldr   Nr   r   r]      r   r*   r   r   )r   r   r   )r   r   r   r  r   r  r   r   uint8emptyr*   r,   ranger.   r-   r  r  rv   r   uniquer   floorceilra   r   ) r   r  r  r  devr  r  rd   grid_size_tensorbitmaskscorner_coordsedge_midpointscube_indicesrp   edge_indicesflat_bitmasks
local_trislocal_masksglobal_trisselected_trisused_vertex_indicesused_edge_midpointsold_index_to_new_indexr   v1v2s1s2p1p2tr   s                                    r#   r   MeshDecoder.forward  s    5;;1$G&GG$ll

c"

c"LL%	wws|KK	 <<	255d; AI>>%++.CRCAI&(12q!8*<*ABAssAI&(1ab!8*<*ABAq#2#I&(1a8*<*AB QYQQ#[[Q6;ll9Q<X[cncn6otTM7
eIaL)1a23 7<ll9Q<X[cncn6otG7
ay|,a23 7<ll9Q<X[cncn6oaE)A,/23 $}QR'88A=FFr1M3B3'-12*>>!CLLRQRS1crc	*]1a8-DDIRRSUWXY
 
 {{aL1ilQ.	!q0@!CW\WaWa
 :?iPQlUVFV_b9cdegkmqdq9rU9Q<!+,aA569>iPQlUVFV_b9cdegkdk9lQilQ./A569>iPQlUVFV_b9cQ51!12A56(00Q7 11BN !((,113)
*ll<J4F4FzGWGWXYGZ\^4_`hh
 $++B2;3F3Fr3JK $ll=+=+=b+AB,-@A!&S-@TYT^T^!_6;ll#$S

7
23
 3Q8J8J28NOWWXeXkXkl [[,-00<ZZ+,//

;2ad8R1Xr!Q$x/02ad8R1Xr!Q$x/0hhj,q01T9IEhhj,q01T9IE 27^QW%B!a%2% uE4PPr%   r   )r   r   r   r   r   r~   r   r   r   r   r   r   s   @r#   r   r     sB    -dQU\\ dQell dQ%,, dQ dQr%   r   c                       \ rS rSr% \R
                  \S'   \R
                  \S'   \R
                  \S'   \R
                  \S'   Srg)MLPNeRFModelOutputiY  rO   signed_distancerP   rN   r   N)r   r   r   r   r   r   r   r   r   r%   r#   r*  r*  Y  s*    \\\\!llr%   r*  c                   n   ^  \ rS rSr\     SS\S\S\S\S\4
U 4S jjj5       rS rS	S
S.S jr	Sr
U =r$ )MLPNeRSTFModelia  d_hiddenn_outputn_hidden_layersact_fninsert_direction_atc           
      n  > [         TU ]  5         [        R                  " SS5      n[	        US9R
                  S   n[        US9R
                  S   nU/U-  n	U/U	-   n
X/-   nUb  X==   U-  ss'   [        R                  " [        X5       VVs/ s H  u  p[        R                  " X5      PM     snn5      U l        US:X  a	  S U l        O[        SU 35      e[        R                  U l        [        R                  R                   R"                  U l        [        R&                  U l        g s  snnf )Nr   r   r9   r   swishc                 .    [         R                  " U 5      $ r   )FsilurE   s    r#   <lambda>)MLPNeRSTFModel.__init__.<locals>.<lambda>  s    q	r%   z Unsupported activation function )r}   r~   r   eyer;   r   r@   r   
ModuleListzipLinearmlp
activation
ValueErrortanhsdf_activation
functionalreludensity_activationsigmoidchannel_activation)r   r.  r/  r0  r1  r2  dummyd_posenc_posd_posenc_dir
mlp_widthsinput_widthsoutput_widthsd_ind_outr   s                 r#   r~   MLPNeRSTFModel.__init__b  s    	
 		!Q&6<<R@'7==bAZ/1
$~
2"Z/*-=-==CP\Ll!mLl[T"))D"8Ll!mnW 2DO?xHII#jj"'(("5"5":":"'-- "ns   "D1
c           
          SSSSSSS.nUR                  5        VVVs0 s H  u  nu  pEX1SXE24   _M     nnnnU$ s  snnnf )	N)r   r   )r   r]   r   )r      )rR  	   )rS     )sdfdensity_coarsedensity_finestfnerf_coarse	nerf_fine.)items)r   outputh_mapkstartendmapped_outputs          r#   map_indices_to_keys"MLPNeRSTFModel.map_indices_to_keys  sY    $"! 
 KP++-X-q,53	>22-X Ys   ;coarsenerf)
nerf_levelrendering_modec                ^   [        U5      nUnS n[        U R                  5       Hx  u  pXR                  R                  :X  a!  Un[        XS9n[        R                  " Xk/SS9nU
" U5      nUnU	[        U R                  5      S-
  :  d  Mg  U R                  U5      nMz     UnUc  UnU R                  U5      nUS:X  a  US   nOUS   nUS:X  a  US:X  a  US	   nOUS
   nOUS:X  a  US   nU R                  U5      nU R                  US   5      nU R                  W5      n[        UUUUS9$ )N)r?   r   r   r   rd  rV  rW  re  rY  rZ  rX  rU  )rO   r+  rP   rN   )r;   	enumerater>  configr2  r@   r   r.   r   r?  rb  rE  rB  rG  r*  )r   r:   r?   rN   rf  rg  hh_preacth_directionlessilayerh_directionh_finalr?  	h_density
h_channelsrO   r+  rP   s                      r#   r   MLPNeRSTFModel.forward  sM   H%!$((+HAKK333"*.xMIIq.B7aAH3txx=1$$OOA& , "&O--g6
!"#34I">2IV#X%'6
'4
u$#E*J)))4--j.?@**:6 "'?]ejlmmr%   )r?  rG  rE  r>  rB  )r   rT  rR  r4  r
  )r   r   r   r   r
   r   r   r~   rb  r   r   r   r   s   @r#   r-  r-  a  sq      #$#0#0 #0 	#0
 #0 !#0 #0J >FV\ +n +nr%   r-  c                   r   ^  \ rS rSrS\S\S\4U 4S jjrS\R                  S\R                  4S jrS	r	U =r
$ )
ChannelsProji  vectorsrP   d_latentc                   > [         TU ]  5         [        R                  " X1U-  5      U l        [        R
                  " U5      U l        X0l        Xl        X l	        g r   )
r}   r~   r   r=  proj	LayerNormnormrx  rw  rP   )r   rw  rP   rx  r   s       r#   r~   ChannelsProj.__init__  sD     	IIh((:;	LL*	  r%   r&   r   c                 b   UnU R                   R                  R                  U R                  U R                  U R
                  5      nU R                   R                  R                  SU R                  U R                  5      n[        R                  " SX#5      nU R                  U5      nXT-   nU$ )Nr   zbvd,vcd->bvc)
rz  weightr   rw  rP   rx  biasr   einsumr|  )r   r&   x_bvdw_vcdb_vcrk  s         r#   r   ChannelsProj.forward  s~    		  %%dllDMM4==Qyy~~""1dllDMMBLL6IIaLHr%   )rP   rx  r|  rz  rw  )r   r   r   r   r   r~   r   r   r   r   r   r   s   @r#   rv  rv    sF    ! ! 	!
 ! %,,  r%   rv  c                      ^  \ rS rSrSr\SSSS.S\\   S\\\      S	\4U 4S
 jjj5       r	S\
R                  4S jrSrU =r$ )ShapEParamsProjModeli  z
project the latent representation of a 3D asset to obtain weights of a multi-layer perceptron (MLP).

For more details, see the original paper:
znerstf.mlp.0.weightznerstf.mlp.1.weightznerstf.mlp.2.weightznerstf.mlp.3.weight)r   ]   r   r   r  r     param_namesparam_shapesrx  r  r  rx  c                  > [         TU ]  5         [        U5      [        U5      :w  a  [        S5      e[        R
                  " 0 5      U l        [        X5       H)  u  nu  pV[        UUUS9U R                  [        U5      '   M+     g )Nz;Must provide same number of `param_names` as `param_shapes`)rw  rP   rx  )
r}   r~   r   r@  r   
ModuleDictprojectionsr<  rv  rF   )r   r  r  rx  r^  rw  rP   r   s          r#   r~   ShapEParamsProjModel.__init__  s|    $ 	 {s<00Z[[==,&)+&D"A"2>!!3D^A./ 'Er%   r&   c                 $   0 nSn[        U R                  R                  U R                  R                  5       HS  u  pEUu  pgX6-   nUS S 2X824   n	U R                  [        U5         " U	5      R                  " [        U5      /UQ76 X$'   UnMU     U$ r   )r<  rj  r  r  r  rF   r-   r   )
r   r&   outr_  r^  r   rw  rQ   r`  r  s
             r#   r   ShapEParamsProjModel.forward  s    DKK33T[[5M5MNHAJG/CalOE%%nQ&78?GGAWQVWCFE O 
r%   )r  )r   r   r   r   r   r
   r   r   r   r~   r   r   r   r   r   r   s   @r#   r  r    sk     #
+
  3Z E#J'  <	 	 	r%   r  c                   J  ^  \ rS rSr\SSSSSSSS	S
S.	S\\   S\\\      S\S\S\S\S\S\S\\   4U 4S jjj5       r	\
R                  " 5       SS j5       r\
R                  " 5           S S\S\4S jj5       r\
R                  " 5          S!S\S\S\4S jj5       rSrU =r$ )"ShapERendereri  r  r  r  r   rT  rR  r4  r
  )r   r   r   )	r  r  rx  r.  r/  r0  r1  r2  r|   r  r  rx  r.  r/  r0  r1  r2  r|   c       	            > [         T
U ]  5         [        UUUS9U l        [	        XEXgU5      U l        [        U	SS9U l        [        / SQ/ SQS9U l	        [        5       U l        g )Nr  r   )r|   r   )rI   rI   rI   )      r  r  )rb   r^   )r}   r~   r  params_projr-  r>  rx   voidr   rc   r   mesh_decoder)r   r  r  rx  r.  r/  r0  r1  r2  r|   r   s             r#   r~   ShapERenderer.__init__  s`    8 	/#%

 "(oObc!ZuM	'K]^'Mr%   c                    USSSS24   USSSS24   pvU R                   R                  XgSS9nUR                  UR                  UR                  U5      n	U	R                  UR                  5      n	Ub=  [        R                  " [        R                  " XR                  /SS9SS9R                  n	UR                  R                  GtpnU	R                  Gtpn[        R                  " UR                  S5      U
/UQSP5      nUR                  S5      U	U-  -   nUR                  U R                  R                  5      nUR                  U R                  R                  5      nU(       a  UOSnU R                  UUU	Uc  S	OS
S9n[!        UUR                  UR"                  UR$                  5      u  nnn[        R&                  " UR(                  U[        R*                  " U5      5      n[        R&                  " UR(                  U[        R,                  " U5      5      nUUU R/                  U5      -  -   n[1        UUR                  US9nUUU4$ )a  
Perform volumetric rendering over a partition of possible t's in the union of rendering volumes (written below
with some abuse of notations)

    C(r) := sum(
        transmittance(t[i]) * integrate(
            lambda t: density(t) * channels(t) * transmittance(t), [t[i], t[i + 1]],
        ) for i in range(len(parts))
    ) + transmittance(t[-1]) * void_model(t[-1]).channels

where

1) transmittance(s) := exp(-integrate(density, [t[0], s])) calculates the probability of light passing through
the volume specified by [t[0], s]. (transmittance of 1 means light can pass freely) 2) density and channels are
obtained by evaluating the appropriate part.model at time t. 3) [t[i], t[i + 1]] is defined as the range of t
where the ray intersects (parts[i].volume \ union(part.volume for part in parts[:i])) at the surface of the
shell (if bounded). If the ray does not intersect, the integral over this segment is evaluated as 0 and
transmittance(t[i + 1]) := transmittance(t[i]). 4) The last term is integration to infinity (e.g. [t[-1],
math.inf]) that is evaluated by the void_model (i.e. we consider this space to be empty).

Args:
    rays: [batch_size x ... x 2 x 3] origin and direction. sampler: disjoint volume integrals. n_samples:
    number of ts to sample. prev_model_outputs: model outputs from the previous rendering step, including

:return: A tuple of
    - `channels`
    - A importance samplers for additional fine-grained rendering
    - raw model output
.r   Nr   )r   rH   r   r   rd  fine)r:   r?   rN   rf  )rN   rX   )rc   r   r   r   r   r   r*   r   r   r.   rN   r   r   r   r   r>  rY   rO   rP   rn   r   r   r>   r  r   )r   rayssamplerr   prev_model_outrender_with_directionr   r?   vrangerN   r   _shape_t0_dimrQ   ts_shape_ts_dim
directions	positionsoptional_directions	model_outrP   rX   rU   weighted_samplers                           r#   render_raysShapERenderer.render_rays9  s   > !aOT#q!)_	
 &&v4&H^^FIIvyy)<UU4::% EIIr+<+<&=2FBOVVB'-yy$
W "g ''	(;(;B(?*A[xA[YZA[\
$$R(2
?:	]]488>>2
LL0	,AjtHH)#1#9xv	  
	 ,=ILL)"3"3Y5G5G,
('=
 F$6$6uWdGef;;v118U=M=Mh=WXmdii.???/9<<QXY)944r%   r  ray_batch_sizec                    U R                  U5      nU R                  R                  5       R                  5        HD  u  pSU 3UR	                  5       ;   d  M  U	R                  USU 3   R                  S5      5        MF     [        U5      n
U
R                  nUR                  U5      nUR                  S   U-  n[        5       n/ n[        U5       HS  nUS S 2X-  US-   U-  24   nU R                  UX5      u  nnnU R                  UUUUS9u  n  nUR                  U5        MU     [        R                   " USS9nUR"                  " / U
R                  QU
R$                  PU
R&                  PSP76 R                  S5      nU$ )Nnerstf.r   r   )r  r   r   )r  r>  
state_dictr[  keyscopy_squeezer   camera_raysr   r   r   r  r  appendr   r.   r   heightwidth)r   latentsr   r  r  r   n_fine_samplesprojected_paramsnameparamcamerar  	n_batchescoarse_samplerimagesidx
rays_batchrQ   fine_samplercoarse_model_outrP   s                        r#   decode_to_imageShapERenderer.decode_to_image  s     ++G4  88..0668KD#3#8#8#::,wtf-=>FFqIJ 9
 $D)!!wwvJJqM^3	-/#Ca!5qN8R!RRSJ 150@0@^0n-A|-!--L.IY . NHa MM(# $ 6q)LfllLFMML6<<LLTTUVWr%   rd   query_batch_sizetexture_channelsc           
         U R                  U5      nU R                  R                  5       R                  5        HD  u  pxSU 3UR	                  5       ;   d  M  UR                  USU 3   R                  S5      5        MF     [        U R                  U5      n	U	S    R                  SSS5      R                  X R                  R                  S9n
/ n[        SU
R                  S   U5       H>  nU
S S 2XU-   24   nU R                  US S SSS9nUR                  UR                  5        M@     [         R"                  " USS9nUR%                  5       n['        UR                  5      S	:X  a  UR                  S
   S:X  d   SUR                   35       eUR(                  " S/U/S	-  Q76 n[         R*                  " SUS-   US-   US-   UR,                  UR                  S9nUR/                  S5        XS S 2SS
2SS
2SS
24'   Un/ n/ nU Hw  nU R1                  UU R                  R2                  U R                  R4                  U R                  R2                  -
  5      nUR                  S5        UR                  U5        My     [         R6                  " UUR,                  S9n[9        S U 5       5      n[         R:                  " U Vs/ s H=  nUR<                  [         R>                  " SU5      ['        UR<                  5      -     PM?     snSS9nUR                  X R                  R                  S9n/ n[        SUR                  S   U5       H>  nUS S 2XU-   24   nU R                  US S SSS9nUR                  UR@                  5        M@     [         R"                  " USS9n[C        U5      nUR%                  5       n['        UR                  5      S	:X  a  UR                  S
   ['        U5      :X  d   SUR                   35       e[E        UU5       HG  u  nnUS ['        UR<                  5       n[G        [E        UURI                  S
5      5      5      Ul%        MI     US   $ s  snf )Nr  r   r   r  r  rX  )r:   r?   rN   rf  rg  r   r   r   z9expected [meta_batch x inner_batch] SDF results, but got r]   r  Tr   c              3   L   #    U  H  n[        UR                  5      v   M     g 7fr   )r   r   ).0ms     r#   	<genexpr>/ShapERenderer.decode_to_mesh.<locals>.<genexpr>  s     <A3qww<<s   "$zNexpected [meta_batch x inner_batch x texture_channels] field results, but got )&r  r>  r  r[  r  r  r  rj   rc   repeatr   r*   r  r   r  r+  r   r.   ra   r   r-   r  r   fill_r  r^   rb   r   r   r`   r   r,   rP   ro   r<  dictunbindr   )r   r  r   rd   r  r  r  r  r  query_pointsquery_positionsfieldsr  query_batchr  	full_grid
raw_meshes	mesh_maskr  raw_meshmax_verticesr  texture_query_positionstexturestexture_model_outtextures                             r#   decode_to_meshShapERenderer.decode_to_mesh  s>     ++G4  88..0668KD#3#8#8#::,wtf-=>FFqIJ 9 +4;;	B&t,33Aq!<??vU]U]UcUc?dO11!46FGC)!S9I3I-I*IJK$&af ! I MM)334 H 6q)6<< A%&,,r*:a*? 	
G~V	
? 6i[1_6 KKMMM==,,
	 	)/!QrT1R42%& 
	E((0D0DdkkFZFZ]a]h]h]q]qFqrHT"h' 
 LL6==A	<<< #(++LVWJqQWWU\\!\2S\ABJW#
 #:"<"<FRZRZR`R`"<"a399!<>NOC1!SAQ;Q5Q2QRK $$&af !) ! OO-667 P 99X1-*84>># 8>>"a'HNN2,>#FVBW,W 	
\]e]k]k\lm	
W j(3JAwnAGG-G $S)97>>";M%N OA 4 !}= Xs   AQ()r  r>  r  r  rc   )NF)@      r     )r  r  )RGB)r   r   r   r   r
   r   r   r   ra   r~   r   r   r  r  r  r   r   r   s   @r#   r  r    sF   #
+
  #$$
+%* 3Z%* E#J'%* %*  !%*" #%*$ %%*& '%*( !)%** %L+%* %*N ]]_L5 L5\ ]]_
 "* 	*
 * *X ]]_
  $"1d 	d
 d  d dr%   r  )r   r7   r   )0r/   dataclassesr   typingr   r   r   numpyr   r   torch.nn.functionalr   rC  r6  configuration_utilsr	   r
   modelsr   utilsr   r  r   r   r   r$   r5   r;   r@   r   rF   rY   rj   ro   rv   Modulerx   r   r   r   r   r   r   r*  r-  rv  r  r  r   r%   r#   <module>r     s    ! ( (      B    &FELL FS FU\\ F.'5<< '# 'C ' '"8< c  c  ,DhNu|| NO||OS#s]#OdBII , # # #@QB		 QBh1 299 1 h:299 :z -
 - -$qQ")) qQh   `nZ `nF299 4/:{ /dJJ Jr%   