-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathline.py
238 lines (164 loc) · 8.48 KB
/
line.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
import math
import extras
def calc_points(shapes):
warning_points = {}
for shape in shapes:
if shape['angle'] is not None:
points = shape['vertices']
cornice_points = []
valid_cornise = False
cornice_points = []
for line in shape['lines']:
if line['type'] == 'cornice':
if line['points'][0]['z'] is not None and \
line['points'][1]['z'] is not None:
valid_cornise = line
for point in line['points']:
cornice_points.append(point['id'])
if line['type'] == 'gable':
valid_cornise = None
if valid_cornise is None or not valid_cornise:
continue
x1 = valid_cornise['points'][0]['x']
y1 = valid_cornise['points'][0]['y']
x2 = valid_cornise['points'][1]['x']
y2 = valid_cornise['points'][1]['y']
if x2 - x1 == 0:
m = 0
else:
m = (y2 - y1) / (x2 - x1)
b = y1 - m * x1
for line in shape['lines']:
for point in line['points']:
if point['id'] not in cornice_points:
x3 = point['x']
y3 = point['y']
length_plan = abs(y3 + x3 * m + b) / math.sqrt(m * m + 1)
if line['points'][0]['z'] is None:
line['points'][0]['z'] = length_plan * math.tan(math.radians(shape['angle'])) + \
valid_cornise['points'][0]['z']
else:
line['points'][1]['z'] = length_plan * math.tan(math.radians(shape['angle'])) + \
valid_cornise['points'][0]['z']
return shapes
def set_vertices(shapes):
vertices_points = extras.extract_vertices(shapes)
for i, shape in enumerate(shapes):
for j, line in enumerate(shape['lines']):
for k, point in enumerate(line['points']):
if point['id'] in vertices_points.keys() and point['z'] is None:
shapes[i]['lines'][j]['points'][k]['z'] = vertices_points[point['id']]
return shapes
def set_cornice(shapes):
for i, shape in enumerate(shapes):
valid_cornise = True
for line in shape['lines']:
if line['type'] == 'gable' or line['type'] == 'roof_fracture':
valid_cornise = False
if valid_cornise:
cornice_points = []
for line in shape['lines']:
if line['type'] == 'cornice':
for point in line['points']:
cornice_points.append(point['id'])
cornice_height = 0
for point in shape['vertices']:
if point['id'] in cornice_points:
if point['z'] is not None:
cornice_height = point['z']
break
for j, point in enumerate(shape['vertices']):
if point['id'] in cornice_points:
shapes[i]['vertices'][j]['z'] = cornice_height
return shapes
def calc_lines(shapes):
lines = extras.extract_lines(shapes)
warnings_lines = {}
"""for id, line in lines.items():
for i, point in enumerate(line['points']):
if point['id'] in vertices_points.keys():
lines[id]['points'][i]['z'] = vertices_points[point['id']]"""
for i, shape in enumerate(shapes):
for j, line in enumerate(shape['lines']):
shapes[i]['lines'][j]['line_height'] = None
if is_valid(line):
shapes[i]['lines'][j] = calc_line(line)
#shapes = extras.set_points(shapes)
else:
if shape['id'] in warnings_lines.keys():
warnings_lines[shape['id']].append(line['id'])
else:
warnings_lines[shape['id']] = [line['id']]
lines = list(extras.extract_lines(shapes).values())
points = list(extras.exact_coords(lines).values())
for i, shape in enumerate(shapes):
for j, point in enumerate(shape['vertices']):
for solved_point in points:
if point['id'] == solved_point['id']:
shapes[i]['vertices'][j]['z'] = solved_point['z']
return shapes, warnings_lines
def is_valid(line):
"""
Checks whether the given line can be solved
:param line: dict
:return: bool
"""
if line['type'] == 'edge' or line['type'] == 'endova' or \
line['type'] == 'gable' or line['type'] == 'roof_fracture':
if line['points'][0]['z'] is not None or line['points'][1]['z'] is not None:
# Line has all the coordinates
if line['points'][0]['z'] is not None and line['points'][1]['z'] is not None:
return True
# Line has both lengths
if line['length_real'] is not None and line['length_plan'] is not None:
return True
# Line has an angle and at least one height
if line['angle']:
return True
elif line['type'] == 'skate' or line['type'] == 'cornice':
if line['points'][0]['z'] is not None or line['points'][1]['z'] is not None:
return True
return False
def calc_line(line):
"""
Solves the given line(finds real and plan length, angle and coords of top)
:param line: dict
:return: dict
"""
if line['type'] == 'edge' or line['type'] == 'endova' or \
line['type'] == 'gable' or line['type'] == 'roof_fracture':
if line['angle']:
line['length_plan'] = math.sqrt(math.pow(line['points'][0]['x'] - line['points'][1]['x'], 2) + \
math.pow(line['points'][0]['y'] - line['points'][1]['y'], 2))
line['length_real'] = line['length_plan'] / math.cos(math.radians(line['angle']))
line['line_height'] = abs(line['length_plan'] * math.tan(line['angle']))
elif line['length_plan'] is not None and line['length_real'] is not None:
line['angle'] = math.degrees(math.acos(line['length_plan'] / line['length_real']))
line['length_plan'] = math.sqrt(math.pow(line['points'][0]['x'] - line['points'][1]['x'], 2) + \
math.pow(line['points'][0]['y'] - line['points'][1]['y'], 2))
line['length_real'] = line['length_plan'] / math.cos(math.radians(line['angle']))
line['line_height'] = abs(line['length_plan'] * math.tan(line['angle']))
elif line['points'][0]['z'] is not None and line['points'][1]['z'] is not None:
length_real = math.sqrt(math.pow(line['points'][0]['x'] - line['points'][1]['x'], 2) + \
math.pow(line['points'][0]['y'] - line['points'][1]['y'], 2) + \
math.pow(line['points'][0]['z'] - line['points'][1]['z'], 2))
if line['points'][0]['z'] > line['points'][1]['z']:
line['angle'] = abs(math.degrees(math.atan2(1, (line['points'][0]['z'] - line['points'][1]['z']) \
/ length_real)))
else:
line['angle'] = abs(math.degrees(math.atan2(1, (line['points'][1]['z'] - line['points'][0]['z']) \
/ length_real)))
line['length_plan'] = math.sqrt(math.pow(line['points'][0]['x'] - line['points'][1]['x'], 2) + \
math.pow(line['points'][0]['y'] - line['points'][1]['y'], 2))
line['length_real'] = line['length_plan'] / math.cos(math.radians(line['angle']))
#line['line_height'] = abs(line['length_plan'] * math.tan(line['angle']))
elif line['type'] == 'cornice' or line['type'] == 'skate':
line['angle'] = 0
line['length_plan'] = math.sqrt(math.pow(line['points'][0]['x'] - line['points'][1]['x'], 2) + \
math.pow(line['points'][0]['y'] - line['points'][1]['y'], 2))
line['length_real'] = line['length_plan']
if line['points'][0]['z'] is not None:
line['points'][1]['z'] = line['points'][0]['z']
elif line['points'][1]['z'] is not None:
line['points'][0]['z'] = line['points'][1]['z']
return line