-
Notifications
You must be signed in to change notification settings - Fork 1
/
templates.py
279 lines (234 loc) · 10.3 KB
/
templates.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
'''
Creates trees of relationships, organized hierarchically, and associates the nodes of
the trees with functions (most of them returning a boolean value)
Created on 2012-02-21
@authors:
Sebastien Ouellet [email protected]
'''
import math
import tools
####### Parameters #######
#first_fuz = 0.50
#second_fuz = 0.50
good_threshold = 0.75
acceptable_threshold = 0.50
##########################
class Tree:
""" Basic type of object used to create the trees of relationships.
The structure of those trees organizes knowledge about how the relationships interact,
with a hierarchy of states """
def __init__(self, state=None, test=None, if_true=None, if_false=None):
self.state = state
self.test = test
self.left = if_true
self.right = if_false
def make_directional():
""" Creates trees of directional relationships and outputs them in a list """
left = Tree(test=directional_test, if_true=Tree(state="isRightOf"), if_false=Tree(state="isLeftOf"))
over = Tree(test=directional_test, if_true=Tree(state="isAbove"), if_false=Tree(state="isBelow"))
front = Tree(test=directional_test, if_true=Tree(state="isInFrontOf"), if_false=Tree(state="isBehind"))
return [left, over, front]
def make_other_relationships():
""" Creates a tree of relationships and outputs it """
protruding = Tree(state="protrudesFrom")
containment = Tree(test=contain_test, if_true=Tree(state="contains"), if_false=Tree(state="isContainedBy"))
intersect = Tree(state="intersects", test=protrude_test, if_true=protruding, if_false=containment)
contact = Tree(test=contact_test, if_true=Tree(state="isAdjacentTo"), if_false=Tree())
near = Tree(state="isCloseTo", test=intersect_test, if_true=intersect, if_false=contact)
relational = Tree(test=far_test, if_true=Tree(state="isFarFrom"), if_false=near)
return relational
def calculate_other_relationships(tree, shape1, shape2, depth=10):
""" Takes the information from two shapes and interprets the tree to output a list of
relationships which apply to those shapes """
other_relationships = []
counter = 1
current_tree = tree
while counter <= depth and current_tree.test != None:
if current_tree.test(shape1, shape2):
current_tree = current_tree.left
else:
current_tree = current_tree.right
if current_tree.state != None:
if current_tree.state == "isFarFrom":
other_relationships.append((shape1.name,current_tree.state,shape2.name, far_test(shape1, shape2, membership=1)))
elif current_tree.state == "isCloseTo":
other_relationships.append((shape1.name,current_tree.state,shape2.name, far_test(shape1, shape2, membership=2)))
else:
other_relationships.append((shape1.name,current_tree.state,shape2.name, 1.0))
counter += 1
return other_relationships
def calculate_directions(trees, shape1, shape2):
""" Interprets the tree of directional relationships for two shapes and decides if they are
relevant given the individual contribution of their vector to the total distance """
results = []
i = 0
for direction in trees:
results.append(direction.test(shape1, shape2,i))
i += 1
distances = [math.fabs(value[0]) for value in results]
booleans = [value[1] for value in results]
to_remove = []
for i in xrange(3):
if tools.calculate_side(shape1, i) < tools.calculate_side(shape2, i):
if shape1.bounding_box[1][i] < shape2.bounding_box[1][i] and shape1.bounding_box[0][i] > shape2.bounding_box[0][i]:
to_remove.append(i)
else:
if shape1.bounding_box[1][i] > shape2.bounding_box[1][i] and shape1.bounding_box[0][i] < shape2.bounding_box[0][i]:
to_remove.append(i)
"""
if booleans[i]:
if shape1.bounding_box[0][i] < shape2.bounding_box[1][i]:
to_remove.append(i)
else:
if shape1.bounding_box[1][i] > shape2.bounding_box[0][i]:
to_remove.append(i)
"""
"""
if booleans[i]:
if shape1.bounding_box[1][i] < shape2.bounding_box[1][i] and shape1.bounding_box[0][i] > shape2.bounding_box[0][i]:
to_remove.append(i)
else:
if shape1.bounding_box[1][i] > shape2.bounding_box[1][i] and shape1.bounding_box[0][i] < shape2.bounding_box[0][i]:
to_remove.append(i)
"""
for item in to_remove:
distances[item] = 0.0
#total_length = sum(distances) #Old way
total_length = math.sqrt(sum([distance**2 for distance in distances]))
if total_length != 0:
proportions = []
for direction in distances:
proportions.append(float(direction)/total_length)
#print proportions
props = zip(proportions,trees,booleans)
properties = []
for prop in props:
if prop[0] > acceptable_threshold:
properties.append(prop)
'''
props.sort()
properties = [(props[-1][1],props[-1][2])]
if len(props) > 1:
if props[-2][0] > first_fuz:
properties.append((props[-2][1],props[-2][2]))
if len(props) > 2:
if props[-3][0] > second_fuz:
properties.append((props[-3][1],props[-3][2]))
'''
directional_relationships = []
for property in properties:
if property[2]: # shape 1 is less far along an axe
directional_relationships.append((shape1.name,property[1].left.state,shape2.name, property[0]))
else:
directional_relationships.append((shape1.name,property[1].right.state,shape2.name, property[0]))
return directional_relationships
else:
return []
def directional_test(shape1, shape2, direction):
""" Computes the distance vector from the center of the shapes and returns it with a boolean
indicating the direction of the vector """
center_distance = tools.calculate_absolute_distance_center(shape1, shape2)[direction]
#side_distance = tools.calculate_absolute_distance_sides(shape1, shape2, direction)
if center_distance > 0:
return (center_distance, False)
else:
return (center_distance, True)
def contact_test(shape1, shape2):
""" Checks to see if two shapes are near enough to be called adjacent by comparing the
distance from the center and their size """
# Needs work
return False
sides1 = []
for index in xrange(2):
sides1.append(shape1.bounding_box[1][index] - shape1.bounding_box[0][index])
sides2 = []
for index in xrange(2):
sides2.append(shape2.bounding_box[1][index] - shape2.bounding_box[0][index])
side1 = min(sides1)
side2 = min(sides2)
side = min([side1, side2])
distance = tools.calculate_length(tools.calculate_absolute_distance_center(shape1, shape2))
num_shape1s_in_distance = math.fabs(float(distance) / side)
if num_shape1s_in_distance <= 1.5 and not intersect_test(shape1, shape2):
return True
else:
return False
def contain_test(shape1, shape2):
""" Checks to see which of two shapes is the largest """
if shape1.volume > shape2.volume:
return True
else:
return False
def protrude_test(shape1, shape2):
""" Checks to see if both corners of the bounding box of a shape extends
beyond those of the other shape """
containing = []
for corner in xrange(2):
for side in xrange(3):
containing.append(shape1.bounding_box[corner][side]-shape2.bounding_box[corner][side])
index = 0
first = 0
second = 0
for difference in containing:
if difference >= 0 and index < 3:
first += 1
if difference >= 0 and index > 2:
second += 1
index += 1
if (first == 3 and second == 0) or (first == 0 and second == 3) or shape1.bounding_box == shape2.bounding_box:
return False
else:
return True
def intersect_test(shape1, shape2):
""" Checks to see if the distance vector between the center and the distance vector
between the sides of two shapes are in the opposite direction """
counter = 0
for side in xrange(3):
if tools.calculate_absolute_distance_sides(shape1, shape2, side)*tools.calculate_absolute_distance_center(shape1, shape2)[side] <= 0:
counter += 1
if counter > 2:
return True
else:
return False
def far_value(distance):
return (1/(1+30*math.e**(-7*distance)))
def near_value(distance):
return 1-(1/(1+30*math.e**(-7*distance)))
def far_test(shape1, shape2, membership = 0):
""" Checks to see if the distance is large enough between two shapes to determine they are
far from each other, estimating the possible maximum distance between them as a baseline """
xs = [vertex[0] for vertex in shape1.scene_bounding_box]
ys = [vertex[1] for vertex in shape1.scene_bounding_box]
zs = [vertex[2] for vertex in shape1.scene_bounding_box]
corners = [(x,y,z) for x in xs for y in ys for z in zs]
distances = [tools.calculate_length(((shape1.location[0]-corner[0]), (shape1.location[1]-corner[1]), (shape1.location[2]-corner[2]))) for corner in corners]
maximum_distance = max(distances)
distance = tools.calculate_length(tools.calculate_absolute_distance_center(shape1, shape2))
relative_distance = float(distance)/maximum_distance
value = far_value(relative_distance)
#print shape1.name, value, shape2.name
if membership == 1:
return value
if membership == 2:
return near_value(relative_distance)
if value > acceptable_threshold:
return True
else:
return False
"""
sides1 = []
for index in xrange(2):
sides1.append(shape1.bounding_box[1][index] - shape1.bounding_box[0][index])
sides2 = []
for index in xrange(2):
sides2.append(shape2.bounding_box[1][index] - shape2.bounding_box[0][index])
side1 = sum(sides1)/3.0
side2 = sum(sides2)/3.0
side = (side1+side2)/2.0
distance = tools.calculate_length(tools.calculate_absolute_distance_center(shape1, shape2))
num_shape1s_in_distance = math.fabs(distance / side)
if num_shape1s_in_distance>=4:
return True
else:
return False
"""