
    3jv2                         S SK Jr  S SKrS SKJr  S SKJr   " S S\5      r " S S5      r	S	\R                  S
S4S jrS\R                  S
S4S jrS	\R                  S\R                  S
\R                  4S jrg)    )AnyN)create_meshgrid)transform_pointsc                   @   ^  \ rS rSrSrS\S\S\SS4U 4S jjrS	rU =r	$ )
StereoException   zEHandle errors related to stereo camera calibration and rectification.msgargskwargsreturnNc                 :   > SnX-   n[         TU ]  " U/UQ70 UD6  g)aA  Construct custom exception for the :module:`~kornia.geometry.camera.stereo` module.

Adds a general helper module redirecting the user to the proper documentation site.

Args:
    msg: Custom message to add to the general message.
    *args: Additional argument passthrough
    **kwargs: Additional argument passthrough

z
 Please check documents here: https://kornia.readthedocs.io/en/latest/geometry.camera.stereo.html for further information and examples.N)super__init__)selfr	   r
   r   doc_help	final_msg	__class__s         W/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/geometry/camera/stereo.pyr   StereoException.__init__   s0    x 	 N	 	4T4V4     )
__name__
__module____qualname____firstlineno____doc__strr   r   __static_attributes____classcell__)r   s   @r   r   r      s,    O5C 5 5s 5t 5 5r   r   c                   F   \ rS rSrSrS\R                  S\R                  SS4S jr\S\R                  S\R                  SS4S j5       r	\
S\4S	 j5       r\
S\R                  4S
 j5       r\
S\R                  4S j5       r\
S\R                  4S j5       r\
S\R                  4S j5       r\
S\R                  4S j5       r\
S\R                  4S j5       r\
S\R                  4S j5       rS\R                  4S jrS\R                  S\R                  4S jrSrg)StereoCamera2   zRepresent a horizontal stereo camera setup for depth estimation.

Args:
    rectified_left_camera: The intrinsic matrix for the left camera.
    rectified_right_camera: The intrinsic matrix for the right camera.
rectified_left_camerarectified_right_camerar   Nc                     U R                  X5        Xl        X l        U R                  R                  U l        U R                  R                  U l        U R                  5       U l        g)a  Class representing a horizontal stereo camera setup.

Args:
    rectified_left_camera: The rectified left camera projection matrix
      of shape :math:`(B, 3, 4)`
    rectified_right_camera: The rectified right camera projection matrix
      of shape :math:`(B, 3, 4)`

N)_check_stereo_camerar#   r$   devicedtype_init_Q_matrix	_Q_matrix)r   r#   r$   s      r   r   StereoCamera.__init__:   sU     	!!"7P3H"4J#0077//55
,,.r   c           
         [        U R                  5      S:w  a  [        SU R                   S35      e[        UR                  5      S:w  a  [        SUR                   S35      eU R                  SS S:X  a  [        SU R                  SS  S35      eUR                  SS S:X  a  [        S	UR                  SS  S35      eU R                  UR                  :w  a&  [        S
U R                   SUR                   S35      eU R                  UR                  :w  a&  [        SU R                   SUR                   S35      e[
        R                  " [
        R                  " U SSS2SS24   USSS2SS24   5      5      (       d&  [        SU SSS2SS24    SUSSS2SS24    S35      eUS   n[
        R                  " [
        R                  " US5      5      (       a  [        SU S35      eg)a	  Ensure user specified correct camera matrices.

Args:
    rectified_left_camera: The rectified left camera projection matrix
      of shape :math:`(B, 3, 4)`
    rectified_right_camera: The rectified right camera projection matrix
      of shape :math:`(B, 3, 4)`

   z;Expected 'rectified_left_camera' to have 3 dimensions. Got .z;Expected 'rectified_right_camera' to have 3 dimension. Got N   )r-      z@Expected each 'rectified_left_camera' to be of shape (3, 4).Got zAExpected each 'rectified_right_camera' to be of shape (3, 4).Got z\Expected 'rectified_left_camera' and 'rectified_right_camera' to be on the same devices.Got z and zTExpected 'rectified_left_camera' and 'rectified_right_camera' tohave same dtype.Got .ztExpected 'left_rectified_camera' and 'rectified_right_camera' to havesame parameters except for the last column.Got .r   r-   r   z/Expected :math:`T_x * f_x` to be negative. Got )	lenshaper   r'   r(   torchalleqgt)r#   r$   tx_fxs      r   r&   !StereoCamera._check_stereo_cameraM   s`    $**+q0!MNcNiNiMjjkl  %++,1!MNdNjNjMkklm  !&&r*f4!RShSnSnoqpqSrRsstu  "''+v5!STjTpTpqsrsTtSuuvw 
 !''+A+H+HH!,334E:P:W:W9XXY[  !&&*@*F*FF!,22359O9U9U8VVWY  yy"7Q
"CE[\_abdfefdf\fEghii!,S!RaRZ89?UVY[\^`_`^`V`?a>bbce  'y199UXXeQ'((!$STYSZZ["\]] )r   c                 4    U R                   R                  S   $ )zOReturn the batch size of the storage.

Returns:
   scalar with the batch size

r   )r#   r3   r   s    r   
batch_sizeStereoCamera.batch_size   s     ))//22r   c                      U R                   S   $ )zReturn the focal length in the x-direction.

Note that the focal lengths of the rectified left and right
camera are assumed to be equal.

Returns:
    torch.Tensor of shape :math:`(B)`

).r   r   r#   r;   s    r   fxStereoCamera.fx        )))44r   c                      U R                   S   $ )zReturns the focal length in the y-direction.

