
    Ti/                     V    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dS )    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dS )CameraN{Gz?     @@c                    |/dt          j        d                                           | _        n|| _        ||t	          d          d| _        || _        || _        |9| j        dz                                  	                    t          j                  }|| _        t          |          | _        t          |          | _        dS )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          O/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/scene/cameras.py__init__zCamera.__init__	   s    2 <=$."3"3"9"9";";==DII DI;5=CDDD  % 
 (T/002299"(CCJ$ 5\\
Fmm    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?   sW     ty))}T_55-
++dh''	
 
 
 	
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k    rt	          d          d|j        _        || _        | j        r	d| _	        dS d| _
        dS )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   sk     vRX666<4<===!&! 	DKKK DIIIr   c                     | j         L| 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
        N       @F)r-   r$   r   tanradiansr.   r+   r,   )r   r   s     r   r   zCamera.focall   sQ     ;$bfRZ	C5P5P.Q.Q(QRE$)EK!DK{r   c                     |	d| _         dS d| _        t          j        |t          j                  }|j        dk    rt          d          d|j        _        || _         d| _	        dS )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~   sl     >DKKK
 $)D ]6<<<F|t## !BCCC%*FL" DKDIIIr   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      r:   r:   r2   Nr   )r   eyer6   r   r   )r   Ks     r   r=   zCamera.K   sU     F1BJ'''*Q-$*Q-$?S("1"a%r   c                 T   |d S t          j        |t           j                  }|j        dk    rt	          d          t          j        |                                g d         g d          st	          d          |d         |d         g| _        |d d	d	f         d	z  | _        d S )
Nr&   )r8   r8   zmatrix must be (3,3)!)r:   r8   r         )r   r   r   r   r:   z4matrix should only have focal length and resolution!r9   r;   r   )	r   r)   r6   r*   r   allcloseflattenr   r   r/   s     r   r=   zCamera.K   s    >FvRZ888<6!!4555{6>>++OOO<oooNN 	USTTT TlF4L1
 !Q-!+r   c                     | j         Ld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
        Nr2   F)r.   r   degreesarctanr$   r-   r+   r,   )r   r   s     r   r   z
Camera.fov   sR     9
29d.>.D-S#T#TUUUC"'CIDIyr   c                     |	d| _         dS d| _        t          j        |t          j                  }|j        dk    rt          d          d|j        _        || _         d| _	        dS )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)   r6   r*   r   r+   r,   r-   r/   s     r   r   z
Camera.fov   sl     >DIII
 $(D ]6<<<F|t## !>???%*FL"DIDKKKr   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                 F    t          j        t          |                      S )z
        Get ray spherical coordinates in radians.


        Returns
        --------------
        angles : (n, 2) float
          Ray spherical coordinate angles in radians.
        )r   rE   ray_pixel_coordsr"   s    r   angleszCamera.angles   s      y*4000111r   c                 *    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   rN   zCamera.look_at   s!    * v66486v6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    VdhVVT_VVVr   )NNNNr   r   )__name__
__module____qualname__r   r    propertyr   setterr   r=   r   rI   rL   rN   rR    r   r   r   r      s^       SY4$ 4$ 4$ 4$l	
 	
 	
 	  	  X	    *   X" \  \2   X X, , X,   X 	Z  Z2$ $ $
2 
2 
27 7 7.W W W W Wr   r   c                 ^   |t          j        d          }n t          j        |t           j                  }t          j        | t           j                  } |ddddf         j        }|                    | j                  j        }|1|                    d          dt          j        |d          z  z   }n|                    |          }||z  }t          j        t          j	        |          dz            }	|Vt          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                             |          }
|                                }|
||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&   r8   r   )axisg      ?r2   r   )r   r<   r)   r6   Tdotminptpr3   r4   maxabsnewaxisr    )rO   r   rotationdistancecenterpadrinvpoints_ccenter_ctfovcenter_wcam_poses               r   rN   rN     s   > 6!99=<<<]6444F BQBFDxx!!#H~<<Q<''#xa0H0H0H*HH 88F## H 6"*S//C'((D6"&!!!RaR%11D88AAAqD>!!!RZ-;XXY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|                              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
    r2   r:   g      ?)boundscountr   )
r   r   r4   r   r3   r   grid_linspacer   r   r*   )camerareshalf_fov	right_topleft_bottomrighttopleftbottomxypixelss              r   rK   rK   [  s    < 
Cz&*%%+Hx  IcCi I*K JE3LD& 
	seV_-V5F
 
 
B
 CFQJ#a&1*a1  fRX  8v|####v:r   rq   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
    Nr:   )rK   r   unitizer   column_stack	ones_like)rq   rz   r{   vectorss       r   rH   rH     sZ      "&))JBl2?BbBQBi1H1H0H+IJJKKGF?r   )NNNN)	r    numpyr    r   r   rN   rK   rH   rX   r   r   <module>r      s              KW KW KW KW KW KW KW KW\B B B BJ6 6 6r6      r   