
    h#&                     2    d dl Z d	dZd
dZd
dZd Zd ZdS )    N   c                 <   | j         |         |k    rt          j        |           S t          d          gt	          | j                   z  }t          d|           ||<   t          j        j                            | t          |                   |dfdd          }|S )aa  Right shift an array of binary values.

    Parameters:
    -----------
     binary: An ndarray of binary values.

     k: The number of bits to shift. Default 1.

     axis: The axis along which to shift.  Default -1.

    Returns:
    --------
     Returns an ndarray with zero prepended and the ends truncated, along
     whatever axis was specified.Nr   constant)modevalue)	shapetorch
zeros_likeslicelennn
functionalpadtuple)binarykaxisslicingshifteds        X/data/cameron/tmprepos/2unirig/src/model/pointcept/models/utils/serialization/hilbert.pyright_shiftr      s    " |DQ''' T{{mc&,///G$OOGDMh!%%uW~~AZq &  G N    c                 R    t          | |          }t          j        | |          }|S )aS  Convert an array of binary values into Gray codes.

    This uses the classic X ^ (X >> 1) trick to compute the Gray code.

    Parameters:
    -----------
     binary: An ndarray of binary values.

     axis: The axis along which to compute the gray code. Default=-1.

    Returns:
    --------
     Returns an ndarray of Gray codes.
    )r   )r   r
   logical_xor)r   r   r   grays       r   binary2grayr   &   s/     &t,,,G VW--DKr   c                 J   dt          j        | j        |         g                                                                                                          dz
  z  }|dk    r@t          j        | t          | |                    } t          j        |dd          }|dk    @| S )a  Convert an array of Gray codes back into binary values.

    Parameters:
    -----------
     gray: An ndarray of gray codes.

     axis: The axis along which to perform Gray decoding. Default=-1.

    Returns:
    --------
     Returns an ndarray of binary values.
       r   r   floor)rounding_mode)	r
   Tensorr	   log2ceilintr   r   div)r   r   shifts      r   gray2binaryr(   =   s     %,
4 01227799>>@@DDFFJKE
!)) {4'?'?@@	%'::: !)) Kr   c                    | j         }dt          j        dd                              | j                  z  }|                    d          }|d         |k    rt          d|d         |fz            ||z  dk    rt          d||||z  fz            |                                                     t          j	                  
                    d|df                              d          }|                    d                              |                              d                                                              dd          d	| d
f         }t!          d|          D ]N}t!          d|          D ]9}	|d
d
|	|f         }
t          j        |d
d
d|dz   d
f         |
d
d
d
f                   |d
d
d|dz   d
f<   t          j        t          j        |
d
d
d
f                                       d|j         d         |z
  dz
            t          j        |d
d
d|dz   d
f         |d
d
|	|dz   d
f                             }t          j        |d
d
|	|dz   d
f         |          |d
d
|	|dz   d
f<   t          j        |d
d
d|dz   d
f         |          |d
d
d|dz   d
f<   ;P|                    dd          
                    d||z  f          }t-          |          }d||z  z
  }t          j        j                            ||dfdd          }|                    d          
                    d          |z                      d                                                              t          j	                  }|                    t          j                                                  }|S )a  Decode an array of locations in a hypercube into a Hilbert integer.

    This is a vectorized-ish version of the Hilbert curve implementation by John
    Skilling as described in:

    Skilling, J. (2004, April). Programming the Hilbert curve. In AIP Conference
      Proceedings (Vol. 707, No. 1, pp. 381-387). American Institute of Physics.

    Params:
    -------
     locs - An ndarray of locations in a hypercube of num_dims dimensions, in
            which each dimension runs from 0 to 2**num_bits-1.  The shape can
            be arbitrary, as long as the last dimension of the same has size
            num_dims.

     num_dims - The dimensionality of the hypercube. Integer.

     num_bits - The number of bits for each dimension. Integer.

    Returns:
    --------
     The output is an ndarray of uint64 integers with the same shape as the
     input, excluding the last dimension, which needs to be num_dims.
    r   r      r   z
      The shape of locs was surprising in that the last dimension was of size
      %d, but num_dims=%d.  These need to be equal.
      ?   z
      num_dims=%d and num_bits=%d for %d bits total, which can't be encoded
      into a int64.  Are you sure you need that many points on your Hilbert
      curve?
      .Nr   @   r   )r   r*   r*   )r	   r
   arangetodeviceflip
