Testing AABB intersection routine#

This example shows how to use AABB intersection and plot it in matplotlib

10 import matplotlib.pyplot as plt
11 from mpl_toolkits.mplot3d.art3d import Line3DCollection, Poly3DCollection
12
13 import shamrock

Use shamrock documentation style for matplotlib

17 shamrock.matplotlib.set_shamrock_mpl_style()
23 def draw_aabb(ax, aabb, color, alpha):
24     """
25     Draw a 3D AABB in matplotlib
26
27     Parameters
28     ----------
29     ax : matplotlib.Axes3D
30         The axis to draw the AABB on
31     aabb : shamrock.math.AABB_f64_3
32         The AABB to draw
33     color : str
34         The color of the AABB
35     alpha : float
36         The transparency of the AABB
37     """
38     xmin, ymin, zmin = aabb.lower
39     xmax, ymax, zmax = aabb.upper
40
41     points = [
42         aabb.lower,
43         (aabb.lower[0], aabb.lower[1], aabb.upper[2]),
44         (aabb.lower[0], aabb.upper[1], aabb.lower[2]),
45         (aabb.lower[0], aabb.upper[1], aabb.upper[2]),
46         (aabb.upper[0], aabb.lower[1], aabb.lower[2]),
47         (aabb.upper[0], aabb.lower[1], aabb.upper[2]),
48         (aabb.upper[0], aabb.upper[1], aabb.lower[2]),
49         aabb.upper,
50     ]
51
52     faces = [
53         [points[0], points[1], points[3], points[2]],
54         [points[4], points[5], points[7], points[6]],
55         [points[0], points[1], points[5], points[4]],
56         [points[2], points[3], points[7], points[6]],
57         [points[0], points[2], points[6], points[4]],
58         [points[1], points[3], points[7], points[5]],
59     ]
60
61     edges = [
62         [points[0], points[1]],
63         [points[0], points[2]],
64         [points[0], points[4]],
65         [points[1], points[3]],
66         [points[1], points[5]],
67         [points[2], points[3]],
68         [points[2], points[6]],
69         [points[3], points[7]],
70         [points[4], points[5]],
71         [points[4], points[6]],
72         [points[5], points[7]],
73         [points[6], points[7]],
74     ]
75
76     collection = Poly3DCollection(faces, alpha=alpha, color=color)
77     ax.add_collection3d(collection)
78
79     edge_collection = Line3DCollection(edges, color="k", alpha=alpha)
80     ax.add_collection3d(edge_collection)
 85 fig = plt.figure()
 86 ax = fig.add_subplot(111, projection="3d")
 87 ax.minorticks_off()
 88
 89 aabb1 = shamrock.math.AABB_f64_3((-1.0, -1.0, -1.0), (2.0, 2.0, 2.0))
 90 aabb2 = shamrock.math.AABB_f64_3((-2.0, -2.0, -2.0), (1.0, 1.0, 1.0))
 91
 92 draw_aabb(ax, aabb1, "b", 0.1)
 93 draw_aabb(ax, aabb2, "r", 0.1)
 94 draw_aabb(ax, aabb1.get_intersect(aabb2), "g", 0.5)
 95
 96 ax.set_xlabel("X")
 97 ax.set_ylabel("Y")
 98 ax.set_zlabel("Z")
 99
100 ax.set_xlim(-2, 2)
101 ax.set_ylim(-2, 2)
102 ax.set_zlim(-2, 2)
103
104 plt.show()
run aabb interesect

Total running time of the script: (0 minutes 0.657 seconds)

Estimated memory usage: 159 MB

Gallery generated by Sphinx-Gallery