Examples

Example 1: Basic Voxel Manipulation

This example demonstrates how to create a VoxelGrid and add voxels with various identities:

# Import the VoxelForge package
import voxelforge as vf

# Create a new VoxelGrid instance
grid = vf.VoxelGrid()

# Add several voxels with default and custom identities
grid.addVoxel(1, 2, 3)  # Default identity (integer)
grid.addVoxel(4, 5, 6, "Colorful Voxel")  # String identity
grid.addVoxel(7, 8, 9, {"key": "value"})  # Dictionary identity
grid.addVoxel(10, 11, 12, 3.14159)  # Floating-point identity

# Print out the voxel information
voxels = grid.getVoxels()
for voxel in voxels:
    print(f'Voxel at ({voxel.x}, {voxel.y}, {voxel.z}) with data: {voxel.data}')

Example 2: Using Octrees for Spatial Indexing

Here we show how to initialize an Octree, insert points, and locate nodes:

# Import the VoxelForge package and numpy for handling coordinates
import voxelforge as vf
import numpy as np

# Initialize an Octree with a specific origin, size, and maximum depth
origin = np.array([0.0, 0.0, 0.0])
size = 50.0
max_depth = 4
octree = vf.Octree(origin, size, max_depth)

# Insert points into the Octree
points = [
    np.array([5.0, 5.0, 5.0]),
    np.array([15.0, 15.0, 15.0]),
    np.array([35.0, 35.0, 35.0]),
    np.array([45.0, 45.0, 45.0])
]

for point in points:
    octree.insert_point(point)

# Locate a specific point and print its information
target_point = np.array([5.0, 5.0, 5.0])
leaf_node = octree.locate_leaf_node(target_point)
if leaf_node:
    print(f"Leaf node found at {leaf_node.get_point()}")
else:
    print("No leaf node found at the specified location.")