@flighthq/path-boolean 0.5.1-next.1350.8ce488b → 0.5.1-next.1404.457d1da

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.
Files changed (51) hide show
  1. package/README.md +2 -2
  2. package/dist/booleanPaths.d.ts +6 -6
  3. package/dist/booleanPaths.d.ts.map +1 -1
  4. package/dist/booleanPaths.js +10 -19
  5. package/dist/booleanPaths.js.map +1 -1
  6. package/dist/contract.d.ts +0 -2
  7. package/dist/contract.d.ts.map +1 -1
  8. package/dist/contract.js +0 -2
  9. package/dist/contract.js.map +1 -1
  10. package/dist/explainPathBoolean.d.ts +3 -3
  11. package/dist/explainPathBoolean.d.ts.map +1 -1
  12. package/dist/explainPathBoolean.js +4 -4
  13. package/dist/explainPathBoolean.js.map +1 -1
  14. package/dist/index.d.ts +1 -1
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +1 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/martinezKernel.d.ts +4 -3
  19. package/dist/martinezKernel.d.ts.map +1 -1
  20. package/dist/martinezKernel.js +11 -4
  21. package/dist/martinezKernel.js.map +1 -1
  22. package/dist/offsetPath.d.ts +2 -2
  23. package/dist/offsetPath.d.ts.map +1 -1
  24. package/dist/offsetPath.js +2 -2
  25. package/dist/offsetPath.js.map +1 -1
  26. package/dist/resolvePathRegions.d.ts +2 -2
  27. package/dist/resolvePathRegions.d.ts.map +1 -1
  28. package/dist/resolvePathRegions.js +2 -12
  29. package/dist/resolvePathRegions.js.map +1 -1
  30. package/dist/simplifyPath.d.ts +2 -2
  31. package/dist/simplifyPath.d.ts.map +1 -1
  32. package/dist/simplifyPath.js +2 -10
  33. package/dist/simplifyPath.js.map +1 -1
  34. package/dist/unionAllPaths.d.ts +2 -2
  35. package/dist/unionAllPaths.d.ts.map +1 -1
  36. package/dist/unionAllPaths.js +2 -10
  37. package/dist/unionAllPaths.js.map +1 -1
  38. package/package.json +4 -4
  39. package/src/booleanPaths.test.ts +38 -37
  40. package/src/explainPathBoolean.test.ts +9 -6
  41. package/src/fuzzInvariants.test.ts +21 -33
  42. package/src/martinezKernel.test.ts +23 -6
  43. package/src/offsetPath.test.ts +32 -50
  44. package/src/resolvePathRegions.test.ts +6 -7
  45. package/src/simplifyPath.test.ts +12 -22
  46. package/src/unionAllPaths.test.ts +10 -12
  47. package/dist/pathBooleanBackend.d.ts +0 -5
  48. package/dist/pathBooleanBackend.d.ts.map +0 -1
  49. package/dist/pathBooleanBackend.js +0 -24
  50. package/dist/pathBooleanBackend.js.map +0 -1
  51. package/src/pathBooleanBackend.test.ts +0 -53
@@ -1,14 +1,6 @@
1
1
  import { flattenPath } from '@flighthq/path/contract';
2
- import { getPathBooleanBackend } from './pathBooleanBackend';
3
2
  import { writePathBooleanContours } from './writePathBooleanContours';
