+
    åûjÕ  ã                  ó®  € R t ^ RIHt ^ RIHt ^ RIt^ RIHu Ht	 ^ RIHt ^ RI
HtHtHtHt ^ RIHt ^ RIHtHtHtHtHt ^ RIHt ^ R	IHt ^R
IHtHtHtHtH t  ^RI!H"t" ^RI#H$t$H%t% ^RI&H't'H(t( . R$Ot)R R lt*R%R R llt+R&R R llt,R'R R llt-R'R R llt.R(R R llt/R'R R llt0 ! R R]Pb                  4      t2R)R R llt3R  R! lt4R" R# lt5R# )*a¡  Depth maps: unproject them to 3D points and normals, get depth from stereo or planes, and warp images by depth.

Functions that take ``camera_matrix`` expect pixel intrinsics of shape ``(B, 3, 3)`` with integer pixel
centres, and read ``depth`` as the camera-frame ``z`` (the Euclidean ray length with ``normalize_points=True``).

Which function to use:

- 3D points from a depth map: :func:`depth_to_3d` returns ``(B, 3, H, W)``; :func:`depth_to_3d_v2` returns
  ``(*, H, W, 3)`` and can reuse the rays from :func:`unproject_meshgrid` for every frame of one camera.
- Surface normals from a depth map: :func:`depth_to_normals`.
- Depth from a rectified stereo disparity map: :func:`depth_from_disparity`. Depth of given pixels on a known
  plane: :func:`depth_from_plane_equation`.
- An image rendered from another viewpoint by depth: :func:`warp_frame_depth` (a 4x4 pose and one camera
  matrix), or :class:`DepthWarper` and :func:`depth_warp` (two :class:`~kornia.geometry.camera.pinhole.PinholeCamera`
  models).
- Selected pixels instead of whole maps: :func:`~kornia.geometry.camera.perspective.unproject_points` and
  :func:`~kornia.geometry.camera.perspective.project_points`.
)Úannotations)ÚOptionalN)Únn)ÚKORNIA_CHECKÚKORNIA_CHECK_IS_TENSORÚKORNIA_CHECK_SHAPEÚKORNIA_CHECK_SIZES)Ú
ShapeError)Ú_autocast_disabledÚ_reconcile_under_autocastÚ_samples_half_in_float32Ú_torch_grid_sampleÚ_torch_inverse_cast)Úspatial_gradient)Úcreate_meshgrid)ÚPinholeCameraÚ	cam2pixelÚ	pixel2camÚproject_pointsÚunproject_points)Ú_embed_intrinsics)Únormalize_pixel_coordinatesÚ normalize_points_with_intrinsics)Úconvert_points_to_homogeneousÚtransform_pointsÚDepthWarperc               ó$   € V ^8„  d   QhRRRRRR/# )é   Údepthútorch.TensorÚcamera_matrixÚreturnztorch.dtype© )Úformats   "ÚQC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\geometry\depth.pyÚ__annotate__r%   K   s!   € ÷ ñ ˜\ð ¸,ð È;ñ ó    c                ó2  € V P                  4       pVP                  4       pV'       ga   V'       gY   V P                  4       '       gC   VP                  4       '       g-   \        P                  ! ^ V P                  R7      P
                  # \        P                  ! V P
                  VP
                  4      pV P                  4       '       g   VP                  4       '       d8   \        P                  ! ^ V P                  VR7      P                  P
                  # V# )a<  Return the dtype the pixel grid is built in before unprojection.

The grid holds the integer pixel indices at the precision of the promoted inputs: a bfloat16 or
float16 grid rounds them, and an integer grid cannot represent them. Integer inputs promote to
the default floating dtype instead: ``promote_types`` does not support unsigned/signed integer
pairs. A complex promotion keeps a real grid, because ``arange`` has no complex kernels and the
subsequent arithmetic promotes to complex anyway, which also unblocks complex inputs. An all-half
call keeps its half grid.
)Údevice©r(   Údtype)Úis_floating_pointÚ
is_complexÚtorchÚemptyr(   r*   Úpromote_typesÚreal)r   r    Údepth_is_floatÚcamera_is_floatÚpromoteds   &&   r$   Ú_depth_grid_dtyper4   K   sÀ   € ð ×,Ñ,Ó.€NØ#×5Ñ5Ó7€Oß§/¸%×:JÑ:J×:LÒ:LÐUb×UmÑUm×UoÒUoÜ�{Š{˜1 U§\¡\Ô2×8Ñ8Ð8Ü×"Ò" 5§;¡;°×0CÑ0CÓD€Hð ×Ñ×Ò˜]×5Ñ5×7Ò7Ü�{Š{˜1 U§\¡\¸ÔB×GÑG×MÑMÐMØ€Or&   c               ó4   € V ^8„  d   QhRRRRRRRRRR	R
RRR/# )r   ÚheightÚintÚwidthr    r   Únormalize_pointsÚboolr(   zOptional[torch.device]r*   zOptional[torch.dtype]r!   r"   )r#   s   "r$   r%   r%   a   sN   € ÷ ;ñ ;Øð;àð;ð  ð;ð ð	;ð
 #ð;ð !ð;ð ñ;r&   c                óÜ   € \        V. RO4       \        WRWER7      P                  ^ 4      pVR,          p\        Wg4      p\	        V4      p	V'       d   \
        P                  ! V	RRR7      p	V	# )a8  Compute the camera ray through every pixel, to reuse across frames with depth_to_3d_v2.

``rays = unproject_meshgrid(H, W, camera_matrix)`` returns the camera-frame ray of each pixel as a
``(B, H, W, 3)`` tensor, scaled so that ``z`` is 1 by default (unit length with ``normalize_points=True``). It
takes no depth: multiply the rays by ``depth[..., None]`` for a ``(B, H, W)`` depth map, or pass them to
:func:`depth_to_3d_v2` as ``xyz_grid``, to get the 3D points of many frames from one camera without rebuilding
the grid each time.

Convention:
    - pixel centres are integers; see :class:`~kornia.geometry.camera.pinhole.PinholeCamera`.
    - ``camera_matrix`` must be ``(B, 3, 3)``; an unbatched matrix or extra leading dimensions raise
      ``ShapeError``.

Args:
    height: height of image.
    width: width of image.
    camera_matrix: tensor containing the camera intrinsics with shape ``(B, 3, 3)``.
    normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth is
      represented as the Euclidean ray length from the camera position.
    device: device to place the result on.
    dtype: dtype of the pixel grid; the output dtype follows promotion with ``camera_matrix``.

Return:
    the ray through each pixel, with ``z`` equal to 1 (or unit length with ``normalize_points=True``), with
    shape ``(B, H, W, 3)``.

See Also:
    - :func:`depth_to_3d_v2`: takes these rays as ``xyz_grid`` and scales them by a depth map.
    - :func:`~kornia.geometry.camera.perspective.unproject_points`: the camera-frame point of selected pixels.

F©Únormalized_coordinatesr(   r*   ç       @©ÚdimÚp©ÚBÚ3rD   ©ºNNNNNéÿÿÿÿ)r   r   Úsqueezer   r   ÚFÚ	normalize)
r6   r8   r    r9   r(   r*   Ú	points_uvÚcamera_matrix_tmpÚ	points_xyÚ
points_xyzs
   &&&&&&    r$   Úunproject_meshgridrO   a   sl   € ôN �}¢oÔ6ô
 .Ø¨e¸Fôç�gˆaƒjð ð
 '4°MÕ&BÐä0°ÓN€Iô /¨yÓ9€JçÜ—[’[ °°sÔ;ˆ
