
    jM5                         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y)    N   )util)log)	ArrayLikeNumberOptionalUnionc                 <   | 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   F   K   | ]  }t        j                  |        y w)N)nparange).0ss     >/DATA/.local/lib/python3.12/site-packages/trimesh/voxel/ops.py	<genexpr>z$fill_orthographic.<locals>.<genexpr>   s     2EqbiilEs   !indexingijaxisc                    d| f   }|j                         }|    |<   t        j                  || d      }|j                         }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$T{evvm$>*//1!evvm$>~~$&$&
 	
    r      )r#   r   stackmeshgridlogical_notrangelenr   )denser$   filledr   r!   r"   r#   s       @@@r   fill_orthographicr.      s    KKEhh
2E2BTBG NN5!E
 AFaU$	$8 %Mr%   c                    t        j                  | t         j                        } t        j                  | d      st        d      | j                         dz   }t        j                  |||ft              }t        j                  | d      }d|t        |j                        <   t        |      D ]c  }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kD  xr |	d
kD  }|	d
k  ryt        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jt        d|	|	d
z  z
  d
      D ]  }
d||||
   ||
dz      |f<     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   #   sG    ]]>BN==1*++ ""$q(I88Y	95t<DFF>1-M#$D}	 9
q)a-(AC $q!SbSy/41ab>:B((2>>"-.q1A5CCAa%1.QJ1u1a!a%i+23Q3q6CAJ../ , )  q)a-(AC$q#2#qy/412q>:B((2>>"-.q1A5CCA1u1a!a%i+23QAQU+Q./ , )% < __RXXd^,F
aKFMr%   matrixpitch	thresholdc                 f   ddl m} ddlm} |t	        j
                  |       |kD  } 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)skimagerQ   baserS   r   r5   r:   r)   padhasattrrX   marching_cubessizer+   r   warningsubtract)rM   rN   rO   rQ   rS   
rev_matrixrU   funcmeshedr\   r]   normals_valsmeshs                 r   matrix_to_marching_cubesrm   c   s(   2  v&2vT2'J Iy
QJ
 w01--%% MM% EzzQ1F 6{aFG %	V	*0'%% {{8Y%67HHE'JDKr%   c                    t        j                  | t         j                        } t        j                  | d      st        d      | j                  d      dz   }t        j                  t        j                  |      t              }t        j                  t        j                  |dd       |d   dg      }| |z  j                  d      }d	||<   |j                  |      }|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#   rM   
multiplierindexr,   s         r   sparse_to_matrixrv      s      ]]62F==)011JJAJ"EXXbggenD1F27759-uQx;<Jj %%1%-EF5MNN5!ELr%   c                 j   t        j                  | t         j                        } t        j                  |t              }| j	                  d      }| |z
  |z  j                         j                  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   )rN   )r   r5   float64floatr   roundastyper6   rv   rm   r\   )pointsrN   originru   rM   rl   s         r   points_to_marching_cubesr~      s      ]]64FMM%u-E ZZQZFvo&--/66rxx@E e$F $F%8DMMVMKr%   c                 P   ddl m} ddlm} t	        j
                  | t        j                        } |j                         }|j                  t        |             t	        j                  | dt        |j                        f      j                  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$                        j'                  t(              }	t	        j                  |	t        |       df      }
t	        j                  t	        j                   t        |             t        |j                              }t	        j"                  | |z        j'                  t(              }t+        t-        t.        |            }||   }||
z   }t	        j0                  |D cg c]  }t/        |      |v c}      }n$t	        j2                  t        |      t4              }d}|t	        j6                  |      }|j8                  dk(  r|d   j                  t        |       d	      }|j8                  dk(  r-t        |      t        |       k(  r|j                  d
d	      |   } ||||   |      }|S c c}w )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   )
primitivesrR   r0   r&   r2   Nr   r      )r\   r]   face_colors) r   r`   rS   r   r5   rx   Boxapply_scalery   tiler+   r\   rr   r]   repeatr   rz   face_normalsr{   intsetmapr<   rp   onesr:   asarrayndim)centersrN   colorsremove_internal_facesr   rS   bvfbase_normalsr   face_voxel_idxvoxel_coords	voxel_setvoxel_face_coordsneighbor_coordsrJ   	keep_maskr   rl   s                       r   multiboxr      sZ   ,  mmG2::6G 	AMM%,
!S_-.66w?Ac'lA.	//A
#g,*+A299S\*S_<c!''l	KAtG	TTAxx/66s;ww|c'lA->? IIc'l#S\
 xx%077<E<01	 )8+l:HHQAeAhi7QR	GGCF$/	KF#;;!D\((WA(>F;;!Fs7| ; ---3I>KAQy\{KDK Rs   L#c                    ddl }t        j                  | j                  d      | j	                  d      |j                  d      |j	                  d      g      }|j                  d      dz
  }t        t        j                  |d      dz         }|j                  | |z
  j                  t        j                  t        |       t              |      }|j                  ||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#   )rs   r   rp   r   r   r<   ptpCOOr=   r   r+   r:   r@   coords)ar   	operationrs   extremar}   rd   sp_asp_bappliedr   s              r   boolean_sparser   <  s    &  hh1quu!u}aeeemQUUPQU]STG[[a[ 1$Fa(1,-D ::q6znn2773q6+Fd:SD::q6znn2773q6+Fd:SD d#G__W^^,v5FMr%   c                    | j                   }t        |      }g }g }t        t        |            D ]  }t        t        |            t        t        |dz   |            z   }t	        j
                  | |      }t	        j                  |      \  }|d   }	|d   }
|j                  |	|
g       |j                  t        |	|
              | t        |         t	        j                  |t              fS )Nr&   r   r   r   )r#   r+   r*   r<   r   anynonzeroappendslicerp   r   )r   r#   ndimspaddingslicesdimr   r-   r"   pad_left	pad_rights              r   strip_arrayr   c  s    JJEJEGFSZ U3Z 5sQw)>#??4(ZZ'
1:BK	),-eHi01 ! f# 666r%   c                 @   t        j                  |       } | j                  dd dk7  rt        d      t        j                  | t         j
                        }||t        |      z  }|4t        j                  |      }|j                  dk7  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   rp   rx   ry   )r"   rN   r}   r|   s       r   indices_to_pointsr   s  s     mmG$G}}QRD :;;XXgRZZ0F%,v&<<4;<<&Mr%   c                 r    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"   rN   r}   )r   r@   r   r   )rM   rN   r}   r"   r|   s        r   matrix_to_pointsr     s.     oobjj01GweFKFMr%   c                    t        j                  | t         j                        } | j                  | j                  d   dfk7  rt	        d      |4t        j
                  |      }|j                  dk7  rt	        d      | |z  } || |z  } t        j
                  |t         j                        }t        |      }t        j                  |       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)r   r   )
r   rp   rx   r#   r8   r5   ry   rz   r{   r   )r|   rN   r}   r"   s       r   points_to_indicesr     s    $ XXfBJJ/F||Q++9::v&<<4;<<&%]]64F%LEhhv%%c*GNr%   )      ?N)r   )r   NF)NN)numpyr   r   r   	constantsr   typedr   r   r   r	   r.   rL   fill_voxelizationrm   rv   r~   r   r   r   r   r   r   r    r%   r   <module>r      s       6 66:z  
 '*"&HH"#H HV>DJZ $&>> $N7 <&"r%   