-
Notifications
You must be signed in to change notification settings - Fork 21
/
cdt2d.js
82 lines (66 loc) · 2.02 KB
/
cdt2d.js
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
'use strict'
var monotoneTriangulate = require('./lib/monotone')
var makeIndex = require('./lib/triangulation')
var delaunayFlip = require('./lib/delaunay')
var filterTriangulation = require('./lib/filter')
module.exports = cdt2d
function canonicalizeEdge(e) {
return [Math.min(e[0], e[1]), Math.max(e[0], e[1])]
}
function compareEdge(a, b) {
return a[0]-b[0] || a[1]-b[1]
}
function canonicalizeEdges(edges) {
return edges.map(canonicalizeEdge).sort(compareEdge)
}
function getDefault(options, property, dflt) {
if(property in options) {
return options[property]
}
return dflt
}
function cdt2d(points, edges, options) {
if(!Array.isArray(edges)) {
options = edges || {}
edges = []
} else {
options = options || {}
edges = edges || []
}
//Parse out options
var delaunay = !!getDefault(options, 'delaunay', true)
var interior = !!getDefault(options, 'interior', true)
var exterior = !!getDefault(options, 'exterior', true)
var infinity = !!getDefault(options, 'infinity', false)
//Handle trivial case
if((!interior && !exterior) || points.length === 0) {
return []
}
//Construct initial triangulation
var cells = monotoneTriangulate(points, edges)
//If delaunay refinement needed, then improve quality by edge flipping
if(delaunay || interior !== exterior || infinity) {
//Index all of the cells to support fast neighborhood queries
var triangulation = makeIndex(points.length, canonicalizeEdges(edges))
for(var i=0; i<cells.length; ++i) {
var f = cells[i]
triangulation.addTriangle(f[0], f[1], f[2])
}
//Run edge flipping
if(delaunay) {
delaunayFlip(points, triangulation)
}
//Filter points
if(!exterior) {
return filterTriangulation(triangulation, -1)
} else if(!interior) {
return filterTriangulation(triangulation, 1, infinity)
} else if(infinity) {
return filterTriangulation(triangulation, 0, infinity)
} else {
return triangulation.cells()
}
} else {
return cells
}
}