àÐr&   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR/# )	r   r   r   r    r9   r:   Úxyz_gridzOptional[torch.Tensor]r!   r"   )r#   s   "r$   r%   r%   Ÿ   sA   € ÷ K)ñ K)ØðK)àðK)ð ðK)ð %ð	K)ð
 ñK)r&   c                óä   € \        V . RO4       \        V. RO4       V P                  RR w  rEVe   TM \        WEWV P                  \	        W4      4      p\        V. RO4       W`R,          ,          # )aH  Unproject a depth map to a 3D point per pixel, channels last.

``points = depth_to_3d_v2(depth, camera_matrix)`` takes a ``(*, H, W)`` map of camera-frame depths and
``(B, 3, 3)`` pixel intrinsics, and returns the camera-frame point of every pixel as a ``(*, H, W, 3)``
tensor. The ``(B, H, W, 3)`` rays of the ``B`` cameras broadcast against ``depth`` by torch's rules, so the
camera batch lines up with the axis of ``depth`` just before ``H``, not with its first axis: an ``(H, W)`` depth
gives one map per camera, a ``(B, H, W)`` depth pairs camera ``b`` with ``depth[b]``, and a ``(B, T, H, W)``
depth pairs camera ``t`` with ``depth[:, t]`` when ``T == B``. With ``T == 1`` it pairs every camera with every
map and returns ``(B, B, H, W, 3)``, so pass ``depth[:, 0]``; any other ``T`` raises, unless there is a single
camera, which then serves every map. For one camera per ``(B, T, H, W)`` batch element, pass
``xyz_grid=unproject_meshgrid(H, W, camera_matrix)[:, None]``; the same grid also saves rebuilding the pixel
rays for many frames of one camera. For a channels-first ``(B, 3, H, W)`` result, use :func:`depth_to_3d`.

Convention:
    - pixel ``(u, v)``, with ``u`` the column and ``v`` the row, maps to ``((u - cx) z / fx, (v - cy) z / fy, z)``.
      Pixel centres are integers, as in :class:`~kornia.geometry.camera.pinhole.PinholeCamera`.
      ``depth_to_3d_v2(depth[:, 0], K)`` equals ``depth_to_3d(depth, K).permute(0, 2, 3, 1)``.
    - without ``xyz_grid``, ``camera_matrix`` must be exactly ``(B, 3, 3)`` (another shape raises
      ``ShapeError``); with ``xyz_grid`` only its shape is checked.
    - ``normalize_points=True`` reads ``depth`` as the Euclidean ray length instead of ``z``. With ``xyz_grid`` it
      is ignored: build the grid with the same ``normalize_points``.
    - without ``xyz_grid``, the pixel grid is built in ``promote_types(depth.dtype, camera_matrix.dtype)``
      when either input is floating (the default floating dtype when both are integer), so half-precision
      depth does not round the pixel indices below the intrinsics' precision.

Args:
    depth: image tensor containing a depth value per pixel with shape ``(*, H, W)``.
    camera_matrix: tensor containing the camera intrinsics with shape ``(B, 3, 3)``.
    normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth is
      represented as the Euclidean ray length from the camera position.
    xyz_grid: explicit xyz point values.

Return:
    tensor with a 3d point per pixel of the same resolution as the input, ``(*, H, W, 3)``, whose
    leading dimensions are the broadcast of ``depth``'s and the grid's: the Example broadcasts a
    ``(4, 4)`` depth against a ``(2, 3, 3)`` camera to ``(2, 4, 4, 3)``, and a
    ``(B, T, H, W)`` depth with an ``xyz_grid`` of shape ``(B, 1, H, W, 3)`` returns
    ``(B, T, H, W, 3)``.

See Also:
    - :func:`depth_to_3d`: the same points channels first, ``(B, 3, H, W)``.
    - :func:`unproject_meshgrid`: the per-pixel rays to pass as ``xyz_grid``.
    - :func:`depth_to_normals`: per-pixel surface normals, from a ``(B, 1, H, W)`` depth.
    - :func:`~kornia.geometry.camera.perspective.unproject_points`: the camera-frame point of selected pixels.

Example:
    >>> depth = torch.rand(4, 4)
    >>> K = torch.eye(3).repeat(2,1,1)
    >>> depth_to_3d_v2(depth, K).shape
    torch.Size([2, 4, 4, 3])

N©Ú*ÚHÚW)rT   rD   rD   éþÿÿÿ)rT   rU   rV   rD   ).N)r   ÚshaperO   r(   r4   )r   r    r9   rQ   r6   r8   rN   s   &&&&   r$   Údepth_to_3d_v2rY   Ÿ   sv   € ôx �ušoÔ.Ü�}¢oÔ6ð —K‘K  Ð$�M€Fð Òñ 	äØ˜=¸E¿L¹LÔJ[Ð\aÓJqó
ð ô �zÒ#7Ô8à˜iÕ(Õ(Ð(r&   c               ó(   € V ^8„  d   QhRRRRRRRR/# ©r   r   r   r    r9   r:   r!   r"   )r#   s   "r$   r%   r%   í   s0   € ÷ A)ñ A)�|ð A)°Lð A)ÐTXð A)Ðeqñ A)r&   c           
     óH  € \        V 4       \        V4       \        V . RO4       \        V. RO4       V P                  w   r4p\        VVRV P                  \        W4      R7      pV P                  ^ ^^^4      pVR,          p\        WgW‚R7      p	V	P                  ^ ^^^4      # )a		  Unproject a depth map to a 3D point per pixel, channels first.

``points = depth_to_3d(depth, camera_matrix)`` takes a ``(B, 1, H, W)`` map of camera-frame depths and
``(B, 3, 3)`` pixel intrinsics, and returns the camera-frame point of every pixel as a ``(B, 3, H, W)``
tensor. For a channels-last ``(*, H, W, 3)`` result, or to reuse the pixel rays across frames, use
:func:`depth_to_3d_v2` on ``depth[:, 0]``. To unproject selected pixels rather than a whole map, use
:func:`~kornia.geometry.camera.perspective.unproject_points`.

Convention:
    - pixel ``(u, v)``, with ``u`` the column and ``v`` the row, maps to ``((u - cx) z / fx, (v - cy) z / fy, z)``.
      Pixel centres are integers, as in :class:`~kornia.geometry.camera.pinhole.PinholeCamera`.
      ``depth_to_3d(depth, K).permute(0, 2, 3, 1)`` equals ``depth_to_3d_v2(depth[:, 0], K)``.
    - ``normalize_points=True`` reads ``depth`` as the Euclidean ray length instead of ``z``.
    - an integer ``depth`` is not rejected; the result takes the floating dtype of ``camera_matrix``.
      The pixel grid is built in ``promote_types(depth.dtype, camera_matrix.dtype)`` when either input is
      floating (the default floating dtype when both are integer), so half-precision ``depth`` does not
      round the pixel indices below the intrinsics' precision.

Args:
    depth: image tensor containing a depth value per pixel with shape ``(B, 1, H, W)``.
    camera_matrix: tensor containing the camera intrinsics with shape ``(B, 3, 3)``.
    normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth is
      represented as the Euclidean ray length from the camera position.

Return:
    tensor with a 3d point per pixel of the same resolution as the input ``(B, 3, H, W)``.

See Also:
    - :func:`depth_to_3d_v2`: the same points channels last, with an optional cached grid.
    - :func:`depth_to_normals`: per-pixel surface normals from the same inputs.
    - :func:`depth_from_disparity`: depth from a stereo disparity map, to feed this function.
    - :func:`~kornia.geometry.camera.perspective.unproject_points`: the camera-frame point of selected pixels.

Example:
    >>> depth = torch.rand(1, 1, 4, 4)
    >>> K = torch.eye(3)[None]
    >>> depth_to_3d(depth, K).shape
    torch.Size([1, 3, 4, 4])

Fr<   )rJ   ©rC   Ú1rU   rV   rB   rE   )r   r   rX   r   r(   r4   Úpermuter   )
r   r    r9   Ú_r6   r8   Ú	points_2dÚpoints_depthrL   Ú	points_3ds
   &&&       r$   Údepth_to_3drd   í   sª   € ôR ˜5Ô!Ü˜=Ô)Ü�uÒ2Ô3Ü�}¢oÔ6ð  Ÿ+™+Ñ€A€q�%Ü-ØØØ$Ø�|‰|Ü Ó5ô€Ið "'§¡¨q°!°Q¸Ó!:€Lð '4°MÕ&BÐÜ.ØÐ!2ô€Ið ×Ñ˜Q  1 aÓ(Ð(r&   c               ó(   € V ^8„  d   QhRRRRRRRR/# r[   r"   )r#   s   "r$   r%   r%   1  s6   € ÷ KKñ KK˜Lð KK¸ð KKÐY]ð KKÐjvñ KKr&   c                óÚ  € \        V 4       \        V4       \        V . RO4       \        V. R	O4       V P                  R
R w  r4V^8  g   V^8  d+   \        RV RV R2\	        V P                  4      . ROR7      h\
        P                  ! V P                  VP                  4      pV P                  \
        P                  \
        P                  39   d   V P                  4       p VP                  \
        P                  \
        P                  39   d   VP                  4       p\        V P                  ^4      W4      P                  ^ ^^^4      pVP                  '       d   TMVP                  p\        VP                   P"                  4      ;_uu_ 4        \%        V4      pRRR4       XR,          P                  ^ ^^^4      P'                  4       pVR,          P                  ^ ^^^4      P'                  4       p	\
        P(                  P+                  W‰RR7      p
\,        P.                  ! V
R^R7      P1                  V4      P                  ^ ^^^4      #   + '       g   i     L½; i)aK  Estimate a unit surface normal per pixel from a depth map.

``normals = depth_to_normals(depth, camera_matrix)`` takes the same ``(B, 1, H, W)`` depth map and
``(B, 3, 3)`` intrinsics as :func:`depth_to_3d`, differentiates the unprojected points with a Sobel
:func:`~kornia.filters.spatial_gradient`, and returns the normalized cross product of the two tangents as a
``(B, 3, H, W)`` tensor. Normals point away from the camera: a fronto-parallel plane gets ``(0, 0, 1)``.

Convention:
    - both spatial dimensions must be at least 2: a surface normal requires two tangent directions.
    - a depth growing along the columns tilts the normal towards ``-x``, one growing along the rows towards
      ``-y``.
    - float16 and bfloat16 inputs are widened to float32 first. The result has the promoted dtype of ``depth`` and
      ``camera_matrix``, or the default floating dtype when both are integer, and it does not follow an
      enclosing autocast region: its dtype and values are the same inside autocast as outside.

Args:
    depth: image tensor containing a depth value per pixel with shape ``(B, 1, H, W)``, with
        :math:`H \geq 2` and :math:`W \geq 2`.
    camera_matrix: tensor containing the camera intrinsics with shape ``(B, 3, 3)``.
    normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth is
        represented as the Euclidean ray length from the camera position.

Return:
    tensor with a normal surface vector per pixel of the same resolution as the input ``(B, 3, H, W)``.

Raises:
    ShapeError: if either spatial dimension of ``depth`` is smaller than 2.

See Also:
    - :func:`depth_to_3d`: the point map whose tangents give the normals.

Example:
    >>> depth = torch.rand(1, 1, 4, 4)
    >>> K = torch.eye(3)[None]
    >>> depth_to_normals(depth, K).shape
    torch.Size([1, 3, 4, 4])

Nz3depth_to_normals requires H >= 2 and W >= 2. Got H=z, W=Ú.)Úactual_shapeÚexpected_shape©r@   r?   r]   rB   rW   )rC   r^   zH >= 2zW >= 2)rF   rF   é    )rF   rF   é   rG   )r   r   rX   r	   Úlistr-   r/   r*   Úfloat16Úbfloat16ÚfloatrY   rH   r_   r+   r
   r(   Útyper   Ú
contiguousÚlinalgÚcrossrI   rJ   Úto)r   r    r9   r6   r8   r*   ÚxyzÚ	gradientsÚaÚbÚnormalss   &&&        r$   Údepth_to_normalsr{   1  sæ  € ôN ˜5Ô!Ü˜=Ô)Ü�uÒ2Ô3Ü�}¢oÔ6à—K‘K  Ð$�M€FØ�„z�U˜Q”YÜØAÀ&ÀÈÈeÈWÐTUÐVÜ˜eŸk™kÓ*Ú9ô
ð 	
ô ×Ò §¡¨]×-@Ñ-@ÓA€Eð ‡{�{”u—}‘}¤e§n¡nÐ5Ô5Ø—‘“ˆØ×ÑœuŸ}™}¬e¯n©nÐ=Ô=Ø%×+Ñ+Ó-ˆô ' u§}¡}°QÓ'7¸ÓY×aÑaÐbcÐefÐhiÐklÓm€CØ×,×,Ð,‰E°#·)±)€Eô 
˜CŸJ™JŸO™O×	,Õ	,Ü"2°3Ó"7ˆ	÷ 
-ð   Õ(×0Ñ0°°A°q¸!Ó<×GÑGÓI€AØ Õ(×0Ñ0°°A°q¸!Ó<×GÑGÓI€Aä!ŸL™L×.Ñ.¨q¸Ð.Ó<€GÜ�;Š;�w B¨!Ô,×/Ñ/°Ó6×>Ñ>¸qÀ!ÀQÈÓJÐJ÷ 
-×	,ús   ÆIÉI*	c               ó0   € V ^8„  d   QhRRRRRRRRRRRR/# )	r   Úplane_normalsr   Úplane_offsetsrK   r    Úepsrp   r!   r"   )r#   s   "r$   r%   r%     sL   € ÷ M!ñ M!ØðM!àðM!ð ðM!ð  ð	M!ð
 
ðM!ð ñM!r&   c                óZ  € \        V RR.4       \        VRR.4       \        V. RO4       \        V. RO4       \        W#4      p\        V4      pV P                  ^4      p\        P
                  ! Wg,          R	R7      pVP                  \        P                  8X  d   VR8”  d   \        VR4      p\        P                  ! V4      p	W”8  p
\        P                  ! V^ 8  \        P                  ! W„) 4      \        P                  ! W„4      4      p\        P                  ! W«V4      pW,          # )
aÈ  Compute the depth of given pixels that lie on a known plane.

``depth = depth_from_plane_equation(plane_normals, plane_offsets, points_uv, camera_matrix)`` intersects the
camera ray of each pixel in ``points_uv`` of shape ``(B, N, 2)`` with the camera-frame plane
:math:`n \cdot X = d` and returns the ``z`` of each intersection as a ``(B, N)`` tensor: a list of depths,
not a depth map.

Convention:
    - the plane is in Hessian form, with the normal ``plane_normals`` of shape ``(B, 3)`` and
      the offset ``plane_offsets`` of shape ``(B, 1)``: ``n = (0, 0, 1)`` with ``d = 2`` is the plane ``z = 2``,
      and every pixel on it has depth 2.
    - ``points_uv`` are **pixels** (integer centres; see
      :class:`~kornia.geometry.camera.pinhole.PinholeCamera`), normalized here with ``camera_matrix``.
    - a ray-plane dot product inside :math:`(-eps, eps)` is replaced by :math:`\pm` ``eps`` with its sign
      (``+eps`` for an exact zero), so a grazing ray returns the large signed depth ``d / eps`` rather than
      ``inf``. In float16 a positive ``eps`` is floored at the smallest subnormal, ``2**-24``, because the
      default ``1e-8`` rounds to zero there; ``d / 2**-24`` is still ``inf`` in float16 once ``|d|`` exceeds
      about ``3.9e-3``, so pass a larger ``eps`` for a finite depth (``eps=1e-4`` keeps ``|d| <= 6.5`` finite).

Args:
    plane_normals (torch.Tensor): Plane normal vectors of shape ``(B, 3)``.
    plane_offsets (torch.Tensor): Plane offsets of shape ``(B, 1)``.
    points_uv (torch.Tensor): Pixel coordinates of shape ``(B, N, 2)``.
    camera_matrix (torch.Tensor): Camera intrinsic matrix of shape ``(B, 3, 3)``.
    eps: epsilon for numerical stability.

Returns:
    torch.Tensor: Computed depth values at the given pixels, shape ``(B, N)``.

See Also:
    - :func:`~kornia.geometry.camera.perspective.unproject_points`: turn these pixels and depths into 3D points.
    - :func:`depth_from_disparity`: a whole depth map from a rectified stereo disparity map, not from a plane.
    - :class:`~kornia.geometry.plane.Hyperplane`: a plane object, :math:`n \cdot x + d = 0`, with the offset of
      the opposite sign: for a batch of planes, pass ``plane.normal.data`` and ``-plane.offset.data[:, None]``.

rC   rD   r^   rj   ç        g      p>)rC   ÚNÚ2rB   rG   )r   r   r   Ú	unsqueezer-   Úsumr*   rn   ÚmaxÚabsÚwhereÚ	full_like)r}   r~   rK   r    r   rM   ÚraysÚplane_normals_expÚdenomÚ	denom_absÚ	zero_maskÚ
signed_epss   &&&&&       r$   Údepth_from_plane_equationr�     sõ   € ôV �} s¨C jÔ1Ü�} s¨C jÔ1Ü�y¢/Ô2Ü�}¢oÔ6ô 1°ÓJ€IÜ(¨Ó3€Dð &×/Ñ/°Ó2Ðô �IŠI�dÕ.°BÔ7€Eð ‡{�{”e—m‘mÔ#¨¨c¬	Ü�#Ð,Ó-ˆÜ—	’	˜%Ó €IØ‘€Iô —’˜U Q™Y¬¯ª¸¸tÓ(DÄeÇoÂoÐV[ÓFaÓb€JÜ�KŠK˜	¨uÓ5€Eð Õ Ð r&   c               ó0   € V ^8„  d   QhRRRRRRRRRRRR/# )	r   Ú	image_srcr   Ú	depth_dstÚsrc_trans_dstr    r9   r:   r!   r"   )r#   s   "r$   r%   r%   Ï  sT   € ÷ ]_ñ ]_Øð]_àð]_ð  ð]_ð  ð	]_ð
 ð]_ð ñ]_r&   c                ó8  a € \        S . RO4       \        V. RO4       \        V. RO4       \        V. RO4       \        S P                  RR VP                  RR 8H  R\        S P                  RR 4       R\        VP                  RR 4       R24       S P                  ^ ,          ^ 8X  dÒ   VP                  ^ ,          ^ 8X  dº   VP                  ^ ,          ^ 8X  d¢   VP                  ^ ,          ^ 8X  dŠ   ^ S P                  ^,          VP                  R,          VP                  R,          3pS P	                  R4      R,          P                  4       R,          pVP	                  ^^^^4      P                  V4      # S P                  p\        S 4      '       d…   \        ;QJ d!    V 3R lWV3 4       F  '       d   K   RM	  R	M! V 3R lWV3 4       4      '       dA   S P                  4       VP                  4       uo pVP                  4       VP                  4       r2\        VP                  ^4      W44      p\        VR,          V4      p	VR,          p
\        Wš4      pVP                  RR w  rÍ\        W¼V4      p\!        S VR	R
7      P#                  V4      # )aÿ
  Render an image from another viewpoint, given that viewpoint's depth map and the relative pose.

``image_dst = warp_frame_depth(image_src, depth_dst, src_trans_dst, camera_matrix)`` shows the source image
``(B, D, H, W)`` as the destination camera sees it. Each destination pixel is unprojected with its depth
from ``depth_dst`` of shape ``(B, 1, H, W)``, moved into the source frame by the ``(B, 4, 4)`` pose
``src_trans_dst``, projected with the ``(B, 3, 3)`` intrinsics ``camera_matrix`` that both views share, and
sampled bilinearly. :class:`DepthWarper` and :func:`depth_warp` do the same warp from two
:class:`~kornia.geometry.camera.pinhole.PinholeCamera` models; for a planar scene,
:func:`~kornia.geometry.transform.warp_perspective` with a homography needs no depth.

Convention:
    - ``src_trans_dst`` maps destination-frame points into the source frame. With ``fx = fy = 1`` and a
      unit depth, a translation of 1 along ``x`` samples ``image_src`` one pixel to the right of each
      destination pixel.
    - the unprojection is :func:`depth_to_3d_v2` and the reprojection
      :func:`~kornia.geometry.camera.perspective.project_points`, whose conventions apply.
    - the sampling is bilinear ``grid_sample`` with a fixed ``align_corners=True`` and
      ``padding_mode="zeros"``; the output keeps ``image_src``'s channels.
    - :class:`DepthWarper` and :func:`depth_warp` name the frames the other way round: this function's
      ``dst`` (the frame holding the depth) is their ``src``, and ``image_src`` is their ``patch_dst``.
    - a caller's autocast region does not change the sampling grid, outside TorchScript.

Args:
    image_src: image tensor in the source frame with shape ``(B, D, H, W)``.
    depth_dst: depth tensor in the destination frame with shape ``(B, 1, H, W)``.
    src_trans_dst: transformation matrix from destination to source with shape ``(B, 4, 4)``.
    camera_matrix: tensor containing the camera intrinsics with shape ``(B, 3, 3)``.
    normalize_points: whether to normalize the pointcloud. This must be set to ``True`` when the depth
       is represented as the Euclidean ray length from the camera position.

Return:
    ``image_src`` resampled onto the destination pixel grid, with shape ``(B, D, H, W)``.

A ``float16``/``bfloat16`` call with every input in that dtype runs in ``float32`` on every device and returns the
input dtype: half-precision pixel coordinates and a half-precision grid are off by a pixel or more at 256 px.

See Also:
    - :class:`DepthWarper`, :func:`depth_warp`: the same warp from two pinhole camera models.
    - :func:`depth_to_3d_v2`, :func:`~kornia.geometry.camera.perspective.project_points`: the two halves of the
      warp.
    - :func:`~kornia.geometry.transform.warp_perspective`: warp by a homography, without depth.

NzAimage_src and depth_dst must have the same height and width. Got z and rg   :Nrl   Nr�   c              3  óT   <"  € T F  qP                   SP                   8H  x € K  	  R # 5i©N©r*   )Ú.0Útr’   s   & €r$   Ú	<genexpr>Ú#warp_frame_depth.<locals>.<genexpr>  s    øé € ð 3Ù,U q�‰�9—?‘?Ö"Ó,Uùó   ƒ%(FT©Úalign_corners)rC   ÚDrU   rV   r]   )rC   Ú4r¡   rB   rW   rG   )rF   NrE   )r   r   rX   rm   Úreshaper…   Úexpandr*   r   Úallrp   rY   rH   r   r   r   r   ru   )r’   r“   r”   r    r9   Úoutput_shapeÚoutput_zeroÚ	out_dtypeÚpoints_3d_dstÚpoints_3d_srcrL   Úpoints_2d_srcr6   r8   Úpoints_2d_src_norms   f&&&&          r$   Úwarp_frame_depthr¬   Ï  sM  ø€ ôd �yÒ"6Ô7Ü�yÒ"6Ô7Ü�}¢oÔ6Ü�}¢oÔ6ÜØ�‰˜˜Ð 	§¡°°Ð 4Ñ4ðÜ�I—O‘O B CÐ(Ó)Ð*¨%´°Y·_±_ÀRÀSÐ5IÓ0JÐ/KÈ1ð	Nôð 	�‰˜Õ˜aÔØ�O‰O˜AÕ !Ô#Ø×Ñ Õ" aÔ'Ø×Ñ Õ" aÔ'à˜9Ÿ?™?¨1Õ-¨y¯©¸rÕ/BÀIÇOÁOÐTVÕDWÐXˆØ×'Ñ'¨Ó+¨BÕ/×3Ñ3Ó5¸Õ;ˆØ×"Ñ" 1 a¨¨AÓ.×5Ñ5°lÓCÐCð —‘€IÜ 	×*Ò*¯s«sô 3Ø-6À}Ñ,Uó3¯s¯sªsô 3Ø-6À}Ñ,Uó3÷ 0ò 0ð  )Ÿ™Ó0°)·/±/Ó2CÐˆ	�9Ø'4×':Ñ':Ó'<¸m×>QÑ>QÓ>S�}ô #1°×1BÑ1BÀ1Ó1EÀ}Ó"g€Mô % ]°7Õ%;¸]ÓK€Mð '4°MÕ&BÐÜ"0°Ó"R€Mð —O‘O B CÐ(�M€FÜ'BÀ=ÐZ_Ó'`Ðä˜iÐ);È4ÔP×SÑSÐT]Ó^Ð^r&   c                  ó    a € ] tR tRtRtRR V 3R lllt]R R l4       tR R ltR	 R
 lt	R R lt
