+
    åûjÞw  ã                   óò   € R t ^ RIHtHtHt ^ RIt^ RIHtHt ^ RI	H
t
Ht ^ RIHt . ROt ! R R]4      t ! R	 R4      tR
 R ltR R lt]RR R ll4       t]R R l4       t]R R l4       tRR R lltR# )a¬  Rectified stereo: reproject disparity maps to 3D points, from the rig's projection matrices or from a Q matrix.

A disparity map is channels-last, ``(B, H, W, 1)``, and holds ``d = u_left - u_right`` in pixels, positive for a
point in front of the rig. Pixel ``(u, v)`` is (column, row) with integer pixel centres, as in
``cv2.reprojectImageTo3D``, and the points are ``(B, H, W, 3)``.

Which function to use:

- A calibrated rig given by its two rectified ``(B, 3, 4)`` projection matrices (``P1`` and ``P2`` of
  ``cv2.stereoRectify``): :class:`StereoCamera`, whose
  :meth:`~kornia.geometry.camera.stereo.StereoCamera.reproject_disparity_to_3D` returns points in the left
  rectified camera frame.
- A ``(B, 4, 4)`` ``Q`` matrix from elsewhere: :func:`reproject_disparity_to_3D`.
- The error both raise for the invalid rigs, disparity maps and ``Q`` matrices its docstring lists:
  :class:`StereoException`. A rig or ``Q`` batch that is neither 1 nor the disparity batch raises ``ValueError``,
  and left and right matrices with different batch sizes, other than a right batch of 1, raise ``RuntimeError``.
- A depth map instead of points: :func:`~kornia.geometry.depth.depth_from_disparity`.
)ÚAnyÚLiteralÚoverloadN)Ú_autocast_disabledÚis_compiling)Úconvert_points_from_homogeneousÚconvert_points_to_homogeneous)Úcreate_meshgridÚStereoCameraÚStereoExceptionc                   óB   a a€ ] tR t^0t oRtV3R lV 3R lltRtVtV ;t# )r   a<  Signal an invalid stereo rig, disparity map or Q matrix, with a link to the stereo docs.

``raise StereoException(msg)`` raises an exception whose message is ``msg`` followed by a link to the
:doc:`/geometry.camera.stereo` page. :class:`StereoCamera` raises it for projection matrices of the wrong
shape, on different devices or with different dtypes and, outside graph capture, for different intrinsics or a
baseline that is zero or points the wrong way; :func:`reproject_disparity_to_3D` raises it for a disparity map
or ``Q`` of the wrong type, shape or dtype. It derives from :class:`Exception` directly, so
``except ValueError`` does not catch it; a ``Q`` batch that is neither 1 nor the disparity batch raises
:class:`ValueError` instead. Left and right projection matrices with different batch sizes, other than a right
batch of 1, raise a plain :class:`RuntimeError` from the constructor, not this exception.

See Also:
    - :class:`StereoCamera`: the rig whose constructor checks raise it.
    - :func:`reproject_disparity_to_3D`: the function whose input checks raise it.

c                ó0   <€ V ^8„  d   QhRS[ RS[RS[RR/# )é   ÚmsgÚargsÚkwargsÚreturnN)Ústrr   )ÚformatÚ__classdict__s   "€ÚYC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\geometry\camera\stereo.pyÚ__annotate__ÚStereoException.__annotate__B   s)   ø€ ÷ 5ñ 5™Cð 5©ð 5±sð 5¸tñ 5ó    c                óD   <€ RpW,           p\         SV `  ! V.VO5/ VB  R# )a%  Construct custom exception for the :mod:`kornia.geometry.camera.stereo` module.

Appends a link to the stereo documentation page to ``msg``.

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   &&*,  €r   r   ÚStereoException.__init__B   s0   ø€ ðxð 	ð •Nˆ	ô 	‰Ò˜Ð4 TÒ4¨VÔ4r   © )	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r   Ú__static_attributes__Ú__classdictcell__Ú__classcell__)r    r   s   @@r   r   r   0   s   ù‡ € ñ÷"5÷ 5ó 5r   c                   ó²  a € ] tR t^Wt o RtV 3R lR lt]V 3R lR l4       t]V 3R lR l4       t	]V 3R lR	 l4       t
]V 3R
 lR l4       t]V 3R lR l4       t]V 3R lR l4       t]V 3R lR l4       t]V 3R lR l4       t]V 3R lR l4       tV 3R lR lt]R"V 3R lR ll4       t]V 3R lR l4       t]V 3R lR l4       tR"V 3R lR lltR tV tR!# )#r
   a3  Model a rectified horizontal stereo rig and reproject its disparity maps to 3D points.

