
    MiV                     &   S r SSKJr  SSKJr  SSKrSSKJr  SSK	J
r
  SSKJr  SS	KJrJrJr  SS
KJrJr  \" \5      rS\S\4S jrSS jrSS jrS rS rS r\ " S S5      5       r SS\4S jjrS rS rSS jrS S jr S r!S!S jr"S r#S r$g)"z`
triangles.py
-------------

Functions for dealing with triangle soups in (n, 3, 3) float form.
    )	dataclass)	getLoggerN   )util)tol)point_plane_distance)NDArrayOptionalfloat64)diagonal_dotunitize	trianglesreturnc                 n   U SS2SS2SS24   U SS2SS2SS24   -
  nU R                   S   S:X  a%  [        R                  " USS2S4   USS2S4   5      $ U R                   S   S:X  a9  USS2S4   nUSS2S4   nUSS2S4   USS2S4   -  USS2S4   USS2S4   -  -
  $ [        U R                   5      e)z
Returns the cross product of two edges from input triangles

Parameters
--------------
triangles: (n, 3, 3) float
  Vertices of triangles

Returns
--------------
crosses : (n, 3) float
  Cross product of two edge vectors
Nr         r   )shapenpcross
ValueError)r   vectorsabs       k/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/triangles.pyr   r      s     12q!Ia!Qh$77GqQxx1wq!t}55		q	 AqDMAqDMAw1a4 1QT7Qq!tW#444
Y__
%%    c                    Uc,  [        [        R                  " U [        R                  S95      n[	        UR
                  5      S:X  a  [        R                  " U5      S-  $ [        R                  " US-  R                  SS95      S-  $ )ai  
Calculates the sum area of input triangles

Parameters
----------
triangles : (n, 3, 3) float
  Vertices of triangles
crosses : (n, 3) float or None
  As a speedup don't re- compute cross products
sum : bool
  Return summed area or individual triangle area

Returns
----------
area : (n,) float or float
  Individual or summed area depending on `sum` argument
dtyper   g       @r   axis)	r   r   
asanyarrayr   lenr   abssqrtsum)r   crossess     r   arear'   0   sl    $ irzzBC
7==Qvvg$$77GQJ###+,s22r   c                 H   U b  [         R                  " U [         R                  S9n U R                  S   S:X  aM  [         R                  " / SQU R                  S   S45      n[         R
                  " [        U 5      [        S9nX#4$ Uc  [        U 5      n[        USS9u  p#X#4$ )	a  
Calculates the normals of input triangles

Parameters
------------
triangles : (n, 3, 3) float
  Vertex positions
crosses : (n, 3) float
  Cross products of edge vectors

Returns
------------
normals : (m, 3) float
  Normal vectors
valid : (n,) bool
  Was the face nonzero area or not
r   r   )        r*         ?r   r   T)check_valid)
r   r!   r   r   tileonesr"   boolr   r   )r   r&   unitvalids       r   normalsr2   J   s    $ MM)2::>	??2!#77?Y__Q-?,CDDGGC	N$7E;	"'t4KD;r   c                    [         R                  " U [         R                  S9n [        U SS2S4   U SS2S4   -
  5      n[        U SS2S4   U SS2S4   -
  5      n[        U SS2S4   U SS2S4   -
  5      n[         R                  " [        U 5      S4[         R                  S9n[         R                  " [         R                  " [        X5      SS5      5      USS2S4'   [         R                  " [         R                  " [        U* U5      SS5      5      USS2S4'   [         R                  USS2S4   -
  USS2S4   -
  USS2S4'   SXD[        R                  :  R                  SS	9SS24'   U$ )
z
Calculates the angles of input triangles.

Parameters
------------
triangles : (n, 3, 3) float
  Vertex positions

Returns
------------
angles : (n, 3) float
  Angles at vertex positions in radians
  Degenerate angles will be returned as zero
r   Nr   r   r   r   r)   r*   r   )r   r!   r   r   zerosr"   arccosclipr   pir   mergeany)r   uvwresults        r   anglesr>   j   s@     irzz:I 		!Q$)AqD/12A	!Q$)AqD/12A	!Q$)AqD/12A XXs9~q)<F99RWW\!%7Q?@F1a4L99RWW\1"a%8"a@AF1a4L556!Q$<'&A,6F1a4L 36FSYY###+Q./Mr   c                 v   [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      e[        U 5      S   nU S   S   n[        U SS R                  S5      UUS9n[         R                  " [         R                  " U5      [        R                  :  5      nU$ )	z
Check to see if a list of triangles are all coplanar

Parameters
----------------
triangles: (n, 3, 3) float
  Vertices of triangles

Returns
---------------
all_coplanar : bool
  True if all triangles are coplanar
r   r)   r   r   Triangles must be (n, 3, 3)!r   r   Nr)   r   pointsplane_normalplane_origin)r   r!   r   r   is_shaper   r2   r   reshapeallr#   r   zero)r   test_normaltest_vertex	distancesall_coplanars        r   rN   rN      s     irzz:I==J//788)$Q'KA,q/K$}$$W-  I
 66"&&+chh67Lr   c           	         [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      e[        U 5      S   nU S   S   n[        U SS R                  S5      UUS9n[         R                  " [         R                  " [         R                  " UR                  S5      [        R                  :  5      SS	95      nU$ )
z
For a list of triangles if the FIRST triangle is coplanar
with ANY of the following triangles, return True.
Otherwise, return False.
r   r@   rA   r   r   NrB   rC   r   )r   r!   r   r   rG   r   r2   r   rH   r9   rI   r#   r   rJ   )r   rK   rL   rM   any_coplanars        r   rP   rP      s     irzz:I==J//788)$Q'KA,q/K$}$$W-  I
 66"&&	(9(9'(BSXX(M!NUVWXLr   c                   l    \ rS rSr% \\S'   \\S'   \\S'   \\   \S'   Sr\	\\      \S'   S\
4S	 jrS
rg)MassProperties   densitymassvolumecenter_massNinertiaitemc                     [        X5      $ N)getattr)selfrY   s     r   __getitem__MassProperties.__getitem__   s    t""r    )__name__
__module____qualname____firstlineno__float__annotations__r	   r   rX   r
   strr^   __static_attributes__r`   r   r   rR   rR      sF     N K M !! +/GXgg&'.# #r   rR   c                 P   [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      eUc  [        U 5      nUc  SnU SS2SSS24   U SS2SSS24   -   U SS2SSS24   -   nU SS2SSS24   S-  U SS2SSS24   S-  -   U SS2SSS24   U SS2SSS24   -  -   U SS2SSS24   U-  -   nU SS2SSS24   S	-  U SS2SSS24   S-  U SS2SSS24   -  -   U SS2SSS24   U SS2SSS24   S-  -  -   U SS2SSS24   S	-  -   U SS2SSS24   U-  -   nX`SS2SSS24   U-   U SS2SSS24   -  -   nX`SS2SSS24   U-   U SS2SSS24   -  -   n	X`SS2SSS24   U-   U SS2SSS24   -  -   n