R R ltR R ltR R ltR R ltRtV ;t# )r   i/  a   Render an image from another viewpoint by depth, given two PinholeCamera models (module form).

Build ``warper = DepthWarper(pinhole_dst, H, W)``, call ``warper.compute_projection_matrix(pinhole_src)``,
then ``image = warper(depth_src, patch_dst)`` samples the destination image ``patch_dst`` of shape
``(B, C, H, W)`` at the pixels where the source-frame depth ``depth_src`` projects, so the ``(B, C, H, W)``
result shows ``patch_dst`` as the source camera sees it. Calling the warper before
:meth:`compute_projection_matrix` raises ``ValueError``.
:func:`depth_warp` is the one-call form; :func:`warp_frame_depth` takes a 4x4 pose and one intrinsics
matrix instead of two cameras.

.. math::
    P_{src}^{\{dst\}} = K_{dst} * T_{src}^{\{dst\}}

    I_{src} = \omega(I_{dst}, P_{src}^{\{dst\}}, D_{src})

Convention:
    - the depth lives in the **source** frame and the image in the **destination** frame.
    - until :meth:`compute_projection_matrix` has been called, :meth:`warp_grid` raises ``ValueError`` and
      :meth:`compute_subpixel_step` raises ``RuntimeError``.
    - Half-precision cameras of one dtype compose in ``float32``, and a ``float16``/``bfloat16`` patch with depth of
      its dtype builds its grid and samples in ``float32`` on every device and returns the input dtype.
      :meth:`warp_grid` casts the grid back to the depth's dtype.
    - :meth:`compute_projection_matrix` stores ``K_dst @ (E_dst @ inverse(E_src))``, the matrix above, from
      the constructor's ``pinhole_dst`` and that method's ``pinhole_src`` (both
      :class:`~kornia.geometry.camera.pinhole.PinholeCamera`, world-to-camera extrinsics).
      Both cameras use only the top-left 3x3 block of their intrinsics, embedded in
      homogeneous 4x4 form; the stored fourth row and column are ignored.
    - :meth:`compute_subpixel_step` is half a pixel divided by how far the image centre,
      ``((W - 1) / 2, (H - 1) / 2)``, moves in the destination image per unit of inverse depth around
      inverse depth 1, the smallest over the batch. Two cameras with no baseline have no parallax, so it
      is ``inf``, or a very large value when rounding leaves a residual translation; such a camera passes no
      gradient (zero, by convention, since none exists at ``inf``), so it cannot turn the gradients of the
      rest of the batch into NaN. With focal length ``f`` and baseline ``b``, a rectified pair gives
      ``0.5 / (f * b)``. It measures the image centre only: exact for a rectified pair, where every pixel
      moves the same, and not a bound for the whole image otherwise. Under forward motion the centre barely
      moves, so the step can be far coarser than the corners need.
    - the ``grid`` attribute is the homogeneous integer-centre pixel grid (pixel ``(0, 0)`` is
      ``(0, 0, 1)``); :meth:`warp_grid` returns sampling positions in ``grid_sample``'s
      ``align_corners=True`` normalized coordinates of the **destination** image.
    - ``align_corners`` (default ``True``), ``mode`` and ``padding_mode`` are forwarded to ``grid_sample``,
      but :meth:`warp_grid` normalizes for ``align_corners=True`` whatever the flag, so ``False`` misaligns
      the warp and an identity warp no longer returns its input
      (`#6317 <https://github.com/kornia/kornia/issues/6317>`_).
    - :func:`warp_frame_depth` names the frames the other way round: its ``dst`` (the frame holding the depth)
      is this class's ``src``, and its ``image_src`` is ``patch_dst``.
    - a caller's autocast region does not change :meth:`compute_projection_matrix` for cameras of one dtype. Cameras
      that mix float32 and half dtypes raise outside a region and compose in ``float32`` inside one.

