
    l0jQ                     h   d dl ZddlmZmZmZmZmZmZ ddl	m
Z
mZ 	 d dlmZ d dlmZ n8# e$ r0ZddlmZ  ej        e          Z ej        e          ZY dZ[ndZ[ww xY w	 d dlmZ n# e$ r eZY nw xY w ej        ej        d	z  
          Zdej        _        ddZddZddZd Zd Zd ZdS )    N   )convexgeometrygroupingnspheretransformationsutil)lognow)optimize)
ConvexHull)
exceptions)
QhullError   )thetaFQbBc                    t          | |          }|j        |j                 }|j        |j                 }|dddf         |dddf         z
  }t	          j        t	          j        |dz  ddg                    }|dk    }||         ||                             d          z  }t	          j        |          dd	gz  }t	          j        ||j	                  }	t	          j        ||j	                  }
t	          j
        |	                    d
          |
                    d
          |	                    d
          |
                    d
          f          }t	          j        |                    d          d
                              d          }t	          j        |d
          }|                                }||         }||         dd          |dz  z
  }t	          j        ||         ddd          }t#          j        ||          }|d         |d         k    r%t	          j        t&          |          }|ddd         }||fS )a  
    Find an oriented bounding box for an array of 2D points.

    Details on qhull options:
      http://www.qhull.org/html/qh-quick.htm#options

    Parameters
    ----------
    points : (n,2) float
      Points in 2D.

    Returns
    ----------
    transform : (3,3) float
      Homogeneous 2D transformation matrix to move the
      input points so that the axis aligned bounding box
      is CENTERED AT THE ORIGIN.
    rectangle : (2,) float
       Size of extents once input points are transformed
       by transform
    )qhull_optionsNr   r   r   绽|=r               ?axis)r   r   r   r   r         ?r   )r   points	simplicesverticesnpsqrtdotreshapefliplrTcolumn_stackminmaxdiffprodargminarctan2r   planar_matrix_flip)r   r   r   
hull_edgeshull_pointsedge_vectors	edge_normedge_nonzeroperp_vectorsxyboundsextentsareaarea_min	rectangleoffsetr   	transforms                      Q/home/wildlama/miniconda3/envs/lam/lib/python3.11/site-packages/trimesh/bounds.pyoriented_bounds_2Dr@      s$   2 m<<<F v/0J-0K aaad#jA&66L|QA7788Iu$L-	,0G0O0OPW0X0XXL 9\**dC[8L 	|[]++A
|[]++A _aeeemmQUUU]]AEEqEMM155VW5==YZZF gfnnZ00q999AA'JJG77###D{{}}H !I Xrr""i#o6FJX.ttt45E-fe<<I
 |il""F5),,	dddO	i    T-q=c           	         fd}	 t          | d          r| j        }nt          j        |           rst	          j        |           }t          j        |d          rt          |          S t          j        |d          rt          j        |d          }nt          d          t          d          nz# t          $ rm t          | d	          r%| j                            t          j                  }n*t          j        |           rt	          j        |           }n  ||          cY S w xY w|j        }|j        j        }	|j        }
|j        }|ot          j        t          j        |                    }t)          j        ||          d         }d ||         D             }t          j        ||                   }nt/          j        |g d          g}|g}t3                      }d
}t          j        }t7          ||          D ]\  }}t	          j        ||          dk    }|                                s4|
t	          j        ||	                   }t	          j        |d
dd
df         |j                  j        d
d
d
df         }|d
d
d
df         |         }|d
d
dd
d
f         |d
d
dd
d
f         z
  }t	          j        t	          j        |dz  ddg                    }|dk    }||         ||                              d          z  }t	          j!        |          ddgz  }t	          j        ||d
d
dd
df         j                  }t	          j        ||d
d
dd
df         j                  }|"                    d          |#                    d          z
  |"                    d          |#                    d          z
  z  #                                }|t	          j$        |d
