-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprimitives.py
More file actions
240 lines (208 loc) · 10.2 KB
/
primitives.py
File metadata and controls
240 lines (208 loc) · 10.2 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
# Asset Generator
# Copyright (C) <2015> <Sebastian Schmidt>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from collections import defaultdict
import numpy as np
import baseclasses
class Cuboid(baseclasses.BasePrimitive, baseclasses.Brush):
def __init__(self, center, size, texture="common/caulk"):
"""
\brief Generate a cuboid
\param center center of the cuboid
\param size size of the cuboid
\param texture texture of the cuboid as string (applied to all faces)
or as a dictionary with 'top','front' etc for individual faces
"""
super().__init__(center, size)
if isinstance(texture, str):
self.texture = defaultdict(lambda: texture)
else:
self.texture = defaultdict(lambda: "common/caulk", texture)
@property
def verticies(self):
"""
\brief Returns a list of all verticies, starting in the right bottom
corner of the front face, going clockwise and then the back face
"""
# TODO: rotation
basecuboid = np.array([[-0.5, -0.5, -0.5], [-0.5, 0.5, -0.5],
[-0.5, 0.5, 0.5], [-0.5, -0.5, 0.5],
[0.5, -0.5, -0.5], [0.5, 0.5, -0.5],
[0.5, 0.5, 0.5], [0.5, -0.5, 0.5]])
return self.center + self.size*basecuboid
@property
def faces(self):
"""
\brief Return the faces described by 3 verticies and the texture
order: front, back, right, left, bottom, top
"""
verts = self.verticies
return [baseclasses.Face(verts[0], verts[1], verts[3],
self.texture["front"]),
baseclasses.Face(verts[5], verts[4], verts[6],
self.texture["back"]),
baseclasses.Face(verts[4], verts[0], verts[7],
self.texture["right"]),
baseclasses.Face(verts[1], verts[5], verts[2],
self.texture["left"]),
baseclasses.Face(verts[4], verts[5], verts[0],
self.texture["bottom"]),
baseclasses.Face(verts[3], verts[2], verts[7],
self.texture["top"])]
class TruncatedConeBrush(baseclasses.BasePrimitive, baseclasses.Brush):
def __init__(self, center, radius, height, radius2=0, truncation_ratio=0,
numSides=16, texture="common/caulk"):
"""
\brief Generate a (truncated) cone with numSides sides
\param center center of the cone
\param radius radius of the base area
\param height height of the cone
\param radius2 if != 0 generate a cone with eliptical base area
\param truncation_ratio ratio of the radii of top to bottom face
0 means no truncation at all
\param numSides number of sides of the cone
\param texture texture of the cone as string (applied to all faces)
or as a dictionary('top', 'bottom' and 'sides') for individual faces
"""
size = np.array([2*radius, 2*radius2, height], dtype=np.float)
super().__init__(center, size)
self.truncation_ratio = truncation_ratio
self.numSides = numSides
if isinstance(texture, str):
self.texture = defaultdict(lambda: texture)
else:
self.texture = defaultdict(lambda: "common/caulk", texture)
@property
def size(self):
rad2 = self.radius2 if self.radius2 else self.radius
return np.array([2*self.radius, 2*rad2, self.height],
dtype=np.float)
@size.setter
def size(self, value):
self.radius = value[0]/2
self.radius2 = value[1]/2
self.height = value[2]
@property
def truncation_ratio(self):
return self._truncation_ratio
@truncation_ratio.setter
def truncation_ratio(self, value):
if value > 1 or value < 0:
raise ValueError("Truncation ratio must be between 0 and 1")
self._truncation_ratio = value
@property
def faces(self):
faces = []
rad2 = self.radius2 if self.radius2 else self.radius
ratio = self.truncation_ratio
# sides
angle = 2*np.pi/self.numSides
for i in range(self.numSides):
v0 = self.center + np.array([self.radius*np.cos((i+1)*angle),
rad2*np.sin((i+1)*angle),
-self.height/2], dtype=np.float)
v1 = self.center + np.array([self.radius*np.cos((i)*angle),
rad2*np.sin((i)*angle),
-self.height/2], dtype=np.float)
v2 = self.center + np.array([ratio*self.radius*np.cos((i+1)*angle),
ratio*rad2*np.sin((i+1)*angle),
+self.height/2], dtype=np.float)
faces.append(baseclasses.Face(v0, v1, v2, self.texture["sides"]))
# top - only if the truncation_ratio is not 0
if ratio:
v0 = self.center + np.array([self.radius, -self.radius,
self.height/2], dtype=np.float)
v1 = self.center + np.array([-self.radius, -self.radius,
self.height/2], dtype=np.float)
v2 = self.center + np.array([self.radius, self.radius,
self.height/2], dtype=np.float)
faces.append(baseclasses.Face(v0, v1, v2, self.texture["top"]))
# bottom
v0 = self.center + np.array([self.radius, self.radius,
-self.height/2], dtype=np.float)
v1 = self.center + np.array([-self.radius, self.radius,
-self.height/2], dtype=np.float)
v2 = self.center + np.array([self.radius, -self.radius,
-self.height/2], dtype=np.float)
faces.append(baseclasses.Face(v0, v1, v2, self.texture["bottom"]))
return faces
class CylinderBrush(TruncatedConeBrush):
def __init__(self, center, radius, height, radius2=0, numSides=16,
texture="common/caulk"):
"""
\brief Generate a Cylinder with numSides sides
\param center center of the cylinder
\param radius radius of the base area
\param height height of the cylinder
\param radius2 if != 0 generate a cylinder with eliptical base area
\param numSides number of sides of the cylinder
\param texture texture of the cylinder as string (applied to all faces)
or as a dictionary('top', 'bottom' and 'sides') for individual faces
"""
super().__init__(center, radius, height, radius2, 1, numSides, texture)
@property
def truncation_ratio(self):
return 1
@truncation_ratio.setter
def truncation_ratio(self, value):
pass
class EllipsoidBrush(baseclasses.BasePrimitive, baseclasses.Brush):
def __init__(self, center, size, numSegments=16, numRings=16,
texture="common/caulk"):
super().__init__(center, size)
self.numSegments = numSegments
self.numRings = numRings
self.texture = texture
@property
def faces(self):
faces = []
segmentAngle = 2*np.pi/self.numSegments
ringAngle = np.pi/self.numRings
for i_seg in range(self.numSegments):
# faces of the lowest ring have to be computed differently
mu_i = -np.pi/2
nu_i = i_seg*segmentAngle
mu_ip1 = ringAngle - np.pi/2
nu_ip1 = (i_seg+1)*segmentAngle
v0_normalized = np.array([np.cos(mu_ip1)*np.cos(nu_ip1),
np.cos(mu_ip1)*np.sin(nu_ip1),
np.sin(mu_ip1)], dtype=np.float)
v1_normalized = np.array([np.cos(mu_i)*np.cos(nu_ip1),
np.cos(mu_i)*np.sin(nu_ip1),
np.sin(mu_i)], dtype=np.float)
v2_normalized = np.array([np.cos(mu_ip1)*np.cos(nu_i),
np.cos(mu_ip1)*np.sin(nu_i),
np.sin(mu_ip1)], dtype=np.float)
v0 = v0_normalized*self.size/2 + self.center
v1 = v1_normalized*self.size/2 + self.center
v2 = v2_normalized*self.size/2 + self.center
faces.append(baseclasses.Face(v0, v1, v2, self.texture))
# rest of the rings
for i_ring in range(1, self.numRings):
mu_i = i_ring*ringAngle - np.pi/2
nu_i = i_seg*segmentAngle
mu_ip1 = (i_ring+1)*ringAngle - np.pi/2
nu_ip1 = (i_seg+1)*segmentAngle
v0_normalized = np.array([np.cos(mu_i)*np.cos(nu_ip1),
np.cos(mu_i)*np.sin(nu_ip1),
np.sin(mu_i)], dtype=np.float)
v1_normalized = np.array([np.cos(mu_i)*np.cos(nu_i),
np.cos(mu_i)*np.sin(nu_i),
np.sin(mu_i)], dtype=np.float)
v2_normalized = np.array([np.cos(mu_ip1)*np.cos(nu_ip1),
np.cos(mu_ip1)*np.sin(nu_ip1),
np.sin(mu_ip1)], dtype=np.float)
v0 = v0_normalized*self.size/2 + self.center
v1 = v1_normalized*self.size/2 + self.center
v2 = v2_normalized*self.size/2 + self.center
faces.append(baseclasses.Face(v0, v1, v2, self.texture))
return faces