
    wi              	          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d6de	j        dedede	j        fdZd Zd7d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j#                  Z$e G d  d!                      Z% G d" d#ej#                  Z& G d$ d%ej#                  Z' G d& d'ej#                  Z(e G d( d)e                      Z) G d* d+ej#                  Z*e G d, d-e                      Z+ G d. d/ee          Z, G d0 d1ej#                  Z- G d2 d3ee          Z. G d4 d5ee          Z/dS )8    N)	dataclass)DictOptionalTuple)nn   )ConfigMixinregister_to_config)
ModelMixin)
BaseOutput   )create_pan_cameraspmf	n_samplesreturnc                 @   | j         ^ }}}|dk    sJ t          j        |                     d|          d          }t          j        |t          j        |j         d         ||j                            } |j        g ||dR                      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          s/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/pipelines/shap_e/renderer.py
sample_pmfr$      s     &)Y"UL(q====
,sxxL11q
9
9
9Cc5:cilIcj#Y#Y#YZZD49*e*Y****00L14DEEE       xmin_degmax_degc                    ||k    r| S dt          j        ||| j        | j                  z  }| j        ^ }} |                     dd|          |                    ddd          z  j        g |dR  }|j        d         |||z
  z  k    sJ t          j        ||t          j	        dz  z   gd          
                                }t          j        | |gd          S )zz
    Concatenate x and its positional encodings, following NeRF.

    Reference: https://arxiv.org/pdf/2210.04628.pdf
    g       @)dtyper   r   r   )axisr   )r   aranger+   r   r   reshaper   catmathpisin)r'   r(   r)   scalesr   r   xbembs           r#   posenc_nerfr6   5   s     'EL'RRRRF'KUC	@!))B3

&++aQ"7"7
7	@	L%	L	L	L	LB8B<3'G"344444
)Rdgm+,2
6
6
6
:
:
<
<C9aX2&&&&r%   c                 &    t          | dd          S )Nr   r&   r(   r)   )r6   positions    r#   encode_positionr;   F   s    xB7777r%   c                 r    |$t          j        t          | dd                    S t          |dd          S )Nr      r8   )r   
zeros_liker6   )r:   	directions     r#   encode_directionr@   J   s=    Ha K K KLLL9a;;;;r%   c                 .    |                      dd          S )N.__)replacer'   s    r#   _sanitize_namerF   Q   s    99S$r%   c                    |                      |          \  }}}||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ItCQQQJ//00M59hY'''F	59e.tC!QQQJ/?@@4SbSRSRSRSCTBTU[]^^^	_	_B rkG yG+444HWm++r%   c                 V   t          j        |dz  | j        j                  }||z  }t          j        ||d          |z  }t          j        ||dz  d          |z  }t          j        |||gd          }|                                |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    l9a<0FGGGG	9	B	7IW	=	=	=		IB	7IqL	@	@	@9	LB{BB<Q///HNN	A.6?V_3TUX^Xg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MMr%   flat_cube_indicesrd   c                 p   |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; aaad#il2Yq\A1%	!451%& aaad#il2Yq\A A&*il:;1%& aaad#il2Yq\A1%	!451%&  aaad#il2Yq\A A&*il:;1%&  #AAAqD)Yq\A-=>1MN#AAAqD)IaL89 $AAAqD)* $QQQT*Q.9Q<!3CDyQR|ST#AAAqD)IaL89 $AAAqD)* #AAAqD)Yq\A-=>1MN#AAAqD)IaL89 $AAAqD)* 	 $QQQT*Q.9Q<!3CDyQR|ST#AAAqD)IaL89 $AAAqD)* 	 #AAAqD)IaL8IaL1<LMN#AAAqD)Yq\A-=>? $AAAqD)* $QQQT*Q.)A,>)A,QRBRST#AAAqD)Yq\A-=>? $AAAqD)* #AAAqD)IaL8IaL1<LMN$QQQT*Q.9Q<!3CDE $AAAqD)* $QQQT*Q.)A,>)A,QRBRST$QQQT*Q.9Q<!3CDE $AAAqD)*D	
J MG G 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.
         o@c                 &   t                                                       t          j        t	          j        t          j        |                                        t          j	                  |z            }| 
                    d|           d S )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__   st    \%"228J3G3G"H"H"K"KRWR_"K"`"`cp"pqq
\:66666r%   c                    | j         d                              |j                  }|j        d d         }dgt	          |          dz
  z  }|j        d         }t          j         |j        |j        d         g||R  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*--ho>>
ss#sc%jj1n%%b)
'

8H8K(_d(_T^(_(_(_auchaujtauvv
r%   )ry   )__name__
__module____qualname____doc__r~   r   __classcell__r   s   @r#   rx   rx      sV         7 7 7 7 7 7      r%   rx   c                   X    e Zd ZU ej        ed<   ej        ed<   ej        ed<   d Zd ZdS )VolumeRanget0t1intersectedc                 d    | j         j        | j        j        cxk    r| j        j        k    sn J d S N)r   r   r   r   )r   s    r#   __post_init__zVolumeRange.__post_init__   s;    w}GGGG1A1GGGGGGGGGr%   c                 R   |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n 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AAA:C"aaaK0C7	473aaa<0$7R@@@	4dAAA!67R@@@{ekDDDDU[DDDDBHDDDDDDeU""r%   N)r   r   r   r   Tensor__annotations__r   rJ    r%   r#   r   r      sa         H H H# # # # #r%   r   c                   x     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                V   t                                                       || _        || _        t	          j        |          | _        t	          j        |          | _        t	          j        | j        | j        g          | _	        | j	        j
        dk    sJ |dk    sJ |dk    sJ dS )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..K >??	y&((((3S      r%   Nư>originr?   t0_lowerc                 j   |j         ^}}}dgt          |          z  } | j        j        dg|ddR                      |j                  }	dd}
 |
|	|ddddf         z
  |ddddf         |          }|                    d	
          j                            dd          j        	                    | j
                  }|                    d	
          j                            dd          j        }|j         |j         cxk    r|g|dR k    sn J |'|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   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CCr%   .N)r   rH   r   r   Tr   keepdim)r   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ZZty~a-$--1---00??	D 	D 	D 	D \$T111!55ydAAA7NX_``` VVV^^"&&2t&<<CII$-XXVVV^^"&&2t&<<Cx28>>>>
