
    bi              	       (   d dl Z d dlmZ d dlmZmZmZ d dlZd dl	Z	d dl
mc mZ d dl	mZ ddlmZmZ ddlmZ ddlmZ d	d
lmZ de	j.                  dede	j.                  fdZd5de	j.                  dedede	j.                  fdZd Zd6dZdedefdZd Zd Z de	j.                  fdZ!de	j.                  deeeef   fdZ" G d dejF                        Z$e G d d              Z% G d! d"ejF                        Z& G d# d$ejF                        Z' G d% d&ejF                        Z(e G d' d(e             Z) G d) d*ejF                        Z*e G d+ d,e             Z+ G d- d.ee      Z, G d/ d0ejF                        Z- G d1 d2ee      Z. G d3 d4ee      Z/y)7    N)	dataclass)DictOptionalTuple)nn   )ConfigMixinregister_to_config)
ModelMixin)
BaseOutput   )create_pan_cameraspmf	n_samplesreturnc                 V   | j                   ^ }}}|dk(  sJ t        j                  | j                  d|      d      }t        j                  |t        j
                  |j                   d   ||j                              } |j                  g ||d j                  d|dz
        S )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/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/shap_e/renderer.py
sample_pmfr$      s     &)YY"UL(q==
,,sxxL1q
9Cc5::ciilIcjj#YZD499*e*Y**00L14DEE    xmin_degmax_degc                    ||k(  r| S dt        j                  ||| j                  | j                        z  }| j                  ^ }} | j                  dd|      |j                  ddd      z  j
                  g |d }|j                  d   |||z
  z  k(  sJ t        j                  ||t        j                  dz  z   gd      j                         }t        j                  | |gd      S )z~
    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''KUC	@!))B3
&++aQ"7
7	@	@	L%	L	LB88B<3'G"34444
))Rdggm+,2
6
:
:
<C99aX2&&r%   c                     t        | dd      S )Nr      r'   r(   )r5   positions    r#   encode_positionr;   F   s    xB77r%   c                 d    |!t        j                  t        | dd            S t        |dd      S )Nr      r8   )r   
zeros_liker5   )r:   	directions     r#   encode_directionr@   J   s1    Ha KLL9a;;r%   c                 &    | j                  dd      S )N.__)replacer&   s    r#   _sanitize_namerF   Q   s    99S$r%   c                    | j                  |      \  }}}||z  }t        j                  |d      }t        j                  |ddddf          }dt        j                  |       z
  }	t        j                  t        j                  t        j
                  |dddddf         |dddddf    gd            }
