
    l0j(                         d Z ddlZddlmZ 	 ddlZn## e$ rZddl	m
Z
  e
e          ZY dZ[ndZ[ww xY wddZd Zd	 Zd
 ZdS )z;
poses.py
-----------

Find stable orientations of meshes.
    N   )points_to_barycentric)ExceptionWrapper        c           	      <	   | j         }|| j        }g }t          |          |k     r|t          |          z
  }t          j                            ||t          j        d          z  |          }|D ]R}	t          j        d|	|j        z
  |j	                  }
t          j
        |
dk               r|                    |	           St          |          |k     i }|D ]}t          ||          fd                                D             }d}t          |          dk    r|t          | j                  k    rg }|D ]}                    |          dk    rt!          t#                              |                              }j        |         dxx         j        |         d         z  cc<   dj        |         d<   |                    |           |}|dz  }t          |          dk    r|t          | j                  k    Ӊ                                D ]}j        |         d         dk    rr|j	        |         }j        |         d         }t'          t          j        |d	                    }||v r||         dxx         d
|z  |z  z  cc<   }d
|z  |z  |d||<   g }g }|D ]}||         d         }||k    rt          j        d          }d||         d         z  }t          j        |d          |d         dg          }t          j                            |          dk    rt          j        g d          }n"|t          j                            |          z  }t          j        ||          }|t          j                            |          z  }t          j        |||g          |ddddf<   |                                }|                    |           |j        d         d          }t          j        dd|g          |dddf<   |                    |           |                    |           t          j        |          }t          j        |          }t          j        |           }||         ||         fS )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   ij,ij->ir   c                 F    g | ]}                     |          d k    |S )r   )	in_degree).0ndgs     P/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/poses.py
<listcomp>z(compute_stable_poses.<locals>.<listcomp>]   s-    ???q",,q//Q*>*>*>*>*>    probr   r   )decimals      ?)r   normal   g      r   )r   r   r      )convex_hullcenter_masslennprandommultivariate_normaleyeeinsumtriangles_centerface_normalsallappend_create_topple_graphnodes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_comr%   n_iters	new_nodesnode	successorr   r   key
transformsprobstfzxymindsr   s                                @r   compute_stable_posesrN      s   ` 
C& K
k

Y
&
&K 0 00	y,,[%"&)):KYWW 	& 	&A9ZS-A)A3CSTTDvdQh &""1%%% k

Y
&
& N "  
!#z22 @???BHHJJ???%jj1nnC
OO!;!;I , ,==&&!++ bmmD&9&9!:!:;;	#F+++rx~f/EE+++),v&  ++++EqLG %jj1nnC
OO!;!; HHJJ 	 	Dx~f%++)$/x~f-BIfq999::.(("3'///3?T3II//// !$i$ 6"(+ +N3'	 JE   c"6*)B ~c*844A1Q4%1q)**Ay~~a  C''HYYY''	q)))AABINN1%%%A1a),,Brr2A2vJ 

Ab!!!!QA!Q++Brr1uIb!!!LL *%%JHUOOE:ufDdU4[((r   c                    | \  }}}|d         |d         z
  }|d         |d         z
  }|d         |d         z
  }|d         |d         z
  }|d         |d         z
  }	|d         |d         z
  }
|d         |d         z
  }|d         |d         z
  }|d         |d         z
  }||	|z  ||
z  z
  z  ||
|z  ||z  z
  z  z   |||z  ||	z  z
  z  z   S )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                 r   _orient3dfastr_      s      JBB
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 	sSy39$%
sS3Y&
'	(
sS3Y&
'	(r   c                    fd| D             }t          j        t          dt          dt          j        |d         |d                                                 }t          j        t          dt          dt          j        |d         |d                                                 }t          j        t          dt          dt          j        |d         |d                                                 }||z   |z   dz  }	 dt           j        z  t          j        t          j        t          j        |dz            t          j        ||z
  dz            z  t          j        ||z
  dz            z  t          j        ||z
  dz            z                      z  S # t          $ r |dz   }dt           j        z  t          j        t          j        t          j        |dz            t          j        ||z
  dz            z  t          j        ||z
  dz            z  t          j        ||z
  dz            z                      z  cY S w xY w)	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
    c                 `    g | ]*}|z
  t           j                            |z
            z  +S rP   )r   r.   r/   )r   vcoms     r   r   z(_compute_static_prob.<locals>.<listcomp>   s4    	;	;	;!1s7binnQW--
-	;	;	;r   r   r   r   g       @r   g:0yE>)
r   arccosminmaxdotpiarctansqrttanBaseException)trirc   svabr=   ss    `     r   _compute_static_probrs      s4    
<	;	;	;s	;	;	;B 		#aR1r!u!5!5667788A
	#aR1r!u!5!5667788A
	#aR1r!u!5!5667788A	
QcA
eiF1q5MMfa!eq[))*fa!eq[))* fa!eq[))*  		
  
 
 
HeiF1q5MMfa!eq[))*fa!eq[))* fa!eq[))*  		
 	
 	

s   	BF% %B+IIc           	      v   t          j                    }t          j                    }| j        }| j        }g }t          ||          D ]8\  }}| j        |         }	|                    |d         |d         d|	ig           9|                    |           t          | j
                  D ],\  }
}t          ||          }|                    |
|           -t          j        d| j        || j
        dddf         z
            }|t          j        d|| j                  z
  }t!          | j
        |          }t          j        t          j        |dk     d                    d         }|D ]}||         }| j        |         }| j        |         }||         D ]}||         |         d         \  }}t          j        t          j        ||z
  ||z
            |          dk     r|}|}|}||||z   g}|||z   |g}t-          ||          dk    rt-          ||          dk    r n|                    ||           |S )	aW  
    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   r	   Nzi,ij->ij)axis)nxGraphDiGraphface_adjacencyface_adjacency_edgeszipverticesr#   add_edges_from	enumerate	trianglesrs   add_noder   r   r!   r   whereanyr    rh   r0   r_   add_edge)cvh_meshrc   	adj_graphtopple_graph
face_pairsedgesgraph_edgesfperu   irn   r   
proj_dists	proj_comsbarysunstable_face_indicesfiproj_comcentroidr/   tfiv1v2tmpplane1plane2s                              r   r$   r$      su   6 

I:<<L (J)EKZ'' = =A!!$BqE2a57E*:;<<<<[))) H.// , ,3#C--ad++++ H)31CAAAqD1I+I J bi
J8MNNNI!("4i@@EHRVEAIA%>%>%>??B $ ' 'R=,R0$R(R= 	 	Cr]3'0FBvbhrH}b8m<<dCCaGGBI.FT	2.Ffh//144!&(33q88b#&&&&r   )Nr   r   r   )__doc__numpyr   r   r   networkxrw   rm   E
exceptionsr   rN   r_   rs   r$   rP   r   r   <module>r      s         , , , , , ,    -,,,,,		!		BBBBBBE) E) E) E)P  D2
 2
 2
jL L L L Ls    3.3