[         R                  " S
[        U5      45      nUSS2S4   USS2S4   -  US'   X-  R                  USS& X-  R                  USS& [        S	5       Hj  n[         R                  " US-   S	5      nUSS2U4   U SS2SU4   USS2U4   -  U SS2SU4   U	SS2U4   -  -   U SS2SU4   U
SS2U4   -  -   -  XS-   '   Ml     UR                  SS9[         R                  " / SQ[         R                  S9-  nUS   nUcE  [         R                  " U5      [        R                   :  a  ["        R%                  S5        USS U-  n['        UX/-  UUS9nU(       a  U$ [         R                  " S5      nUS   US   -   XSS/   S-  R                  5       -  -
  US'   US   US   -   XSS/   S-  R                  5       -  -
  US'   US   US   -   XSS/   S-  R                  5       -  -
  US'   US   U[         R(                  " USS/   5      -  -
  * US'   US   U[         R(                  " USS/   5      -  -
  * US'   US   U[         R(                  " USS/   5      -  -
  * US'   US   US'   US   US'   US   US'   UU-  Ul        U$ )a  
Calculate the mass properties of a group of triangles.

Implemented from:
http://www.geometrictools.com/Documentation/PolyhedralMassProperties.pdf

Parameters
----------
triangles : (n, 3, 3) float
  Triangle vertices in space
crosses : (n,) float
  Optional cross products of triangles
density : float
  Optional override for density
center_mass :  (3,) float
  Optional override for center mass
skip_inertia : bool
  if True will not return moments matrix

Returns
---------
info : dict
  Mass properties
r   r@   rA   Nr+   r   r   r   r   
         r   )
      rn   rn   <   ro   ro   x   rp   rp   z@volume is negative center of mass is almost certain to be wrong!)rT   rU   rV   rW   )r   r      rm   )r   r   )r   r   )r   r   )r   r      )r   r   	   )r   r   )r   r   )r   r   )r   r   )r   r!   r   r   rG   r   r   r4   r"   Trangemodr%   arrayr#   r   rJ   logdebugrR   prodrX   )r   r&   rT   rW   skip_inertiaf1f2f3g0g1g2integrali