4
- // N-way union of a list of paths into one clean filled-region outline. Every path is flattened to polygon
5
- // contours at the option tolerance and the whole set is folded together in a single union pass through the
6
- // active kernel under the fill rule (default `nonZero`), so overlaps merge and holes are preserved exactly
7
- // as a repeated binary `unionPaths` fold would produce, without the intermediate allocations. The result
8
- // contours are written into `out` (a fresh `nonZero` path when omitted). An empty list yields an empty
9
- // path; a single path yields its own self-overlap-resolved region, matching `simplifyPath`. Every input is
10
- // fully read before `out` is written, so `out` may safely alias any path in the list.
11
- export function unionAllPaths(paths, out, options) {
3
+ export function unionAllPaths(pathBooleanKernel, paths, out, options) {
12
4
  const fillRule = options?.fillRule ?? 'nonZero';
13
5
  const contours = [];
14
6
  for (const path of paths) {
@@ -17,7 +9,7 @@ export function unionAllPaths(paths, out, options) {
17
9
  }
18
10
  const result = contours.length === 0
19
11
  ? EMPTY_CONTOURS
20
- : getPathBooleanBackend().computePathBoolean(contours, EMPTY_CONTOURS, 'union', fillRule);
12
+ : pathBooleanKernel.computePathBoolean(contours, EMPTY_CONTOURS, 'union', fillRule);
21
13
  return writePathBooleanContours(result, out);
22
14
  }
23
15
  const EMPTY_CONTOURS = [];
@@ -1 +1 @@
1
- {"version":3,"file":"unionAllPaths.js","sourceRoot":"","sources":["../src/unionAllPaths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAEtE,0GAA0G;AAC1G,2GAA2G;AAC3G,2GAA2G;AAC3G,yGAAyG;AACzG,uGAAuG;AACvG,2GAA2G;AAC3G,sFAAsF;AACtF,MAAM,UAAU,aAAa,CAC3B,KAAgC,EAChC,GAAU,EACV,OAAsC;IAEtC,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAI,SAAS,CAAC;IAChD,MAAM,QAAQ,GAAyB,EAAE,CAAC;IAC1C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,KAAK,MAAM,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtF,CAAC;IACD,MAAM,MAAM,GACV,QAAQ,CAAC,MAAM,KAAK,CAAC;QACnB,CAAC,CAAC,cAAc;QAChB,CAAC,CAAC,qBAAqB,EAAE,CAAC,kBAAkB,CAAC,QAAQ,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IAE9F,OAAO,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,cAAc,GAAkC,EAAE,CAAC"}
1
+ {"version":3,"file":"unionAllPaths.js","sourceRoot":"","sources":["../src/unionAllPaths.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAGtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,4BAA4B,CAAC;AAEtE,MAAM,UAAU,aAAa,CAC3B,iBAA8C,EAC9C,KAAgC,EAChC,GAAU,EACV,OAAsC;IAEtC,MAAM,QAAQ,GAAG,OAAO,EAAE,QAAQ,IAAI,SAAS,CAAC;IAChD,MAAM,QAAQ,GAAyB,EAAE,CAAC;IAC1C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACzB,KAAK,MAAM,OAAO,IAAI,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,CAAC;YAAE,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtF,CAAC;IACD,MAAM,MAAM,GACV,QAAQ,CAAC,MAAM,KAAK,CAAC;QACnB,CAAC,CAAC,cAAc;QAChB,CAAC,CAAC,iBAAiB,CAAC,kBAAkB,CAAC,QAAQ,EAAE,cAAc,EAAE,OAAO,EAAE,QAAQ,CAAC,CAAC;IAExF,OAAO,wBAAwB,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;AAC/C,CAAC;AAED,MAAM,cAAc,GAAkC,EAAE,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flighthq/path-boolean",
3
- "version": "0.5.1-next.1350.8ce488b",
3
+ "version": "0.5.1-next.1404.457d1da",
4
4
  "author": "Joshua Granick and other contributors",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -38,9 +38,9 @@
38
38
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
39
39
  },
40
40
  "dependencies": {
41
- "@flighthq/entity": "0.5.1-next.1350.8ce488b",
42
- "@flighthq/path": "0.5.1-next.1350.8ce488b",
43
- "@flighthq/types": "0.5.1-next.1350.8ce488b"
41
+ "@flighthq/entity": "0.5.1-next.1404.457d1da",
42
+ "@flighthq/path": "0.5.1-next.1404.457d1da",
43
+ "@flighthq/types": "0.5.1-next.1404.457d1da"
44
44
  },
45
45
  "devDependencies": {
46
46
  "typescript": "^5.3.0"
@@ -1,10 +1,12 @@
1
1
  import { allocateEntity, finishEntity } from '@flighthq/entity/contract';
2
2
  import { createPath, appendPathRectangle, flattenPath } from '@flighthq/path/contract';
3
- import type { Path, PathWinding } from '@flighthq/types/contract';
3
+ import type { PathBooleanKernel, Path, PathWinding } from '@flighthq/types/contract';
4
4
  import { describe, expect, it } from 'vitest';
5
5
 
6
6
  import { booleanPaths, differencePaths, intersectPaths, unionPaths, xorPaths } from './booleanPaths';
7
- import { setPathBooleanBackend } from './pathBooleanBackend';
7
+ import { martinezPathBooleanKernel } from './martinezKernel';
8
+
9
+ const kernel = martinezPathBooleanKernel;
8
10
 
9
11
  function rectanglePath(x: number, y: number, w: number, h: number, winding: PathWinding = 'nonZero'): Path {
10
12
  const path = createPath(winding);
@@ -12,8 +14,6 @@ function rectanglePath(x: number, y: number, w: number, h: number, winding: Path
12
14
  return path;
13
15
  }
14
16
 
15
- // Net filled area of a path under non-zero fill: flatten to contours and sum signed ring areas so a
16
- // hole's opposite winding cancels an outer ring.
17
17
  function pathFilledArea(path: Readonly<Path>): number {
18
18
  let total = 0;
19
19
  for (const ring of flattenPath(path)) {
@@ -49,21 +49,21 @@ describe('booleanPaths', () => {
49
49
  it('dispatches to the operation named in the argument', () => {
50
50
  const a = rectanglePath(0, 0, 10, 10);
51
51
  const b = rectanglePath(5, 5, 10, 10);
52
- expect(pathFilledArea(booleanPaths(a, b, 'union'))).toBeCloseTo(175, 4);
53
- expect(pathFilledArea(booleanPaths(a, b, 'intersection'))).toBeCloseTo(25, 4);
54
- expect(pathFilledArea(booleanPaths(a, b, 'difference'))).toBeCloseTo(75, 4);
55
- expect(pathFilledArea(booleanPaths(a, b, 'xor'))).toBeCloseTo(150, 4);
52
+ expect(pathFilledArea(booleanPaths(kernel, a, b, 'union'))).toBeCloseTo(175, 4);
53
+ expect(pathFilledArea(booleanPaths(kernel, a, b, 'intersection'))).toBeCloseTo(25, 4);
54
+ expect(pathFilledArea(booleanPaths(kernel, a, b, 'difference'))).toBeCloseTo(75, 4);
55
+ expect(pathFilledArea(booleanPaths(kernel, a, b, 'xor'))).toBeCloseTo(150, 4);
56
56
  });
57
57
 
58
58
  it('returns a fresh non-zero path when out is omitted', () => {
59
- const result = booleanPaths(rectanglePath(0, 0, 10, 10), rectanglePath(20, 20, 5, 5), 'union');
59
+ const result = booleanPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(20, 20, 5, 5), 'union');
60
60
  expect(result.winding).toBe('nonZero');
61
61
  expect(result.commands.length).toBeGreaterThan(0);
62
62
  });
63
63
 
64
64
  it('writes into the provided out path and returns it', () => {
65
65
  const out = createPath('nonZero');
66
- const returned = booleanPaths(rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10), 'union', out);
66
+ const returned = booleanPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10), 'union', out);
67
67
  expect(returned).toBe(out);
68
68
  expect(pathFilledArea(out)).toBeCloseTo(175, 4);
69
69
  });
@@ -71,7 +71,7 @@ describe('booleanPaths', () => {
71
71
  it('is safe when out aliases the subject input', () => {
72
72
  const a = rectanglePath(0, 0, 10, 10);
73
73
  const b = rectanglePath(5, 5, 10, 10);
74
- const result = booleanPaths(a, b, 'union', a);
74
+ const result = booleanPaths(kernel, a, b, 'union', a);
75
75
  expect(result).toBe(a);
76
76
  expect(pathFilledArea(result)).toBeCloseTo(175, 4);
77
77
  });
@@ -79,32 +79,36 @@ describe('booleanPaths', () => {
79
79
  it('is safe when out aliases the clip input', () => {
80
80
  const a = rectanglePath(0, 0, 10, 10);
81
81
  const b = rectanglePath(5, 5, 10, 10);
82
- const result = booleanPaths(a, b, 'intersection', b);
82
+ const result = booleanPaths(kernel, a, b, 'intersection', b);
83
83
  expect(result).toBe(b);
84
84
  expect(pathFilledArea(result)).toBeCloseTo(25, 4);
85
85
  });
86
86
 
87
87
  it('honors the even-odd fill rule option on self-overlapping input', () => {
88
- // A single subject path with two overlapping rectangles: non-zero fills the overlap, even-odd holes
89
- // it. (A path holds many contours; two appendPathRectangle calls append two.)
90
88
  const a = createPath('nonZero');
91
89
  appendPathRectangle(a, 0, 0, 10, 10);
92
90
  appendPathRectangle(a, 5, 5, 10, 10);
93
91
  const empty = createPath('nonZero');
94
- expect(pathFilledArea(booleanPaths(a, empty, 'union', undefined, { fillRule: 'nonZero' }))).toBeCloseTo(175, 4);
95
- expect(pathFilledArea(booleanPaths(a, empty, 'union', undefined, { fillRule: 'evenOdd' }))).toBeCloseTo(150, 4);
92
+ expect(pathFilledArea(booleanPaths(kernel, a, empty, 'union', undefined, { fillRule: 'nonZero' }))).toBeCloseTo(
93
+ 175,
94
+ 4,
95
+ );
96
+ expect(pathFilledArea(booleanPaths(kernel, a, empty, 'union', undefined, { fillRule: 'evenOdd' }))).toBeCloseTo(
97
+ 150,
98
+ 4,
99
+ );
96
100
  });
97
101
 
98
102
  it('accepts a coarser tolerance option without error', () => {
99
103
  const a = rectanglePath(0, 0, 10, 10);
100
104
  const b = rectanglePath(5, 5, 10, 10);
101
- expect(pathFilledArea(booleanPaths(a, b, 'union', undefined, { tolerance: 2 }))).toBeCloseTo(175, 4);
105
+ expect(pathFilledArea(booleanPaths(kernel, a, b, 'union', undefined, { tolerance: 2 }))).toBeCloseTo(175, 4);
102
106
  });
103
107
  });
104
108
 
105
109
  describe('differencePaths', () => {
106
110
  it('cuts a contained hole', () => {
107
- const result = differencePaths(rectanglePath(0, 0, 30, 30), rectanglePath(10, 10, 10, 10));
111
+ const result = differencePaths(kernel, rectanglePath(0, 0, 30, 30), rectanglePath(10, 10, 10, 10));
108
112
  expect(pathFilledArea(result)).toBeCloseTo(800, 4);
109
113
  expect(pathFillContains(result, 15, 15)).toBe(false);
110
114
  expect(pathFillContains(result, 2, 2)).toBe(true);
@@ -113,48 +117,45 @@ describe('differencePaths', () => {
113
117
  it('is empty for a path differenced against itself', () => {
114
118
  const a = rectanglePath(0, 0, 10, 10);
115
119
  const b = rectanglePath(0, 0, 10, 10);
116
- expect(pathFilledArea(differencePaths(a, b))).toBeCloseTo(0, 4);
120
+ expect(pathFilledArea(differencePaths(kernel, a, b))).toBeCloseTo(0, 4);
117
121
  });
118
122
  });
119
123
 
120
124
  describe('intersectPaths', () => {
121
125
  it('keeps the overlap', () => {
122
- const result = intersectPaths(rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
126
+ const result = intersectPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
123
127
  expect(pathFilledArea(result)).toBeCloseTo(25, 4);
124
128
  });
125
129
 
126
130
  it('is empty for disjoint inputs', () => {
127
- const result = intersectPaths(rectanglePath(0, 0, 10, 10), rectanglePath(20, 20, 10, 10));
131
+ const result = intersectPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(20, 20, 10, 10));
128
132
  expect(pathFilledArea(result)).toBeCloseTo(0, 4);
129
133
  });
130
134
  });
131
135
 
132
136
  describe('unionPaths', () => {
133
137
  it('merges overlapping rectangles', () => {
134
- const result = unionPaths(rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
138
+ const result = unionPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
135
139
  expect(pathFilledArea(result)).toBeCloseTo(175, 4);
136
140
  });
137
141
 
138
142
  it('commutes', () => {
139
143
  const a = rectanglePath(0, 0, 10, 10);
140
144
  const b = rectanglePath(4, 4, 10, 10);
141
- expect(pathFilledArea(unionPaths(a, b))).toBeCloseTo(pathFilledArea(unionPaths(b, a)), 4);
145
+ expect(pathFilledArea(unionPaths(kernel, a, b))).toBeCloseTo(pathFilledArea(unionPaths(kernel, b, a)), 4);
142
146
  });
143
147
 
144
- it('routes through a swapped-in backend', () => {
148
+ it('routes through a provided kernel', () => {
145
149
  let seen = false;
146
- setPathBooleanBackend(
147
- (() => {
148
- const out = allocateEntity<any>();
149
- out.computePathBoolean = () => {
150
- seen = true;
151
- return [];
152
- };
153
- return finishEntity(out);
154
- })(),
155
- );
156
- const result = unionPaths(rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
157
- setPathBooleanBackend(null);
150
+ const custom = (() => {
151
+ const out = allocateEntity<PathBooleanKernel>();
152
+ out.computePathBoolean = () => {
153
+ seen = true;
154
+ return [];
155
+ };
156
+ return finishEntity(out);
157
+ })();
158
+ const result = unionPaths(custom, rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
158
159
  expect(seen).toBe(true);
159
160
  expect(result.commands.length).toBe(0);
160
161
  });
@@ -162,7 +163,7 @@ describe('unionPaths', () => {
162
163
 
163
164
  describe('xorPaths', () => {
164
165
  it('keeps the symmetric difference', () => {
165
- const result = xorPaths(rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
166
+ const result = xorPaths(kernel, rectanglePath(0, 0, 10, 10), rectanglePath(5, 5, 10, 10));
166
167
  expect(pathFilledArea(result)).toBeCloseTo(150, 4);
167
168
  });
168
169
  });
@@ -2,30 +2,33 @@ import { appendPathClose, appendPathLineTo, appendPathMoveTo, createPath } from
2
2
  import { describe, expect, it } from 'vitest';
3
3
 
4
4
  import { explainOffsetPath, explainSimplifyPath } from './explainPathBoolean';
5
+ import { martinezPathBooleanKernel } from './martinezKernel';
6
+
7
+ const kernel = martinezPathBooleanKernel;
5
8
 
6
9
  describe('explainOffsetPath', () => {
7
10
  it('distinguishes empty, invalid, collapsed, and viable results', () => {
8
- expect(explainOffsetPath(createPath(), 1)?.reason).toBe('empty-input');
11
+ expect(explainOffsetPath(kernel, createPath(), 1)?.reason).toBe('empty-input');
9
12
  const square = createPath();
10
13
  appendPathMoveTo(square, 0, 0);
11
14
  appendPathLineTo(square, 1, 0);
12
15
  appendPathLineTo(square, 1, 1);
13
16
  appendPathLineTo(square, 0, 1);
14
17
  appendPathClose(square);
15
- expect(explainOffsetPath(square, Number.NaN)?.reason).toBe('non-finite-delta');
16
- expect(explainOffsetPath(square, -1)?.reason).toBe('collapsed-or-degenerate');
17
- expect(explainOffsetPath(square, 1)).toBeNull();
18
+ expect(explainOffsetPath(kernel, square, Number.NaN)?.reason).toBe('non-finite-delta');
19
+ expect(explainOffsetPath(kernel, square, -1)?.reason).toBe('collapsed-or-degenerate');
20
+ expect(explainOffsetPath(kernel, square, 1)).toBeNull();
18
21
  });
19
22
  });
20
23
 
21
24
  describe('explainSimplifyPath', () => {
22
25
  it('distinguishes empty input from a viable region', () => {
23
- expect(explainSimplifyPath(createPath())?.reason).toBe('empty-input');
26
+ expect(explainSimplifyPath(kernel, createPath())?.reason).toBe('empty-input');
24
27
  const triangle = createPath();
25
28
  appendPathMoveTo(triangle, 0, 0);
26
29
  appendPathLineTo(triangle, 1, 0);
27
30
  appendPathLineTo(triangle, 0, 1);
28
31
  appendPathClose(triangle);
29
- expect(explainSimplifyPath(triangle)).toBeNull();
32
+ expect(explainSimplifyPath(kernel, triangle)).toBeNull();
30
33
  });
31
34
  });
@@ -3,12 +3,13 @@ import type { Path } from '@flighthq/types/contract';
3
3
  import { describe, expect, it } from 'vitest';
4
4
 
5
5
  import { differencePaths, unionPaths } from './booleanPaths';
6
+ import { martinezPathBooleanKernel } from './martinezKernel';
6
7
  import { offsetPath } from './offsetPath';
7
8
  import { simplifyPath } from './simplifyPath';
8
9
  import { unionAllPaths } from './unionAllPaths';
9
10
 
10
- // Deterministic xorshift32 PRNG. Seeded by a constant so every fuzz case is reproducible run to run — a
11
- // failing invariant always reproduces from the same seed. Never uses Math.random.
11
+ const kernel = martinezPathBooleanKernel;
12
+
12
13
  function makeRandom(seed: number): () => number {
13
14
  let state = seed >>> 0 || 1;
14
15
  return () => {
@@ -21,7 +22,6 @@ function makeRandom(seed: number): () => number {
21
22
  };
22
23
  }
23
24
 
24
- // Builds a closed polygon path from a flat [x0, y0, ...] vertex list.
25
25
  function polygonPath(vertices: readonly number[]): Path {
26
26
  const path = createPath('nonZero');
27
27
  appendPathMoveTo(path, vertices[0], vertices[1]);
@@ -30,8 +30,6 @@ function polygonPath(vertices: readonly number[]): Path {
30
30
  return path;
31
31
  }
32
32
 
33
- // A simple (non-self-intersecting) star-shaped polygon: distinct sorted angles about a center, each at a
34
- // random radius. Sorting the angles guarantees the boundary never crosses itself.
35
33
  function randomSimplePolygon(random: () => number, count: number): number[] {
36
34
  const angles: number[] = [];
37
35
  for (let i = 0; i < count; i++) angles.push(random() * Math.PI * 2);
@@ -44,21 +42,18 @@ function randomSimplePolygon(random: () => number, count: number): number[] {
44
42
  return vertices;
45
43
  }
46
44
 
47
- // A random convex polygon: the convex hull of random points, always simple and convex.
48
45
  function randomConvexPolygon(random: () => number, count: number): number[] {
49
46
  const points: [number, number][] = [];
50
47
  for (let i = 0; i < count; i++) points.push([10 + random() * 80, 10 + random() * 80]);
51
48
  return convexHull(points);
52
49
  }
53
50
 
54
- // An arbitrary, possibly self-intersecting polygon: random points visited in random order.
55
51
  function randomMessyPolygon(random: () => number, count: number): number[] {
56
52
  const vertices: number[] = [];
57
53
  for (let i = 0; i < count; i++) vertices.push(random() * 100, random() * 100);
58
54
  return vertices;
59
55
  }
60
56
 
61
- // Counter-clockwise convex hull (Andrew's monotone chain) of a point set, as a flat vertex list.
62
57
  function convexHull(points: readonly (readonly [number, number])[]): number[] {
63
58
  const sorted = points.slice().sort((a, b) => a[0] - b[0] || a[1] - b[1]);
64
59
  const cross = (o: readonly number[], a: readonly number[], b: readonly number[]): number =>
@@ -82,7 +77,6 @@ function convexHull(points: readonly (readonly [number, number])[]): number[] {
82
77
  return flat;
83
78
  }
84
79
 
85
- // Total absolute filled area of a path's flattened outline (sum of signed ring areas).
86
80
  function pathArea(path: Readonly<Path>): number {
87
81
  let total = 0;
88
82
  for (const ring of flattenPath(path)) {
@@ -94,12 +88,10 @@ function pathArea(path: Readonly<Path>): number {
94
88
  return Math.abs(total);
95
89
  }
96
90
 
97
- // Number of contours in a path's flattened outline.
98
91
  function ringCount(path: Readonly<Path>): number {
99
92
  return flattenPath(path).length;
100
93
  }
101
94
 
102
- // Whether two areas agree within a combined absolute + relative tolerance.
103
95
  function areasClose(a: number, b: number, relative = 1e-3, absolute = 1e-3): boolean {
104
96
  return Math.abs(a - b) <= absolute + relative * Math.max(Math.abs(a), Math.abs(b));
105
97
  }
@@ -117,8 +109,8 @@ describe('fuzz invariants', () => {
117
109
  for (let i = start; i < vertices.length; i += 2) appendPathLineTo(path, vertices[i], vertices[i + 1]);
118
110
  }
119
111
  appendPathClose(path);
120
- const nonZero = simplifyPath(path, { fillRule: 'nonZero' });
121
- const evenOdd = simplifyPath(path, { fillRule: 'evenOdd' });
112
+ const nonZero = simplifyPath(kernel, path, { fillRule: 'nonZero' });
113
+ const evenOdd = simplifyPath(kernel, path, { fillRule: 'evenOdd' });
122
114
  expect(pathArea(nonZero)).toBeGreaterThan(0);
123
115
  expect(pathArea(evenOdd)).toBeCloseTo(0, 6);
124
116
  }
@@ -129,8 +121,8 @@ describe('fuzz invariants', () => {
129
121
  for (let iteration = 0; iteration < 40; iteration++) {
130
122
  const a = polygonPath(randomSimplePolygon(random, 3 + (iteration % 6)));
131
123
  const b = polygonPath(randomSimplePolygon(random, 3 + ((iteration + 3) % 6)));
132
- const ab = unionPaths(a, b);
133
- const ba = unionPaths(b, a);
124
+ const ab = unionPaths(kernel, a, b);
125
+ const ba = unionPaths(kernel, b, a);
134
126
  expect(areasClose(pathArea(ab), pathArea(ba))).toBe(true);
135
127
  expect(ringCount(ab)).toBe(ringCount(ba));
136
128
  }
@@ -140,7 +132,7 @@ describe('fuzz invariants', () => {
140
132
  const random = makeRandom(0x55aa33cc);
141
133
  for (let iteration = 0; iteration < 40; iteration++) {
142
134
  const a = polygonPath(randomMessyPolygon(random, 4 + (iteration % 5)));
143
- const result = differencePaths(a, a);
135
+ const result = differencePaths(kernel, a, a);
144
136
  expect(pathArea(result)).toBeCloseTo(0, 6);
145
137
  }
146
138
  });
@@ -149,8 +141,8 @@ describe('fuzz invariants', () => {
149
141
  const random = makeRandom(0x0f0f0f0f);
150
142
  for (let iteration = 0; iteration < 40; iteration++) {
151
143
  const a = polygonPath(randomSimplePolygon(random, 3 + (iteration % 6)));
152
- const union = unionPaths(a, a);
153
- const simplified = simplifyPath(a);
144
+ const union = unionPaths(kernel, a, a);
145
+ const simplified = simplifyPath(kernel, a);
154
146
  expect(areasClose(pathArea(union), pathArea(simplified))).toBe(true);
155
147
  expect(ringCount(union)).toBe(ringCount(simplified));
156
148
  }
@@ -160,8 +152,8 @@ describe('fuzz invariants', () => {
160
152
  const random = makeRandom(0x7e577e57);
161
153
  for (let iteration = 0; iteration < 40; iteration++) {
162
154
  const a = polygonPath(randomMessyPolygon(random, 4 + (iteration % 6)));
163
- const once = simplifyPath(a);
164
- const twice = simplifyPath(once);
155
+ const once = simplifyPath(kernel, a);
156
+ const twice = simplifyPath(kernel, once);
165
157
  expect(areasClose(pathArea(once), pathArea(twice))).toBe(true);
166
158
  expect(ringCount(once)).toBe(ringCount(twice));
167
159
  }
@@ -171,8 +163,8 @@ describe('fuzz invariants', () => {
171
163
  const random = makeRandom(0x13571357);
172
164
  for (let iteration = 0; iteration < 40; iteration++) {
173
165
  const a = polygonPath(randomMessyPolygon(random, 4 + (iteration % 6)));
174
- const union = unionAllPaths([a]);
175
- const simplified = simplifyPath(a);
166
+ const union = unionAllPaths(kernel, [a]);
167
+ const simplified = simplifyPath(kernel, a);
176
168
  expect(areasClose(pathArea(union), pathArea(simplified))).toBe(true);
177
169
  expect(ringCount(union)).toBe(ringCount(simplified));
178
170
  }
@@ -183,7 +175,7 @@ describe('fuzz invariants', () => {
183
175
  for (let iteration = 0; iteration < 40; iteration++) {
184
176
  const vertices = randomSimplePolygon(random, 3 + (iteration % 6));
185
177
  const a = polygonPath(vertices);
186
- const zeroOffset = offsetPath(a, 0);
178
+ const zeroOffset = offsetPath(kernel, a, 0);
187
179
  expect(areasClose(pathArea(zeroOffset), pathArea(a), 1e-2, 1e-2)).toBe(true);
188
180
  expect(ringCount(zeroOffset)).toBe(1);
189
181
  }
@@ -193,15 +185,12 @@ describe('fuzz invariants', () => {
193
185
  const random = makeRandom(0x0badf00d);
194
186
  for (let iteration = 0; iteration < 40; iteration++) {
195
187
  const vertices = randomConvexPolygon(random, 5 + (iteration % 8));
196
- if (vertices.length < 8) continue; // need at least a quad after hull dedup
188
+ if (vertices.length < 8) continue;
197
189
  const a = polygonPath(vertices);
198
190
  const baseArea = pathArea(a);
199
- if (baseArea < 50) continue; // skip near-degenerate hulls where the erosion tolerance dominates
200
- // For a convex polygon, inflating by d then deflating by d with a miter join recovers the polygon:
201
- // the corner miters added on the grow are exactly removed on the shrink. A high miter limit keeps
202
- // sharp corners from falling back to a bevel (which would not fully recover).
203
- const grown = offsetPath(a, 3, { join: 'miter', miterLimit: 100 });
204
- const recovered = offsetPath(grown, -3, { join: 'miter', miterLimit: 100 });
191
+ if (baseArea < 50) continue;
192
+ const grown = offsetPath(kernel, a, 3, { join: 'miter', miterLimit: 100 });
193
+ const recovered = offsetPath(kernel, grown, -3, { join: 'miter', miterLimit: 100 });
205
194
  expect(ringCount(recovered)).toBe(1);
206
195
  expect(areasClose(pathArea(recovered), baseArea, 3e-2, 1)).toBe(true);
207
196
  }
@@ -211,10 +200,9 @@ describe('fuzz invariants', () => {
211
200
  const random = makeRandom(0x0c0ffee0);
212
201
  for (let iteration = 0; iteration < 40; iteration++) {
213
202
  const a = polygonPath(randomSimplePolygon(random, 5 + (iteration % 8)));
214
- const offset = offsetPath(a, 4);
203
+ const offset = offsetPath(kernel, a, 4);
215
204
  if (offset.commands.length === 0) continue;
216
- // A self-intersection-free outline is unchanged by a non-zero simplify: its area and ring count hold.
217
- const simplified = simplifyPath(offset, { fillRule: 'nonZero' });
205
+ const simplified = simplifyPath(kernel, offset, { fillRule: 'nonZero' });
218
206
  expect(areasClose(pathArea(simplified), pathArea(offset), 1e-2, 1e-2)).toBe(true);
219
207
  expect(ringCount(simplified)).toBe(ringCount(offset));
220
208
  }
@@ -1,7 +1,11 @@
1
1
  import type { PathBooleanContour, PathBooleanFillRule, PathBooleanOperation } from '@flighthq/types/contract';
2
2
  import { describe, expect, it } from 'vitest';
3
3
 
4
- import { createMartinezPathBooleanBackend, initializeMartinezPathBooleanBackend } from './martinezKernel';
4
+ import {
5
+ createMartinezPathBooleanKernel,
6
+ initializeMartinezPathBooleanKernel,
7
+ martinezPathBooleanKernel,
8
+ } from './martinezKernel';
5
9
 
6
10
  // A closed square contour [x, y, x+s, y, x+s, y+s, x, y+s], wound consistently (screen y-down CW).
7
11
  function square(x: number, y: number, s: number): number[] {
@@ -78,10 +82,10 @@ function run(
78
82
  operation: PathBooleanOperation,
79
83
  fillRule: PathBooleanFillRule = 'nonZero',
80
84
  ): readonly PathBooleanContour[] {
81
- return createMartinezPathBooleanBackend().computePathBoolean(subject, clip, operation, fillRule);
85
+ return createMartinezPathBooleanKernel().computePathBoolean(subject, clip, operation, fillRule);
82
86
  }
83
87
 
84
- describe('createMartinezPathBooleanBackend', () => {
88
+ describe('createMartinezPathBooleanKernel', () => {
85
89
  describe('coincident and shared-boundary degeneracies', () => {
86
90
  it('unions two squares sharing a full edge into one rectangle', () => {
87
91
  const result = run([square(0, 0, 10)], [square(10, 0, 10)], 'union');
@@ -374,8 +378,21 @@ describe('createMartinezPathBooleanBackend', () => {
374
378
  });
375
379
  });
376
380
  });
377
- describe('initializeMartinezPathBooleanBackend', () => {
378
- it('is the construction initializer of createMartinezPathBooleanBackend', () => {
379
- expect(typeof initializeMartinezPathBooleanBackend).toBe('function');
381
+
382
+ describe('initializeMartinezPathBooleanKernel', () => {
383
+ it('is the construction initializer of createMartinezPathBooleanKernel', () => {
384
+ expect(typeof initializeMartinezPathBooleanKernel).toBe('function');
385
+ });
386
+ });
387
+
388
+ describe('martinezPathBooleanKernel', () => {
389
+ it('is the shared kernel the boolean operations are handed', () => {
390
+ const result = martinezPathBooleanKernel.computePathBoolean(
391
+ [square(0, 0, 10)],
392
+ [square(5, 5, 10)],
393
+ 'union',
394
+ 'nonZero',
395
+ );
396
+ expect(netArea(result)).toBeCloseTo(175, 4);
380
397
  });
381
398
  });