@margoengine/loaders 0.0.0-stage → 0.1.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.
Files changed (184) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +1198 -3
  4. package/dist/3dm.d.ts +161 -0
  5. package/dist/3dm.js +289 -0
  6. package/dist/3ds.d.ts +124 -0
  7. package/dist/3ds.js +392 -0
  8. package/dist/acclaim.d.ts +90 -0
  9. package/dist/acclaim.js +312 -0
  10. package/dist/amf.d.ts +45 -0
  11. package/dist/amf.js +256 -0
  12. package/dist/babylon.d.ts +197 -0
  13. package/dist/babylon.js +701 -0
  14. package/dist/common.d.ts +7 -0
  15. package/dist/common.js +6 -0
  16. package/dist/dae-animation.d.ts +66 -0
  17. package/dist/dae-animation.js +266 -0
  18. package/dist/dae-common.d.ts +95 -0
  19. package/dist/dae-common.js +226 -0
  20. package/dist/dae-controllers.d.ts +57 -0
  21. package/dist/dae-controllers.js +164 -0
  22. package/dist/dae-pose.d.ts +131 -0
  23. package/dist/dae-pose.js +381 -0
  24. package/dist/dae-scene.d.ts +203 -0
  25. package/dist/dae-scene.js +279 -0
  26. package/dist/dae.d.ts +161 -0
  27. package/dist/dae.js +633 -0
  28. package/dist/detect.d.ts +20 -0
  29. package/dist/detect.js +72 -0
  30. package/dist/dxf.d.ts +81 -0
  31. package/dist/dxf.js +774 -0
  32. package/dist/engine.d.ts +36 -0
  33. package/dist/engine.js +117 -0
  34. package/dist/font-opentype.d.ts +14 -0
  35. package/dist/font-opentype.js +33 -0
  36. package/dist/font-woff.d.ts +23 -0
  37. package/dist/font-woff.js +293 -0
  38. package/dist/font.d.ts +41 -0
  39. package/dist/font.js +26 -0
  40. package/dist/gcode.d.ts +55 -0
  41. package/dist/gcode.js +278 -0
  42. package/dist/geo-model.d.ts +84 -0
  43. package/dist/geo-model.js +220 -0
  44. package/dist/geo-mvt.d.ts +15 -0
  45. package/dist/geo-mvt.js +302 -0
  46. package/dist/geo-shp.d.ts +15 -0
  47. package/dist/geo-shp.js +234 -0
  48. package/dist/geo-xml.d.ts +6 -0
  49. package/dist/geo-xml.js +165 -0
  50. package/dist/geo.d.ts +8 -0
  51. package/dist/geo.js +59 -0
  52. package/dist/geometry.d.ts +60 -0
  53. package/dist/geometry.js +273 -0
  54. package/dist/index.d.ts +54 -0
  55. package/dist/index.js +28 -0
  56. package/dist/kmz.d.ts +78 -0
  57. package/dist/kmz.js +173 -0
  58. package/dist/las.d.ts +92 -0
  59. package/dist/las.js +110 -0
  60. package/dist/ldraw.d.ts +118 -0
  61. package/dist/ldraw.js +474 -0
  62. package/dist/load.d.ts +72 -0
  63. package/dist/load.js +205 -0
  64. package/dist/lottie.d.ts +154 -0
  65. package/dist/lottie.js +893 -0
  66. package/dist/lwo.d.ts +81 -0
  67. package/dist/lwo.js +454 -0
  68. package/dist/lzf.d.ts +13 -0
  69. package/dist/lzf.js +56 -0
  70. package/dist/materialx.d.ts +120 -0
  71. package/dist/materialx.js +563 -0
  72. package/dist/md2.d.ts +52 -0
  73. package/dist/md2.js +112 -0
  74. package/dist/mmcif.d.ts +14 -0
  75. package/dist/mmcif.js +246 -0
  76. package/dist/mmd.d.ts +244 -0
  77. package/dist/mmd.js +958 -0
  78. package/dist/mocap.d.ts +71 -0
  79. package/dist/mocap.js +178 -0
  80. package/dist/molecule-common.d.ts +4 -0
  81. package/dist/molecule-common.js +54 -0
  82. package/dist/mtl.d.ts +120 -0
  83. package/dist/mtl.js +469 -0
  84. package/dist/normals.d.ts +7 -0
  85. package/dist/normals.js +31 -0
  86. package/dist/nrrd.d.ts +63 -0
  87. package/dist/nrrd.js +743 -0
  88. package/dist/nurbs.d.ts +118 -0
  89. package/dist/nurbs.js +265 -0
  90. package/dist/obj.d.ts +143 -0
  91. package/dist/obj.js +456 -0
  92. package/dist/off.d.ts +10 -0
  93. package/dist/off.js +155 -0
  94. package/dist/outline-font.d.ts +87 -0
  95. package/dist/outline-font.js +110 -0
  96. package/dist/outline.d.ts +20 -0
  97. package/dist/outline.js +215 -0
  98. package/dist/pcd.d.ts +34 -0
  99. package/dist/pcd.js +258 -0
  100. package/dist/pdb.d.ts +67 -0
  101. package/dist/pdb.js +126 -0
  102. package/dist/ply.d.ts +35 -0
  103. package/dist/ply.js +392 -0
  104. package/dist/polygon.d.ts +9 -0
  105. package/dist/polygon.js +49 -0
  106. package/dist/prwm.d.ts +24 -0
  107. package/dist/prwm.js +114 -0
  108. package/dist/render.d.ts +27 -0
  109. package/dist/render.js +153 -0
  110. package/dist/sfnt-cff.d.ts +37 -0
  111. package/dist/sfnt-cff.js +749 -0
  112. package/dist/sfnt-layout.d.ts +24 -0
  113. package/dist/sfnt-layout.js +266 -0
  114. package/dist/sfnt-reader.d.ts +28 -0
  115. package/dist/sfnt-reader.js +87 -0
  116. package/dist/sfnt-variation.d.ts +40 -0
  117. package/dist/sfnt-variation.js +318 -0
  118. package/dist/shared/asset-url.js +82 -0
  119. package/dist/shared/crc32.d.ts +2 -0
  120. package/dist/shared/crc32.js +47 -0
  121. package/dist/shared/errors.js +36 -0
  122. package/dist/shared/inflate.js +284 -0
  123. package/dist/shared/normals.js +26 -0
  124. package/dist/shared/scene-scope.js +56 -0
  125. package/dist/shared/trailing-data.js +42 -0
  126. package/dist/stl.d.ts +64 -0
  127. package/dist/stl.js +309 -0
  128. package/dist/svg.d.ts +90 -0
  129. package/dist/svg.js +1007 -0
  130. package/dist/tds.d.ts +2 -0
  131. package/dist/tds.js +2 -0
  132. package/dist/textures.d.ts +42 -0
  133. package/dist/textures.js +53 -0
  134. package/dist/threemf-lattice.d.ts +139 -0
  135. package/dist/threemf-lattice.js +514 -0
  136. package/dist/threemf-materials.d.ts +75 -0
  137. package/dist/threemf-materials.js +314 -0
  138. package/dist/threemf-mesh.d.ts +60 -0
  139. package/dist/threemf-mesh.js +201 -0
  140. package/dist/threemf.d.ts +101 -0
  141. package/dist/threemf.js +415 -0
  142. package/dist/tilt.d.ts +62 -0
  143. package/dist/tilt.js +186 -0
  144. package/dist/tmx.d.ts +151 -0
  145. package/dist/tmx.js +394 -0
  146. package/dist/ttf.d.ts +78 -0
  147. package/dist/ttf.js +568 -0
  148. package/dist/types.d.ts +90 -0
  149. package/dist/types.js +35 -0
  150. package/dist/usd.d.ts +2 -0
  151. package/dist/usd.js +2 -0
  152. package/dist/usd.worker.d.ts +1 -0
  153. package/dist/usdz.d.ts +50 -0
  154. package/dist/usdz.js +646 -0
  155. package/dist/util.d.ts +75 -0
  156. package/dist/util.js +241 -0
  157. package/dist/vdb.d.ts +111 -0
  158. package/dist/vdb.js +663 -0
  159. package/dist/vrml.d.ts +53 -0
  160. package/dist/vrml.js +532 -0
  161. package/dist/vtk-cells.d.ts +29 -0
  162. package/dist/vtk-cells.js +211 -0
  163. package/dist/vtk-legacy.d.ts +57 -0
  164. package/dist/vtk-legacy.js +439 -0
  165. package/dist/vtk-reader.d.ts +19 -0
  166. package/dist/vtk-reader.js +134 -0
  167. package/dist/vtk-xml.d.ts +96 -0
  168. package/dist/vtk-xml.js +578 -0
  169. package/dist/vtk.d.ts +13 -0
  170. package/dist/vtk.js +234 -0
  171. package/dist/woff-glyf.d.ts +17 -0
  172. package/dist/woff-glyf.js +215 -0
  173. package/dist/worker.d.ts +10 -0
  174. package/dist/worker.js +93 -0
  175. package/dist/workers/usd.js +3 -0
  176. package/dist/xfile.d.ts +99 -0
  177. package/dist/xfile.js +706 -0
  178. package/dist/xml.d.ts +107 -0
  179. package/dist/xml.js +843 -0
  180. package/dist/xyz.d.ts +16 -0
  181. package/dist/xyz.js +110 -0
  182. package/dist/zip.d.ts +38 -0
  183. package/dist/zip.js +150 -0
  184. package/package.json +246 -6