triangle_i
integratedrV   r=   rX   s                     r   mass_propertiesr      s   6 irzz:I==J//788	" 
1a7	i1a0	09Q1W3E	EB 	!Q'a
Aq!G

!	"
Aq!G
yAq1
1	2 Aq!G
r
!	"  
1a7	q	 Q1W"yAq'9
:	;Q1W)Aq!G"4"9
:	; Q1W"	$ Q1W"		$  
Aq!B&)Aq!G*<<	<B	Aq!B&)Aq!G*<<	<B	Aq!B&)Aq!G*<<	<BxxSW&H!Q$-"QT(*HQK\$$HQqM\$$HQqM1XVVAE1%
!!Q$-q!Z'(2ad83Az)*R1X57Az)*R1X57
Q  1%2"**) J ]F 66&>CHH$IIXY 1o.	F hhvG1
1%1v3F!3K2P2P2R)RS DM 	1
1%1v3F!3K2P2P2R)RS DM 	1
1%1v3F!3K2P2P2R)RS DM !mvQF8K0L'LMNGDM mvQF8K0L'LMNGDM mvQF8K0L'LMNGDMDMGDMDMGDMDMGDMw&FNMr   c                    [         R                  " U [         R                  S9n [        R                  " U SSS9(       d  [        SU R                  < 35      e[         R                  " U[         R                  S9n[        U 5      u  p#UR                  S:X  a  [         R                  " X!5      nO[        X!U   5      n[         R                  " [        U 5      [        S9nUS:  XS'   U$ )a*  
Given a list of triangles and a list of normals determine if the
two are aligned

Parameters
----------
triangles : (n, 3, 3) float
  Vertex locations in space
normals_compare : (n, 3) float
  List of normals to compare

Returns
----------
aligned : (n,) bool
  Are normals aligned with triangles
r   r@   T)allow_zerosz)triangles must have shape (n, 3, 3), got )r   r*   )r   r!   r   r   rG   r   r   r2   dotr   r4   r"   r/   )r   normals_compare
calculatedr1   
differencealigneds         r   windings_alignedr   O  s    " irzz:I==JDADY__DWXYYmmO2::FO	*J$VVJ8
 "*e.DE
hhs9~T2G#%GNNr   c                 *   [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      e[         R                  " U R                  SS9U R                  SS945      n[        R                  " U5      nU$ )z
Given a list of triangles, create an r-tree for broad- phase
collision detection

Parameters
---------
triangles : (n, 3, 3) float
  Triangles in space

Returns
---------
tree : rtree.Rtree
  One node per triangle
r   )r)   r   )r   r   rA   r   r   )
r   r!   r   r   rG   r   column_stackminmaxbounds_tree)r   triangle_boundstrees      r   r   r   s  sv     irzz:I==O44788 ooy}}!}'<immQRm>S&TUOO,DKr   c                     [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      eUc  [        R                  n[        XS9U:  R                  SS9nU$ )a  
Find all triangles which have an oriented bounding box
where both of the two sides is larger than a specified height.

Degenerate triangles can be when:
1) Two of the three vertices are colocated
2) All three vertices are unique but colinear


Parameters
----------
triangles : (n, 3, 3) float
  Triangles in space
height : float
  Minimum edge length of a triangle to keep

Returns
----------
nondegenerate : (n,) bool
  True if a triangle meets required minimum height