``stereo = StereoCamera(P_left, P_right)`` takes the ``(B, 3, 4)`` rectified projection matrices of the left and
the right camera, such as ``P1`` and ``P2`` of ``cv2.stereoRectify``, and
``points = stereo.reproject_disparity_to_3D(disparity)`` takes a channels-last ``(B, H, W, 1)`` disparity map
``d = u_left - u_right``, in pixels, and returns the 3D point of every pixel in the left rectified camera
frame, ``(B, H, W, 3)``, in the units of the baseline: ``Z = fx * tx / (d - (cx_left - cx_right))``. Where the
homogeneous denominator ``W`` satisfies ``abs(W) > 1e-8`` the result matches ``cv2.reprojectImageTo3D`` with
OpenCV's ``Q``; elsewhere a placeholder is returned. ``W = -fy * (d - (cx_left - cx_right))``, which is
``-fy * d`` on a rig with equal ``cx``, so there a disparity with ``abs(fy * d) <= 1e-8``, zero included, gets
the placeholder (see the warning below). With a ``Q`` matrix from elsewhere, call
:func:`reproject_disparity_to_3D` instead; for a depth map rather than points,
:func:`~kornia.geometry.depth.depth_from_disparity`.

Convention:
    - the two arguments are the **rectified projection matrices** of the left and the right camera, each of
      shape ``(B, 3, 4)``: ``[[fx, 0, cx_left, 0], [0, fy, cy, 0], [0, 0, 1, 0]]`` for the left camera and
      ``[[fx, 0, cx_right, -tx * fx], [0, fy, cy, 0], [0, 0, 1, 0]]`` for the right one. The constructor
      requires the same ``fx``, ``fy`` and ``cy``; ``cx`` may differ.
    - the baseline is read back as the attribute :attr:`tx` ``= -P_right[0, 3] / fx``, which must be
      strictly **positive** for every rig in the batch: a zero baseline and swapped cameras both raise. A
      non-finite ``P_right[0, 3]`` is not screened, and these checks and the intrinsics check are
      skipped under graph capture (``torch.compile``, ``torch.export``).
      The :doc:`/geometry.camera.stereo` page's symbol ``tx`` is the negation of this attribute; :attr:`Q`
      is the page's matrix evaluated at the page's ``tx``.
    - ``Q[0, 0]`` carries ``fy`` and ``Q[1, 1]`` carries ``fx``; with the divide by
      ``W = -fy * (disparity - (cx_left - cx_right))`` the result is ``X = (u - cx_left) Z / fx``,
      ``Y = (v - cy) Z / fy``, ``Z = fx * tx / (disparity - (cx_left - cx_right))``.
    - ``u`` is the **column** index and ``v`` the **row** index, as in ``cv2.reprojectImageTo3D``.
    - a disparity map is channels-**last**, ``(B, H, W, 1)`` (a ``(B, 1, H, W)`` map is rejected),
      and the point cloud is ``(B, H, W, 3)``. On a rig with equal ``cx``, where ``abs(fy * d) > 1e-8``, a
      positive disparity puts the point in front of the rig and a negative one behind it; a smaller disparity,
      positive or not, gets the placeholder of the warning below. A rig with ``B = 1`` serves a batch of
      disparity maps; any other batch must match the disparity batch, else :class:`ValueError`.
    - pixel centres are integers; see the Convention block on
      :class:`~kornia.geometry.camera.pinhole.PinholeCamera`.
    - the constructor raises :class:`StereoException` for a matrix that is not ``(B, 3, 4)``, for matrices on
      different devices or with different dtypes and, outside graph capture, for a different ``fx``, ``fy`` or
      ``cy`` or a zero or reversed baseline. Left and right matrices with different batch sizes raise a plain
      :class:`RuntimeError` instead, except a right batch of 1, which is paired with every left matrix. The
      method raises :class:`StereoException` for a disparity map that is not a floating ``(B, H, W, 1)``
      tensor. An integer rig is accepted; its ``Q`` is in the default floating dtype, and a ``float16`` or
      ``bfloat16`` rig's ``Q`` is ``float32``.

.. warning::
    ``W = Q[3, 3] - fy * d`` with ``Q[3, 3] = fy * (cx_left - cx_right)``, so ``W = 0`` where the disparity
    equals ``cx_left - cx_right``: a point at infinity, which is ``d = 0`` on a rig with equal ``cx``. Wherever
    ``abs(W) <= 1e-8``, which includes small nonzero disparities such as ``d = 1e-9`` at ``fy = 1``, the divide
    is skipped and a finite placeholder, behind the camera on a real rig, is returned, so ``d > 0`` alone does
    not select points in front of the rig. Request
    ``return_mask=True`` from :meth:`reproject_disparity_to_3D` to identify numerical reconstruction failures
    without changing those coordinates or their gradients.

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

