vamtoolbox.voxelize#

Module Contents#

Classes#

Bounds

BodyMesh

Voxelizer

ShaderProgram

OpenGLSlicer

Functions#

orthoMatrix(left, right, bottom, top, zNear, zFar, dtype)

Return the following matrix

translationMatrix(direction, dtype)

Return matrix to translate by direction vector.

voxelizeTarget(input_path, resolution[, bodies, ...])

Takes a mesh of surface points from the input .stl file, voxelizes the mesh,

rotate(mesh, rot_angles)

Rotates mesh before voxelization

pad_target_to_square(input_voxel_array[, xy_side_length])

Places input array inside a square array (nx,ny,nz) where nx = ny

rotate_mesh(mesh, rot_angles)

Rotates mesh before voxelization

Attributes#

EPSILON

vox

vamtoolbox.voxelize.EPSILON = 0.0001#
vamtoolbox.voxelize.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 |

vamtoolbox.voxelize.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 |
class vamtoolbox.voxelize.Bounds#
xmin#
xmax#
ymin#
ymax#
zmin#
zmax#
length_x#
length_y#
length_z#
class vamtoolbox.voxelize.BodyMesh(mesh: trimesh.Trimesh)#
class vamtoolbox.voxelize.Voxelizer#
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.

Parameters:

stl_struct (dict) –

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’}

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)#
Parameters:
  • body_name (str) – name of the mesh to be voxelized

  • layer_thickness (float) – thickness of each slice in voxelized array in same units as the mesh file

  • voxel_value (float) –

    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)

  • store_voxel_array (bool, optional) – store the voxel array in the Voxelizer object. Retrieve with voxelizer_object.voxel_arrays[body_name]

  • slice_save_path (str, optional) – file path to directory in which to save .png images of each slice

Returns:

voxel_array – voxelized array of the selected mesh

Return type:

np.ndarray

_updateBounds()#

Identify max and min bounds of every mesh and set global bounding box size from these

class vamtoolbox.voxelize.ShaderProgram#
_glsl_vert = Multiline-String#
Show Value
"""
    #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);
    }
    """
_glsl_frag = Multiline-String#
Show Value
"""
    #version 330 core
    out vec4 FragColor;

    void main()
    {
        FragColor = vec4(1.0);
    }
    """
_compileShaders()#
_compileProgram()#
use()#
delete()#
setInt(name, value)#
setMat4(name, arr)#
get_uniform_location(name)#
class vamtoolbox.voxelize.OpenGLSlicer#
begin()#

Begin the pyglet window which will render each slice

quit()#

Close the pyglet slice rendering window

_drawWindow()#

Draw slice to window

slice(bounds: Bounds, mesh: BodyMesh, layer_thickness: float, square_xy: bool = True, slice_save_path: str = None, value=1.0, dtype: str = 'uint8')#
Parameters:
  • bounds (Bounds) –

  • mesh (BodyMesh) – mesh object to be voxelized

  • layer_thickness (float) – thickness of each slice in voxelized array in same units as the mesh file

  • 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)

  • slice_save_path (str, optional) – file path to directory in which to save .png images of each slice

  • value (np.uint8, optional) –

    value of voxels in voxelized array

    Only homogeneous voxel values are implemented

Returns:

voxel_array – voxelized array of the selected mesh

Return type:

np.ndarray

_makeMasks(body_mesh: BodyMesh)#
prepareSlice(width, height)#
_setModelLocation(translation)#
_draw(translation, body_mesh: BodyMesh)#
_renderSlice(translation, body_mesh: BodyMesh, filename=None)#
vamtoolbox.voxelize.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

Parameters:
  • input_path (str) – path to .stl file

  • resolution (int) – number of layers in height to voxelize the target

  • bodies (str or dict) – 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

  • rot_angles (list, optional) – angles in degrees around (x,y,z) axes which to rotate the target geometry

Returns:

voxels – voxelized target

Return type:

np.ndarray

Examples

>>> voxelizeTarget(")
vamtoolbox.voxelize.rotate(mesh, rot_angles)#

Rotates mesh before voxelization

Parameters:
  • mesh (pyvista mesh object) – input .stl mesh read by pyvista

  • rot_angles (list) – angles in degrees around (x,y,z) axes which to rotate the target geometry

Returns:

mesh – rotated mesh

Return type:

pyvista mesh object

vamtoolbox.voxelize.pad_target_to_square(input_voxel_array, xy_side_length=None)#

Places input array inside a square array (nx,ny,nz) where nx = ny

Parameters:
  • input_voxel_array (ndarray) – target voxel array

  • nR (int or None) – 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))

Returns:

voxels – voxelized target

Return type:

ndarray

vamtoolbox.voxelize.rotate_mesh(mesh, rot_angles)#

Rotates mesh before voxelization

Parameters:
  • mesh (Trimesh mesh object) – input .stl mesh read by Trimesh

  • rot_angles (Nx3 array) – angles in degrees around (x,y,z) axes which to rotate the target geometry

Returns:

mesh – rotated mesh

Return type:

Trimesh mesh object

vamtoolbox.voxelize.vox#