
    TiM5                         d dl ZddlmZ ddlmZ ddlmZmZm	Z	m
Z
 d Zd ZeZ	 	 dd	ed
e
eef         de	e         fdZd ZddZddZej        fdZd ZddZddZddZdS )    N   )util)log)	ArrayLikeNumberOptionalUnionc                 B   | j         t          j        t          j        d D             ddid          t          j        |           fd} |d          }t          dt                              D ] }t          j        | ||                    }!|S )	Nc              3   >   K   | ]}t          j        |          V  d S )N)nparange).0ss     K/DATA/AppData/hermes/venv/lib/python3.11/site-packages/trimesh/voxel/ops.py	<genexpr>z$fill_orthographic.<locals>.<genexpr>   s*      22qbill222222    indexingijaxisc                    d| f         }|                                 }|          |<   t          j        || d          }|                                 }d|<   t          j        || d          }t          j        ||k    ||k              S )N.T)r   keepdimsr   )copyr   minmaxlogical_and)r   base_local_indiceslocal_indicesminsmaxsemptyindicesshapes        r   	fill_axisz$fill_orthographic.<locals>.fill_axis   s    $S$Y/*//11$T{evm$>>>*//11!evm$>>>~$&$&
 
 	
r   r      )r$   r   stackmeshgridlogical_notrangelenr   )denser%   filledr   r"   r#   r$   s       @@@r   fill_orthographicr.      s    KEh
22E222BTBB  G N5!!E
 
 
 
 
 
 
 YAFaU$$ 9 9		$88Mr   c                    t          j        | t           j                  } t          j        | d          st          d          |                                 dz   }t          j        |||ft                    }t          j	        | d          }d|t          |j                  <   t          |          D ]}d}t          d|dz
            D ]}g }t          j        |||dd	f         |||ddf                   }t          j        t          j        |                    d         dz   }t!          |          }	|	d
z  dk    o|	d
k    }|	d
k     rt          d|	|	d
z  z
  d
          D ]}
d|||||
         ||
dz            f<   |st          d|dz
            D ]}g }t          j        ||dd	|f         ||dd|f                   }t          j        t          j        |                    d         dz   }t!          |          }	|	d
k     rtt          d|	|	d
z  z
  d
          D ]}
d||||
         ||
dz            |f<   t          j        t          j        |                    }|dz  }|S )z
    Given a sparse surface voxelization, fill in between columns.

    Parameters
    --------------
    sparse_indices: (n, 3) int, location of filled cells

    Returns
    --------------
    filled: (m, 3) int, location of filled cells
    dtyper      zincorrect shaper3   r&   Fr   Nr      )r   
asanyarrayint64r   is_shape
ValueErrorr   zerosbooladdtupleTr*   equalwherer)   r+   column_stack)sparse_indices	max_valuegridvoxels_sparsei
check_dir2jidxeqcr   kr-   s                r   	fill_baserL   #   s    ]>BBBN=11 ,*+++ ""$$q(I8Y	95t<<DF>1--M#$D}		 9 4 4
q)a-(( 	4 	4AC $q!SbSy/41abb>::B(2>"--..q1A5CCAa%1.QJ1uu1a!a%i++ 4 423Q3q6CAJ..//4 	 q)a-(( 		4 		4AC$q#2#qy/4122q>::B(2>"--..q1A5CCA1uu1a!a%i++ 4 423QAQU+Q.//4		4 _RXd^^,,F
aKFMr         ?matrixpitch	thresholdc                 h   ddl m} ddlm} |t	          j        |           |k    } nt	          j        | t                    } t	          j        |           }d}t	          j        ||dd	          }t          |d
          r|j
        }n|j        }t	          j        |          }|j        dk    r|fdz  } ||d|          }t          |          dk    rt          j        d           |\  }	}
d}nt          |          dk    r|\  }	}
}}t	          j        |	||z            }	 ||	|
|          }|S )a&  
    Convert an (n, m, p) matrix into a mesh, using marching_cubes.

    Parameters
    -----------
    matrix : (n, m, p) bool
      Occupancy array
    pitch : float or length-3 tuple of floats, optional
      Voxel spacing in each dimension
    threshold : float or None, optional
      If specified, converts the input into a boolean
      matrix by considering values above `threshold` as True


    Returns
    ----------
    mesh : trimesh.Trimesh
      Mesh generated by meshing voxels using
      the marching cubes algorithm in skimage
    r   )measurer   TrimeshNr0   r&   constant)	pad_widthmodeconstant_valuesmarching_cubes_lewinerr3   g      ?)volumelevelspacingz0using old marching cubes, may not be watertight!r4   )verticesfacesvertex_normals)skimagerR   baserT   r   r5   r:   r)   padhasattrrY   marching_cubessizer+   r   warningsubtract)rN   rO   rP   rR   rT   