.. warning::
    The two warps agree to rounding for float32/float64 away from ``z = 0``, and split at ``z = 0``, where
    ``cam2pixel`` sends every pixel with a nonzero projected numerator far outside the image:
    `#4267 <https://github.com/kornia/kornia/issues/4267>`_.

Args:
    pinhole_dst: the pinhole model for the destination frame.
    height: the height of the image to warp.
    width: the width of the image to warp.
    mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
    padding_mode: padding mode for outside grid values ``'zeros'`` | ``'border'`` | ``'reflection'``.
    align_corners: interpolation flag.

See Also:
    - :func:`depth_warp`: this warp in one call.
    - :func:`warp_frame_depth`: the same warp from a 4x4 pose and one camera matrix.

c               ó4   € V ^8„  d   QhRRRRRRRRRRR	R
RR/# )r   Úpinhole_dstr   r6   r7   r8   ÚmodeÚstrÚpadding_moderŸ   r:   r!   ÚNoner"   )r#   s   "r$   r%   ÚDepthWarper.__annotate__t  sY   € ÷ Gñ Gà"ðGð ðGð ð	Gð
 ðGð ðGð ðGð 
ñGr&   c                	ó  <€ \         SV `  4        W0n        W n        W@n        WPn        R V n        W`n        \        V\        4      '       g   \        R\        V4       24      hWn        RV n        RV n        V P                  W#4      V n        R# )g�íµ ÷Æ°>z+Expected pinhole_dst as PinholeCamera, got N)ÚsuperÚ__init__r8   r6   r°   r²   r   rŸ   Ú
isinstancer   Ú	TypeErrorrq   Ú_pinhole_dstÚ_pinhole_srcÚ_dst_proj_srcÚ_create_meshgridÚgrid)Úselfr¯   r6   r8   r°   r²   rŸ   Ú	__class__s   &&&&&&&€r$   r·   ÚDepthWarper.__init__t  s„   ø€ ô 	‰ÑÔØŒ
Ø!ŒØŒ	Ø!-ÔØˆŒØ#0Ôô ˜+¤}×5Ò5ÜÐIÌ$È{ÓJ[ÐI\Ð]Ó^Ð^Ø+6ÔØ26ˆÔØ26ˆÔð #'×"7Ñ"7¸Ó"FˆŽ	r&   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r6   r7   r8   r!   r   r"   )r#   s   "r$   r%   r´   ‘  s!   € ÷ 3ñ 3 ð 3¨Sð 3°\ñ 3r&   c                	ó2   € \        WR R7      p\        V4      # )F)r=   )r   r   )r6   r8   r¾   s   && r$   r½   ÚDepthWarper._create_meshgrid�  s   € ä,¨VÐSXÔYˆÜ,¨TÓ2Ð2r&   c               ó    € V ^8„  d   QhRRRR/# )r   Úpinhole_srcr   r!   r   r"   )r#   s   "r$   r%   r´   •  s   € ÷ ;ñ ;°]ð ;À{ñ ;r&   c                óš  a€ \        V P                  4      \        Jd"   \        R\        V P                  4       24      h\        V4      \        Jd   \        R\        V4       24      h\	        VP
                  V P                  P
                  V P                  P                  4      w  or#\        S4      '       dt   \        ;QJ d     V3R lW#3 4       F  '       d   K   RM	  RM! V3R lW#3 4       4      '       d2   SP                  4       VP                  4       uopVP                  4       pSP                  RR pSP                  pSP                  p\        VP                   4      ;_uu_ 4        \        P                  ! ^WVR7      P                   ! . VO^N^N5!  P#                  4       p\        P                  ! ^WVR7      P                   ! . VO^N^N5!  P#                  4       pSR	,          p	SR
