-
Notifications
You must be signed in to change notification settings - Fork 5
/
enclosing_box.py
117 lines (88 loc) · 3.9 KB
/
enclosing_box.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""
This module demonstrates the workflow of creating and visualizing a set of randomly positioned spheres within an enclosing box using the Visualization Toolkit (VTK). It highlights the use of VTK's capabilities for generating and displaying 3D objects in a defined space, showcasing fundamental concepts in computational geometry and 3D visualization.
Workflow Overview:
1. Sphere Creation (create_random_sphere):
- Generates individual spheres with specified centers and radii.
- Illustrates how to create basic 3D objects (spheres) in VTK.
2. Random Spheres Mapper (create_random_spheres_mapper):
- Constructs multiple spheres, each positioned randomly within a specified box size.
- Utilizes vtkAppendPolyData to combine multiple vtkSphereSources into a single data structure.
- Creates a vtkPolyDataMapper for the set of spheres, preparing them for visualization.
3. Enclosing Box Mapper (create_enclosing_box_mapper):
- Generates an enclosing box (using vtkCubeSource) that defines the boundary for the random spheres.
- Applies vtkOutlineFilter to create an outline of the box, enhancing spatial understanding.
- Produces a vtkPolyDataMapper for the box, allowing it to be rendered alongside the spheres.
4. Visualization Pipeline:
- The module uses a predefined 'VisualisationPipeline' class to manage the rendering process.
- Both the random spheres and the enclosing box are visualized together, providing context and enhancing the perception of the 3D space.
"""
import random
from typing import Tuple
import vtk
from src.common.simple_pipeline import VisualisationPipeline
def create_random_sphere(
center: Tuple[float, float, float], radius: float
) -> vtk.vtkSphereSource:
"""
Create a sphere source with specified center and radius.
Args:
center (tuple): The center of the sphere.
radius (float): The radius of the sphere.
Returns:
vtkSphereSource: A VTK sphere source.
"""
sphere = vtk.vtkSphereSource()
sphere.SetCenter(*center)
sphere.SetRadius(radius)
sphere.Update()
return sphere
def create_random_spheres_mapper(
n_spheres: int, box_size: float, sphere_radius: float = 0.2
) -> vtk.vtkPolyDataMapper:
"""
Create a mapper for a set of randomly positioned spheres within a box.
Args:
n_spheres (int): Number of spheres to create.
box_size (float): Size of the box in which spheres are contained.
sphere_radius (float): Radius of each sphere.
Returns:
vtkPolyDataMapper: A mapper for the random spheres.
"""
append_filter = vtk.vtkAppendPolyData()
for _ in range(n_spheres):
center = (
random.uniform(-box_size / 2, box_size / 2),
random.uniform(-box_size / 2, box_size / 2),
random.uniform(-box_size / 2, box_size / 2),
)
sphere = create_random_sphere(center, sphere_radius)
append_filter.AddInputConnection(sphere.GetOutputPort())
append_filter.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(append_filter.GetOutputPort())
return mapper
def create_enclosing_box_mapper(box_size: float) -> vtk.vtkPolyDataMapper:
"""
Create a mapper for an enclosing box.
Args:
box_size (float): Size of the box.
Returns:
vtkPolyDataMapper: A mapper for the enclosing box.
"""
box = vtk.vtkCubeSource()
box.SetXLength(box_size)
box.SetYLength(box_size)
box.SetZLength(box_size)
box.Update()
outline = vtk.vtkOutlineFilter()
outline.SetInputConnection(box.GetOutputPort())
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(outline.GetOutputPort())
return mapper
if __name__ == "__main__":
n_spheres = 50
box_size = 5.0
spheres_mapper = create_random_spheres_mapper(n_spheres, box_size)
box_mapper = create_enclosing_box_mapper(box_size)
pipeline = VisualisationPipeline(mappers=[spheres_mapper, box_mapper])
pipeline.run()