I used SciPy’s scipy.spatial.Voronoi to perform a 3D Voronoi partition, clipped the Voronoi regions (polyhedra) to a cube or sphere, and created 3D models of the Voronoi regions from the clipped vertices. Clone the repository, run clip_to_cube.py or clip_to_sphere.py, and press the [U] key on your keyboard. A force is applied to the 3D models, causing them to collapse. The parameters for the Voronoi partitioning and 3D models are managed in clipping_config.yaml. If you want to increase the number of Voronoi cuts (cut_points) or the number of times the 3D model’s faces are subdivided into triangles (max_depth), setting multi_processing to true will speed up the generation of the 3D models.
demo.mp4
demo2.mp4
- Panda3D 1.10.16
- numpy 2.2.6
- scipy 1.16.2
- shapely 2.1.2
- PyYAML 6.0.3
- Python 3.13
- Windows11
git clone --recursive https://github.com/taKana671/Clipped3DVoronoi.git
# cube
python clip_to_cube.py
# sphere
python clip_to_sphere.py
| key | description |
|---|---|
| Esc | Close the screen. |
| d | Toggles physical object display on and off. |
| w | Toggles wireframe display on and off. |
| u | Apply force to each voronoi region model. |
After profiling with cProfile and refactoring the code, the time taken to run clip_to_cube.py or clip_to_sphere.py with the default settings in clipping_config.yaml is now around 4 seconds, which is nearly the same as when using multiple processes. Some NumPy methods (such as numpy.cross) were taking a significant amount of time when repeatedly called with small arrays like shape=(3,) as arguments. After changing the implementation to avoid using these NumPy methods, the speed improved.
| parameter | data type | default | description |
|---|---|---|---|
| cut_points | int | 30 | The number of polyhedrons to divide a cube into. |
| cube_size | float | 1 | Length of a cube's edge. Must be greater than 0. |
| diff | float | 0.5 | How far from the vertices of the cube the dummy points should be placed. Must be greater than 0. |
| max_depth | int | 3 | The number of divisions of one triangle. Must be greater than 0. |
| scale | int | 2 | The scale of the polyhedron. Must be greater than 0. |
| multi_processing | bool | false | true or false. If true, the 3D models are created using multiprocessing. |
| parameter | data type | default | description |
|---|---|---|---|
| cut_points | int | 30 | The number of polyhedrons to divide a sphere into. |
| max_depth | int | 3 | The number of divisions of one triangle. Must be greater than 0. |
| scale | int | 2 | The scale of the polyhedron. Must be greater than 0. |
| multi_processing | bool | false | true or false. If true, the 3D models are created using multiprocessing. |