'>U'>A'>'>>>>>>>8x~----r8,,B4++b0[b%*:2*>*>??[b%/"*=*=>>bR[AAAAr%   )Nr   )r   r   r   r   ra   r~   r   r   r   r   r   r   s   @r#   r   r     s          !! ! !
 ! ! ! ! ! ! !< ,02B 2B2B <2B 5<(	2B 2B 2B 2B 2B 2B 2B 2Br%   r   c                   ^    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
dS )StratifiedRaySamplerz_
    Instead of fixed intervals, a sample is drawn uniformly at random from each interval.
    linear
depth_modec                 *    || _         | j         dv sJ dS )z
        :param depth_mode: linear samples ts linearly in depth. harmonic ensures
            closer points are sampled more densely.
        )r   	geometricharmonicN)r   )r   r   s     r#   r~   zStratifiedRaySampler.__init__f  s%    
 %"EEEEEEEr%   r   r   r   r   r   r   c                    dgt          |j                  dz
  z  } t          j        dd|          j        g ||R                      |j                                      |j                  }| j        dk    r|d|z
  z  ||z  z   }n| j        dk    rk|	                    |          
                                d|z
  z  |	                    |          
                                |z  z                                   }nH| j        dk    r=dd|	                    |          z  d|z
  z  d|	                    |          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   }|                    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"(mma'(1U^Aq),,1C4CCCCFFrxPPSSTVT]^^?h&&sRx27*BB_++((7##''))S2X6'9J9J9N9N9P9PSU9UUZZ\\BB_
** bhhw///38<sRXXgEVEV?VY[?[[\Bbabb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 F3 F F F F $  $ L$  L$  	$ 
 $  
$  $  $  $  $  $ 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dS )ImportanceRaySamplerzp
    Given the initial estimate of densities, this samples more from regions/bins expected to have objects.
    Fh㈵>rM   rN   rX   	blur_poolalphac                     || _         |                                                                | _        |                                                                | _        || _        || _        dS )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__  sT      )((**##%%}}--//"


r%   r   r   r   r   c           	      l   | j                             | j                  \  }}}| j        j        ^}}}	}| j        }
| j        rt          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   }
|
|

                    dd	          z  }t          ||          }|j        |g||dR k    sJ |d