,          p
\        P$                  ! V	RR4      p\        P&                  ! V) V
4      pW·R	&   WÇR
&   VR	,          pVR
,          p\        P&                  ! WÛ4      p\        P&                  ! WÜ4      V,           pWøR	&   VVR
&   \        P&                  ! \)        V4      V4      pRRR4       Wn        XV n        V #   + '       g   i     L; i)zwCompute the projection matrix from the source to destination frame.

See the Convention block on :class:`DepthWarper`.
zDMember self._pinhole_dst expected to be of class PinholeCamera. Got z@Argument pinhole_src expected to be of class PinholeCamera. Got c              3  óT   <"  € T F  qP                   SP                   8H  x € K  	  R # 5ir—   r˜   )r™   rš   Úsrc_extrinsicss   & €r$   r›   Ú8DepthWarper.compute_projection_matrix.<locals>.<genexpr>«  s"   øé € ð <
Ù5U°�G‰G�~×+Ñ+Ö+Ó5Uùr�   FTNr)   rW   ).ºNé   NrË   ).rË   :rÌ   NNrG   )rq   rº   r   r¹   r   Ú
extrinsicsr    r   r¤   rp   rX   r(   r*   r
   r-   Úeyer£   rr   Ú	transposeÚmatmulr   r»   r¼   )r¿   rÆ   Údst_extrinsicsÚdst_intrinsicsÚbatch_shaper(   r*   Úinv_extrÚdst_trans_srcÚsrc_rmatÚsrc_tvecÚinv_rmatÚinv_tvecÚdst_rmatÚdst_tvecÚcomposed_rmatÚcomposed_tvecÚdst_proj_srcrÉ   s   &&                @r$   Úcompute_projection_matrixÚ%DepthWarper.compute_projection_matrix•  sx  ø€ ô �×!Ñ!Ó"¬-Ó7ÜØVÔW[Ð\`×\mÑ\mÓWnÐVoÐpóð ô �Ó¤MÓ1ÜÐ^Ô_cÐdoÓ_pÐ^qÐrÓsÐsô :SØ×"Ñ" D×$5Ñ$5×$@Ñ$@À$×BSÑBS×BaÑBaó:
Ñ6ˆ˜ô
 $ N×3Ò3¿»ô <
Ø6DÑ5Uó<
¿¿ºô <
Ø6DÑ5Uó<
÷ 9
ò 9
ð .<×-AÑ-AÓ-CÀ^×EYÑEYÓE[Ð*ˆN˜NØ+×1Ñ1Ó3ˆNØ$×*Ñ*¨3¨BÐ/ˆØ×&Ñ&ˆØ×$Ñ$ˆä §¡×,Õ,ä—y’y ¨6Ô?×FÒFÐZÈÐZÐUVÐZÐXYÓZ×eÑeÓgˆHÜ!ŸIšI a°ÔD×KÒKÐ_È[Ð_ÐZ[Ð_Ð]^Ó_×jÑjÓlˆMð & kÕ2ˆHØ% kÕ2ˆHÜ—’ x°°RÓ8ˆHÜ—|’| X I¨xÓ8ˆHð %-�[Ñ!Ø$,�[Ñ!ð & kÕ2ˆHØ% kÕ2ˆHÜ!ŸLšL¨Ó<ˆMÜ!ŸLšL¨Ó<¸xÕGˆMà)6˜+Ñ&Ø)6ˆM˜+Ñ&ä Ÿ<š<Ô(9¸.Ó(IÈ=ÓYˆL÷1 -ð4 (ÔØ)ˆÔØˆ÷9 -×,ús   Å4D/J:Ê:K
	c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   Úxrp   ÚyÚinvdsztuple[float, ...]r!   r   r"   )r#   s   "r$   r%   r´   Ò  s/   € ÷ Qñ Q ð Q¨%ð QÐ8Ið QÈlñ Qr&   c           	     ón  € V P                   e   V P                  f   \        R4      hV P                   P                  V P                   P                  rT\
        P                  ! V.V.R.R..WER7      pV P                  P                  P                  WER7      p\
        P                  ! \        \        V4      4      V4      p\
        P                  ! W4VR7      P                  VP                  ^ ,          ^R4      p	\
        P                  ! VR,          P                  RR\        V4      4      V	.^R7      # )zdReturn the homogeneous source-frame points of pixel ``(x, y)`` at each inverse depth, ``(B, 4, N)``.ú'Please, call compute_projection_matrix.ç      ð?r�   r)   rj   rG   )rF   rË   )r¼   r»   Ú
ValueErrorr(   r*   r-   Útensorr    ru   rÐ   r   r   r£   rX   ÚcatÚlen)
r¿   râ   rã   rä   r(   r*   ÚpixelÚk_srcrŠ   Ústepss
   &&&&      r$   Ú_source_pointsÚDepthWarper._source_pointsÒ  s÷   € à×ÑÒ%¨×):Ñ):Ò)BÜÐFÓGÐGà×*Ñ*×1Ñ1°4×3EÑ3E×3KÑ3K�Ü—’˜q˜c A 3¨¨°¨uÐ5¸fÔRˆà×!Ñ!×/Ñ/×2Ñ2¸&Ð2ÓNˆÜ�|Š|Ô-Ô.AÀ%Ó.HÓIÈ5ÓQˆÜ—’˜U¸Ô?×FÑFÀtÇzÁzÐRSÅ}ÐVWÐY[Ó\ˆÜ�yŠy˜$˜u�+×,Ñ,¨R°´S¸³ZÓ@À%ÐHÈaÔPÐPr&   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   râ   rp   rã   Úinvdr!   r   r"   )r#   s   "r$   r%   r´   ß  s(   € ÷ &ñ & Uð &¨uð &¸Eð &Àlñ &r&   c                	óú   € V P                  WV34      p\        P                  ! V P                  V4      pR VR,          ,          pVR,          V,          pVR,          V,          p\        P                  ! Wx.^4      # )rç   ©rF   r   ©rF   rk   ©rF   rl   )rï   r-   rÐ   r¼   rê   )	r¿   râ   rã   rò   ÚpointÚflowÚzÚ_xÚ_ys	   &&&&     r$   Ú_compute_projectionÚDepthWarper._compute_projectionß  sg   € Ø×#Ñ# A¨4¨'Ó2ˆÜ�|Š|˜D×.Ñ.°Ó6ˆØ�$�t•*ÕˆØ�$�Z˜!�^ˆØ�$�Z˜!�^ˆÜ�yŠy˜"˜ 1Ó%Ð%r&   c               ó   € V ^8„  d   QhRR/# )r   r!   r   r"   )r#   s   "r$   r%   r´   ç  s   € ÷ !Fñ !F |ñ !Fr&   c           
     ó˜  € V P                   f   \        R4      hRpV P                  ^,
          ^,          pV P                  ^,
          ^,          pRV,
          RV,           3pV P	                  W#V4      pV P                   p\
        P                  ! We4      pRVR	,          ,          pVR
,          V,          p	VR,          V,          p
\
        P                  ! Wš3RR7      p\
        P                  ! VR,          VR
,          ,
          ^^R7      R,          pWÁ,          pV^ 8„  p\
        P                  ! Wí\
        P                  ! V4      4      p\
        P                  ! VRV,          \
        P                  ! V\        R4      4      4      p\
        P                  ! V4      P                  V P                  P                   P"                  4      # )a7  Compute the smallest inverse depth step over the batch for sub-pixel sampling of the depth cost volume.

See the Convention block on :class:`DepthWarper`.

Szeliski, Richard, and Daniel Scharstein. "Symmetric sub-pixel stereo matching." European Conference on Computer
Vision. Springer Berlin Heidelberg, 2002.
zCExpected torch.Tensor, but got None Type from the projection matrixg{®Gáz„?rç   rj   )rA   r@   r>   g      à?Úinfrô   rõ   rö   rG   )r¼   ÚRuntimeErrorr8   r6   rï   r-   rÐ   ÚstackÚnormrˆ   Ú	ones_liker‰   rp   Úminru   rº   rÍ   r*   )r¿   Údelta_dÚcenter_xÚcenter_yrä   ÚpointsÚprojrø   ÚzsÚxsÚysÚxysÚdxyÚdxddÚmovedÚsafeÚsteps   &                r$   Úcompute_subpixel_stepÚ!DepthWarper.compute_subpixel_stepç  s]  € ð ×ÑÒ%ÜÐdÓeÐeàˆØ—J‘J •N aÕ'ˆØ—K‘K !•O qÕ(ˆð �w•  g¥Ð.ˆà×$Ñ$ X¸Ó?ˆà×!Ñ!ˆÜ�|Š|˜DÓ)ˆØ�4˜•:ÕˆØ�$�Z˜"�_ˆØ�$�Z˜"�_ˆÜ�kŠk˜2˜(¨Ô+ˆÜ�jŠj˜˜T� S¨¥YÕ.°!¸Ô;¸cÕAˆØ�}ˆð �q‘ˆÜ�{Š{˜5¬¯ª¸Ó(=Ó>ˆÜ�{Š{˜5 #¨¥*¬e¯oªo¸dÄEÈ%ÃLÓ.QÓRˆÜ�yŠy˜‹×!Ñ! $×"3Ñ"3×">Ñ">×"DÑ"DÓEÐEr&   c               ó    € V ^8„  d   QhRRRR/# ©r   Ú	depth_srcr   r!   r"   )r#   s   "r$   r%   r´   
  s   € ÷ 	>ñ 	> <ð 	>°Lñ 	>r&   c                óV   € V P                  V4      P                  VP                  4      # )a|  Compute where each source pixel samples the destination image, given the depth in the source frame.

See the Convention block on :class:`DepthWarper`.

The method ``compute_projection_matrix`` has to be called beforehand in order to have precomputed the
projection matrix, which moves a source-frame point into the destination camera frame and applies the
destination intrinsics.
)Ú
_warp_gridru   r*   )r¿   r  s   &&r$   Ú	warp_gridÚDepthWarper.warp_grid
  s"   € ð �‰˜yÓ)×,Ñ,¨Y¯_©_Ó=Ð=r&   c               ó    € V ^8„  d   QhRRRR/# r  r"   )r#   s   "r$   r%   r´     s   € ÷ $%ñ $% Lð $%°\ñ $%r&   c                ól  € V P                   e   V P                  f   \        R4      h\        VP                  4      ^8w  d   \        RVP                   24      hVP                  \        P                  ! VP                  \        P                  4      4      pVP                  w  p  pVP                  pVP                  pV P                  P                  WER7      P                  VRRR4      pV P                  P                  p\        V4      '       d$   \        \!        VP#                  4       4      4      pMV P                  P%                  4       p\'        WP                  WER7      V4      p	\)        W�P                   P                  WER7      4      p