ValueErrorlongviewuint8reshape	unsqueezebitwise_andnebyteflattenranger   logical_andlogical_notrepeatswapaxesr(   r   r   r   sumsqueezetypeint64)locsnum_dimsnum_bits
orig_shapebitpack_maskbitpack_mask_rev
locs_uint8r   bitdimmaskto_fliphh_bin
extra_dimspaddedhh_uint8	hh_uint64s                    r   encoderU   S   s   6 JQ**--dk:::L#((,,"~!!
 "~x(	)
 
 	
 (R

 8h#678
 
 	
 !!%+..66Ha7HIINNrRRJ 	R  	%	&	&	A		Rxijj		* 	 Q!! V VH%% 	V 	VC3$D %*$5QQQ3799_%tAAAtG}% %DAsQwyy!
 '!$qqq$w-00774:a=3;NQR;RSS!$qqq!S1WYY"7aaacAgii>O9PQQ G ',&7QQQS1WYY&'' 'DCq"# %*$5d111aq?6KW$U$UDAsQwyy!!#	V( ==A&&Hx,?'@AAD F h))JX $$Vj!_j!LLF 
R	 	 	,	,|	;	Q		ek			  ek**2244Ir   c                 v   ||z  dk    rt          d||fz            t          j        |           } | j        }dt          j        dd                              | j                  z  }|                    d          }|                                 	                    t          j
                                      t          j                                      d                              d          }|                    d                              |                              d                                                              dd          d	d	| |z  d	f         }t'          |          }|                    d||f                              d
d          }t+          |d
z
  dd          D ]-}	t+          |d
z
  dd          D ]}
|d	d	|
|	f         }t          j        |d	d	d|	d
z   d	f         |d	d	d	f                   |d	d	d|	d
z   d	f<   t          j        t          j        |d	d	d	f                   t          j        |d	d	d|	d
z   d	f         |d	d	|
|	d
z   d	f                             }t          j        |d	d	|
|	d
z   d	f         |          |d	d	|
|	d
z   d	f<   t          j        |d	d	d|	d
z   d	f         |          |d	d	d|	d
z   d	f<   /d|z
  }t          j        j                            ||dfdd          }|                    d                              d|ddf          }||z                      d                                          	                    t          j                  }|                    t          j
                  }|                    g ||R           S )a(  Decode an array of Hilbert integers into locations in a hypercube.

    This is a vectorized-ish version of the Hilbert curve implementation by John
    Skilling as described in:

    Skilling, J. (2004, April). Programming the Hilbert curve. In AIP Conference
      Proceedings (Vol. 707, No. 1, pp. 381-387). American Institute of Physics.

    Params:
    -------
     hilberts - An ndarray of Hilbert integers.  Must be an integer dtype and
                cannot have fewer bits than num_dims * num_bits.

     num_dims - The dimensionality of the hypercube. Integer.

     num_bits - The number of bits for each dimension. Integer.

    Returns:
    --------
     The output is an ndarray of unsigned integers with the same shape as hilberts
     but with an additional dimension of size num_dims.
    r-   z
      num_dims=%d and num_bits=%d for %d bits total, which can't be encoded
      into a uint64.  Are you sure you need that many points on your Hilbert
      curve?
      r   r   r*   r   )r   r*   r,   Nr   r      )r2   r
   
atleast_1dr	   r.   r/   r0   r1   ravelrC   rD   r4   r5   r6   r7   r8   r9   r:   r;   r   r@   r<   r   r=   r>   r   r   r   rA   rB   )hilbertsrF   rG   rH   rI   rJ   rS   hh_bitsr   rL   rM   rN   rO   rQ   rR   locs_choppedrK   	flat_locss                     r   decoder^      s   0 (R

 "#
 
 	
 ))H JQ**--ho>>>L#((,,
 	ek**//<<DDWMMRRSUVV  	2	%	&	&	A		RXI0222		4  wD <<Xx011::1a@@D X\2r** V VAr2.. 	V 	VC3$D %*$5QQQ3799_%tAAAtG}% %DAsQwyy!
 '!$qqq$w-00!$qqq!S1WYY"7aaacAgii>O9PQQ G ',&7QQQS1WYY&'' 'DCq"# %*$5d111aq?6KW$U$UDAsQwyy!!#	V( hJX $$TJ?JJJF ;;r??**B!Q+?@@L -22155==??DDU[QQJ ,,I 4z4844555r   )r   r   )r   )r
   r   r   r(   rU   r^    r   r   <module>r`      s|       D   .   ,k k k\f6 f6 f6 f6 f6r   