Skip to content

Voxelization

0. Initialization

Importing all necessary libraries and specifying the inputs

import os
import topogenesis as tg
import pyvista as pv
import trimesh as tm
import numpy as np
#setting the specified voxel size
vs = 16.20
unit = [vs, vs, vs]
mesh_path = os.path.relpath('../data/optional_envelope.obj')

1. Input Mesh

# load the mesh from file
mesh = tm.load(mesh_path)
# Check if the mesh is watertight
print(mesh.is_watertight)
# Visualize the mesh using pyvista plotter

# initiating the plotter
pv.set_plot_theme("document")
p = pv.Plotter(notebook=True)

# convert mesh to pv_mesh
faces = np.pad(mesh.faces, ((0, 0),(1,0)), 'constant', constant_values=3)
pv_mesh = pv.PolyData(mesh.vertices, faces)
# adding the base mesh: light blue
p.add_mesh(pv_mesh, color='#abd8ff')

# plotting
cpos = [(785.8704805788776, 708.4540755788776, 741.8613927288776),
 (65.08283250000001, -12.333572500000002, 21.07374465),
 (0.0, 0.0, 1.0)]
p.camera_position = cpos
p.window_size = 2000, 2000
p.show(use_ipyvtk=True)
# p.screenshot("lattice_1")
print(p.camera_position)

2. Voxelize the Mesh

# initialize the base lattice
base_lattice = tg.lattice(mesh.bounds, unit=unit, default_value=1, dtype=int)

# check which voxel centroids is inside the mesh
interior_condition = mesh.contains(base_lattice.centroids)

# reshape the interior condition to the shape of the base_lattice
interior_array = interior_condition.reshape(base_lattice.shape)

# convert the interior array into a lattice
interior_lattice = tg.to_lattice(interior_array, base_lattice.minbound, base_lattice.unit)
# Visualize the voxelization using pyvista plotter

# initiating the plotter
p = pv.Plotter(notebook=True)

# fast visualization of the lattice
interior_lattice.fast_vis(p)

# convert mesh to pv_mesh
faces = np.pad(mesh.faces, ((0, 0),(1,0)), 'constant', constant_values=3)
pv_mesh = pv.PolyData(mesh.vertices, faces)
# adding the base mesh: light blue
p.add_mesh(pv_mesh, color='#abd8ff')

# plotting
cpos = [(785.8704805788776, 708.4540755788776, 741.8613927288776),
 (65.08283250000001, -12.333572500000002, 21.07374465),
 (0.0, 0.0, 1.0)]
p.camera_position = cpos
p.window_size = 2000, 2000
p.show(use_ipyvtk=True)
# p.screenshot("lattice_2_1620")
print(p.camera_position)
# Visualize the voxelization using pyvista plotter

# initiating the plotter
p = pv.Plotter(notebook=True)

# fast visualization of the lattice
interior_lattice.fast_vis(p)

# plotting
cpos = [(785.8704805788776, 708.4540755788776, 741.8613927288776),
 (65.08283250000001, -12.333572500000002, 21.07374465),
 (0.0, 0.0, 1.0)]
p.camera_position = cpos
p.window_size = 2000, 2000
p.show(use_ipyvtk=True)
# p.screenshot("lattice_3_1620")
print(p.camera_position)

3. Saving the lattice to CSV

csv_path = os.path.relpath('../data/lattice972.csv')
interior_lattice.to_csv(csv_path)

Credits

__author__ = "Shervin Azadi"
__license__ = "MIT"
__version__ = "1.0"
__url__ = "https://github.com/shervinazadi/spatial_computing_workshops"
__summary__ = "Spatial Computing Design Studio Workshop on Voxelization"