
    Mi?                     *   S SK r S SKrSSKJrJrJr  SSKJrJ	r	J
r
JrJr  SSKJr   S SKrS rSS	 jrS
 rSS jr " S S5      r    SS\S\\	   S\\   S\S\S\
\R4                     4S jjrS rg! \ a  rSSKJr  \" \5      r SrCNaSrCff = f)    N   )	constantsgroupingutil)	ArrayLikeIntegerNDArrayNumberOptional   )is_ccw)ExceptionWrapperc                    [         R                  " 5       n/ n[        U 5       HC  u  p4UR                  (       a  UR	                  U5        M)  UR                  UR                  US9  ME     U[        R                  " U5      4$ )aS  
Given a set of entity objects generate a networkx.Graph
that represents their vertex nodes.

Parameters
--------------
entities : list
   Objects with 'closed' and 'nodes' attributes

Returns
-------------
graph : networkx.Graph
    Graph where node indexes represent vertices
closed : (n,) int
    Indexes of entities which are 'closed'
)entity_index)	nxGraph	enumerateclosedappendadd_edges_fromnodesnparray)entitiesgraphr   indexentitys        p/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/path/traversal.pyvertex_graphr      sg    " HHJEF"8,==MM%    E B - "((6"""    c                   ^ ^^^^ UUUUU 4S jnUb  UR                   S   S:w  a  SnO.[        U[        R                  " T T S   5         5      nUS-  S-
  n[        R                  " T [        R
                  S9m / m[        R                  " [        T 5      S-   5       Hb  n[        R                  " [        R                  " S5      U-   [        T 5      5      nT U   n	UR                  " U	6 S   n
TR                  U
5        Md     [        R                  " T5      SSU2   m[        T5      S:X  a   US:  a  TS   nTU   R                  5         T$ [        R                  " TTS   5      n[        USS USS 5      nU HT  u  mmU" TT   R                  TT   R                  5      u  pUc  M0  TT   R                  US	9  TT   R                  US	9  MV     [        R                  " T5      mT$ )
a  
Convert a path of vertex indices to a path of entity indices.

Parameters
----------
vertex_path : (n,) int
    Ordered list of vertex indices representing a path
graph : nx.Graph
    Vertex connectivity
entities : (m,) list
    Entity objects
vertices :  (p, dimension) float
    Vertex points in space

Returns
----------
entity_path : (q,) int
    Entity indices which make up vertex_path
c                 
  > U S   US   :X  a  gU S   US   :X  a  gU S   US   :X  a  gU S   US   :X  a  g[         R                  R                  SR                  / SQ5      TTTT   R                  TT   R                  5        g	)
a  
Given two edges, figure out if the first needs to be
 reversed to keep the progression forward.

 [1,0] [1,2] -1  1
 [1,0] [2,1] -1 -1
 [0,1] [1,2]  1  1
 [0,1] [2,1]  1 -1

Parameters
------------
a : (2,) int
b : (2,) int

Returns
------------
a_direction : int
b_direction : int
r   r   r   )r$   r$   )r   r   )r   r$   
)zedges not connected!zvertex path %szentity path: %szentity[a]: %s,zentity[b]: %s)NN)r   logdebugjoinpoints)abeaebr   entity_pathvertex_paths     r   edge_direction-vertex_to_entity_path.<locals>.edge_directionD   s    ( Q41Q4<qTQqT\qTQqT\qTQqT\MM		 ####  r    Nr   r   r   dtyper   r$   )	direction)shaper   r   r   
asanyarrayint64arangelenmodget_edge_datar   unique_orderedreversezip
end_pointsr   )r/   r   r   verticesr0   ccw_direction	ccw_checkivertex_path_posvertex_indexr   r   
round_tripdadbr,   r-   r.   s   ` `            @@@r   vertex_to_entity_pathrI   /   s   *- -^ 8>>!,18BIIk;q>$JKL	"Q!+ --288<KKYYs;'!+,&&1!13{3CD"?3**L9.I<( - ))+6GK
;11NEUO##% ;A7JZ_jn5JB 7 7"9P9PQ>RL  2 .RL  2 .	  ((;'Kr    c           	         [        U 5      u  p#[        R                  " US5      R                  5       n[        R
                  R                  U5      nU H/  n[        U5      S:  a  M  UR                  [        XbX5      5        M1     U$ )a  
Paths are lists of entity indices.
We first generate vertex paths using graph cycle algorithms,
and then convert them to entity paths.

This will also change the ordering of entity.points in place
so a path may be traversed without having to reverse the entity.

Parameters
-------------
entities : (n,) entity objects
    Entity objects
vertices : (m, dimension) float
    Vertex points in space

Returns
-------------
entity_paths : sequence of (n,) int
    Ordered traversals of entities
r#   r   )
r   r   reshapetolistr   cyclescycle_basisr9   r   rI   )r   r@   r   r   entity_pathsvertex_pathsr/   s          r   closed_pathsrQ      sw    , !*ME::fg.557L99((/L ${a1+hYZ $ r    c                    [         R                  " U5      n[        U5      nUS:X  a  [        S5      eUS:X  a)  [         R                  " XS      R	                  XS95      nOl/ n[        U5       HE  u  pgX   R	                  XS9nXdS-
  :  a  UR                  U5        M1  UR                  USS 5        MG     [         R                  " U5      nUR                  S   S:X  a,  [        U5      (       d  [         R                  " USSS2   5      nU$ )a  
Turn a list of entity indices into a path of connected points.

Parameters
-----------
entities : (j,) entity objects
   Objects like 'Line', 'Arc', etc.
vertices: (n, dimension) float
    Vertex points in space.
path : (m,) int
    Indexes of entities
scale : float
    Overall scale of drawing used for
    Number tolerances in certain cases

Returns
-----------
discrete : (p, dimension) float
   Connected points in space that lie on the
   path and can be connected with line segments.
r   zCannot discretize empty path!r   )scaleNr$   r   )r   r6   r9   
ValueErrordiscreter   r   vstackr5   r   ascontiguousarray)	r   r@   pathrS   path_lenrU   rC   	entity_idcurrents	            r   discretize_pathr\      s    . }}X&H4yH1}8991}==q'!2!;!;H!;!RS %dOLA)2282IG\"( - , 99X&~~aAfX&6&6''27Or    c                       \ rS rSrS\4S jr SS\S\S\\R                     4S jjr
S\S\\R                     4S	 jrS
rg)
PathSample   r)   c                 F   [         R                  " U5      U l        [         R                  " U R                  SS9U l        [
        R                  " U R                  5      U l        U R                  [        R                  R                  :  nU R                  R                  5       U l        U R                  U==   U R                  U   R                  S5      -  ss'   U R                  R                  5       U l        [         R                   " U R                  5      U l        g )Nr   axisr#   )r   r   _pointsdiff_vectorsr   row_norm_normsr   tol_pathzerocopy	_unit_vecrK   sumlengthcumsum	_cum_norm)selfr)   nonzeros      r   __init__PathSample.__init__   s    xx'15mmDMM2++	 2 2 7 77++-w4;;w#7#?#?#HHkkoo' 4;;/r    	distancesinclude_originalreturnc                    [         R                  " U R                  U5      n[         R                  " US[	        U R
                  5      S-
  5      n[         R                  " SU R                  5      U   nX-
  nU R
                  U   nU R                  U   nXvUR                  S5      -  -   nU(       a  [         R                  " [         R                  " U R                  S5      U5      ) n	U	R                  5       (       aQ  [         R                  " U[         R                  " U	5      S   5      n
[         R                  " XU R                  U	   SS9nU$ )a  
Return points at the distances along the path requested.

Parameters
----------
distances
  Distances along the path to sample at.
include_original
  Include the original vertices even if they are not
  specified in `distance`. Useful as this will return
  a result with identical area and length, however
  indexes of `distance` will not correspond with result.

Returns
--------
samples : (n, dimension)
  Samples requested.
  `n==len(distances)` if not `include_original`
r   r   r#   g        ra   )r   searchsortedro   clipr9   rk   r   rc   rK   isinanyrq   insert)rp   rt   ru   	positionsoffsets
projectionr4   origin	resampled
uninsertedr   s              r   samplePathSample.sample  s   0 OODNNI>	GGIq#dnn*=*AB	))At~~.y9(
NN9-	i( **<*<W*EEF	 ''"))DNNC"@*MMJ~~	2::j3I!3LMIIiZ8PWXY	r    distancec                    [         R                  " U R                  U5      nXR                  US-
     -
  nU[        R                  R
                  :  a  U R                  SUS-    nO[        R                  " [         R                  " U R                  [         R                  " S5      U-      SS9R                  S5      5      nXS-  nU R                  U   U-   n[         R                  " U R                  SUS-    U45      n[        R                  " [         R                  " USS95      R                  5       U-
  [        R                  R
                  :  d   eU$ )z
Return a truncated version of the path.
Only one vertex (at the endpoint) will be added.

Parameters
----------
distance
  Distance along the path to truncate at.

Returns
----------
path
  Path clipped to `distance` requested.
r   Nr   r   ra   r$   )r   rx   ro   r   rh   mergerc   r   unitizerd   r8   rK   rV   rf   rl   )rp   r   positionoffset	truncatedvectorendpoints          r   truncatePathSample.truncate>  s    ??4>>8<NN8a<88I&&,,,^x!|4I\\RYYq\H%<=AFNNrRF F||H-6H		4<<(Q,#?"JKIMM"'')!4599;hF$$% 	% % r    )ro   rg   rc   rk   re   rm   N)F)__name__
__module____qualname____firstlineno__r   rr   boolr	   r   float64r   r
   r   __static_attributes__ r    r   r^   r^      sW    0y 0$ >C0"06:0		0d GBJJ,? r    r^   r)   countstep
step_roundru   rv   c                 
   [         R                  " U [         R                  S9n Ub  Ub  [        S5      eUc  Uc  [        S5      e[	        U 5      nUbI  U(       aB  X%R
                  :  a  U SS/   $ [        [         R                  " UR
                  U-  5      5      nUb#  [         R                  " SUR
                  U5      nO%Ub"  [         R                  " SUR
                  U5      nUR                  WUS9n[        R                  R                  (       al  [        R                  " U SS/   USS/   -
  5      nUS   [        R                   R"                  :  d   eUb#  US   [        R                   R"                  :  d   eU$ )am  
Given a path along (n,d) points, resample them such that the
distance traversed along the path is constant in between each
of the resampled points. Note that this can produce clipping at
corners, as the original vertices are NOT guaranteed to be in the
new, resampled path.

ONLY ONE of count or step can be specified
Result can be uniformly distributed (np.linspace) by specifying count
Result can have a specific distance (np.arange) by specifying step


Parameters
----------
points:   (n, d) float
  Points in space
count : int,
  Number of points to sample evenly (aka np.linspace)
step : float
  Distance each step should take along the path (aka np.arange)
step_round
  Alter `step` to the nearest integer division of overall length.
include_original
  Include the exact original points in the output.

Returns
----------
resampled : (j,d) float
    Points on the path
r2   z#Only step OR count can be specifiedz&Either step or count must be specifiedr   r$   )ru   r   )r   r   r   rT   r^   rm   intceillinspacer8   r   r   tolstrictr   rf   rh   r   )	r)   r   r   r   ru   samplersamplesr   checks	            r   resample_pathr   `  sO   J XXfBJJ/F 0>??DLABB GJ>>!1b'?"BGGGNNT123++a7		))Aw~~t4w9IJI}}faWo	1b'0BBCQx),,222228i0066666r    c                 8   [        U 5      n/ nU R                  nU R                  nU R                  nU R                  n[        U R                  5       GH  u  px[        Xh   R                  5       5      n	U	R                  U5        / n
/ nU	 HR  nX<   nU
R                  [        R                  " [        U5      5      [        U5      -   5        UR                  U5        MT     [        R                  " U R                   5      nX~S'   [        R"                  " [        U
5      S-
  /5      nU R$                     UR                  U" [        R                  " U R&                  U   5      [        R                  " U R(                  5      US95        US   R$                  R+                  U
XY   U	 Vs/ s H  nUU   PM
     snUS.5        US   R$                  R-                  5         SSS5        GM     [        R"                  " U5      $ s  snf ! , (       d  f       GM  = f)a   
Split a Path2D into multiple Path2D objects where each
one has exactly one root curve.

Parameters
--------------
path : trimesh.path.Path2D
  Input geometry

Returns
-------------
split : list of trimesh.path.Path2D
  Original geometry as separate paths
split_2Dr   )r   r@   metadatar$   )pathspolygons_closedrU   rootN)typer   rU   r   enclosure_directedr   r   listkeysr   r   r8   r9   extendrj   deepcopyr   r   _cacher   r@   updateid_set)rX   Path2Dsplitr   rU   r   r   
root_indexr   	connected	new_pathsnew_entitiesr   r   r   new_rootcs                    r   r   r     s     $ZF E JJE}}H**O00%dii0
+16689	 	ELERYYs5z2S5FFG&  ==/)88S^a/01 [[LL!]]4==+FG!]]4==9% "I##&'6'A6? @i!i @$	 "I##%) [1 1\ 88E? !A [s   4A7H	+H:'H	H		
H	)N)g      ?)NNTF)rj   numpyr    r   r   r   typedr   r   r	   r
   r   r   networkxr   BaseExceptionE
exceptionsr   r   rI   rQ   r\   r^   r   r   r   r   r   r    r   <module>r      s      ( ( A A #8k\$N3lc cP  $!"@@G@ 6
@ 	@
 @ RZZ@FJq   .	!	Bs   A4 4B:BB