-
Notifications
You must be signed in to change notification settings - Fork 2
/
MeshGenerator.py
203 lines (180 loc) · 7.7 KB
/
MeshGenerator.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
#!/usr/bin/python
#
# Author: Jarkko Vatjus-Anttila <[email protected]>
#
# For conditions of distribution and use, see copyright notice in license.txt
#
import sys, os, io
import random
import math
import MeshContainer
import MeshIO
class MeshGenerator():
""" class MeshGenerator(): A collection of procedural methods for
creating mesh files with various content.
"""
def __init__(self, meshcontainer, sharedgeometry=False):
""" MeshGenerator.__init__(meshcontainer, sharedgeometry):
"""
self.meshcontainer = meshcontainer
self.sharedgeometry = sharedgeometry
#############################################################################
# Procedural primitive creators
#
# - Plane
# - Cube
# - Cylinder
# - Sphere
#############################################################################
#########################################################################
# Plane
# - Creates a planar mesh
#
def createPlane(self, LOD=1, materialref=""):
""" MeshGenerator.createPlane(self, LOD): Create a plane mesh component
- Method will create a plane mesh component in XZ-plane whose size is 1 times 1 units in
coordinate system and which is centered to origin.
"""
self.meshcontainer.initialize()
if self.sharedgeometry == True:
self.meshcontainer.newSharedGeometry()
else:
self.meshcontainer.newSubmesh(materialref=materialref) # The plane is pushed into single submesh
x_delta = 1.0 / LOD
z_delta = 1.0 / LOD
#
# First we create vertices, normals and texcoords
#
for x in range(LOD+1):
for z in range(LOD+1):
self.meshcontainer.addVertex([-0.5 + x*x_delta, 0.0, -0.5 + z*z_delta])
self.meshcontainer.addNormal([0.0, 1.0, 0.0])
self.meshcontainer.addTexcoord([x*x_delta, z*z_delta], 0)
#
# And according to above, we create the faces
#
if self.sharedgeometry == True:
self.meshcontainer.newSubmesh(materialref=materialref)
for x in range(LOD):
for z in range(LOD):
self.meshcontainer.addFace([z+x*(LOD+1), 1+z+x*(LOD+1), LOD+2+z+x*(LOD+1)])
self.meshcontainer.addFace([z+x*(LOD+1), LOD+2+z+x*(LOD+1), LOD+1+z+x*(LOD+1)])
#########################################################################
# Cube
# - Constructs a cubic mesh from 6 planar meshes. Does the merge with
# meshcontainer provided translation tools
#
def createCube(self, LOD=1):
self.meshcontainer.initialize()
self.meshcontainer.newSubmesh()
#
# Temporary mesh container for plane mesh (single side of a cube)
#
mesh = MeshContainer.MeshContainer()
meshgen = MeshGenerator(mesh)
meshgen.createPlane(LOD)
# Cube Face 1
mesh.translate(0.0, 0.5, 0.0)
self.meshcontainer.merge(mesh)
# Cube Face 2
mesh.translate(0.0, -0.5, 0.0)
mesh.rotate(180, 1, 0, 0)
mesh.translate(0.0, -0.5, 0.0)
self.meshcontainer.merge(mesh)
# Cube Face 3
mesh.translate(0.0, 0.5, 0.0)
mesh.rotate(90, 1, 0, 0)
mesh.translate(0.0, 0.0, -0.5)
self.meshcontainer.merge(mesh)
# Cube Face 4
mesh.translate(0.0, 0.0, 0.5)
mesh.rotate(90, 0, 1, 0)
mesh.translate(-0.5, 0.0, 0.0)
self.meshcontainer.merge(mesh)
# Cube Face 5
mesh.translate(0.5, 0.0, 0.0)
mesh.rotate(90, 0, 1, 0)
mesh.translate(0.0, 0.0, 0.5)
self.meshcontainer.merge(mesh)
# Cube Face 6
mesh.translate(0.0, 0.0, -0.5)
mesh.rotate(90, 0, 1, 0)
mesh.translate(0.5, 0.0, 0.0)
self.meshcontainer.merge(mesh)
#########################################################################
# Cylinder
# - Constructs a cylinder with experimental support for callbacks
#
def createCylinder(self, slices=2, LOD=1, minH=0.0, maxH=1.0, callback=None):
nR = 5+LOD
rDelta = 2.0*math.pi/nR
hDelta = (maxH-minH)/(slices-1)
print "MeshGenerator::createCylinder slices=%d, nR=%d, rDelta=%f, hDelta=%f" % (slices, nR, rDelta, hDelta)
self.meshcontainer.initialize()
if self.sharedgeometry == True:
self.meshcontainer.newSharedGeometry()
else:
self.meshcontainer.newSubmesh() # The cylinder is pushed into single submesh
for i in xrange(slices): # Vertical slices
for j in xrange(nR): # Circular slices
if callback == None:
r = 0.5
else:
r = callback(i*hDelta, j*rDelta)
self.meshcontainer.addVertex([r*math.sin(j*rDelta), minH+i*hDelta, r*math.cos(j*rDelta)])
self.meshcontainer.addNormal([0.0, -1.0, 0.0]) # This is bogus, for the time being
self.meshcontainer.addTexcoord([j*1.0/(nR-1), i*hDelta], 0)
if self.sharedgeometry == True:
self.meshcontainer.newSubmesh()
for i in xrange(slices-1):
for j in xrange(nR-1):
self.meshcontainer.addFace([i*nR+j, (i+1)*nR+j+1, (i+1)*nR+j])
self.meshcontainer.addFace([i*nR+j, i*nR+j+1, (i+1)*nR+j+1])
self.meshcontainer.addFace([i*nR+j+1, (i+1)*nR+0, (i+1)*nR+j+1])
self.meshcontainer.addFace([i*nR+j+1, i*nR+0, (i+1)*nR+0])
# Surface normal re-calculation should be here once the faces have been set ...
#########################################################################
# Sphere
# - Constructs a Sphere primitive
# - center at origin, radius 1.0
#
def createSphere(self, LOD=1):
nR = 5+LOD
slices = LOD+1
hDelta = 2.0/slices
self.meshcontainer.initialize()
if self.sharedgeometry == True:
self.meshcontainer.newSharedGeometry()
else:
self.meshcontainer.newSubmesh() # The sphere is pushed into single submesh
for i in xrange(slices+1): # Vertical slices
r = math.sqrt(1.0-(-1.0+i*hDelta)*(-1.0+i*hDelta))
for j in xrange(nR): # Circular slices
a = j*2*math.pi/(nR-1) # Current circle angle
self.meshcontainer.addVertex([r*math.sin(a), -1.0+i*hDelta, r*math.cos(a)])
self.meshcontainer.addNormal([0.0, -1.0, 0.0]) # This is bogus
self.meshcontainer.addTexcoord([j*1.0/(nR-1), i*hDelta], 0)
if self.sharedgeometry == True:
self.meshcontainer.newSubmesh()
for i in xrange(slices):
for j in xrange(nR-1):
self.meshcontainer.addFace([i*nR+j, (i+1)*nR+j+1, (i+1)*nR+j ])
self.meshcontainer.addFace([i*nR+j, i*nR+j+1, (i+1)*nR+j+1 ])
#############################################################################
if __name__ == "__main__": # if run standalone
mesh = MeshContainer.MeshContainer()
meshgen = MeshGenerator(mesh, sharedgeometry=True)
meshgen.createPlane(LOD=1)
#meshgen.createCube(LOD=1)
#meshgen.createCylinder(0.25, 0.75, LOD=10, end1=True, end2=True)
#meshgen.createSphere(LOD=15)
meshio = MeshIO.MeshIO(mesh)
meshio.toFile("./Plane_4.mesh.xml", overwrite=True)
meshgen.createPlane(LOD=9)
meshio.toFile("./Plane_100.mesh.xml", overwrite=True)
meshgen.createPlane(LOD=31)
meshio.toFile("./Plane_1000.mesh.xml", overwrite=True)
meshgen.createPlane(LOD=99)
meshio.toFile("./Plane_10000.mesh.xml", overwrite=True)
meshgen.createPlane(LOD=316)
meshio.toFile("./Plane_100000.mesh.xml", overwrite=True)