See Also:
    - :func:`reproject_disparity_to_3D`: the same reprojection from a ``Q`` matrix instead of the two
      projection matrices.
    - :func:`~kornia.geometry.depth.depth_from_disparity`: a depth map, ``fx * tx / d`` at every pixel,
      instead of points. It equals ``Z`` on a rig with equal ``cx``; otherwise pass
      ``d - (cx_left - cx_right)`` as the disparity.
    - :func:`~kornia.geometry.depth.depth_to_3d_v2`: points from a depth map; after
      :func:`~kornia.geometry.depth.depth_from_disparity` of that same disparity, the same points up to the
      ``1e-8`` that function adds to the disparity.
    - :class:`~kornia.geometry.camera.pinhole.PinholeCamera`: a single camera with intrinsics and
      world-to-camera extrinsics.
c                óR   <€ V ^8„  d   QhRS[ P                  RS[ P                  RR/# ©r   Úrectified_left_cameraÚrectified_right_camerar   N©ÚtorchÚTensor)r   r   s   "€r   r   ÚStereoCamera.__annotate__ž   s.   ø€ ÷ /ñ /©e¯l©lð /ÑTY×T`ÑT`ð /Ðeiñ /r   c                óÖ   € V P                  W4       Wn        W n        V P                  P                  V n        V P                  P                  V n        V P                  4       V n        R# )a  Class representing a horizontal stereo camera setup.

Args:
    rectified_left_camera: The rectified left camera projection matrix
      of shape ``(B, 3, 4)``
    rectified_right_camera: The rectified right camera projection matrix
      of shape ``(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ÔPØ3HÔ"Ø4JÔ#à×0Ñ0×7Ñ7ˆŒØ×/Ñ/×5Ñ5ˆŒ
à×,Ñ,Ó.ˆŽr   c                óR   <€ V ^8„  d   QhRS[ P                  RS[ P                  RR/# r-   r0   )r   r   s   "€r   r   r3   ²   s8   ø€ ÷ Nxñ Nx±E·L±Lð NxÑZ_×ZfÑZfð NxÐkoñ Nxr   c                óà  € \        V P                  4      ^8w  d   \        RV P                   R24      h\        VP                  4      ^8w  d   \        RVP                   R24      hV P                  RR R8w  d   \        RV P                  RR  R24      hVP                  RR R8w  d   \        RVP                  RR  R24      hV P                  VP                  8w  d'   \        RV P                   RVP                   R24      hV P                  VP                  8w  d'   \        R	V P                   RVP                   R24      h\        4       '       gŸ   \        P                  ! \        P                  ! V R,          VR,          4      4      '       d@   \        P                  ! \        P                  ! V R,          VR,          4      4      '       g!   \        R
V R,           RVR,           R24      hVR,          p\        4       '       g_   \        P                  ! V^ 8H  4      '       d   \        RV R24      h\        P                  ! V^ 8„  4      '       d   \        RV R24      hR# R# )a  Ensure user specified correct camera matrices.

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

z;Expected 'rectified_left_camera' to have 3 dimensions. Got Ú.z;Expected 'rectified_right_camera' to have 3 dimension. Got NzAExpected each 'rectified_left_camera' to be of shape (3, 4). Got zBExpected 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 zŠExpected 'rectified_left_camera' and 'rectified_right_camera' to have the same fx, fy and cy; only cx and the last column may differ. Got z¢Expected a non-zero stereo baseline, but :math:`T_x * f_x` is 0 for at least one camera pair, so `tx` is 0 and every disparity would reproject to the origin. Got zEExpected :math:`T_x * f_x` to be negative for every camera pair. Got éþÿÿÿ)é   é   ).ºNNN:Nr   N).ºé   NNr   ).rA   :Nr?   N©.é    r?   )
ÚlenÚshaper   r6   r7   r   r1   ÚallÚeqÚany)r.   r/   Útx_fxs   && r   r5   Ú!StereoCamera._check_stereo_camera±   s™  € ô Ð$×*Ñ*Ó+¨qÔ0Ü!ØMÐNc×NiÑNiÐMjÐjkÐlóð ô Ð%×+Ñ+Ó,°Ô1Ü!ØMÐNd×NjÑNjÐMkÐklÐmóð ð !×&Ñ& r sÐ+¨vÔ5Ü!ØSÐTi×ToÑToÐprÐpsÐTtÐSuÐuvÐwóð ð "×'Ñ'¨¨Ð,°Ô6Ü!ðØ-×3Ñ3°B°CÐ8Ð9¸ð<óð ð !×'Ñ'Ð+A×+HÑ+HÔHÜ!ðà,×3Ñ3Ð4°EÐ:P×:WÑ:WÐ9XÐXYð[óð ð !×&Ñ&Ð*@×*FÑ*FÔFÜ!ðà,×2Ñ2Ð3°5Ð9O×9UÑ9UÐ8VÐVWðYóð ô �~Š~Ü�IŠI”e—h’hÐ4°ZÕ@ÐBXÐYcÕBdÓe×fÒfÜ—	’	œ%Ÿ(š(Ð#8¸Õ#DÐF\Ð]gÕFhÓi×jÒjä!ðà,¨ZÕ8Ð9¸Ð?UÐV`Õ?aÐ>bÐbcðeóð ð ' yÕ1ˆÜ�~Š~Ü�yŠy˜ !™×$Ò$Ü%ð[Ø[`ÐZaÐabðdóð ô �yŠy˜ ™×#Ò#Ü%Ð(mÐnsÐmtÐtuÐ&vÓwÐwñ $ñ r   c                ó    <€ V ^8„  d   QhRS[ /# ©r   r   )Úint)r   r   s   "€r   r   r3     s   ø€ ÷ 3ñ 3™Cñ 3r   c                ó<   € V P                   P                  ^ ,          # )zOReturn the batch size of the storage.

