
    l0j!                         d Z ddlZddlmZmZmZmZmZ ddl	m
Z
 	 ddededed	ee         d
ee         f
dZdeded
ee         fdZdefdZ	 	 dd	edededee         fdZd Zdd	ee         d
ee         fdZdS )z
inertia.py
-------------

Functions for dealing with inertia tensors.

Results validated against known geometries and checked for
internal consistency.
    N   )	ArrayLikeNDArrayNumberOptionalfloat64)	multi_dotmassradiusheight	transformreturnc                     |dz  |dz  }}t          j        | |z  dz  | |z  dz  z   | |z  dz  | |z  dz  z   | |z  dz  g          }|t          j        d          z  }|t          ||          }|S )a_  
    Return the inertia tensor of a cylinder.

    Parameters
    ------------
    mass : float
      Mass of cylinder
    radius : float
      Radius of cylinder
    height : float
      Height of cylinder
    transform : (4, 4) float
      Transformation of cylinder

    Returns
    ------------
    inertia : (3, 3) float
      Inertia tensor
                )nparrayeyetransform_inertia)r
   r   r   r   h2r2diagonalinertias           R/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/inertia.pycylinder_inertiar      s    , QY	BxRi24"9/2Ri24"9/2BY!O	
 H "G#Iw77N    c                 B    d|dz  z  | z  t          j        d          z  S )z
    Return the inertia tensor of a sphere.

    Parameters
    ------------
    mass : float
      Mass of sphere
    radius : float
      Radius of sphere

    Returns
    ------------
    inertia : (3, 3) float
      Inertia tensor
    g?r   r   )r   r   )r
   r   s     r   sphere_inertiar    7   s%      &!)$t+bfQii77r   r   c                     t          j        | t           j                  } | j        dk    rt	          d          t           j                            |           \  }}|j        }||fS )as  
    Find the principal components and principal axis
    of inertia from the inertia tensor.

    Parameters
    ------------
    inertia : (3, 3) float
      Inertia tensor

    Returns
    ------------
    components : (3,) float
      Principal components of inertia
    vectors : (3, 3) float
      Row vectors pointing along the
      principal axes of inertia
    dtyper   r   zinertia tensor must be (3, 3)!)r   
asanyarrayr   shape
ValueErrorlinalgeighT)r   
componentsvectorss      r   principal_axisr-   J   sd    $ mG2:666G}9::: )..11J iGwr   Finertia_tensorparallel_axisc           	         t          j        | t           j                  } | j        dk    r| ddddf         }n| j        dk    r| }nt	          d          t          j        |t           j                  }|j        dk    rt	          d          |r| j        dk    r!t          j        dt           j                  }n| dddf         }t          j        |d         d	z  |d	         d	z  z   |d
          |d         z  |d
          |d	         z  g|d
          |d         z  |d
         d	z  |d	         d	z  z   |d          |d	         z  g|d
          |d	         z  |d          |d	         z  |d
         d	z  |d         d	z  z   gg          }|||z  z   }t          |j        ||g          S t          |||j        g          S )am  
     Transform an inertia tensor to a new frame.

     Note that in trimesh `mesh.moment_inertia` is *axis aligned*
     and at `mesh.center_mass`.

     So to transform to a new frame and get the moment of inertia at
     the center of mass the translation should be ignored and only
     rotation applied.

     If parallel axis is enabled it will compute the inertia
     about a new location.

     More details in the MIT OpenCourseWare PDF:
    ` MIT16_07F09_Lec26.pdf`


     Parameters
     ------------
     transform : (3, 3) or (4, 4) float
       Transformation matrix
     inertia_tensor : (3, 3) float
       Inertia tensor.
     parallel_axis : bool
       Apply the parallel axis theorum or not.
       If the passed inertia tensor is at the center of mass
       and you want the new post-transform tensor also at the
       center of mass you DON'T want this enabled as you *only*
       want to apply the rotation. Use this to get moment of
       inertia at an arbitrary frame that isn't the center of mass.

     Returns
     ------------
     transformed : (3, 3) float
       Inertia tensor in new frame.
    r"   )r   r   Nr   r$   z#transform must be (3, 3) or (4, 4)!zinertia_tensor must be (3, 3)!r   r   r   )	r   r%   r   r&   r'   zerosr   r	   r*   )r   r.   r/   r
   rotationaMaligned_inertias           r   r   r   l   s   V irz:::I&  RaR!V$	F	"	">???]>DDDNv%%9::: B?f$$"*---AA "1"a% A H1QqTQY&1!qteadlCA$1qtqy1Q4194qteadlCA$1!uqt|QqTQY1-BC
 
 )4!83(*ox@AAAh
;<<<r   c                    | j                                         }|dk                                     rdS |t          j                            |          z  }| j        }|                                }t          j        t          j	        ||                             }|dk     }|
                                r|d         }|dd         }d||fS |                                ru|t          j        ddgddgg          |                                                  }||         }|t          j        ddg          |                  d         }	||	         }d	||fS dS )
a  
    Check whether a mesh has radial symmetry.

    Returns
    -----------
    symmetry : None or str
         None         No rotational symmetry
         'radial'     Symmetric around an axis
         'spherical'  Symmetric around a point
    axis : None or (3,) float
      Rotation axis or point
    section : None or (3, 2) float
      If radial symmetry provide vectors
      to get cross section
    gKH9)NNNg-C6?r   r   N	sphericalradial)principal_inertia_componentscopyanyr   r(   normprincipal_inertia_vectorsargsortabsdiffallr   flatten)
meshscalarvectororderrA   	diff_zeroaxissectionsection_index
axis_indexs
             r   radial_symmetryrM      sU   $ .3355F 	   binnV,,,F+FNNE
 6"'&-(())DtI}} ' ay*D'))	 ' bhAB'899)DEMMOO' 28RG,,Y78;
j!w&&r   c                     | j         | j        fdj        D             }t          j        fd|D             t          j                  }|                    d          S )aK  
    Calculate the inertia of a scene about a specific frame.

    Parameters
    ------------
    scene : trimesh.Scene
      Scene with geometry.
    transform : None or (4, 4) float
      Homogeneous transform to compute inertia at.

    Returns
    ----------
    moment : (3, 3)
      Inertia tensor about requested frame
    c                      g | ]
}|         S  rP   ).0ngraphs     r   
<listcomp>z!scene_inertia.<locals>.<listcomp>  s    444!U1X444r   c           	          g | ]f\  }}t          |         d           |                             t          j        t          j                            |                              gS )moment_inertia_frame)hasattrrV   r   dotr(   inv)rQ   matggeomsr   s      r   rT   z!scene_inertia.<locals>.<listcomp>  sn     	
 	
 	
QuQx!788	
!H))"&s1C1CY*O*OPP	
 	
 	
r   r"   r   )rI   )rS   geometrynodes_geometryr   r   r   sum)scener   nodesmomentsr\   rS   s    `  @@r   scene_inertiarc     s    " KENE 5444u3444Eh	
 	
 	
 	
 	
	
 	
 	

 j  G ;;A;r   )N)FN)__doc__numpyr   typedr   r   r   r   r   utilr	   r   r    r-   boolr   rM   rc   rP   r   r   <module>ri      s        @ @ @ @ @ @ @ @ @ @ @ @ @ @       TX# #
# #*0#=Ei=P#W# # # #L8 8 8GG4D 8 8 8 8&I    J  !	L= L=L=L= L= 6
	L= L= L= L=^F F FR   HY$7  77CS            r   