Note that the focal lengths of the rectified left and right
camera are assumed to be equal.

Returns:
    torch.Tensor of shape :math:`(B)`

).r/   r/   r?   r;   s    r   fyStereoCamera.fy   rB   r   c                      U R                   S   $ )zuReturn the x-coordinate of the principal point for the left camera.

Returns:
    torch.Tensor of shape :math:`(B)`

.r      r?   r;   s    r   cx_leftStereoCamera.cx_left   s     )))44r   c                      U R                   S   $ )zvReturn the x-coordinate of the principal point for the right camera.

Returns:
    torch.Tensor of shape :math:`(B)`

rG   )r$   r;   s    r   cx_rightStereoCamera.cx_right   s     **955r   c                      U R                   S   $ )zReturn the y-coordinate of the principal point.

Note that the y-coordinate of the principal points
is assumed to be equal for the left and right camera.

Returns:
    torch.Tensor of shape :math:`(B)`

).r/   rH   r?   r;   s    r   cyStereoCamera.cy   rB   r   c                 <    U R                   S   * U R                  -  $ )zbThe horizontal baseline between the two cameras.

Returns:
    torch.Tensor of shape :math:`(B)`

r1   )r$   r@   r;   s    r   txStereoCamera.tx   s!     ++I66@@r   c                     U R                   $ )a'  The Q matrix of the horizontal stereo setup.

This matrix is used for reprojecting a disparity torch.Tensor to
the corresponding point cloud. Note that this is in a general form that allows different focal
lengths in the x and y direction.

Return:
    The Q matrix of shape :math:`(B, 4, 4)`.

)r*   r;   s    r   QStereoCamera.Q   s     ~~r   c                 L   [         R                  " U R                  SS4U R                  U R                  S9nU R
                  * nU R                  U-  USS2SS4'   U R                  * U R                  -  U-  USS2SS4'   U R                  U-  USS2SS4'   U R                  * U R                  -  U-  USS2SS4'   U R                  U R                  -  U-  USS2SS4'   U R                  * USS2SS4'   U R                  U R                  U R                  -
  -  USS2SS4'   U$ )zInitialize the Q matrix of the horizontal stereo setup. See the Q property.

Returns:
    The Q matrix of shape :math:`(B, 4, 4)`.

r0   )r'   r(   Nr   r-   r/   rH   )r4   zerosr<   r'   r(   rR   rD   rI   r@   rO   rL   )r   rU   baselines      r   r)   StereoCamera._init_Q_matrix   s
    KK!Q/4::V"&''WWx'!Q'
ggX,x7!Q'
WWx'!Q'
ggX'(2!Q'
WWtww&1!Q'
ggX!Q'
WWt}} <=!Q'
r   disparity_tensorc                 ,    [        XR                  5      $ )zReproject the disparity torch.Tensor to a 3D point cloud.

Args:
    disparity_tensor: Disparity torch.Tensor of shape :math:`(B, 1, H, W)`.

