-
Notifications
You must be signed in to change notification settings - Fork 0
/
geommaker.py
379 lines (297 loc) · 12.3 KB
/
geommaker.py
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import array
import math
import random
from enum import Enum
from panda3d.core import Vec3, LColor
from panda3d.core import GeomVertexFormat, GeomVertexData, GeomVertexArrayFormat
from panda3d.core import Geom, GeomNode, GeomTriangles
from polyhedrons_data import POLYHEDRONS
class Colors(Enum):
RED = LColor(1, 0, 0, 1)
BLUE = LColor(0, 0, 1, 1)
YELLOW = LColor(1, 1, 0, 1)
GREEN = LColor(0, 0.5, 0, 1)
ORANGE = LColor(1, 0.549, 0, 1)
MAGENTA = LColor(1, 0, 1, 1)
PURPLE = LColor(0.501, 0, 0.501, 1)
LIME = LColor(0, 1, 0, 1)
VIOLET = LColor(0.54, 0.16, 0.88, 1)
SKY = LColor(0, 0.74, 1, 1)
@classmethod
def select(cls, n):
return random.sample([m.value for m in cls], n)
class GeomMaker:
def triangle(self, start):
return (start, start + 1, start + 2)
def square(self, start):
for x, y, z in [(2, 1, 0), (0, 3, 2)]:
yield (start + x, start + y, start + z)
def polygon(self, start, vertices_num):
for i in range(2, vertices_num):
if i == 2:
yield (start, start + i - 1, start + i)
else:
yield (start + i - 1, start, start + i)
def prim_indices(self, start, num):
match num:
case 3:
yield self.triangle(start)
case 4:
yield from self.square(start)
case _:
yield from self.polygon(start, num)
class PolyhedronGeomMaker(GeomMaker):
def __init__(self):
self.idx = 0
self.polh_names = tuple(POLYHEDRONS.keys())
self.make_format()
def make_format(self):
arr_format = GeomVertexArrayFormat()
arr_format.add_column('vertex', 3, Geom.NTFloat32, Geom.CPoint)
arr_format.add_column('color', 4, Geom.NTFloat32, Geom.CColor)
arr_format.add_column('normal', 3, Geom.NTFloat32, Geom.CNormal)
self.format_ = GeomVertexFormat.register_format(arr_format)
def next_geomnode(self):
if self.idx >= len(self.polh_names):
self.idx = 0
polh_name = self.polh_names[self.idx]
self.idx += 1
geom_node = self.make_geomnode(polh_name)
return geom_node
def make_geomnode(self, key, colors=None):
self.data = POLYHEDRONS[key]
self.colors = colors if colors else self.select_colors()
geom_node = self._make_geomnode()
return geom_node
def faces(self):
vertices = self.data['vertices']
faces = self.data['faces']
color_pattern = self.data['color_pattern']
normals = self.data['normals']
for idxes, n, normal in zip(faces, color_pattern, normals):
vert = (vertices[i] for i in idxes)
if normal is None:
normal = (Vec3(vertices[i]).normalized() for i in idxes)
yield (vert, self.colors[n], normal, len(idxes))
def select_colors(self):
n = max(self.data['color_pattern'])
return Colors.select(n + 1)
def num_rows(self):
return sum(len(face) for face in self.data['faces'])
def _make_geomnode(self):
vdata_values = array.array('f', [])
prim_indices = array.array("H", [])
start = 0
for verts, rgba, norms, num_verts in self.faces():
for pt, norm in zip(verts, norms):
vdata_values.extend(pt)
vdata_values.extend(rgba)
vdata_values.extend(norm)
for indices in self.prim_indices(start, num_verts):
prim_indices.extend(indices)
start += num_verts
vdata = GeomVertexData('polyhedron', self.format_, Geom.UHStatic)
vdata.unclean_set_num_rows(self.num_rows())
vdata_mem = memoryview(vdata.modify_array(0)).cast('B').cast('f')
vdata_mem[:] = vdata_values
prim = GeomTriangles(Geom.UHStatic)
prim_array = prim.modify_vertices()
prim_array.unclean_set_num_rows(len(prim_indices))
prim_mem = memoryview(prim_array).cast('B').cast('H')
prim_mem[:] = prim_indices
node = GeomNode('geomnode')
geom = Geom(vdata)
geom.add_primitive(prim)
node.add_geom(geom)
return node
class PyramidGeomMaker(GeomMaker):
def __init__(self, length=2, cycle=12, radius=0.5):
self.cone_length = length
self.cycle = cycle
self.cone_radius = radius
self.make_format()
def make_format(self):
arr_format = GeomVertexArrayFormat()
arr_format.add_column('vertex', 3, Geom.NTFloat32, Geom.CPoint)
arr_format.add_column('normal', 3, Geom.NTFloat32, Geom.CNormal)
self.format_ = GeomVertexFormat.register_format(arr_format)
def num_rows(self):
return self.cycle + 3 * self.cycle
def faces(self):
point = Vec3(0, 0, self.cone_length)
bottom = []
for i in range(self.cycle):
theta = i * (2 * math.pi / self.cycle)
x = self.cone_radius * math.sin(theta)
y = self.cone_radius * math.cos(theta)
bottom.append(Vec3(x, y, 0.0))
yield bottom
for i in range(0, len(bottom)):
if i == len(bottom) - 1:
yield (point, bottom[0], bottom[i])
else:
yield (point, bottom[i], bottom[i + 1])
def make_geomnode(self):
vdata_values = array.array('f', [])
prim_indices = array.array("H", [])
start = 0
for face in self.faces():
for pt in face:
vdata_values.extend(pt)
normal = Vec3(0, 0, -1) if len(face) == self.cycle else pt.normalized()
vdata_values.extend(normal)
for indices in self.prim_indices(start, len(face)):
prim_indices.extend(indices)
start += len(face)
vdata = GeomVertexData('pyramid', self.format_, Geom.UHStatic)
vdata.unclean_set_num_rows(self.num_rows())
vdata_mem = memoryview(vdata.modify_array(0)).cast('B').cast('f')
vdata_mem[:] = vdata_values
prim = GeomTriangles(Geom.UHStatic)
prim_array = prim.modify_vertices()
prim_array.unclean_set_num_rows(len(prim_indices))
prim_mem = memoryview(prim_array).cast('B').cast('H')
prim_mem[:] = prim_indices
node = GeomNode('geomnode')
geom = Geom(vdata)
geom.add_primitive(prim)
node.add_geom(geom)
return node
class SphereGeomMaker(GeomMaker):
def __init__(self):
self.data = POLYHEDRONS['icosahedron']
self.divnum = 3
self.make_format()
def make_format(self):
arr_format = GeomVertexArrayFormat()
arr_format.add_column('vertex', 3, Geom.NTFloat32, Geom.CPoint)
arr_format.add_column('color', 4, Geom.NTFloat32, Geom.CColor)
arr_format.add_column('normal', 3, Geom.NTFloat32, Geom.CNormal)
self.format_ = GeomVertexFormat.register_format(arr_format)
def make_geomnode(self, colors=None):
self.colors = colors if colors else Colors.select(2)
node = self._make_geomnode()
return node
def num_rows(self):
"""One triangle is subdivided into 4.
The number of subdivide repetition is self.divnum.
An icosahedron has 20 faces and a face has 3 vertices.
"""
return 4 ** self.divnum * 20 * 3
def calc_midpoints(self, face):
"""face: A list of Vec3, having 3 elements.
"""
# (i, j): [(0, 1), (1, 2), (2, 0)]
for i, pt1 in enumerate(face):
j = i + 1 if i < len(face) - 1 else 0
pt2 = face[j]
mid_pt = (pt1 + pt2) / 2
yield mid_pt
def subdivide(self, face, divnum=0):
if divnum == self.divnum:
yield face
else:
midpoints = [pt for pt in self.calc_midpoints(face)]
for i, vertex in enumerate(face):
j = len(face) - 1 if i == 0 else i - 1
face = [vertex, midpoints[i], midpoints[j]]
yield from self.subdivide(face, divnum + 1)
yield from self.subdivide(midpoints, divnum + 1)
def faces(self):
vertices = self.data['vertices']
faces = self.data['faces']
for tup in faces:
face = [Vec3(vertices[n]) for n in tup]
for subdiv_face in self.subdivide(face):
idx = 0 if any(pt.z == 0 for pt in subdiv_face) else 1
yield (subdiv_face, self.colors[idx])
def _make_geomnode(self):
vdata_values = array.array('f', [])
prim_indices = array.array("H", [])
start = 0
for face, rgba in self.faces():
for pt in face:
n = pt.normalized()
vdata_values.extend(n)
vdata_values.extend(rgba)
vdata_values.extend(n)
indices = self.triangle(start)
prim_indices.extend(indices)
start += 3
vdata = GeomVertexData('sphere', self.format_, Geom.UHStatic)
vdata.unclean_set_num_rows(self.num_rows())
vdata_mem = memoryview(vdata.modify_array(0)).cast('B').cast('f')
vdata_mem[:] = vdata_values
prim = GeomTriangles(Geom.UHStatic)
prim_array = prim.modify_vertices()
prim_array.unclean_set_num_rows(len(prim_indices))
prim_mem = memoryview(prim_array).cast('B').cast('H')
prim_mem[:] = prim_indices
node = GeomNode('geomnode')
geom = Geom(vdata)
geom.add_primitive(prim)
node.add_geom(geom)
return node
class TorusGeomMaker(GeomMaker):
def __init__(self, segs_r=24, segs_s=12, ring_radius=1.2, section_radius=0.5):
self.segs_r = segs_r
self.segs_s = segs_s
self.ring_radius = ring_radius
self.section_radius = section_radius
self.make_format()
def make_format(self):
arr_format = GeomVertexArrayFormat()
arr_format.add_column('vertex', 3, Geom.NTFloat32, Geom.CPoint)
arr_format.add_column('color', 4, Geom.NTFloat32, Geom.CColor)
arr_format.add_column('normal', 3, Geom.NTFloat32, Geom.CColor)
arr_format.add_column('texcoord', 2, Geom.NTFloat32, Geom.CTexcoord)
self.format_ = GeomVertexFormat.register_format(arr_format)
def make_geomnode(self):
self.colors = Colors.select(2)
geomnode = self._make_geomnode()
return geomnode
def _make_geomnode(self):
vdata_values = array.array('f', [])
prim_indices = array.array('H', [])
delta_angle_h = 2.0 * math.pi / self.segs_r
delta_angle_v = 2.0 * math.pi / self.segs_s
for i in range(self.segs_r + 1):
angle_h = delta_angle_h * i
u = i / self.segs_r
color = self.colors[0] if i // 2 % 2 == 0 else self.colors[1]
for j in range(self.segs_s + 1):
angle_v = delta_angle_v * j
r = self.ring_radius - self.section_radius * math.cos(angle_v)
c = math.cos(angle_h)
s = math.sin(angle_h)
x = r * c
y = r * s
z = self.section_radius * math.sin(angle_v)
nx = x - self.ring_radius * c
ny = y - self.ring_radius * s
normal_vec = Vec3(nx, ny, z).normalized()
v = 1.0 - j / self.segs_s
vdata_values.extend((x, y, z))
vdata_values.extend(color)
vdata_values.extend(normal_vec)
vdata_values.extend((u, v))
for i in range(self.segs_r):
for j in range(0, self.segs_s):
idx = j + i * (self.segs_s + 1)
prim_indices.extend([idx, idx + 1, idx + self.segs_s + 1])
prim_indices.extend([idx + self.segs_s + 1, idx + 1, idx + 1 + self.segs_s + 1])
vdata = GeomVertexData('torous', self.format_, Geom.UHStatic)
rows = (self.segs_r + 1) * (self.segs_s + 1)
vdata.unclean_set_num_rows(rows)
vdata_mem = memoryview(vdata.modify_array(0)).cast('B').cast('f')
vdata_mem[:] = vdata_values
prim = GeomTriangles(Geom.UHStatic)
prim_array = prim.modify_vertices()
prim_array.unclean_set_num_rows(len(prim_indices))
prim_mem = memoryview(prim_array).cast('B').cast('H')
prim_mem[:] = prim_indices
node = GeomNode('geomnode')
geom = Geom(vdata)
geom.add_primitive(prim)
node.add_geom(geom)
return node