
    j/                     N    d dl Z d dlZddlmZ  G d d      Zd
dZd Zdefd	Zy)    N   )utilc                      e Zd Z	 ddZd Zed        Zej                  d        Zed        Zej                  d        Zed        Z	e	j                  d	        Z	ed
        Z
e
j                  d        Z
d Zd Zd Zd Zy)CameraNc                 |   |,dt        j                  d      j                          | _        n|| _        ||t	        d      d| _        || _        || _        |:| j                  dz  j                         j                  t        j                        }|| _        t        |      | _        t        |      | _        y)a  
        Create a new Camera object that stores camera intrinsic
        and extrinsic parameters.

        TODO: skew is not supported
        TODO: cx and cy that are not half of width and height

        Parameters
        ------------
        name : str or None
          Name for camera to be used as node name
        resolution : (2,) int
          Pixel size in (height, width)
        focal : (2,) float
          Focal length in pixels. Either pass this OR FOV
          but not both.  focal = (K[0][0], K[1][1])
        fov : (2,) float
          Field of view (fovx, fovy) in degrees
        z_near : float
          What is the closest
        Ncamera_   z$either focal length or FOV required!Fg      >@)r   	unique_iduppername
ValueError_focal_computedfocalfovroundastypenpint64
resolutionfloatz_farz_near)selfr   r   r   r   r   r   s          B/DATA/.local/lib/python3.12/site-packages/trimesh/scene/cameras.py__init__zCamera.__init__	   s    2 <!$.."3"9"9";!<=DI DI;5=CDD  % 
 ((T/00299"((CJ$ 5\
Fm    c                    t        t        j                  | j                        t        j                  | j                        t        j                  | j
                        t        j                  | j                              S )z:
        Safely get a copy of the current camera.
        )r   r   r   r   )r   copydeepcopyr   r   r   r   r   s    r   r   zCamera.copy?   sQ     tyy)}}T__5--

+dhh'	
 	
r   c                     | j                   S )z
        Get the camera resolution in pixels.

        Returns
        ------------
        resolution (2,) float
          Camera resolution in pixels
        )_resolutionr    s    r   r   zCamera.resolutionJ   s     r   c                     t        j                  |t         j                        }|j                  dk7  rt	        d      d|j
                  _        || _        | j                  rd| _	        yd| _
        y)z
        Set the camera resolution in pixels.

        Parameters
        ------------
        resolution (2,) float
          Camera resolution in pixels
        dtyper   zresolution must be (2,) floatFN)r   
asanyarrayr   shaper   flags	writeabler"   r   _focal_fovr   valuess     r   r   zCamera.resolutionV   s]     vRXX6<<4<==!&!DK DIr   c                     | j                   ]| j                  dt        j                  t        j                  | j
                  dz              z  z  }d|j                  _        || _         | j                   S )z
        Get the focal length in pixels for the camera.

        Returns
        ------------
        focal : (2,) float
          Focal length in pixels
               @F)r+   r"   r   tanradiansr,   r)   r*   )r   r   s     r   r   zCamera.focall   sZ     ;;$$bffRZZ		C5P.Q(QRE$)EKK!DK{{r   c                     |d| _         yd| _        t        j                  |t        j                        }|j
                  dk7  rt        d      d|j                  _        || _         d| _	        y)z
        Set the focal length in pixels for the camera.

        Returns
        ------------
        focal : (2,) float
          Focal length in pixels.
        NFr$   r&   zfocal length must be (2,) float)
r+   r   r   r'   float64r(   r   r)   r*   r,   r-   s     r   r   zCamera.focal~   s`     >DK
 $)D ]]6<F||t# !BCC%*FLL" DKDIr   c                     t        j                  dt         j                        }| j                  d   |d<   | j                  d   |d<   | j                  dz  |dd	d	f<   |S )
z
        Get the intrinsic matrix for the Camera object.

        Returns
        -----------
        K : (3, 3) float
          Intrinsic matrix for camera
           r$   r   r   r      r8   r8   r0   Nr   )r   eyer4   r   r   )r   Ks     r   r;   zCamera.K   sY     FF1BJJ'**Q-$**Q-$??S("1"a%r   c                 4   |y t        j                  |t         j                        }|j                  dk7  rt	        d      t        j
                  |j                         g d   g d      st	        d      |d   |d   g| _        |d d	d	f   d	z  | _        y )
Nr$   )r6   r6   zmatrix must be (3,3)!)r8   r6   r	         )r   r   r   r   r8   z4matrix should only have focal length and resolution!r7   r9   r   )	r   r'   r4   r(   r   allcloseflattenr   r   r-   s     r   r;   zCamera.K   s    >vRZZ8<<6!455{{6>>+O<oNSTT TlF4L1
 !Q-!+r   c                     | j                   ]dt        j                  t        j                  | j                  dz  | j
                  z              z  }d|j                  _        || _         | j                   S )z
        Get the field of view in degrees.

        Returns
        -------------
        fov : (2,) float
          XY field of view in degrees
        r0   F)r,   r   degreesarctanr"   r+   r)   r*   )r   r   s     r   r   z
Camera.fov   s[     99

299d.>.>.D-S#TUUC"'CIIDIyyr   c                     |d| _         yd| _        t        j                  |t        j                        }|j
                  dk7  rt        d      d|j                  _        || _         d| _	        y)z
        Set the field of view in degrees.

        Parameters
        -------------
        values : (2,) float
          Size of FOV to set in degrees
        NTr$   r&   zfov length must be (2,) intF)
