-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_figures.py
More file actions
258 lines (219 loc) · 7.98 KB
/
Copy pathgenerate_figures.py
File metadata and controls
258 lines (219 loc) · 7.98 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
"""Generate the figures embedded in the README using pga3d_display helpers.
Run from the repository root:
python doc/generate_figures.py
"""
import math
import os
import sys
import matplotlib
matplotlib.use("Agg") # non-interactive backend for figure generation
import matplotlib.pyplot as plt
# Make the repo root importable when running from anywhere.
_REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if _REPO_ROOT not in sys.path:
sys.path.insert(0, _REPO_ROOT)
from pga3d import Point, Line, Plane, Translator, Rotor # noqa: E402
from pga3d.pga3d_display import ( # noqa: E402
plot_points,
plot_lines,
plot_plane,
plot_line_object,
plot_plane_object,
)
DOC_DIR = os.path.dirname(os.path.abspath(__file__))
def _save(fig, name):
path = os.path.join(DOC_DIR, name)
fig.savefig(path, dpi=120, bbox_inches="tight")
plt.close(fig)
print(f"wrote {path}")
def fig_point():
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([Point(1, 2, 3)], ax=ax)
ax.legend()
ax.set_title("Single Point")
_save(fig, "fig_point.png")
def fig_line():
p1, p2 = Point(0, 0, 0), Point(1, 1, 1)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p1, p2], ax=ax)
plot_lines([(p1, p2)], ax=ax)
ax.legend()
ax.set_title("Line from Two Points")
_save(fig, "fig_line.png")
def fig_plane():
p1, p2, p3 = Point(0, 0, 0), Point(1, 0, 0), Point(0, 1, 0)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p1, p2, p3], ax=ax)
plot_plane([p1, p2, p3], ax=ax)
ax.legend()
ax.set_title("Plane from Three Points")
_save(fig, "fig_plane.png")
def fig_transformations():
p = Point(1, 0, 0)
t = Translator.from_xyz(1, 0, 0)
r = Rotor.from_angle_and_line(math.pi / 2, Line.from_xyz(0, 0, 1))
m = t * r
p_t = t.project(p)
p_tr = m.project(p)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p, p_t, p_tr], ax=ax, color="C1")
ax.legend()
ax.set_title("Point, Translated, and Translated+Rotated")
_save(fig, "fig_transformations.png")
def fig_projection():
p1 = Point(2, 3, 4)
p2 = Point(20, 3, 7)
p3 = Point(9, 12, 17)
line = Line.from_points(p3, p1)
plane = Plane.from_points(p1, p2, p3)
p_proj = p1.project_onto(line)
plane.project_onto(Point(0, 0, 0))
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p1, p_proj], ax=ax, color="C2")
plot_lines([(p3, p1)], ax=ax)
plot_plane([p1, p2, p3], ax=ax)
ax.legend()
ax.set_title("Projection of Point onto Line")
_save(fig, "fig_projection.png")
def fig_scene():
p0 = Point(0, 0, 0)
p1 = Point(2, 3, 4)
p2 = Point(20, 3, 7)
p3 = Point(9, 12, 17)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p0, p1, p2, p3], ax=ax)
plot_lines([(p1, p2), (p2, p3), (p3, p1)], ax=ax)
plot_plane([p1, p2, p3], ax=ax)
ax.legend()
ax.set_title("PGA3D Example Objects")
_save(fig, "fig_scene.png")
# ---------------------------------------------------------------------------
# Meet (^ outer product) and Join (& regressive product) figures
# ---------------------------------------------------------------------------
def _algebra_point_to_point(p_alg):
z = p_alg.value[11]
y = p_alg.value[12]
x = p_alg.value[13]
w = p_alg.value[14]
if abs(w) < 1e-12:
return None
return Point(x / w, y / w, z / w)
def fig_join_two_points():
p1 = Point(0, 0, 0)
p2 = Point(4, 3, 2)
line = p1 & p2 # join of two points = line
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p1, p2], ax=ax, color="C0")
plot_line_object(line, ax=ax, color="C3", length=6.0,
label="Join: p1 & p2", center=(2, 1.5, 1))
ax.legend()
ax.set_title("Join of two points -> line")
_save(fig, "fig_join_two_points.png")
def fig_join_three_points():
p1 = Point(3, 0, 0)
p2 = Point(0, 3, 0)
p3 = Point(0, 0, 3)
plane = p1 & p2 & p3 # join of three points = plane
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([p1, p2, p3], ax=ax, color="C0")
plot_lines([(p1, p2), (p2, p3), (p3, p1)], ax=ax, color="C0")
plot_plane_object(plane, ax=ax, color="C3", alpha=0.3,
center=(1, 1, 1), size=4.0,
label="Join: p1 & p2 & p3")
ax.legend()
ax.set_title("Join of three points -> plane")
_save(fig, "fig_join_three_points.png")
def fig_join_line_and_point():
a = Point(0, 0, 0)
b = Point(4, 0, 0)
line = a & b
p = Point(2, 4, 3)
plane = line & p # join of line and point = plane
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_points([a, b, p], ax=ax, color="C0")
plot_line_object(line, ax=ax, color="C2", length=4.0,
label="input line", center=(2, 0, 0))
plot_plane_object(plane, ax=ax, color="C3", alpha=0.3,
center=(2, 1.5, 1), size=4.0,
label="Join: line & p")
ax.legend()
ax.set_title("Join of line and point -> plane")
_save(fig, "fig_join_line_point.png")
def fig_meet_two_planes():
pi1 = Plane.from_abcd(1, 0, 0, 0) # x = 0
pi2 = Plane.from_abcd(0, 1, 0, 0) # y = 0
line = pi1 ^ pi2 # meet of two planes = line
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_plane_object(pi1, ax=ax, color="C0", alpha=0.2,
center=(0, 0, 0), size=4.0, label="plane 1 (x=0)")
plot_plane_object(pi2, ax=ax, color="C1", alpha=0.2,
center=(0, 0, 0), size=4.0, label="plane 2 (y=0)")
plot_line_object(line, ax=ax, color="C3", length=5.0,
label="Meet: pi1 ^ pi2", center=(0, 0, 0))
ax.legend()
ax.set_title("Meet of two planes -> line")
_save(fig, "fig_meet_two_planes.png")
def fig_meet_three_planes():
pi1 = Plane.from_abcd(1, 0, 0, -2) # x = 2
pi2 = Plane.from_abcd(0, 1, 0, -3) # y = 3
pi3 = Plane.from_abcd(0, 0, 1, -1) # z = 1
p_alg = pi1 ^ pi2 ^ pi3 # meet of three planes = point
pt = _algebra_point_to_point(p_alg)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_plane_object(pi1, ax=ax, color="C0", alpha=0.2,
center=(2, 3, 1), size=3.0, label="plane 1 (x=2)")
plot_plane_object(pi2, ax=ax, color="C1", alpha=0.2,
center=(2, 3, 1), size=3.0, label="plane 2 (y=3)")
plot_plane_object(pi3, ax=ax, color="C2", alpha=0.2,
center=(2, 3, 1), size=3.0, label="plane 3 (z=1)")
if pt is not None:
plot_points([pt], ax=ax, color="C3",
label_prefix="Meet: pi1^pi2^pi3 = ")
ax.legend()
ax.set_title("Meet of three planes -> point")
_save(fig, "fig_meet_three_planes.png")
def fig_meet_plane_and_line():
pi = Plane.from_abcd(0, 0, 1, -2) # z = 2
a = Point(-1, -1, 0)
b = Point(3, 4, 5)
line = a & b
p_alg = pi ^ line # meet of plane and line = point
pt = _algebra_point_to_point(p_alg)
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
plot_plane_object(pi, ax=ax, color="C0", alpha=0.2,
center=(1, 1, 2), size=4.0, label="plane (z=2)")
plot_line_object(line, ax=ax, color="C2", length=5.0,
label="input line", center=(1, 1, 2))
if pt is not None:
plot_points([pt], ax=ax, color="C3",
label_prefix="Meet: pi ^ line = ")
ax.legend()
ax.set_title("Meet of plane and line -> point")
_save(fig, "fig_meet_plane_line.png")
def main():
fig_point()
fig_line()
fig_plane()
fig_transformations()
fig_projection()
fig_scene()
fig_join_two_points()
fig_join_three_points()
fig_join_line_and_point()
fig_meet_two_planes()
fig_meet_three_planes()
fig_meet_plane_and_line()
if __name__ == "__main__":
main()