k                                    r||	k                                     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5dg>>ua26'-/
U,a,> 	EYRaR
 3Wgc233PQPQPQk>RSY[\\\FM&crc111"5vc122qqqj7IJJEU3QQQ;/%QRR
2CCDGDJ&D999#y))zj?5?)?Q??????	  Dd-=&=%B%B%D%DDDDDJt{;;;eR..eR..v&00Z###*	r%   N)Fr   )r   r   r   r   r   r   r   boolra   r~   no_gradr   r   r   r%   r#   r   r     s            ! L 	
     , U]__ 5< C EL    _  r%   r   c                   `    e Zd ZU dZej        ed<   ej        ed<   eeej        f         ed<   dS )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     sR         
 
 <<#u|+,,,,,,r%   r   c                   T     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                  }|                     d|           |                     d|           d S )N      r   r{   casesmasks)r}   r~   r   zeroslongr   r   )r   r   r   r   s      r#   r~   zMeshDecoder.__init__  s}    CAUZ888C%*555We,,,We,,,,,r%   field	min_pointsizec           
         t          |j                  dk    s
J d            |j        }| j                            |          }| j                            |          }|                    |          }|                    |          }|j        }t          j        |                              |          }|dk                        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R ||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                      dd          |
ddddf         |
ddddf         z   dz                      dd          |
ddddddf         |
ddddddf         z   dz                      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<   |                    dd          }t#          ||          }|	                    d                                          }||         }||         }t          j        |d|                    |j        d         d                                        |j                  }|                    dd          |                    d                   }t          j        |                    d                    }||         }t          j        t          |          |t          j        	          }t          j        t          |          |t          j        	          ||<   t          j        |d|                    d                                        |j                  }t          j        |                              t          j                  }t          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         }|                                |dz
  z  |z  |z   }|                                |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  sP    5;1$$$&G$$$l
c""
c""LL%%	wws||K	 <	2255d;; AI>>%+..CRCAAAI&(122qqq!!!8*<*ABAAAssAAAI&(111abb!!!8*<*ABAAAqqq#2#I&(111aaa8*<*AB QYQQQ#U[QQQ6;l9Q<X[chcn6o6o6oAAtTM7
eIaL))111aaa23 7<l9Q<X[chcn6o6o6oAAtG7
aaay|,,aaa23 7<l9Q<X[chcn6o6o6oaaaE)A,//23 $}QRR'88A=FFr1MM3B3'-122*>>!CLLRQRSS111crc	*]111aaa8-DDIRRSUWXYY
 
 
 
 {aL1ilQ.	!q0@!CW\Wa
 
 
 :?iPQlUVFV_b9c9c9cdededegkmqdq9rU9Q<!+,,aaaA569>iPQlUVFV_b9c9c9cdededegkdk9lQQQilQ.//A569>iPQlUVFV_b9c9c9cQQQ51!122A56(00Q77 11BINN !((,,1133=)
M*l<J4F4FzGWXYGZ\^4_4_``hh
 
 $++B22;3F3Fr3J3JK $l=+=+=b+A+ABB,-@A!&S-@-@TYT^!_!_!_6;l#$$S
7
 7
 7
23
 3Q8J8J28N8NOOWWXeXkll [,--00<<Z+,,//
;;2aaad8R1Xr!!!Q$x/02aaad8R1Xr!!!Q$x/0hhjj,q01T9IEhhjj,q01T9IE 27^QQQW%B!a%2% uE4PPPPr%   )	r   r   r   r   r~   r   r   r   r   r   s   @r#   r   r     s         - - - - -dQU\ dQel dQ%, dQ dQ dQ dQ dQ dQ 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<   dS )MLPNeRFModelOutputrO   signed_distancerP   rN   N)r   r   r   r   r   r   r   r%   r#   r  r  Y  sJ         \\!!!lr%   r  c                   d     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 )MLPNeRSTFModelr         swishr   d_hiddenn_outputn_hidden_layersact_fninsert_direction_atc                 D   t                                                       t          j        dd          }t	          |          j        d         }t          |          j        d         }|g|z  }	|g|	z   }
|	|gz   }||
|xx         |z  cc<   t          j        d t          |
|          D                       | _
        |dk    r	d | _        nt          d|           t          j        | _        t          j        j        j        | _        t          j        | _        d S )	Nr   r   r9   r   c                 >    g | ]\  }}t          j        ||          S r   )r   Linear).0d_ind_outs      r#   
<listcomp>z+MLPNeRSTFModel.__init__.<locals>.<listcomp>{  s(    !m!m!m[T5")D%"8"8!m!m!mr%   r  c                 *    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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_widthsr   s               r#   r~   zMLPNeRSTFModel.__init__b  s)    	
 	!Q&666<R@'777=bAZ/1
$~
2"hZ/*,---=---=!m!mCP\^kLlLl!m!m!mnnW 21DOOHHHIII#j"'("5":"'-r%   c                 Z    ddddddd}fd|                                 D             }|S )	N)r   r   )r   r]   r   )r   r  )r  	   )rD  r  )sdfdensity_coarsedensity_finestfnerf_coarse	nerf_finec                 6    i | ]\  }\  }}|d ||f         S ).r   )r(  kstartendoutputs       r#   
<dictcomp>z6MLPNeRSTFModel.map_indices_to_keys.<locals>.<dictcomp>  s0    XXXq,5#F3c	>2XXXr%   )items)r   rO  h_mapmapped_outputs    `  r#   map_indices_to_keysz"MLPNeRSTFModel.map_indices_to_keys  sK    $"! 
 
 YXXX%++--XXXr%   coarsenerf)
