-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathplot_shape.py
More file actions
107 lines (96 loc) · 2.62 KB
/
plot_shape.py
File metadata and controls
107 lines (96 loc) · 2.62 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
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
def plot_shape(ax, verts, faces, title):
ax.add_collection3d(Poly3DCollection([verts[face] for face in faces], alpha=0.6, edgecolor='k'))
ax.scatter3D(*verts.T, color='r')
ax.set_title(title)
ax.set_box_aspect([1, 1, 1])
ax.set_xlim(-2, 2)
ax.set_ylim(-2, 2)
ax.set_zlim(-2, 2)
def make_reference_prism():
# Two triangle faces (top & bottom)
h = np.sqrt(3.) / 4
top = np.array([[0, 1, h], [0, 0, h], [1, 0, h]])
bottom = top.copy()
bottom[:, 2] = -1.
verts = np.vstack([top, bottom])
faces = [
[0, 1, 2], # top
[3, 4, 5], # bottom
[0, 1, 4, 3],
[1, 2, 5, 4],
[2, 0, 3, 5]
]
return verts, faces
def make_target_prism():
# Two triangle faces (top & bottom)
h = np.sqrt(3) / 2
top = np.array([[0, 1, h], [-np.sqrt(3)/2, -0.5, h], [np.sqrt(3)/2, -0.5, h]])
bottom = top.copy()
bottom[:, 2] = 0
verts = np.vstack([top, bottom])
faces = [
[0, 1, 2], # top
[3, 4, 5], # bottom
[0, 1, 4, 3],
[1, 2, 5, 4],
[2, 0, 3, 5]
]
return verts, faces
def make_tetrahedron():
# Vertices of a regular tetrahedron centered at origin
verts = np.array([
[1, 0, -1/np.sqrt(2)],
[-0.5, np.sqrt(3)/2, -1/np.sqrt(2)],
[-0.5, -np.sqrt(3)/2, -1/np.sqrt(2)],
[0, 0, np.sqrt(2)]
])
faces = [
[0, 1, 2],
[0, 1, 3],
[1, 2, 3],
[2, 0, 3]
]
return verts, faces
def make_square_pyramid():
# Square base pyramid
verts = np.array([
[-1, -1, 0],
[1, -1, 0],
[1, 1, 0],
[-1, 1, 0],
[0, 0, 1.5]
])
faces = [
[0, 1, 2, 3], # base
[0, 1, 4],
[1, 2, 4],
[2, 3, 4],
[3, 0, 4]
]
return verts, faces
# reference Prism
fig = plt.figure(figsize=(7, 7))
ax1 = fig.add_subplot(111, projection='3d')
verts, faces = make_reference_prism()
plot_shape(ax1, verts, faces, "Reference Prism")
# target Prism
fig = plt.figure(figsize=(7, 7))
ax1 = fig.add_subplot(111, projection='3d')
verts, faces = make_target_prism()
plot_shape(ax1, verts, faces, "Target Prism")
## Tetrahedron
#fig = plt.figure(figsize=(7, 7))
#ax2 = fig.add_subplot(111, projection='3d')
#verts, faces = make_tetrahedron()
#plot_shape(ax2, verts, faces, "Tetrahedron")
#
## Pyramid
#fig = plt.figure(figsize=(7, 7))
#ax3 = fig.add_subplot(111, projection='3d')
#verts, faces = make_square_pyramid()
#plot_shape(ax3, verts, faces, "Square Pyramid")
plt.tight_layout()
plt.show()