o
    i+                     @   s  d Z ddlmZ ddlZ	d)dejdejdedejfd	d
Zd*dejdedejfddZ		d+dejdejdejdejdejdededejfddZd,dejde	dejfddZ
	 d-dejd!ejd"edejfd#d$Z	%d.dejdejdejd&edeejejf f
d'd(ZdS )/z>
Alignment utilities for depth estimation and metric scaling.
    )TupleN-q=abepsreturnc                 C   s   | j |j krtd| j  d|j  | j|jkr$td| j d|j |  r,| s0tdt| d|d}t|d|d|}|| S )uw  
    Compute least squares scale factor s such that a ≈ s * b.

    Args:
        a: First tensor
        b: Second tensor
        eps: Small epsilon for numerical stability

    Returns:
        Scalar tensor containing the scale factor

    Raises:
        ValueError: If tensors have mismatched shapes or devices
        TypeError: If tensors are not floating point
    zShape mismatch: z vs zDevice mismatch: z#Tensors must be floating point type)	shape
ValueErrordeviceis_floating_point	TypeErrortorchdotreshape	clamp_min)r   r   r   numden r   >/data/cameron/da3_repo/src/depth_anything_3/utils/alignment.pyleast_squares_scale_scalar   s   r   333333?sky_prediction	thresholdc                 C   s   | |k S )z
    Compute non-sky mask from sky prediction.

    Args:
        sky_prediction: Sky prediction tensor
        threshold: Threshold for sky classification

    Returns:
        Boolean mask where True indicates non-sky regions
    r   )r   r   r   r   r   compute_sky_mask6   s   r   MbP?{Gz?
depth_confnon_sky_maskdepthmetric_depthmedian_confmin_depth_thresholdmin_metric_depth_thresholdc                 C   s   | |k|@ ||k@ ||k@ S )a  
    Compute mask for depth alignment based on confidence and depth thresholds.

    Args:
        depth_conf: Depth confidence tensor
        non_sky_mask: Non-sky region mask
        depth: Predicted depth tensor
        metric_depth: Metric depth tensor
        median_conf: Median confidence threshold
        min_depth_threshold: Minimum depth threshold
        min_metric_depth_threshold: Minimum metric depth threshold

    Returns:
        Boolean mask for valid alignment regions
    r   )r   r   r   r    r!   r"   r#   r   r   r   compute_alignment_maskD   s   r$   順 tensormax_samplesc                 C   s8   |   |kr| S tj|   | jdd| }|  | S )z
    Sample tensor elements for quantile computation to reduce memory usage.

    Args:
        tensor: Input tensor to sample
        max_samples: Maximum number of samples to take

    Returns:
        Sampled tensor
    )r   N)numelr   randpermr   flatten)r&   r'   idxr   r   r   sample_tensor_for_quantiled   s   r,        r@
intrinsicsscale_factorc                 C   sT   |ddddddf |ddddddf  d }| |ddddddf |  S )a  
    Apply metric scaling to depth based on camera intrinsics.

    Args:
        depth: Input depth tensor
        intrinsics: Camera intrinsics tensor
        scale_factor: Scaling factor for metric conversion

    Returns:
        Scaled depth tensor
    Nr         r   )r   r.   r/   focal_lengthr   r   r   apply_metric_scalingv   s   4 r3         i@	max_depthc                 C   s<   |   } || | < |dur|  }d|| < | |fS | dfS )a6  
    Set sky regions to maximum depth and high confidence.

    Args:
        depth: Depth tensor
        depth_conf: Depth confidence tensor
        non_sky_mask: Non-sky region mask
        max_depth: Maximum depth value for sky regions

    Returns:
        Tuple of (updated_depth, updated_depth_conf)
    Ng      ?)clone)r   r   r   r5   r   r   r   set_sky_regions_to_max_depth   s   

r7   )r   )r   )r   r   )r%   )r-   )r4   )__doc__typingr   r   Tensorfloatr   r   r$   intr,   r3   r7   r   r   r   r   <module>   sp   

 