r   r@   rA   )r   areasr   r   )
r   r!   r   r   rG   r   r   r8   extentsrI   )r   r   heightoks       r   nondegenerater     sg    , irzz:I==J//788~ I
3f
<	A	Aq	A	IBIr   c                 R   [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      eUc	  [        U S9nU SS2S4   U SS2S4   -
  nU SS2S4   U SS2S4   -
  nUS-  R                  SS	9S
-  nUS-  R                  SS	9S
-  nU[        R                  :  nU[        R                  :  n[         R                  " [        U 5      S4[         R                  S9nX   S-  XF   -  USS2S4   U'   X   S-  XW   -  USS2S4   U'   U$ )a  
Return the 2D bounding box size of each triangle.

Parameters
----------
triangles : (n, 3, 3) float
  Triangles in space
areas : (n,) float
  Optional area of input triangles

Returns
----------
box :  (n, 2) float
  The size of each triangle's 2D oriented bounding box
r   r@   rA   N)r   r   r   r   r   g      ?)r   r!   r   r   rG   r   r'   r%   r   r8   r4   r"   )	r   r   r   r   length_alength_b	nonzero_a	nonzero_bboxs	            r   r   r     s4     irzz:I==J//788}y) 	!Q$)AqD/)A!Q$)AqD/)A 1zzqz!S(H1zzqz!S(H 399$I399$I
 ((C	NA&bjj
9C!,q0H4GGC1Ii!,q0H4GGC1IiJr   c                    [         R                  " U[         R                  S9n[         R                  " U [         R                  S9n XR	                  SS9R                  S5      -  nXR                  S5      -  R	                  SS9nU$ )a  
Convert a list of barycentric coordinates on a list of triangles
to cartesian points.

Parameters
------------
triangles : (n, 3, 3) float
  Triangles in space
barycentric : (n, 2) float
  Barycentric coordinates

Returns
-----------
points : (m, 3) float
  Points in space
r   r   r   r)   r   )r)   r   r   )r   rw   r   r!   r%   rH   )r   barycentricrD   s      r   barycentric_to_pointsr     sr    " ((;bjj9Kirzz:I ???*227;;K--j99>>A>FFMr   c                 X  ^ ^^ UU U4S jnUU U4S jn[         R                  " T [         R                  S9m [         R                  " U[         R                  S9n[        T R                  5      S:w  a  [        S5      eT R                  S   n[        UR                  5      S:w  d3  UR                  S   U:w  d   UR                  S   T R                  S   :w  a  [        S	5      eT S
S
2SS
24   T S
S
2S
S24   -
  mUT S
S
2S4   R                  SU45      -
  mUS:X  a  U" 5       $ U" 5       $ )a  
Find the barycentric coordinates of points relative to triangles.

The Cramer's rule solution implements:
    http://blackpawn.com/texts/pointinpoly

The cross product solution implements:
    https://www.cs.ubc.ca/~heidrich/Papers/JGT.05.pdf


Parameters
-----------
triangles : (n, 3, 2 | 3) float
  Triangles vertices in space
points : (n, 2 | 3) float
  Point in space associated with a triangle
method :  str
  Which method to compute the barycentric coordinates with:
    - 'cross': uses a method using cross products, roughly 2x slower but
              different numerical robustness properties
    - anything else: uses a cramer's rule solution

Returns
-----------
barycentric : (n, 3) float
  Barycentric coordinates of each point
c                    > [         R                  " TS S 2S4   TS S 2S4   5      n [        X 5      n[         R                  " [	        T5      S4[         R
                  S9n[        [         R                  " TS S 2S4   T5      U 5      U-  US S 2S4'   [        [         R                  " TTS S 2S4   5      U 5      U-  US S 2S4'   SUS S 2S4   -
  US S 2S4   -
  US S 2S4'   U$ )Nr   r   r   r   r   )r   r   r   r4   r"   r   )ndenominatorr   edge_vectorsr   r<   s      r   method_cross+points_to_barycentric.<locals>.method_cross  s    HH\!Q$'ad);<"1(hhI2"**E(,q!t2Da)H!L{ZAqD(!\!Q$5G)H!L{ZAqDAqD 11K14EEAqDr   c                    > [        TS S 2S4   TS S 2S4   5      n [        TS S 2S4   TS S 2S4   5      n[        TS S 2S4   T	5      n[        TS S 2S4   TS S 2S4   5      n[        TS S 2S4   T	5      nSX-  X-  -
  -  n[        R                  " [        T5      S4[        R                  S9nX-  X-  -
  U-  US S 2S4'   X2-  X-  -
  U-  US S 2S4'   SUS S 2S4   -
  US S 2S4   -
  US S 2S4'   U$ )Nr   r   r+   r   r   r   )r   r   r4   r"   r   )
dot00dot01dot02dot11dot12inverse_denominatorr   r   r   r<   s
          r   method_cramer,points_to_barycentric.<locals>.method_cramer  s"   \!Q$/ad1CD\!Q$/ad1CD\!Q$/3\!Q$/ad1CD\!Q$/3!U]U]%BChhI2"**E"]U]:>QQAqD"]U]:>QQAqDAqD 11K14EEAqDr   r   r   triangles shape incorrectr   r   r   z$triangles and points must correspondNr)   r   )r   r!   r   r"   r   r   rH   )r   rD   methodr   r   dimr   r<   s   `     @@r   points_to_barycentricr     s   :  irzz:I]]64F 9??q 455 //!
CFLLQ<<?c!<<?iooa00?@@QU#i2A2&66L1a4(("c33A~?r   c                 H	   [         R                  " U [         R                  S9n [         R                  " U[         R                  S9n[        R                  " U S5      (       d  [        S5      e[        R                  " U[        U 5      S45      (       d  [        S5      e[         R                  " U5      n[         R                  " [        U5      [        S9n/ SQnU SS2SSS24   nU SS2S	SS24   nU SS2S
SS24   nXe-
  nXu-
  n	X-
  n
[         R                  " X-  U5      n[         R                  " X-  U5      n[         R                  " U[        R                  :  U[        R                  :  5      n[        U5      (       a
  X]   X-'   SX='   X-
  n[         R                  " X-  U5      n[         R                  " X-  U5      nU[        R                  * :  UU:*  -  U-  n[        U5      (       a  UU   UU'   SUU'   UU-  X-  -
  nU[        R                  :  U[        R                  * :  -  U[        R                  :  -  U-  n[        U5      (       a6  UU   UU   UU   -
  -  R                  S5      nUU   UUU   -  -   UU'   SUU'   X-
  n[         R                  " UU-  U5      n[         R                  " U	U-  U5      nU[        R                  * :  UU:*  -  U-  n[        U5      (       a  UU   UU'   SUU'   UU-  UU-  -
  nU[        R                  :  U[        R                  * :  -  U[        R                  :  -  U-  n[        U5      (       a6  UU   UU   UU   -
  -  R                  S5      nUU   UU	U   -  -   UU'   SUU'   UU-  UU-  -
  nU[        R                  :  UU-
  [        R                  * :  -  UU-
  [        R                  * :  -  U-  n[        U5      (       aG  UU   UU   -
  nUUUU   UU   -
  -   -  R                  S5      nUU   UUU   UU   -
  -  -   UU'   SUU'   [        U5      (       aX  SUU   UU   -   UU   -   -  nUU   U-  R                  S5      nUU   U-  R                  S5      nXS   X   U-  -   X   U-  -   X#'   U$ )a  
Return the closest point on the surface of each triangle for a
list of corresponding points.

Implements the method from "Real Time Collision Detection" and
use the same variable names as "ClosestPtPointTriangle" to avoid
being any more confusing.


Parameters
----------
triangles : (n, 3, 3) float
  Triangle vertices in space
points : (n, 3) float
  Points in space

Returns
----------
closest : (n, 3) float
  Point on each triangle closest to each point
r   r@   r   r   z)need same number of triangles and points!)r+   r+   r+   Nr   r   r   Fr   r+   )r   r!   r   r   rG   r   r"   
zeros_liker.   r/   r   logical_andr   rJ   r9   rH   ) r   rD   r=   remainr.   r   r   cabacapd1d2is_abpd3d4is_bvcis_abr;   cpd5d6is_cvbis_acr<   vais_bcd43denoms                                    r   closest_pointr   G  s   0 irzz:I]]64F==J//455==#i.!!455DEE ]]6"FWWS[-F D 	!Q'A!Q'A!Q'A 
B	
B	B 
	B		B >>"sxx-chh7D
4yyw 
B		B		B #((NrRx(61D
4yywtt r'bg	B#((]rSXXI~."sxx-@6IE
5zzY"U)bi/099'B%A5	M2uu 
B	R	B	R	B#((NrRx(61D
4yywtt r'b2g	B#((]rSXXI~."sxx-@6IE
5zzY"U)bi/099'B%1r%y=0uu r'b2g	B#((]RCHH945"r'chhY9NORXXE
5zzi"U)#C2e9r%y012;;GD%1%1U8(;#<<uu 6{{r&zBvJ.F;<Z%((1Z%((1bj1n5aHMr   c                     [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      eU R                  S5      n[         R                  " [        U5      5      R                  S5      nXS.nU$ )ah  
Convert a list of triangles to the kwargs for the Trimesh
constructor.

Parameters
---------
triangles : (n, 3, 3) float
  Triangles in space

Returns
---------
kwargs : dict
  Keyword arguments for the trimesh.Trimesh constructor
  Includes keys 'vertices' and 'faces'

Examples
---------
>>> mesh = trimesh.Trimesh(**trimesh.triangles.to_kwargs(triangles))
r   r@   rA   rB   )verticesfaces)	r   r!   r   r   rG   r   rH   aranger"   )r   r   r   kwargss       r   	to_kwargsr     so    ( irzz:I==J//788  )HIIc(m$,,W5E"3FMr   )NN)NNNFr[   )cramer)%__doc__dataclassesr   loggingr   numpyr    r   	constantsr   rD   r   typedr	   r
   r   r   r   ra   rx   r   r'   r2   r>   rN   rP   rR   r   r   r   r   r   r   r   r   r   r`   r   r   <module>r      s    "     ( - - '&W & &434@#L:* # # #. KPssl!H2!H*Z6L^slr   