|	|
z  }t        j                  ||z  d      }|||fS )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 rkG yyG+4HWm++r%   c                    t        j                  |dz  | j                  j                        }||z  }t        j                  ||d      |z  }t        j                  ||dz  d      |z  }t        j
                  |||gd      }|j                         |dz
  z  | j                  | j                  z
  z  | j                  z   S )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	9	B	7IW	=		IB	7IqL	@9	LB{{BB<Q/HNN	A.6??V__3TUX^XgXgggr%   uc                 N    t        j                  | dk  | dz  | dz   dz  dz        S )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                    |d   dz
  |d   z  |d   z  }|}|d   |d   dz
  z  |d   z  }||z   }t        j                  | d d df   |d   z  |d   z  | d d df   |d   z  z   | d d df   z   | d d df   |d   z  |d   z  | d d df   dz   |d   z  z   | d d df   z   | d d df   |d   z  |d   z  | d d df   |d   z  z   | d d df   z   dz   | d d df   |d   z  |d   z  | d d df   dz   |d   z  z   | d d df   z   dz   || d d df   |d   dz
  z  |d   z  z   | d d df   |d   z  z   | d d df   z   || d d df   dz   |d   dz
  z  |d   z  z   | d d df   |d   z  z   | d d df   z   || d d df   |d   dz
  z  |d   z  z   | d d df   |d   z  z   | d d df   z   dz   || d d df   dz   |d   dz
  z  |d   z  z   | d d df   |d   z  z   | d d df   z   dz   || d d df   |d   z  |d   dz
  z  z   | d d df   |d   dz
  z  z   | d d df   z   || d d df   dz   |d   z  |d   dz
  z  z   | d d df   |d   dz
  z  z   | d d df   z   || d d df   |d   z  |d   dz
  z  z   | d d df   dz   |d   dz
  z  z   | d d df   z   || d d df   dz   |d   z  |d   dz
  z  z   | d d df   dz   |d   dz
  z  z   | d d df   z   gd      S )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                   *     e Zd ZdZd fd	Zd Z xZS )VoidNeRFModelz`
    Implements the default empty space model where all queries are rendered as background.
    c                     t         |           t        j                  t	        j
                  t        j                  |            j                  t        j                        |z        }| j                  d|       y )Nr*   
background)super__init__r   	Parameterr   
from_numpynparraytofloat32register_buffer)selfr{   channel_scale	__class__s      r#   r}   zVoidNeRFModel.__init__   s[    \\%"2"2288J3G"H"K"KRWR_R_"K"`cp"pq
\:6r%   c                 *   | j                   d    j                  |j                        }|j                  d d }dgt	        |      dz
  z  }|j                  d   }t        j                   |j                  |j                  d   g|| g ||      }|S )Nr   r   r   )r{   r   r   r   lenr   broadcast_tor   )r   r:   r{   r   ones
n_channelss         r#   forwardzVoidNeRFModel.forward   s    __T*--hoo>
s#sc%j1n%%%b)
''

8H8H8K(_d(_T^(_auchaujtauv
r%   )     o@)__name__
__module____qualname____doc__r}   r   __classcell__r   s   @r#   rx   rx      s    7r%   rx   c                   t    e Zd ZU ej                  ed<   ej                  ed<   ej                  ed<   d Zd Zy)VolumeRanget0t1intersectedc                     | j                   j                  | j                  j                  cxk(  r| j                  j                  k(  sJ  J y N)r   r   r   r   )r   s    r#   __post_init__zVolumeRange.__post_init__   s7    ww}}G1A1A1G1GGGGGGr%   c                 v   |dddddf   |dddddf   z   dz  }t        j                  | j                  ddddf   |gd      }t        j                  || j                  ddddf   gd      }||z
  }|j                  |j                  cxk(  r |j                  cxk(  r|j                  k(  sJ  J |||fS )ar  
        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   z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eU""r%   N)r   r   r   r   Tensor__annotations__r   rJ    r%   r#   r   r      s+    H#r%   r   c                        e Zd ZdZddddedef fdZ	 	 ddej                  d	ej                  d
eej                     fdZ	 xZ
S )BoundingBoxVolumezH
    Axis-aligned bounding box defined by the two opposite corners.
            MbP?)min_distmin_t_ranger   r   c                X   t         |           || _        || _        t	        j
                  |      | _        t	        j
                  |      | _        t	        j                  | j                  | j                  g      | _	        | j                  j                  dk(  sJ |dk\  sJ |dkD  sJ y)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}   zBoundingBoxVolume.__init__  s     	 &X.X.KK >?	yy&(((3S   r%   originr?   t0_lowerc                    |j                   ^}}}dgt        |      z  } | j                  j                  dg|dd j	                  |j
                        }	dd}
 |
|	|ddddf   z
  |ddddf   |      }|j                  d	      j                  j                  d
d      j                  j                  | j                        }|j                  d	      j                  j                  d
d      j                  }|j                   |j                   cxk(  r|g|dk(  sJ  J |1|j                   |j                   k(  sJ t        j                  ||      }|| j                  z   |k  }t        j                  ||t        j                  |            }t        j                  ||t        j                   |            }t#        |||      S )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                 H    | t        j                  |dk  ||z
  ||z         z  S Nr   rm   )abepsilons      r#   _safe_dividez1BoundingBoxVolume.intersect.<locals>._safe_divideE  s&    u{{1q5!g+q7{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#   	intersectzBoundingBoxVolume.intersect,  s   * !'
UAsSZtyy~~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~~---r8,B4+++b0[[b%*:*:2*>?[[b%//"*=>bR[AAr%   )Nr   )r   r   r   r   ra   r}   r   r   r   r   r   r   s   @r#   r   r     sg     !!
 ! !< ,02B2B <<2B 5<<(	2Br%   r   c                   z    e Zd ZdZddefdZ	 ddej                  dej                  dede	dej                  f
d	Z
y
)StratifiedRaySamplerz_
    Instead of fixed intervals, a sample is drawn uniformly at random from each interval.
    
depth_modec                 2    || _         | j                   dv sJ y)z
        :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}   zStratifiedRaySampler.__init__f  s    
 %"EEEEr%   r   r   r   r   r   c                    dgt        |j                        dz
  z  } t        j                  dd|      j                  g || j                  |j                        j                  |j                        }| j                  dk(  r|d|z
  z  ||z  z   }n| j                  dk(  rW|j                  |      j                         d|z
  z  |j                  |      j                         |z  z   j                         }nD| j                  dk(  r5dd|j                  |      z  d|z
  z  d|j                  |      z  |z  z   z  }d|ddd	f   |dd	d
