:py:mod:`vamtoolbox.voxelize`
=============================
.. py:module:: vamtoolbox.voxelize
Module Contents
---------------
Classes
~~~~~~~
.. autoapisummary::
vamtoolbox.voxelize.Bounds
vamtoolbox.voxelize.BodyMesh
vamtoolbox.voxelize.Voxelizer
vamtoolbox.voxelize.ShaderProgram
vamtoolbox.voxelize.OpenGLSlicer
Functions
~~~~~~~~~
.. autoapisummary::
vamtoolbox.voxelize.orthoMatrix
vamtoolbox.voxelize.translationMatrix
vamtoolbox.voxelize.voxelizeTarget
vamtoolbox.voxelize.rotate
vamtoolbox.voxelize.pad_target_to_square
vamtoolbox.voxelize.rotate_mesh
Attributes
~~~~~~~~~~
.. autoapisummary::
vamtoolbox.voxelize.EPSILON
vamtoolbox.voxelize.vox
.. py:data:: EPSILON
:value: 0.0001
.. py:function:: orthoMatrix(left, right, bottom, top, zNear, zFar, dtype)
Return the following matrix
| 2 -(right+left) |
| ---------- 0 0 ------------- |
| right-left right-left |
| |
| 2 -(top+bottom) |
| 0 ---------- 0 ------------- |
| top-bottom top-bottom |
| |
| -2 -(zFar+zNear) |
| 0 0 ---------- ------------- |
| zFar-zNear zFar-zNear |
| |
| |
| 0 0 0 1 |
.. py:function:: translationMatrix(direction, dtype)
Return matrix to translate by direction vector.
If direction is [x, y, z], return the following matrix
| 1 0 0 x |
| |
| 0 1 0 y |
| |
| 0 0 1 z |
| |
| 0 0 0 1 |
.. py:class:: Bounds
.. py:attribute:: xmin
.. py:attribute:: xmax
.. py:attribute:: ymin
.. py:attribute:: ymax
.. py:attribute:: zmin
.. py:attribute:: zmax
.. py:attribute:: length_x
.. py:attribute:: length_y
.. py:attribute:: length_z
.. py:class:: BodyMesh(mesh: trimesh.Trimesh)
.. py:class:: Voxelizer
.. py:method:: addMeshes(stl_struct: dict)
Add mesh files to be voxelized. After adding meshes, the global bounding box of all meshes is calculated and used for subsequent voxelization such that all voxel arrays have the same physical bounds.
If a mesh is added in a later call of addMeshes(), the global bounding box is updated. If the new mesh has a larger bounding box, previously voxelized meshes should be re-voxelized.
:param stl_struct: dict with file name, body name pairs for each mesh file. The body name is only an identifier for the user and accepts arbitrary strings.
Example: {'mymesh.stl': 'print_body'}
:type stl_struct: dict
.. py:method:: voxelize(body_name: str, layer_thickness: float, voxel_value: float, voxel_dtype: str = 'uint8', square_xy: bool = True, store_voxel_array: bool = False, slice_save_path: str = None)
:param body_name: name of the mesh to be voxelized
:type body_name: str
:param layer_thickness: thickness of each slice in voxelized array in same units as the mesh file
:type layer_thickness: float
:param voxel_value: value of voxels in voxelized array
Only homogeneous voxel values are implemented
voxel_dtype : str, optional
datatype of voxel array
square_xy : bool, optional
if the resulting voxel array should have equal number of voxels in x and y dimensions (i.e. square in x-y)
:type voxel_value: float
:param store_voxel_array: store the voxel array in the Voxelizer object. Retrieve with voxelizer_object.voxel_arrays[body_name]
:type store_voxel_array: bool, optional
:param slice_save_path: file path to directory in which to save .png images of each slice
:type slice_save_path: str, optional
:returns: **voxel_array** -- voxelized array of the selected mesh
:rtype: np.ndarray
.. py:method:: _updateBounds()
Identify max and min bounds of every mesh and set global bounding box size from these
.. py:class:: ShaderProgram
.. py:attribute:: _glsl_vert
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""
#version 330 core
layout (location = 0) in vec3 vert;
uniform mat4 model;
uniform mat4 proj;
void main() {
gl_Position = proj * model * vec4(vert, 1.0f);
}
"""
.. raw:: html
.. py:attribute:: _glsl_frag
:value: Multiline-String
.. raw:: html
Show Value
.. code-block:: python
"""
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0);
}
"""
.. raw:: html
.. py:method:: _compileShaders()
.. py:method:: _compileProgram()
.. py:method:: use()
.. py:method:: delete()
.. py:method:: setInt(name, value)
.. py:method:: setMat4(name, arr)
.. py:method:: get_uniform_location(name)
.. py:class:: OpenGLSlicer
.. py:method:: begin()
Begin the pyglet window which will render each slice
.. py:method:: quit()
Close the pyglet slice rendering window
.. py:method:: _drawWindow()
Draw slice to window
.. py:method:: slice(bounds: Bounds, mesh: BodyMesh, layer_thickness: float, square_xy: bool = True, slice_save_path: str = None, value=1.0, dtype: str = 'uint8')
:param bounds:
:type bounds: Bounds
:param mesh: mesh object to be voxelized
:type mesh: BodyMesh
:param layer_thickness: thickness of each slice in voxelized array in same units as the mesh file
:type layer_thickness: float
:param square_xy: if the resulting voxel array should have equal number of voxels in x and y dimensions (i.e. square in x-y)
:type square_xy: bool, optional
:param slice_save_path: file path to directory in which to save .png images of each slice
:type slice_save_path: str, optional
:param value: value of voxels in voxelized array
Only homogeneous voxel values are implemented
:type value: np.uint8, optional
:returns: **voxel_array** -- voxelized array of the selected mesh
:rtype: np.ndarray
.. py:method:: _makeMasks(body_mesh: BodyMesh)
.. py:method:: prepareSlice(width, height)
.. py:method:: _setModelLocation(translation)
.. py:method:: _draw(translation, body_mesh: BodyMesh)
.. py:method:: _renderSlice(translation, body_mesh: BodyMesh, filename=None)
.. py:function:: voxelizeTarget(input_path, resolution, bodies='all', rot_angles=[0, 0, 0])
Takes a mesh of surface points from the input .stl file, voxelizes the mesh,
and places the array inside a square array (nx,ny,nz) where nx = ny
:param input_path: path to .stl file
:type input_path: str
:param resolution: number of layers in height to voxelize the target
:type resolution: int
:param bodies: specifier of which bodies in the stl (if multibody) are to be printed, default is 'all' meaning that all mesh bodies will be included in the voxel array
:type bodies: str or dict
:param rot_angles: angles in degrees around (x,y,z) axes which to rotate the
target geometry
:type rot_angles: list, optional
:returns: **voxels** -- voxelized target
:rtype: np.ndarray
.. rubric:: Examples
>>> voxelizeTarget(")
.. py:function:: rotate(mesh, rot_angles)
Rotates mesh before voxelization
:param mesh: input .stl mesh read by pyvista
:type mesh: pyvista mesh object
:param rot_angles: angles in degrees around (x,y,z) axes which to rotate the target geometry
:type rot_angles: list
:returns: **mesh** -- rotated mesh
:rtype: pyvista mesh object
.. py:function:: pad_target_to_square(input_voxel_array, xy_side_length=None)
Places input array inside a square array (nx,ny,nz) where nx = ny
:param input_voxel_array: target voxel array
:type input_voxel_array: ndarray
:param nR: The specified number of voxels of the output array in x and y direction.
If None, nR is determined to be minimium radial distance. nR = np.round(np.sqrt(nX**2 + nY**2))
:type nR: int or None
:returns: **voxels** -- voxelized target
:rtype: ndarray
.. py:function:: rotate_mesh(mesh, rot_angles)
Rotates mesh before voxelization
:param mesh: input .stl mesh read by Trimesh
:type mesh: Trimesh mesh object
:param rot_angles: angles in degrees around (x,y,z) axes which to rotate the target geometry
:type rot_angles: Nx3 array
:returns: **mesh** -- rotated mesh
:rtype: Trimesh mesh object
.. py:data:: vox