§
    ÷×j(  ã                   ó6   — d Z ddlZd
d„Zdd„Zdd„Zd„ Zd	„ ZdS )zÁ
Hilbert Order
Modified from https://github.com/PrincetonLIPS/numpy-hilbert-curve

Author: Xiaoyang Wu (xiaoyang.wu.cs@gmail.com), Kaixin Xu
Please cite our work if the code is helpful to you.
é    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        úq/root/private/project/pallet_detection_v2/outputs/pallet_pose_benchmark_v3/upstream/ptv3/serialization/hilbert.pyÚright_shiftr      s–   € ð" „|�DÔ˜QÒÐÝÔ Ñ'Ô'Ð'õ �T‰{Œ{ˆm�c &¤,Ñ/Ô/Ñ/€GÝ˜$  ‘O”O€GˆD�MÝŒhÔ!×%Ò%Ø�u�W‰~Œ~Ô  A ¨Z¸qð &ñ ô €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õ Ô˜V WÑ-Ô-€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(   E   s˜   € ð •%”, ¤
¨4Ô 0Ð1Ñ2Ô2×7Ò7Ñ9Ô9×>Ò>Ñ@Ô@×DÒDÑFÔFÈÑJÑK€EØ
�!Š)ˆ)ÝÔ  ¥{°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ø  € ð6 ”€JØ�œ Q¨Ñ*Ô*×-Ò-¨d¬kÑ:Ô:Ñ:€LØ#×(Ò(¨Ñ,Ô,Ðà�"„~˜Ò!Ð!Ýð
ð ˜"Œ~˜xÐ(ñ	)ñ
ô 
ð 	
ð �(Ñ˜RÒÐÝð
ð
 ˜ 8¨hÑ#6Ð7ñ8ñ
ô 
ð 	
ð —’‘”×!Ò!¥%¤+Ñ.Ô.×6Ò6¸¸HÀaÐ7HÑIÔI×NÒNÈrÑRÔR€Jð 	×Ò˜RÑ Ô ß	ŠÐ%Ñ	&Ô	&ß	ŠˆA‰Œß	Š‰Œß	Š��R‰Œ˜˜x˜i˜j˜j˜ô		*ð 	õ �Q˜Ñ!Ô!ð Vñ Vˆå˜˜HÑ%Ô%ð 	Vñ 	VˆCà˜˜˜˜3 ˜Ô$ˆDõ %*Ô$5Ø�Q�Q�Q˜˜3 ™7˜9˜9�_Ô% t¨A¨A¨A¨t¨G¤}ñ%ô %ˆD����A�s˜Q‘w�y�y�Ñ!õ
 Ô'ÝÔ! $ q q q¨$ w¤-Ñ0Ô0×7Ò7¸¸4¼:Àa¼=È3Ñ;NÐQRÑ;RÑSÔSÝÔ! $ q q q¨!¨S°1©W¨Y¨Y Ô"7¸¸a¸a¸aÀÀcÈAÁgÀiÀiÐ>OÔ9PÑQÔQñô ˆGõ ',Ô&7Ø�Q�Q�Q˜˜S 1™W˜Y˜YÐ&Ô'¨ñ'ô 'ˆD����C˜˜q™˜˜Ð"Ñ#õ %*Ô$5°d¸1¸1¸1¸aÀÀqÁÀÀ¸?Ô6KÈWÑ$UÔ$UˆD����A�s˜Q‘w�y�y�Ñ!Ñ!ñ#	Vð( �=Š=˜˜AÑÔ×&Ò&¨¨H°xÑ,?Ð'@ÑAÔA€Dõ ˜ÑÔ€Fð �h Ñ)Ñ)€JÝŒXÔ ×$Ò$ V¨j¸!¨_¸jÈ!ÑLÔL€Fð 
�Š�R‰Œ×	 Ò	  Ñ	,Ô	,¨|Ñ	;ß	ŠˆQ‰Œß	Š‰Œß	Š�eŒkÑ	Ô	ð	 ð —’�eœkÑ*Ô*×2Ò2Ñ4Ô4€IàÐ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¨Ñ*Ô*×-Ò-¨h¬oÑ>Ô>Ñ>€LØ#×(Ò(¨Ñ,Ô,Ðð
 	�ŠÑÔ×Ò�eœkÑ*Ô*×/Ò/µ´Ñ<Ô<×DÒDÀWÑMÔM×RÒRÐSUÑVÔVð ð 	×Ò˜2ÑÔß	ŠÐ%Ñ	&Ô	&ß	ŠˆA‰Œß	Š‰Œß	Š��R‰Œ˜˜˜˜X˜I¨Ñ0Ð2Ð2Ð2ô		4ð õ �wÑÔ€Dð �<Š<˜˜X xÐ0Ñ1Ô1×:Ò:¸1¸aÑ@Ô@€Dõ �X ‘\ 2 rÑ*Ô*ð Vñ Vˆå˜ A™ r¨2Ñ.Ô.ð 	Vñ 	VˆCà˜˜˜˜3 ˜Ô$ˆDõ %*Ô$5Ø�Q�Q�Q˜˜3 ™7˜9˜9�_Ô% t¨A¨A¨A¨t¨G¤}ñ%ô %ˆD����A�s˜Q‘w�y�y�Ñ!õ
 Ô'ÝÔ! $ q q q¨$ w¤-Ñ0Ô0ÝÔ! $ q q q¨!¨S°1©W¨Y¨Y Ô"7¸¸a¸a¸aÀÀcÈAÁgÀiÀiÐ>OÔ9PÑQÔQñô ˆGõ ',Ô&7Ø�Q�Q�Q˜˜S 1™W˜Y˜YÐ&Ô'¨ñ'ô 'ˆD����C˜˜q™˜˜Ð"Ñ#õ %*Ô$5°d¸1¸1¸1¸aÀÀqÁÀÀ¸?Ô6KÈWÑ$UÔ$UˆD����A�s˜Q‘w�y�y�Ñ!Ñ!ñ#	Vð( �h‘€JÝŒXÔ ×$Ò$ T¨J¸¨?¸JÈÑJÔJ€Fð —;’;˜r‘?”?×*Ò*¨B°¸!¸QÐ+?Ñ@Ô@€Lð  Ñ-×2Ò2°1Ñ5Ô5×=Ò=Ñ?Ô?×DÒDÅUÄ[ÑQÔQ€Jð —’¥¤Ñ,Ô,€Ið ×ÒÐ4˜zÐ4¨8Ð4Ð4Ñ5Ô5Ð5r   )r   r   )r   )Ú__doc__r
   r   r   r(   rU   r^   © r   r   ú<module>ra      sˆ   ððð ð €€€ðð ð ð ðDð ð ð ð.ð ð ð ð,kð kð kð\f6ð f6ð f6ð f6ð f6r   