rev_matrixrV   funcmeshedr]   r^   normals_valsmeshs                 r   matrix_to_marching_cubesrn   c   s   2  v&&2vT222''J Iy
Q  J
 w011 &-% M%  EzQ1T  F 6{{aFGGG %	V		*0'%% {8Y%677H7HE'JJJDKr   c                    t          j        | t           j                  } t          j        | d          st          d          |                     d          dz   }t          j        t          j        |          t                    }t          j
        t          j        |dd                   |d         dg          }| |z                      d          }d	||<   |                    |          }|S )
a  
    Take a sparse (n,3) list of integer indexes of filled cells,
    turn it into a dense (m,o,p) matrix.

    Parameters
    -----------
    sparse : (n, 3) int
      Index of filled cells

    Returns
    ------------
    dense : (m, o, p) bool
      Matrix of filled cells
    r0   r2   zsparse must be (n,3)!r   r   r&   Nr   T)r   r5   r6   r   r7   r8   r   r9   prodr:   arraysumreshape)sparser$   rN   
multiplierindexr,   s         r   sparse_to_matrixrw      s      ]6222F=)) 20111JJAJ"EXbgennD111F2759--uQx;<<Jj %%1%--EF5MNN5!!ELr   c                 v   t          j        | t           j                  } t          j        |t                    }|                     d          }| |z
  |z                                                      t           j                  }t          |          }t          ||          }|xj
        |z  c_
        |S )a  
    Mesh points by assuming they fill a voxel box, and then
    running marching cubes on them

    Parameters
    ------------
    points : (n, 3) float
      Points in 3D space

    Returns
    -------------
    mesh : trimesh.Trimesh
      Points meshed using marching cubes
    r0   r   r   )rO   )r   r5   float64floatr   roundastyper6   rw   rn   r]   )pointsrO   originrv   rN   rm   s         r   points_to_marching_cubesr      s      ]6444FM%u---E ZZQZFvo&--//66rx@@E e$$F $F%888DMMVMMKr   Fc                    ddl m} ddlm} t	          j        | t          j                  } |                                }|                    t          |                     t	          j
        | dt          |j                  f                              d          }|t	          j
        |j        t          |           df          z  }t	          j
        |j        t          |           df          }|t	          j        t	          j        t          |                     t          |j                  z  t          |j                            dddf         z  }|rt	          j        |j                                      t(                    }	t	          j
        |	t          |           df          }
t	          j        t	          j        t          |                     t          |j                            }t	          j        | |z                                t(                    }t+          t-          t.          |                    ||         }||
z   }t	          j        fd|D                       }n(t	          j        t          |          t4                    }d}|t	          j        |          }|j        dk    r*|d                             t          |           d	
          }|j        dk    r=t          |          t          |           k    r|                    dd	
          |         } ||||         |          }|S )a  
    Return a Trimesh object with a box at every center.

    Doesn't do anything nice or fancy.

    Parameters
    -----------
    centers : (n, 3) float
      Center of boxes that are occupied
    pitch : float
      The edge length of a voxel
    colors : (3,) or (4,) or (n,3) or (n, 4) float
      Color of boxes
    remove_internal_faces : bool
      If True, removes internal faces shared between adjacent boxes

    Returns
    ---------
    rough : Trimesh
      Mesh object representing inputs
    r   )
primitivesrS   r0   r&   r2   Nc                 2    g | ]}t          |          vS  )r<   )r   rJ   	voxel_sets     r   
<listcomp>zmultibox.<locals>.<listcomp>+  s$    QQQAeAhhi7QQQr   r   r      )r]   r^   face_colors) r   ra   rT   r   r5   ry   Boxapply_scalerz   tiler+   r]   rs   r^   repeatr   r{   face_normalsr|   intsetmapr<   rq   onesr:   asarrayndim)centersrO   colorsremove_internal_facesr   rT   bvfbase_normalsr   face_voxel_idxvoxel_coordsvoxel_face_coordsneighbor_coords	keep_maskr   rm   r   s                     @r   multiboxr      s   ,  mG2:666G 	AMM%,,
!S__-..66w??Ac'llA.	/	//A
#g,,*++A29S\\**S__<c!'ll	K	KAAAtG	TTA 0x//66s;;w|c'llA->?? Ic'll##S\\
 
 x%0077<<E<0011	 )8+l:HQQQQQQQRR		GCFF$///	KF##;!D\((WA(>>F;!Fs7|| ; ; ---33I>K7AQy\{KKKDKr   c                    ddl }t          j        |                     d          |                     d          |                    d          |                    d          g          }|                    d          dz
  }t          t          j        |d          dz             }|                    | |z
  j        t          j	        t          |           t                    |          }|                    ||z
  j        t          j	        t          |          t                    |          } |||          }	t          j        |	j                  |z   }
