-
Notifications
You must be signed in to change notification settings - Fork 19
/
delaunay.lua
451 lines (401 loc) · 12.8 KB
/
delaunay.lua
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#!/usr/bin/env lua
---------------
-- ## Delaunay, Lua module for convex polygon triangulation
-- @author Roland Yonaba
-- @copyright 2013-2016
-- @license MIT
-- @script delaunay
-- ================
-- Private helpers
-- ================
local setmetatable = setmetatable
local tostring = tostring
local assert = assert
local unpack = unpack or table.unpack
local remove = table.remove
local sqrt = math.sqrt
local max = math.max
-- Internal class constructor
local class = function(...)
local klass = {}
klass.__index = klass
klass.__call = function(_,...) return klass:new(...) end
function klass:new(...)
local instance = setmetatable({}, klass)
klass.__init(instance, ...)
return instance
end
return setmetatable(klass,{__call = klass.__call})
end
-- Triangle semi-perimeter by Heron's formula
local function quatCross(a, b, c)
local p = (a + b + c) * (a + b - c) * (a - b + c) * (-a + b + c)
return sqrt(p)
end
-- Cross product (p1-p2, p2-p3)
local function crossProduct(p1, p2, p3)
local x1, x2 = p2.x - p1.x, p3.x - p2.x
local y1, y2 = p2.y - p1.y, p3.y - p2.y
return x1 * y2 - y1 * x2
end
-- Checks if angle (p1-p2-p3) is flat
local function isFlatAngle(p1, p2, p3)
return (crossProduct(p1, p2, p3) == 0)
end
-- ================
-- Module classes
-- ================
--- `Edge` class
-- @type Edge
local Edge = class()
Edge.__eq = function(a, b) return (a.p1 == b.p1 and a.p2 == b.p2) end
Edge.__tostring = function(e)
return (('Edge :\n %s\n %s'):format(tostring(e.p1), tostring(e.p2)))
end
--- Creates a new `Edge`
-- @name Edge:new
-- @param p1 a `Point`
-- @param p2 a `Point`
-- @return a new `Edge`
-- @usage
-- local Delaunay = require 'Delaunay'
-- local Edge = Delaunay.Edge
-- local Point = Delaunay.Point
-- local e = Edge:new(Point(1,1), Point(2,5))
-- local e = Edge(Point(1,1), Point(2,5)) -- Alias to Edge.new
-- print(e) -- print the edge members p1 and p2
--
function Edge:__init(p1, p2)
self.p1, self.p2 = p1, p2
end
--- Test if `otherEdge` is similar to self. It does not take into account the direction.
-- @param otherEdge an `Edge`
-- @return `true` or `false`
-- @usage
-- local e1 = Edge(Point(1,1), Point(2,5))
-- local e2 = Edge(Point(2,5), Point(1,1))
-- print(e1:same(e2)) --> true
-- print(e1 == e2)) --> false, == operator considers the direction
--
function Edge:same(otherEdge)
return ((self.p1 == otherEdge.p1) and (self.p2 == otherEdge.p2))
or ((self.p1 == otherEdge.p2) and (self.p2 == otherEdge.p1))
end
--- Returns the length.
-- @return the length of self
-- @usage
-- local e = Edge(Point(), Point(10,0))
-- print(e:length()) --> 10
--
function Edge:length()
return self.p1:dist(self.p2)
end
--- Returns the midpoint coordinates.
-- @return the x-coordinate of self midpoint
-- @return the y-coordinate of self midpoint
-- @usage
-- local e = Edge(Point(), Point(10,0))
-- print(e:getMidPoint()) --> 5, 0
--
function Edge:getMidPoint()
local x = self.p1.x + (self.p2.x - self.p1.x) / 2
local y = self.p1.y + (self.p2.y - self.p1.y) / 2
return x, y
end
--- Point class
-- @type Point
local Point = class()
Point.__eq = function(a,b) return (a.x == b.x and a.y == b.y) end
Point.__tostring = function(p)
return ('Point (%s) x: %.2f y: %.2f'):format(p.id, p.x, p.y)
end
--- Creates a new `Point`
-- @name Point:new
-- @param x the x-coordinate
-- @param y the y-coordinate
-- @return a new `Point`
-- @usage
-- local Delaunay = require 'Delaunay'
-- local Point = Delaunay.Point
-- local p = Point:new(1,1)
-- local p = Point(1,1) -- Alias to Point.new
-- print(p) -- print the point members x and y
--
function Point:__init(x, y)
self.x, self.y, self.id = x or 0, y or 0, '?'
end
--- Returns the square distance to another `Point`.
-- @param p a `Point`
-- @return the square distance from self to `p`.
-- @usage
-- local p1, p2 = Point(), Point(1,1)
-- print(p1:dist2(p2)) --> 2
--
function Point:dist2(p)
local dx, dy = (self.x - p.x), (self.y - p.y)
return dx * dx + dy * dy
end
--- Returns the distance to another `Point`.
-- @param p a `Point`
-- @return the distance from self to `p`.
-- @usage
-- local p1, p2 = Point(), Point(1,1)
-- print(p1:dist2(p2)) --> 1.4142135623731
--
function Point:dist(p)
return sqrt(self:dist2(p))
end
--- Checks if self lies into the bounds of a circle
-- @param cx the x-coordinate of the circle center
-- @param cy the y-coordinate of the circle center
-- @param r the radius of the circle
-- @return `true` or `false`
-- @usage
-- local p = Point()
-- print(p:isInCircle(0,0,1)) --> true
--
function Point:isInCircle(cx, cy, r)
local dx = (cx - self.x)
local dy = (cy - self.y)
return ((dx * dx + dy * dy) <= (r * r))
end
--- `Triangle` class
-- @type Triangle
local Triangle = class()
Triangle.__tostring = function(t)
return (('Triangle: \n %s\n %s\n %s')
:format(tostring(t.p1), tostring(t.p2), tostring(t.p3)))
end
--- Creates a new `Triangle`
-- @name Triangle:new
-- @param p1 a `Point`
-- @param p2 a `Point`
-- @param p3 a `Point`
-- @return a new `Triangle`
-- @usage
-- local Delaunay = require 'Delaunay'
-- local Triangle = Delaunay.Triangle
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle:new(p1, p2, p3)
-- local t = Triangle(p1, p2, p3) -- Alias to Triangle.new
-- print(t) -- print the triangle members p1, p2 and p3
--
function Triangle:__init(p1, p2, p3)
assert(not isFlatAngle(p1, p2, p3), ("angle (p1, p2, p3) is flat:\n %s\n %s\n %s")
:format(tostring(p1), tostring(p2), tostring(p3)))
self.p1, self.p2, self.p3 = p1, p2, p3
self.e1, self.e2, self.e3 = Edge(p1, p2), Edge(p2, p3), Edge(p3, p1)
end
--- Checks if the triangle is defined clockwise (sequence p1-p2-p3)
-- @return `true` or `false`
-- @usage
-- local p1, p2, p3 = Point(), Point(1,1), Point(2,0)
-- local t = Triangle(p1, p2, p3)
-- print(t:isCW()) --> true
--
function Triangle:isCW()
return (crossProduct(self.p1, self.p2, self.p3) < 0)
end
--- Checks if the triangle is defined counter-clockwise (sequence p1-p2-p3)
-- @return `true` or `false`
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:isCCW()) --> true
--
function Triangle:isCCW()
return (crossProduct(self.p1, self.p2, self.p3) > 0)
end
--- Returns the length of the edges
-- @return the length of the edge p1-p2
-- @return the length of the edge p2-p3
-- @return the length of the edge p3-p1
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getSidesLength()) --> 2 1.4142135623731 1.4142135623731
--
function Triangle:getSidesLength()
return self.e1:length(), self.e2:length(), self.e3:length()
end
--- Returns the coordinates of the center
-- @return the x-coordinate of the center
-- @return the y-coordinate of the center
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getCenter()) --> 1 0.33333333333333
--
function Triangle:getCenter()
local x = (self.p1.x + self.p2.x + self.p3.x) / 3
local y = (self.p1.y + self.p2.y + self.p3.y) / 3
return x, y
end
--- Returns the coordinates of the circumcircle center and its radius
-- @return the x-coordinate of the circumcircle center
-- @return the y-coordinate of the circumcircle center
-- @return the radius of the circumcircle
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getCircumCircle()) --> 1 0 1
--
function Triangle:getCircumCircle()
local x, y = self:getCircumCenter()
local r = self:getCircumRadius()
return x, y, r
end
--- Returns the coordinates of the circumcircle center
-- @return the x-coordinate of the circumcircle center
-- @return the y-coordinate of the circumcircle center
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getCircumCenter()) --> 1 0
--
function Triangle:getCircumCenter()
local p1, p2, p3 = self.p1, self.p2, self.p3
local D = ( p1.x * (p2.y - p3.y) +
p2.x * (p3.y - p1.y) +
p3.x * (p1.y - p2.y)) * 2
local x = (( p1.x * p1.x + p1.y * p1.y) * (p2.y - p3.y) +
( p2.x * p2.x + p2.y * p2.y) * (p3.y - p1.y) +
( p3.x * p3.x + p3.y * p3.y) * (p1.y - p2.y))
local y = (( p1.x * p1.x + p1.y * p1.y) * (p3.x - p2.x) +
( p2.x * p2.x + p2.y * p2.y) * (p1.x - p3.x) +
( p3.x * p3.x + p3.y * p3.y) * (p2.x - p1.x))
return (x / D), (y / D)
end
--- Returns the radius of the circumcircle
-- @return the radius of the circumcircle
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getCircumRadius()) --> 1
--
function Triangle:getCircumRadius()
local a, b, c = self:getSidesLength()
return ((a * b * c) / quatCross(a, b, c))
end
--- Returns the area
-- @return the area
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:getArea()) --> 1
--
function Triangle:getArea()
local a, b, c = self:getSidesLength()
return (quatCross(a, b, c) / 4)
end
--- Checks if a given point lies into the triangle circumcircle
-- @param p a `Point`
-- @return `true` or `false`
-- @usage
-- local p1, p2, p3 = Point(), Point(2,0), Point(1,1)
-- local t = Triangle(p1, p2, p3)
-- print(t:inCircumCircle(Point(1,-1))) --> true
--
function Triangle:inCircumCircle(p)
return p:isInCircle(self:getCircumCircle())
end
--- Delaunay module
-- @section public
--- Delaunay module
-- @table Delaunay
-- @field Point reference to the `Point` class
-- @field Edge reference to the `Edge` class
-- @field Triangle reference to the `Triangle` class
-- @field convexMultiplier multiplier heuristic for bounding triangle calculation. When small (~1) produces convex-hull, when large, produces concave hulls. Defaults to 1000.
-- @field _VERSION the version of the current module
local Delaunay = {
Point = Point,
Edge = Edge,
Triangle = Triangle,
convexMultiplier = 1e3,
_VERSION = "0.1"
}
--- Triangulates a set of given vertices
-- @param ... a `vargarg` list of objects of type `Point`
-- @return a set of objects of type `Triangle`
-- @usage
-- local Delaunay = require 'Delaunay'
-- local Point = Delaunay.Point
-- local p1, p2, p3, p4 = Point(), Point(2,0), Point(1,1), Point(1,-1)
-- local triangles = Delaunay.triangulate(p1, p2, p3, p4)
-- for i = 1, #triangles do
-- print(triangles[i])
-- end
--
function Delaunay.triangulate(...)
local vertices = {...}
local nvertices = #vertices
assert(nvertices > 2, "Cannot triangulate, needs more than 3 vertices")
if nvertices == 3 then
return {Triangle(unpack(vertices))}
end
local trmax = nvertices * 4
local minX, minY = vertices[1].x, vertices[1].y
local maxX, maxY = minX, minY
for i = 1, #vertices do
local vertex = vertices[i]
vertex.id = i
if vertex.x < minX then minX = vertex.x end
if vertex.y < minY then minY = vertex.y end
if vertex.x > maxX then maxX = vertex.x end
if vertex.y > maxY then maxY = vertex.y end
end
local convex_mult = Delaunay.convexMultiplier
local dx, dy = (maxX - minX) * convex_mult, (maxY - minY) * convex_mult
local deltaMax = max(dx, dy)
local midx, midy = (minX + maxX) * 0.5, (minY + maxY) * 0.5
local p1 = Point(midx - 2 * deltaMax, midy - deltaMax)
local p2 = Point(midx, midy + 2 * deltaMax)
local p3 = Point(midx + 2 * deltaMax, midy - deltaMax)
p1.id, p2.id, p3.id = nvertices + 1, nvertices + 2, nvertices + 3
vertices[p1.id] = p1
vertices[p2.id] = p2
vertices[p3.id] = p3
local triangles = {}
triangles[#triangles + 1] = Triangle(vertices[nvertices + 1],
vertices[nvertices + 2],
vertices[nvertices + 3]
)
for i = 1, nvertices do
local edges = {}
local ntriangles = #triangles
for j = #triangles, 1, -1 do
local curTriangle = triangles[j]
if curTriangle:inCircumCircle(vertices[i]) then
edges[#edges + 1] = curTriangle.e1
edges[#edges + 1] = curTriangle.e2
edges[#edges + 1] = curTriangle.e3
remove(triangles, j)
end
end
for j = #edges - 1, 1, -1 do
for k = #edges, j + 1, -1 do
if edges[j] and edges[k] and edges[j]:same(edges[k]) then
remove(edges, j)
remove(edges, k-1)
end
end
end
for j = 1, #edges do
local n = #triangles
assert(n <= trmax, "Generated more than needed triangles")
triangles[n + 1] = Triangle(edges[j].p1, edges[j].p2, vertices[i])
end
end
for i = #triangles, 1, -1 do
local triangle = triangles[i]
if (triangle.p1.id > nvertices or
triangle.p2.id > nvertices or
triangle.p3.id > nvertices) then
remove(triangles, i)
end
end
for _ = 1,3 do remove(vertices) end
return triangles
end
return Delaunay