-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboolean_sdfs.py
60 lines (46 loc) · 1.79 KB
/
boolean_sdfs.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
"""Compute and render surface normals"""
import matplotlib.pyplot as plt
import sdftoolbox
import numpy as np
def extract(scene: sdftoolbox.sdfs.SDF, grid: sdftoolbox.Grid):
verts, faces = sdftoolbox.dual_isosurface(
scene,
grid,
vertex_strategy=sdftoolbox.DualContouringVertexStrategy(),
edge_strategy=sdftoolbox.NewtonEdgeStrategy(),
triangulate=False,
)
return verts, faces
def main():
fig, ax = sdftoolbox.plotting.create_figure(fig_aspect=9 / 16, proj_type="persp")
max_corner = np.array([-np.inf, -np.inf, -np.inf])
min_corner = np.array([np.inf, np.inf, np.inf])
box = sdftoolbox.sdfs.Box.create((1, 2, 0.5))
sphere = sdftoolbox.sdfs.Sphere.create(radius=0.4)
grid = sdftoolbox.Grid(
res=(40, 40, 40), min_corner=(-1.2, -1.2, -1.2), max_corner=(1.2, 1.2, 1.2)
)
# Union
scene = box.merge(sphere, alpha=np.inf)
verts, faces = extract(scene, grid)
sdftoolbox.plotting.plot_mesh(ax, verts, faces)
max_corner = np.maximum(verts.max(0), max_corner)
min_corner = np.minimum(verts.min(0), min_corner)
# Intersection
scene = box.intersect(sphere, alpha=np.inf)
verts, faces = extract(scene, grid)
verts += (1.5, 0.0, 0.0)
sdftoolbox.plotting.plot_mesh(ax, verts, faces)
max_corner = np.maximum(verts.max(0), max_corner)
min_corner = np.minimum(verts.min(0), min_corner)
# Difference
scene = box.subtract(sphere, alpha=np.inf)
verts, faces = extract(scene, grid)
verts += (3.0, 0.0, 0.0)
sdftoolbox.plotting.plot_mesh(ax, verts, faces)
max_corner = np.maximum(verts.max(0), max_corner)
min_corner = np.minimum(verts.min(0), min_corner)
sdftoolbox.plotting.setup_axes(ax, min_corner, max_corner)
plt.show()
if __name__ == "__main__":
main()