f   z   z  }t        j                  ||gd
      }t        j                  ||gd
      }	t        j                  d       t        j                  |      }
|	||	z
  |
z  z   }|j                  d
      S )  
        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#   samplezStratifiedRaySampler.samplen  s    sc"((ma'(1U^^Aq),11C4CCFFrxxPSSTVT]T]^??h&sRx27*B__+((7#'')S2X6'9J9N9N9PSU9UUZZ\B__
* bhhw//38<sRXXgEV?VY[?[[\BbabkBsCRCxL01		4*"-		2t*"-!$eemv--||Br%   N)r   )r   )r   r   r   r   strr}   r   r   intra   r   r   r%   r#   r   r   a  s[    F3 F $ LL$  LL$  	$ 
 $  
$ r%   r   c                       e Zd ZdZ	 	 ddedej                  dej                  dedef
dZ	 ej                         dej                  d	ej                  d
edej                  fd       Zy)ImportanceRaySamplerzp
    Given the initial estimate of densities, this samples more from regions/bins expected to have objects.
    rM   rN   rX   	blur_poolalphac                     || _         |j                         j                         | _        |j                         j                         | _        || _        || _        y)am  
        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}   zImportanceRaySampler.__init__  sF      )((*##%}}--/"
r%   r   r   r   r   c           	         | j                   j                  | j                        \  }}}| j                  j                  ^}}}	}| j                  }
| j
                  rtt        j                  |
dddddf   |
|
dddddf   gd      }t        j                  |dddddf   |dddddf         }d|dddddf   |dddddf   z   z  }
|
| j                  z   }
|
|
j                  dd	      z  }t        ||      }|j                  |g||dk(  sJ |d
k\  j                         r||	k  j                         sJ t        j                  |j                  |j                        }t        j                  |d|      }t        j                  |d|      }|||z
  |z  z   }t        j                   |d      j"                  }|S )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   zImportanceRaySampler.sample  s    ++55dgg>ua26''--/
U,a,,>>YYRaR
 3Wgc23PQk>RSY[\FMM&crc1"5vc12qj7IJEU3Q;/%QR
2CCDGDJJ&D99#y)zzj?5?)?Q????	 d-=&=%B%B%DDDDJJt{{;eR.eR.v&00ZZ#**	r%   N)Fgh㈵>)r   r   r   r   r   r   r   boolra   r}   no_gradr   r   r   r%   r#   r   r     s      ! LL 	
  , U]]_ 5<< C ELL  r%   r   c                   v    e Zd ZU dZej
                  ed<   ej
                  ed<   eeej
                  f   ed<   y)MeshDecoderOutputax  
    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_channelsN)	r   r   r   r   r   r   r   r   r   r   r%   r#   r   r     s1    
 <<<<#u||+,,r%   r   c                   r     e Zd ZdZ fdZdej                  dej                  dej                  fdZ xZS )MeshDecoderz\
    Construct meshes from Signed distance functions (SDFs) using marching cubes method
    c                    t         |           t        j                  dddt        j                        }t        j                  ddt        j
                        }| j                  d|       | j                  d|       y )N      r   rz   casesmasks)r|   r}   r   zeroslongr   r   )r   r   r   r   s      r#   r}   zMeshDecoder.__init__  s\    CAUZZ8C%**5We,We,r%   field	min_pointsizec           
      6   t        |j                        dk(  sJ d       |j                  }| j                  j	                  |      }| j
                  j	                  |      }|j	                  |      }|j	                  |      }|j                  }t        j                  |      j	                  |      }|dkD  j	                  t        j                        }	|	ddddddf   |	ddddddf   dz  z  }	|	ddddddf   |	ddddddf   dz  z  }	|	ddddddf   |	ddddddf   dz  z  }	t        j                  g |d||j                  d	}
