Debian Codegraph @

src/meshzoo/_platonic.py

raw
1 import numpy as np
2
3 from ._helpers import _compose_from_faces
4
5
6 def tetra_surface(n: int):
7 corners = np.array(
8 [
9 [2 * np.sqrt(2) / 3, 0.0, -1.0 / 3.0],
10 [-np.sqrt(2) / 3, np.sqrt(2.0 / 3.0), -1.0 / 3.0],
11 [-np.sqrt(2) / 3, -np.sqrt(2.0 / 3.0), -1.0 / 3.0],
12 [0.0, 0.0, 1.0],
13 ]
14 )
15 # make sure the normals are pointing outwards
16 faces = [(0, 2, 1), (0, 1, 3), (0, 3, 2), (1, 2, 3)]
17
18 return _compose_from_faces(corners, faces, n)
19
20
21 def octa_surface(n: int):
22 corners = np.array(
23 [
24 [1.0, 0.0, 0.0],
25 [-1.0, 0.0, 0.0],
26 [0.0, 1.0, 0.0],
27 [0.0, -1.0, 0.0],
28 [0.0, 0.0, 1.0],
29 [0.0, 0.0, -1.0],
30 ]
31 )
32 faces = [
33 (0, 2, 4),
34 (1, 4, 2),
35 (1, 3, 4),
36 (0, 4, 3),
37 (0, 5, 2),
38 (1, 2, 5),
39 (1, 5, 3),
40 (0, 3, 5),
41 ]
42 return _compose_from_faces(corners, faces, n)
43
44
45 def icosa_surface(n: int, flat_top: bool = False):
46 assert n >= 1
47 # Start off with an isosahedron and refine.
48
49 # Construction from
50 # <http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html>.
51 # Create 12 vertices of a icosahedron.
52 t = (1.0 + np.sqrt(5.0)) / 2.0
53
54 if flat_top:
55 # This is basically a rotation of the "pointy-top" variant below; more
56 # precisely a rotation of theta around [0, 1, 0].
57 #
58 # theta = np.arctan((t - 1) / t)
59 cos_theta = t / np.sqrt(t**2 + (t - 1) ** 2)
60 sin_theta = (t - 1) / np.sqrt(t**2 + (t - 1) ** 2)
61
62 corners = np.array(
63 [
64 [-cos_theta, +t, +sin_theta],
65 [+cos_theta, +t, -sin_theta],
66 [-cos_theta, -t, +sin_theta],
67 [+cos_theta, -t, -sin_theta],
68 #
69 [+t * sin_theta, -1, +t * cos_theta],
70 [+t * sin_theta, +1, +t * cos_theta],
71 [-t * sin_theta, -1, -t * cos_theta],
72 [-t * sin_theta, +1, -t * cos_theta],
73 #
74 [+t * cos_theta - sin_theta, 0, -cos_theta - t * sin_theta],
75 [+t * cos_theta + sin_theta, 0, +cos_theta - t * sin_theta],
76 [-t * cos_theta - sin_theta, 0, -cos_theta + t * sin_theta],
77 [-t * cos_theta + sin_theta, 0, +cos_theta + t * sin_theta],
78 ]
79 )
80 else:
81 corners = np.array(
82 [
83 [-1, +t, +0],
84 [+1, +t, +0],
85 [-1, -t, +0],
86 [+1, -t, +0],
87 #
88 [+0, -1, +t],
89 [+0, +1, +t],
90 [+0, -1, -t],
91 [+0, +1, -t],
92 #
93 [+t, +0, -1],
94 [+t, +0, +1],
95 [-t, +0, -1],
96 [-t, +0, +1],
97 ]
98 )
99
100 faces = [
101 (0, 11, 5),
102 (0, 5, 1),
103 (0, 1, 7),
104 (0, 7, 10),
105 (0, 10, 11),
106 (1, 5, 9),
107 (5, 11, 4),
108 (11, 10, 2),
109 (10, 7, 6),
110 (7, 1, 8),
111 (3, 9, 4),
112 (3, 4, 2),
113 (3, 2, 6),
114 (3, 6, 8),
115 (3, 8, 9),
116 (4, 9, 5),
117 (2, 4, 11),
118 (6, 2, 10),
119 (8, 6, 7),
120 (9, 8, 1),
121 ]
122
123 return _compose_from_faces(corners, faces, n)