-
Notifications
You must be signed in to change notification settings - Fork 1
/
geometry.py
209 lines (187 loc) · 7.28 KB
/
geometry.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
'''
Transforms the data parsed from the file into geometrical representations of the objects.
Implements:
- IndexedFaceSet (used by Blender export function)
- Cone
- Box
- Sphere
- Cylinder
- Transform translation
Created on 2012-02-20
@authors:
Sebastien Ouellet [email protected]
'''
import tools
from math import *
class Transform:
""" Stores all the information parsed from the file about the transform node for a shape """
def __init__(self):
self.transformations = []
self.values = []
self.name = None
class Shape:
""" Stores all the information parsed from the file for a shape and
computes a few numerical attributes """
def __init__(self, transform=Transform()):
self.transform = transform
self.attributes = []
self.attributes_values = []
self.child_names = []
self.child_nodes = []
self.type_id = []
self.type_values = []
self.location = [0,0,0]
self.name = "None"
self.type = None
self.bounding_box = None
self.scale = 1
self.radius = 1
self.height = 2
self.size = [2,2,2]
self.bottomRadius = 1
self.pointCoordinate = []
self.volume = 1
self.scene_bounding_box = None
def update_shape(self):
""" Takes information from the parser to find numerical values
for the location and size of objects """
### Updating all shapes
for attribute in zip(self.transform.transformations,self.transform.values):
if attribute[0] == "translation":
self.location = read_values(attribute)
"""
if attribute[0] ==
"""
### Updating according to type
if self.type == "Box":
for attribute in zip(self.type_id,self.type_values):
if attribute[0] == "size":
self.size = read_values(attribute)
if self.type == "IndexedFaceSet" > 0:
strings_list = self.pointCoordinate.split(" ")
self.pointCoordinate = []
index = 0
vertex = []
for point in strings_list:
if len(point) > 0:
if point[-1] == ",":
vertex.append(eval(point[:-1]))
else:
vertex.append(eval(point))
if index == 2:
self.pointCoordinate.append(vertex)
vertex = []
index = 0
else:
index += 1
if self.type == "Sphere":
for attribute in zip(self.type_id,self.type_values):
if attribute[0] == "radius":
self.radius = eval(attribute[1])
if self.type == "Cone":
for attribute in zip(self.type_id,self.type_values):
if attribute[0] == "bottomRadius":
self.bottomRadius = eval(attribute[1])
if attribute[0] == "height":
self.height = eval(attribute[1])
if self.type == "Cylinder":
for attribute in zip(self.type_id,self.type_values):
if attribute[0] == "radius":
self.radius = eval(attribute[1])
if attribute[0] == "height":
self.height = eval(attribute[1])
def calculate_bounding_box(self):
""" Computes a bounding box in the form [min_vertex, max_vertex] """
if self.type == "Box":
minCorner = list(self.location)
maxCorner = list(self.location)
a = zip(self.size,maxCorner)
newMaxCorner = []
for value in a:
newMaxCorner.append(value[1] + value[0]/2.0)
b = zip(self.size,minCorner)
newMinCorner = []
for value in b:
newMinCorner.append(value[1]-value[0]/2.0)
self.bounding_box = [newMinCorner, newMaxCorner]
elif self.type == "Sphere":
minCorner = list(self.location)
maxCorner = list(self.location)
newMaxCorner = []
for value in maxCorner:
newMaxCorner.append(value + self.radius)
newMinCorner = []
for value in minCorner:
newMinCorner.append(value - self.radius)
self.bounding_box = [newMinCorner, newMaxCorner]
elif self.type == "Cone":
minCorner = list(self.location)
maxCorner = list(self.location)
newMaxCorner = []
count = 0
for value in maxCorner:
count +=1
if count == 2:
newMaxCorner.append(value + self.height/2.0)
else:
newMaxCorner.append(value + self.bottomRadius)
newMinCorner = []
count = 0
for value in minCorner:
count +=1
if count == 2:
newMinCorner.append(value - self.height/2.0)
else:
newMinCorner.append(value - self.bottomRadius)
self.bounding_box = [newMinCorner, newMaxCorner]
elif self.type == "Cylinder":
minCorner = list(self.location)
maxCorner = list(self.location)
newMaxCorner = []
count = 0
for value in maxCorner:
count +=1
if count == 2:
newMaxCorner.append(value + self.height/2.0)
else:
newMaxCorner.append(value + self.radius)
newMinCorner = []
count = 0
for value in minCorner:
count +=1
if count == 2:
newMinCorner.append(value - self.height/2.0)
else:
newMinCorner.append(value - self.radius)
self.bounding_box = [newMinCorner, newMaxCorner]
elif self.type == "IndexedFaceSet":
minCorner = []
maxCorner = []
for i in xrange(3):
axevertices = [vertex[i] for vertex in self.pointCoordinate]
minCorner.append(self.location[i]+min(axevertices))
maxCorner.append(self.location[i]+max(axevertices))
self.bounding_box = [minCorner, maxCorner]
tools.calculate_volume(self)
"""
### Update all shapes
for attribute in zip(self.transform.transformations,self.transform.values):
if attribute[0] == "rotation":
rotation_matrix = []
values = read_values(attribute)
vector = values[:3]
angle = values[3]
length = x3d_tools.calculate_length(vector)
vector = [axe/length for axe in vector]
line0 = [cos(angle)+(vector[1]**2)*(1-cos(angle)), vector[0]*vector[1]*(1-cos(angle))-(vector[2]*sin(angle)), vector[0]*vector[2]*(1-cos(angle))-(vector[1]*sin(angle))
"""
def oriented_bouding_box(self):
""" Not used/Incomplete """
pass
def read_values(attribute):
""" Takes a strings and outputs a list of numerical values taken from the string """
strings_list = attribute[1].split(" ")
values = []
for value in strings_list:
values.append(eval(value))
return values