@ac-kit/math-geometry 0.2.3 → 0.4.0
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/CHANGELOG.md +321 -0
- package/README.md +363 -52
- package/dist/_brand.d.ts +14 -0
- package/dist/_brand.d.ts.map +1 -0
- package/dist/_brand.js +2 -0
- package/dist/_brand.js.map +1 -0
- package/dist/_ear-clipping.d.ts +20 -0
- package/dist/_ear-clipping.d.ts.map +1 -0
- package/dist/_ear-clipping.js +360 -0
- package/dist/_ear-clipping.js.map +1 -0
- package/dist/_polygon3-cross.d.ts +23 -0
- package/dist/_polygon3-cross.d.ts.map +1 -0
- package/dist/_polygon3-cross.js +418 -0
- package/dist/_polygon3-cross.js.map +1 -0
- package/dist/_ray3-intersect.d.ts +19 -0
- package/dist/_ray3-intersect.d.ts.map +1 -0
- package/dist/_ray3-intersect.js +122 -0
- package/dist/_ray3-intersect.js.map +1 -0
- package/dist/_triangle3-cross.d.ts +66 -0
- package/dist/_triangle3-cross.d.ts.map +1 -0
- package/dist/_triangle3-cross.js +396 -0
- package/dist/_triangle3-cross.js.map +1 -0
- package/dist/box2-array-boundary.d.ts +50 -0
- package/dist/box2-array-boundary.d.ts.map +1 -0
- package/dist/box2-array-boundary.js +97 -0
- package/dist/box2-array-boundary.js.map +1 -0
- package/dist/box2-array.d.ts +42 -0
- package/dist/box2-array.d.ts.map +1 -0
- package/dist/box2-array.js +134 -0
- package/dist/box2-array.js.map +1 -0
- package/dist/box2-componentwise.d.ts +110 -0
- package/dist/box2-componentwise.d.ts.map +1 -0
- package/dist/box2-componentwise.js +325 -0
- package/dist/box2-componentwise.js.map +1 -0
- package/dist/box2-grid-index.d.ts +67 -0
- package/dist/box2-grid-index.d.ts.map +1 -0
- package/dist/box2-grid-index.js +198 -0
- package/dist/box2-grid-index.js.map +1 -0
- package/dist/box2-record.d.ts +83 -0
- package/dist/box2-record.d.ts.map +1 -0
- package/dist/box2-record.js +124 -0
- package/dist/box2-record.js.map +1 -0
- package/dist/box2.d.ts +38 -0
- package/dist/box2.d.ts.map +1 -0
- package/dist/box2.js +97 -0
- package/dist/box2.js.map +1 -0
- package/dist/box3-array-boundary.d.ts +50 -0
- package/dist/box3-array-boundary.d.ts.map +1 -0
- package/dist/box3-array-boundary.js +107 -0
- package/dist/box3-array-boundary.js.map +1 -0
- package/dist/box3-componentwise.d.ts +110 -0
- package/dist/box3-componentwise.d.ts.map +1 -0
- package/dist/box3-componentwise.js +392 -0
- package/dist/box3-componentwise.js.map +1 -0
- package/dist/box3-record.d.ts +92 -0
- package/dist/box3-record.d.ts.map +1 -0
- package/dist/box3-record.js +147 -0
- package/dist/box3-record.js.map +1 -0
- package/dist/box3.d.ts +19 -16
- package/dist/box3.d.ts.map +1 -1
- package/dist/box3.js +73 -76
- package/dist/box3.js.map +1 -1
- package/dist/box4-array-boundary.d.ts +50 -0
- package/dist/box4-array-boundary.d.ts.map +1 -0
- package/dist/box4-array-boundary.js +117 -0
- package/dist/box4-array-boundary.js.map +1 -0
- package/dist/box4-componentwise.d.ts +110 -0
- package/dist/box4-componentwise.d.ts.map +1 -0
- package/dist/box4-componentwise.js +459 -0
- package/dist/box4-componentwise.js.map +1 -0
- package/dist/box4-record.d.ts +96 -0
- package/dist/box4-record.d.ts.map +1 -0
- package/dist/box4-record.js +167 -0
- package/dist/box4-record.js.map +1 -0
- package/dist/box4.d.ts +13 -8
- package/dist/box4.d.ts.map +1 -1
- package/dist/box4.js +27 -5
- package/dist/box4.js.map +1 -1
- package/dist/circle2-array-boundary.d.ts +51 -0
- package/dist/circle2-array-boundary.d.ts.map +1 -0
- package/dist/circle2-array-boundary.js +93 -0
- package/dist/circle2-array-boundary.js.map +1 -0
- package/dist/circle2-array.d.ts +17 -0
- package/dist/circle2-array.d.ts.map +1 -0
- package/dist/circle2-array.js +44 -0
- package/dist/circle2-array.js.map +1 -0
- package/dist/circle2-record.d.ts +81 -0
- package/dist/circle2-record.d.ts.map +1 -0
- package/dist/circle2-record.js +114 -0
- package/dist/circle2-record.js.map +1 -0
- package/dist/circle2.d.ts +27 -82
- package/dist/circle2.d.ts.map +1 -1
- package/dist/circle2.js +59 -104
- package/dist/circle2.js.map +1 -1
- package/dist/coordinates/cylindrical-record.d.ts +86 -0
- package/dist/coordinates/cylindrical-record.d.ts.map +1 -0
- package/dist/coordinates/cylindrical-record.js +114 -0
- package/dist/coordinates/cylindrical-record.js.map +1 -0
- package/dist/coordinates/horizontal-record.d.ts +86 -0
- package/dist/coordinates/horizontal-record.d.ts.map +1 -0
- package/dist/coordinates/horizontal-record.js +114 -0
- package/dist/coordinates/horizontal-record.js.map +1 -0
- package/dist/coordinates/horizontal.d.ts +48 -0
- package/dist/coordinates/horizontal.d.ts.map +1 -0
- package/dist/coordinates/horizontal.js +100 -0
- package/dist/coordinates/horizontal.js.map +1 -0
- package/dist/coordinates/index.d.ts +10 -0
- package/dist/coordinates/index.d.ts.map +1 -0
- package/dist/coordinates/index.js +10 -0
- package/dist/coordinates/index.js.map +1 -0
- package/dist/coordinates/longitude-latitude-record.d.ts +81 -0
- package/dist/coordinates/longitude-latitude-record.d.ts.map +1 -0
- package/dist/coordinates/longitude-latitude-record.js +104 -0
- package/dist/coordinates/longitude-latitude-record.js.map +1 -0
- package/dist/coordinates/longitude-latitude.d.ts +33 -0
- package/dist/coordinates/longitude-latitude.d.ts.map +1 -0
- package/dist/coordinates/longitude-latitude.js +78 -0
- package/dist/coordinates/longitude-latitude.js.map +1 -0
- package/dist/coordinates/polar-record.d.ts +77 -0
- package/dist/coordinates/polar-record.d.ts.map +1 -0
- package/dist/coordinates/polar-record.js +104 -0
- package/dist/coordinates/polar-record.js.map +1 -0
- package/dist/coordinates/polar.d.ts +28 -0
- package/dist/coordinates/polar.d.ts.map +1 -0
- package/dist/coordinates/polar.js +68 -0
- package/dist/coordinates/polar.js.map +1 -0
- package/dist/coordinates/spherical-cylindrical.d.ts +49 -0
- package/dist/coordinates/spherical-cylindrical.d.ts.map +1 -0
- package/dist/coordinates/spherical-cylindrical.js +110 -0
- package/dist/coordinates/spherical-cylindrical.js.map +1 -0
- package/dist/coordinates/spherical-record.d.ts +86 -0
- package/dist/coordinates/spherical-record.d.ts.map +1 -0
- package/dist/coordinates/spherical-record.js +114 -0
- package/dist/coordinates/spherical-record.js.map +1 -0
- package/dist/frustum3-record.d.ts +132 -0
- package/dist/frustum3-record.d.ts.map +1 -0
- package/dist/frustum3-record.js +333 -0
- package/dist/frustum3-record.js.map +1 -0
- package/dist/frustum3.d.ts +60 -0
- package/dist/frustum3.d.ts.map +1 -0
- package/dist/frustum3.js +186 -0
- package/dist/frustum3.js.map +1 -0
- package/dist/index.d.ts +36 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +36 -16
- package/dist/index.js.map +1 -1
- package/dist/line2-record.d.ts +87 -0
- package/dist/line2-record.d.ts.map +1 -0
- package/dist/line2-record.js +123 -0
- package/dist/line2-record.js.map +1 -0
- package/dist/line2.d.ts +24 -33
- package/dist/line2.d.ts.map +1 -1
- package/dist/line2.js +78 -52
- package/dist/line2.js.map +1 -1
- package/dist/line3-record.d.ts +91 -0
- package/dist/line3-record.d.ts.map +1 -0
- package/dist/line3-record.js +146 -0
- package/dist/line3-record.js.map +1 -0
- package/dist/line3.d.ts +11 -24
- package/dist/line3.d.ts.map +1 -1
- package/dist/line3.js +32 -36
- package/dist/line3.js.map +1 -1
- package/dist/mesh/_generator-kit.d.ts +14 -0
- package/dist/mesh/_generator-kit.d.ts.map +1 -0
- package/dist/mesh/_generator-kit.js +39 -0
- package/dist/mesh/_generator-kit.js.map +1 -0
- package/dist/mesh/_index-kernels-u16.d.ts +85 -0
- package/dist/mesh/_index-kernels-u16.d.ts.map +1 -0
- package/dist/mesh/_index-kernels-u16.js +1045 -0
- package/dist/mesh/_index-kernels-u16.js.map +1 -0
- package/dist/mesh/_index-kernels-u32.d.ts +85 -0
- package/dist/mesh/_index-kernels-u32.d.ts.map +1 -0
- package/dist/mesh/_index-kernels-u32.js +1045 -0
- package/dist/mesh/_index-kernels-u32.js.map +1 -0
- package/dist/mesh/generate-box.d.ts +16 -0
- package/dist/mesh/generate-box.d.ts.map +1 -0
- package/dist/mesh/generate-box.js +79 -0
- package/dist/mesh/generate-box.js.map +1 -0
- package/dist/mesh/generate-cylinder.d.ts +25 -0
- package/dist/mesh/generate-cylinder.d.ts.map +1 -0
- package/dist/mesh/generate-cylinder.js +117 -0
- package/dist/mesh/generate-cylinder.js.map +1 -0
- package/dist/mesh/generate-heightfield.d.ts +24 -0
- package/dist/mesh/generate-heightfield.d.ts.map +1 -0
- package/dist/mesh/generate-heightfield.js +48 -0
- package/dist/mesh/generate-heightfield.js.map +1 -0
- package/dist/mesh/generate-plane.d.ts +18 -0
- package/dist/mesh/generate-plane.d.ts.map +1 -0
- package/dist/mesh/generate-plane.js +39 -0
- package/dist/mesh/generate-plane.js.map +1 -0
- package/dist/mesh/generate-sphere.d.ts +50 -0
- package/dist/mesh/generate-sphere.d.ts.map +1 -0
- package/dist/mesh/generate-sphere.js +244 -0
- package/dist/mesh/generate-sphere.js.map +1 -0
- package/dist/mesh/index.d.ts +8 -0
- package/dist/mesh/index.d.ts.map +1 -0
- package/dist/mesh/index.js +8 -0
- package/dist/mesh/index.js.map +1 -0
- package/dist/mesh/mesh3-kernels.d.ts +122 -0
- package/dist/mesh/mesh3-kernels.d.ts.map +1 -0
- package/dist/mesh/mesh3-kernels.js +220 -0
- package/dist/mesh/mesh3-kernels.js.map +1 -0
- package/dist/mesh/mesh3.d.ts +66 -0
- package/dist/mesh/mesh3.d.ts.map +1 -0
- package/dist/mesh/mesh3.js +128 -0
- package/dist/mesh/mesh3.js.map +1 -0
- package/dist/plane3-record.d.ts +88 -0
- package/dist/plane3-record.d.ts.map +1 -0
- package/dist/plane3-record.js +123 -0
- package/dist/plane3-record.js.map +1 -0
- package/dist/plane3.d.ts +43 -28
- package/dist/plane3.d.ts.map +1 -1
- package/dist/plane3.js +149 -48
- package/dist/plane3.js.map +1 -1
- package/dist/polygon2.d.ts +132 -29
- package/dist/polygon2.d.ts.map +1 -1
- package/dist/polygon2.js +529 -96
- package/dist/polygon2.js.map +1 -1
- package/dist/polygon3.d.ts +56 -21
- package/dist/polygon3.d.ts.map +1 -1
- package/dist/polygon3.js +239 -91
- package/dist/polygon3.js.map +1 -1
- package/dist/polyline2.d.ts +37 -0
- package/dist/polyline2.d.ts.map +1 -0
- package/dist/polyline2.js +146 -0
- package/dist/polyline2.js.map +1 -0
- package/dist/polyline3.d.ts +35 -0
- package/dist/polyline3.d.ts.map +1 -0
- package/dist/polyline3.js +159 -0
- package/dist/polyline3.js.map +1 -0
- package/dist/quadtree.d.ts +255 -0
- package/dist/quadtree.d.ts.map +1 -0
- package/dist/quadtree.js +1096 -0
- package/dist/quadtree.js.map +1 -0
- package/dist/ray2-record.d.ts +86 -0
- package/dist/ray2-record.d.ts.map +1 -0
- package/dist/ray2-record.js +123 -0
- package/dist/ray2-record.js.map +1 -0
- package/dist/ray2.d.ts +38 -13
- package/dist/ray2.d.ts.map +1 -1
- package/dist/ray2.js +85 -22
- package/dist/ray2.js.map +1 -1
- package/dist/ray3-array.d.ts +27 -0
- package/dist/ray3-array.d.ts.map +1 -0
- package/dist/ray3-array.js +89 -0
- package/dist/ray3-array.js.map +1 -0
- package/dist/ray3-record.d.ts +90 -0
- package/dist/ray3-record.d.ts.map +1 -0
- package/dist/ray3-record.js +146 -0
- package/dist/ray3-record.js.map +1 -0
- package/dist/ray3.d.ts +72 -25
- package/dist/ray3.d.ts.map +1 -1
- package/dist/ray3.js +233 -64
- package/dist/ray3.js.map +1 -1
- package/dist/segment2-record.d.ts +80 -0
- package/dist/segment2-record.d.ts.map +1 -0
- package/dist/segment2-record.js +122 -0
- package/dist/segment2-record.js.map +1 -0
- package/dist/segment2.d.ts +39 -19
- package/dist/segment2.d.ts.map +1 -1
- package/dist/segment2.js +104 -42
- package/dist/segment2.js.map +1 -1
- package/dist/segment3-record.d.ts +90 -0
- package/dist/segment3-record.d.ts.map +1 -0
- package/dist/segment3-record.js +146 -0
- package/dist/segment3-record.js.map +1 -0
- package/dist/segment3.d.ts +24 -12
- package/dist/segment3.d.ts.map +1 -1
- package/dist/segment3.js +67 -30
- package/dist/segment3.js.map +1 -1
- package/dist/size.d.ts +40 -0
- package/dist/size.d.ts.map +1 -0
- package/dist/size.js +72 -0
- package/dist/size.js.map +1 -0
- package/dist/sphere3-array-boundary.d.ts +51 -0
- package/dist/sphere3-array-boundary.d.ts.map +1 -0
- package/dist/sphere3-array-boundary.js +98 -0
- package/dist/sphere3-array-boundary.js.map +1 -0
- package/dist/sphere3-record.d.ts +88 -0
- package/dist/sphere3-record.d.ts.map +1 -0
- package/dist/sphere3-record.js +123 -0
- package/dist/sphere3-record.js.map +1 -0
- package/dist/sphere3.d.ts +55 -12
- package/dist/sphere3.d.ts.map +1 -1
- package/dist/sphere3.js +233 -24
- package/dist/sphere3.js.map +1 -1
- package/dist/spherical/cap.d.ts +56 -0
- package/dist/spherical/cap.d.ts.map +1 -0
- package/dist/spherical/cap.js +363 -0
- package/dist/spherical/cap.js.map +1 -0
- package/dist/spherical/great-circle.d.ts +39 -0
- package/dist/spherical/great-circle.d.ts.map +1 -0
- package/dist/spherical/great-circle.js +158 -0
- package/dist/spherical/great-circle.js.map +1 -0
- package/dist/spherical/index.d.ts +3 -0
- package/dist/spherical/index.d.ts.map +1 -0
- package/dist/spherical/index.js +3 -0
- package/dist/spherical/index.js.map +1 -0
- package/dist/triangle2-record.d.ts +91 -0
- package/dist/triangle2-record.d.ts.map +1 -0
- package/dist/triangle2-record.js +146 -0
- package/dist/triangle2-record.js.map +1 -0
- package/dist/triangle2.d.ts +29 -14
- package/dist/triangle2.d.ts.map +1 -1
- package/dist/triangle2.js +86 -22
- package/dist/triangle2.js.map +1 -1
- package/dist/triangle3-record.d.ts +97 -0
- package/dist/triangle3-record.d.ts.map +1 -0
- package/dist/triangle3-record.js +178 -0
- package/dist/triangle3-record.js.map +1 -0
- package/dist/triangle3.d.ts +60 -17
- package/dist/triangle3.d.ts.map +1 -1
- package/dist/triangle3.js +287 -38
- package/dist/triangle3.js.map +1 -1
- package/package.json +15 -8
- package/dist/angles.d.ts +0 -31
- package/dist/angles.d.ts.map +0 -1
- package/dist/angles.js +0 -53
- package/dist/angles.js.map +0 -1
- package/dist/coordinate-spaces.d.ts +0 -39
- package/dist/coordinate-spaces.d.ts.map +0 -1
- package/dist/coordinate-spaces.js +0 -52
- package/dist/coordinate-spaces.js.map +0 -1
- package/dist/coordinates-vec2.d.ts +0 -7
- package/dist/coordinates-vec2.d.ts.map +0 -1
- package/dist/coordinates-vec2.js +0 -35
- package/dist/coordinates-vec2.js.map +0 -1
- package/dist/coordinates-vec3.d.ts +0 -11
- package/dist/coordinates-vec3.d.ts.map +0 -1
- package/dist/coordinates-vec3.js +0 -61
- package/dist/coordinates-vec3.js.map +0 -1
- package/dist/coordinates.d.ts +0 -54
- package/dist/coordinates.d.ts.map +0 -1
- package/dist/coordinates.js +0 -2
- package/dist/coordinates.js.map +0 -1
- package/dist/euler-angles-quaternion.d.ts +0 -7
- package/dist/euler-angles-quaternion.d.ts.map +0 -1
- package/dist/euler-angles-quaternion.js +0 -100
- package/dist/euler-angles-quaternion.js.map +0 -1
- package/dist/euler-angles.d.ts +0 -33
- package/dist/euler-angles.d.ts.map +0 -1
- package/dist/euler-angles.js +0 -35
- package/dist/euler-angles.js.map +0 -1
- package/dist/geometry-vectors.d.ts +0 -11
- package/dist/geometry-vectors.d.ts.map +0 -1
- package/dist/geometry-vectors.js +0 -19
- package/dist/geometry-vectors.js.map +0 -1
- package/dist/mesh3.d.ts +0 -32
- package/dist/mesh3.d.ts.map +0 -1
- package/dist/mesh3.js +0 -142
- package/dist/mesh3.js.map +0 -1
- package/dist/point2.d.ts +0 -41
- package/dist/point2.d.ts.map +0 -1
- package/dist/point2.js +0 -92
- package/dist/point2.js.map +0 -1
- package/dist/point3.d.ts +0 -43
- package/dist/point3.d.ts.map +0 -1
- package/dist/point3.js +0 -103
- package/dist/point3.js.map +0 -1
- package/dist/point4.d.ts +0 -12
- package/dist/point4.d.ts.map +0 -1
- package/dist/point4.js +0 -28
- package/dist/point4.js.map +0 -1
- package/dist/rect2.d.ts +0 -54
- package/dist/rect2.d.ts.map +0 -1
- package/dist/rect2.js +0 -149
- package/dist/rect2.js.map +0 -1
- package/dist/size2.d.ts +0 -33
- package/dist/size2.d.ts.map +0 -1
- package/dist/size2.js +0 -81
- package/dist/size2.js.map +0 -1
- package/dist/size3.d.ts +0 -21
- package/dist/size3.d.ts.map +0 -1
- package/dist/size3.js +0 -59
- package/dist/size3.js.map +0 -1
- package/dist/size4.d.ts +0 -10
- package/dist/size4.d.ts.map +0 -1
- package/dist/size4.js +0 -16
- package/dist/size4.js.map +0 -1
package/dist/polygon2.js
CHANGED
|
@@ -1,127 +1,560 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { point2Distance, point2Equals } from "./point2.js";
|
|
1
|
+
import { scaleByPowerOfTwoInPlace, viewPrefixesMayOverlap } from "@ac-kit/core";
|
|
2
|
+
import { dotExactInto } from "@ac-kit/math-scalar";
|
|
3
|
+
import { earClip_ } from "./_ear-clipping.js";
|
|
5
4
|
/**
|
|
6
|
-
*
|
|
7
|
-
*
|
|
5
|
+
* Polygons are packed `Float64Array`s of `x, y` pairs, rings implicitly closed
|
|
6
|
+
* (the last vertex joins the first), with holes given by the vertex index where
|
|
7
|
+
* each hole ring starts (earcut's convention).
|
|
8
8
|
*/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
return
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const a = p.vertices[i];
|
|
37
|
-
const b = p.vertices[(i + 1) % n];
|
|
38
|
-
const cross = diffOfProducts(a.x, b.y, b.x, a.y);
|
|
39
|
-
cx.add((a.x + b.x) * cross);
|
|
40
|
-
cy.add((a.y + b.y) * cross);
|
|
41
|
-
signedArea.add(cross);
|
|
42
|
-
}
|
|
43
|
-
const area = signedArea.value;
|
|
44
|
-
if (area === 0) {
|
|
45
|
-
const vx = new PreciseSum();
|
|
46
|
-
const vy = new PreciseSum();
|
|
47
|
-
for (const v of p.vertices) {
|
|
48
|
-
vx.add(v.x);
|
|
49
|
-
vy.add(v.y);
|
|
9
|
+
function assertCoordinates_(name, coordinates) {
|
|
10
|
+
if (coordinates.length % 2 !== 0) {
|
|
11
|
+
throw new RangeError(`${name}: coordinates has ${coordinates.length} numbers, not a multiple of 2.`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The vertex count a kernel reads: `vertexCount`, checked against the
|
|
16
|
+
* coordinates, or all of them (an even length) when it is omitted.
|
|
17
|
+
*/
|
|
18
|
+
function vertexCountOf_(name, coordinates, vertexCount) {
|
|
19
|
+
if (vertexCount === undefined) {
|
|
20
|
+
assertCoordinates_(name, coordinates);
|
|
21
|
+
return coordinates.length / 2;
|
|
22
|
+
}
|
|
23
|
+
if (!(vertexCount >= 0 &&
|
|
24
|
+
2 * vertexCount <= coordinates.length &&
|
|
25
|
+
Math.floor(vertexCount) === vertexCount)) {
|
|
26
|
+
throw new RangeError(`${name}: vertexCount is ${vertexCount}, not an integer from 0 to ${Math.floor(coordinates.length / 2)}.`);
|
|
27
|
+
}
|
|
28
|
+
return vertexCount;
|
|
29
|
+
}
|
|
30
|
+
function assertHoleStarts_(name, vertexCount, holeStarts) {
|
|
31
|
+
let previous = 0;
|
|
32
|
+
for (let hole = 0; hole < holeStarts.length; hole++) {
|
|
33
|
+
const start = holeStarts[hole];
|
|
34
|
+
if (start <= previous || start >= vertexCount) {
|
|
35
|
+
throw new RangeError(`${name}: holeStarts[${hole}] is ${start}, which is not above the previous start (${previous}) and below the vertex count (${vertexCount}).`);
|
|
50
36
|
}
|
|
51
|
-
|
|
37
|
+
previous = start;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The signed area: positive when the vertices run counter-clockwise in a y-up
|
|
42
|
+
* frame (clockwise on a y-down screen). Coordinates are taken relative to the
|
|
43
|
+
* first vertex, so a polygon far from the origin loses no digits. Fewer than
|
|
44
|
+
* three vertices give `0`. O(n).
|
|
45
|
+
*
|
|
46
|
+
* @param vertexCount How many vertices, from the start of `coordinates`, make
|
|
47
|
+
* the polygon; all of them when omitted. A count lets a polygon live in a
|
|
48
|
+
* longer working buffer, as {@link polygon2ClipHalfPlane} writes it.
|
|
49
|
+
* @throws {RangeError} When `vertexCount` is omitted and `coordinates.length`
|
|
50
|
+
* is odd, or given and not an integer from 0 to `coordinates.length / 2`.
|
|
51
|
+
*/
|
|
52
|
+
export function polygon2SignedArea(coordinates, vertexCount) {
|
|
53
|
+
const count = vertexCountOf_("polygon2SignedArea", coordinates, vertexCount);
|
|
54
|
+
if (count < 3) {
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
const originX = coordinates[0];
|
|
58
|
+
const originY = coordinates[1];
|
|
59
|
+
let sum = 0;
|
|
60
|
+
for (let index = 1; index + 1 < count; index++) {
|
|
61
|
+
sum +=
|
|
62
|
+
(coordinates[2 * index] - originX) *
|
|
63
|
+
(coordinates[2 * index + 3] - originY) -
|
|
64
|
+
(coordinates[2 * index + 2] - originX) *
|
|
65
|
+
(coordinates[2 * index + 1] - originY);
|
|
66
|
+
}
|
|
67
|
+
return sum / 2;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The area, never negative. O(n).
|
|
71
|
+
*
|
|
72
|
+
* @param vertexCount As {@link polygon2SignedArea} takes it.
|
|
73
|
+
* @throws {RangeError} As {@link polygon2SignedArea} throws.
|
|
74
|
+
*/
|
|
75
|
+
export function polygon2Area(coordinates, vertexCount) {
|
|
76
|
+
return Math.abs(polygon2SignedArea(coordinates, vertexCount));
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Sutherland–Hodgman against one line: writes into `out` the polygon of the
|
|
80
|
+
* first `vertexCount` vertices of `coordinates` cut to the closed half-plane
|
|
81
|
+
* `normalX · x + normalY · y ≤ limit`, read as `(normalX, normalY, limit)` from
|
|
82
|
+
* `halfPlanes[index]` on, and returns its vertex count. A convex polygon gives
|
|
83
|
+
* the convex intersection, at most `vertexCount + 1` vertices; a concave one
|
|
84
|
+
* gives pieces joined by bridges of zero width along the line, the same area as
|
|
85
|
+
* the intersection, at most `⌊3 · vertexCount / 2⌋` vertices, which `out` must
|
|
86
|
+
* have room for. A vertex on the line is kept, and an edge that only touches
|
|
87
|
+
* the line adds no vertex; the normal need not be unit, and an infinite `limit`
|
|
88
|
+
* keeps everything (`+∞`) or nothing (`−∞`), as a zero normal does by the sign
|
|
89
|
+
* of `limit`. The half-plane is scaled by a power of two, exactly, before any
|
|
90
|
+
* side is evaluated, so every finite input, coordinates up to
|
|
91
|
+
* `Number.MAX_VALUE` included, gives finite vertices; a vertex the scaled
|
|
92
|
+
* evaluation cannot place beyond its rounding error is placed by the exact sign
|
|
93
|
+
* of `normal · p − limit` (math-scalar's `dotExactInto`), so which vertices are
|
|
94
|
+
* kept is exact, and a crossing is then found from the exactly rounded sides,
|
|
95
|
+
* every component of the normal kept.
|
|
96
|
+
*
|
|
97
|
+
* The clipping that area-coverage antialiasing and polygon rasterisers repeat
|
|
98
|
+
* per pixel, so nothing is allocated, inlined or not: the polygon, its clip and
|
|
99
|
+
* the half-plane live in caller buffers (a double passed to a call the engine
|
|
100
|
+
* does not inline is boxed), and one buffer can hold the half-planes of a whole
|
|
101
|
+
* clip window, `index` choosing one. O(n).
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* const square = new Float64Array([0, 0, 4, 0, 4, 4, 0, 4]);
|
|
105
|
+
* const out = new Float64Array(12);
|
|
106
|
+
* const left = new Float64Array([1, 0, 1]); // x ≤ 1
|
|
107
|
+
* const count = polygon2ClipHalfPlane(out, square, 4, left); // 4
|
|
108
|
+
* polygon2Area(out, count); // 4
|
|
109
|
+
*
|
|
110
|
+
* @param out At least `2 · ⌊3 · vertexCount / 2⌋` entries, sharing no memory
|
|
111
|
+
* with the vertices read; its first `2 ·` (returned count) entries are
|
|
112
|
+
* written.
|
|
113
|
+
* @param halfPlanes Holds the half-plane at `index`, read before anything is
|
|
114
|
+
* written (it may share memory with `out`).
|
|
115
|
+
* @param index Where the half-plane starts in `halfPlanes`; `0` by default.
|
|
116
|
+
* @returns How many vertices `out` holds.
|
|
117
|
+
* @throws {RangeError} When `vertexCount` is not an integer from 0 to
|
|
118
|
+
* `coordinates.length / 2`, `index` is not an integer with three entries from
|
|
119
|
+
* it in `halfPlanes`, `out` is too short or shares memory with the vertices,
|
|
120
|
+
* the normal is not finite or `limit` is `NaN`; nothing is written.
|
|
121
|
+
*/
|
|
122
|
+
export function polygon2ClipHalfPlane(out, coordinates, vertexCount, halfPlanes, index = 0) {
|
|
123
|
+
const count = vertexCountOf_("polygon2ClipHalfPlane", coordinates, vertexCount);
|
|
124
|
+
const capacity = 2 * (count + Math.floor(count / 2));
|
|
125
|
+
if (out.length < capacity) {
|
|
126
|
+
throw new RangeError(`polygon2ClipHalfPlane: out has ${out.length} entries, ${capacity} are needed for ${count} vertices.`);
|
|
127
|
+
}
|
|
128
|
+
if (!(index >= 0 &&
|
|
129
|
+
index + 3 <= halfPlanes.length &&
|
|
130
|
+
Math.floor(index) === index)) {
|
|
131
|
+
throw new RangeError(`polygon2ClipHalfPlane: index is ${index}, not an integer with three entries from it in halfPlanes (${halfPlanes.length} entries).`);
|
|
132
|
+
}
|
|
133
|
+
const normalX = halfPlanes[index];
|
|
134
|
+
const normalY = halfPlanes[index + 1];
|
|
135
|
+
const limit = halfPlanes[index + 2];
|
|
136
|
+
// x − x is 0 for a finite x only; NaN differs from itself
|
|
137
|
+
if (normalX - normalX !== 0 || normalY - normalY !== 0 || limit !== limit) {
|
|
138
|
+
throw new RangeError(`polygon2ClipHalfPlane: the normal must be finite and the limit not NaN, got (${normalX}, ${normalY}) and ${limit}.`);
|
|
139
|
+
}
|
|
140
|
+
if (viewPrefixesMayOverlap(out, 8 * capacity, coordinates, 16 * count)) {
|
|
141
|
+
throw new RangeError("polygon2ClipHalfPlane: out shares memory with coordinates.");
|
|
142
|
+
}
|
|
143
|
+
const largest = Math.max(Math.abs(normalX), Math.abs(normalY));
|
|
144
|
+
// an infinite limit, or a zero normal, keeps everything or nothing: no
|
|
145
|
+
// side is evaluated, where ∞ − ∞ or 0 · ∞ would be NaN
|
|
146
|
+
if (limit === Infinity || limit === -Infinity || largest === 0) {
|
|
147
|
+
if (limit < 0) {
|
|
148
|
+
return 0;
|
|
149
|
+
}
|
|
150
|
+
for (let entry = 0; entry < 2 * count; entry++) {
|
|
151
|
+
out[entry] = coordinates[entry];
|
|
152
|
+
}
|
|
153
|
+
return count;
|
|
154
|
+
}
|
|
155
|
+
// the half-plane scaled by a power of two (exactly) so that its normal's
|
|
156
|
+
// largest component lies in [1/64, 1/16): a side, the difference of two
|
|
157
|
+
// sides and a crossing then stay finite for every finite coordinate, as
|
|
158
|
+
// does a limit scaled out of range to ±∞, which then sides with no vertex
|
|
159
|
+
PLANE_[0] = normalX;
|
|
160
|
+
PLANE_[1] = normalY;
|
|
161
|
+
PLANE_[2] = limit;
|
|
162
|
+
if (largest >= 0.5 &&
|
|
163
|
+
largest < 2 &&
|
|
164
|
+
(normalX === 0 || Math.abs(normalX) >= SAFE_LOW_) &&
|
|
165
|
+
(normalY === 0 || Math.abs(normalY) >= SAFE_LOW_) &&
|
|
166
|
+
(limit === 0 || Math.abs(limit) >= SAFE_LOW_)) {
|
|
167
|
+
// the usual unit-sized normal: the same scaling, exact as a product
|
|
168
|
+
PLANE_[0] = normalX * 2 ** -5;
|
|
169
|
+
PLANE_[1] = normalY * 2 ** -5;
|
|
170
|
+
PLANE_[2] = limit * 2 ** -5;
|
|
171
|
+
PLANE_[3] = 0;
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
// what a component scaled below the normal range may lose, per unit
|
|
175
|
+
// of the coordinate it multiplies
|
|
176
|
+
PLANE_[3] = 2 ** -1070;
|
|
177
|
+
// Math.log2 may round across a power of two: one step of margin
|
|
178
|
+
const exponent = -(Math.floor(Math.log2(largest)) | 0) - 5;
|
|
179
|
+
scaleByPowerOfTwoInPlace(PLANE_, 0, exponent);
|
|
180
|
+
scaleByPowerOfTwoInPlace(PLANE_, 1, exponent);
|
|
181
|
+
scaleByPowerOfTwoInPlace(PLANE_, 2, exponent);
|
|
182
|
+
}
|
|
183
|
+
ORIGINAL_[0] = normalX;
|
|
184
|
+
ORIGINAL_[1] = normalY;
|
|
185
|
+
ORIGINAL_[2] = limit;
|
|
186
|
+
let written = 0;
|
|
187
|
+
if (count === 0) {
|
|
188
|
+
return 0;
|
|
189
|
+
}
|
|
190
|
+
// each vertex's side once, carried from an edge's end to the next start;
|
|
191
|
+
// the scaled evaluation inline, the exact one only when it is uncertain
|
|
192
|
+
const scaledX = PLANE_[0];
|
|
193
|
+
const scaledY = PLANE_[1];
|
|
194
|
+
const scaledLimit = PLANE_[2];
|
|
195
|
+
const loss = PLANE_[3];
|
|
196
|
+
const firstCode = sideOf_(coordinates, 0);
|
|
197
|
+
const firstSide = SIDE_[0];
|
|
198
|
+
let startCode = firstCode;
|
|
199
|
+
let startSide = firstSide;
|
|
200
|
+
for (let vertex = 0; vertex < count; vertex++) {
|
|
201
|
+
const next = vertex + 1 === count ? 0 : vertex + 1;
|
|
202
|
+
const startX = coordinates[2 * vertex];
|
|
203
|
+
const startY = coordinates[2 * vertex + 1];
|
|
204
|
+
const endX = coordinates[2 * next];
|
|
205
|
+
const endY = coordinates[2 * next + 1];
|
|
206
|
+
let endCode = firstCode;
|
|
207
|
+
let endSide = firstSide;
|
|
208
|
+
if (next !== 0) {
|
|
209
|
+
const termX = scaledX * endX;
|
|
210
|
+
const termY = scaledY * endY;
|
|
211
|
+
endSide = termX + termY - scaledLimit;
|
|
212
|
+
// as sideOf_ bounds it
|
|
213
|
+
let bound = (Math.abs(termX) + Math.abs(termY) + Math.abs(scaledLimit)) * 2 ** -50 +
|
|
214
|
+
2 ** -1070;
|
|
215
|
+
if (loss !== 0) {
|
|
216
|
+
bound += (Math.abs(endX) + Math.abs(endY)) * loss;
|
|
217
|
+
}
|
|
218
|
+
endCode =
|
|
219
|
+
endSide > bound
|
|
220
|
+
? 2
|
|
221
|
+
: endSide < -bound
|
|
222
|
+
? -2
|
|
223
|
+
: exactSign_(coordinates, next);
|
|
224
|
+
}
|
|
225
|
+
if (startCode <= 0) {
|
|
226
|
+
out[2 * written] = startX;
|
|
227
|
+
out[2 * written + 1] = startY;
|
|
228
|
+
written++;
|
|
229
|
+
}
|
|
230
|
+
if ((startCode < 0 && endCode > 0) || (startCode > 0 && endCode < 0)) {
|
|
231
|
+
// the scaled sides when both are certain, else the exact ones:
|
|
232
|
+
// both keep every component of the normal
|
|
233
|
+
let t;
|
|
234
|
+
if ((startCode === 2 || startCode === -2) &&
|
|
235
|
+
(endCode === 2 || endCode === -2)) {
|
|
236
|
+
t = startSide / (startSide - endSide);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
exactCrossing_(coordinates, vertex, next);
|
|
240
|
+
t = SIDE_[0];
|
|
241
|
+
}
|
|
242
|
+
const spanX = endX - startX;
|
|
243
|
+
const spanY = endY - startY;
|
|
244
|
+
// an edge longer than the largest double: the convex combination,
|
|
245
|
+
// whose terms stay finite
|
|
246
|
+
out[2 * written] =
|
|
247
|
+
spanX - spanX === 0 ? startX + t * spanX : startX * (1 - t) + endX * t;
|
|
248
|
+
out[2 * written + 1] =
|
|
249
|
+
spanY - spanY === 0 ? startY + t * spanY : startY * (1 - t) + endY * t;
|
|
250
|
+
written++;
|
|
251
|
+
}
|
|
252
|
+
startCode = endCode;
|
|
253
|
+
startSide = endSide;
|
|
254
|
+
}
|
|
255
|
+
return written;
|
|
256
|
+
}
|
|
257
|
+
/** The half-plane {@link polygon2ClipHalfPlane} cuts to, scaled. */
|
|
258
|
+
const PLANE_ = new Float64Array(4);
|
|
259
|
+
/** The half-plane as given, for the exact sides. */
|
|
260
|
+
const ORIGINAL_ = new Float64Array(3);
|
|
261
|
+
/** A vertex `(x, y, −1)`, or its opposite, for the exact sides. */
|
|
262
|
+
const POINT_ = new Float64Array(3);
|
|
263
|
+
/** An exact side, then a scaled side or a crossing's parameter. */
|
|
264
|
+
const EXACT_ = new Float64Array(1);
|
|
265
|
+
const SIDE_ = new Float64Array(1);
|
|
266
|
+
/** A magnitude that `2⁻⁵` times stays normal, so the product is exact. */
|
|
267
|
+
const SAFE_LOW_ = 2 ** -1000;
|
|
268
|
+
/**
|
|
269
|
+
* The side of vertex `vertex` of `coordinates`: `±2` when the scaled
|
|
270
|
+
* evaluation, whose side it leaves in `SIDE_`, is certain of it (beyond its
|
|
271
|
+
* rounding error and what the scaling lost below the subnormal range), else the
|
|
272
|
+
* exact sign of `normal · p − limit`, `-1`, `0` (on the line) or `1`.
|
|
273
|
+
*/
|
|
274
|
+
function sideOf_(coordinates, vertex) {
|
|
275
|
+
const scaledX = PLANE_[0];
|
|
276
|
+
const scaledY = PLANE_[1];
|
|
277
|
+
const scaledLimit = PLANE_[2];
|
|
278
|
+
const x = coordinates[2 * vertex];
|
|
279
|
+
const y = coordinates[2 * vertex + 1];
|
|
280
|
+
const termX = scaledX * x;
|
|
281
|
+
const termY = scaledY * y;
|
|
282
|
+
const side = termX + termY - scaledLimit;
|
|
283
|
+
SIDE_[0] = side;
|
|
284
|
+
// three roundings of at most 2⁻⁵³ each, relative to the terms, at most
|
|
285
|
+
// 2⁻¹⁰⁷⁵ lost per product and sum, and what the scaling lost (PLANE_[3]
|
|
286
|
+
// per unit of coordinate)
|
|
287
|
+
let bound = (Math.abs(termX) + Math.abs(termY) + Math.abs(scaledLimit)) * 2 ** -50 +
|
|
288
|
+
2 ** -1070;
|
|
289
|
+
const loss = PLANE_[3];
|
|
290
|
+
if (loss !== 0) {
|
|
291
|
+
bound += (Math.abs(x) + Math.abs(y)) * loss;
|
|
292
|
+
}
|
|
293
|
+
if (side > bound) {
|
|
294
|
+
return 2;
|
|
295
|
+
}
|
|
296
|
+
if (side < -bound) {
|
|
297
|
+
return -2;
|
|
52
298
|
}
|
|
53
|
-
return
|
|
299
|
+
return exactSign_(coordinates, vertex);
|
|
54
300
|
}
|
|
55
301
|
/**
|
|
56
|
-
*
|
|
57
|
-
*
|
|
302
|
+
* The exact sign of `normal · p − limit` (`ORIGINAL_`) at vertex `vertex`, from
|
|
303
|
+
* `dotExactInto`, which rounds once and keeps the sign of a sum too small to
|
|
304
|
+
* round to anything but zero: `+0` is an exact zero or a positive one too
|
|
305
|
+
* small, which the opposite sum tells apart.
|
|
58
306
|
*/
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
307
|
+
function exactSign_(coordinates, vertex) {
|
|
308
|
+
POINT_[0] = coordinates[2 * vertex];
|
|
309
|
+
POINT_[1] = coordinates[2 * vertex + 1];
|
|
310
|
+
POINT_[2] = -1;
|
|
311
|
+
dotExactInto(EXACT_, 0, ORIGINAL_, POINT_);
|
|
312
|
+
const exact = EXACT_[0];
|
|
313
|
+
if (exact !== 0) {
|
|
314
|
+
return exact > 0 ? 1 : -1;
|
|
315
|
+
}
|
|
316
|
+
if (1 / exact < 0) {
|
|
317
|
+
return -1;
|
|
318
|
+
}
|
|
319
|
+
POINT_[0] = -POINT_[0];
|
|
320
|
+
POINT_[1] = -POINT_[1];
|
|
321
|
+
POINT_[2] = 1;
|
|
322
|
+
dotExactInto(EXACT_, 0, ORIGINAL_, POINT_);
|
|
323
|
+
return 1 / EXACT_[0] < 0 ? 1 : 0;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* The parameter, into `SIDE_`, at which the edge from vertex `start` to vertex
|
|
327
|
+
* `end` crosses the line, from their exactly rounded sides; the middle of the
|
|
328
|
+
* edge when both sides are too small to round to anything but zero (both ends
|
|
329
|
+
* then lie within `2⁻¹⁰⁷⁵` of the line).
|
|
330
|
+
*/
|
|
331
|
+
function exactCrossing_(coordinates, start, end) {
|
|
332
|
+
POINT_[0] = coordinates[2 * start];
|
|
333
|
+
POINT_[1] = coordinates[2 * start + 1];
|
|
334
|
+
POINT_[2] = -1;
|
|
335
|
+
dotExactInto(EXACT_, 0, ORIGINAL_, POINT_);
|
|
336
|
+
const startSide = EXACT_[0] / 2;
|
|
337
|
+
POINT_[0] = coordinates[2 * end];
|
|
338
|
+
POINT_[1] = coordinates[2 * end + 1];
|
|
339
|
+
dotExactInto(EXACT_, 0, ORIGINAL_, POINT_);
|
|
340
|
+
const endSide = EXACT_[0] / 2;
|
|
341
|
+
const t = startSide / (startSide - endSide);
|
|
342
|
+
// halved, the difference of two finite sides stays finite; a side past the
|
|
343
|
+
// largest double, or both too small, leave no usable ratio
|
|
344
|
+
SIDE_[0] = t >= 0 && t <= 1 ? t : 0.5;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* The length of the closed boundary. O(n).
|
|
348
|
+
*
|
|
349
|
+
* @throws {RangeError} When `coordinates.length` is odd.
|
|
350
|
+
*/
|
|
351
|
+
export function polygon2Perimeter(coordinates) {
|
|
352
|
+
assertCoordinates_("polygon2Perimeter", coordinates);
|
|
353
|
+
const count = coordinates.length / 2;
|
|
354
|
+
if (count < 2) {
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
let sum = 0;
|
|
358
|
+
let previousX = coordinates[2 * count - 2];
|
|
359
|
+
let previousY = coordinates[2 * count - 1];
|
|
360
|
+
for (let index = 0; index < count; index++) {
|
|
361
|
+
const x = coordinates[2 * index];
|
|
362
|
+
const y = coordinates[2 * index + 1];
|
|
363
|
+
sum += Math.sqrt((x - previousX) * (x - previousX) + (y - previousY) * (y - previousY));
|
|
364
|
+
previousX = x;
|
|
365
|
+
previousY = y;
|
|
366
|
+
}
|
|
367
|
+
return sum;
|
|
368
|
+
}
|
|
369
|
+
/**
|
|
370
|
+
* Writes the centroid of the enclosed area into `out`. A polygon of zero area
|
|
371
|
+
* (or fewer than three vertices) gives `NaN`. O(n).
|
|
372
|
+
*
|
|
373
|
+
* @throws {RangeError} When `coordinates.length` is odd.
|
|
374
|
+
*/
|
|
375
|
+
export function polygon2Centroid(out, coordinates) {
|
|
376
|
+
assertCoordinates_("polygon2Centroid", coordinates);
|
|
377
|
+
const count = coordinates.length / 2;
|
|
378
|
+
if (count < 3) {
|
|
379
|
+
out[0] = Number.NaN;
|
|
380
|
+
out[1] = Number.NaN;
|
|
381
|
+
return out;
|
|
382
|
+
}
|
|
383
|
+
const originX = coordinates[0];
|
|
384
|
+
const originY = coordinates[1];
|
|
385
|
+
let area = 0;
|
|
386
|
+
let sumX = 0;
|
|
387
|
+
let sumY = 0;
|
|
388
|
+
for (let index = 1; index + 1 < count; index++) {
|
|
389
|
+
const ax = coordinates[2 * index] - originX;
|
|
390
|
+
const ay = coordinates[2 * index + 1] - originY;
|
|
391
|
+
const bx = coordinates[2 * index + 2] - originX;
|
|
392
|
+
const by = coordinates[2 * index + 3] - originY;
|
|
393
|
+
const cross = ax * by - bx * ay;
|
|
394
|
+
area += cross;
|
|
395
|
+
sumX += (ax + bx) * cross;
|
|
396
|
+
sumY += (ay + by) * cross;
|
|
397
|
+
}
|
|
398
|
+
out[0] = originX + sumX / (3 * area);
|
|
399
|
+
out[1] = originY + sumY / (3 * area);
|
|
400
|
+
return out;
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Whether the point `(x, y)` is inside the polygon under the fill `rule`. Rings
|
|
404
|
+
* are the outer ring (vertices `[0, holeStarts[0])`, or all of them without
|
|
405
|
+
* holes) and each hole ring. A point exactly on the boundary is decided by the
|
|
406
|
+
* half-open crossing convention: inside on some edges, outside on others.
|
|
407
|
+
* O(n).
|
|
408
|
+
*
|
|
409
|
+
* @param holeStarts Vertex indices where the hole rings start, strictly
|
|
410
|
+
* increasing.
|
|
411
|
+
* @throws {RangeError} When `coordinates.length` is odd or `holeStarts` is not
|
|
412
|
+
* strictly increasing within the vertices.
|
|
413
|
+
*/
|
|
414
|
+
export function polygon2ContainsXy(coordinates, x, y, rule, holeStarts) {
|
|
415
|
+
assertCoordinates_("polygon2ContainsXy", coordinates);
|
|
416
|
+
const count = coordinates.length / 2;
|
|
417
|
+
if (holeStarts !== undefined) {
|
|
418
|
+
assertHoleStarts_("polygon2ContainsXy", count, holeStarts);
|
|
419
|
+
}
|
|
420
|
+
const ringCount = holeStarts === undefined ? 1 : holeStarts.length + 1;
|
|
421
|
+
let odd = false;
|
|
422
|
+
let winding = 0;
|
|
423
|
+
for (let ring = 0; ring < ringCount; ring++) {
|
|
424
|
+
const start = ring === 0 ? 0 : holeStarts[ring - 1];
|
|
425
|
+
const end = ring === ringCount - 1 ? count : holeStarts[ring];
|
|
426
|
+
let previousX = coordinates[2 * (end - 1)];
|
|
427
|
+
let previousY = coordinates[2 * (end - 1) + 1];
|
|
428
|
+
for (let index = start; index < end; index++) {
|
|
429
|
+
const currentX = coordinates[2 * index];
|
|
430
|
+
const currentY = coordinates[2 * index + 1];
|
|
431
|
+
if (previousY > y !== currentY > y) {
|
|
432
|
+
const crossingX = previousX +
|
|
433
|
+
((y - previousY) * (currentX - previousX)) / (currentY - previousY);
|
|
434
|
+
if (x < crossingX) {
|
|
435
|
+
odd = !odd;
|
|
436
|
+
winding += currentY > previousY ? 1 : -1;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
previousX = currentX;
|
|
440
|
+
previousY = currentY;
|
|
68
441
|
}
|
|
69
442
|
}
|
|
70
|
-
return
|
|
443
|
+
return rule === "evenodd" ? odd : winding !== 0;
|
|
444
|
+
}
|
|
445
|
+
/** {@link polygon2ContainsXy} for a point value. */
|
|
446
|
+
export function polygon2ContainsPoint(coordinates, point, rule, holeStarts) {
|
|
447
|
+
return polygon2ContainsXy(coordinates, point[0], point[1], rule, holeStarts);
|
|
71
448
|
}
|
|
72
|
-
/**
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
449
|
+
/**
|
|
450
|
+
* Whether the single ring is convex: every turn goes the same way and the
|
|
451
|
+
* boundary turns once (a pentagram, whose turns all agree, is not convex).
|
|
452
|
+
* Collinear vertices are allowed; fewer than three vertices or zero area give
|
|
453
|
+
* `false`. O(n).
|
|
454
|
+
*
|
|
455
|
+
* @throws {RangeError} When `coordinates.length` is odd.
|
|
456
|
+
*/
|
|
457
|
+
export function polygon2IsConvex(coordinates) {
|
|
458
|
+
assertCoordinates_("polygon2IsConvex", coordinates);
|
|
459
|
+
const count = coordinates.length / 2;
|
|
460
|
+
if (count < 3) {
|
|
76
461
|
return false;
|
|
77
462
|
}
|
|
78
463
|
let sign = 0;
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
464
|
+
let xFlips = 0;
|
|
465
|
+
let yFlips = 0;
|
|
466
|
+
let lastXSign = 0;
|
|
467
|
+
let lastYSign = 0;
|
|
468
|
+
let firstXSign = 0;
|
|
469
|
+
let firstYSign = 0;
|
|
470
|
+
for (let index = 0; index < count; index++) {
|
|
471
|
+
const a = index;
|
|
472
|
+
const b = (index + 1) % count;
|
|
473
|
+
const c = (index + 2) % count;
|
|
474
|
+
const abX = coordinates[2 * b] - coordinates[2 * a];
|
|
475
|
+
const abY = coordinates[2 * b + 1] - coordinates[2 * a + 1];
|
|
476
|
+
const bcX = coordinates[2 * c] - coordinates[2 * b];
|
|
477
|
+
const bcY = coordinates[2 * c + 1] - coordinates[2 * b + 1];
|
|
478
|
+
const cross = abX * bcY - abY * bcX;
|
|
84
479
|
if (cross !== 0) {
|
|
480
|
+
const turn = cross > 0 ? 1 : -1;
|
|
85
481
|
if (sign === 0) {
|
|
86
|
-
sign =
|
|
482
|
+
sign = turn;
|
|
87
483
|
}
|
|
88
|
-
else if (
|
|
484
|
+
else if (turn !== sign) {
|
|
89
485
|
return false;
|
|
90
486
|
}
|
|
91
487
|
}
|
|
488
|
+
const xSign = Math.sign(abX);
|
|
489
|
+
const ySign = Math.sign(abY);
|
|
490
|
+
if (xSign !== 0) {
|
|
491
|
+
if (firstXSign === 0) {
|
|
492
|
+
firstXSign = xSign;
|
|
493
|
+
}
|
|
494
|
+
else if (xSign !== lastXSign) {
|
|
495
|
+
xFlips++;
|
|
496
|
+
}
|
|
497
|
+
lastXSign = xSign;
|
|
498
|
+
}
|
|
499
|
+
if (ySign !== 0) {
|
|
500
|
+
if (firstYSign === 0) {
|
|
501
|
+
firstYSign = ySign;
|
|
502
|
+
}
|
|
503
|
+
else if (ySign !== lastYSign) {
|
|
504
|
+
yFlips++;
|
|
505
|
+
}
|
|
506
|
+
lastYSign = ySign;
|
|
507
|
+
}
|
|
92
508
|
}
|
|
93
|
-
|
|
509
|
+
if (firstXSign !== 0 && lastXSign !== firstXSign) {
|
|
510
|
+
xFlips++;
|
|
511
|
+
}
|
|
512
|
+
if (firstYSign !== 0 && lastYSign !== firstYSign) {
|
|
513
|
+
yFlips++;
|
|
514
|
+
}
|
|
515
|
+
return sign !== 0 && xFlips <= 2 && yFlips <= 2;
|
|
94
516
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
517
|
+
/**
|
|
518
|
+
* Triangulates the polygon by ear clipping, holes bridged to the outer ring
|
|
519
|
+
* (Eberly's method): writes three vertex indices per triangle,
|
|
520
|
+
* counter-clockwise in a y-up frame whatever the winding of the input, into
|
|
521
|
+
* `out` and returns the number of triangles. A polygon with `n` vertices in
|
|
522
|
+
* total and `h` holes gives `n + 2h − 2` triangles; degenerate input (collinear
|
|
523
|
+
* runs, duplicate points) may give fewer. Self-intersecting input gives an
|
|
524
|
+
* unspecified, but finite, result. Allocates O(n) scratch. O(n²) worst case.
|
|
525
|
+
*
|
|
526
|
+
* @param out At least `3 · (n + 2h − 2)` entries.
|
|
527
|
+
* @param holeStarts Vertex indices where the hole rings start, strictly
|
|
528
|
+
* increasing.
|
|
529
|
+
* @throws {RangeError} When `coordinates.length` is odd, `holeStarts` is not
|
|
530
|
+
* strictly increasing within the vertices, or `out.length < 3·(n + 2h − 2)`.
|
|
531
|
+
*/
|
|
532
|
+
export function polygon2Triangulate(out, coordinates, holeStarts) {
|
|
533
|
+
assertCoordinates_("polygon2Triangulate", coordinates);
|
|
534
|
+
const count = coordinates.length / 2;
|
|
535
|
+
if (holeStarts !== undefined) {
|
|
536
|
+
assertHoleStarts_("polygon2Triangulate", count, holeStarts);
|
|
537
|
+
}
|
|
538
|
+
const holes = holeStarts === undefined ? 0 : holeStarts.length;
|
|
539
|
+
const capacity = 3 * (count + 2 * holes - 2);
|
|
540
|
+
if (out.length < capacity) {
|
|
541
|
+
throw new RangeError(`polygon2Triangulate: out holds ${out.length} indices, ${capacity} are needed for ${count} vertices and ${holes} holes.`);
|
|
542
|
+
}
|
|
543
|
+
return earClip_(out, coordinates, holeStarts);
|
|
101
544
|
}
|
|
102
|
-
/**
|
|
545
|
+
/**
|
|
546
|
+
* Whether the two polygons have the same vertices in the same order from the
|
|
547
|
+
* same start: exact.
|
|
548
|
+
*/
|
|
103
549
|
export function polygon2Equals(a, b) {
|
|
104
|
-
if (a.
|
|
550
|
+
if (a.length !== b.length) {
|
|
105
551
|
return false;
|
|
106
552
|
}
|
|
107
|
-
for (let
|
|
108
|
-
if (
|
|
553
|
+
for (let index = 0; index < a.length; index++) {
|
|
554
|
+
if (a[index] !== b[index]) {
|
|
109
555
|
return false;
|
|
110
556
|
}
|
|
111
557
|
}
|
|
112
558
|
return true;
|
|
113
559
|
}
|
|
114
|
-
/**
|
|
115
|
-
* Fan-triangulates the polygon from vertex 0. Only produces correct results for
|
|
116
|
-
* convex polygons or polygons star-shaped with respect to vertex 0.
|
|
117
|
-
*/
|
|
118
|
-
export function polygon2Triangulate(p) {
|
|
119
|
-
const triangles = [];
|
|
120
|
-
const n = p.vertices.length;
|
|
121
|
-
const v0 = p.vertices[0];
|
|
122
|
-
for (let i = 1; i < n - 1; i++) {
|
|
123
|
-
triangles.push({ a: v0, b: p.vertices[i], c: p.vertices[i + 1] });
|
|
124
|
-
}
|
|
125
|
-
return triangles;
|
|
126
|
-
}
|
|
127
560
|
//# sourceMappingURL=polygon2.js.map
|