
    Mi@                        S r SSKrSSKJrJrJr  SSKJr  SSK	J
r
Jr  SSKJr  SSKJrJrJrJrJrJrJr  S	\4S
 jrS\\   S\S\\   4S jr   S&S	\S\S\S\\   S\\R0                     4
S jjrS'S	\S\S\\   4S jjrS(S	\S\S\4S jjrS)S	\S\4S jjrS*S	\S\S\4S jjr S+S	\S\S\\R*                  \\R*                     4   4S jjr  S,S	\S\S \S!\4S" jjr    S-S	\S\S#\\   S$\S\!4
S% jjr"g).z@
segments.py
--------------

Deal with (n, 2, 3) line segments.
    N   )geometrytransformationsutil)tol)
group_rowsunique_rows)union)	ArrayLikeIntegerNDArrayNumberOptionalUnionfloat64segmentsc                    [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        SU R                  5      eU SS2S4   nU SS2S4   U-
  n[        R                  " U5      nX#R                  S5      -  n[        R                  " X5      nXR                  S5      U* -  -   n[         R                  " XDU-   45      n[        R                  " USS	9u  p'XgR                  S5      -  nXRU4$ )
a  
For 3D line segments defined by two points, turn
them in to an origin defined as the closest point along
the line to the zero origin as well as a direction vector
and start and end parameter.

Parameters
------------
segments : (n, 2, 3) float
   Line segments defined by start and end points

Returns
--------------
origins : (n, 3) float
   Point on line closest to [0, 0, 0]
vectors : (n, 3) float
   Unit line directions
parameters : (n, 2) float
   Start and end distance pairs for each line
dtype)r   )r      zincorrect segment shape!Nr      r   r   T)return_sign)np
asanyarrayr   r   is_shape
ValueErrorshaperow_normreshapediagonal_dotcolumn_stackvector_hemisphere)r   endpointvectorsvectors_normoffsetorigins
parameterssignss           o/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/path/segments.pysegments_to_parametersr-      s    * }}XRZZ8H==?333X^^DD 1~Hq!tnx'G==)L##G,,G x1F..1WH<=G &<*?!@AJ++GFNG--((JZ''    r)   r&   r*   c           	      ~   [         R                  " U [         R                  S9n [         R                  " U[         R                  S9n[         R                  " U[         R                  S9n[         R                  " XUSS2SS24   -  -   XUSS2SS24   -  -   45      nUR	                  SSU R
                  S   45      $ )a}  
Convert a parametric line segment representation to
a two point line segment representation

Parameters
------------
origins : (n, 3) float
   Line origin point
vectors : (n, 3) float
   Unit line directions
parameters : (n, 2) float
   Start and end distance pairs for each line

Returns
--------------
segments : (n, 2, 3) float
   Line segments defined by start and end points