\+        W P,                  V P.                  4      pV# )zdCompute :meth:`warp_grid`, in float32 for half-precision depth, whose pixel coordinates would round.ræ   z7Input depth_src has to be in the shape of Bx1xHxW. Got r)   rG   )r¼   r»   rè   rë   rX   ru   r-   r/   r*   Úfloat32r(   r¾   r£   r    r   r   r   rp   Úintrinsics_inverser   r   r   r6   r8   )r¿   r  Ú
batch_sizer`   r(   r*   Úpixel_coordsr    r   Úcam_coords_srcÚpixel_coords_srcÚpixel_coords_src_norms   &&          r$   r  ÚDepthWarper._warp_grid  s{  € ð ×ÑÒ%¨×):Ñ):Ò)BÜÐFÓGÐGäˆy�‰Ó 1Ô$ÜÐVÐW`×WfÑWfÐVgÐhÓiÐià—L‘L¤×!4Ò!4°Y·_±_ÄeÇmÁmÓ!TÓUˆ	ð (Ÿo™oÑˆ
�A�q˜!Ø(×/Ñ/ˆØ&Ÿ_™_ˆð &*§Y¡Y§\¡\¸ \Ó%M×%TÑ%TÐU_ÐacÐegÐikÓ%lˆð ×)Ñ)×7Ñ7ˆÜ# M×2Ò2Ü!2Ô3FÀ}×GZÑGZÓG\Ó3]Ó!^Ñà!%×!2Ñ!2×!EÑ!EÓ!GÐÜ'0Ø×,Ñ,°FÐ,ÓHÈ,ó(
ˆô
 *3Ø×.Ñ.×1Ñ1¸Ð1ÓMó*