@@ -0,0 +1,87 @@
1
+ import type { Shape } from '@margoengine/geometry/shapes';
2
+ import { type OutlineCommand, type OutlineFillRule } from './outline.js';
3
+ import type { FontVariationAxis } from './sfnt-variation.js';
4
+ /** One glyph in font units: y up, origin on the baseline at the pen position. */
5
+ export interface OutlineGlyph {
6
+ /** Glyph index in the font. */
7
+ index: number;
8
+ /** Horizontal advance to the next pen position. */
9
+ advance: number;
10
+ /** Outline commands: quadratic for TrueType, any commands for other sources. Empty for blank glyphs (space). */
11
+ commands: readonly OutlineCommand[];
12
+ }
13
+ /** What an `OutlineFont` reads glyphs from. `parseTTF` builds one; implement it for other outline sources. */
14
+ export interface OutlineFontSource {
15
+ /** Font units per em: `generateShapes`' size is one em. */
16
+ unitsPerEm: number;
17
+ /** Height above the baseline, positive. */
18
+ ascender: number;
19
+ /** Depth below the baseline, negative. */
20
+ descender: number;
21
+ /** Baseline-to-baseline distance. */
22
+ lineHeight: number;
23
+ /** Glyph indices run 0 to glyphCount - 1. */
24
+ glyphCount: number;
25
+ familyName?: string;
26
+ styleName?: string;
27
+ /** Glyph index of a Unicode code point, or undefined when the font has no glyph for it. */
28
+ glyphIndex(codePoint: number): number | undefined;
29
+ /** The glyph at `index` (0 <= index < glyphCount). */
30
+ glyph(index: number): OutlineGlyph;
31
+ /**
32
+ * Extra advance between two glyph indices, in font units; 0 when the pair has none. `script` is the OpenType script
33
+ * tag of the right character ('latn', 'grek', 'cyrl' or 'DFLT').
34
+ */
35
+ kerning?(left: number, right: number, script?: string): number;
36
+ /** Glyph drawn for characters the font lacks (TrueType: 0, .notdef). Undefined skips such characters. */
37
+ missingGlyph?: number;
38
+ /** Fill rule of the outlines. Default 'nonzero'. */
39
+ fillRule?: OutlineFillRule;
40
+ /** Variation axes of a variable font. Default none. */
41
+ variationAxes?: readonly FontVariationAxis[];
42
+ }
43
+ /** Layout options of `generateShapes` and `textGeometry`. */
44
+ export interface TextShapeOptions {
45
+ /** Extra space after every character, in ems of `size`. Default 0. */
46
+ letterSpacing?: number;
47
+ /** Line distance as a multiple of the font's `lineHeight`; `'\n'` starts a new line. Default 1. */
48
+ lineHeight?: number;
49
+ /** Where each line sits relative to x = 0. Default 'left'. */
50
+ align?: 'left' | 'center' | 'right';
51
+ /** Apply the source's kerning pairs. Default true. */
52
+ kerning?: boolean;
53
+ }
54
+ /**
55
+ * A font of glyph outlines that lays out text as geometry Shapes, for `shapeGeometry`, `extrudeGeometry` or
56
+ * `textGeometry`. Build one with `parseTTF`, or from any `OutlineFontSource`. Glyphs are decoded on first use and
57
+ * cached.
58
+ */
59
+ export declare class OutlineFont {
60
+ readonly unitsPerEm: number;
61
+ readonly ascender: number;
62
+ readonly descender: number;
63
+ readonly lineHeight: number;
64
+ readonly glyphCount: number;
65
+ readonly familyName: string | undefined;
66
+ readonly styleName: string | undefined;
67
+ /** Variation axes of a variable font (pick an instance with `parseTTF`'s `variations`); empty for a static font. */
68
+ readonly variationAxes: readonly FontVariationAxis[];
69
+ private readonly source;
70
+ private readonly glyphs;
71
+ /** Throws `TypeError` for a source without `glyphIndex()`/`glyph()`, `RangeError` for non-finite metrics. */
72
+ constructor(source: OutlineFontSource);
73
+ /** Glyph index of a character (a string's first code point) or code point; undefined when the font lacks it. */
74
+ glyphIndex(char: string | number): number | undefined;
75
+ /** The glyph of a character or code point, in font units; undefined when the font lacks it. */
76
+ glyph(char: string | number): OutlineGlyph | undefined;
77
+ /** Kerning between two characters or code points, in font units; 0 when either is missing or there is none. */
78
+ kerning(left: string | number, right: string | number): number;
79
+ /**
80
+ * Lays out `text` as Shapes with holes, `size` units per em (default 100). The first baseline lies on y = 0 with
81
+ * y up, and each `'\n'` (also CR LF or CR) moves down one line. Characters the font lacks draw its `missingGlyph`,
82
+ * or are skipped when it has none.
83
+ */
84
+ generateShapes(text: string, size?: number, options?: TextShapeOptions): Shape[];
85
+ /** The glyph at a font index, decoded once and then served from the cache. */
86
+ private decoded;
87
+ }
@@ -0,0 +1,110 @@
1
+ import { outlineShapes } from './outline.js';
2
+ import { scriptTag } from './sfnt-layout.js';
3
+ const NAME = 'OutlineFont';
4
+ const METRICS = ['unitsPerEm', 'ascender', 'descender', 'lineHeight', 'glyphCount'];
5
+ const ALIGNS = ['left', 'center', 'right'];
6
+ /** The code point a character argument stands for: a string's first code point, or an integer code point. */
7
+ function toCodePoint(char) {
8
+ const cp = typeof char === 'string' ? char.codePointAt(0) : char;
9
+ if (typeof cp !== 'number' || !Number.isInteger(cp) || cp < 0 || cp > 0x10ffff)
10
+ throw new TypeError(`${NAME} character ${describe(char)} is not a character or code point; pass a one-character string or a number`);
11
+ return cp;
12
+ }
13
+ function describe(value) {
14
+ return typeof value === 'string' ? JSON.stringify(value.slice(0, 16)) : String(value).slice(0, 32);
15
+ }
16
+ /**
17
+ * A font of glyph outlines that lays out text as geometry Shapes, for `shapeGeometry`, `extrudeGeometry` or
18
+ * `textGeometry`. Build one with `parseTTF`, or from any `OutlineFontSource`. Glyphs are decoded on first use and
19
+ * cached.
20
+ */
21
+ export class OutlineFont {
22
+ /** Throws `TypeError` for a source without `glyphIndex()`/`glyph()`, `RangeError` for non-finite metrics. */
23
+ constructor(source) {
24
+ this.glyphs = new Map();
25
+ if (!source || typeof source.glyphIndex !== 'function' || typeof source.glyph !== 'function')
26
+ throw new TypeError(`${NAME} source is not a font source; pass one with glyphIndex() and glyph()`);
27
+ for (const key of METRICS)
28
+ if (!Number.isFinite(source[key]))
29
+ throw new RangeError(`${NAME} source ${key} is ${String(source[key])}; give a finite number`);
30
+ if (source.unitsPerEm <= 0)
31
+ throw new RangeError(`${NAME} source unitsPerEm is ${source.unitsPerEm}; give a positive number of units per em`);
32
+ this.source = source;
33
+ this.unitsPerEm = source.unitsPerEm;
34
+ this.ascender = source.ascender;
35
+ this.descender = source.descender;
36
+ this.lineHeight = source.lineHeight;
37
+ this.glyphCount = source.glyphCount;
38
+ this.familyName = source.familyName;
39
+ this.styleName = source.styleName;
40
+ this.variationAxes = source.variationAxes ?? [];
41
+ }
42
+ /** Glyph index of a character (a string's first code point) or code point; undefined when the font lacks it. */
43
+ glyphIndex(char) {
44
+ return this.source.glyphIndex(toCodePoint(char));
45
+ }
46
+ /** The glyph of a character or code point, in font units; undefined when the font lacks it. */
47
+ glyph(char) {
48
+ const index = this.glyphIndex(char);
49
+ return index === undefined ? undefined : this.decoded(index);
50
+ }
51
+ /** Kerning between two characters or code points, in font units; 0 when either is missing or there is none. */
52
+ kerning(left, right) {
53
+ const l = this.glyphIndex(left), r = this.glyphIndex(right);
54
+ if (l === undefined || r === undefined || !this.source.kerning)
55
+ return 0;
56
+ return this.source.kerning(l, r, scriptTag(String.fromCodePoint(toCodePoint(right))));
57
+ }
58
+ /**
59
+ * Lays out `text` as Shapes with holes, `size` units per em (default 100). The first baseline lies on y = 0 with
60
+ * y up, and each `'\n'` (also CR LF or CR) moves down one line. Characters the font lacks draw its `missingGlyph`,
61
+ * or are skipped when it has none.
62
+ */
63
+ generateShapes(text, size = 100, options = {}) {
64
+ if (typeof text !== 'string')
65
+ throw new TypeError(`${NAME} text is ${typeof text}, not a string; pass the text to lay out`);
66
+ if (!Number.isFinite(size) || size <= 0)
67
+ throw new RangeError(`${NAME} size is ${String(size)}; give a positive number of units per em`);
68
+ const opts = options ?? {}, spacing = opts.letterSpacing === undefined ? 0 : opts.letterSpacing, leading = opts.lineHeight === undefined ? 1 : opts.lineHeight, align = opts.align === undefined ? 'left' : opts.align;
69
+ if (!Number.isFinite(spacing))
70
+ throw new RangeError(`${NAME} letterSpacing is ${String(spacing)}; give a finite number of ems`);
71
+ if (!Number.isFinite(leading))
72
+ throw new RangeError(`${NAME} lineHeight is ${String(leading)}; give a finite multiple of the line height`);
73
+ if (!ALIGNS.includes(align))
74
+ throw new RangeError(`${NAME} align is ${describe(align)}; use 'left', 'center' or 'right'`);
75
+ const source = this.source, scale = size / this.unitsPerEm, fillRule = source.fillRule ?? 'nonzero', kern = opts.kerning !== false && typeof source.kerning === 'function', gap = spacing * size, lineStep = this.lineHeight * scale * leading, shapes = [];
76
+ const lines = text.replace(/\r\n?/g, '\n').split('\n');
77
+ for (let row = 0; row < lines.length; row++) {
78
+ const baseline = row ? -row * lineStep : 0, placed = [];
79
+ let pen = 0, previous;
80
+ for (const char of lines[row]) {
81
+ const index = source.glyphIndex(char.codePointAt(0)) ?? source.missingGlyph;
82
+ if (index === undefined)
83
+ continue;
84
+ if (kern && previous !== undefined)
85
+ pen += source.kerning(previous, index, scriptTag(char)) * scale;
86
+ const glyph = this.decoded(index);
87
+ placed.push({ glyph, x: pen });
88
+ pen += glyph.advance * scale + gap;
89
+ previous = index;
90
+ }
91
+ const width = placed.length ? pen - gap : 0, shift = align === 'center' ? -width / 2 : align === 'right' ? -width : 0;
92
+ for (const { glyph, x } of placed) {
93
+ if (!glyph.commands?.length)
94
+ continue;
95
+ const left = x + shift;
96
+ shapes.push(...outlineShapes(glyph.commands, fillRule, (gx, gy) => [left + gx * scale, baseline + gy * scale]));
97
+ }
98
+ }
99
+ return shapes;
100
+ }
101
+ /** The glyph at a font index, decoded once and then served from the cache. */
102
+ decoded(index) {
103
+ let glyph = this.glyphs.get(index);
104
+ if (!glyph) {
105
+ glyph = this.source.glyph(index);
106
+ this.glyphs.set(index, glyph);
107
+ }
108
+ return glyph;
109
+ }
110
+ }
@@ -0,0 +1,20 @@
1
+ import { Shape } from '@margoengine/geometry/shapes';
2
+ /**
3
+ * One drawing command of a 2D outline, in absolute coordinates. Every contour starts with `'M'`; a contour fills as
4
+ * closed whether or not it ends with `'Z'`.
5
+ */
6
+ export type OutlineCommand = ['M', number, number] | ['L', number, number] | ['Q', number, number, number, number] | ['C', number, number, number, number, number, number] | ['Z'];
7
+ /**
8
+ * Fill rule, with SVG `fill-rule` semantics: `'nonzero'` fills where the winding number is not 0 (TrueType and the
9
+ * SVG default), `'evenodd'` where a ray crosses an odd number of contours.
10
+ */
11
+ export type OutlineFillRule = 'nonzero' | 'evenodd';
12
+ /**
13
+ * Turns outline commands (glyph outlines, SVG paths) into geometry Shapes with holes. Which contours are outlines
14
+ * and which are holes comes from how they nest plus `fillRule`, never from a fixed winding convention, so outlines
15
+ * drawn in either direction (TrueType, PostScript, SVG) give the same holes. Curves are kept as curves. `map` is
16
+ * applied to every output point (classification uses the unmapped coordinates), so it should preserve nesting:
17
+ * a scale and translation, or a flip. Never throws; contours that are degenerate or contain NaN are left out.
18
+ * @internal Shared by the SVG and font loaders.
19
+ */
20
+ export declare function outlineShapes(commands: readonly OutlineCommand[], fillRule?: OutlineFillRule, map?: (x: number, y: number) => [number, number]): Shape[];
@@ -0,0 +1,215 @@
1
+ import { Path, Shape } from '@margoengine/geometry/shapes';
2
+ /** Classification samples per curve, at equal parameter steps (the end point included). */
3
+ const QUAD_STEPS = 6;
4
+ const CUBIC_STEPS = 8;
5
+ /** Points along the inner contour that vote in the containment test (odd, so there is no tie). */
6
+ const VOTES = 9;
7
+ /** Relative slack of the bounding-box test, to absorb float noise. */
8
+ const BOX_SLACK = 1e-9;
9
+ /** Breaks a command list into contours, following the SVG path rules for M, Z and commands after Z. */
10
+ function splitContours(commands) {
11
+ const rings = [];
12
+ let open;
13
+ // Where a drawing command after Z (with no new M) starts: the start of the contour Z closed.
14
+ let restart;
15
+ const ring = (x, y) => ({
16
+ x,
17
+ y,
18
+ segments: [],
19
+ poly: [],
20
+ area: 0,
21
+ minX: 0,
22
+ minY: 0,
23
+ maxX: 0,
24
+ maxY: 0,
25
+ });
26
+ const finish = () => {
27
+ if (open && open.segments.length)
28
+ rings.push(open);
29
+ open = undefined;
30
+ };
31
+ if (!Array.isArray(commands))
32
+ return rings;
33
+ for (const command of commands) {
34
+ if (!Array.isArray(command))
35
+ continue;
36
+ const kind = command[0];
37
+ if (kind === 'M') {
38
+ finish();
39
+ open = ring(+command[1], +command[2]);
40
+ restart = [open.x, open.y];
41
+ }
42
+ else if (kind === 'Z') {
43
+ if (open)
44
+ restart = [open.x, open.y];
45
+ finish();
46
+ }
47
+ else if (kind === 'L' || kind === 'Q' || kind === 'C') {
48
+ if (!open) {
49
+ if (!restart)
50
+ continue; // before the first M
51
+ open = ring(restart[0], restart[1]);
52
+ }
53
+ const count = kind === 'L' ? 2 : kind === 'Q' ? 4 : 6, values = [];
54
+ for (let i = 1; i <= count; i++)
55
+ values.push(+command[i]);
56
+ open.segments.push([kind, ...values]);
57
+ }
58
+ }
59
+ finish();
60
+ return rings;
61
+ }
62
+ /** Samples a contour as a polygon and records its signed shoelace area and bounding box. */
63
+ function measure(r) {
64
+ const poly = [r.x, r.y];
65
+ let px = r.x, py = r.y;
66
+ for (const s of r.segments) {
67
+ if (s[0] === 'L')
68
+ poly.push(s[1], s[2]);
69
+ else if (s[0] === 'Q') {
70
+ const [, cx, cy, x, y] = s;
71
+ for (let k = 1; k <= QUAD_STEPS; k++) {
72
+ const t = k / QUAD_STEPS, u = 1 - t;
73
+ poly.push(u * u * px + 2 * u * t * cx + t * t * x, u * u * py + 2 * u * t * cy + t * t * y);
74
+ }
75
+ }
76
+ else {
77
+ const [, ax, ay, bx, by, x, y] = s;
78
+ for (let k = 1; k <= CUBIC_STEPS; k++) {
79
+ const t = k / CUBIC_STEPS, u = 1 - t, w0 = u * u * u, w1 = 3 * u * u * t, w2 = 3 * u * t * t, w3 = t * t * t;
80
+ poly.push(w0 * px + w1 * ax + w2 * bx + w3 * x, w0 * py + w1 * ay + w2 * by + w3 * y);
81
+ }
82
+ }
83
+ px = poly[poly.length - 2];
84
+ py = poly[poly.length - 1];
85
+ }
86
+ // A closing sample equal to the start adds nothing to the area or the crossing test; drop it.
87
+ if (poly.length > 2 && poly[poly.length - 2] === r.x && poly[poly.length - 1] === r.y)
88
+ poly.length -= 2;
89
+ let twice = 0, minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
90
+ const n = poly.length / 2;
91
+ for (let i = 0; i < n; i++) {
92
+ const x0 = poly[i * 2], y0 = poly[i * 2 + 1], j = (i + 1) % n;
93
+ twice += x0 * poly[j * 2 + 1] - poly[j * 2] * y0;
94
+ if (x0 < minX)
95
+ minX = x0;
96
+ if (x0 > maxX)
97
+ maxX = x0;
98
+ if (y0 < minY)
99
+ minY = y0;
100
+ if (y0 > maxY)
101
+ maxY = y0;
102
+ }
103
+ r.poly = poly;
104
+ r.area = twice / 2;
105
+ r.minX = minX;
106
+ r.minY = minY;
107
+ r.maxX = maxX;
108
+ r.maxY = maxY;
109
+ }
110
+ /** Even-odd crossing test of a point against a flat polygon (ray towards +x). */
111
+ function inside(poly, x, y) {
112
+ let odd = false;
113
+ const n = poly.length / 2;
114
+ for (let i = 0, j = n - 1; i < n; j = i++) {
115
+ const xi = poly[i * 2], yi = poly[i * 2 + 1], xj = poly[j * 2], yj = poly[j * 2 + 1];
116
+ if (yi > y !== yj > y && x < xj + ((y - yj) * (xi - xj)) / (yi - yj))
117
+ odd = !odd;
118
+ }
119
+ return odd;
120
+ }
121
+ /** Whether ring `a` encloses ring `b`: larger, box inside box, and most of b's sample points inside a. */
122
+ function encloses(a, b) {
123
+ if (!(Math.abs(a.area) > Math.abs(b.area)))
124
+ return false;
125
+ const slack = BOX_SLACK * Math.max(a.maxX - a.minX, a.maxY - a.minY, 1);
126
+ if (b.minX < a.minX - slack || b.minY < a.minY - slack || b.maxX > a.maxX + slack || b.maxY > a.maxY + slack)
127
+ return false;
128
+ // Voters sit at evenly spaced fractional vertex positions, so most fall inside edges rather than on corners.
129
+ const n = b.poly.length / 2;
130
+ let yes = 0;
131
+ for (let k = 0; k < VOTES; k++) {
132
+ const at = ((k + 0.5) * n) / VOTES, i = Math.floor(at), j = (i + 1) % n, f = at - i, x = b.poly[i * 2] + (b.poly[j * 2] - b.poly[i * 2]) * f, y = b.poly[i * 2 + 1] + (b.poly[j * 2 + 1] - b.poly[i * 2 + 1]) * f;
133
+ if (inside(a.poly, x, y))
134
+ yes++;
135
+ }
136
+ return yes * 2 > VOTES;
137
+ }
138
+ /** Replays a contour's original commands into a Shape or Path through `map`, closing it when it ends elsewhere. */
139
+ function replay(target, r, map) {
140
+ const [sx, sy] = map(r.x, r.y);
141
+ target.moveTo(sx, sy);
142
+ let ex = sx, ey = sy;
143
+ for (const s of r.segments) {
144
+ if (s[0] === 'L') {
145
+ [ex, ey] = map(s[1], s[2]);
146
+ target.lineTo(ex, ey);
147
+ }
148
+ else if (s[0] === 'Q') {
149
+ const [cx, cy] = map(s[1], s[2]);
150
+ [ex, ey] = map(s[3], s[4]);
151
+ target.quadraticCurveTo(cx, cy, ex, ey);
152
+ }
153
+ else {
154
+ const [ax, ay] = map(s[1], s[2]), [bx, by] = map(s[3], s[4]);
155
+ [ex, ey] = map(s[5], s[6]);
156
+ target.bezierCurveTo(ax, ay, bx, by, ex, ey);
157
+ }
158
+ }
159
+ if (ex !== sx || ey !== sy)
160
+ target.lineTo(sx, sy);
161
+ return target;
162
+ }
163
+ const same = (x, y) => [x, y];
164
+ /**
165
+ * Turns outline commands (glyph outlines, SVG paths) into geometry Shapes with holes. Which contours are outlines
166
+ * and which are holes comes from how they nest plus `fillRule`, never from a fixed winding convention, so outlines
167
+ * drawn in either direction (TrueType, PostScript, SVG) give the same holes. Curves are kept as curves. `map` is
168
+ * applied to every output point (classification uses the unmapped coordinates), so it should preserve nesting:
169
+ * a scale and translation, or a flip. Never throws; contours that are degenerate or contain NaN are left out.
170
+ * @internal Shared by the SVG and font loaders.
171
+ */
172
+ export function outlineShapes(commands, fillRule = 'nonzero', map = same) {
173
+ const rings = [];
174
+ for (const r of splitContours(commands)) {
175
+ measure(r);
176
+ const extent = (r.maxX - r.minX) * (r.maxY - r.minY);
177
+ if (r.poly.length < 6 || !Number.isFinite(r.area) || !Number.isFinite(extent))
178
+ continue;
179
+ if (!(Math.abs(r.area) > extent * 1e-12))
180
+ continue;
181
+ rings.push(r);
182
+ }
183
+ const n = rings.length, containers = rings.map(() => []);
184
+ for (let a = 0; a < n; a++)
185
+ for (let b = 0; b < n; b++)
186
+ if (a !== b && encloses(rings[a], rings[b]))
187
+ containers[b].push(a);
188
+ // 1 solid, -1 hole, 0 dropped.
189
+ const role = rings.map((r, i) => {
190
+ const around = containers[i];
191
+ if (fillRule === 'evenodd')
192
+ return around.length % 2 === 0 ? 1 : -1;
193
+ let outside = 0;
194
+ for (const a of around)
195
+ outside += Math.sign(rings[a].area);
196
+ const within = outside + Math.sign(r.area);
197
+ if (outside === 0 && within !== 0)
198
+ return 1;
199
+ if (outside !== 0 && within === 0)
200
+ return -1;
201
+ return 0;
202
+ });
203
+ const shapes = rings.map((r, i) => (role[i] === 1 ? replay(new Shape(), r, map) : undefined));
204
+ for (let i = 0; i < n; i++) {
205
+ if (role[i] !== -1)
206
+ continue;
207
+ let owner = -1;
208
+ for (const a of containers[i])
209
+ if (role[a] === 1 && (owner < 0 || Math.abs(rings[a].area) < Math.abs(rings[owner].area)))
210
+ owner = a;
211
+ if (owner >= 0)
212
+ shapes[owner].holes.push(replay(new Path(), rings[i], map));
213
+ }
214
+ return shapes.filter((s) => s !== undefined);
215
+ }
package/dist/pcd.d.ts ADDED
@@ -0,0 +1,34 @@
1
+ import { type LoadedGeometry, type ParseLimits } from './types.js';
2
+ import { type BinaryInput } from './util.js';
3
+ /** One PCD field: its name, size, type, element count and byte offset. */
4
+ export interface PCDField {
5
+ name: string;
6
+ size: number;
7
+ type: 'I' | 'U' | 'F';
8
+ count: number;
9
+ offset: number;
10
+ }
11
+ /** A parsed PCD header: version, fields, size, viewpoint and the data encoding. */
12
+ export interface PCDHeader {
13
+ version: string;
14
+ fields: PCDField[];
15
+ width: number;
16
+ height: number;
17
+ points: number;
18
+ viewpoint: number[];
19
+ data: 'ascii' | 'binary' | 'binary_compressed';
20
+ rowSize: number;
21
+ bodyOffset: number;
22
+ }
23
+ /** Options of parsePCD: the shared size limits and whether to keep invalid (NaN) points. */
24
+ export interface PCDOptions extends ParseLimits {
25
+ /** Keep points whose x, y or z is NaN or infinite (organized clouds mark holes that way). Default false. */
26
+ keepInvalid?: boolean;
27
+ }
28
+ /** Parses only the PCD header. */
29
+ export declare function parsePCDHeader(input: BinaryInput, options?: ParseLimits): PCDHeader;
30
+ /**
31
+ * Parses a PCD point cloud with `DATA ascii`, `binary` or `binary_compressed` (LZF).
32
+ * Reads x/y/z, normal_x/y/z, packed rgb or rgba, and intensity (as `attributes.intensity`).
33
+ */
34
+ export declare function parsePCD(input: BinaryInput, options?: PCDOptions): LoadedGeometry;