r   Nr   r   r   )r   r   r   hstackr!   r   )r)   r&   r*   r   s       r,   parameters_to_segmentsr1   >   s    , mmG2::6GmmG2::6Gz<J yy	Z2A2..	.JqRSRTuDU:U0UVH RGMM!$4566r.   radiusanglelengthreturnc           	         SSK Jn  [        U 5      u  pVnUR                  U5      nUR	                  USS9n	[
        R                  " Xi   5      n
[        R                  " [        R                  " U
[        R                  US9[        R                  " U
SUS95      nX   nUb_  UR                  u  p[        R                  R                  X   SS2/ SQSS24   X   SS2/ S	QSS24   -
  S
S9R                  SS9nXU:     nU$ )a  
Find pairs of segments which are colinear.

Parameters
-------------
segments : (n, 2, (2, 3)) float
  Two or three dimensional line segments
radius
  Maximum radius line origins can differ
  and be considered colinear
angle
  Maximum angle in radians segments can
  differ and still be considered colinear
length
  If specified, will additionally require
  that pairs have a *vertex* within this distance.

Returns
------------
pairs : (m, 2) int
  Indexes of segments which are colinear
r   )spatialndarray)routput_typeatol        N)r   r   r   r   )r   r   r   r   r   axisr   )scipyr7   r-   cKDTreequery_pairsr   vector_angler   
logical_orr   isclosepiTlinalgnormmin)r   r2   r3   r4   r7   r)   r&   _paramtreepairsanglesangle_okcolinearab
min_vertexs                   r,   colinear_pairsrT   `   s   8 
  6h?Gf ??7#D v9=E ""7>2F }}VRUU/fcPU1VH
 H zz YY^^K<*+hk!\1:L.MMTU $ 

#1#+ 	
 /0Or.   digitsc                    [        U 5      u  p#nUR                  SS9  [        [        R                  " X#45      US9nU Vs/ s H)  n[        XF   XF   SS2S4   R                  5          SS9PM+     nn[        R                  " [        XW5       VVs/ s H  u  phUS[        U5       PM     snn5      n	[        X)   X9   [        R                  " U5      S9$ s  snf s  snnf )	a  
Clean up line segments by unioning the ranges of colinear segments.

Parameters
------------
segments : (n, 2, 2) or (n, 2, 3)
  Line segments in space.
digits
  How many digits to consider.

Returns
-----------
cleaned : (m, 2, 2) or (m, 2, 3)
  Where `m <= n`
r   r>   rU   Nr   F)sort)r)   r&   r*   )r-   rX   r   r   r#   r
   argsortconcatenateziplenr1   vstack)
r   rU   r)   r&   paramgroupsgunionsuindexess
             r,   cleanrd      s    & 5X>Ge 
JJAJ (:;FKF NTTVeEHUXad^3356UCVFTnnc&6IJ6Idaa#a&k6IJKG " '*:ryyQWGX  UJs   0CC
pointsr<   c           
         [         R                  " U[         R                  S9n[         R                  " U [         R                  S9n U R                  SU R                  S   45      nU SS2SSS24   U SS2SSS24   -
  S-  R                  SS9S-  n[         R                  " [        U 5      [        S9n/ nU H  nX7-
  S-  R                  SS9R                  S	5      S-  n[         R                  " [        R                  " XHR                  SS9US
9[        R                  " USUS
9R                  SS9) 5      n	U	R                  5       (       d  M  [         R                  " XY) 5      nX	    H-  n
UR                  XzS   /5        UR                  XzS   /5        M/     M     [        U5      S:  a  [         R                  " X   U45      $ U $ )a  
Find any points that lie on a segment (not an endpoint)
and then split that segment into two segments.

We are basically going to find the distance between
point and both segment vertex, and see if it is with
tolerance of the segment length.

Parameters
--------------
segments : (n, 2, (2, 3) float
  Line segments in space
points : (n, (2, 3)) float
  Points in space
atol : float
  Absolute tolerance for distances

Returns
-------------
split : (n, 2, (3 | 3) float
  Line segments in space, split at vertices
r   r   r   Nr   r   r>   g      ?r   r   r;   r=   )r   r   r   r!   r   sumonesr\   boollogical_andr   rE   anyappendr]   )r   re   r<   seg_flatr4   keepnew_segppairon_segsegs              r,   splitru      s   0 ]]64F}}XRZZ8HX^^A%6 78H 1a 8Aq!G#44:??Q?G3NF 773x=-DG 
 !#((a(088ASH LLq!1=\\$$/333;;
 ::<<>>$0D'q6{+q6{+ (% , 7|ayy(.'233r.   c                 v   [         R                  " U [         R                  S9n [        U R	                  SU R
                  S   45      US9S   R	                  S5      nUR                  SS9  [         R                  " [        U 5      [        S9nSU[        U5      S	   '   S
X2SS2S	4   USS2S4   :H  '   X   nU$ )a  
Find unique non-zero line segments.

Parameters
------------
segments : (n, 2, (2|3)) float
  Line segments in space
digits : int
  How many digits to consider when merging vertices

Returns
-----------
unique : (m, 2, (2|3)) float
  Segments with duplicates merged
r   r   r   rW   r   rg   r>   Tr   FN)
r   r   r   r	   r!   r   rX   zerosr\   rj   )r   rU   inversemaskuniques        r,   rz   rz     s      }}XRZZ8H (**Bq0A+BCFS	gg  LLaL88CM.D$(DW	a	 !+0DA'!Q$-	'(^FMr.   heightdouble_sidedc           	         [         R                  " U [         R                  S9n [        R                  " U S5      (       d  [        S5      e[         R                  " [         R                  " U R                  S5      S5      R                  S5      [         R                  " SUSU/[        U 5      5      45      n[         R                  " / SQ[        U 5      S45      [         R                  " [        U 5      5      R                  S	5      S
-  -   R                  S5      nU(       a,  [         R                  " U[         R                  " U5      45      nX44$ )a^  
Extrude 2D line segments into 3D triangles.

Parameters
-------------
segments : (n, 2, 2) float
  2D line segments
height : float
  Distance to extrude along Z
double_sided : bool
  If true, return 4 triangles per segment

Returns
-------------
vertices : (n, 3) float
  Vertices in space
faces : (n, 3) int
  Indices of vertices forming triangles
r   r   r   r   zsegments shape incorrectrg   r   r   )r   r   r   r   r   r   r   r      )r   r   )r   r   r   r   r   r   r#   tiler!   r\   aranger]   fliplr)r   r{   r|   verticesfacess        r,   extruder   5  s    ( }}XRZZ8H==:..344 GGH$$W-q199'BGGQ6*CM:	
H 	"S]A$67
))CM
"
*
*7
3a
7	8gg 

 		5"))E"234?r.   summedc                     [         R                  " U [         R                  S9n [        R                  " U SS2SSS24   U SS2SSS24   -
  5      nU(       a  UR                  5       $ U$ )a  
Return the lengths of an array of line segments.

Parameters
-------------
segments : (n, 2, 2) float
  2D line segments
summed
  Return the total length, not the per-segment length.

Returns
-------------
length
  Either total length or per-segment length.
r   Nr   r   )r   r   r   r   r    rh   )r   r   normss      r,   r4   r4   a  sU    $ }}XRZZ8HMM(1a7+hq!Qw.??@Eyy{Lr.   maxlenreturn_indexreturn_countc           	         [        U5      n[        R                  " U [        R                  S9n [	        U R
                  5      S:w  a  [        U R
                   S35      eU R
                  S   nU SS2S4   nU SS2S4   nXe-
  n[        R                  " [        R                  " U5      U-  5      R                  [        R                  5      n/ n	/ n
[        R                  n[        R                  " [        R                  " UR                  5       S-   5      5      n[        R                   " U5       GH  nX:H  nX~   U-  nU" XS-   5      R#                  SU45      U" [        R                  " US-   5      [	        U5      5      R#                  S	5      -  nU" X^   US-   5      R#                  SU45      nUU-   R#                  SUS-   U45      nU	R%                  USS2USU 4   5        U(       av  [        R&                  " U5      S   R#                  S	5      nU
R)                  [        R*                  " [	        U5      U4[        R                  S9U-  R-                  5       5        [.        R0                  (       d  GMM  [3        X   U5       HN  u  nn[        R4                  " US   US   5      (       d   e[        R4                  " US   US   5      (       a  MN   e   [        R4                  " [        R                  " [        R                  " US-   5      5      USU 5      (       a  GM   e   [        R6                  " U	5      /n	[.        R0                  (       a2  [        R8                  " [;        U 5      [;        U	S   5      S
S9(       d   eU(       a  [        R6                  " U
5      n
[.        R0                  (       aI  [	        U
5      [	        U	S   5      :X  d   e[=        U
5      [=        [?        [	        U 5      5      5      :X  d   eU	R)                  U
5        U(       a  U	R)                  U5        [	        U	5      S:X  a  U	S   $ U	$ )a>  
Resample line segments until no segment
is longer than maxlen.

Parameters
-------------
segments : (n, 2, 2|3) float
  2D line segments
maxlen : float
  The maximum length of a line segment
return_index : bool
  Return the index of the source segment
return_count : bool
  Return how many segments each original was split into

Returns
-------------
resampled : (m, 2, 2|3) float
  Line segments where no segment is longer than maxlen
index : (m,) int
  [OPTIONAL] The index of segments resampled came from
count : (n,) int
  [OPTIONAL] The count of the original segments
r   r   z != (n, 2, 2|3)r   Nr   r   r   r   gMbP?r;   ) floatr   arrayr   r\   r   r   ceilr   r    astypeint64r   stack_linesr   maxrz   r!   extendnonzerorm   ri   ravelr   strictr[   allcloserZ   rE   r4   setrange)r   r   r   r   	dimensionpt1pt2vecsplitsresultindexr   stacksru   ry   	incrementvopolyindex_originaloriginalrecons                         r,   resampler   z  sg   > 6]Fxx

3H
8>>aHNN+?;<<q!I 1a4.C
1a4.C
)CWWT]]3'&0188BF FE77Dbii

q(89:F 6"I%	AI&..I?$IIeai #i.C

''
 EAI&..I?AEAIy9: 	d1fVen,-.ZZ-a088ANLL#d)U+288<~MTTV ::: $'x~t#<%{{8A;a9999{{8B<r;;;; $= ;;t//		%!)0DEvfu~VVVVA #F nnV$%F
zzzz&*F6!9,=DIII u%::u:VAY///u:U3x=%9!::::ef
6{aayMr.   matrixmergec           	         [         R                  " U SS9n [        R                  " U S5      (       d  [	        S5      eUb3  [
        R                  " U R                  S5      US9R                  S5      n U(       a	  [        XS9n SR                  S	S
[        [        U5      5      -   S-   5      nU[        U 5      -  R                  " U R                  5       6 nU$ )a{  
Convert (n, 2, 2) line segments to an SVG path string.

Parameters
------------
segments : (n, 2, 2) float
  Line segments to convert
digits : int
  Number of digits to include in SVG string
matrix : None or (3, 3) float
  Homogeneous 2D transformation to apply before export

Returns
-----------
path : str
  SVG path string with one line per segment
  IE: 'M 0.1 0.2 L 10 12'
T)copyr~   zonly for (n, 2, 2) segments!rg   )r   rW   zM_ _L_ __z{:0.zf})r   r   r   r   r   r   transform_pointsr!   rz   replacestrintr\   formatr   )r   rU   r   r   baser   s         r,   to_svgr     s    0 xxt,H==:..788 "33W%f

'*
 	 (2 c6CF,<#<t#CDDS]"**HNN,<=FMr.   ){Gz?r   N)
   )gh㈵>)   )F)T)FF)r   NT)#__doc__numpyr    r   r   r   	constantsr   groupingr   r	   intervalr
   typedr   r   r   r   r   r   r   r-   r1   r   rT   rd   ru   rz   rj   r   r4   r   r   r    r.   r,   <module>r      s    . .  .  Q Q Q*(Y *(Z7W7(17?Fw?O7H #	FFF F V	F
 RXXFR#I #w #8H #L?I ?y ? ?D!Y ! !H)i ) )t )Z )-!%
2::wrzz**+8 	qqq q 	ql "&	+++ Y+ 	+
 	+r.   