plotboilerplate 1.23.2 → 1.23.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.esm.js +7052 -6754
- package/dist/index.esm.js.map +1 -1
- package/dist/index.esm.min.js +1 -1
- package/dist/index.esm.min.js.map +1 -1
- package/dist/plotboilerplate.browser.min.js +1 -1
- package/dist/plotboilerplate.browser.min.js.map +1 -1
- package/dist/plotboilerplate.js +329 -27
- package/dist/plotboilerplate.min.js +1 -1
- package/package.json +4 -2
- package/src/cjs/PlotBoilerplate.d.ts +4 -2
- package/src/cjs/PlotBoilerplate.js +7 -3
- package/src/cjs/PlotBoilerplate.js.map +1 -1
- package/src/cjs/Polygon.d.ts +107 -4
- package/src/cjs/Polygon.js +215 -9
- package/src/cjs/Polygon.js.map +1 -1
- package/src/cjs/Triangle.d.ts +23 -3
- package/src/cjs/Triangle.js +15 -2
- package/src/cjs/Triangle.js.map +1 -1
- package/src/cjs/VertTuple.d.ts +12 -7
- package/src/cjs/VertTuple.js +14 -9
- package/src/cjs/VertTuple.js.map +1 -1
- package/src/cjs/Vertex.d.ts +2 -1
- package/src/cjs/Vertex.js +5 -2
- package/src/cjs/Vertex.js.map +1 -1
- package/src/cjs/draw.d.ts +1 -0
- package/src/cjs/draw.js +2 -1
- package/src/cjs/draw.js.map +1 -1
- package/src/cjs/drawutilssvg.js +24 -0
- package/src/cjs/drawutilssvg.js.map +1 -1
- package/src/cjs/geomutils.d.ts +32 -1
- package/src/cjs/geomutils.js +47 -1
- package/src/cjs/geomutils.js.map +1 -1
- package/src/cjs/utils/algorithms/PolygonInset.d.ts +184 -0
- package/src/cjs/utils/algorithms/PolygonInset.js +431 -0
- package/src/cjs/utils/algorithms/PolygonInset.js.map +1 -0
- package/src/cjs/utils/algorithms/clearDuplicateVertices.d.ts +5 -4
- package/src/cjs/utils/algorithms/clearDuplicateVertices.js +6 -3
- package/src/cjs/utils/algorithms/clearDuplicateVertices.js.map +1 -1
- package/src/cjs/utils/algorithms/findPolygonSelfIntersections.d.ts +2 -1
- package/src/cjs/utils/algorithms/findPolygonSelfIntersections.js +3 -3
- package/src/cjs/utils/algorithms/findPolygonSelfIntersections.js.map +1 -1
- package/src/cjs/utils/algorithms/splitPolygonToNonIntersecting.d.ts +3 -2
- package/src/cjs/utils/algorithms/splitPolygonToNonIntersecting.js +11 -9
- package/src/cjs/utils/algorithms/splitPolygonToNonIntersecting.js.map +1 -1
- package/src/cjs/utils/detectDarkMode.js.map +1 -1
- package/src/cjs/utils/dom/guiSizeToggler.d.ts +2 -1
- package/src/cjs/utils/dom/guiSizeToggler.js +5 -3
- package/src/cjs/utils/dom/guiSizeToggler.js.map +1 -1
- package/src/esm/PlotBoilerplate.d.ts +4 -2
- package/src/esm/PlotBoilerplate.js +7 -3
- package/src/esm/PlotBoilerplate.js.map +1 -1
- package/src/esm/Polygon.d.ts +107 -4
- package/src/esm/Polygon.js +214 -9
- package/src/esm/Polygon.js.map +1 -1
- package/src/esm/Triangle.d.ts +23 -3
- package/src/esm/Triangle.js +15 -2
- package/src/esm/Triangle.js.map +1 -1
- package/src/esm/VertTuple.d.ts +12 -7
- package/src/esm/VertTuple.js +14 -9
- package/src/esm/VertTuple.js.map +1 -1
- package/src/esm/Vertex.d.ts +2 -1
- package/src/esm/Vertex.js +5 -2
- package/src/esm/Vertex.js.map +1 -1
- package/src/esm/draw.d.ts +1 -0
- package/src/esm/draw.js +2 -1
- package/src/esm/draw.js.map +1 -1
- package/src/esm/drawutilssvg.js +24 -0
- package/src/esm/drawutilssvg.js.map +1 -1
- package/src/esm/geomutils.d.ts +32 -1
- package/src/esm/geomutils.js +47 -1
- package/src/esm/geomutils.js.map +1 -1
- package/src/esm/utils/algorithms/PolygonInset.d.ts +184 -0
- package/src/esm/utils/algorithms/PolygonInset.js +424 -0
- package/src/esm/utils/algorithms/PolygonInset.js.map +1 -0
- package/src/esm/utils/algorithms/clearDuplicateVertices.d.ts +5 -4
- package/src/esm/utils/algorithms/clearDuplicateVertices.js +6 -3
- package/src/esm/utils/algorithms/clearDuplicateVertices.js.map +1 -1
- package/src/esm/utils/algorithms/findPolygonSelfIntersections.d.ts +2 -1
- package/src/esm/utils/algorithms/findPolygonSelfIntersections.js +3 -3
- package/src/esm/utils/algorithms/findPolygonSelfIntersections.js.map +1 -1
- package/src/esm/utils/algorithms/splitPolygonToNonIntersecting.d.ts +3 -2
- package/src/esm/utils/algorithms/splitPolygonToNonIntersecting.js +11 -9
- package/src/esm/utils/algorithms/splitPolygonToNonIntersecting.js.map +1 -1
- package/src/esm/utils/detectDarkMode.js.map +1 -1
- package/src/esm/utils/dom/guiSizeToggler.d.ts +2 -1
- package/src/esm/utils/dom/guiSizeToggler.js +4 -2
- package/src/esm/utils/dom/guiSizeToggler.js.map +1 -1
- package/src/ts/PlotBoilerplate.ts +7 -3
- package/src/ts/Polygon.ts +230 -9
- package/src/ts/Triangle.ts +18 -4
- package/src/ts/VertTuple.ts +15 -10
- package/src/ts/Vertex.ts +5 -2
- package/src/ts/draw.ts +2 -1
- package/src/ts/drawutilssvg.ts +29 -0
- package/src/ts/geomutils.ts +49 -1
- package/src/ts/utils/algorithms/PolygonInset.ts +529 -0
- package/src/ts/utils/algorithms/clearDuplicateVertices.ts +11 -8
- package/src/ts/utils/algorithms/findPolygonSelfIntersections.ts +25 -26
- package/src/ts/utils/algorithms/splitPolygonToNonIntersecting.ts +92 -99
- package/src/ts/utils/detectDarkMode.ts +1 -3
- package/src/ts/utils/dom/guiSizeToggler.ts +4 -2
|
@@ -0,0 +1,424 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculate the inset of any non-self-overlapping polygon.
|
|
3
|
+
*
|
|
4
|
+
* The polygon may be
|
|
5
|
+
* - convex or non-convex
|
|
6
|
+
* - self-intersecting
|
|
7
|
+
*
|
|
8
|
+
* The algorithms will
|
|
9
|
+
* - first construct a general offset polygon which may be corrupt due to ouf-of-bounds or in illegal-area problems.
|
|
10
|
+
* - then dissect the possibly self-intersecting inset polygon into separate simple polygons.
|
|
11
|
+
* - then detect for each if it's out of bounds or in some illegal area.
|
|
12
|
+
* - keeping only the valid ones.
|
|
13
|
+
*
|
|
14
|
+
* @required sutherlandHodgman
|
|
15
|
+
* @required splitPolygonToNonIntersecting
|
|
16
|
+
*
|
|
17
|
+
* @author Ikaros Kappler
|
|
18
|
+
* @date 2024-11-04 Ported the script to a class.
|
|
19
|
+
* @modified 2024-12-02 Ported to Typescript.
|
|
20
|
+
*/
|
|
21
|
+
import { Line } from "../../Line";
|
|
22
|
+
import { Polygon } from "../../Polygon";
|
|
23
|
+
import { Vector } from "../../Vector";
|
|
24
|
+
import { splitPolygonToNonIntersecting } from "./splitPolygonToNonIntersecting";
|
|
25
|
+
import { sutherlandHodgman } from "./sutherlandHodgman";
|
|
26
|
+
export class PolygonInset {
|
|
27
|
+
/**
|
|
28
|
+
* Constructs a new PolygonInset instance with the underlying given polygon to work with.
|
|
29
|
+
*
|
|
30
|
+
* Please note that the algorithm will reverse the vertex order if the given polygon
|
|
31
|
+
* is not clockwise.
|
|
32
|
+
*
|
|
33
|
+
* @param {Polygon} polygon - The polygon to calculate the offset for.
|
|
34
|
+
*/
|
|
35
|
+
constructor(polygon) {
|
|
36
|
+
/**
|
|
37
|
+
* The original polygon as a sequence of line objects. This is easier to use than a list of points.
|
|
38
|
+
*/
|
|
39
|
+
this.originalPolygonLines = [];
|
|
40
|
+
/**
|
|
41
|
+
* The simple inset lines, directly generated by offsetting the original polygon lines by the given amount.
|
|
42
|
+
*/
|
|
43
|
+
this.insetLines = [];
|
|
44
|
+
/**
|
|
45
|
+
* The cropped inset lines that makes up the first direct iteration of the inset polygon.
|
|
46
|
+
* This one will probably be self-intersecting.
|
|
47
|
+
*/
|
|
48
|
+
this.insetPolygonLines = [];
|
|
49
|
+
/**
|
|
50
|
+
* The inset polygon split up into simple non-self-intersecting polygons.
|
|
51
|
+
* Represented as a list of vertex-lists. Each sub-list represents a single polygon.
|
|
52
|
+
*/
|
|
53
|
+
this.splitPolygons = [];
|
|
54
|
+
this.polygon = polygon;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* This is the main method.
|
|
58
|
+
*
|
|
59
|
+
* Call this with the required option params to compute your desired offset polygon(s).
|
|
60
|
+
*
|
|
61
|
+
* @param {number} options.innerPolygonOffset - The offset to use. Should be a positive number. Correct result for negative numbers are not guaranteed.
|
|
62
|
+
* @param {number?} options.maxPolygonSplitDepth - (default is the number of polygon.vertices) The maximum amount of recursive stepts to simplify the complex first iteration of the offset polygon.
|
|
63
|
+
* @param {number?} options.intersectionEpsilon - (default is 1.0) The epsilon to use for detecting overlapping polygons. This indicates a tolerance value, because directly adjecent polygons may have a little arithmetic intersection area.
|
|
64
|
+
* @returns {Array<Array<Vertex>>} A list of vertex-lists, each one representing a simple polygon from the offset-polygons.
|
|
65
|
+
*/
|
|
66
|
+
computeOutputPolygons(options) {
|
|
67
|
+
var _a;
|
|
68
|
+
// No offset implies: no change.
|
|
69
|
+
if (options.innerPolygonOffset === 0) {
|
|
70
|
+
return [this.polygon.vertices]; // No change
|
|
71
|
+
}
|
|
72
|
+
// This algorithm only works with polygon in clockwise vertex order.
|
|
73
|
+
// If the polygon is not clockwise: revert.
|
|
74
|
+
PolygonInset._assertPolygonIsClockwise(this.polygon.vertices);
|
|
75
|
+
// The potential result polygons might overlap with other parts. These are not part of the desired
|
|
76
|
+
// result set and need to be filtered out.
|
|
77
|
+
// Define an intersection-epsilon: minimal critical overlaps might result of rounding errors and
|
|
78
|
+
// don't represent real overlaps.
|
|
79
|
+
const intersectionEpsilon = options.intersectionEpsilon;
|
|
80
|
+
// This will create a deep clone
|
|
81
|
+
this.optimizedPolygon = this.polygon.clone();
|
|
82
|
+
// In case the potential result polygons are self intersecting we need to split them apart later.
|
|
83
|
+
// Using the initial vertex number as the upper split limit should be safe here.
|
|
84
|
+
const maxPolygonSplitDepth = (_a = options === null || options === void 0 ? void 0 : options.maxPolygonSplitDepth) !== null && _a !== void 0 ? _a : this.optimizedPolygon.vertices.length;
|
|
85
|
+
// For calculations of initial inset rectagles we need the original polygon's lines.
|
|
86
|
+
this.originalPolygonLines = this.optimizedPolygon.getEdges();
|
|
87
|
+
this._collectInsetLines(this.originalPolygonLines, options.innerPolygonOffset);
|
|
88
|
+
this._collectInsetPolygonLines(this.insetLines);
|
|
89
|
+
this.insetRectanglePolygons = this._collectRectangularPolygonInsets(this.originalPolygonLines, this.insetLines);
|
|
90
|
+
// Optimize inset polygon AND the insetRectanglePolygons?
|
|
91
|
+
if (options.removeEars) {
|
|
92
|
+
// console.log("Remove ears");
|
|
93
|
+
// This will modify both params!
|
|
94
|
+
PolygonInset._optimizeInsetPolygon(this.optimizedPolygon, this.insetRectanglePolygons);
|
|
95
|
+
}
|
|
96
|
+
// Create a naive first inset polygon for later optimization.
|
|
97
|
+
this.insetPolygon = PolygonInset.convertToBasicInsetPolygon(this.insetPolygonLines);
|
|
98
|
+
// // Optimize inset polygon AND the insetRectanglePolygons!
|
|
99
|
+
// if (options.removeEars) {
|
|
100
|
+
// // This will modify both params!
|
|
101
|
+
// PolygonInset._optimizeInsetPolygon(this.insetPolygon, this.insetRectanglePolygons);
|
|
102
|
+
// }
|
|
103
|
+
// The resulting polygon will likely intersect itself multiple times.
|
|
104
|
+
// For further calculations split apart into non-self-intersecting.
|
|
105
|
+
this.splitPolygons = splitPolygonToNonIntersecting(this.insetPolygon.vertices, maxPolygonSplitDepth, true); // insideBoundsOnly
|
|
106
|
+
// This method was initially meant to calculate inset-polygons only.
|
|
107
|
+
// But with a simple filter we COULD also create outer offset-polygons.
|
|
108
|
+
// Maybe this is a task for the future
|
|
109
|
+
// console.log("DEBUG", DEBUG);
|
|
110
|
+
// DEBUG("TEST");
|
|
111
|
+
if (options.innerPolygonOffset < 0) {
|
|
112
|
+
// return [this.polygon.vertices]; // No change
|
|
113
|
+
}
|
|
114
|
+
// console.log("splitPolygons.length", this.splitPolygons.length);
|
|
115
|
+
// Assert all polygons are clockwise!
|
|
116
|
+
PolygonInset._assertAllPolygonsAreClockwise(this.splitPolygons);
|
|
117
|
+
this.filteredSplitPolygons = PolygonInset._filterInnerSplitPolygonsByCoverage(this.splitPolygons, this.insetRectanglePolygons, intersectionEpsilon);
|
|
118
|
+
// console.log("[0] filteredSplitPolygons.length", this.filteredSplitPolygons.length);
|
|
119
|
+
this.filteredSplitPolygons = PolygonInset._filterInnerSplitPolygonsByOriginalBounds(this.filteredSplitPolygons, this.polygon);
|
|
120
|
+
// console.log("[1] filteredSplitPolygons.length", this.filteredSplitPolygons.length);
|
|
121
|
+
return this.filteredSplitPolygons;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* This method transforms each polygon line into a new line
|
|
125
|
+
* by moving it to the inside direction of the polygon (by the given `insetAmount`).
|
|
126
|
+
*
|
|
127
|
+
* @param {Array<Line>} polygonLines
|
|
128
|
+
* @param {number} insetAmount
|
|
129
|
+
* @return {Array<Line>} The transformed lines. The result array has the same length and order as the input array.
|
|
130
|
+
*/
|
|
131
|
+
_collectInsetLines(polygonLines, insetAmount) {
|
|
132
|
+
const insetLines = []; // Array<Line>
|
|
133
|
+
for (var i = 0; i < polygonLines.length; i++) {
|
|
134
|
+
const line = polygonLines[i];
|
|
135
|
+
const perp = new Vector(line.a, line.b).perp();
|
|
136
|
+
const t = insetAmount / perp.length();
|
|
137
|
+
const offsetOnPerp = perp.vertAt(t);
|
|
138
|
+
const diff = line.a.difference(offsetOnPerp);
|
|
139
|
+
// Polygon is is clockwise order.
|
|
140
|
+
// Move line inside polygon
|
|
141
|
+
const movedLine = line.clone();
|
|
142
|
+
movedLine.a.add(diff);
|
|
143
|
+
movedLine.b.add(diff);
|
|
144
|
+
insetLines.push(movedLine);
|
|
145
|
+
}
|
|
146
|
+
this.insetLines = insetLines;
|
|
147
|
+
return insetLines;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* For a sequence of inset polygon lines get the inset polygon by detecting
|
|
151
|
+
* useful intersections (by cropping or extending them).
|
|
152
|
+
*
|
|
153
|
+
* The returned lines resemble a new polygon.
|
|
154
|
+
*
|
|
155
|
+
* Please note that the returned polygon can be self-intersecting!
|
|
156
|
+
*
|
|
157
|
+
* @param {Array<Line>} insetLines
|
|
158
|
+
* @returns {Array<Line>} The cropped or exented inset polygon lines.
|
|
159
|
+
*/
|
|
160
|
+
_collectInsetPolygonLines(insetLines) {
|
|
161
|
+
if (insetLines.length <= 1) {
|
|
162
|
+
return [];
|
|
163
|
+
}
|
|
164
|
+
const insetPolygonLines = [];
|
|
165
|
+
// Collect first intersection at beginning :)
|
|
166
|
+
const lastInsetLine = insetLines[insetLines.length - 1];
|
|
167
|
+
const firstInsetLine = insetLines[0];
|
|
168
|
+
let lastIntersectionPoint = lastInsetLine.intersection(firstInsetLine); // Must not be null
|
|
169
|
+
for (var i = 0; i < insetLines.length; i++) {
|
|
170
|
+
const insetLine = insetLines[i];
|
|
171
|
+
const nextInsetLine = insetLines[(i + 1) % insetLines.length];
|
|
172
|
+
// Find desired intersection
|
|
173
|
+
const intersection = insetLine.intersection(nextInsetLine);
|
|
174
|
+
if (intersection == null) {
|
|
175
|
+
console.warn("[collectInsetPolygon] WARN intersection line must not be null", i, nextInsetLine);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
// By construction they MUST have any non-null intersection!
|
|
179
|
+
if (lastIntersectionPoint != null) {
|
|
180
|
+
const resultLine = new Line(lastIntersectionPoint, intersection);
|
|
181
|
+
insetPolygonLines.push(resultLine);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
lastIntersectionPoint = intersection;
|
|
185
|
+
}
|
|
186
|
+
// Store intermediate result for later retrieval.
|
|
187
|
+
this.insetPolygonLines = insetPolygonLines;
|
|
188
|
+
return insetPolygonLines;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Converts two lists (same length) of original polygon lines and inset lines (interpreted as
|
|
192
|
+
* pairs) to a list of rectangular polyons.
|
|
193
|
+
*
|
|
194
|
+
* @static
|
|
195
|
+
* @param {Array<Line>} originalPolygonLines
|
|
196
|
+
* @param {Array<Line>} insetLines
|
|
197
|
+
* @returns {Array<Polygon>} A list of rectangular polygons; each returned polyon has exactly four vertices.
|
|
198
|
+
*/
|
|
199
|
+
_collectRectangularPolygonInsets(originalPolygonLines, insetLines) {
|
|
200
|
+
// Convert to rectangle polygon
|
|
201
|
+
const insetRectanglePolygons = originalPolygonLines.map((polygonLine, index) => {
|
|
202
|
+
const rectPolygon = new Polygon([], false);
|
|
203
|
+
// Add in original order
|
|
204
|
+
rectPolygon.vertices.push(polygonLine.a.clone());
|
|
205
|
+
rectPolygon.vertices.push(polygonLine.b.clone());
|
|
206
|
+
// Add in reverse order
|
|
207
|
+
const insetLine = insetLines[index];
|
|
208
|
+
rectPolygon.vertices.push(insetLine.b.clone());
|
|
209
|
+
rectPolygon.vertices.push(insetLine.a.clone());
|
|
210
|
+
return rectPolygon;
|
|
211
|
+
});
|
|
212
|
+
return insetRectanglePolygons;
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Converts a sequence of (hopefully adjacent) lines to a polygon by using all second line vertices `b`.
|
|
216
|
+
*
|
|
217
|
+
* @static
|
|
218
|
+
* @param insetPolygonLines
|
|
219
|
+
* @returns
|
|
220
|
+
*/
|
|
221
|
+
static convertToBasicInsetPolygon(insetPolygonLines) {
|
|
222
|
+
const insetPolygon = new Polygon([], false);
|
|
223
|
+
insetPolygonLines.forEach((insetLine) => {
|
|
224
|
+
insetPolygon.vertices.push(insetLine.a);
|
|
225
|
+
});
|
|
226
|
+
return insetPolygon;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Filter split polygons: only keep those whose vertices are all contained inside the original polygon.
|
|
230
|
+
* Reason: scaling too much will result in excessive translation beyond the opposite bounds of the polygon (like more than 200% of possible insetting).
|
|
231
|
+
*
|
|
232
|
+
* @param {Array<Array<Vertex>>} splitPolygonsVertices
|
|
233
|
+
* @param {Polygon} originalPolygon
|
|
234
|
+
* @return {Array<Array<Vertex>>} The filtered polygon list.
|
|
235
|
+
*/
|
|
236
|
+
static _filterInnerSplitPolygonsByOriginalBounds(splitPolygonsVertices, originalPolygon) {
|
|
237
|
+
return splitPolygonsVertices.filter((splitPolyVerts, _splitPolyIndex) => {
|
|
238
|
+
return splitPolyVerts.every((splitPVert) => {
|
|
239
|
+
return originalPolygon.containsVert(splitPVert);
|
|
240
|
+
});
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Filter split polygons: only keep those that do not (signifiantly) interset with any rectangles.
|
|
245
|
+
*
|
|
246
|
+
* @static
|
|
247
|
+
* @param {Array<Array<Vertex>>} splitPolygonsVertices
|
|
248
|
+
* @param {Array<Polygon>} insetRectanglePolygons
|
|
249
|
+
* @param {number?=1.0} intersectionEpsilon - (optional, default is 1.0) A epsislon to define a tolerance for checking if two polygons intersect.
|
|
250
|
+
*/
|
|
251
|
+
static _filterInnerSplitPolygonsByCoverage(splitPolygonsVertices, insetRectanglePolygons, intersectionEpsilon) {
|
|
252
|
+
// TEST: Add some jitter
|
|
253
|
+
// splitPolygonsVertices.forEach(split => {
|
|
254
|
+
// split.forEach(vert => {
|
|
255
|
+
// vert.x += (0.5 - Math.random()) * 0.01;
|
|
256
|
+
// vert.y += (0.5 - Math.random()) * 0.01;
|
|
257
|
+
// });
|
|
258
|
+
// });
|
|
259
|
+
splitPolygonsVertices.forEach((split, index) => {
|
|
260
|
+
const isCW = Polygon.utils.isClockwise(split);
|
|
261
|
+
if (!isCW) {
|
|
262
|
+
console.warn("------ split is not isClockwise!", index, isCW);
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
insetRectanglePolygons.forEach((rect, index) => {
|
|
266
|
+
const isCW = rect.isClockwise();
|
|
267
|
+
if (!isCW) {
|
|
268
|
+
console.warn("------ rect is not isClockwise!", index, isCW);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
const eps = intersectionEpsilon === undefined || typeof intersectionEpsilon === "undefined" ? 1.0 : intersectionEpsilon;
|
|
272
|
+
return splitPolygonsVertices.filter((splitPolyVerts, _splitPolyIndex) => {
|
|
273
|
+
const intersectionTestCallback = PolygonInset._hasIntersectionCallback(splitPolyVerts, eps, _splitPolyIndex);
|
|
274
|
+
const intersectsWithAnyRect = insetRectanglePolygons.some(intersectionTestCallback);
|
|
275
|
+
return !intersectsWithAnyRect;
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* This private method will reverse each polygon's vertex order that's not clockwise.
|
|
280
|
+
*
|
|
281
|
+
* @param {Array<Vertex[]>} polygons
|
|
282
|
+
*/
|
|
283
|
+
static _assertAllPolygonsAreClockwise(polygons) {
|
|
284
|
+
polygons.forEach((polygonVerts, _polyIndex) => {
|
|
285
|
+
// if (!Polygon.utils.isClockwise(polygonVerts)) {
|
|
286
|
+
// polygonVerts.reverse(); // Attention: this happens in-place (Array.reverse is destructive!)
|
|
287
|
+
// }
|
|
288
|
+
PolygonInset._assertPolygonIsClockwise(polygonVerts);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* This private method will revert the vertex order if the polygon is not clockwise.
|
|
293
|
+
*
|
|
294
|
+
* @param {Vertex[]} polygonVerts
|
|
295
|
+
*/
|
|
296
|
+
static _assertPolygonIsClockwise(polygonVerts) {
|
|
297
|
+
if (!Polygon.utils.isClockwise(polygonVerts)) {
|
|
298
|
+
polygonVerts.reverse(); // Attention: this happens in-place (Array.reverse is destructive!)
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Optimize the inset polygon by removing critical `ear` edges.
|
|
304
|
+
* Such an is identified by: the edge itself is completely located inside its neighbours inset rectangle.
|
|
305
|
+
*
|
|
306
|
+
* In this process multiple edges from the ear might be dropped and two adjacent edges get a new
|
|
307
|
+
* common intersection point.
|
|
308
|
+
*
|
|
309
|
+
* Note: this method is not working as expected in all cases and quite experimental.
|
|
310
|
+
*
|
|
311
|
+
* @param insetPolygon
|
|
312
|
+
* @param insetRectangles
|
|
313
|
+
*/
|
|
314
|
+
PolygonInset._optimizeInsetPolygon = (insetPolygon, insetRectangles) => {
|
|
315
|
+
// Locate edge that can be removed:
|
|
316
|
+
var earEdgeIndex = -1;
|
|
317
|
+
var maxLimit = insetPolygon.vertices.length;
|
|
318
|
+
var i = 0;
|
|
319
|
+
while (i++ < maxLimit &&
|
|
320
|
+
insetPolygon.vertices.length > 3 &&
|
|
321
|
+
(earEdgeIndex = PolygonInset._locateExcessiveEarEdge(insetPolygon, insetRectangles)) != -1) {
|
|
322
|
+
// console.log("REMOVE EAR EDGE", earEdgeIndex);
|
|
323
|
+
// As this is an ear edge: don't just remove the vertex.
|
|
324
|
+
// i) Remove the vertex and calculate the new intersection point of neighbour edges.
|
|
325
|
+
// ii) Update neightbour rectangles
|
|
326
|
+
var leftEdge = insetPolygon.getEdgeAt(earEdgeIndex - 2);
|
|
327
|
+
var rightEdge = insetPolygon.getEdgeAt(earEdgeIndex + 1);
|
|
328
|
+
var newIntersection = leftEdge.intersection(rightEdge);
|
|
329
|
+
newIntersection && rightEdge.a.set(newIntersection);
|
|
330
|
+
// Update rectangles
|
|
331
|
+
// ...
|
|
332
|
+
insetPolygon.vertices.splice(earEdgeIndex, 1);
|
|
333
|
+
insetRectangles.splice(earEdgeIndex, 1);
|
|
334
|
+
}
|
|
335
|
+
// TODO remove if not -1
|
|
336
|
+
};
|
|
337
|
+
PolygonInset._locateExcessiveEarEdge = (insetPolygon, insetRectangles) => {
|
|
338
|
+
for (var i = 0; i < insetPolygon.vertices.length; i++) {
|
|
339
|
+
const thisEdge = insetPolygon.getEdgeAt(i);
|
|
340
|
+
const leftIndex = i == 0 ? insetPolygon.vertices.length - 1 : i - 1;
|
|
341
|
+
// const rightIndex = i + 1 >= insetPolygon.vertices.length ? 0 : i + 1;
|
|
342
|
+
const leftRect = insetRectangles[leftIndex];
|
|
343
|
+
// const rightRect = insetRectangles[rightIndex];
|
|
344
|
+
const leftRectContainsThisEdge = PolygonInset._rectangleFullyContainsLine(leftRect, thisEdge);
|
|
345
|
+
if (leftRectContainsThisEdge) {
|
|
346
|
+
return i;
|
|
347
|
+
}
|
|
348
|
+
const leftEdge = insetPolygon.getEdgeAt(leftIndex);
|
|
349
|
+
const thisRect = insetRectangles[i];
|
|
350
|
+
const thisRectContainsLeftEdge = PolygonInset._rectangleFullyContainsLine(thisRect, leftEdge);
|
|
351
|
+
if (thisRectContainsLeftEdge) {
|
|
352
|
+
return i; // leftIndex;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return -1;
|
|
356
|
+
};
|
|
357
|
+
// Pre: rectangle.vertices.length === 4
|
|
358
|
+
PolygonInset._rectangleFullyContainsLine = (rectangle, edge) => {
|
|
359
|
+
var rectWidth = rectangle.getEdgeAt(0).length();
|
|
360
|
+
var rectHeight = rectangle.getEdgeAt(1).length();
|
|
361
|
+
// Both line corners must be at least 1% within the rectangle limits.
|
|
362
|
+
const eps = Math.min(rectWidth, rectHeight) / 100;
|
|
363
|
+
const edgeLen = edge.length();
|
|
364
|
+
var epsPointA = edge.vertAt(0.01); // edgeLen * eps);
|
|
365
|
+
var epsPointB = edge.vertAt(0.99); // 1.0 - edgeLen * eps);
|
|
366
|
+
var centerPoint = edge.vertAt(0.5);
|
|
367
|
+
return rectangle.containsVerts([epsPointA, epsPointB, centerPoint]);
|
|
368
|
+
};
|
|
369
|
+
/**
|
|
370
|
+
* For simplification I'm using a callback generator function here.
|
|
371
|
+
*
|
|
372
|
+
* @param splitPolyVerts
|
|
373
|
+
* @param eps
|
|
374
|
+
* @param _splitPolyIndex
|
|
375
|
+
* @returns {Function} The callback for an `Array.some` parameter.
|
|
376
|
+
*/
|
|
377
|
+
PolygonInset._hasIntersectionCallback = (splitPolyVerts, eps, _splitPolyIndex) => (rectanglePoly, _rectanglePolyIndex) => {
|
|
378
|
+
// const intersectionVerts: XYCoords[] = sutherlandHodgman(splitPolyVerts, rectanglePoly.vertices);
|
|
379
|
+
const intersectionVerts = sutherlandHodgman(rectanglePoly.vertices, splitPolyVerts);
|
|
380
|
+
// var intersection = GreinerHorman.intersection(sourcePolygon.vertices, clipPolygon.vertices);
|
|
381
|
+
// const uniqueIntersectionVerts = clearDuplicateVertices(intersectionVerts);
|
|
382
|
+
const intersectionAreaSize = Polygon.utils.area(intersectionVerts);
|
|
383
|
+
if (intersectionAreaSize < 0) {
|
|
384
|
+
console.warn("%cFound a polygon split with negative area. Counterclockwise against all odds?", "color: red; background: black;", // font-size: 30px",
|
|
385
|
+
"intersectionAreaSize", intersectionAreaSize, "_splitPolyIndex", _splitPolyIndex, "_rectanglePolyIndex", _rectanglePolyIndex, "intersectionVerts", intersectionVerts
|
|
386
|
+
// "uniqueIntersectionVerts",
|
|
387
|
+
// uniqueIntersectionVerts
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
// if (intersectionAreaSize >= eps) {
|
|
391
|
+
// console.log(
|
|
392
|
+
// "%cFound a polygon split for removal",
|
|
393
|
+
// "color: orange; background: grey;", // font-size: 30px",
|
|
394
|
+
// "intersectionAreaSize",
|
|
395
|
+
// intersectionAreaSize,
|
|
396
|
+
// "_splitPolyIndex",
|
|
397
|
+
// _splitPolyIndex,
|
|
398
|
+
// "_rectanglePolyIndex",
|
|
399
|
+
// _rectanglePolyIndex,
|
|
400
|
+
// "intersectionVerts",
|
|
401
|
+
// intersectionVerts
|
|
402
|
+
// // "uniqueIntersectionVerts",
|
|
403
|
+
// // uniqueIntersectionVerts
|
|
404
|
+
// );
|
|
405
|
+
// }
|
|
406
|
+
// if (intersectionAreaSize < eps) {
|
|
407
|
+
// // console.log("%cHello", "color: green; background: yellow; font-size: 30px");
|
|
408
|
+
// console.log(
|
|
409
|
+
// "Polygon split can be kept. Area is position but below epsilon.",
|
|
410
|
+
// "intersectionAreaSize",
|
|
411
|
+
// intersectionAreaSize,
|
|
412
|
+
// "_splitPolyIndex",
|
|
413
|
+
// _splitPolyIndex,
|
|
414
|
+
// "_rectanglePolyIndex",
|
|
415
|
+
// _rectanglePolyIndex,
|
|
416
|
+
// "intersectionVerts",
|
|
417
|
+
// intersectionVerts
|
|
418
|
+
// // "uniqueIntersectionVerts",
|
|
419
|
+
// // uniqueIntersectionVerts
|
|
420
|
+
// );
|
|
421
|
+
// }
|
|
422
|
+
return intersectionAreaSize >= eps;
|
|
423
|
+
};
|
|
424
|
+
//# sourceMappingURL=PolygonInset.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PolygonInset.js","sourceRoot":"","sources":["../../../ts/utils/algorithms/PolygonInset.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAGtC,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAYxD,MAAM,OAAO,YAAY;IAkDvB;;;;;;;OAOG;IACH,YAAY,OAAgB;QA/C5B;;WAEG;QACH,yBAAoB,GAAgB,EAAE,CAAC;QAEvC;;WAEG;QACH,eAAU,GAAgB,EAAE,CAAC;QAE7B;;;WAGG;QACH,sBAAiB,GAAgB,EAAE,CAAC;QAapC;;;WAGG;QACH,kBAAa,GAAyB,EAAE,CAAC;QAiBvC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IAED;;;;;;;;;OASG;IACI,qBAAqB,CAAC,OAA6B;;QACxD,gCAAgC;QAChC,IAAI,OAAO,CAAC,kBAAkB,KAAK,CAAC,EAAE,CAAC;YACrC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,YAAY;QAC9C,CAAC;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B,IAAI,GAAG,CAAC;AACrC,CAAC,AA1DoC,CA0DnC"}
|
|
@@ -6,16 +6,17 @@
|
|
|
6
6
|
* @author Ikaros Kappler
|
|
7
7
|
* @date 2021-07-13
|
|
8
8
|
* @modified 2023-10-28 Refactored and ported to Typescript.
|
|
9
|
-
* @
|
|
9
|
+
* @modified 2024-12-17 Simplified this method to work with generic sub types of XYCoords as well.
|
|
10
|
+
* @version 1.0.1
|
|
10
11
|
*/
|
|
11
|
-
import {
|
|
12
|
+
import { XYCoords } from "../../interfaces";
|
|
12
13
|
/**
|
|
13
14
|
* Filter the array and clear all duplicates.
|
|
14
15
|
*
|
|
15
16
|
* The original array is left unchanged. The vertices in the array are not cloned.
|
|
16
17
|
*
|
|
17
|
-
* @param {
|
|
18
|
+
* @param {XYCoords[]} vertices
|
|
18
19
|
* @param {number=EPS} epsilon
|
|
19
20
|
* @return {Vertex[]}
|
|
20
21
|
*/
|
|
21
|
-
export declare const clearDuplicateVertices: (vertices:
|
|
22
|
+
export declare const clearDuplicateVertices: <T extends XYCoords>(vertices: T[], epsilon?: number) => T[];
|
|
@@ -6,15 +6,17 @@
|
|
|
6
6
|
* @author Ikaros Kappler
|
|
7
7
|
* @date 2021-07-13
|
|
8
8
|
* @modified 2023-10-28 Refactored and ported to Typescript.
|
|
9
|
-
* @
|
|
9
|
+
* @modified 2024-12-17 Simplified this method to work with generic sub types of XYCoords as well.
|
|
10
|
+
* @version 1.0.1
|
|
10
11
|
*/
|
|
12
|
+
import { geomutils } from "../../geomutils";
|
|
11
13
|
const EPS = 0.000001;
|
|
12
14
|
/**
|
|
13
15
|
* Filter the array and clear all duplicates.
|
|
14
16
|
*
|
|
15
17
|
* The original array is left unchanged. The vertices in the array are not cloned.
|
|
16
18
|
*
|
|
17
|
-
* @param {
|
|
19
|
+
* @param {XYCoords[]} vertices
|
|
18
20
|
* @param {number=EPS} epsilon
|
|
19
21
|
* @return {Vertex[]}
|
|
20
22
|
*/
|
|
@@ -31,7 +33,8 @@ export const clearDuplicateVertices = (vertices, epsilon) => {
|
|
|
31
33
|
return result;
|
|
32
34
|
};
|
|
33
35
|
const isCloseTo = (vertA, vertB, eps) => {
|
|
34
|
-
return vertA.distance(vertB) < eps;
|
|
36
|
+
// return vertA.distance(vertB) < eps;
|
|
37
|
+
return geomutils.dist4(vertA.x, vertA.y, vertB.x, vertB.y) < eps;
|
|
35
38
|
};
|
|
36
39
|
const containsElementFrom = (vertices, vertex, fromIndex, epsilon) => {
|
|
37
40
|
for (var i = fromIndex; i < vertices.length; i++) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"clearDuplicateVertices.js","sourceRoot":"","sources":["../../../ts/utils/algorithms/clearDuplicateVertices.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"clearDuplicateVertices.js","sourceRoot":"","sources":["../../../ts/utils/algorithms/clearDuplicateVertices.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,MAAM,GAAG,GAAW,QAAQ,CAAC;AAE7B;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAqB,QAAa,EAAE,OAAgB,EAAE,EAAE;IAC5F,IAAI,OAAO,OAAO,KAAK,WAAW,EAAE,CAAC;QACnC,OAAO,GAAG,GAAG,CAAC;IAChB,CAAC;IACD,MAAM,MAAM,GAAa,EAAE,CAAC;IAC5B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACzC,IAAI,CAAC,mBAAmB,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,OAAO,CAAC,EAAE,CAAC;YAChE,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3B,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC,CAAC;AAEF,MAAM,SAAS,GAAG,CAAqB,KAAQ,EAAE,KAAQ,EAAE,GAAW,EAAW,EAAE;IACjF,sCAAsC;IACtC,OAAO,SAAS,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;AACnE,CAAC,CAAC;AAEF,MAAM,mBAAmB,GAAG,CAAqB,QAAkB,EAAE,MAAS,EAAE,SAAiB,EAAE,OAAe,EAAW,EAAE;IAC7H,KAAK,IAAI,CAAC,GAAG,SAAS,EAAE,CAAC,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjD,IAAI,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC;YAC5C,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC,CAAC"}
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @author Ikaros Kappler
|
|
6
6
|
* @date 2020-12-04
|
|
7
7
|
* @modified 2020-12-09 Ported from vanilla JS to Typescript.
|
|
8
|
+
* @modified 2024-11-22 Fixed a type error in line 41.
|
|
8
9
|
*/
|
|
9
10
|
import { Vertex } from "../../Vertex";
|
|
10
11
|
import { XYCoords } from "../../interfaces";
|
|
@@ -15,6 +16,6 @@ import { XYCoords } from "../../interfaces";
|
|
|
15
16
|
*
|
|
16
17
|
* @name findPolygonSelfIntersections
|
|
17
18
|
* @param {Array<Vertex>} vertices - The vertices that form the polygon.
|
|
18
|
-
* @return Array<Vertex>
|
|
19
|
+
* @return {Array<Vertex>}
|
|
19
20
|
*/
|
|
20
21
|
export declare const findPolygonSelfIntersections: (vertices: Array<XYCoords>) => Array<Vertex>;
|
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* @author Ikaros Kappler
|
|
6
6
|
* @date 2020-12-04
|
|
7
7
|
* @modified 2020-12-09 Ported from vanilla JS to Typescript.
|
|
8
|
+
* @modified 2024-11-22 Fixed a type error in line 41.
|
|
8
9
|
*/
|
|
9
10
|
import { Line } from "../../Line";
|
|
10
11
|
import { Vertex } from "../../Vertex";
|
|
@@ -15,7 +16,7 @@ import { Vertex } from "../../Vertex";
|
|
|
15
16
|
*
|
|
16
17
|
* @name findPolygonSelfIntersections
|
|
17
18
|
* @param {Array<Vertex>} vertices - The vertices that form the polygon.
|
|
18
|
-
* @return Array<Vertex>
|
|
19
|
+
* @return {Array<Vertex>}
|
|
19
20
|
*/
|
|
20
21
|
export const findPolygonSelfIntersections = (vertices) => {
|
|
21
22
|
const pointList = [];
|
|
@@ -36,8 +37,7 @@ export const findPolygonSelfIntersections = (vertices) => {
|
|
|
36
37
|
// The point is undefined if and only if both line are parallel (co-linear).
|
|
37
38
|
const intersectionPoint = lineA.intersection(lineB);
|
|
38
39
|
// Check if the intersection point is on both (finite) edges of the polygon.
|
|
39
|
-
if (intersectionPoint
|
|
40
|
-
&& lineA.hasPoint(intersectionPoint) && lineB.hasPoint(intersectionPoint)) {
|
|
40
|
+
if (intersectionPoint && lineA.hasPoint(intersectionPoint) && lineB.hasPoint(intersectionPoint)) {
|
|
41
41
|
pointList.push(intersectionPoint);
|
|
42
42
|
}
|
|
43
43
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findPolygonSelfIntersections.js","sourceRoot":"","sources":["../../../ts/utils/algorithms/findPolygonSelfIntersections.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"findPolygonSelfIntersections.js","sourceRoot":"","sources":["../../../ts/utils/algorithms/findPolygonSelfIntersections.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAClC,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,C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C,2DAA2D;YAC3D,4EAA4E;YAC5E,MAAM,iBAAiB,GAAkB,KAAK,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC;YAEnE,4EAA4E;YAC5E,IAAI,iBAAiB,IAAI,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,iBAAiB,CAAC,EAAE,CAAC;gBAChG,SAAS,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC;YACpC,CAAC;QACH,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC"}
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @author Ikaros Kappler
|
|
6
6
|
* @date 2020-12-04
|
|
7
7
|
* @modified 2020-12-07 Ported from vanilla JS to typescript.
|
|
8
|
-
* @
|
|
8
|
+
* @modified 2024-11-22 Added the `insideBoundsOnly` param to the `splitPolygonToNonIntersecting` algorithm.
|
|
9
|
+
* @version 1.1.0
|
|
9
10
|
*/
|
|
10
11
|
import { Vertex } from "../../Vertex";
|
|
11
12
|
/**
|
|
@@ -24,4 +25,4 @@ import { Vertex } from "../../Vertex";
|
|
|
24
25
|
* @param {number=10} maxDepth - Number of max recursive steps (default is 10).
|
|
25
26
|
* @return {Array<Array<Vertex>>} A sequence of non-self-intersecting sub polygons.
|
|
26
27
|
*/
|
|
27
|
-
export declare const splitPolygonToNonIntersecting: (vertices: Array<Vertex>, maxDepth?: number) => Array<Array<Vertex>>;
|
|
28
|
+
export declare const splitPolygonToNonIntersecting: (vertices: Array<Vertex>, maxDepth?: number, insideBoundsOnly?: boolean) => Array<Array<Vertex>>;
|
|
@@ -5,7 +5,8 @@
|
|
|
5
5
|
* @author Ikaros Kappler
|
|
6
6
|
* @date 2020-12-04
|
|
7
7
|
* @modified 2020-12-07 Ported from vanilla JS to typescript.
|
|
8
|
-
* @
|
|
8
|
+
* @modified 2024-11-22 Added the `insideBoundsOnly` param to the `splitPolygonToNonIntersecting` algorithm.
|
|
9
|
+
* @version 1.1.0
|
|
9
10
|
*/
|
|
10
11
|
import { Line } from "../../Line";
|
|
11
12
|
import { Vertex } from "../../Vertex";
|
|
@@ -30,12 +31,12 @@ export const splitPolygonToNonIntersecting = (() => {
|
|
|
30
31
|
* @param {Array<Vertex>} vertices
|
|
31
32
|
* @param {number=10} maxDepth
|
|
32
33
|
*/
|
|
33
|
-
const splitPolygonToNonIntersecting = (vertices, maxDepth) => {
|
|
34
|
+
const splitPolygonToNonIntersecting = (vertices, maxDepth, insideBoundsOnly) => {
|
|
34
35
|
if (typeof maxDepth === "undefined")
|
|
35
36
|
maxDepth = 10;
|
|
36
|
-
return _splitPolygonToNonIntersecting(vertices, maxDepth);
|
|
37
|
+
return _splitPolygonToNonIntersecting(vertices, maxDepth, insideBoundsOnly);
|
|
37
38
|
};
|
|
38
|
-
const _splitPolygonToNonIntersecting = (vertices, maxDepth) => {
|
|
39
|
+
const _splitPolygonToNonIntersecting = (vertices, maxDepth, insideBoundsOnly) => {
|
|
39
40
|
if (maxDepth <= 0) {
|
|
40
41
|
// aborting at max depth
|
|
41
42
|
return [vertices];
|
|
@@ -53,18 +54,19 @@ export const splitPolygonToNonIntersecting = (() => {
|
|
|
53
54
|
for (var b = 0; b < vertices.length; b++) {
|
|
54
55
|
// Equal edges or neighbour edges intersect by definition.
|
|
55
56
|
// We ignore them.
|
|
56
|
-
if (a == b || a + 1 == b || a == b + 1
|
|
57
|
-
|| (a == 0 && b + 1 == vertices.length) || (b == 0 && a + 1 == vertices.length))
|
|
57
|
+
if (a == b || a + 1 == b || a == b + 1 || (a == 0 && b + 1 == vertices.length) || (b == 0 && a + 1 == vertices.length))
|
|
58
58
|
continue;
|
|
59
59
|
lineB.a.set(vertices[b]);
|
|
60
60
|
lineB.b.set(vertices[(b + 1) % n]);
|
|
61
61
|
const intersectionPoint = lineA.intersection(lineB);
|
|
62
|
-
if (intersectionPoint &&
|
|
62
|
+
if (intersectionPoint &&
|
|
63
|
+
lineA.hasPoint(intersectionPoint, insideBoundsOnly) &&
|
|
64
|
+
lineB.hasPoint(intersectionPoint, insideBoundsOnly)) {
|
|
63
65
|
// Cut polygon into two here
|
|
64
66
|
const split = splitPolygonAt(vertices, a, b, intersectionPoint);
|
|
65
67
|
// Split has 2 elements.
|
|
66
|
-
const leftCleaned = _splitPolygonToNonIntersecting(split[0], maxDepth - 1);
|
|
67
|
-
const rightCleaned = _splitPolygonToNonIntersecting(split[1], maxDepth - 1);
|
|
68
|
+
const leftCleaned = _splitPolygonToNonIntersecting(split[0], maxDepth - 1, insideBoundsOnly);
|
|
69
|
+
const rightCleaned = _splitPolygonToNonIntersecting(split[1], maxDepth - 1, insideBoundsOnly);
|
|
68
70
|
return leftCleaned.concat(rightCleaned);
|
|
69
71
|
}
|
|
70
72
|
}
|