forked from volsn/roof-calculation-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroof_calc_api.py
265 lines (194 loc) · 8.26 KB
/
roof_calc_api.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
from flask import Flask, jsonify, request
from flask_restful import Resource, Api
import polygon
import side
import extras
import copy
import math
#from flask_cors import CORS
import pprint
PP = pprint.PrettyPrinter(indent=2)
app = Flask(__name__)
#cors = CORS(app)
api = Api(app)
def parse_holes(shapes):
for shape in shapes:
if shape['is_hole'] == True:
for line in shapes['line']:
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']
shape[square] = polygon.calc_square(shape, angle=0)
return shapes
def parse_min_shapes(shapes):
def parse_shapes(shapes):
"""
Function that parses shape array fulfilling all the data about shpape where it is possible
:param shapes: list
:return: list
"""
for shape in shapes:
if polygon.is_shape_valid(shape) and shape['is_hole'] != False:
if shape['angle'] is None:
shape['angle'] = polygon.calc_angle(shape)
shape['square'] = polygon.calc_square(shape, shape['angle'])
return shapes
def parse_lines(shapes):
"""
Function that parses lines array fulfilling all the data about line where it is possible
:param shapes: list
:return: list
"""
lines = list(extras.exact_lines(shapes).values()) # a dict containing information about all the lines
lines_to_check = [] # list of lines that are about to be solved
checked_lines = [] # list of lines that are already solved
lines_to_check = side.check_lines(lines, checked_lines)
while len(lines_to_check) != 0:
print(lines_to_check)
for id in lines_to_check:
i = extras.find_element_by_id(id, lines)
checked_line = side.solve_line(lines[i])
checked_lines.append(id)
lines[i] = checked_line
points = extras.exact_coords(lines)
for line in lines:
if line['id'] not in checked_lines:
if checked_line['points'][0]['z'] is not None:
if line['points'][0]['id'] == checked_line['points'][0]['id']:
line['points'][0] = checked_line['points'][0]
if line['points'][1]['id'] == checked_line['points'][0]['id']:
line['points'][1] = checked_line['points'][0]
if checked_line['points'][1]['z'] is not None:
if line['points'][0]['id'] == checked_line['points'][1]['id']:
line['points'][0] = checked_line['points'][1]
if line['points'][1]['id'] == checked_line['points'][1]['id']:
line['points'][1] = checked_line['points'][1]
lines_to_check = side.check_lines(lines, checked_lines)
# Set values of lines to shapes
for shape in shapes:
answer = []
for line in shape['lines']:
answer.append(lines[
extras.find_element_by_id(line['id'], lines)
])
shape['lines'] = answer
return shapes
def parse_points(shapes):
lines = list(extras.exact_lines(shapes).values())
cornice_points = []
cornice_height = 0
# Setting cornice height
for line in lines:
if line['type'] == 'cornice':
for point in line['points']:
cornice_points.append(point['id'])
if point['z'] is not None:
cornice_height = point['z']
for line in lines:
for point in line['points']:
if point['id'] in cornice_points:
point['z'] = cornice_height
for shape in shapes:
answer = []
for line in shape['lines']:
answer.append(lines[
extras.find_element_by_id(line['id'], lines)
])
shape['lines'] = answer
for shape in shapes:
if shape['angle'] is not None:
not_vertical_line = None
vertical_line = None
for line in shape['lines']:
if line['type'] != 'cornice':
not_vertical_line = line
for line in shape['lines']:
if line['type'] == 'cornice':
vertical_line = line
x1 = vertical_line['points'][0]['x']
y1 = vertical_line['points'][0]['y']
x2 = vertical_line['points'][1]['x']
y2 = vertical_line['points'][1]['y']
if not_vertical_line['points'][0]['z'] is None:
x3 = not_vertical_line['points'][0]['x']
y3 = not_vertical_line['points'][0]['y']
else:
x3 = not_vertical_line['points'][1]['x']
y3 = not_vertical_line['points'][1]['y']
px = x2 - x1
py = y2 - y1
dab = px * px + py * py
u = ((x3 - x1) * px + (y3-y1) * py) / dab
x = x1 + u * px
y = y1 + u * py
length_plan = math.sqrt((x3 - x) * (x3 - x) + (y3 - y) * (y3 - y))
if not_vertical_line['points'][0]['z'] is None:
not_vertical_line['points'][0]['z'] = length_plan * math.tan(math.radians(shape['angle'])) + \
not_vertical_line['points'][1]['z']
else:
not_vertical_line['points'][1]['z'] = length_plan * math.tan(math.radians(shape['angle'])) + \
not_vertical_line['points'][0]['z']
for line in lines:
if line['id'] == not_vertical_line['id']:
lines[
extras.find_element_by_id(line['id'], lines)
] = not_vertical_line
return shapes
def calc_real_length(shapes_orig, shapes_solved):
lines_orig = list(extras.exact_lines(shapes_orig).values())
lines_solved = list(extras.exact_lines(shapes_solved).values())
koefficient = 1
for line in lines_orig:
if line['length_real'] is not None:
id = line['id']
line_num = extras.find_element_by_id(id, lines_solved)
koefficient = line['length_real'] / lines_solved[line_num]['length_real']
elif line['length_plan'] is not None:
id = line['id']
line_num = extras.find_element_by_id(id, lines_solved)
koefficient = line['length_plan'] / lines_solved[line_num]['length_plan']
if koefficient == 1:
for shape in shapes_orig:
if shape['square'] is not None:
id = shape['id']
shape_num = extras.find_element_by_id(id, shapes_solved)
koefficient = math.sqrt(shape['square'] / shapes_solved[shape_num]['square'])
break
for line in lines_solved:
line['length_real'] *= koefficient
line['length_plan'] *= koefficient
# Set koefficient to calculate real shapes lengths
koefficient = koefficient * koefficient
for shape in shapes_solved:
answer = []
for line in shape['lines']:
answer.append(lines_solved[
extras.find_element_by_id(line['id'], lines_solved)
])
shape['lines'] = answer
if shape['square'] is not None:
shape['square'] *= koefficient
return shapes_solved
def _is_shapes_valid(shapes):
for shape in shapes:
if shape['angle'] is not None:
return True
return False
class Index(Resource):
def post(self):
json = request.json
original = copy.deepcopy(json['shapes'])
json['shapes'] = parse_points(json['shapes'])
json['shapes'] = parse_lines(json['shapes'])
json['shapes'] = parse_holes(json['shapes'])
if _is_shapes_valid(json['shapes']):
json['shapes'] = parse_shapes(json['shapes'])
else:
json['shapes'] = parse_min_shapes(json['shapes'])
json['shapes'] = calc_real_length(original, json['shapes'])
return jsonify(json)
def get(self):
return jsonify({'message': 'Hello, world!'})
api.add_resource(Index, '/')
if __name__ == '__main__':
app.run(debug=True, host='127.0.0.1', port='5000')