nerf_levelrendering_modec                   t          |          }|}d }t          | j                  D ]}\  }	}
|	| j        j        k    r+|}t          ||          }t          j        ||gd          } |
|          }|}|	t          | j                  dz
  k     r| 	                    |          }~|}||}| 
                    |          }|dk    r	|d         }n|d         }|dk    r|dk    r	|d	         }n|d
         }n|dk    r|d         }|                     |          }|                     |d                   }|                     |          }t          ||||          S )N)r?   r   r   r   rU  rF  rG  rV  rI  rJ  rH  rE  )rO   r  rP   rN   )r;   	enumerater3  configr$  r@   r   r/   r   r4  rT  r:  r7  r<  r  )r   r:   r?   rN   rW  rX  hh_preacth_directionlessilayerh_directionh_finalr4  	h_density
h_channelsrO   r  rP   s                      r#   r   zMLPNeRSTFModel.forward  s   H%%!$(++ 	' 	'HAuDK333"*.x9MMMIq+.B777aAH3tx==1$$$OOA&&"&O--g66
!!"#34II">2IV##X%%'6

'4

u$$#E*J)))44--j.?@@**:66 "'?]ejlmmmmr%   )r   r  r  r  r   )
r   r   r   r
   r   r   r~   rT  r   r   r   s   @r#   r  r  a  s          #$#0 #0#0 #0 	#0
 #0 !#0 #0 #0 #0 #0 #0J   >FV\ +n +n +n +n +n +n +n +n +nr%   r  c                   P     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        |          | _        || _        || _        || _	        d S r   )
r}   r~   r   r'  proj	LayerNormnormrh  rg  rP   )r   rg  rP   rh  r   s       r#   r~   zChannelsProj.__init__  s]     	Ih((:;;	L**	  r%   r'   r   c                     |}| j         j                            | j        | j        | j                  }| j         j                            d| j        | j                  }t          j        d||          }| 	                    |          }||z   }|S )Nr   zbvd,vcd->bvc)
rj  weightr   rg  rP   rh  biasr   einsumrl  )r   r'   x_bvdw_vcdb_vcr\  s         r#   r   zChannelsProj.forward  sx    	 %%dlDM4=QQy~""1dlDMBBL66IIaLLHr%   )	r   r   r   r   r~   r   r   r   r   r   s   @r#   rf  rf    s        ! ! 	!
 ! ! ! ! ! ! %,        r%   rf  c                        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   ry  ry     param_namesparam_shapesrh  r|  r}  rh  c                X   t                                                       t          |          t          |          k    rt          d          t	          j        i           | _        t          ||          D ]/\  }\  }}t          |||          | j        t          |          <   0d S )Nz;Must provide same number of `param_names` as `param_shapes`)rg  rP   rh  )