|
S )a  
    Find common rows between two arrays very quickly
    using 3D boolean sparse matrices.

    Parameters
    -----------
    a: (n, d)  int, coordinates in space
    b: (m, d)  int, coordinates in space
    operation: numpy operation function, ie:
                  np.logical_and
                  np.logical_or

    Returns
    -----------
    coords: (q, d) int, coordinates in space
    r   Nr   r&   r   r0   )datar$   )rt   r   rq   r   r   r<   ptpCOOr=   r   r+   r:   r@   coords)ar   	operationrt   extremar~   re   sp_asp_bappliedr   s              r   boolean_sparser   <  s%   & MMM h1quu!u}}aeeemmQUUPQU]]STTG[[a[  1$Fa(((1,--D ::q6zn273q66+F+F+Fd:SSD::q6zn273q66+F+F+Fd:SSD id##G_W^,,v5FMr   c                 8   | j         }t          |          }g }g }t          t          |                    D ]}t          t          |                    t          t          |dz   |                    z   }t	          j        | |          }t	          j        |          \  }|d         }	|d         }
|                    |	|
g           |                    t          |	|
                     | t          |                   t	          j	        |t                    fS )Nr&   r   r   r   )r$   r+   r*   r<   r   anynonzeroappendslicerq   r   )r   r$   ndimspaddingslicesdimr   r-   r#   pad_left	pad_rights              r   strip_arrayr   c  s    JEJJEGFSZZ   2 2U3ZZ  5sQw)>)>#?#??4(((Z''
1:BK	),---eHi001111f# 6 666r   c                 D   t          j        |           } | j        dd         dk    rt          d          t          j        | t           j                  }||t          |          z  }|3t          j        |          }|j        dk    rt          d          ||z  }|S )ak  
    Convert indices of an (n,m,p) matrix into a set of voxel center points.

    Parameters
    ----------
    indices: (q, 3) int, index of voxel matrix (n,m,p)
    pitch: float, what pitch was the voxel matrix computed with
    origin: (3,) float, what is the origin of the voxel matrix

    Returns
    ----------
    points: (q, 3) float, list of points
    r&   Nr3   zshape of indices must be (q, 3)r0   shape of origin must be (3,))r   r5   r$   r8   rq   ry   rz   )r#   rO   r~   r}   s       r   indices_to_pointsr   s  s     mG$$G}QRRD  :;;;XgRZ000F%,,v&&<4;<<<&Mr   c                 v    t          j        t          j        |                     }t          |||          }|S )aS  
    Convert an (n,m,p) matrix into a set of points for each voxel center.

    Parameters
    -----------
    matrix: (n,m,p) bool, voxel matrix
    pitch: float, what pitch was the voxel matrix computed with
    origin: (3,) float, what is the origin of the voxel matrix

    Returns
    ----------
    points: (q, 3) list of points
    )r#   rO   r~   )r   r@   r   r   )rN   rO   r~   r#   r}   s        r   matrix_to_pointsr     s6     obj0011GweFKKKFMr   c                    t          j        | t           j                  } | j        | j        d         dfk    rt	          d          |3t          j        |          }|j        dk    rt	          d          | |z  } || |z  } t          j        |t           j                  }t          |          }t          j        |                               t                    }|S )a  
    Convert center points of an (n,m,p) matrix into its indices.

    Parameters
    ----------
    points : (q, 3) float
      Center points of voxel matrix (n,m,p)
    pitch : float
      What pitch was the voxel matrix computed with
    origin : (3,) float
      What is the origin of the voxel matrix

    Returns
    ----------
    indices : (q, 3) int
      List of indices
    r0   r   r3   zshape of points must be (q, 3)Nr   r   )
r   rq   ry   r$   r8   r5   rz   r{   r|   r   )r}   rO   r~   r#   s       r   points_to_indicesr     s    $ XfBJ///F|Q+++9:::v&&<4;<<<&%]6444F%LLEhv%%c**GNr   )rM   N)rM   )rM   NF)NN)numpyr   r   r   	constantsr   typedr   r   r   r	   r.   rL   fill_voxelizationrn   rw   r   r   r   r   r   r   r   r   r   r   r   <module>r      s                   6 6 6 6 6 6 6 6 6 6 6 6  6: : :z  
 '*"&H HH"#H H H H HV  >   DJ J J JZ $&> $ $ $ $N7 7 7    <   &" " " " " "r   