Ðô
 /JÐJZ×\gÑ\gÐim×isÑisÓ.tÐØ$Ð$r&   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r  r   Ú	patch_dstr!   r"   )r#   s   "r$   r%   r´   ;  s!   € ÷ &ñ & ð &¸,ð &È<ñ &r&   c                óh  € \        V4      ;'       d    VP                  VP                  8H  p\        V'       d   VP                  4       MTV'       d   V P	                  V4      MV P                  V4      V P                  V P                  V P                  R7      P                  VP                  4      # )aê  Warp a tensor from the destination frame to the source frame, given the depth in the source frame.

See the Convention block on :class:`DepthWarper`.

Args:
    depth_src: the depth in the source frame. The tensor must have a shape ``(B, 1, H, W)``.
    patch_dst: the patch in the destination frame. The tensor must have a shape ``(B, C, H, W)``.

Return:
    the patch warped from the destination frame to the source frame.

Shape:
    - Input: ``(B, 1, H, W)`` and ``(B, C, H, W)``.
    - Output: ``(B, C, H, W)`` where C = number of channels.

Example:
    >>> # pinholes camera models
    >>> pinhole_dst = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
    ... torch.tensor([32]), torch.tensor([32]))
    >>> pinhole_src = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
    ... torch.tensor([32]), torch.tensor([32]))
    >>> # create the depth warper, compute the projection matrix
    >>> warper = DepthWarper(pinhole_dst, 32, 32)
    >>> _ = warper.compute_projection_matrix(pinhole_src)
    >>> # warp the destination frame to the source frame by depth
    >>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
    >>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
    >>> image_src = warper(depth_src, image_dst)  # NxCxHxW

)r°   r²   rŸ   )
r   r*   r   rp   r  r  r°   r²   rŸ   ru   )r¿   r  r(  Ú
in_float32s   &&& r$   ÚforwardÚDepthWarper.forward;  sƒ   € ô> .¨iÓ8×_Ð_¸Y¿_¹_ÐPY×P_ÑP_Ñ=_ˆ
Ü!ß!+ˆI�O‰OÔ°ß*4ˆD�O‰O˜IÔ&¸$¿.¹.ÈÓ:SØ—‘Ø×*Ñ*Ø×,Ñ,ô
÷ ‰"ˆY�_‰_Ó
ð	r&   )
r¼   rº   r»   rŸ   r   r¾   r6   r°   r²   r8   )ÚbilinearÚzerosT)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r·   Ústaticmethodr½   rß   rï   rü   r  r  r  r+  Ú__static_attributes__Ú__classcell__)rÀ   s   @r$   r   r   /  sW   ø† ñA÷HGò Gð8 ô3ó ð3õ;õzQõ&õ!FõF	>õ$%÷L&ó &r&   c               ó8   € V ^8„  d   QhRRRRRRRRRRR	RR
RRR/# )r   r¯   r   rÆ   r  r   r(  r6   r7   r8   rŸ   r:   r!   r"   )r#   s   "r$   r%   r%   d  sX   € ÷ 7(ñ 7(Øð7(àð7(ð ð7(ð ð	7(ð
 ð7(ð ð7(ð ð7(ð ñ7(r&   c                óN   € \        WWVR7      pVP                  V4       V! W#4      # )a”  Render an image from another viewpoint by depth, given two PinholeCamera models (one call).

``image = depth_warp(pinhole_dst, pinhole_src, depth_src, patch_dst, H, W)`` samples the destination image
``patch_dst`` of shape ``(B, C, H, W)`` at the pixels where the source-frame depth ``depth_src`` projects, so the
result shows ``patch_dst`` as the source camera sees it. It is :class:`DepthWarper` in one call, with that
class's default ``mode`` and ``padding_mode`` and the same output bit for bit; see its Convention block.
:func:`warp_frame_depth` takes a 4x4 pose and one intrinsics matrix instead of two cameras.

Args:
    pinhole_dst: the pinhole model for the destination frame, the one ``patch_dst`` is seen from.
    pinhole_src: the pinhole model for the source frame, the one ``depth_src`` belongs to.
    depth_src: the depth in the source frame, with shape ``(B, 1, H, W)``.
    patch_dst: the image in the destination frame, with shape ``(B, C, H, W)``.
    height: the height of the images, ``H``.
    width: the width of the images, ``W``.
    align_corners: forwarded to ``grid_sample`` only; the sampling grid is always normalized for
        ``align_corners=True``, so ``False`` misaligns the warp
        (`#6317 <https://github.com/kornia/kornia/issues/6317>`_). The default ``True`` is what
        :class:`DepthWarper` uses.

Return:
    ``patch_dst`` resampled onto the source pixel grid, with shape ``(B, C, H, W)``.

See Also:
    - :class:`DepthWarper`: the module form, with ``mode`` and ``padding_mode``.
    - :func:`warp_frame_depth`: the same warp from a 4x4 pose and one camera matrix.

Example:
    >>> # pinholes camera models
    >>> pinhole_dst = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
    ... torch.tensor([32]), torch.tensor([32]))
    >>> pinhole_src = PinholeCamera(torch.randn(1, 4, 4), torch.randn(1, 4, 4),
    ... torch.tensor([32]), torch.tensor([32]))
    >>> # warp the destination frame to the source frame by depth
    >>> depth_src = torch.ones(1, 1, 32, 32)  # Nx1xHxW
    >>> image_dst = torch.rand(1, 3, 32, 32)  # NxCxHxW
    >>> image_src = depth_warp(pinhole_dst, pinhole_src, depth_src, image_dst, 32, 32)  # NxCxHxW

rž   )r   rß   )r¯   rÆ   r  r(  r6   r8   rŸ   Úwarpers   &&&&&&& r$   Ú
depth_warpr:  d  s*   € ôf ˜¨eÔQ€Fà
×$Ñ$ [Ô1á�)Ó'Ð'r&   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Ú	parameterúfloat | torch.TensorÚ	disparityr   r!   r"   )r#   s   "r$   r%   r%   ž  s#   € ÷ ñ Ð,@ð È\ð Ð^rñ r&   c                ó¢  € \        V \        P                  4      '       d#   V P                  ^ 8X  g   V P                  R8X  d   V # V P                  ^8X  dm   VP                  ^8¼  d\   \        WP                  ^ ,          .4       V P                  ! VP                  ^ ,          .^.VP                  ^,
          ,          O5!  # \        V R.4       V # )aø  Shape a per-batch-element ``(B,)`` camera parameter so each value meets its own disparity sample.

A Python number, a scalar tensor and a ``(1,)`` tensor already broadcast over the whole disparity and are
returned untouched, so every input accepted before ``(B,)`` support existed takes the same path as it did.
A ``(B,)`` tensor pairs element ``b`` with ``disparity[b]``, which needs a batch axis: ``disparity`` must
have rank 3 or more and a leading size of ``B``. Anything else raises ``ShapeError``.
r^   )rl   )r¸   r-   ÚTensorÚndimrX   r   r¢   r   )r<  r>  s   &&r$   Ú_per_sample_camera_parameterrB  ž  sž   € ô �i¤§¡×.Ò.°)·.±.ÀAÔ2EÈÏÉÐ\`ÔI`ØÐØ‡~�~˜Ô˜yŸ~™~°Ô2ä˜9§¡°qÕ'9Ð&:Ô;Ø× Ò  §¡°Õ!3ÐS¸°s¸i¿n¹nÈqÕ>PÕ7QÓSÐSÜ�y 3 %Ô(ØÐr&   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r>  r   Úbaseliner=  Úfocalr!   r"   )r#   s   "r$   r%   r%   °  s2   € ÷ G1ñ G1ØðG1Ø';ðG1ØDXðG1àñG1r&   c                ó  € \        V R\        V 4       R24       \        V . RO4       \        \	        V\
        \        \        P                  34      ;'       d    \	        V\        4      '       * R\        V4       24       \        \	        V\
        \        \        P                  34      ;'       d    \	        V\        4      '       * R\        V4       24       \        W4      p\        W 4      pW,          V R,           ,          # )aê  Convert a rectified stereo disparity map to depth.