t        j                  |d   ||j                  	      ddddf   |
t        |d         dddddf<   t        j                  |d   ||j                  	      dddf   |
ddt        |d         dddf<   t        j                  |d   ||j                  	      |
ddddt        |d         df<   t        j                  |
dd |
dd z   dz  j                  dd      |
ddddf   |
ddddf   z   dz  j                  dd      |
ddddddf   |
ddddddf   z   dz  j                  dd      gd
      }t        j                  |d   dz
  |d   dz
  |d   dz
  d|t        j                   	      }t        j                  |d   dz
  |      ddddf   |t        |d   dz
        dddddf<   t        j                  |d   dz
  |      dddf   |ddt        |d   dz
        dddf<   t        j                  |d   dz
  |      |ddddt        |d   dz
        df<   |j                  dd      }t#        ||      }|	j                  d      j!                         }||   }||   }t        j$                  |d|j                  |j                  d   d            j                  |j                        }|j                  dd      |j                  d         }t        j&                  |j)                  d            }||   }t        j                  t        |      |t        j                   	      }t        j                  t        |      |t        j                   	      ||<   t        j$                  |d|j)                  d            j                  |j                        }t        j*                  |      j	                  t        j                         }t        j,                  |      j	                  t        j                         }||dddf   |dddf   |dddf   f   }||dddf   |dddf   |dddf   f   }|j/                         |dz
  z  |z  |z   }|j/                         |dz
  z  |z  |z   }|||z
  z  dddf   }||z  d|z
  |z  z   }t1        ||d      S )aI  
        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   z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#U[[Q6;ll9Q<X[chcncn6otTM7
eIaL)1a23 7<ll9Q<X[chcncn6otG7
ay|,a23 7<ll9Q<X[chcncn6o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 11BIN !((,113=)
M*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   s   @r#   r   r     s;    -dQU\\ dQell dQ%,, dQr%   r   c                       e Zd ZU ej                  ed<   ej                  ed<   ej                  ed<   ej                  ed<   y)MLPNeRFModelOutputrO   signed_distancerP   rN   N)r   r   r   r   r   r   r   r%   r#   r  r  Y  s*    \\\\!llr%   r  c                   ^     e Zd Ze	 	 	 	 	 ddededededef
 fd       Zd Zdd	d
dZ xZ	S )MLPNeRSTFModeld_hiddenn_outputn_hidden_layersact_fninsert_direction_atc           
      t   t         |           t        j                  dd      }t	        |      j
                  d   }t        |      j
                  d   }|g|z  }	|g|	z   }
|	|gz   }||
|xx   |z  cc<   t        j                  t        |
|      D cg c]  \  }}t        j                  ||       c}}      | _        |dk(  r	d | _        nt        d|       t        j                  | _        t        j                  j                   j"                  | _        t        j&                  | _        y c c}}w )Nr   r   r9   r   swishc                 ,    t        j                  |       S r   )FsilurE   s    r#   <lambda>z)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  r  r  r   dummyd_posenc_posd_posenc_dir
mlp_widthsinput_widthsoutput_widthsd_ind_outr   s                 r#   r}   zMLPNeRSTFModel.__init__b  s    	
 		!Q&6<<R@'7==bAZ/1
$~
2"hZ/*,-=-==CP\^kLl!m[T5"))D%"8!mnW 2DO?xHII#jj"'(("5"5":":"'-- "ns    D4
c           
          ddddddd}|j                         D ci c]  \  }\  }}||d||f    }}}}|S c c}}}w )	N)r   r   )r   r]   r   )r      )r>  	   )r?     )sdfdensity_coarsedensity_finestfnerf_coarse	nerf_fine.)items)r   outputh_mapkstartendmapped_outputs          r#   map_indices_to_keysz"MLPNeRSTFModel.map_indices_to_keys  sa    $"! 
 KP++-XXq,5#F3c	>22XX Ys   ;coarsenerf)
nerf_levelrendering_modec                b   t        |      }|}d }t        | j                        D ]}  \  }	}
|	| j                  j                  k(  r(|}t        ||      }t        j                  ||gd      } |
|      }|}|	t        | j                        dz
  k  sm| j                  |      } |}||}| j                  |      }|dk(  r|d   }n|d   }|dk(  r|dk(  r|d	   }n|d
   }n
