
    Mi(                         S r SSKrSSKJr  SSKJr   SSKrSS jrS rS	 rS
 rg! \	 a  r
SSKJr  \" \
5      r Sr
C
N(Sr
C
ff = f)z;
poses.py
-----------

Find stable orientations of meshes.
    N   )points_to_barycentric)diagonal_dot)ExceptionWrapperc           	      *	   U R                   nUc  U R                  n/ n[        U5      U:  a  U[        U5      -
  n[        R                  R                  X[        R                  " S5      -  U5      nU HV  n	[        XR                  -
  UR                  5      n
[        R                  " U
S:  5      (       d  ME  UR                  U	5        MX     [        U5      U:  a  M  0 nU GH  n[        X\5      nUR                  5        Vs/ s H  oR                  U5      S:X  d  M  UPM     nnSn[        U5      S:  a  U[        U R                  5      ::  a  / nU H  nUR!                  U5      S:X  a  M  [#        [%        UR'                  U5      5      5      nUR                  U   S==   UR                  U   S   -  ss'   SUR                  U   S'   UR                  U5        M     UnUS-  n[        U5      S:  a  U[        U R                  5      ::  a  M  UR                  5        H  nUR                  U   S   S:  d  M  UR                  U   nUR                  U   S   n[)        [        R*                  " USS95      nUU;   a  UU   S==   SU-  U-  -  ss'   Mx  SU-  U-  US	.UU'   M     GM     / n/ nU GH  nUU   S   nUU:  d  M  [        R                  " S
5      nSUU   S   -  n[        R,                  " US   * US   S/5      n[        R.                  R1                  U5      S:X  a  [        R,                  " / SQ5      nO"U[        R.                  R1                  U5      -  n[        R2                  " UU5      nU[        R.                  R1                  U5      -  n[        R,                  " UUU/5      USS2SS24'   UR5                  5       nUR7                  U5        UR8                  S   S   * n[        R,                  " SSU/5      USS2S4'   UR                  U5        UR                  U5        GM     [        R,                  " U5      n[        R,                  " U5      n[        R:                  " U* 5      nUU   UU   4$ s  snf )a  
Computes stable orientations of a mesh and their quasi-static probabilities.

This method samples the location of the center of mass from a multivariate
gaussian with the mean at the center of mass, and a covariance
equal to and identity matrix times sigma, over n_samples.

For each sample, it computes the stable resting poses of the mesh on a
a planar workspace and evaluates the probabilities of landing in
each pose if the object is dropped onto the table randomly.

This method returns the 4x4 homogeneous transform matrices that place
the shape against the planar surface with the z-axis pointing upwards
and a list of the probabilities for each pose.

The transforms and probabilities that are returned are sorted, with the
most probable pose first.

Parameters
----------
mesh : trimesh.Trimesh
  The target mesh
com : (3,) float
  Rhe object center of mass. If None, this method
  assumes uniform density and watertightness and
  computes a center of mass explicitly
sigma : float
  Rhe covariance for the multivariate gaussian used
  to sample center of mass locations
n_samples : int
  The number of samples of the center of mass location
threshold : float
  The probability value at which to threshold
  returned stable poses

Returns
-------
transforms : (n, 4, 4) float
  The homogeneous matrices that transform the
  object to rest in a stable pose, with the
  new z-axis pointing upwards from the table
  and the object just touching the table.
probs : (n,) float
  Probability in (0, 1) for each pose
N   r   prob        r   )decimals      ?)r	   normal   g      r   )r   r   r      )convex_hullcenter_masslennprandommultivariate_normaleyer   triangles_centerface_normalsallappend_create_topple_graphnodes	in_degreefaces
out_degreenextiter
successorstuplearoundarraylinalgnormcrosscopyapply_transformboundsargsort)meshr   sigma	n_samples	thresholdcvhsample_coms	remainingcomscdotsnorms_to_probs
sample_comdgnr   n_iters	new_nodesnode	successorr   r	   key
transformsprobstfzxymindss                                  g/var/www/eduai.edurigo.com/storigo/production/storigo_env/lib/python3.13/site-packages/trimesh/poses.pycompute_stable_posesrI      s   ` 

C&& K
k
Y
&K 00	yy,,["&&):KYWA$8$8 8#:J:JKDvvdQh""1%  k
Y
& N "
!#2 HHJ?Jq,,q/Q*>J?%j1nC

O!;I==&!+ bmmD&9!:;	#F+rxx~f/EE+),v&  +  EqLG %j1nC

O!; HHJDxx~f%+))$/xx~f-BIIfq9:.("3'/3?T3II/ !$i$ 6"(+N3' ) "B JE c"6*)B ~c*844A1Q4%1q)*Ayy~~a C'HHY'		q))AABIINN1%%A1a),Brr2A2vJ 
Ab!!QA!Q+Brr1uIb!LL/ 4 *%JHHUOE::ufDdU4[((} @s   R Rc                    U u  p#nUS   US   -
  nUS   US   -
  nUS   US   -
  nUS   US   -
  nUS   US   -
  n	US   US   -
  n
US   US   -
  nUS   US   -
  nUS   US   -
  nXYU-  X-  -
  -  XjU-  X-  -
  -  -   XxU-  X-  -
  -  -   $ )a  
Performs a fast 3D orientation test.

Parameters
----------
plane: (3,3) float, three points in space that define a plane
pd:    (3,)  float, a single point

Returns
-------
result: float, if greater than zero then pd is above the plane through
               the given three points, if less than zero then pd is below
               the given plane, and if equal to zero then pd is on the
               given plane.
r   r   r    )planepdpapbpcadxbdxcdxadybdycdyadzbdzcdzs                 rH   _orient3dfastrZ      s      JBB
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C
Q%"Q%-C 	Sy39$%
sSY&
'	(
sSY&
'	(    c                    U  Vs/ s H)  o"U-
  [         R                  R                  X!-
  5      -  PM+     nn[         R                  " [	        S[        S[         R                  " US   US   5      5      5      5      n[         R                  " [	        S[        S[         R                  " US   US   5      5      5      5      n[         R                  " [	        S[        S[         R                  " US   US   5      5      5      5      nXE-   U-   S-  n S[         R                  -  [         R                  " [         R                  " [         R                  " US-  5      [         R                  " Xt-
  S-  5      -  [         R                  " Xu-
  S-  5      -  [         R                  " Xv-
  S-  5      -  5      5      -  $ s  snf ! [         a    US-   nS[         R                  -  [         R                  " [         R                  " [         R                  " US-  5      [         R                  " Xt-
  S-  5      -  [         R                  " Xu-
  S-  5      -  [         R                  " Xv-
  S-  5      -  5      5      -  s $ f = f)a  
For an object with the given center of mass, compute
the probability that the given tri would be the first to hit the
ground if the object were dropped with a pose chosen uniformly at random.

Parameters
----------
tri: (3,3) float, the vertices of a triangle
cm:  (3,) float, the center of mass of the object

Returns
-------
prob: float, the probability in [0,1] for the given triangle
r   r   r   g       @r   g:0yE>)r   r&   r'   arccosminmaxdotpiarctansqrttanBaseException)tricomvsvabr5   ss           rH   _compute_static_probrn      s    8;	;s!s7biinnQW-
-sB	; 			#aR1r!u!5678A
		#aR1r!u!5678A
		#aR1r!u!5678A	
cA
eeiiFF1q5Mffaeq[)*ffaeq[)* ffaeq[)*		
 
<,  
HeeiiFF1q5Mffaeq[)*ffaeq[)* ffaeq[)*		

s   0F:B(F? ?B8I:9I:c           	      :   [         R                  " 5       n[         R                  " 5       nU R                  nU R                  n/ n[        XE5       H0  u  pxU R                  U   n	UR                  US   US   SU	0/5        M2     UR                  U5        [        U R                  5       H  u  p[        X5      nUR                  XS9  M!     [        U R                  XR                  SS2S4   -
  5      nXSS2S4   U R                  -  -
  n[        U R                  U5      n[         R"                  " [         R$                  " US:  SS95      S   nU H  nUU   nU R&                  U   nU R                  U   nUU    H  nUU   U   S   u  nn[         R(                  " [         R*                  " UU-
  UU-
  5      U5      S:  a  UnUnUnUUUU-   /nUUU-   U/n[-        UU5      S:  d  Mo  [-        UU5      S:  d  M    O   UR/                  UW5        M     U$ )a  
Constructs a toppling digraph for the given convex hull mesh and
center of mass.

Each node n_i in the digraph corresponds to a face f_i of the mesh and is
labelled with the probability that the mesh will land on f_i if dropped
randomly. Not all faces are stable, and node n_i has a directed edge to
node n_j if the object will quasi-statically topple from f_i to f_j if it
lands on f_i initially.

This computation is described in detail in
http://goldberg.berkeley.edu/pubs/eps.pdf.

Parameters
----------
cvh_mesh : trimesh.Trimesh
  Rhe convex hull of the target shape
com : (3,) float
  The 3D location of the target shape's center of mass

Returns
-------
graph : networkx.DiGraph
  Graph representing static probabilities and toppling
  order for the convex hull
r   r   verts)r	   N)axis)nxGraphDiGraphface_adjacencyface_adjacency_edgeszipverticesr   add_edges_from	enumerate	trianglesrn   add_noder   r   r   r   whereanyr   ra   r(   rZ   add_edge)cvh_meshrh   	adj_graphtopple_graph
face_pairsedgesgraph_edgesfperp   irg   r	   
proj_dists	proj_comsbarysunstable_face_indicesfiproj_comcentroidr'   tfiv1v2tmpplane1plane2s                              rH   r   r      s    6 
I::<L ((J))EKZ'!!!$BqE2a57E*:;< ( [) H../#C-a+ 0
 h33S;M;MaQRd;S5STJD)H,A,AAAI!("4"4i@EHHRVVEAIA%>?B $R=,,R0$$R(R=Cr]3'0FBvvbhhrH}b8m<dCaGBI.FT	2.Ffh/14!&(3q8 ! 	b#&' $* r[   )Nr
   r   r
   )__doc__numpyr   r{   r   utilr   networkxrr   rf   E
exceptionsr   rI   rZ   rn   r   rK   r[   rH   <module>r      sX     , E)PD2
jJO   -	!	Bs   ' AA  A