r}   r~   r   r5  r   
ModuleDictprojectionsr2  rf  rF   )r   r|  r}  rh  rL  rg  rP   r   s          r#   r~   zShapEParamsProjModel.__init__  s    $ 	 {s<0000Z[[[=,,&)+|&D&D 	 	"A"2>!!3 3 3D^A..//	 	r%   r'   c                    i }d}t          | j        j        | j        j                  D ]^\  }}|\  }}||z   }|d d ||f         }	  | j        t          |                   |	          j        t          |          g|R  ||<   |}_|S r   )r2  r[  r|  r}  r  rF   r.   r   )
r   r'   outrM  rL  r   rg  rQ   rN  rq  s
             r#   r   zShapEParamsProjModel.forward  s    DK3T[5MNN 	 	HAuJGQ'/CaaaslOEG8T%nQ&7&78??GAWQVWWWCFEE
r%   )r   r   r   r   r
   r   r   r   r~   r   r   r   r   r   s   @r#   ru  ru    s          #
+
    3Z E#J'      <	 	 	 	 	 	 	 	 	r%   ru  c                   V    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 )&ShapERendererrv  rw  rz  r   r  r  r  r   )ry   ry   ry   )	r|  r}  rh  r   r!  r"  r#  r$  r|   r|  r}  rh  r   r!  r"  r#  r$  r|   c       	         ,   t                                                       t          |||          | _        t	          |||||          | _        t          |	d          | _        t          g dg d          | _	        t                      | _        d S )Nr{  ry   )r|   r   )rI   rI   rI   )      r  r  )rb   r^   )r}   r~   ru  params_projr  r3  rx   voidr   rc   r   mesh_decoder)r   r|  r}  rh  r   r!  r"  r#  r$  r|   r   s             r#   r~   zShapERenderer.__init__  s    8 	/#%
 
 

 "(HovObcc!ZuMMM	'K]K]K]^^^'MMr%   NFc                 J   |ddddf         |ddddf         }}| j                             ||d          }|                    |j        |j        |          }	|	                    |j                  }	|6t          j        t          j	        |	|j
        gd          d          j        }	|j        j        ^}
}}|	j        ^}}}t          j        |                    d          |
g|d          }|                    d          |	|z  z   }|                    | j        j                  }|                    | j        j                  }|r|nd}|                     |||	|d	nd
          }t!          ||j
        |j        |j                  \  }}}t          j        |j        |t          j        |                    }t          j        |j        |t          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   rU  fine)r:   r?   rN   rW  )rN   rX   )rc   r   r   r   r   r   r+   r   r   r/   rN   r   r   r   r   r3  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H^^FIvy)<<UU4:% EIr>+<&=2FFFBOOOVB'-y$
VW "Hg '	(;(;B(?(?*A[xA[YZA[\\
$$R((2
?:	]]48>22
LL00	,AKjjtHH)#1#9xxv	  
 
	 ,=IL)"3Y5G,
 ,
('=
 F$6uWdGeGeff;v18U=Mh=W=WXXmdii.?.???/9<QXYYY)944r%   @         r   ray_batch_sizec                 >   |                      |          }| j                                                                        D ]O\  }}	d| |                                v r1|	                    |d|                              d                     Pt          |          }
|
j        }|	                    |          }|j
        d         |z  }t                      }g }t          |          D ]f}|d d ||z  |dz   |z  f         }|                     |||          \  }}}|                     ||||          \  }}}|                    |           gt          j        |d          } |j        g |
j
        |
j        |
j        dR                      d          }|S )Nnerstf.r   r   )r  r   r   )r  r3  
state_dictrQ  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4  8..006688 	K 	KKD%#3#8#8#:#:::,-=t-=-=>FFqIIJJJ $D))!wwvJqM^3	-//## 
	$ 
	$Caaa~!5qN8R!RRSJ 150@0@^]m0n0n-A|-!--L.IY .  NHa MM(####6q)))LflLFML6<LLLLTTUVWWr%   RGBrd   query_batch_sizetexture_channelsc           	         |                      |          }| j                                                                        D ]O\  }}d| |                                v r1|                    |d|                              d                     Pt          | j        |          }	|	d          	                    ddd          
                    || j        j                  }
g }t          d|
j        d         |          D ]G}|
d d |||z   f         }|                     |d d dd          }|                    |j                   Ht!          j        |d          }|                                }t'          |j                  d	k    r|j        d
         dk    sJ d|j                      |j        dg|gd	z  R  }t!          j        d|dz   |dz   |dz   |j        |j                  }|                    d           ||d d dd