|dk(  r|d   }| j                  |      }| j                  |d         }| j                        }t        ||||      S )N)r?   r   r   r   rO  rB  rC  rP  rE  rF  rD  rA  )rO   r  rP   rN   )r;   	enumerater+  configr   r@   r   r.   r   r,  rN  r2  r/  r4  r  )r   r:   r?   rN   rQ  rR  hh_preacth_directionlessilayerh_directionh_finalr,  	h_density
h_channelsrO   r  rP   s                      r#   r   zMLPNeRSTFModel.forward  sZ   H%!$((+ 	'HAuDKK333"*.x9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   r@  r>  r"  r   )
r   r   r   r
   r   r   r}   rN  r   r   r   s   @r#   r  r  a  sq      #$#0#0 #0 	#0
 #0 !#0 #0J >FV\ +n +nr%   r  c                   d     e Zd Zdededef fdZdej                  dej                  fdZ xZS )ChannelsProjvectorsrP   d_latentc                    t         |           t        j                  |||z        | _        t        j
                  |      | _        || _        || _        || _	        y r   )
r|   r}   r   r*  proj	LayerNormnormrb  ra  rP   )r   ra  rP   rb  r   s       r#   r}   zChannelsProj.__init__  sL     	IIh((:;	LL*	  r%   r&   r   c                 d   |}| j                   j                  j                  | j                  | j                  | j
                        }| j                   j                  j                  d| j                  | j                        }t        j                  d||      }| j                  |      }||z   }|S )Nr   zbvd,vcd->bvc)
rd  weightr   ra  rP   rb  biasr   einsumrf  )r   r&   x_bvdw_vcdb_vcrV  s         r#   r   zChannelsProj.forward  s    		  %%dllDMM4==Qyy~~""1dllDMMBLL6IIaLHr%   )	r   r   r   r   r}   r   r   r   r   r   s   @r#   r`  r`    sA    ! ! 	!
 ! %,, r%   r`  c                   v     e Zd ZdZedddddee   deee      def fd	       Zd
e	j                  fdZ xZS )ShapEParamsProjModelz
    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   rs  rs     param_namesparam_shapesrb  rv  rw  rb  c                   t         |           t        |      t        |      k7  rt        d      t	        j
                  i       | _        t        ||      D ],  \  }\  }}t        |||      | j                  t        |      <   . y )Nz;Must provide same number of `param_names` as `param_shapes`)ra  rP   rb  )
r|   r}   r   r-  r   
ModuleDictprojectionsr)  r`  rF   )r   rv  rw  rb  rJ  ra  rP   r   s          r#   r}   zShapEParamsProjModel.__init__  s    $ 	 {s<00Z[[==,&)+|&D 	"A"2>!!3D^A./	r%   r&   c                 *   i }d}t        | j                  j                  | j                  j                        D ]W  \  }}|\  }}||z   }|d d ||f   }	  | j                  t        |         |	      j                  t        |      g| ||<   |}Y |S r   )r)  rU  rv  rw  rz  rF   r-   r   )
r   r&   outrK  rJ  r   ra  rQ   rL  rk  s
             r#   r   zShapEParamsProjModel.forward  s    DKK33T[[5M5MN 	HAuJGQ'/CaslOEG8T%%nQ&78?GGAWQVWCFE	 
r%   )r   r   r   r   r
   r   r   r   r}   r   r   r   r   r   s   @r#   ro  ro    sf     #
+
  3Z E#J'  <	 	r%   ro  c                   2    e Zd Zeddddddddd	d
	dee   deee      dededededededee   f fd       Z e	j                         dd       Z e	j                         	 	 	 	 ddedefd       Z e	j                         	 	 	 ddededefd       Z xZS ) ShapERendererrp  rq  rt  r   r@  r>  r"  r   )r   r   r   )	rv  rw  rb  r  r  r  r  r   r{   rv  rw  rb  r  r  r  r  r   r{   c       	             t         
|           t        |||      | _        t	        |||||      | _        t        |	d      | _        t        g dg d      | _	        t               | _        y )Nru  r   )r{   r   )rI   rI   rI   )      r  r  )rb   r^   )r|   r}   ro  params_projr  r+  rx   voidr   rc   r   mesh_decoder)r   rv  rw  rb  r  r  r  r  r   r{   r   s             r#   r}   zShapERenderer.__init__  sd    8 	/#%

 "(HovObc!ZuM	'K]^'Mr%   c                    |ddddf   |ddddf   }}| j                   j                  ||d      }|j                  |j                  |j                  |      }	|	j                  |j                        }	|Bt        j                  t        j                  |	|j                  gd      d      j                  }	|j                  j                  ^}
}}|	j                  ^}}}t        j                  |j                  d      |
g|d      }|j                  d      |	|z  z   }|j                  | j                  j                        }|j                  | j                  j                        }|r|nd}| j                  |||	|d	nd
      }t!        ||j                  |j"                  |j$                        \  }}}t        j&                  |j(                  |t        j*                  |            }t        j&                  |j(                  |t        j,                  |            }||| j/                  |      z  z   }t1        ||j                  |      }|||fS )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   rO  fine)r:   r?   rN   rQ  )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_rayszShapERenderer.render_rays9  s   > !aOT#q!)_	
 &&vy4&H^^FIIvyy)<UU4::% EIIr>+<+<&=2FBOVVB'-yy$
