unstructured grid consists of mixed size cubes shows internal faces
PS > pip list | Select-String vtk
vtk 9.3.0
PS > pip list | Select-String pyvista
pyvista 0.43.5
Hi
when i create unstructured grid with packed 27(==3x3x3) cubes, vtk shows only external faces (green actor) but if I merge 8(==2x2x2) cubes into a 2-length cube, vtk shows internal faces (red actor)
tried to convert all pyvista codes into vtk, but replacing pyvista unstructuredgrid into vtk one was not easy
# noinspection PyUnresolvedReferences
import pyvista
import vtkmodules.util
import vtkmodules.util.numpy_support
import vtkmodules.vtkCommonDataModel
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import vtkPoints
from vtkmodules.vtkCommonDataModel import vtkStructuredGrid
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkDataSetMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
import numpy as np
def cube_3_3_3():
_coords: set[tuple[float, float, float]] = set()
cubes: list[tuple[int, int, int, int]] = list()
coords: list[tuple[float, float, float]] = None
coords_to_index: dict[tuple[float, float, float], int] = dict()
cells: list[list[int]] = list()
dim0 = 3
dim1 = 3
dim2 = 3
for i0 in range(dim0):
for i1 in range(dim1):
for i2 in range(dim2):
cubes.append((float(i0), float(i1), float(i2), 1))
for cube in cubes:
i0, i1, i2, cs = cube
_coords.add((i0, i1, i2))
_coords.add((i0, i1, i2+cs))
_coords.add((i0, i1+cs, i2))
_coords.add((i0, i1+cs, i2+cs))
_coords.add((i0+cs, i1, i2))
_coords.add((i0+cs, i1, i2+1))
_coords.add((i0+cs, i1+cs, i2))
_coords.add((i0+cs, i1+cs, i2+cs))
coords = list(_coords)
for idx, key in enumerate(coords):
coords_to_index[key] = idx
for cube in cubes:
i0, i1, i2, cs = cube
cells.append([8,
coords_to_index[(i0, i1, i2)],
coords_to_index[(i0+cs, i1, i2)],
coords_to_index[(i0+cs, i1+cs, i2)],
coords_to_index[(i0, i1+cs, i2)],
coords_to_index[(i0, i1, i2+cs)],
coords_to_index[(i0+cs, i1, i2+cs)],
coords_to_index[(i0+cs, i1+cs, i2+cs)],
coords_to_index[(i0, i1+cs, i2+cs)]
])
cell_types = np.array([pyvista.CellType.HEXAHEDRON] * len(cells), np.int8)
return pyvista.UnstructuredGrid(cells, cell_types, coords)
def big_cube_small_cube_mixed():
_coords: set[tuple[float, float, float]] = set()
cubes: list[tuple[float, float, float, int]] = list()
coords: list[tuple[int, int, int]] = None
coords_to_index: dict[tuple[int, int, int], int] = dict()
cells: list[list[int]] = list()
dim0 = 3
dim1 = 3
dim2 = 3
for i0 in range(dim0):
for i1 in range(dim1):
for i2 in range(dim2):
cubes.append((float(i0), float(i1), float(i2), 1))
# merge 1-length 8 cubes
cubes.remove((float(0), float(0), float(0), 1))
cubes.remove((float(0), float(0), float(1), 1))
cubes.remove((float(0), float(1), float(0), 1))
cubes.remove((float(0), float(1), float(1), 1))
cubes.remove((float(1), float(0), float(0), 1))
cubes.remove((float(1), float(0), float(1), 1))
cubes.remove((float(1), float(1), float(0), 1))
cubes.remove((float(1), float(1), float(1), 1))
# into a 2-length cube
cubes.append((float(0), float(0), float(0), 2))
for cube in cubes:
i0, i1, i2, cube_size = cube
_coords.add((i0, i1, i2))
_coords.add((i0, i1, i2+cube_size))
_coords.add((i0, i1+cube_size, i2))
_coords.add((i0, i1+cube_size, i2+cube_size))
_coords.add((i0+cube_size, i1, i2))
_coords.add((i0+cube_size, i1, i2+1))
_coords.add((i0+cube_size, i1+cube_size, i2))
_coords.add((i0+cube_size, i1+cube_size, i2+cube_size))
coords = list(_coords)
for idx, key in enumerate(coords):
coords_to_index[key] = idx
for cube in cubes:
i0, i1, i2, cube_size = cube
cells.append([8,
coords_to_index[(i0, i1, i2)],
coords_to_index[(i0+cube_size, i1, i2)],
coords_to_index[(i0+cube_size, i1+cube_size, i2)],
coords_to_index[(i0, i1+cube_size, i2)],
coords_to_index[(i0, i1, i2+cube_size)],
coords_to_index[(i0+cube_size, i1, i2+cube_size)],
coords_to_index[(i0+cube_size, i1 +
cube_size, i2+cube_size)],
coords_to_index[(i0, i1+cube_size, i2+cube_size)]
])
cell_types = np.array([pyvista.CellType.HEXAHEDRON] * len(cells), np.int8)
return pyvista.UnstructuredGrid(cells, cell_types, coords)
def main():
colors = vtkNamedColors()
# Create a mapper and actor.
grid_mapper = vtkDataSetMapper()
grid_mapper.SetInputData(cube_3_3_3())
grid_mapper2 = vtkDataSetMapper()
grid_mapper2.SetInputData(big_cube_small_cube_mixed())
grid_actor = vtkActor()
grid_actor.SetMapper(grid_mapper)
grid_actor.GetProperty().SetOpacity(0.3)
grid_actor.GetProperty().SetDiffuseColor(colors.GetColor3d('green'))
grid_actor.SetPosition(-5, 0, 0)
grid_actor2 = vtkActor()
grid_actor2.SetMapper(grid_mapper2)
grid_actor2.GetProperty().SetOpacity(0.3)
grid_actor2.GetProperty().SetDiffuseColor(colors.GetColor3d('red'))
grid_actor2.SetPosition(5, 0, 0)
# Visualize
renderer = vtkRenderer()
ren_win = vtkRenderWindow()
ren_win.AddRenderer(renderer)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
renderer.AddActor(grid_actor)
renderer.AddActor(grid_actor2)
renderer.SetBackground(colors.GetColor3d('gray'))
# ren_win.SetSize(640, 480)
ren_win.SetWindowName('BlankPoint')
ren_win.Render()
iren.Start()
if __name__ == '__main__':
main()
context: i want to convert a voxel into a manifold mesh, compress the mesh by merging cubes into bigger cube, and also want to visualize internal structure by adjust opacity
Edited by KyungMyung Baek