@ac-kit/math-geometry 0.3.0 → 0.5.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 +177 -0
- package/README.md +76 -5
- 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/_polyline-simplify.d.ts +11 -0
- package/dist/_polyline-simplify.d.ts.map +1 -0
- package/dist/_polyline-simplify.js +92 -0
- package/dist/_polyline-simplify.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 +405 -0
- package/dist/_triangle3-cross.js.map +1 -0
- package/dist/convex-hull2.d.ts +26 -0
- package/dist/convex-hull2.d.ts.map +1 -0
- package/dist/convex-hull2.js +88 -0
- package/dist/convex-hull2.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/line2.d.ts +4 -0
- package/dist/line2.d.ts.map +1 -1
- package/dist/line2.js +26 -5
- package/dist/line2.js.map +1 -1
- package/dist/mesh/_index-kernels-u16.d.ts +46 -11
- package/dist/mesh/_index-kernels-u16.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u16.js +1442 -132
- package/dist/mesh/_index-kernels-u16.js.map +1 -1
- package/dist/mesh/_index-kernels-u32.d.ts +46 -11
- package/dist/mesh/_index-kernels-u32.d.ts.map +1 -1
- package/dist/mesh/_index-kernels-u32.js +1442 -132
- package/dist/mesh/_index-kernels-u32.js.map +1 -1
- package/dist/mesh/mesh3-kernels.d.ts +104 -20
- package/dist/mesh/mesh3-kernels.d.ts.map +1 -1
- package/dist/mesh/mesh3-kernels.js +123 -25
- package/dist/mesh/mesh3-kernels.js.map +1 -1
- package/dist/plane3.d.ts +4 -3
- package/dist/plane3.d.ts.map +1 -1
- package/dist/plane3.js +18 -35
- package/dist/plane3.js.map +1 -1
- package/dist/polygon2.d.ts +76 -4
- package/dist/polygon2.d.ts.map +1 -1
- package/dist/polygon2.js +366 -7
- package/dist/polygon2.js.map +1 -1
- package/dist/polygon3.d.ts +19 -3
- package/dist/polygon3.d.ts.map +1 -1
- package/dist/polygon3.js +40 -16
- package/dist/polygon3.js.map +1 -1
- package/dist/polyline2.d.ts +20 -0
- package/dist/polyline2.d.ts.map +1 -1
- package/dist/polyline2.js +25 -0
- package/dist/polyline2.js.map +1 -1
- package/dist/polyline3.d.ts +17 -0
- package/dist/polyline3.d.ts.map +1 -1
- package/dist/polyline3.js +22 -0
- package/dist/polyline3.js.map +1 -1
- package/dist/predicates2.d.ts +51 -0
- package/dist/predicates2.d.ts.map +1 -0
- package/dist/predicates2.js +172 -0
- package/dist/predicates2.js.map +1 -0
- package/dist/quadtree.d.ts +40 -23
- package/dist/quadtree.d.ts.map +1 -1
- package/dist/quadtree.js +196 -119
- package/dist/quadtree.js.map +1 -1
- package/dist/segment2.d.ts +6 -1
- package/dist/segment2.d.ts.map +1 -1
- package/dist/segment2.js +20 -4
- package/dist/segment2.js.map +1 -1
- package/dist/triangle3.d.ts +20 -1
- package/dist/triangle3.d.ts.map +1 -1
- package/dist/triangle3.js +38 -18
- package/dist/triangle3.js.map +1 -1
- package/package.json +8 -8
package/dist/plane3.js
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { vec3Create, } from "@ac-kit/math-linear";
|
|
1
2
|
import { geometryConfig } from "./globals.js";
|
|
3
|
+
import { triangle3Create } from "./triangle3-record.js";
|
|
4
|
+
import { triangle3Normal } from "./triangle3.js";
|
|
2
5
|
/**
|
|
3
6
|
* Writes the plane through `point` with the given `normal` into `out`. The
|
|
4
7
|
* normal is copied as it is, not normalised: `d` is `−normal · point`.
|
|
@@ -16,50 +19,30 @@ export function plane3FromNormalAndPoint(out, normal, point) {
|
|
|
16
19
|
/**
|
|
17
20
|
* Writes the plane through three points into `out`, with a unit normal given by
|
|
18
21
|
* the right-hand rule: counter-clockwise `a, b, c` seen from the positive side.
|
|
19
|
-
*
|
|
20
|
-
*
|
|
22
|
+
* The normal is `triangle3Normal`'s: the direction of the exact cross product
|
|
23
|
+
* `(b − a) × (c − a)` for any finite points. Collinear or coincident points (an
|
|
24
|
+
* exactly zero product) give a zero normal and `d = 0`.
|
|
21
25
|
*/
|
|
22
26
|
export function plane3FromPoints(out, a, b, c) {
|
|
23
|
-
const
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const acZ = c[2] - a[2];
|
|
29
|
-
// Each edge is scaled by its largest component first, so the cross product
|
|
30
|
-
// cannot overflow; its direction is unchanged.
|
|
31
|
-
const abScale = Math.max(Math.abs(abX), Math.abs(abY), Math.abs(abZ));
|
|
32
|
-
const acScale = Math.max(Math.abs(acX), Math.abs(acY), Math.abs(acZ));
|
|
33
|
-
const crossX = (abY / abScale) * (acZ / acScale) - (abZ / abScale) * (acY / acScale);
|
|
34
|
-
const crossY = (abZ / abScale) * (acX / acScale) - (abX / abScale) * (acZ / acScale);
|
|
35
|
-
const crossZ = (abX / abScale) * (acY / acScale) - (abY / abScale) * (acX / acScale);
|
|
36
|
-
const scale = Math.max(Math.abs(crossX), Math.abs(crossY), Math.abs(crossZ));
|
|
37
|
-
let normalX = 0;
|
|
38
|
-
let normalY = 0;
|
|
39
|
-
let normalZ = 0;
|
|
40
|
-
if (abScale === 0 || acScale === 0) {
|
|
41
|
-
// Coincident points: no plane, as for collinear ones.
|
|
42
|
-
}
|
|
43
|
-
else if (scale > 0) {
|
|
44
|
-
const scaledX = crossX / scale;
|
|
45
|
-
const scaledY = crossY / scale;
|
|
46
|
-
const scaledZ = crossZ / scale;
|
|
47
|
-
const length = Math.sqrt(scaledX * scaledX + scaledY * scaledY + scaledZ * scaledZ);
|
|
48
|
-
normalX = scaledX / length;
|
|
49
|
-
normalY = scaledY / length;
|
|
50
|
-
normalZ = scaledZ / length;
|
|
51
|
-
}
|
|
52
|
-
else if (Number.isNaN(scale)) {
|
|
53
|
-
normalX = Number.NaN;
|
|
54
|
-
normalY = Number.NaN;
|
|
55
|
-
normalZ = Number.NaN;
|
|
27
|
+
const triangle = TRIANGLE_;
|
|
28
|
+
for (let axis = 0; axis < 3; axis++) {
|
|
29
|
+
triangle[axis] = a[axis];
|
|
30
|
+
triangle[3 + axis] = b[axis];
|
|
31
|
+
triangle[6 + axis] = c[axis];
|
|
56
32
|
}
|
|
33
|
+
const normal = triangle3Normal(NORMAL_, triangle);
|
|
34
|
+
const normalX = normal[0];
|
|
35
|
+
const normalY = normal[1];
|
|
36
|
+
const normalZ = normal[2];
|
|
57
37
|
out[0] = normalX;
|
|
58
38
|
out[1] = normalY;
|
|
59
39
|
out[2] = normalZ;
|
|
60
40
|
out[3] = 0 - (normalX * a[0] + normalY * a[1] + normalZ * a[2]);
|
|
61
41
|
return out;
|
|
62
42
|
}
|
|
43
|
+
/** Scratch for {@link plane3FromPoints}; never leaves this module. */
|
|
44
|
+
const TRIANGLE_ = triangle3Create();
|
|
45
|
+
const NORMAL_ = vec3Create();
|
|
63
46
|
/**
|
|
64
47
|
* `n · p + d`: the distance from the plane, positive on the side the normal
|
|
65
48
|
* points to, when the normal is a unit vector (see {@link plane3Normalize}).
|
package/dist/plane3.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plane3.js","sourceRoot":"","sources":["../src/plane3.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"plane3.js","sourceRoot":"","sources":["../src/plane3.ts"],"names":[],"mappings":"AAAA,OAAO,EAIN,UAAU,GACV,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AACxD,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAA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IAAI,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE,CAAC,GAAG,IAAI,GAAG,SAAS,CAAC,EAAE,CAAC,CAAC,CAAC;IACzE,IACC,WAAW,KAAK,CAAC;QACjB,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QACrB,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAC;QACrB,CAAC,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,IAAI,IAAI,KAAK,CAAC,CAAC,EACvC,CAAC;QACF,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,GAAG,CAAC,CAAC,CAAC,GAAG,IAAI,CAAC;QACd,OAAO,IAAI,CAAC;IACb,CAAC;IACD,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;IACpB,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;IACpB,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;IACpB,GAAG,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC;IACpB,OAAO,KAAK,CAAC;AACd,CAAC"}
|
package/dist/polygon2.d.ts
CHANGED
|
@@ -13,15 +13,65 @@ export type FillRule = "nonzero" | "evenodd";
|
|
|
13
13
|
* first vertex, so a polygon far from the origin loses no digits. Fewer than
|
|
14
14
|
* three vertices give `0`. O(n).
|
|
15
15
|
*
|
|
16
|
-
* @
|
|
16
|
+
* @param vertexCount How many vertices, from the start of `coordinates`, make
|
|
17
|
+
* the polygon; all of them when omitted. A count lets a polygon live in a
|
|
18
|
+
* longer working buffer, as {@link polygon2ClipHalfPlane} writes it.
|
|
19
|
+
* @throws {RangeError} When `vertexCount` is omitted and `coordinates.length`
|
|
20
|
+
* is odd, or given and not an integer from 0 to `coordinates.length / 2`.
|
|
17
21
|
*/
|
|
18
|
-
export declare function polygon2SignedArea(coordinates: Float64Array): number;
|
|
22
|
+
export declare function polygon2SignedArea(coordinates: Float64Array, vertexCount?: number): number;
|
|
19
23
|
/**
|
|
20
24
|
* The area, never negative. O(n).
|
|
21
25
|
*
|
|
22
|
-
* @
|
|
26
|
+
* @param vertexCount As {@link polygon2SignedArea} takes it.
|
|
27
|
+
* @throws {RangeError} As {@link polygon2SignedArea} throws.
|
|
28
|
+
*/
|
|
29
|
+
export declare function polygon2Area(coordinates: Float64Array, vertexCount?: number): number;
|
|
30
|
+
/**
|
|
31
|
+
* Sutherland–Hodgman against one line: writes into `out` the polygon of the
|
|
32
|
+
* first `vertexCount` vertices of `coordinates` cut to the closed half-plane
|
|
33
|
+
* `normalX · x + normalY · y ≤ limit`, read as `(normalX, normalY, limit)` from
|
|
34
|
+
* `halfPlanes[index]` on, and returns its vertex count. A convex polygon gives
|
|
35
|
+
* the convex intersection, at most `vertexCount + 1` vertices; a concave one
|
|
36
|
+
* gives pieces joined by bridges of zero width along the line, the same area as
|
|
37
|
+
* the intersection, at most `⌊3 · vertexCount / 2⌋` vertices, which `out` must
|
|
38
|
+
* have room for. A vertex on the line is kept, and an edge that only touches
|
|
39
|
+
* the line adds no vertex; the normal need not be unit, and an infinite `limit`
|
|
40
|
+
* keeps everything (`+∞`) or nothing (`−∞`), as a zero normal does by the sign
|
|
41
|
+
* of `limit`. The half-plane is scaled by a power of two, exactly, before any
|
|
42
|
+
* side is evaluated, so every finite input, coordinates up to
|
|
43
|
+
* `Number.MAX_VALUE` included, gives finite vertices; a vertex the scaled
|
|
44
|
+
* evaluation cannot place beyond its rounding error is placed by the exact sign
|
|
45
|
+
* of `normal · p − limit` (math-scalar's `dotExactInto`), so which vertices are
|
|
46
|
+
* kept is exact, and a crossing is then found from the exactly rounded sides,
|
|
47
|
+
* every component of the normal kept.
|
|
48
|
+
*
|
|
49
|
+
* The clipping that area-coverage antialiasing and polygon rasterisers repeat
|
|
50
|
+
* per pixel, so nothing is allocated, inlined or not: the polygon, its clip and
|
|
51
|
+
* the half-plane live in caller buffers (a double passed to a call the engine
|
|
52
|
+
* does not inline is boxed), and one buffer can hold the half-planes of a whole
|
|
53
|
+
* clip window, `index` choosing one. O(n).
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* const square = new Float64Array([0, 0, 4, 0, 4, 4, 0, 4]);
|
|
57
|
+
* const out = new Float64Array(12);
|
|
58
|
+
* const left = new Float64Array([1, 0, 1]); // x ≤ 1
|
|
59
|
+
* const count = polygon2ClipHalfPlane(out, square, 4, left); // 4
|
|
60
|
+
* polygon2Area(out, count); // 4
|
|
61
|
+
*
|
|
62
|
+
* @param out At least `2 · ⌊3 · vertexCount / 2⌋` entries, sharing no memory
|
|
63
|
+
* with the vertices read; its first `2 ·` (returned count) entries are
|
|
64
|
+
* written.
|
|
65
|
+
* @param halfPlanes Holds the half-plane at `index`, read before anything is
|
|
66
|
+
* written (it may share memory with `out`).
|
|
67
|
+
* @param index Where the half-plane starts in `halfPlanes`; `0` by default.
|
|
68
|
+
* @returns How many vertices `out` holds.
|
|
69
|
+
* @throws {RangeError} When `vertexCount` is not an integer from 0 to
|
|
70
|
+
* `coordinates.length / 2`, `index` is not an integer with three entries from
|
|
71
|
+
* it in `halfPlanes`, `out` is too short or shares memory with the vertices,
|
|
72
|
+
* the normal is not finite or `limit` is `NaN`; nothing is written.
|
|
23
73
|
*/
|
|
24
|
-
export declare function
|
|
74
|
+
export declare function polygon2ClipHalfPlane(out: Float64Array, coordinates: Float64Array, vertexCount: number, halfPlanes: Float64Array, index?: number): number;
|
|
25
75
|
/**
|
|
26
76
|
* The length of the closed boundary. O(n).
|
|
27
77
|
*
|
|
@@ -75,6 +125,28 @@ export declare function polygon2IsConvex(coordinates: Float64Array): boolean;
|
|
|
75
125
|
* strictly increasing within the vertices, or `out.length < 3·(n + 2h − 2)`.
|
|
76
126
|
*/
|
|
77
127
|
export declare function polygon2Triangulate(out: Uint32Array, coordinates: Float64Array, holeStarts?: Uint32Array): number;
|
|
128
|
+
/**
|
|
129
|
+
* Groups the rings of a non-zero fill into polygons for
|
|
130
|
+
* {@link polygon2Triangulate}: each ring of positive signed area
|
|
131
|
+
* (counter-clockwise in a y-up frame) is an outer ring, each ring of negative
|
|
132
|
+
* area a hole of the smallest outer ring that contains its first vertex
|
|
133
|
+
* (`"nonzero"`). `rings` lists ring indices in polygon order, each outer ring
|
|
134
|
+
* then its holes, in input order; `polygonStarts` is where each polygon starts
|
|
135
|
+
* in `rings`. A ring of zero area (fewer than three vertices), and a hole that
|
|
136
|
+
* no outer ring contains, are left out: rings that only a winding count over
|
|
137
|
+
* the whole set fills (an outer ring inside a hole, a clockwise ring around
|
|
138
|
+
* everything) need a non-zero tessellation instead. Rings are not checked for
|
|
139
|
+
* crossings. O(n · outer rings).
|
|
140
|
+
*
|
|
141
|
+
* @param ringStarts The first vertex of each ring, the first one 0, strictly
|
|
142
|
+
* increasing; a ring ends where the next starts, the last at the end.
|
|
143
|
+
* @throws {RangeError} When `coordinates.length` is odd or `ringStarts` does
|
|
144
|
+
* not start at 0 or is not strictly increasing within the vertices.
|
|
145
|
+
*/
|
|
146
|
+
export declare function polygon2GroupRings(coordinates: Float64Array, ringStarts: Uint32Array): {
|
|
147
|
+
readonly rings: Uint32Array;
|
|
148
|
+
readonly polygonStarts: Uint32Array;
|
|
149
|
+
};
|
|
78
150
|
/**
|
|
79
151
|
* Whether the two polygons have the same vertices in the same order from the
|
|
80
152
|
* same start: exact.
|
package/dist/polygon2.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"polygon2.d.ts","sourceRoot":"","sources":["../src/polygon2.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"polygon2.d.ts","sourceRoot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wDnE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CAClC,GAAG,EAAE,WAAW,EAChB,WAAW,EAAE,YAAY,EACzB,UAAU,CAAC,EAAE,WAAW,GACtB,MAAM,CAcR;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,CACjC,WAAW,EAAE,YAAY,EACzB,UAAU,EAAE,WAAW,GACrB;IAAE,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC;IAAC,QAAQ,CAAC,aAAa,EAAE,WAAW,CAAA;CAAE,CA0DtE;AAED;;;GAGG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,YAAY,EAAE,CAAC,EAAE,YAAY,GAAG,OAAO,CAUxE"}
|
package/dist/polygon2.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { scaleByPowerOfTwoInPlace, viewPrefixesMayOverlap } from "@ac-kit/core";
|
|
2
|
+
import { dotExactInto } from "@ac-kit/math-scalar";
|
|
1
3
|
import { earClip_ } from "./_ear-clipping.js";
|
|
2
4
|
/**
|
|
3
5
|
* Polygons are packed `Float64Array`s of `x, y` pairs, rings implicitly closed
|
|
@@ -9,6 +11,22 @@ function assertCoordinates_(name, coordinates) {
|
|
|
9
11
|
throw new RangeError(`${name}: coordinates has ${coordinates.length} numbers, not a multiple of 2.`);
|
|
10
12
|
}
|
|
11
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
|
+
}
|
|
12
30
|
function assertHoleStarts_(name, vertexCount, holeStarts) {
|
|
13
31
|
let previous = 0;
|
|
14
32
|
for (let hole = 0; hole < holeStarts.length; hole++) {
|
|
@@ -25,11 +43,14 @@ function assertHoleStarts_(name, vertexCount, holeStarts) {
|
|
|
25
43
|
* first vertex, so a polygon far from the origin loses no digits. Fewer than
|
|
26
44
|
* three vertices give `0`. O(n).
|
|
27
45
|
*
|
|
28
|
-
* @
|
|
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`.
|
|
29
51
|
*/
|
|
30
|
-
export function polygon2SignedArea(coordinates) {
|
|
31
|
-
|
|
32
|
-
const count = coordinates.length / 2;
|
|
52
|
+
export function polygon2SignedArea(coordinates, vertexCount) {
|
|
53
|
+
const count = vertexCountOf_("polygon2SignedArea", coordinates, vertexCount);
|
|
33
54
|
if (count < 3) {
|
|
34
55
|
return 0;
|
|
35
56
|
}
|
|
@@ -48,10 +69,279 @@ export function polygon2SignedArea(coordinates) {
|
|
|
48
69
|
/**
|
|
49
70
|
* The area, never negative. O(n).
|
|
50
71
|
*
|
|
51
|
-
* @
|
|
72
|
+
* @param vertexCount As {@link polygon2SignedArea} takes it.
|
|
73
|
+
* @throws {RangeError} As {@link polygon2SignedArea} throws.
|
|
52
74
|
*/
|
|
53
|
-
export function polygon2Area(coordinates) {
|
|
54
|
-
return Math.abs(polygon2SignedArea(coordinates));
|
|
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;
|
|
298
|
+
}
|
|
299
|
+
return exactSign_(coordinates, vertex);
|
|
300
|
+
}
|
|
301
|
+
/**
|
|
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.
|
|
306
|
+
*/
|
|
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;
|
|
55
345
|
}
|
|
56
346
|
/**
|
|
57
347
|
* The length of the closed boundary. O(n).
|
|
@@ -252,6 +542,75 @@ export function polygon2Triangulate(out, coordinates, holeStarts) {
|
|
|
252
542
|
}
|
|
253
543
|
return earClip_(out, coordinates, holeStarts);
|
|
254
544
|
}
|
|
545
|
+
/**
|
|
546
|
+
* Groups the rings of a non-zero fill into polygons for
|
|
547
|
+
* {@link polygon2Triangulate}: each ring of positive signed area
|
|
548
|
+
* (counter-clockwise in a y-up frame) is an outer ring, each ring of negative
|
|
549
|
+
* area a hole of the smallest outer ring that contains its first vertex
|
|
550
|
+
* (`"nonzero"`). `rings` lists ring indices in polygon order, each outer ring
|
|
551
|
+
* then its holes, in input order; `polygonStarts` is where each polygon starts
|
|
552
|
+
* in `rings`. A ring of zero area (fewer than three vertices), and a hole that
|
|
553
|
+
* no outer ring contains, are left out: rings that only a winding count over
|
|
554
|
+
* the whole set fills (an outer ring inside a hole, a clockwise ring around
|
|
555
|
+
* everything) need a non-zero tessellation instead. Rings are not checked for
|
|
556
|
+
* crossings. O(n · outer rings).
|
|
557
|
+
*
|
|
558
|
+
* @param ringStarts The first vertex of each ring, the first one 0, strictly
|
|
559
|
+
* increasing; a ring ends where the next starts, the last at the end.
|
|
560
|
+
* @throws {RangeError} When `coordinates.length` is odd or `ringStarts` does
|
|
561
|
+
* not start at 0 or is not strictly increasing within the vertices.
|
|
562
|
+
*/
|
|
563
|
+
export function polygon2GroupRings(coordinates, ringStarts) {
|
|
564
|
+
assertCoordinates_("polygon2GroupRings", coordinates);
|
|
565
|
+
const count = coordinates.length / 2;
|
|
566
|
+
if (ringStarts.length > 0) {
|
|
567
|
+
if (ringStarts[0] !== 0) {
|
|
568
|
+
throw new RangeError(`polygon2GroupRings: ringStarts[0] is ${ringStarts[0]}, not 0.`);
|
|
569
|
+
}
|
|
570
|
+
assertHoleStarts_("polygon2GroupRings", count, ringStarts.subarray(1));
|
|
571
|
+
}
|
|
572
|
+
const ringCount = ringStarts.length;
|
|
573
|
+
const ringOf = (ring) => coordinates.subarray(ringStarts[ring] * 2, (ring + 1 < ringCount ? ringStarts[ring + 1] : count) * 2);
|
|
574
|
+
const areas = new Float64Array(ringCount);
|
|
575
|
+
const outers = [];
|
|
576
|
+
for (let ring = 0; ring < ringCount; ring++) {
|
|
577
|
+
areas[ring] = polygon2SignedArea(ringOf(ring));
|
|
578
|
+
if (areas[ring] > 0) {
|
|
579
|
+
outers.push(ring);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
const holder = new Int32Array(ringCount).fill(-1);
|
|
583
|
+
for (let ring = 0; ring < ringCount; ring++) {
|
|
584
|
+
if (!(areas[ring] < 0)) {
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
const x = coordinates[ringStarts[ring] * 2];
|
|
588
|
+
const y = coordinates[ringStarts[ring] * 2 + 1];
|
|
589
|
+
let best = -1;
|
|
590
|
+
for (const outer of outers) {
|
|
591
|
+
if ((best < 0 || areas[outer] < areas[best]) &&
|
|
592
|
+
polygon2ContainsXy(ringOf(outer), x, y, "nonzero")) {
|
|
593
|
+
best = outer;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
holder[ring] = best;
|
|
597
|
+
}
|
|
598
|
+
const rings = [];
|
|
599
|
+
const polygonStarts = [];
|
|
600
|
+
for (const outer of outers) {
|
|
601
|
+
polygonStarts.push(rings.length);
|
|
602
|
+
rings.push(outer);
|
|
603
|
+
for (let ring = 0; ring < ringCount; ring++) {
|
|
604
|
+
if (holder[ring] === outer) {
|
|
605
|
+
rings.push(ring);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
return {
|
|
610
|
+
rings: Uint32Array.from(rings),
|
|
611
|
+
polygonStarts: Uint32Array.from(polygonStarts),
|
|
612
|
+
};
|
|
613
|
+
}
|
|
255
614
|
/**
|
|
256
615
|
* Whether the two polygons have the same vertices in the same order from the
|
|
257
616
|
* same start: exact.
|