Returns:
   scalar with the batch size

)r.   rG   ©r   s   &r   Ú
batch_sizeÚStereoCamera.batch_size  s   € ð ×)Ñ)×/Ñ/°Õ2Ð2r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3     ó   ø€ ÷ 
5ñ 
5‘E—L‘Lñ 
5r   c                ó(   € V P                   R,          # )z¶Return 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 ``(B)``

).rE   rE   ©r.   rQ   s   &r   ÚfxÚStereoCamera.fx  ó   € ð ×)Ñ)¨)Õ4Ð4r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3     rU   r   c                ó(   € V P                   R,          # )z·Returns 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 ``(B)``

).rC   rC   rW   rQ   s   &r   ÚfyÚStereoCamera.fy  rZ   r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   '  s   ø€ ÷ 5ñ 5™Ÿ™ñ 5r   c                ó(   € V P                   R,          # )zqReturn the x-coordinate of the principal point for the left camera.

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

©.rE   r   rW   rQ   s   &r   Úcx_leftÚStereoCamera.cx_left&  s   € ð ×)Ñ)¨)Õ4Ð4r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   1  s   ø€ ÷ 6ñ 6™%Ÿ,™,ñ 6r   c                ó(   € V P                   R,          # )zrReturn the x-coordinate of the principal point for the right camera.

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

ra   )r/   rQ   s   &r   Úcx_rightÚStereoCamera.cx_right0  s   € ð ×*Ñ*¨9Õ5Ð5r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   ;  rU   r   c                ó(   € V P                   R,          # )zÇReturn 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 ``(B)``

).rC   r   rW   rQ   s   &r   ÚcyÚStereoCamera.cy:  rZ   r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   H  s   ø€ ÷ &ñ &‘E—L‘Lñ &r   c                óÄ   € V P                   R,          pVP                  4       '       g%   VP                  \        P                  ! 4       4      pV) V P
                  ,          # )z¶The horizontal baseline between the two cameras.

Integer translations are promoted to the default floating-point dtype before negation.

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

rD   )r/   Úis_floating_pointÚtor1   Úget_default_dtyperX   )r   Útranslations   & r   ÚtxÚStereoCamera.txG  sK   € ð ×1Ñ1°)Õ<ˆØ×,Ñ,×.Ò.Ø%Ÿ.™.¬×)@Ò)@Ó)BÓCˆKØˆ|˜dŸg™gÕ%Ð%r   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   W  s   ø€ ÷ ñ ‘5—<‘<ñ r   c                ó   € V P                   # )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.

Integer camera matrices use the default floating-point dtype for Q; ``float32`` and ``float64`` cameras
retain their dtype. ``float16`` and ``bfloat16`` rigs get a ``float32`` Q: a real rig's
``Q[2, 3] = fx * fy * baseline`` exceeds the ``float16`` range (``-2.8e5`` for a 721 px focal length and a
0.54 m baseline), and reprojection then runs in ``float32`` and returns the disparity dtype. The stored
camera matrices and intrinsic parameter properties keep the rig's dtype.

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

)r9   rQ   s   &r   ÚQÚStereoCamera.QV  s   € ð$ �~‰~Ðr   c                ó4   <€ V ^8„  d   QhRS[ P                  /# rN   r0   )r   r   s   "€r   r   r3   j  s   ø€ ÷ ñ ¡§¡ñ r   c                ó®  € V P                   P                  4       pV'       d   V P                  M\        P                  ! 4       pV\        P
                  \        P                  39   d   \        P                  MTpV P                  P                  V4      V P                  P                  V4      rTV P                  P                  V4      V P                  P                  V4      V P                  P                  V4      r‡pW28X  d   V P                  ) p	M)V P                  R,          P                  V4      V,          p	\        P                   ! V P"                  ^^3V P$                  VR7      p