d
df                   z  }||k     r|}|}t	          j%        |t	          j&        tO          |                    f          j        } t	          j        ||           j        d
d
d
df         }t	          j$        |d
d
df                   }!t          |d
d
d
df                   \  }"}#t	          j(        |#|!          }$d|"d
ddf<   tS          j*        |"          }%t	          j        |%|          }&tS          j+        ||&          }'|'#                    d          t	          j$        |'d          dz  z   }(|( |&d
ddf<   |r|$,                                })t	          j-        d          }*t	          j-        d          |)          |*d
dd
df<   t	          j.        t          j/        0                    |*d
dd
df                   d          s=t	          j        |*d
dd
df         t	          j-        d                     |*d
dd
df<   t	          j        |*|&          }&|$|)         }$tc          j2        dtO          |          t3                      |z
             |&|$fS )a  
    Find the oriented bounding box for a Trimesh

    Parameters
    ----------
    obj : trimesh.Trimesh, (n, 2) float, or (n, 3) float
       Mesh object or points in 2D or 3D space
    angle_digits : int
       How much angular precision do we want on our result.
       Even with less precision the returned extents will cover
       the mesh albeit with larger than minimal volume, and may
       experience substantial speedups.
    ordered : bool
      Return a consistent order for bounds
    normal : None or (3,) float
      Override search for normal on 3D meshes.
    coplanar_tol : float
      If a convex hull fails and we are checking to see if the
      points are coplanar this is the maximum deviation from
      a plane where the points will be considered coplanar.

    Returns
    ----------
    to_origin : (4,4) float
      Transformation matrix which will move the center of the
      bounding box of the input mesh to the origin.
    extents: (3,) float
      The extents of the mesh once transformed with to_origin
    c                 r   t          j        | d          }| |z
  }t           j                            |d          \  }}}t          j        ||j                  }t          j        t          j        |dddf                   k              rt          d          t          j	        d          }||dd	dd	f<   t          j        ||           |dd	d	f<   t          |ddddf                   \  }}t          j        |d
          }	t          j        t          j        |          |          }
|
|	fS )a  
        Find an oriented bounding box for an array of coplanar 3D points.

        Parameters
        ----------
        points : (n, 3) float
          Points in 3D that occupy a 2D subspace.

        Returns
        ----------
        to_origin : (4, 4) float
          Transformation matrix which will move the center of the
          bounding box of the input mesh to the origin.
        extents : (3,) float
          The extents of the mesh once transformed with to_origin
        r   r   F)full_matricesNr   zPoints must be coplanar              )r!   meanlinalgsvdmatmulr&   anyabs
ValueErroreyer@   appendr   planar_matrix_to_3D)r   points_meanpoints_demeaned_vh	points_2dto_2dto_origin_2d
extents_2dr9   	to_origincoplanar_tols              r?   oriented_bounds_coplanarz1oriented_bounds.<locals>.oriented_bounds_coplanar   s1   $ gf1--- ;.9===FF1bIort44	6"&111a4))L899 	86777 q		bqb"1"f	"k222bqb!e $6i2A26F#G#G j)J,,IoA,OOQVWW	'!!rA   convex_hullr   r   rG   F)repairzPoints are not (n,3) or (n,2)!z9Oriented bounds must be passed a mesh or a set of points!r    N)digitsr   c                 4    g | ]}t          j        | j        S  )r   spherical_matrixr&   ).0ss     r?   
<listcomp>z#oriented_bounds.<locals>.<listcomp>   s3     
 
 
 ,a02
 
 
rA   )r   r   r   g|۽rG   r   r   r   r   r   r   r   rH   r   rF   z,oriented_bounds checked %d vectors in %0.4fs)3hasattrr^   r	   is_sequencer!   
asanyarrayis_shaper@   r   rO   r   r    viewndarrayface_adjacencyr&   face_adjacency_edgesface_normalsvector_to_sphericalvector_hemispherer   unique_rowsspherical_to_vectorr   align_vectorsr   infzipr#   rM   bitwise_xorr"   r$   r%   r)   r(   ptpr'   oneslenrQ   r   rR   transform_pointsargsortrP   iscloserJ   detr
   debug)+objangle_digitsorderednormalr\   r]   hullr   r    hull_adj	hull_edgehull_normalsspherical_coordsspherical_uniquematricesnormalsticmin_2D
