-
Notifications
You must be signed in to change notification settings - Fork 91
/
index.js
126 lines (117 loc) · 3.32 KB
/
index.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
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
/*
* @copyright 2016 Sean Connelly (@voidqk), http://syntheti.cc
* @license MIT
* @preserve Project Home: https://github.com/voidqk/polybooljs
*/
var BuildLog = require('./lib/build-log');
var Epsilon = require('./lib/epsilon');
var Intersecter = require('./lib/intersecter');
var SegmentChainer = require('./lib/segment-chainer');
var SegmentSelector = require('./lib/segment-selector');
var GeoJSON = require('./lib/geojson');
var buildLog = false;
var epsilon = Epsilon();
var PolyBool;
PolyBool = {
// getter/setter for buildLog
buildLog: function(bl){
if (bl === true)
buildLog = BuildLog();
else if (bl === false)
buildLog = false;
return buildLog === false ? false : buildLog.list;
},
// getter/setter for epsilon
epsilon: function(v){
return epsilon.epsilon(v);
},
// core API
segments: function(poly){
var i = Intersecter(true, epsilon, buildLog);
poly.regions.forEach(i.addRegion);
return {
segments: i.calculate(poly.inverted),
inverted: poly.inverted
};
},
combine: function(segments1, segments2){
var i3 = Intersecter(false, epsilon, buildLog);
return {
combined: i3.calculate(
segments1.segments, segments1.inverted,
segments2.segments, segments2.inverted
),
inverted1: segments1.inverted,
inverted2: segments2.inverted
};
},
selectUnion: function(combined){
return {
segments: SegmentSelector.union(combined.combined, buildLog),
inverted: combined.inverted1 || combined.inverted2
}
},
selectIntersect: function(combined){
return {
segments: SegmentSelector.intersect(combined.combined, buildLog),
inverted: combined.inverted1 && combined.inverted2
}
},
selectDifference: function(combined){
return {
segments: SegmentSelector.difference(combined.combined, buildLog),
inverted: combined.inverted1 && !combined.inverted2
}
},
selectDifferenceRev: function(combined){
return {
segments: SegmentSelector.differenceRev(combined.combined, buildLog),
inverted: !combined.inverted1 && combined.inverted2
}
},
selectXor: function(combined){
return {
segments: SegmentSelector.xor(combined.combined, buildLog),
inverted: combined.inverted1 !== combined.inverted2
}
},
polygon: function(segments){
return {
regions: SegmentChainer(segments.segments, epsilon, buildLog),
inverted: segments.inverted
};
},
// GeoJSON converters
polygonFromGeoJSON: function(geojson){
return GeoJSON.toPolygon(PolyBool, geojson);
},
polygonToGeoJSON: function(poly){
return GeoJSON.fromPolygon(PolyBool, epsilon, poly);
},
// helper functions for common operations
union: function(poly1, poly2){
return operate(poly1, poly2, PolyBool.selectUnion);
},
intersect: function(poly1, poly2){
return operate(poly1, poly2, PolyBool.selectIntersect);
},
difference: function(poly1, poly2){
return operate(poly1, poly2, PolyBool.selectDifference);
},
differenceRev: function(poly1, poly2){
return operate(poly1, poly2, PolyBool.selectDifferenceRev);
},
xor: function(poly1, poly2){
return operate(poly1, poly2, PolyBool.selectXor);
}
};
function operate(poly1, poly2, selector){
var seg1 = PolyBool.segments(poly1);
var seg2 = PolyBool.segments(poly2);
var comb = PolyBool.combine(seg1, seg2);
var seg3 = selector(comb);
return PolyBool.polygon(seg3);
}
if (typeof window === 'object')
window.PolyBool = PolyBool;
module.exports = PolyBool;