``depth = depth_from_disparity(disparity, baseline, focal)`` returns ``baseline * focal / disparity`` for every
pixel of a ``(*, H, W)`` disparity map, with ``baseline`` the distance between the two camera centres and
``focal`` the focal length in pixels, so the depth comes out in the unit of ``baseline``. A zero disparity
gives a large finite depth, not ``inf``, except in float16 (see below). To go from disparity straight to 3D
points with a calibrated stereo rig, use
:meth:`~kornia.geometry.camera.stereo.StereoCamera.reproject_disparity_to_3D`, which takes a ``(B, H, W, 1)``
disparity.

Convention:
    - the exact formula is ``baseline * focal / (disparity + 1e-8)``: a disparity of 2 with a baseline of
      0.5 and a focal length of 100 gives a depth of 25. The ``1e-8`` lowers the depth by a
      relative ``1e-8 / disparity``. Float32 rounds that away from a disparity of 0.25 up; below it the bias
      shows, ``1e-6`` relative (10 float32 ulps) at a disparity of 0.01.
    - a zero disparity (no stereo match, or a point at infinity) gives a finite far depth,
      ``baseline * focal / 1e-8`` (``5e9`` for baseline 0.5 and focal 100), not ``inf``. Such a point warps
      as a point at infinity, along the rotation-only homography, in
      :func:`warp_frame_depth`, whereas an ``inf`` depth would turn into ``NaN`` in
      :func:`depth_to_3d` wherever a ray component is zero. The far depth is a
      placeholder, not a measurement: mask ``disparity > 0`` to tell matched pixels apart.
    - in float16 the ``1e-8`` rounds to ``0`` and the far depth would overflow anyway, so a zero disparity
      gives ``inf``. A float32 far depth turns back into ``inf`` wherever the warp casts it to float16, as a
      float16 pose or float16 autocast does, so run the whole warp (depth, image, pose and camera matrix) in
      float32, outside float16 autocast, when zero disparities flow into it.
    - ``baseline`` and ``focal`` are each a scalar (Python number, 0-d or ``(1,)`` tensor) shared by the
      whole disparity, or a ``(B,)`` tensor paired with ``disparity[b]``, which then needs a leading
      batch axis of size ``B`` (otherwise ``ShapeError``). The two may mix.
    - ``disparity`` is ``(*, H, W)`` and the result has its shape. Its sign is not checked.

Args:
    disparity: Disparity tensor of shape ``(*, H, W)``.
    baseline: Distance between the two lenses, as an int, float, or tensor of shape ``()`` or ``(1,)``,
        or of shape ``(B,)`` for one value per batch element.
    focal: Focal length, as an int, float, or tensor of shape ``()`` or ``(1,)``, or of shape ``(B,)`` for
        one value per batch element.

Return:
    Depth map of the shape ``(*, H, W)``.

See Also:
    - :func:`depth_to_3d`, :func:`depth_to_3d_v2`: unproject the depth map to 3D points.
    - :meth:`~kornia.geometry.camera.stereo.StereoCamera.reproject_disparity_to_3D`: disparity to 3D points in one
      step.
    - :func:`depth_from_plane_equation`: the depth of given pixels on a known plane, ``(B, N)``, not a map.

Example:
    >>> disparity = torch.rand(4, 1, 4, 4)
    >>> baseline = torch.rand(1)
    >>> focal = torch.rand(1)
    >>> depth_from_disparity(disparity, baseline, focal).shape
    torch.Size([4, 1, 4, 4])

z0Input disparity type is not a torch.Tensor. Got rg   z<Input baseline should be an int, float or torch.Tensor. Got z9Input focal should be an int, float or torch.Tensor. Got ç:Œ0âŽyE>rS   )r   rq   r   r   r¸   r7   rp   r-   r@  r:   rB  )r>  rD  rE  s   &&&r$   Údepth_from_disparityrH  °  sÔ   € ôr ˜9Ð(XÔY]Ð^gÓYhÐXiÐijÐ&kÔlÜ�y¢/Ô2ÜÜ�8œc¤5¬%¯,©,Ð7Ó8×[Ð[ÄÈHÔVZÓA[Ô=[Ø
FÄtÈHÃ~ÐFVÐWôô Ü�5œ3¤¤u§|¡|Ð4Ó5×UÐU¼jÈÔPTÓ>UÔ:UØ
CÄDÈÃKÀ=ÐQôô
 ,¨HÓ@€HÜ(¨Ó:€EàÕ˜y¨4Õ/Õ0Ð0r&   )	r   rH  r�   rd   rY   r{   r:  rO   r¬   )FNN)FN)F)rG  )T)6r3  Ú
__future__r   Útypingr   r-   Útorch.nn.functionalr   Ú
functionalrI   Úkornia.core.checkr   r   r   r   Úkornia.core.exceptionsr	   Úkornia.core.utilsr
   r   r   r   r   Úkornia.filters.sobelr   Úkornia.geometry.gridr   Úcamerar   r   r   r   r   Úcamera.pinholer   Úconversionsr   r   rs   r   r   Ú__all__r4   rO   rY   rd   r{   r�   r¬   ÚModuler   r:  rB  rH  r"   r&   r$   Ú<module>rW     s£   ðñ$õ& #å ã ß Ð Ý ç jÓ jÝ -÷õ õ 2Ý 0ç YÕ YÝ -ß Vß Cò
€õ÷,;÷|K)÷\A)÷HKK÷\M!÷`]_ô@r�"—)‘)ô r÷j	7(õt÷$G1r&   