Returns:
    The 3D point cloud of shape :math:`(B, H, W, 3)`

)reproject_disparity_to_3DrU   )r   r[   s     r   r]   &StereoCamera.reproject_disparity_to_3D   s     ))966BBr   )r*   r'   r(   r#   r$   )r   r   r   r   r   r4   Tensorr   staticmethodr&   propertyintr<   r@   rD   rI   rL   rO   rR   rU   r)   r]   r   r   r   r   r!   r!   2   s   /ell /TYT`T` /ei /& :^ELL :^Z_ZfZf :^ko :^ :^x 3C 3 3 
5ELL 
5 
5 
5ELL 
5 
5 5 5 5 6%,, 6 6 
5ELL 
5 
5 AELL A A 5<<   $
C%,, 
C5<< 
Cr   r!   r[   r   c                    [        U [        R                  5      (       d  [        S[	        U 5       S35      e[        U R                  5      S:w  a  [        SU R                   S35      eU R                  S   S:w  a  [        SU R                   S35      eU R                  [        R                  [        R                  [        R                  [        R                  4;  a  [        SU R                   35      eg	)
zEnsure correct user provided correct disparity torch.Tensor.

Args:
    disparity_tensor: The disparity torch.Tensor of shape :math:`(B, 1, H, W)`.

zFExpected 'disparity_tensor' to be an instance of torch.Tensor but got r.   r0   z6Expected 'disparity_tensor' to have 4 dimensions. Got r/   z]Expected dimension 1 of 'disparity_tensor' to be 1 for as single channeled disparity map.Got zlExpected 'disparity_tensor' to have dtype torch.bfloat16, torch.float16, torch.float32 or torch.float64.Got N
isinstancer4   r_   r   typer2   r3   r(   bfloat16float16float32float64)r[   s    r   _check_disparity_tensorrl     s     &55TUYZjUkTllmn
 	
 !!"a' VWgWmWmVnnopqqb!Q&#))*!-
 	

 ennemmU]]TYTaTa%bb#))*,
 	
 cr   Q_matrixc                    [        U [        R                  5      (       d  [        S[	        U 5       S35      e[        U R                  5      S:X  d  [        SU R                   35      eU R                  SS S:X  d  [        SU R                   35      eU R                  [        R                  [        R                  [        R                  [        R                  4;  a  [        S	U R                   35      eg)
zEnsure Q matrix is of correct form.

Args:
    Q_matrix: The Q matrix for reprojecting disparity to a point cloud of shape :math:`(B, 4, 4)`

z>Expected 'Q_matrix' to be an instance of torch.Tensor but got r.   r-   z.Expected 'Q_matrix' to have 3 dimensions. Got r/   N)r0   r0   zFExpected last two dimensions of 'Q_matrix' to be of shape (4, 4). Got zeExpected 'Q_matrix' to be of type torch.bfloat16, torch.float16, torch.float32 or torch.float64. Got re   )rm   s    r   _check_Q_matrixro   "  s     h-- ^_cdl_m^nnopqqx~~!# Nx~~N^_``>>!"' fgogugufvwxx~~ennemmU]]EMMZZ>>"$
 	
 [r   c                 X   [        U5        [        U 5        U R                  u  p#pEU R                  nU R                  n[        X4SXvS9nUR                  USSS5      n[        R                  " USS9u  p[        R                  " U	S5      [        R                  " U
S5      p[        R                  " XU 4S5      R                  USS5      R                  SSS5      n[        X5      R                  X#US5      nUR                  X#US4:X  d  [        S	X#US4 S
UR                   S35      eU$ )a  Reproject the disparity torch.Tensor to a 3D point cloud.

Args:
    disparity_tensor: Disparity torch.Tensor of shape :math:`(B, H, W, 1)`.
    Q_matrix: torch.Tensor of Q matrices of shapes :math:`(B, 4, 4)`.

Returns:
    The 3D point cloud of shape :math:`(B, H, W, 3)`

F)normalized_coordinatesr'   r(   rd   )dimr/   r-   r   rH   z]Something went wrong in `reproject_disparity_to_3D`. Expected the final outputto be of shape z(.But the computed point cloud had shape zO. Please ensure input are correct. If this is an error, please submit an issue.)ro   rl   r3   r(   r'   r   expandr4   unbind	unsqueezestackreshapepermuter   r   )r[   rm   r<   rowscols_r(   r'   uvvuuvdpointss                r   r]   r]   9  s2    H,- 0 6 6Jd""E$$F	E&	^B	:r2r	*B<<#DA??1b!5??1b#9q
++q-.
2
:
::q"
M
U
UVWYZ\]
^Ch,44ZtQOF <<JdA66)q9: ;66<ll^ D\\
 	
 Mr   )typingr   r4   kornia.geometry.gridr   kornia.geometry.linalgr   	Exceptionr   r!   r_   rl   ro   r]   r   r   r   <module>r      s   $   0 35i 50QC QCh
ell 
t 
8
ell 
t 
.! ! !Y^YeYe !r   