-
Notifications
You must be signed in to change notification settings - Fork 0
/
delaunay.lua
348 lines (277 loc) · 9.6 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
--[[
Based on Delaunay triangulation library by Yonaba ([email protected])
url: https://github.com/Yonaba/delaunay
git: [email protected]:Yonaba/delaunay.git
Original LICENSE file contents:
The MIT License (MIT)
Copyright (c) 2013 Roland Y.
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
-- Coded by Ilya Kolbin ([email protected]).
--
-- Deviations from original library:
--
-- 1. Triangulation function takes array instead of tuple;
-- 2. Use LuaJIT FFI if possible( turnable off ).
--
-- Using FFI increases performance roughly x2.
--
-- Using FFI reduces memory usage approx. on 40-50%. It's possible to futher
-- decrese memory use by setting _G.DELAUNAY_FFI_TYPE = 'float' -- in this
-- case memory usage drops on 60-70%. Note that you must set it before the library
-- is loaded and shouldn't change it later.
local setmetatable, tostring, assert = setmetatable, tostring, assert
local max, sqrt = math.max, math.sqrt
local remove, unpack = table.remove, unpack or table.unpack
local isffi, ffi = pcall( require, 'ffi' )
local ffiPoint, ffiEdge, ffiTriangle
-- 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
--- Point class
local Point = {}
Point.__index = Point
function Point.new( x, y )
if isffi then
return ffiPoint( x, y, 0 )
else
return setmetatable( {x = x, y = y, id = 0}, Point )
end
end
function Point:__eq( other )
return self.x == other.x and self.y == other.y
end
function Point:__tostring()
return ('Point (%s) x: %.2f y: %.2f'):format( self.id, self.x, self.y )
end
function Point:dist2(p)
local dx, dy = (self.x - p.x), (self.y - p.y)
return dx * dx + dy * dy
end
function Point:dist(p)
return sqrt(self:dist2(p))
end
function Point:isInCircle(cx, cy, r)
local dx = (cx - self.x)
local dy = (cy - self.y)
return ((dx * dx + dy * dy) <= (r * r))
end
setmetatable( Point, {__call = function( _, x, y )
return Point.new( x, y )
end} )
-- Edge class
local Edge = {}
Edge.__index = Edge
function Edge.new( p1, p2 )
if isffi then
return ffiEdge( p1, p2 )
else
return setmetatable( { p1 = p1, p2 = p2 }, Edge )
end
end
function Edge:__eq( other )
return self.p1 == other.p1 and self.p2 == other.p2
end
function Edge:__tostring()
return (('Edge :\n %s\n %s'):format(tostring(self.p1), tostring(self.p2)))
end
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
function Edge:length()
return self.p1:dist(self.p2)
end
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
setmetatable( Edge, {__call = function(_,p1,p2)
return Edge.new( p1, p2 )
end} )
--- Triangle class
local Triangle = {}
Triangle.__index = Triangle
function Triangle.new( 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)))
if isffi then
return ffiTriangle( p1, p2, p3, Edge(p1, p2), Edge(p2, p3), Edge(p3, p1))
else
return setmetatable( {
p1 = p1, p2 = p2,
p3 = p3, e1 = Edge(p1, p2), e2 = Edge(p2, p3), e3 = Edge(p3, p1)}, Triangle )
end
end
function Triangle:__tostring()
return (('Triangle: \n %s\n %s\n %s')
:format(tostring(self.p1), tostring(self.p2), tostring(self.p3)))
end
--- Checks if the triangle is defined clockwise (sequence p1-p2-p3)
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)
function Triangle:isCCW()
return (crossProduct(self.p1, self.p2, self.p3) > 0)
end
--- Returns the length of the edges
function Triangle:getSidesLength()
return self.e1:length(), self.e2:length(), self.e3:length()
end
--- Returns the coordinates of the center
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
function Triangle:getCircumCircle()
local x, y = self:getCircumCenter()
local r = self:getCircumRadius()
return x, y, r
end
--- Returns the coordinates of the circumcircle center
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
function Triangle:getCircumRadius()
local a, b, c = self:getSidesLength()
return ((a * b * c) / quatCross(a, b, c))
end
--- Returns the area
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
function Triangle:inCircumCircle(p)
return p:isInCircle(self:getCircumCircle())
end
setmetatable( Triangle, {__call = function( _, p1, p2, p3 )
return Triangle.new( p1, p2, p3 )
end} )
local delaunay = {
Point = Point,
Edge = Edge,
Triangle = Triangle,
convexMultiplier = 1e3,
}
--- Triangulates a set of given vertices
function delaunay.triangulate( vertices )
local nvertices = #vertices
assert( nvertices > 2, "Cannot triangulate, needs more than 3 vertices" )
if nvertices == 3 then
return {Triangle(vertices[1], vertices[2], vertices[3])}
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], vertices[p2.id], vertices[p3.id] = p1, p2, p3
local triangles = {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
function delaunay.setffi( set )
if ffi then
isffi = set
end
end
if isffi then
ffi.cdef(([[
typedef struct { %s x, y; uint32_t id; } Point;
typedef struct { Point p1, p2; } Edge;
typedef struct { Point p1, p2, p3; Edge e1, e2, e3; } Triangle;
]]):format( _G.DELAUNAY_FFI_TYPE or 'double' ))
ffiPoint = ffi.metatype( "Point", Point )
ffiEdge = ffi.metatype( "Edge", Edge )
ffiTriangle = ffi.metatype( "Triangle", Triangle )
end
return delaunay