-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjsclipper-adapter.js
391 lines (307 loc) · 9.84 KB
/
jsclipper-adapter.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
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
var ClipperLib = require ('./jsclipper')
// == CONSTANTS
var DEFAULT_SCALE = Math.pow(10, 5)
// == CONVERSION HELPERS FROM Array TO ClipperLib
function arrayToObjectNotation(arrayOfPoints) {
return arrayOfPoints.map(function(point) {
return {X: point[0], Y: point[1]}
})
}
function objectToArrayNotation(arrayOfPoints) {
return arrayOfPoints.map(function(point) {
return [point.X, point.Y]
})
}
function arrayToClipperPaths(arrayOfPaths) {
return arrayOfPaths.map(arrayToObjectNotation)
}
function clipperPathsToArray(arrayOfPaths) {
return arrayOfPaths.map(objectToArrayNotation)
}
// == TYPES FOR CUSTOM APPLICATION OF clip ==
var FillType = {
EVEN_ODD: ClipperLib.PolyFillType.pftEvenOdd,
NON_ZERO: ClipperLib.PolyFillType.pftNonZero,
NEGATIVE: ClipperLib.PolyFillType.pftNegative,
POSITIVE: ClipperLib.PolyFillType.pftPositive
}
var ClipType = {
INTERSECTION: ClipperLib.ClipType.ctIntersection,
UNION: ClipperLib.ClipType.ctUnion,
DIFFERENCE: ClipperLib.ClipType.ctDifference,
XOR: ClipperLib.ClipType.ctXor
}
var JoinType = {
ROUND: ClipperLib.JoinType.jtRound,
MITER: ClipperLib.JoinType.jtMiter,
SQUARE: ClipperLib.JoinType.jtSquare
}
// == GENERAL PURPOSE CLIPPING ==
function clip(subj, clips, clipType, scale, fillType) {
var scale = scale || DEFAULT_SCALE
var fillType = fillType || FillType.NON_ZERO
if (!Array.isArray(subj)) {
throw new Error('Provide subject polygon as an array of paths.')
}
if (!Array.isArray(clips)) {
throw new Error('Provide clip polygons as arrays of paths.')
}
if (clips.length == 0) {
throw new Error('Provide at least one clip.')
}
if ('number' != typeof clipType || !(0 <= clipType && clipType < 4)) {
throw new Error('Provide a valid clip type!')
}
var subjPaths = arrayToClipperPaths(subj)
var clipsPaths = clips.map(arrayToClipperPaths)
var clipper = new ClipperLib.Clipper()
ClipperLib.JS.ScaleUpPaths(subjPaths, scale)
clipper.AddPaths(subjPaths, ClipperLib.PolyType.ptSubject, true)
clipsPaths.forEach(function(clipPaths) {
ClipperLib.JS.ScaleUpPaths(clipPaths, scale)
clipper.AddPaths(clipPaths, ClipperLib.PolyType.ptClip, true)
})
var solution = []
var succeeded = clipper.Execute(clipType, solution, fillType, fillType)
if (succeeded) {
ClipperLib.JS.ScaleDownPaths(solution, scale)
return clipperPathsToArray(solution)
}
// return false when the clipping failed
return false
}
// == CLIPPING METHODS ==
function intersect(subj, clips) {
return clip(subj, clips, ClipType.INTERSECTION)
}
function union(subj, clips) {
return clip(subj, clips, ClipType.UNION)
}
function diff(subj, clips) {
return clip(subj, clips, ClipType.DIFFERENCE)
}
function xor(subj, clips) {
return clip(subj, clips, ClipType.XOR)
}
// == POLYGON METHODS ==
/**
* Construct with an array of THREE.Vector2/3 instead of [x,y]
*/
Polygon.fromVector = function(shape, holes) {
var shapeArray = shape.map(function(vector) {
return [vector.x, vector.y]
})
var holesArray = undefined
if (holes) {
holesArray = holes.map(function(hole) {
return hole.map(function(vector) {
return [vector.x, vector.y]
})
})
}
return new Polygon(shapeArray, holesArray)
}
// == GENERAL PURPOSE OFFSETTING ==
function offset(
subj,
offset,
scale,
fillType,
cleanDelta,
miterLimit,
arcTolerance,
joinType
) {
var scale = scale || DEFAULT_SCALE
var fillType = fillType || FillType.NON_ZERO
var cleanDelta = cleanDelta || 1 / scale
var miterLimit = miterLimit || 2
var arcTolerance = arcTolerance || 0.25
var joinType = joinType || JoinType.MITER
if (!Array.isArray(subj)) {
throw new Error('Provide subject polygon as an array of paths.')
}
var subjPaths = arrayToClipperPaths(subj)
ClipperLib.JS.ScaleUpPaths(subjPaths, scale)
var simplifiedPaths = ClipperLib.Clipper.SimplifyPolygons(subjPaths, fillType)
var cleanedPaths = ClipperLib.JS.Clean(simplifiedPaths, cleanDelta)
var clipperOffset = new ClipperLib.ClipperOffset()
clipperOffset.AddPaths(
cleanedPaths,
joinType,
ClipperLib.EndType.etClosedPolygon
)
var solution = []
clipperOffset.Execute(solution, offset * scale)
ClipperLib.JS.ScaleDownPaths(solution, scale)
return clipperPathsToArray(solution)
}
function Polygon(shape, holes) {
if (!Array.isArray(shape)) {
throw new Error('Given shape should be an array of points [x,y].')
}
holes = holes || []
if (!Array.isArray(holes)) {
throw new Error('Given holes should be an array of paths.')
}
// force intented orientation on polygons
var _shape = shape.concat()
var _holes = holes.concat()
if (!Polygon.isCounterClockwise(_shape)) {
_shape.reverse()
}
_holes = _holes.map(function(hole) {
if (Polygon.isCounterClockwise(hole)) {
return hole.concat().reverse()
}
return hole
})
this._paths = [_shape].concat(_holes)
}
// produce a clone of the polygon
Polygon.prototype.clone = function() {
clonedShape = this.getShape().map(function(vertex) {
return vertex.concat()
})
clonedHoles = this.getHoles().map((function(holePath) {
return holePath.map(function(vertex) {
return vertex.concat()
})
}))
return new Polygon(clonedShape, clonedHoles)
}
Polygon.prototype.getPaths = function() {
return this._paths.slice()
}
Polygon.prototype.getShape = function() {
return this._paths.slice(0,1)[0]
}
Polygon.prototype.getHoles = function() {
return this._paths.slice(1)
}
/** use clip method on subject polygon with multiple clip polygons **/
Polygon.prototype.clipMultiple = function(clipPolygons, clipType) {
var clipPaths = clipPolygons.map(function(polygon) {
return polygon.getPaths()
})
var solution = clip(this.getPaths(), clipPaths, clipType)
if (solution) {
return Polygon.assignShapesAndHoles(solution)
}
// return false when clipping failed
return false
}
Polygon.prototype.diffMultiple = function (clipPolygons) {
return this.clipMultiple(clipPolygons, ClipType.DIFFERENCE)
}
Polygon.prototype.intersectMultiple = function (clipPolygons) {
return this.clipMultiple(clipPolygons, ClipType.INTERSECTION)
}
Polygon.prototype.unionMultiple = function (clipPolygons) {
return this.clipMultiple(clipPolygons, ClipType.UNION)
}
Polygon.prototype.xorMultiple = function (clipPolygons) {
return this.clipMultiple(clipPolygons, ClipType.XOR)
}
/** use clip method on subject polygon with a single clip polygon **/
Polygon.prototype.diff = function (clipPolygons) {
return this.clipMultiple([clipPolygons], ClipType.DIFFERENCE)
}
Polygon.prototype.intersect = function (clipPolygons) {
return this.clipMultiple([clipPolygons], ClipType.INTERSECTION)
}
Polygon.prototype.union = function (clipPolygons) {
return this.clipMultiple([clipPolygons], ClipType.UNION)
}
Polygon.prototype.xor = function (clipPolygons) {
return this.clipMultiple([clipPolygons], ClipType.XOR)
}
/** use offset method on subject polygon **/
Polygon.prototype.offset = function (delta, scale) {
var solution = offset(this.getPaths(), delta, scale)
return Polygon.assignShapesAndHoles(solution)[0]
}
Polygon.prototype.area = function(scale) {
scale = scale || DEFAULT_SCALE
var path = arrayToObjectNotation(this.getShape())
ClipperLib.JS.ScaleUpPath(path, scale)
return ClipperLib.Clipper.Area(path) / (scale * scale)
}
Polygon.prototype.containsPoint = function(point, scale) {
scale = scale || DEFAULT_SCALE
var shape = this.getShape()
var holes = this.getHoles()
var inHoles = holes.some(function(hole) {
return Polygon.containsPoint(hole, point, scale)
})
return Polygon.containsPoint(shape, point, scale) && !inHoles
}
Polygon.assignShapesAndHoles = function(paths) {
function separateHolesFromShapes(paths) {
var holes = []
var shapes = []
paths.forEach(function(path) {
// by JSClipper convention shape boundaries are CCW
if (Polygon.isCounterClockwise(path)) {
shapes.push(path)
} else {
holes.push(path)
}
})
return {
shapes: shapes,
holes: holes
}
}
function groupHolesForShape(holes) {
return function(shape) {
var _holes = holes.filter(function(hole) {
return Polygon.contains(shape, hole)
})
return new Polygon(shape, _holes)
}
}
var p = separateHolesFromShapes(paths)
return p.shapes.map(groupHolesForShape(p.holes))
}
/**
* Outputs true if the polygon has a CCW winding
* @return {Boolean}
*/
Polygon.isCounterClockwise = function(path) {
return ClipperLib.Clipper.Orientation(arrayToObjectNotation(path))
};
Polygon.contains = function(outer, inner) {
var _outer = arrayToObjectNotation(outer)
var _inner = arrayToObjectNotation(inner)
return _inner.reduce(function(acc, point) {
return acc && 0 !== ClipperLib.Clipper.PointInPolygon(point, _outer)
}, true)
}
Polygon.containsPoint = function(path, point, scale) {
path = arrayToObjectNotation(path)
ClipperLib.JS.ScaleUpPath(path, scale)
return 0 !== ClipperLib.Clipper.PointInPolygon(
{ X: point[0] * scale, Y: point[1] * scale },
path
)
}
// == EXPORTS ==
module.exports = {
DEFAULT_SCALE: DEFAULT_SCALE,
arrayToObjectNotation: arrayToObjectNotation,
objectToArrayNotation: objectToArrayNotation,
arrayToClipperPaths: arrayToClipperPaths,
clipperPathsToArray: clipperPathsToArray,
FillType: FillType,
ClipType: ClipType,
JoinType: JoinType,
clip: clip,
intersect: intersect,
union: union,
diff: diff,
xor: xor,
offset: offset,
Polygon: Polygon,
ClipperLib: ClipperLib
}