Testing Ray AABB intersection#

This example shows how to use Ray AABB intersection 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)
 86 def draw_ray(ax, ray, color):
 87     """
 88     Draw a 3D Ray in matplotlib
 89
 90     Parameters
 91     ----------
 92     ax : matplotlib.Axes3D
 93         The axis to draw the Ray on
 94     ray : shamrock.Ray_f64_3
 95         The Ray to draw
 96     color : str
 97         The color of the Ray
 98     """
 99     xmin, ymin, zmin = ray.origin()
100     nx, ny, nz = ray.direction()
101     inx, iny, inz = ray.inv_direction()
102     print(ray.direction(), ray.inv_direction())
103     print(nx * inx, ny * iny, nz * inz)
104
105     ax.plot3D([xmin, nx + xmin], [ymin, ny + ymin], [zmin, nz + zmin], c=color)
110 fig = plt.figure()
111 ax = fig.add_subplot(111, projection="3d")
112 ax.minorticks_off()
113
114 aabb1 = shamrock.math.AABB_f64_3((-1.0, -1.0, -1.0), (1.0, 1.0, 1.0))
115
116 draw_aabb(ax, aabb1, "b", 0.1)
117
118
119 def add_ray(ray):
120     cd = aabb1.intersect_ray(ray)
121
122     print(cd)
123
124     if cd:
125         draw_ray(ax, ray, "g")
126     else:
127         draw_ray(ax, ray, "r")
128
129
130 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (2.0, 2.0, 2.0)))
131 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (1.0, 2.0, 2.0)))
132 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.7, 2.0, 2.0)))
133 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.6, 2.0, 2.0)))
134 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.5, 2.0, 2.0)))
135 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.1, 2.0, 2.0)))
136 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.0, 2.0, 2.0)))
137 add_ray(shamrock.math.Ray_f64_3((-2.0, -2.0, -2.0), (0.0, 0.0, 2.0)))
138
139 ax.set_xlabel("X")
140 ax.set_ylabel("Y")
141 ax.set_zlabel("Z")
142
143 ax.set_xlim(-2, 2)
144 ax.set_ylim(-2, 2)
145 ax.set_zlim(-2, 2)
146
147 plt.show()
run aabb ray interesect
True
(0.5773502691896258, 0.5773502691896258, 0.5773502691896258) (1.7320508075688772, 1.7320508075688772, 1.7320508075688772)
1.0 1.0 1.0
True
(0.3333333333333333, 0.6666666666666666, 0.6666666666666666) (3.0, 1.5, 1.5)
1.0 1.0 1.0
True
(0.24023937806910306, 0.6863982230545802, 0.6863982230545802) (4.1625149383809905, 1.4568802284333466, 1.4568802284333466)
1.0 1.0 1.0
False
(0.20751433915982237, 0.6917144638660746, 0.6917144638660746) (4.8189440982669876, 1.445683229480096, 1.445683229480096)
1.0 1.0 1.0
False
(0.17407765595569785, 0.6963106238227914, 0.6963106238227914) (5.744562646538029, 1.4361406616345072, 1.4361406616345072)
1.0 1.0 1.0
False
(0.03533326266687867, 0.7066652533375732, 0.7066652533375732) (28.30194339616981, 1.4150971698084907, 1.4150971698084907)
1.0 1.0 1.0
False
(0.0, 0.7071067811865475, 0.7071067811865475) (inf, 1.4142135623730951, 1.4142135623730951)
nan 1.0 1.0
False
(0.0, 0.0, 1.0) (inf, inf, 1.0)
nan nan 1.0

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

Estimated memory usage: 159 MB

Gallery generated by Sphinx-Gallery