-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangulation.py
407 lines (315 loc) · 15.5 KB
/
triangulation.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import numpy as np
class Face:
def __init__(self, edge, children, parent):
self.edge = edge
self.children = children
self.parent = parent
class Edge:
def __init__(self, origin, face=None, next=None, previous=None):
self.origin = origin
self.face = face
self.next = next
self.previous = previous
class Point:
def __init__(self, x, y, edge=None):
self.x = x
self.y = y
self.edge = edge
def add(self, point):
return Point(self.x + point.x, self.y + point.y)
def subtract(self, point):
return Point(self.x - point.x, self.y - point.y)
def det(self, point):
return self.x * point.y - self.y * point.x
def sign(self, point1, point2):
return self.subtract(point2).det(point1.subtract(point2))
def is_inside_triangle(self, triangle):
a = triangle[0]
b = triangle[1]
c = triangle[2]
sign1 = self.sign(a, b)
sign2 = self.sign(b, c)
sign3 = self.sign(c, a)
has_neg = sign1 < 0 or sign2 < 0 or sign3 < 0
has_pos = sign1 > 0 or sign2 > 0 or sign3 > 0
return not (has_neg and has_pos)
def is_inside_circle(self, circle_points):
circle_points = sort_counter_clockwise(circle_points)
point1 = Point(circle_points[0].x, circle_points[0].y)
point2 = Point(circle_points[1].x, circle_points[1].y)
point3 = Point(circle_points[2].x, circle_points[2].y)
matrix = np.array([
[point1.x - self.x, point2.x - self.x, point3.x - self.x],
[point1.y - self.y, point2.y - self.y, point3.y - self.y],
[(point1.x - self.x) ** 2 + (point1.y - self.y) ** 2,
(point2.x - self.x) ** 2 + (point2.y - self.y) ** 2,
(point3.x - self.x) ** 2 + (point3.y - self.y) ** 2]], dtype='float')
return np.linalg.det(matrix) >= 0
def is_adjacent(self, a, b, vertices):
return (is_equal(a, vertices[0]) or is_equal(a, vertices[1]) or is_equal(a, vertices[2])) \
and (is_equal(b, vertices[0]) or is_equal(b, vertices[1]) or is_equal(b, vertices[2])) \
and (not is_equal(self, vertices[0]) and not is_equal(self, vertices[1])
and not is_equal(self, vertices[2]) and not is_line(vertices))
def is_line(triangle):
vector1 = triangle[1].subtract(triangle[0])
vector2 = triangle[2].subtract(triangle[0])
det = vector1.det(vector2)
return det == 0
def is_equal(point1, point2):
return point1.x == point2.x and point1.y == point2.y
def is_left(a, b, c):
vector1 = b.subtract(a)
vector2 = c.subtract(a)
return vector1.det(vector2) >= 0
# def is_exterior(triangle, points, boundaries):
# vertices = get_points(triangle)
# if boundaries is not None:
# for i in range(0, len(boundaries)):
# for j in range(0, len(boundaries[i])):
# boundary_point = points[boundaries[i][j]]
# if is_equal(boundary_point, vertices[0]) or is_equal(boundary_point, vertices[1]) or is_equal(
# boundary_point, vertices[2]):
# if len(boundaries[i]) < 3:
# return True
#
# if is_equal(boundary_point, vertices[0]):
# point1 = vertices[1]
# point2 = vertices[2]
# elif is_equal(boundary_point, vertices[1]):
# point1 = vertices[0]
# point2 = vertices[2]
# else:
# point1 = vertices[0]
# point2 = vertices[1]
#
# if j == 0:
# previous_point = points[boundaries[i][len(boundaries[i]) - 1]]
# else:
# previous_point = points[boundaries[i][j - 1]]
#
# if j == len(boundaries[i]) - 1:
# next_point = points[boundaries[i][0]]
# else:
# next_point = points[boundaries[i][j + 1]]
#
# if is_left(previous_point, boundary_point, next_point):
# if is_left(previous_point, boundary_point, point1) \
# and is_left(previous_point, boundary_point, point2) \
# and is_left(boundary_point, next_point, point1) \
# and is_left(boundary_point, next_point, point2):
# return False
# else:
# return True
# else:
# if (is_left(previous_point, boundary_point, point1)
# or is_left(boundary_point, next_point, point1)) \
# and (is_left(previous_point, boundary_point, point2)
# or is_left(boundary_point, next_point, point2)):
# return False
# else:
# return True
# return False
def is_enclosing(triangle, enclosing_points):
for i in range(0, len(enclosing_points)):
if is_equal(triangle[0], enclosing_points[i]) or is_equal(triangle[1], enclosing_points[i]) \
or is_equal(triangle[2], enclosing_points[i]):
return True
return False
def sort_counter_clockwise(circle_points):
vector12 = circle_points[1].subtract(circle_points[0])
vector13 = circle_points[2].subtract(circle_points[0])
det = vector12.det(vector13)
if det < 0:
circle_points[0], circle_points[2] = circle_points[2], circle_points[0]
return circle_points
def get_points(face):
a = face.edge.previous.origin
b = face.edge.origin
c = face.edge.next.origin
return [a, b, c]
def find_triangle(face, point):
triangle_list = []
if not point.is_inside_triangle(get_points(face)):
return triangle_list
if len(face.children) < 1:
return [face]
for i in range(0, len(face.children)):
if face.children[i] is not None:
child = face.children[i]
triangle = find_triangle(child, point)
for j in range(0, len(triangle)):
triangle_list += [triangle[j]]
return triangle_list
def compute_triangulation(points, vertices=None, edges=None, faces=None):
if vertices is None:
vertices = []
if edges is None:
edges = []
if faces is None:
faces =[]
min_x = -1
min_y = -1
max_x = 1024
max_y = 1024
# min_x = points[0].x
# min_y = points[0].y
#
# max_x = points[0].x
# max_y = points[0].y
# for i in range(0, len(points)):
# if points[i].x < min_x:
# min_x = points[i].x
# if points[i].y < min_y:
# min_y = points[i].y
# if points[i].x > max_x:
# max_x = points[i].x
# if points[i].y > max_y:
# max_y = points[i].y
point1 = Point(min_x, min_y)
point2 = Point(max_x + (max_y - min_y), min_y)
point3 = Point(min_x, max_y + (max_x - min_x))
is_in_points1 = 0
is_in_points2 = 0
is_in_points3 = 0
for i in range(0, len(points)):
if is_equal(points[i], point1):
is_in_points1 = 1
if is_equal(points[i], point2):
is_in_points2 = 1
if is_equal(points[i], point3):
is_in_points3 = 1
enclosing_points = []
if is_in_points1 == 0:
enclosing_points += [point1]
if is_in_points2 == 0:
enclosing_points += [point2]
if is_in_points3 == 0:
enclosing_points += [point3]
points += [point1]
points += [point2]
points += [point3]
vertices += [[], [], []]
edges += [[], [], []]
faces += [[]]
j = -3
for i in range(len(points) - 3, len(points)):
vertices[len(vertices) + j] = Point(points[i].x, points[i].y)
edges[len(edges) + j] = Edge(origin=vertices[j])
faces[len(faces) - 1] = Face(edge=edges[0], children=[], parent=[])
vertices[j].edge = edges[j]
edges[j].face = faces[0]
j += 1
edges[len(edges) - 3].next = edges[len(edges) - 2]
edges[len(edges) - 3].previous = edges[len(edges) - 1]
edges[len(edges) - 2].next = edges[len(edges) - 1]
edges[len(edges) - 2].previous = edges[len(edges) - 3]
edges[len(edges) - 1].next = edges[len(edges) - 3]
edges[len(edges) - 1].previous = edges[len(edges) - 2]
for i in range(0, len(points) - 3):
triangle = find_triangle(faces[0], points[i])
if len(triangle) == 1 or len(triangle) == 2:
triangle_queue = []
vertices += [Point(points[i].x, points[i].y)]
vert = vertices[len(vertices) - 1]
edges_len = len(edges)
for m in range(0, len(triangle)):
vert1 = triangle[m].edge.origin
vert2 = triangle[m].edge.next.origin
vert3 = triangle[m].edge.previous.origin
edges += [Edge(origin=vert1)]
edges += [Edge(origin=vert2)]
edges += [Edge(origin=vert)]
edges_len += 3
edges += [Edge(origin=vert2)]
edges += [Edge(origin=vert3)]
edges += [Edge(origin=vert)]
edges_len += 3
edges += [Edge(origin=vert3)]
edges += [Edge(origin=vert1)]
edges += [Edge(origin=vert)]
for j in range(edges_len - 6, edges_len + 1, 3):
vert1.edge = edges[j]
vert2.edge = edges[j + 1]
vert.edge = edges[j + 2]
edges[j].next = edges[j + 1]
edges[j].previous = edges[j + 2]
edges[j + 1].next = edges[j + 2]
edges[j + 1].previous = edges[j]
edges[j + 2].next = edges[j]
edges[j + 2].previous = edges[j + 1]
faces += [Face(edge=edges[j], children=[], parent=[triangle[m]])]
edges[j].face = faces[len(faces) - 1]
edges[j + 1].face = faces[len(faces) - 1]
edges[j + 2].face = faces[len(faces) - 1]
triangle_queue.append(len(faces) - 1)
triangle[m].children += [faces[len(faces) - 1]]
while len(triangle_queue) > 0:
point = points[i]
index = triangle_queue.pop()
current_triangle_points = get_points(faces[index])
if is_equal(current_triangle_points[0], point):
point1 = current_triangle_points[1]
point2 = current_triangle_points[2]
elif is_equal(current_triangle_points[1], point):
point1 = current_triangle_points[0]
point2 = current_triangle_points[2]
else:
point1 = current_triangle_points[0]
point2 = current_triangle_points[1]
if not is_line(current_triangle_points):
for f in range(len(faces) - 1, -1, -1):
if faces[f] is not None:
current_face_points = get_points(faces[f])
if point.is_adjacent(point1, point2, current_face_points) and len(faces[f].children) == 0:
if is_equal(point1, current_face_points[0]) \
and is_equal(point2, current_face_points[1]) \
or is_equal(point2, current_face_points[0]) and is_equal(point1,
current_face_points[
1]):
point3 = current_face_points[2]
elif is_equal(point1, current_face_points[2]) and is_equal(point2,
current_face_points[1]) \
or is_equal(point2, current_face_points[2]) and is_equal(point1,
current_face_points[
1]):
point3 = current_face_points[0]
else:
point3 = current_face_points[1]
if point.is_inside_circle(current_face_points):
parents = [faces[f], faces[index]]
edges_len = len(edges)
edges += [Edge(origin=point)]
edges += [Edge(origin=point1)]
edges += [Edge(origin=point3)]
edges[edges_len].next = edges[edges_len + 1]
edges[edges_len].previous = edges[edges_len + 2]
edges[edges_len + 1].next = edges[edges_len + 2]
edges[edges_len + 1].previous = edges[edges_len]
edges[edges_len + 2].next = edges[edges_len]
edges[edges_len + 2].previous = edges[edges_len + 1]
faces += [Face(edge=edges[edges_len], children=[], parent=parents)]
edges[edges_len].face = faces[len(faces) - 1]
edges[edges_len + 1].face = faces[len(faces) - 1]
edges[edges_len + 2].face = faces[len(faces) - 1]
edges_len += 3
edges += [Edge(origin=point)]
edges += [Edge(origin=point2)]
edges += [Edge(origin=point3)]
edges[edges_len].next = edges[edges_len + 1]
edges[edges_len].previous = edges[edges_len + 2]
edges[edges_len + 1].next = edges[edges_len + 2]
edges[edges_len + 1].previous = edges[edges_len]
edges[edges_len + 2].next = edges[edges_len]
edges[edges_len + 2].previous = edges[edges_len + 1]
faces += [Face(edge=edges[edges_len], children=[], parent=parents)]
edges[edges_len].face = faces[len(faces) - 1]
edges[edges_len + 1].face = faces[len(faces) - 1]
edges[edges_len + 2].face = faces[len(faces) - 1]
faces[f].children += [faces[len(faces) - 1]]
faces[f].children += [faces[len(faces) - 2]]
faces[index].children += [faces[len(faces) - 1]]
faces[index].children += [faces[len(faces) - 2]]
triangle_queue.append(len(faces) - 1)
triangle_queue.append(len(faces) - 2)
break
return vertices, edges, faces, enclosing_points