VW "Hg ''	(;(;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                    | j                  |      }| j                  j                         j                         D ]A  \  }}	d| |j	                         v s|	j                  |d|    j                  d             C t        |      }
|
j                  }|j                  |      }|j                  d   |z  }t               }g }t        |      D ]W  }|d d ||z  |dz   |z  f   }| j                  |||      \  }}}| j                  ||||      \  }}}|j                  |       Y t        j                   |d      } |j"                  g |
j                  |
j$                  |
j&                  d j                  d      }|S )Nnerstf.r   r   )r  r   r   )r  r+  
state_dictrG  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_imagezShapERenderer.decode_to_image  s     ++G4  88..0668 	KKD%#3#8#8#::,wtf-=>FFqIJ	K
 $D)!!wwvJJqM^3	-/# 
	$Ca~!5qN8R!RRSJ 150@0@^]m0n-A|-!--L.IY . NHa MM(#
	$ 6q)LfllLFMML6<<LLTTUVWr%   rd   query_batch_sizetexture_channelsc           
         | j                  |      }| j                  j                         j                         D ]A  \  }}d| |j	                         v s|j                  |d|    j                  d             C t        | j                  |      }	|	d    j                  ddd      j                  || j                  j                        }
g }t        d|
j                  d   |      D ]A  }|
d d |||z   f   }| j                  |d d dd      }|j                  |j                         C t!        j"                  |d      }|j%                         }t'        |j                        d	k(  r|j                  d
   dk(  sJ d|j                           |j(                  dg|gd	z   }t!        j*                  d|dz   |dz   |dz   |j,                  |j                        }|j/                  d       ||d d dd
dd
dd
f<   |}g }g }|D ]v  }| j1                  || j                  j2                  | j                  j4                  | j                  j2                  z
        }|j                  d       |j                  |       x t!        j6                  ||j,                        }t9        d |D              }t!        j:                  |D cg c];  }|j<                  t!        j>                  d|      t'        |j<                        z     = c}d      }|j                  || j                  j                        }g }t        d|j                  d   |      D ]A  }|d d |||z   f   }| j                  |d d dd      }|j                  |j@                         C t!        j"                  |d      }tC        |      }|j%                         }t'        |j                        d	k(  r|j                  d
   t'        |      k(  sJ d|j                          tE        ||      D ]F  \  }}|d t'        |j<                         }tG        tE        ||jI                  d
                  |_%        H |d   S c c}w )Nr  r   r   r   r  rD  )r:   r?   rN   rQ  rR  r   r   r   z9expected [meta_batch x inner_batch] SDF results, but got r]   r  Tr   c              3   F   K   | ]  }t        |j                          y wr   )r   r   ).0ms     r#   	<genexpr>z/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  rG  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_meshzShapERenderer.decode_to_mesh  se     ++G4  88..0668 	KKD%#3#8#8#::,wtf-=>FFqIJ	K +4;;	B&t,33Aq!<??vUYU]U]UcUc?dO11!46FG 	5C)!S39I3I-I*IJK$&af ! I MM)334	5 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WqQWWU\\!\2S\ABW#
 #:"<"<FRVRZRZR`R`"<"a399!<>NO 	8C1!S3AQ;Q5Q2QRK $$&af !) ! OO-667	8 99X1-*84>># 8>>"a'HNN2,>#FVBW,W 	
\]e]k]k\lm	
W j(3 	PJAwnAGG-G $S)97>>";M%N OA	P !}= Xs   A Q/)NF)@      r     )r  r  )RGB)r   r   r   r
   r   r   r   ra   r}   r   r   r  r  r  r   r   s   @r#   r~  r~    sL   #
+
  #$$
+%* 3Z%* E#J'%* %*  !%*" #%*$ %%*& '%*( !)%** %L+%* %*N U]]_L5 L5\ U]]_
 "* 	*
 * *X U]]_
  $"1d 	d
 d  d dr%   r~  )r   r7   r   )0r/   dataclassesr   typingr   r   r   numpyr   r   torch.nn.functionalr   r0  r$  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  r`  ro  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%   