r,   r   r   r'   r4   r(   r   r)   r*   r+   r-   s     r   r   z
Camera.fov   s`     >DI
 $(D ]]6<F||t# !>??%*FLL"DIDKr   c                     t        |       S )z
        Calculate ray direction vectors.

        Will return one ray per pixel, as set in self.resolution.

        Returns
        --------------
        vectors : (n, 3) float
          Ray direction vectors in camera frame with z == -1
        )camera_to_raysr    s    r   to_rayszCamera.to_rays   s     d##r   c                 @    t        j                  t        |              S )z
        Get ray spherical coordinates in radians.


        Returns
        --------------
        angles : (n, 2) float
          Ray spherical coordinate angles in radians.
        )r   rC   ray_pixel_coordsr    s    r   angleszCamera.angles   s     yy*40011r   c                 2    t        |fd| j                  i|S )a;  
        Generate transform for a camera to keep a list
        of points in the camera's field of view.

        Parameters
        -------------
        points : (n, 3) float
          Points in space
        rotation : None, or (4, 4) float
          Rotation matrix for initial rotation
        distance : None or float
          Distance from camera to center
        center : None, or (3,) float
          Center of field of view.

        Returns
        --------------
        transform : (4, 4) float
          Transformation matrix from world to camera
        r   )look_atr   )r   pointskwargss      r   rL   zCamera.look_at   s    * v64886v66r   c                 :    d| j                    d| j                   S )Nz<trimesh.scene.Camera> FOV: z Resolution: )r   r   r    s    r   __repr__zCamera.__repr__  s    -dhhZ}T__DUVVr   )NNNNg{Gz?g     @@)__name__
__module____qualname__r   r   propertyr   setterr   r;   r   rG   rJ   rL   rP    r   r   r   r      s    SY4$l	
 	  	   *  " \\ 2   XX, ,   	ZZ 2$
27.Wr   r   c                 X   |t        j                  d      }n%t        j                  |t         j                        }t        j                  | t         j                        } |ddddf   j                  }|j                  | j                        j                  }|.|j                  d      dt        j                  |d      z  z   }n|j                  |      }||z  }t        j                  t        j                  |      dz        }	|St        j                  t        j                  |dddd	f         |	z  |ddd	f   ddt         j                  f   z         }|||z  }|ddddf   j                  |      }
|j                         }|
||ddd	f   z  z   |dddf<   |S )
a  
    Generate transform for a camera to keep a list
    of points in the camera's field of view.

    Examples
    ------------
    ```python
    points = np.array([0, 0, 0], [1, 1, 1])
    scene.camera_transform = scene.camera.look_at(points)
    ```

    Parameters
    -------------
    points : (n, 3) float
      Points in space
    fov : (2,) float
      Field of view, in DEGREES
    rotation : None, or (4, 4) float
      Rotation matrix for initial rotation
    distance : None or float
      Distance from camera to center
    center : None, or (3,) float
      Center of field of view.

    Returns
    --------------
    transform : (4, 4) float
      Transformation matrix from world to camera
    N   r$   r6   r   )axisg      ?r0   r   )r   r:   r'   r4   Tdotminptpr1   r2   maxabsnewaxisr   )rM   r   rotationdistancecenterpadrinvpoints_ccenter_ctfovcenter_wcam_poses               r   rL   rL     s   > 66!9==<]]64F BQBFDxx!##H~<<Q<'#xa0H*HH 88F# H 66"**S/C'(D66"&&!RaR%1D88AqD>!RZZ-;XXY
C BQB##H-H}}HHRaRUO!;;HRaRUOOr   c                    | j                   }t        j                  | j                        dz  }t        j                  |      }|dd|z  z
  z  }| }|\  }}|\  }}t        j                  ||g||gg| j                         }	t        j                  d|d   dz
  g|d   dz
  dgg|      j                  t        j                        }
|	j                  |
j                  k(  sJ |	|
fS )a  
    Get the x-y coordinates of rays in camera coordinates at
    z == -1.

    One coordinate pair will be given for each pixel as defined in
    camera.resolution. If reshaped, the returned array corresponds
    to pixels of the rendered image.

    Examples
    ------------
    ```python
    xy = ray_pixel_coords(camera).reshape(
      tuple(camera.coordinates) + (2,))
    top_left == xy[0, 0]
    bottom_right == xy[-1, -1]
    ```

    Parameters
    --------------
    camera : trimesh.scene.Camera
      Camera object to generate rays from

    Returns
    --------------
    xy : (n, 2) float
      x-y coordinates of intersection of each camera ray
      with the z == -1 frame
    r0   r8   g      ?)boundscountr   )
r   r   r2   r   r1   r   grid_linspacer   r   r(   )camerareshalf_fov	right_topleft_bottomrighttopleftbottomxypixelss              r   rI   rI   [  s    < 

Czz&**%+Hx IcCi I*K JE3LD& 
		seV_-V5F5F
B
 CFQJ#a&1*a1fRXX  88v||###v:r   ro   c                     t        |       \  }}t        j                  t        j                  |t        j
                  |ddddf          f            }||fS )aC  
    Calculate the trimesh.scene.Camera object to direction vectors.

    Will return one ray per pixel, as set in camera.resolution.

    Parameters
    --------------
    camera : trimesh.scene.Camera

    Returns
    --------------
    vectors : (n, 3) float
      Ray direction vectors in camera frame with z == -1
    Nr8   )rI   r   unitizer   column_stack	ones_like)ro   rx   ry   vectorss       r   rF   rF     sP      "&)JBll2??BbBQBi1H0H+IJKGF?r   )NNNN)	r   numpyr    r   r   rL   rI   rF   rV   r   r   <module>r      s6      KW KW\BJ6r6 r   