
    Hh"                         d dl Z d dlmZ 	 d dlZn# e$ r dZY nw xY wd dlmZ ddlm	Z	m
Z
 ddlmZmZ d dlZ G d de          ZdS )	    N)Dict   )encodedecode   )offset2batchbatch2offsetc                   :     e Zd ZdZ fdZd
dZddZdd	Z xZS )Pointa  
    Point Structure of Pointcept

    A Point (point cloud) in Pointcept is a dictionary that contains various properties of
    a batched point cloud. The property with the following names have a specific definition
    as follows:

    - "coord": original coordinate of point cloud;
    - "grid_coord": grid coordinate for specific grid size (related to GridSampling);
    Point also support the following optional attributes:
    - "offset": if not exist, initialized as batch size is 1;
    - "batch": if not exist, initialized as batch size is 1;
    - "feat": feature of point cloud, default input of model;
    - "grid_size": Grid size of point cloud (related to GridSampling);
    (related to Serialization)
    - "serialized_depth": depth of serialization, 2 ** depth * grid_size describe the maximum of point cloud range;
    - "serialized_code": a list of serialization codes;
    - "serialized_order": a list of serialization order determined by code;
    - "serialized_inverse": a list of inverse mapping determined by code;
    (related to Sparsify: SpConv)
    - "sparse_shape": Sparse shape for Sparse Conv Tensor;
    - "sparse_conv_feat": SparseConvTensor init with information provide by Point;
    c                 R    t                      j        |i | d|                                 vr/d|                                 v rt          | j                  | d<   d S d|                                 vr/d|                                 v rt          | j                  | d<   d S d S d S )Nbatchoffset)super__init__keysr   r   r	   r   )selfargskwargs	__class__s      [/data/cameron/tmprepos/clean_dreammesh/UniRig/src/model/pointcept/models/utils/structure.pyr   zPoint.__init__'   s    $)&)))$))++%%(diikk*A*A(55DMMMTYY[[((W		-C-C)$*55DNNN )(-C-C    zNFc           	          d                                  v sJ d                                  vrddh                                                                sJ t          j        j                             j        dd          }|t          j         j	        |d	
          }t          j
         j	        | j                 z
   j        d                                           d<   8t           j                                                                                   d<   dz  t#           j                                                  z   dk    sJ dk    sJ  fd|D             }t          j        |          }t          j        |          }t          j        |                              d|t          j        d|j        d         |j                                      |j        d         d                    }|r7t          j        |j        d                   }||         }||         }||         }| d<   | d<   | d<   dS )zv
        Point Cloud Serialization

        relay on ["grid_coord" or "coord" + "grid_size", "batch", "feat"]
        r   
grid_coord	grid_sizecoordr   r   r   valueNminreducetruncrounding_modeserialized_depth   ?      c                 J    g | ]}t          j        j        |            S ))order)r   r   r   ).0order_depthr   s     r   
<listcomp>z'Point.serialization.<locals>.<listcomp>b   s;     
 
 
IOF4?DJVDDD
 
 
r   r   )device)dimindexsrcserialized_codeserialized_orderserialized_inverse)r   issubsettorchnn
functionalpadr   torch_scattersegment_csrr   divr   r   intr   max
bit_lengthlenstackargsort
zeros_likescatter_arangeshaper0   repeatrandperm)	r   r+   r.   shuffle_orders	min_coordidx_ptrcodeinverseperms	   ` `      r   serializationzPoint.serialization/   sh    $))++%%%% tyy{{**
  )22499;;?????h)--dk6-KKG )5dj'RWXXX	!&
Ytz22%" " " cee	  =++--..99;;E#( qy3t{++66888B>>>>
 {{{{
 
 
 
 
SX
 
 
 {4  d##"5))22Q
1elCCCJJ
1q  3 
 
  	$>$*Q-00D:D$KEdmG"&#( %,!"""r   `   c                    ddh                     |                                           sJ d|                                 vrddh                     |                                           sJ t          j        j                            | j        dd          }t          j        | j	        |d	
          }t          j
        | j	        || j                 z
  | j        d                                          | d<   d|                                 v r| j        }nEt          j        t          j        | j        d          j        |                                          }t)          j        | j        t          j        | j                            d                                          | j                                        gd                                          || j        d                                         dz             }|| d<   || d<   dS )a-  
        Point Cloud Serialization

        Point cloud is sparse, here we use "sparsify" to specifically refer to
        preparing "spconv.SparseConvTensor" for SpConv.

        relay on ["grid_coord" or "coord" + "grid_size", "batch", "feat"]

        pad: padding sparse for sparse shape.
        featr   r   r   r   r   r   r   r    r!   r#   r$   sparse_shape)r1   r   )featuresindicesspatial_shape
batch_sizesparse_conv_featN)r7   r   r8   r9   r:   r;   r   r<   r=   r   r>   r   r   r?   rU   addr@   r   valuestolistspconvSparseConvTensorrT   cat	unsqueeze
contiguous)r   r;   rM   rL   rU   r[   s         r   sparsifyzPoint.sparsifyy   s     ))$))++66666 tyy{{**
  )22499;;?????h)--dk6-KKG%1$*geTTTI!&
Ytz22%" " " cee	 
 TYY[[((,LL 9	$/q1118# fhh  "2YI%%b))--//1D1D1F1FGQ  jll&z"~,,..2
 
 
  ,^#3   r   c                    t           
J d            h d                    |                                           sJ |Yd|                                 v r| j        }n;t	          | j                                        dz                                             }|d}|| d<   |dk    sJ | j        d| j        dz
  z  z  dz
  }t           j        	                    || j
        | j                            d	          | j        d	         dz   
          }t           j                            ||| j        d	         dz   |j                  }|                    |           |                                 || d<   dS )z
        Point Cloud Octreelization

        Generate octree with OCNN
        relay on ["grid_coord", "batch", "feat"]
        NzEPlease follow https://github.com/octree-nn/ocnn-pytorch install ocnn.>   rT   r   r   r.   r   r   r)   g      ?rV   )pointsrW   batch_idrZ   )r.   
full_depthrZ   r0   octree)ocnnr7   r   r.   r?   r   r@   rA   ri   PointsrT   r   rb   Octreer0   build_octreeconstruct_all_neigh)r   r.   rh   r   pointri   s         r   octreetizationzPoint.octreetization   s    R ...77		DDDDD=$))++%%
DO//11A566AACCJW{{{{ !
Q"77#=""YZ))"--z"~)	 # 
 
 ##!z"~)<	 $ 
 
 	E"""""$$$Xr   )r   NFN)rR   )NN)	__name__
__module____qualname____doc__r   rQ   rd   rp   __classcell__)r   s   @r   r   r      s         06 6 6 6 6H- H- H- H-T14 14 14 14f&  &  &  &  &  &  &  & r   r   )r8   spconv.pytorchpytorchr_   rj   ImportErroraddictr   rQ   r   r   utilsr   r	   r<   r    r   r   <module>r|      s          KKKK   DDD       ) ) ) ) ) ) ) ) . . . . . . . .    D  D  D  D  D D D  D  D  D  D s    