forked from Zebreu/mist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
131 lines (109 loc) · 5.49 KB
/
database.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
'''
Stores and manipulates the relationships computed
Created on 2012-03-18
@authors:
Sebastien Ouellet [email protected]
'''
import templates
import tools
proximity_threshold = 0.90
good_threshold = 0.75
class RelationshipsTable:
""" Database that includes a container for the relationships and functions that can
be applied to the database to search through it """
def __init__(self):
self.shapes = []
self.relationships = []
self.relevant = []
self.vectors = []
def fill_vectors(self):
""" Keeps a vector from the center of two shapes with the shape information, and the distance """
for shape in self.shapes:
for shape2 in self.shapes:
if shape != shape2:
vector = tools.calculate_absolute_distance_center(shape, shape2)
distance = tools.calculate_length(vector)
self.vectors.append((shape,shape2,shape.name, shape2.name, vector,distance,shape.bounding_box[1][1]-shape.bounding_box[0][1]))
def search_table(self, name1 = None, relation = None, name2 = None, mode = 0):
""" Search the table according to the mode entered:
0: the name of an object
1: the relationship
2: an object and the relationship
3: two objects and the relationship
4: two objects """
found_relationships = []
if mode == 0:
for relationship in self.relationships:
if relationship[0] == name1:
found_relationships.append(relationship)
if mode == 4:
for relationship in self.relationships:
if relationship[0] == name1 and relationship[2] == name2:
found_relationships.append(relationship)
if mode == 1:
for relationship in self.relationships:
if relationship[1] == relation:
found_relationships.append(relationship)
if mode == 2:
for relationship in self.relationships:
if relationship[0] == name1 and relationship[1] == relation:
found_relationships.append(relationship)
if mode == 3:
for relationship in self.relationships:
if relationship[0] == name1 and relationship[1] == relation and relationship[2] == name2:
found_relationships.append(relationship)
return found_relationships
def distance_find_relevant(self, shape1, relation):
distance_relationships = (self.search_table(shape1.name, relation, mode = 2))
for relationship in distance_relationships:
if relationship[3] > good_threshold:
self.relevant.append((shape1.name, relation, relationship[2], relationship[3]))
def find_relevant(self, shape1, relation):
""" Fills a list with the relationships deemed most relevant to the scene """
found_relationships = self.search_table(shape1.name, relation, mode = 2)
distance_relationships = []
for relationship in found_relationships:
distance_relationships.extend(self.search_table(shape1.name, "isCloseTo", relationship[2], mode = 3))
distance_relationships.extend(self.search_table(shape1.name, "isFarFrom", relationship[2], mode = 3))
if len(found_relationships) == 0:
return 0
#shape_names = [relationship[2] for relationship in found_relationships]
#print found_relationships
#print distance_relationships
products = []
i = 0
while i < len(found_relationships):
if distance_relationships[i][1] == "isFarFrom":
products.append((found_relationships[i][3]*(1-distance_relationships[i][3]), found_relationships[i][2]))
else:
products.append((found_relationships[i][3]*(distance_relationships[i][3]), found_relationships[i][2]))
i += 1
reference_name = max(products)[1]
'''
reference = None
for shape in self.shapes:
if shape.name == reference_name:
reference = shape
if reference == None:
return 0
'''
relation_rating = self.search_table(shape1.name, relation, reference_name, 3)[0][3]
self.relevant.append((shape1.name, relation, reference_name, relation_rating))
new_relationships = self.search_table(reference_name, "isCloseTo", mode=2)
for relationship in new_relationships:
if relationship[3] > proximity_threshold and relationship[2] != shape1.name:
relation_rating_other = self.search_table(shape1.name, relation, relationship[2], 3)
if len(relation_rating_other) == 0:
relation_rating_other = relation_rating
else:
relation_rating_other = relation_rating_other[0][3]
self.relevant.append((shape1.name, relation, relationship[2], relation_rating_other))
"""
for shape in self.shapes:
if shape != shape1:
other_shapes.append((shape, )
"""
def complete_relevant(self,shape1):
self.relevant.extend(self.search_table(shape1.name, "isContainedBy", mode= 2))
self.relevant.extend(self.search_table(shape1.name, "contains", mode=2))
self.relevant.extend(self.search_table(shape1.name, "protrudesFrom", mode=2))