min_volumeto_2Dsideedges	projected	edge_vertr2   r3   r4   r5   r6   r7   r:   volume	vert_onesheightrotation_2Dboxmin_extents
rotation_Zr[   transformed
box_centerorderflips+       `                                      r?   oriented_boundsr   j   s   >$" $" $" $" $"L 0 3&& 	Z ?DDc"" 
	Z]3''F}VW-- C)&111vw// C)&??? !ABBBXYYY 	0 	0 	0 3
## 	\&&rz22FFc"" 	]3''FF''/////	0 }H"$H)I$L ~  3D4J<4X4XYY $/0@VVVWXY
 
%&67
 
 
 *+;<L+MNN *6999==>(
%%CFJ Wh// + +
 vlF++f4xxzz 	 ".$x.9: F5!RaR=(*5572A2>	aaa!e$U+	 Aqqq)IaaaAAAg,>>GBF<?QF;;<<	 5(#L1Il4K4S4S5
 5
 
 y..$< F<111a!8!4!677F<111a!8!4!677AA.155a5==155a5==3PQVVXX yA/// JJF 273x==+A+A BCCEIvy))+AAArrE2IVIaaadO$$F))AAArrE*:;;K)C((KKA 4[AAJ z6**I "28YGGKa((26+A+F+F+F+LLJ"{Ibqb!e  )##%% vayyq		%((RaR!V z")--RaR!V55s;; 	<6$rr2A2v,
;;D!RaRL F4++	!%(I<c(mmSUUUX[YYYk!!s   A#B7 ,A
B7 7A4D.-D.   MbP?c                    dfd	t          | d          r'| j        dk    r| j        r| j        }n| j        j        }t          j        || j                  }t          j	        | j
        |          }t          j        |dddf                   }t          j        dd|d	z  |dddf                                         z
  g          }t          j        ||          }|ddddf         dz                      d
                                          dz  }||t          j                            |          d}	|	S t'          j        |           t+          j        d          st/          d          t+          j        ddgt          j        t          j        gg|          }
t          | d          r-t          j        |
t+          j        | j                  f          }
t;                      g}t          j        fd|
D                       }|
|                                         }|                     t;                                 dt          j        z  |z  }|d         |z
  |d         |z   f|d
         |z
  |d
         |z   fg}tC          j"        ||d|          }|                     t;                                 tG          j$        dgt          j%        |          R    |d         d          \  }}}|||d}	|	S )aI  
    Find the approximate minimum volume cylinder which contains
    a mesh or a a list of points.

    Samples a hemisphere then uses scipy.optimize to pick the
    final orientation of the cylinder.

    A nice discussion about better ways to implement this is here:
    https://www.staff.uni-mainz.de/schoemer/publications/ALGO00.pdf


    Parameters
    ----------
    obj : trimesh.Trimesh, or (n, 3) float
      Mesh object or points in space
    sample_count : int
      How densely should we sample the hemisphere.
      Angular spacing is 180 degrees / this number

    Returns
    ----------
    result : dict
      With keys:
        'radius'    : float, radius of cylinder
        'height'    : float, height of cylinder
        'transform' : (4,4) float, transform from the origin
                      to centered cylinder
    Fc                 B   t          j        | ddi}t          j        
|          }t          j        |dddf                   }	 t          j        |ddddf                   \  }}n# t          $ r t          j        cY S w xY wt          j	        |z  |dz  z  }|rt          j
        ||dddf                                         |dz  z             }t          j        t          j                            |          t          j        |                    }	|	||fS |S )a  
        Takes spherical coordinates and calculates the volume
        of a cylinder along that vector

        Parameters
        ---------
        spherical : (2,) float
           Theta and phi
        return_data : bool
           Flag for returned

        Returns
        --------
        if return_data:
            transform ((4,4) float)
            radius (float)
            height (float)
        else:
            volume (float)
        axesrxyz)matrixNr   r   )r   rd   r|   r!   ry   r   minimum_nsphererO   rv   pirQ   r(   r#   rJ   invtranslation_matrix)	sphericalreturn_datar   r   r   	center_2Dradiusr   	center_3Dr>   r   s             r?   volume_from_anglesz,minimum_cylinder.<locals>.volume_from_anglese  s0   *  0)I&II#4T%HHH		!!!Q$((	 ' 7	!!!RaR%8H I IIvv 	 	 	6MMM	 619- 	-	)Yqqq!t_-@-@-B-Bfsl-STTI	e$$o&H&S&S I ff,,s   #A, ,BBsymmetryradial)originr   Nr   r   g       @r   r   r   )r   r   r>   r_   z%Input must be reducable to 3D points!principal_inertia_vectorsc                 &    g | ]} |          S rc   rc   )re   ir   s     r?   rg   z$minimum_cylinder.<locals>.<listcomp>  s%    ???!**1--???rA   SLSQP)tolmethodr8   z)Performed search in %f and minimize in %fr6   T)r   )r>   r   r   )F)&rh   r   is_watertightcenter_massr^   r   plane_transformsymmetry_axisr   r|   r    r!   ry   r   r)   r#   sumrJ   r   r   r1   r	   rk   rO   grid_linspacer   vstackrq   r   r   arrayr,   rQ   r   minimizer
   r   r*   )r   sample_count	angle_tolr   r   on_planer   slider   resultsamplesr   volumesbeststepr8   rr>   r   r   s                     @@r?   minimum_cylinderr   G  s$   <% % % % % %R sJ CLH$<$< 	1_FF _0F(s?PQQQ"3CL%HHA''  2FSLHQQQTN$6$6$8$889
 
 ue$$111bqb5/Q&+++337799S@"f29==QVCWCWXX c""D=w'' B@AAA  1a&25"%.!9<HHG s/00 
)d.s/LMMN
 
 55'Ch????w???@@G7>>##$DJJsuu ru9|#DAw~tAw~.a4a40PQFDi	 	 	A JJsuuI9IBGCLLIIII !3 21S6t L L LIvv$&IIFMrA   c                    t          j        | t           j                  } | j        dk    rt	          d          t          j        | d          }t          j        d          }|                     d          |dddf<   ||fS )	aU  
    Convert an axis aligned bounding box to extents and
    transform.

    Parameters
    ------------
    bounds : (2, 3) float
      Axis aligned bounds in space

    Returns
    ------------
    extents : (3,) float
      Extents of the bounding box
    transform : (4, 4) float
      Homogeneous transform moving extents to bounds
    dtyper   rG   zbounds must be (2, 3)r   r   rF   NrG   )r!   rj   float64shaperO   ry   rP   rI   )r8   r9   r>   s      r?   
to_extentsr     s    " ]6444F|v0111fV!$$$Gq		I{{{**Ibqb!eIrA   c                    t          j        | t           j                  } t          j        | d          rt          j        | ddgf          } n$t          j        | d          st          d          t          j        d          \  }}}}}}t          j        ||||||||||||||||||||||||g          	                    d          }| 	                    d          |         }|S )	a  
    Given a pair of axis aligned bounds, return all
    8 corners of the bounding box.

    Parameters
    ----------
    bounds : (2,3) or (2,2) float
      Axis aligned bounds

    Returns
    ----------
    corners : (8,3) float
      Corner vertices of the cube
    r   )r   r   r   r   zbounds must be (2,2) or (2,3)!r   r_   r   )
r!   rj   r   r	   rk   r'   rO   aranger   r$   )	r8   minxminyminzmaxxmaxymaxzcorner_indexcornerss	            r?   r   r     s     ]6444F}VV$$ ;&1a&!122]66** ;9:::)+1&D$dD$81	
 6 gg7 : nnR  .GNrA   c                    t          j        | t           j                  } t          j        |t           j                  }t          |           dk    rt	          d          t          j        |d| j        d         f          st	          d          t          j        || d         k    	                    d          || d         k     	                    d                    }|S )	aD  
    Do an axis aligned bounding box check on a list of points.

    Parameters
    -----------
    bounds : (2, dimension) float
       Axis aligned bounding box
    points : (n, dimension) float
       Points in space

    Returns
    -----------
    points_inside : (n,) bool
      True if points are inside the AABB
    r   r   zbounds must be (2,dimension)!r   r   zbounds shape must match points!r   r   )
r!   rj   r   r{   rO   r	   rk   r   logical_andall)r8   r   points_insides      r?   containsr   '  s    " ]6444F]6444F
6{{a8999="fl1o!677 <:;;; N	&)	  a ((6F1I+=*B*B*B*J*J M rA   )r   )r   TNrB   )r   r   ) numpyr!    r   r   r   r   r   r	   	constantsr
   r   scipyr   scipy.spatialr   BaseExceptionEr   ExceptionWrapperr   r.   r   r/   flags	writeabler@   r   r   r   r   r   rc   rA   r?   <module>r      s       H H H H H H H H H H H H H H H H        	.((((((( . . .,,Q//J*z*1--HHHHHH.(((((((   JJJ 	&%BEAI666 L  L  L  L ^Z" Z" Z" Z"zH H H HV  86 6 6r    s&   + A &AA $A+ +A54A5