dd
dd
f<   |}g }g }|D ]d}|                     || j        j        | j        j        | j        j        z
            }|                    d           |                    |           et!          j        ||j                  }t9          d |D                       t!          j        fd|D             d          }|
                    || j        j                  }g }t          d|j        d         |          D ]G}|d d |||z   f         }|                     |d d dd          }|                    |j                   Ht!          j        |d          }t?          |          }|                                }t'          |j                  d	k    r|j        d
         t'          |          k    sJ d|j                     tA          ||          D ]V\  }}|d t'          |j!                           }tE          tA          ||#                    d
                              |_$        W|d         S )Nr  r   r   r   r  rH  )r:   r?   rN   rW  rX  r   r   r   z9expected [meta_batch x inner_batch] SDF results, but got r]   r  Tr   c              3   >   K   | ]}t          |j                  V  d S r   )r   r   )r(  ms     r#   	<genexpr>z/ShapERenderer.decode_to_mesh.<locals>.<genexpr>  s*      <<A3qw<<<<<<<<r%   c                 z    g | ]7}|j         t          j        d           t          |j                   z           8S )r   )r   r   r-   r   )r(  r  max_verticess     r#   r+  z0ShapERenderer.decode_to_mesh.<locals>.<listcomp>  s9    WWWqQWU\!\22S\\ABWWWr%   zNexpected [meta_batch x inner_batch x texture_channels] field results, but got )%r  r3  r  rQ  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`   rP   ro   r2  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texture_query_positionstexturestexture_model_outr  texturer  s                            @r#   decode_to_meshzShapERenderer.decode_to_mesh  s     ++G44  8..006688 	K 	KKD%#3#8#8#:#:::,-=t-=-=>FFqIIJJJ
 +4;	BB&t,33Aq!<<??vUYU]Uc?ddO1!46FGG 	5 	5C)!!!S39I3I-I*IJK$&af !  I MM)34444 6q))) ""v|B'71'<'<'<Uv|UU (='<<  6i[1_666 KMMM=,
 
 
	 	)/	!!!QrT1R42%& 
	 	( 	(E((0DdkFZ]a]h]qFqrrHT"""h''''L6=AAA	<<<<<<< #(+WWWWJWWW#
 #
 #
 #:"<"<FRVRZR`"<"a"a39!<>NOO 	8 	8C1!!!S3AQ;Q5Q2QRK $$&af !) ! ! OO-67777 9X1---*844>>## 8>""a''HN2,>#C
 C
 -
 -
 -
l\d\jll-
 -
 
 j(33 	P 	PJAwnAGn-G $S)97>>";M;M%N%N O OA!}r%   )NF)r  r  r  r  )r  r  r  )r   r   r   r
   r   r   r   ra   r~   r   r   r  r  r  r   r   s   @r#   r  r    s       #
+
  #$$
+%* %* %* 3Z%* E#J'%* %*  !%*" #%*$ %%*& '%*( !)%** %L+%* %* %* %* %* %*N U]__L5 L5 L5 _L5\ U]__
 "* * 	*
 * * * _*X U]__
  $"1d d 	d
 d  d d d _d d d d dr%   r  )r   r&   r   )0r0   dataclassesr   typingr   r   r   numpyr   r   torch.nn.functionalr   r8  r-  configuration_utilsr	   r
   modelsr   utilsr   r  r   r   r   r$   r6   r;   r@   r   rF   rY   rj   ro   rv   Modulerx   r   r   r   r   r   r   r  r  rf  ru  r  r   r%   r#   <module>r     su    ! ! ! ! ! ! ( ( ( ( ( ( ( ( ( (                     B B B B B B B B                   & & & & & &FEL FS FU\ F F F F.' '5< '# 'C ' ' ' ' '"8 8 8< < < < c  c        , , ,Dh h hNu| N N N NO|OS#s]#O O O Od    BI   , # # # # # # # #@QB QB QB QB QB	 QB QB QBh1  1  1  1  1 29 1  1  1 h: : : : :29 : : :z - - - - -
 - - -$qQ qQ qQ qQ qQ") qQ qQ qQh        `n `n `n `n `nZ `n `n `nF    29   4/ / / / /:{ / / /dJ J J J JJ J J J J Jr%   