WY,          V
R&   V) V,          V	,          V
R&   WI,          V
R&   V) V,          V	,          V
R&   WE,          V	,          V
R&   V) V
R&   WVV,
          ,          V
R	&   V
# )
z€Initialize the Q matrix of the horizontal stereo setup. See the Q property.

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

)r6   r7   rD   )rA   rE   rE   )rA   rE   r?   )rA   rC   rC   )rA   rC   r?   )rA   r   r?   )rA   r?   r   )rA   r?   r?   )r.   rn   r7   r1   rp   Úfloat16Úbfloat16Úfloat32rX   ro   r]   rb   rf   rj   rr   r/   ÚzerosrR   r6   )r   Úfloating_camerar7   ÚworkrX   r]   rb   rf   rj   Úbaselinerv   s   &          r   r8   ÚStereoCamera._init_Q_matrixj  se  € ð ×4Ñ4×FÑFÓHˆß-�—
’
´5×3JÒ3JÓ3Lˆð !&¬%¯-©-¼¿¹Ð)HÔ HŒu�}Š}ÈeˆØ—‘—‘˜DÓ! 4§7¡7§:¡:¨dÓ#3ˆBØ $§¡§¡°Ó 5°t·}±}×7GÑ7GÈÓ7MÈtÏwÉwÏzÉzÐZ^ÓO_˜2ˆØŒ=Ø&*§g¡g X‰Hà×2Ñ2°9Õ=×@Ñ@ÀÓFÈÕKˆHÜ�KŠK˜Ÿ™¨!¨QÐ/¸¿¹È4ÔPˆØ•]ˆˆ'‰
Ø�S˜7•] XÕ-ˆˆ'‰
Ø•]ˆˆ'‰
Ø�S˜2•X Õ(ˆˆ'‰
Ø•W˜xÕ'ˆˆ'‰
Ø�Sˆˆ'‰
Ø XÕ-Õ.ˆˆ'‰
Øˆr   c                ób   <€ V ^8„  d   QhRS[ P                  RS[R,          RS[ P                  /# )r   Údisparity_tensorÚreturn_maskFr   ©r1   r2   r   )r   r   s   "€r   r   r3   ˆ  s1   ø€ ÷ ñ Ù %§¡ðÙ;BÀ5½>ðá	�‰ñr   c                ó   € R # ©Nr"   ©r   rƒ   r„   s   &&&r   Úreproject_disparity_to_3DÚ&StereoCamera.reproject_disparity_to_3D‡  s   € ñ r   c                óŒ   <€ V ^8„  d   QhRS[ P                  RS[R,          RS[S[ P                  S[ P                  3,          /# )r   rƒ   r„   Tr   ©r1   r2   r   Útuple)r   r   s   "€r   r   r3   �  s?   ø€ ÷ 0ñ 0Ù %§¡ð0Ù;BÀ4½=ð0á	‰u�|‰|™UŸ\™\Ð)Õ	*ñ0r   c                ó   € R # r‡   r"   rˆ   s   &&&r   r‰   rŠ   Œ  s   € ñ -0r   c          	      ó¢   <€ V ^8„  d   QhRS[ P                  RS[RS[ P                  S[S[ P                  S[ P                  3,          ,          /# ©r   rƒ   r„   r   ©r1   r2   Úboolr�   )r   r   s   "€r   r   r3   ’  sD   ø€ ÷ ?ñ ?Ù %§¡ð?Ù;?ð?á	�‰™™eŸl™l©E¯L©LÐ8Õ9Õ	9ñ?r   c                ó   € R # r‡   r"   rˆ   s   &&&r   r‰   rŠ   ‘  s   € ñ <?r   c          	      ó¢   <€ V ^8„  d   QhRS[ P                  RS[RS[ P                  S[S[ P                  S[ P                  3,          ,          /# r�   r‘   )r   r   s   "€r   r   r3   –  sI   ø€ ÷ \ñ \Ù %§¡ð\Ù;?ð\á	�‰™™eŸl™l©E¯L©LÐ8Õ9Õ	9ñ\r   c                ó0   € \        WP                  VR7      # )a   Reproject the disparity torch.Tensor to a 3D point cloud.

See the Convention block on :class:`~kornia.geometry.camera.stereo.StereoCamera`.

Args:
    disparity_tensor: Disparity torch.Tensor of shape ``(B, H, W, 1)``.
    return_mask: If True, also return numerical reconstruction validity with
        shape ``(B, H, W, 1)``. This only adds diagnostics; it does not fill
        invalid points or change their gradients. See the module-level
        :func:`reproject_disparity_to_3D` for the mask's complete contract.

Returns:
    The 3D point cloud of shape ``(B, H, W, 3)``, in the left rectified
    camera frame, or ``(points, valid)`` when ``return_mask=True``.

)r„   )r‰   rv   rˆ   s   &&&r   r‰   rŠ   –  s   € ô& )Ð)9¿6¹6È{Ô[Ð[r   )r9   r6   r7   r.   r/   N©F)r#   r$   r%   r&   r'   r   Ústaticmethodr5   ÚpropertyrR   rX   r]   rb   rf   rj   rr   rv   r8   r   r‰   r(   r)   )r   s   @r   r
   r
   W   s,  ø‡ € ñD÷L/ð /ð& ÷Nxó ðNxð` ÷3ó ð3ð ÷
5ó ð
5ð ÷
5ó ð
5ð ÷5ó ð5ð ÷6ó ð6ð ÷
5ó ð
5ð ÷&ó ð&ð ÷ó ð÷&ð ð: ÷ñ ó ðð ÷0ó ð0ð ÷?ó ð?÷\÷ \ð \r   c                ó<   € V ^8„  d   QhR\         P                  RR/# )r   rƒ   r   Nr0   )r   s   "r   r   r   ¬  s   € ÷ 
ñ 
¬e¯l©lð 
¸tñ 
r   c                ó  € \        V \        P                  4      '       g   \        R\	        V 4       R24      h\        V P                  4      ^8w  d   \        RV P                   R24      hV P                  R,          ^8w  d   \        RV P                   R24      hV P                  \        P                  \        P                  \        P                  \        P                  39  d   \        RV P                   24      hR# )z‘Ensure correct user provided correct disparity torch.Tensor.

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

zFExpected 'disparity_tensor' to be an instance of torch.Tensor but got r=   z6Expected 'disparity_tensor' to have 4 dimensions. Got zvExpected 'disparity_tensor' to have channels-last shape (B, H, W, 1) with a single channel in the last dimension. Got zlExpected 'disparity_tensor' to have dtype torch.bfloat16, torch.float16, torch.float32 or torch.float64.Got Néÿÿÿÿ©Ú
isinstancer1   r2   r   ÚtyperF   rG   r7   r{   rz   r|   Úfloat64)rƒ   s   &r   Ú_check_disparity_tensorr    ¬  sù   € ô Ð&¬¯©×5Ò5ÜØTÔUYÐZjÓUkÐTlÐlmÐnó
ð 	
ô Ð×!Ñ!Ó" aÔ'ÜÐ VÐWg×WmÑWmÐVnÐnoÐpÓqÐqà×Ñ˜bÕ! QÔ&Üðà#×)Ñ)Ð*¨!ð-ó
ð 	
ð ×Ñ¤e§n¡n´e·m±mÄUÇ]Á]ÔTY×TaÑTaÐ%bÔbÜðØ#×)Ñ)Ð*ð,ó
ð 	
ñ cr   c                ó<   € V ^8„  d   QhR\         P                  RR/# )r   ÚQ_matrixr   Nr0   )r   s   "r   r   r   É  s   € ÷ 
ñ 
œeŸl™lð 
¨tñ 
r   c                ó  € \        V \        P                  4      '       g   \        R\	        V 4       R24      h\        V P                  4      ^8X  g   \        RV P                   24      hV P                  R,          R8X  g   \        RV P                   24      hV P                  \        P                  \        P                  \        P                  \        P                  39  d   \        RV P                   24      hR# )	zŠEnsure Q matrix is of correct form.

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

z>Expected 'Q_matrix' to be an instance of torch.Tensor but got r=   z.Expected 'Q_matrix' to have 3 dimensions. Got rB   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 N)r@   r@   rœ   )r¢   s   &r   Ú_check_Q_matrixr¤   É  sÔ   € ô �h¤§¡×-Ò-ÜÐ ^Ô_cÐdlÓ_mÐ^nÐnoÐpÓqÐqäˆx�~‰~Ó !Ô#ÜÐ NÈxÏ~É~ÐN^Ð_Ó`Ð`à�>‰>˜"Õ Ô'ÜÐ fÐgo×guÑguÐfvÐwÓxÐxà‡~�~œeŸn™n¬e¯m©m¼U¿]¹]ÌEÏMÉMÐZÔZÜðØ—>‘>Ð"ð$ó
ð 	
ñ [r   c                ó’   € V ^8„  d   QhR\         P                  R\         P                  R\        R,          R\         P                  /# )r   rƒ   r¢   r„   Fr   r…   )r   s   "r   r   r   á  s=   € ÷ ñ Ü—l‘lðÜ.3¯l©lðÜIPÐQVÍðä
‡\�\ñr   c                 ó   € R # r‡   r"   ©rƒ   r¢   r„   s   &&&r   r‰   r‰   à  s   € ñ r   c          
      óÈ   € V ^8„  d   QhR\         P                  R\         P                  R\        R,          R\        \         P                  \         P                  3,          /# )r   rƒ   r¢   r„   Tr   rŒ   )r   s   "r   r   r   ç  sK   € ÷ ,ñ ,Ü—l‘lð,Ü.3¯l©lð,ÜIPÐQUÍð,ä
Œ5�<‰<œŸ™Ð%Õ&ñ,r   c                 ó   € R # r‡   r"   r§   s   &&&r   r‰   r‰   æ  s   € ñ ),r   c                óä   € V ^8„  d   QhR\         P                  R\         P                  R\        R\         P                  \        \         P                  \         P                  3,          ,          /# ©r   rƒ   r¢   r„   r   r‘   )r   s   "r   r   r   í  sO   € ÷ ;ñ ;Ü—l‘lð;Ü.3¯l©lð;ÜIMð;ä
‡\�\”Eœ%Ÿ,™,¬¯©Ð4Õ5Õ5ñ;r   c                 ó   € R # r‡   r"   r§   s   &&&r   r‰   r‰   ì  s   € ñ 8;r   c                óä   € V ^8„  d   QhR\         P                  R\         P                  R\        R\         P                  \        \         P                  \         P                  3,          ,          /# r«   r‘   )r   s   "r   r   r   ò  sU   € ÷ oñ oÜ—l‘lðoÜ.3¯l©lðoÜIMðoä
‡\�\”Eœ%Ÿ,™,¬¯©Ð4Õ5Õ5ñor   c                ó€  € \        V4       \        V 4       V P                  w  r4rVV P                  pV P                  pVP                  ^ ,          V8w  d$   VP                  ^ ,          ^8w  d   \        R4      h\        WERW‡R7      p	V	P                  VRRR4      p	\        P                  ! V	RR7      w  r«\        P                  ! V
R4      \        P                  ! VR4      rº\        P                  ! W«V 3^4      P                  V^WE,          4      P                  ^ ^^4      pWE,          ^ 8X  dF   VP                  W4V^4      pV'       d)   V\        P                  ! V \        P                  R7      3# V# \!        V4      P#                  VP                  4      pVP                  VRR4      P%                  RR4      p\        P&                  P)                  4       '       d   \        P*                  ! Wï4      pM<\-        VP.                  4      ;_uu_ 4        \        P*                  ! Wï4      pRRR4       Rp\1        XVR7      P#                  V4      P                  W4V^4      pVP                  W4V^38X  g    \3        R	W4V^3 R
VP                   R24      hV'       dŽ   \        P4                  ! V4      P7                  RRR7      VRRR13,          P9                  4       V8„  ,          pVP                  W4V^4      \        P4                  ! V4      P7                  RRR7      ,          pVV3# V#   + '       g   i     EL
; i)a×  Reproject a disparity map to 3D points with a Q matrix, as cv2.reprojectImageTo3D does.

``points = reproject_disparity_to_3D(disparity, Q)`` takes a channels-last ``(B, H, W, 1)`` disparity map and
``Q`` matrices ``(B, 4, 4)`` or ``(1, 4, 4)``, and returns ``(X / W, Y / W, Z / W)`` of
``Q (u, v, d, 1)`` for every pixel ``(u, v)`` = (column, row), ``(B, H, W, 3)``, in the frame ``Q`` defines.
Where ``abs(W) <= 1e-8``, as at a zero disparity when ``Q[3, 3] = 0``, it returns the undivided ``(X, Y, Z)``,
while OpenCV divides (infinities at ``W = 0``); see the Note. With ``return_mask=True`` it also returns a
``(B, H, W, 1)`` bool mask of numerically valid points. For a rig given by its two rectified projection
matrices, :class:`StereoCamera` builds ``Q``; for a depth map rather than points, use
:func:`~kornia.geometry.depth.depth_from_disparity`.

See the Convention block on :class:`~kornia.geometry.camera.stereo.StereoCamera`.

Args:
    disparity_tensor: Disparity torch.Tensor of shape ``(B, H, W, 1)``.
    Q_matrix: torch.Tensor of Q matrices of shape ``(B, 4, 4)`` or ``(1, 4, 4)``.
    return_mask: If True, also return a boolean numerical reconstruction mask of
        shape ``(B, H, W, 1)``, on the input device. Defaults to False.

Returns:
    The 3D point cloud of shape ``(B, H, W, 3)``, or ``(points, valid)`` when
    ``return_mask=True``. Points retain the disparity dtype in either mode.

Raises:
    ValueError: If the Q matrix batch size is neither 1 nor the disparity batch size.
    StereoException: If ``disparity_tensor`` or ``Q_matrix`` is not a floating tensor of the documented shape.

See Also:
    - :class:`StereoCamera`: builds ``Q`` from the two rectified projection matrices of a rig.
    - :func:`~kornia.geometry.depth.depth_from_disparity`: a depth map instead of points, from a baseline and a
      focal length.
    - :func:`~kornia.geometry.depth.depth_to_3d_v2`: points from a depth map and a ``3x3`` ``K``.

Note:
    A point is valid when all four homogeneous components are finite,
    ``abs(W) > 1e-8`` in the converter's working dtype, and XYZ is finite after
    conversion to the returned disparity dtype. The matrix multiplication and
    homogeneous conversion use Q's dtype, which a caller's autocast region does not
    change, outside TorchScript, where a scripted call keeps the caller's precision.
    The absolute W cutoff is the existing coordinate policy: scaling Q can change
    validity near the cutoff, while Q and -Q have the same mask.

    This mask does not certify a stereo match, front-of-camera eligibility,
    image bounds or occlusion. In particular, zero disparity can reconstruct a
    finite point when Q has a nonzero offset. Combine the mask with the matcher's
    own validity information. The output frame is defined by Q; for a
    :class:`StereoCamera` it is the left rectified camera frame.

    Requesting the mask changes neither coordinates nor gradients. Below the W
    cutoff, the converter still returns the homogeneous numerator as a placeholder;
    nonfinite inputs can still produce nonfinite coordinates or gradients. This is
    a diagnostic API, not a safe invalid-point filling policy. Masking a loss after
    unsafe arithmetic is not guaranteed to prevent nonfinite gradients.

z8Q_matrix batch size must match disparity_tensor or be 1.F)Únormalized_coordinatesr6   r7   )Údim)r7   Ng:Œ0âŽyE>)Úepsz]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.T)Úkeepdim.r›   r>   )r¤   r    rG   r7   r6   Ú
ValueErrorr	   Úexpandr1   ÚunbindÚ	unsqueezeÚstackÚreshapeÚpermuteÚ
empty_liker’   r   ro   Ú	transposeÚjitÚis_scriptingÚbmmr   rž   r   r   ÚisfiniterH   Úabs)rƒ   r¢   r„   rR   ÚrowsÚcolsÚ_r7   r6   ÚuvÚuÚvÚuvdÚpointsÚuvd_hÚQ_tÚhomogeneousr±   Úvalids   &&&                r   r‰   r‰   ò  sÅ  € ôt �HÔÜÐ,Ô-à 0× 6Ñ 6Ñ€J�dØ×"Ñ"€EØ×$Ñ$€FØ‡~�~�aÕ˜JÔ&¨8¯>©>¸!Õ+<ÀÔ+AÜÐSÓTÐTä	˜¸EÈ&Ô	^€BØ	�‰�:˜r 2 rÓ	*€Bô �<Š<˜ Ô#�D€AÜ�?Š?˜1˜bÓ!¤5§?¢?°1°bÓ#9€qä
�+Š+�qÐ-Ð.°Ó
2×
:Ñ
:¸:ÀqÈ$Í+Ó
V×
^Ñ
^Ð_`ÐbcÐefÓ
g€Cð …{�aÔØ—‘˜Z¨t°QÓ7ˆßØœ5×+Ò+Ð,<ÄEÇJÁJÔOÐOÐOØˆô
 *¨#Ó.×1Ñ1°(·.±.ÓA€EØ
�/‰/˜* b¨"Ó
-×
7Ñ
7¸¸BÓ
?€CÜ‡y�y×Ñ×ÒÜ—i’i Ó+‰ä §¡×,Õ,ÜŸ)š) EÓ/ˆK÷ -à
€CÜ,¨[¸cÔB×EÑEÀeÓL×TÑTÐU_ÐgkÐmnÓo€Fð �<‰<˜J¨d°AÐ6Ô6ÜðØ)°°qÐ9Ð:ð ;6Ø6<·l±l°^ð D\ð\ó
ð 	
÷ Ü—’˜{Ó+×/Ñ/°¸DÐ/ÓAÀ[ÐQTÐVXÑVYÐQYÕEZ×E^ÑE^ÓE`ÐcfÑEfÕgˆØ—‘˜j°°aÓ8¼5¿>º>È&Ó;Q×;UÑ;UÐVXÐbfÐ;UÓ;gÕgˆØ�uˆ}ÐØ€M÷# -×,Ð,ús   ÈL,Ì,L=	)r
   r   r‰   r–   )r'   Útypingr   r   r   r1   Úkornia.core.utilsr   r   Úkornia.geometry.conversionsr   r   Úkornia.geometry.gridr	   Ú__all__Ú	Exceptionr   r
   r    r¤   r‰   r"   r   r   Ú<module>rÓ      s‘   ðñ$÷& *Ñ )ã ç >ß fÝ 0â
J€ô$5�iô $5÷NR\ñ R\õj

õ:
ð. 
öó 
ðð
 
ô,ó 
ð,ð
 
ô;ó 
ð;÷
oñ or   