@margoengine/points 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 (52) hide show
  1. package/LICENSE +201 -0
  2. package/NOTICE +4 -0
  3. package/README.md +794 -3
  4. package/dist/attributes-DNc-1pdr.js +256 -0
  5. package/dist/attributes.d.ts +48 -0
  6. package/dist/clip.d.ts +55 -0
  7. package/dist/clip.js +122 -0
  8. package/dist/cloud-B4O_G-Df.js +545 -0
  9. package/dist/cloud.d.ts +155 -0
  10. package/dist/color.d.ts +1 -0
  11. package/dist/copc.d.ts +55 -0
  12. package/dist/copc.js +130 -0
  13. package/dist/depth-video.d.ts +98 -0
  14. package/dist/depth-video.js +164 -0
  15. package/dist/e57.d.ts +163 -0
  16. package/dist/e57.js +624 -0
  17. package/dist/frame.d.ts +9 -0
  18. package/dist/index.d.ts +16 -0
  19. package/dist/index.js +1246 -0
  20. package/dist/las.d.ts +213 -0
  21. package/dist/las.js +500 -0
  22. package/dist/laz.d.ts +50 -0
  23. package/dist/laz.js +1025 -0
  24. package/dist/material.d.ts +167 -0
  25. package/dist/math.d.ts +103 -0
  26. package/dist/octree.d.ts +182 -0
  27. package/dist/octree.js +360 -0
  28. package/dist/pick.d.ts +76 -0
  29. package/dist/pick.js +192 -0
  30. package/dist/plugin.d.ts +235 -0
  31. package/dist/potree.d.ts +70 -0
  32. package/dist/potree.js +451 -0
  33. package/dist/range-BQuJAjUM.js +23 -0
  34. package/dist/range.d.ts +4 -0
  35. package/dist/screen-door-CALlYs39.js +7 -0
  36. package/dist/shaders-Deu8H36P.js +278 -0
  37. package/dist/shaders.d.ts +52 -0
  38. package/dist/shared/fog.d.ts +33 -0
  39. package/dist/shared/pass.d.ts +53 -0
  40. package/dist/shared/selection-mask.d.ts +52 -0
  41. package/dist/shared/style-expression.d.ts +18 -0
  42. package/dist/sprites.d.ts +78 -0
  43. package/dist/storage-limit.d.ts +6 -0
  44. package/dist/style.d.ts +124 -0
  45. package/dist/style.js +708 -0
  46. package/dist/surfels.d.ts +95 -0
  47. package/dist/surfels.js +538 -0
  48. package/dist/survey-DToZwkJv.js +46 -0
  49. package/dist/survey.d.ts +10 -0
  50. package/dist/tier-BcFbLPVT.js +91 -0
  51. package/dist/tier.d.ts +6 -0
  52. package/package.json +96 -6
@@ -0,0 +1,155 @@
1
+ /// <reference lib="esnext.disposable" preserve="true" />
2
+ import type { Mesh } from '@margoengine/core';
3
+ import { type PointBlocks } from './math.js';
4
+ import { PointsMaterial, type PointsMaterialOptions } from './material.js';
5
+ import { SelectionMasked } from './shared/selection-mask.js';
6
+ import type { MaskStyle } from './shared/selection-mask.js';
7
+ /**
8
+ * Point data. This is also the point-cloud output of `@margoengine/loaders`
9
+ * (`{kind: 'points', positions, colors?, normals?}`), which can be passed straight in.
10
+ */
11
+ export interface PointCloudData {
12
+ /** xyz per point. A Float32Array is used in place (not copied); other arrays are copied. */
13
+ positions: Float32Array | ArrayLike<number>;
14
+ /** RGB or RGBA per point: floats in 0..1, or 0..255 in a Uint8Array/Uint8ClampedArray. Packed to RGBA8. */
15
+ colors?: Float32Array | Uint8Array | Uint8ClampedArray | ArrayLike<number> | null;
16
+ /** 3 or 4. Inferred from `colors.length / count` when omitted. */
17
+ colorComponents?: 3 | 4;
18
+ /** Per-point size multiplier of `material.size`. A Float32Array is used in place. */
19
+ sizes?: Float32Array | ArrayLike<number> | null;
20
+ /** Accepted for loader interop and kept on the cloud; `SurfelPlugin` lays each disc flat on its normal. */
21
+ normals?: Float32Array | null;
22
+ }
23
+ /**
24
+ * Options for `new PointCloud(...)`. Throws a TypeError without positions, and a RangeError when the
25
+ * positions length is not a multiple of 3 or an option is out of range.
26
+ */
27
+ export interface PointCloudOptions extends PointCloudData {
28
+ /** A shared material or options for a new one. Default a new `PointsMaterial`. */
29
+ material?: PointsMaterial | PointsMaterialOptions;
30
+ /** Maximum points per culling block; a positive integer. Default 16384. */
31
+ blockSize?: number;
32
+ /** Seed of the per-block shuffle used for level of detail. Default 1. */
33
+ seed?: number;
34
+ /**
35
+ * How per-point colors are stored: 'rgba8', 4 bytes a point, clamped to 0..1, or 'float', 16 bytes a point, so HDR and
36
+ * emissive colors keep values above 1. Default 'rgba8'.
37
+ */
38
+ colorFormat?: 'rgba8' | 'float';
39
+ /**
40
+ * Color space of per-point colors. Loader output and 8-bit scanner colors are sRGB. Default 'srgb', or 'linear' with
41
+ * `colorFormat: 'float'`.
42
+ */
43
+ colorSpace?: 'srgb' | 'linear';
44
+ /**
45
+ * Permute the arrays into block order once at construction so the GPU reads them sequentially (faster for
46
+ * huge, spatially scrambled inputs). Point indices then refer to the new order; `sourceIndex` maps back.
47
+ * Default false.
48
+ */
49
+ reorder?: boolean;
50
+ /** Column-major model matrix, 16 floats, used in place, so clouds can share one. Default a new identity. */
51
+ matrix?: Float32Array;
52
+ /** World-unit margin added to every frustum plane test, for large points at block edges. Default 0. */
53
+ cullMargin?: number;
54
+ /** Label for debugging. Default ''. */
55
+ name?: string;
56
+ }
57
+ /** A per-point data stream of a PointCloud that can be marked dirty. */
58
+ export type PointChannel = 'positions' | 'colors' | 'sizes';
59
+ /** Owner interface the plugin implements, so `dispose()` can detach the cloud. */
60
+ export interface PointOwner {
61
+ remove(item: unknown): void;
62
+ /** @internal Retained per-cloud allocation inventory, without shared renderer state. */
63
+ memoryFor?(cloud: PointCloud): {
64
+ cpu: readonly ArrayLike<number>[];
65
+ gpu: readonly GPUBuffer[];
66
+ };
67
+ }
68
+ /**
69
+ * A chunked, GPU-resident point cloud drawn with a `PointsMaterial`.
70
+ * Add it to a `PointsPlugin` to draw it.
71
+ */
72
+ export declare class PointCloud extends SelectionMasked {
73
+ /** @internal Allocation inventory consumed by optional diagnostic entries. */
74
+ get memoryResources(): {
75
+ cpu: readonly (ArrayLike<number> | null)[];
76
+ gpu: readonly GPUBuffer[];
77
+ };
78
+ name: string;
79
+ /** Hidden clouds are skipped entirely. */
80
+ visible: boolean;
81
+ /** Column-major model matrix, used when the cloud is not attached to a mesh. */
82
+ readonly matrix: Float32Array;
83
+ readonly material: PointsMaterial;
84
+ readonly count: number;
85
+ readonly positions: Float32Array;
86
+ /** Packed RGBA8 per point (r in the low byte), or null. With `colorFormat: 'float'`, the bits of `floatColors`. */
87
+ colors: Uint32Array | null;
88
+ /** Component count of the colors last given (3 or 4). */
89
+ colorComponents: 3 | 4;
90
+ sizes: Float32Array | null;
91
+ /** xyz per point, in the same order as `positions`, or null. */
92
+ readonly normals: Float32Array | null;
93
+ readonly colorFormat: 'rgba8' | 'float';
94
+ readonly colorSpace: 'srgb' | 'linear';
95
+ readonly blockSize: number;
96
+ readonly seed: number;
97
+ cullMargin: number;
98
+ /** True when `reorder` permuted the arrays; the GPU then reads slots directly. */
99
+ readonly direct: boolean;
100
+ /** With `reorder`, the original index of each stored point. */
101
+ readonly sourceIndex: Uint32Array | null;
102
+ /** Chunking result: blocks, bounds and the shuffled draw order. */
103
+ blocks: PointBlocks;
104
+ /** Points drawn and blocks visible in the latest frame. */
105
+ readonly stats: {
106
+ blocksVisible: number;
107
+ pointsVisible: number;
108
+ pointsDrawn: number;
109
+ };
110
+ /** Bumped when GPU buffers must be recreated (colors or sizes added or removed, rebuild). @internal */
111
+ structure: number;
112
+ /** Pending `[start, end)` point ranges per channel. @internal */
113
+ readonly dirty: Record<PointChannel, Array<[number, number]>>;
114
+ private mesh;
115
+ private owner?;
116
+ constructor(options: PointCloudOptions);
117
+ /** 32-bit words of `colors` per point: 1, or 4 with `colorFormat: 'float'`. */
118
+ get colorStride(): number;
119
+ /** With `colorFormat: 'float'` and colors, RGBA float32 per point (a view of `colors`); otherwise null. */
120
+ get floatColors(): Float32Array | null;
121
+ /** The mesh whose world matrix this cloud follows, or null. */
122
+ get attachedMesh(): Mesh | null;
123
+ /** Follow a core mesh's world matrix (and its visibility). Pass null to use `matrix` again. */
124
+ attach(mesh: Mesh | null): this;
125
+ /** Copies `values` (xyz per point) over points `start ..` and uploads only that range next frame. */
126
+ updatePositions(start: number, values: ArrayLike<number>): this;
127
+ /** Overwrites colors of points `start ..`. `components` defaults to the cloud's current component count. */
128
+ updateColors(start: number, values: ArrayLike<number>, components?: 3 | 4): this;
129
+ /** Overwrites per-point sizes of points `start ..`. */
130
+ updateSizes(start: number, values: ArrayLike<number>): this;
131
+ /**
132
+ * Marks points `start .. start + count` of a channel as changed after you wrote into `positions`, `colors`
133
+ * (packed, or `floatColors`) or `sizes` directly. Position edits also grow the block bounds.
134
+ */
135
+ markDirty(channel: PointChannel, start?: number, count?: number): this;
136
+ /** Replaces or removes all per-point colors (reallocates the GPU buffer). */
137
+ setColors(values: ArrayLike<number> | null, components?: 3 | 4, notify?: boolean): this;
138
+ /** Replaces or removes all per-point sizes (reallocates the GPU buffer). */
139
+ setSizes(values: ArrayLike<number> | null, notify?: boolean): this;
140
+ /**
141
+ * Replaces or removes the selection mask: one value a point (stored order), selected when true or above 0. Every
142
+ * draw style keeps working (shapes, sizes, sphere shading, EDL, fog, picking): the mask only fades, highlights or
143
+ * filters the points, as `style` (or the current `maskStyle`) says. Adding or removing it reallocates; `updateMask`
144
+ * and `setMaskStyle` do not.
145
+ */
146
+ setMask(values: ArrayLike<number | boolean> | null, style?: MaskStyle): this;
147
+ /** Re-chunks from the current positions: tight bounds again, and points that became finite join a block. */
148
+ rebuild(): this;
149
+ /** Detaches from its plugin, which frees the GPU buffers. The CPU arrays stay usable. */
150
+ dispose(): void;
151
+ [Symbol.dispose]: () => void;
152
+ /** @internal */
153
+ bind(owner: PointOwner | undefined): void;
154
+ private range;
155
+ }
@@ -0,0 +1 @@
1
+ export { parseColor, type ColorInput } from '@margoengine/math/color';
package/dist/copc.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ import type { LASHeader, LASRecord } from './las.js';
2
+ import type { OctreeNode, OctreeNodeData, OctreeSource } from './octree.js';
3
+ /** Options of `CopcSource.open`. */
4
+ export interface CopcOptions {
5
+ /** Used for every range request. Default `globalThis.fetch`. */
6
+ fetch?: typeof fetch;
7
+ /** Cancels the header and hierarchy reads. */
8
+ signal?: AbortSignal;
9
+ }
10
+ /** The file's info record (user id `copc`, record id 1). */
11
+ export interface CopcInfo {
12
+ /** Centre of the octree's cube, world units. */
13
+ readonly center: readonly [number, number, number];
14
+ /** Half of the cube's side. */
15
+ readonly halfSize: number;
16
+ /** Distance between the root node's points. */
17
+ readonly spacing: number;
18
+ readonly rootHierarchyOffset: number;
19
+ readonly rootHierarchySize: number;
20
+ /** Smallest and largest GPS time in the file. */
21
+ readonly gpsTime: readonly [number, number];
22
+ }
23
+ /** Parses the 160-byte COPC info record. Throws a TypeError when the file has none. */
24
+ export declare function copcInfo(records: readonly LASRecord[]): CopcInfo;
25
+ /**
26
+ * An `OctreeSource` over a COPC file on any server that answers range requests: `CopcSource.open(url)`, then
27
+ * `OctreeCloud.attach(engine, points, source)`. The hierarchy pages are read at open, a node's points when it is
28
+ * shown. Positions and bounds are relative to `origin` (the cube's centre), so place the cloud there. A server that
29
+ * ignores `Range` sends the whole file, which works and costs that download per request.
30
+ */
31
+ export declare class CopcSource implements OctreeSource {
32
+ private readonly url;
33
+ private readonly fetcher;
34
+ readonly header: LASHeader;
35
+ readonly records: readonly LASRecord[];
36
+ readonly info: CopcInfo;
37
+ readonly root: OctreeNode;
38
+ /** World position of local [0, 0, 0]: the cube's centre. */
39
+ readonly origin: readonly [number, number, number];
40
+ private constructor();
41
+ /**
42
+ * Whether the file's colours are 16-bit (the specification) or 8-bit values stored as they are, decided once from the
43
+ * first node loaded with colour, the root when `OctreeCloud` draws it: deciding per node would brighten a dark node of
44
+ * a 16-bit file 256 times.
45
+ */
46
+ private wideColors?;
47
+ /**
48
+ * Reads the header, the records and every hierarchy page.
49
+ * @throws MargoError `'failed'` when the server answers an error; TypeError when the file is not COPC; RangeError for
50
+ * a hierarchy that is corrupt; the `signal`'s reason when it aborts.
51
+ */
52
+ static open(url: string | URL, options?: CopcOptions): Promise<CopcSource>;
53
+ /** Fetches and decodes one node's chunk. An empty node gives no points without a request. */
54
+ load(node: OctreeNode, signal: AbortSignal): Promise<OctreeNodeData>;
55
+ }
package/dist/copc.js ADDED
@@ -0,0 +1,130 @@
1
+ import { t as e } from "./range-BQuJAjUM.js";
2
+ //#region src/copc.ts
3
+ var t = 32, n = 375;
4
+ function r(e) {
5
+ let t = e.find((e) => e.userId === "copc" && e.recordId === 1);
6
+ if (!t || t.data.byteLength < 160) throw TypeError("Not a COPC file (no copc info record); pass a .copc.laz, or load a plain LAS with loadLAS");
7
+ let n = new DataView(t.data.buffer, t.data.byteOffset, t.data.byteLength), r = (e) => n.getFloat64(e, !0), i = (e) => Number(n.getBigUint64(e, !0));
8
+ return {
9
+ center: [
10
+ r(0),
11
+ r(8),
12
+ r(16)
13
+ ],
14
+ halfSize: r(24),
15
+ spacing: r(32),
16
+ rootHierarchyOffset: i(40),
17
+ rootHierarchySize: i(48),
18
+ gpsTime: [r(56), r(64)]
19
+ };
20
+ }
21
+ var i = class i {
22
+ constructor(e, t, n, r, i, a) {
23
+ this.url = e, this.fetcher = t, this.header = n, this.records = r, this.info = i, this.root = a, this.origin = i.center;
24
+ }
25
+ static async open(o, s = {}) {
26
+ let c = s.fetch ?? globalThis.fetch, l = s.signal, u = (t, n) => e(c, o, t, n, l), [d, { readLASHeader: f }] = await Promise.all([u(0, n), import("./las.js")]), { header: p, records: m } = f(await u(0, a(d))), h = r(m), g = h.halfSize * 2;
27
+ if (!(g > 0)) throw RangeError("COPC info holds a cube of no size; the file is corrupt");
28
+ let _ = /* @__PURE__ */ new Map(), v = (e, t, n, r, i, a, o) => {
29
+ let s = g / 2 ** e, c = /* @__PURE__ */ new Float64Array(6), l = [
30
+ t,
31
+ n,
32
+ r
33
+ ];
34
+ for (let e = 0; e < 3; e++) c[e] = l[e] * s - h.halfSize, c[e + 3] = c[e] + s;
35
+ let u = {
36
+ id: "",
37
+ level: e,
38
+ bounds: c,
39
+ spacing: h.spacing / 2 ** e,
40
+ count: i,
41
+ parent: null,
42
+ children: Array(8).fill(null),
43
+ offset: a,
44
+ bytes: o
45
+ };
46
+ _.set(`${e}-${t}-${n}-${r}`, u);
47
+ }, y = [[h.rootHierarchyOffset, h.rootHierarchySize]], b = /* @__PURE__ */ new Set([h.rootHierarchyOffset]);
48
+ for (; y.length;) {
49
+ let e = y.splice(0), n = await Promise.all(e.map(([e, t]) => u(e, t)));
50
+ for (let e of n) {
51
+ if (e.byteLength % t) throw RangeError("COPC hierarchy page is not whole entries; the file is corrupt");
52
+ let n = new DataView(e.buffer, e.byteOffset, e.byteLength);
53
+ for (let r = 0; r < e.byteLength; r += t) {
54
+ let e = n.getInt32(r, !0), t = n.getInt32(r + 4, !0), i = n.getInt32(r + 8, !0), a = n.getInt32(r + 12, !0), o = Number(n.getBigUint64(r + 16, !0)), s = n.getInt32(r + 24, !0), c = n.getInt32(r + 28, !0), l = 2 ** e;
55
+ if (!(e >= 0 && e < 32) || !(t >= 0 && t < l && i >= 0 && i < l && a >= 0 && a < l)) throw RangeError(`COPC hierarchy voxel ${e}-${t}-${i}-${a} lies outside the cube; the file is corrupt`);
56
+ if (c === -1) {
57
+ if (b.has(o)) throw RangeError(`COPC hierarchy page at ${o} is listed twice; the file is corrupt`);
58
+ b.add(o), y.push([o, s]);
59
+ } else if (c < 0) throw RangeError("COPC hierarchy holds a negative point count; the file is corrupt");
60
+ else v(e, t, i, a, c, o, s);
61
+ }
62
+ }
63
+ }
64
+ let x = _.get("0-0-0-0");
65
+ if (!x) throw RangeError("COPC hierarchy has no root voxel; the file is corrupt");
66
+ x.id = "r";
67
+ let S = [..._].sort((e, t) => e[1].level - t[1].level);
68
+ for (let [e, t] of S) {
69
+ if (t === x) continue;
70
+ let [n, r, i, a] = e.split("-").map(Number), o = _.get(`${n - 1}-${r >> 1}-${i >> 1}-${a >> 1}`);
71
+ if (!o) throw RangeError(`COPC voxel ${e} has no parent voxel; the file is corrupt`);
72
+ let s = r & 1 | (i & 1) << 1 | (a & 1) << 2;
73
+ t.parent = o, t.id = o.id + s, o.children[s] = t;
74
+ }
75
+ for (let e = S.length - 1; e >= 0; e--) {
76
+ let t = S[e][1], n = t.parent;
77
+ n && !t.count && !t.children.some(Boolean) && (n.children[n.children.indexOf(t)] = null);
78
+ }
79
+ return new i(o, c, p, m, h, x);
80
+ }
81
+ async load(t, n) {
82
+ let { offset: r, bytes: i, count: a } = t;
83
+ if (!a) return { positions: /* @__PURE__ */ new Float32Array() };
84
+ let [o, { decodeLazChunk: s }, { decodePoints: c }] = await Promise.all([
85
+ e(this.fetcher, this.url, r, i, n),
86
+ import("./laz.js"),
87
+ import("./las.js")
88
+ ]), l = s(o, a, this.header, this.records);
89
+ n.throwIfAborted();
90
+ let u = c({
91
+ ...this.header,
92
+ pointCount: a
93
+ }, this.records, l, { origin: this.origin }), d = u.rgb, f = {
94
+ intensity: u.intensity,
95
+ classification: u.classification,
96
+ return_number: u.returnNumber,
97
+ number_of_returns: u.numberOfReturns,
98
+ classification_flags: u.classificationFlags,
99
+ scan_angle: u.scanAngle,
100
+ point_source_id: u.pointSourceId,
101
+ user_data: u.userData,
102
+ ...u.gpsTime ? { gps_time: u.gpsTime } : {},
103
+ ...u.nir ? { nir: u.nir } : {},
104
+ ...u.extra
105
+ };
106
+ if (!d) return {
107
+ positions: u.positions,
108
+ attributes: f
109
+ };
110
+ let p = new Uint8Array(d.length), m = this.wideColors;
111
+ if (m === void 0 && d.length) {
112
+ m = !1;
113
+ for (let e = 0; e < d.length && !m; e++) m = d[e] > 255;
114
+ this.wideColors = m;
115
+ }
116
+ for (let e = 0; e < d.length; e++) p[e] = m ? d[e] >> 8 : d[e];
117
+ return {
118
+ positions: u.positions,
119
+ colors: p,
120
+ colorComponents: 3,
121
+ attributes: f
122
+ };
123
+ }
124
+ };
125
+ function a(e) {
126
+ if (e.byteLength < 100) throw RangeError("COPC header is cut short; the file is truncated");
127
+ return new DataView(e.buffer, e.byteOffset, e.byteLength).getUint32(96, !0);
128
+ }
129
+ //#endregion
130
+ export { i as CopcSource, r as copcInfo };
@@ -0,0 +1,98 @@
1
+ import { PointCloud, type PointCloudData, type PointCloudOptions } from './cloud.js';
2
+ /** 8-bit RGBA pixels, row 0 at the top: an `ImageData` fits. */
3
+ export interface DepthFrameImage {
4
+ width: number;
5
+ height: number;
6
+ data: Uint8Array | Uint8ClampedArray;
7
+ }
8
+ /** How a depth video stores depth. */
9
+ export type DepthEncoding =
10
+ /** One 8-bit channel (the red one) of a grey picture: `near` at 0 and `far` at 255 (`invert` swaps them). */
11
+ 'gray'
12
+ /** 16 bits: red is the high byte and green the low one (lossless formats only). Value / 65535 between `near` and `far`. */
13
+ | 'rg16'
14
+ /** 16 bits as in `'rg16'`, read as millimetres: metres = value / 1000, `near` and `far` still clip. */
15
+ | 'millimeters';
16
+ /** Where the depth sits in a frame. */
17
+ export type DepthLayout =
18
+ /** The colour is the left half of the frame and the depth the right half (the usual one-file depth video). */
19
+ 'side-by-side'
20
+ /** The colour is the top half and the depth the bottom half. */
21
+ | 'top-bottom'
22
+ /** The depth is a second picture, passed beside the frame. */
23
+ | 'separate';
24
+ /** Options of {@link depthVideoPoints} and {@link DepthVideoCloud}. */
25
+ export interface DepthVideoOptions {
26
+ /** Where the depth is. Default `'side-by-side'`. */
27
+ layout?: DepthLayout;
28
+ /** How depth is stored. Default `'gray'`. */
29
+ encoding?: DepthEncoding;
30
+ /** The depth, in metres, that the lowest stored value means, and the nearest depth kept. Default 0.3. */
31
+ near?: number;
32
+ /** The depth that the highest stored value means, and the farthest kept. Default 5. */
33
+ far?: number;
34
+ /** The stored values run the other way: the highest is near. Default false. */
35
+ invert?: boolean;
36
+ /** Vertical field of view in degrees, for a camera with square pixels. Default 60. Ignored with `intrinsics`. */
37
+ fov?: number;
38
+ /** The camera's focal lengths and principal point in pixels of the colour picture, when known (they set `fov`). */
39
+ intrinsics?: {
40
+ fx: number;
41
+ fy: number;
42
+ cx: number;
43
+ cy: number;
44
+ };
45
+ /** Keep every n-th pixel in both directions. Default 1. */
46
+ stride?: number;
47
+ /**
48
+ * Drop a pixel whose depth differs from a neighbour's (left, right, above, below) by more than this share of its own
49
+ * depth: the flying pixels along an edge. 0 keeps everything. Default 0.05.
50
+ */
51
+ maxJump?: number;
52
+ }
53
+ /** Points from one frame, in `PointCloudData`'s shape. */
54
+ export interface DepthVideoPoints extends PointCloudData {
55
+ positions: Float32Array;
56
+ /** RGB in 0..255 per point. */
57
+ colors: Uint8Array;
58
+ /** Pixels kept, which is `positions.length / 3`. */
59
+ count: number;
60
+ /** Size of the colour picture the points came from. */
61
+ width: number;
62
+ height: number;
63
+ }
64
+ /**
65
+ * Unprojects one RGB + depth frame into points, dropping pixels with no depth or with a jump (see `maxJump`). Returns
66
+ * `PointCloudData` for `new PointCloud(...)`. `depth` is the second picture of `layout: 'separate'`; it may differ in
67
+ * size from the colour picture.
68
+ * @throws TypeError or RangeError for options out of range, MargoError `'failed'` for an empty picture.
69
+ */
70
+ export declare function depthVideoPoints(frame: DepthFrameImage, options?: DepthVideoOptions, depth?: DepthFrameImage): DepthVideoPoints;
71
+ /** Options of {@link DepthVideoCloud}: the video options plus those of the cloud it makes. */
72
+ export interface DepthVideoCloudOptions extends DepthVideoOptions {
73
+ /** Size of the colour picture the video will deliver (after `layout` splitting), in pixels. */
74
+ width: number;
75
+ height: number;
76
+ /** Options of the `PointCloud`, for example `material`. */
77
+ cloud?: Omit<PointCloudOptions, 'positions' | 'colors' | 'sizes' | 'colorComponents'>;
78
+ }
79
+ /**
80
+ * A `PointCloud` that follows a depth video: one point per kept pixel, rewritten by every `update(frame)`. Pixels with
81
+ * no depth, and flying pixels, get size 0 for the frame, so the point count never changes and nothing is reallocated.
82
+ * Add `cloud` to a `PointsPlugin`.
83
+ */
84
+ export declare class DepthVideoCloud implements Disposable {
85
+ /** The cloud to add to a `PointsPlugin`. */
86
+ readonly cloud: PointCloud;
87
+ /** Points the cloud holds (showing or not). */
88
+ readonly count: number;
89
+ private readonly options;
90
+ constructor(options: DepthVideoCloudOptions);
91
+ /**
92
+ * Writes a frame into the cloud and returns how many points show. A frame whose colour picture is not `width` x
93
+ * `height` throws a RangeError.
94
+ */
95
+ update(frame: DepthFrameImage, depth?: DepthFrameImage): number;
96
+ /** Releases the cloud. */
97
+ [Symbol.dispose](): void;
98
+ }
@@ -0,0 +1,164 @@
1
+ import { t as e } from "./cloud-B4O_G-Df.js";
2
+ import { MargoError as t } from "@margoengine/math/errors";
3
+ //#region src/depth-video.ts
4
+ function n(e) {
5
+ let t = {
6
+ layout: e.layout ?? "side-by-side",
7
+ encoding: e.encoding ?? "gray",
8
+ near: e.near ?? .3,
9
+ far: e.far ?? 5,
10
+ invert: e.invert ?? !1,
11
+ fov: e.fov ?? 60,
12
+ intrinsics: e.intrinsics,
13
+ stride: e.stride ?? 1,
14
+ maxJump: e.maxJump ?? .05
15
+ };
16
+ if (![
17
+ "side-by-side",
18
+ "top-bottom",
19
+ "separate"
20
+ ].includes(t.layout)) throw TypeError(`layout ${t.layout} is unknown; use 'side-by-side', 'top-bottom' or 'separate'`);
21
+ if (![
22
+ "gray",
23
+ "rg16",
24
+ "millimeters"
25
+ ].includes(t.encoding)) throw TypeError(`encoding ${t.encoding} is unknown; use 'gray', 'rg16' or 'millimeters'`);
26
+ if (!(t.near > 0) || !(t.far > t.near) || !Number.isFinite(t.far)) throw RangeError(`near ${t.near} and far ${t.far} must satisfy 0 < near < far`);
27
+ if (!(t.fov > 0 && t.fov < 180)) throw RangeError(`fov ${t.fov} must be between 0 and 180 degrees`);
28
+ if (!Number.isInteger(t.stride) || t.stride < 1) throw RangeError(`stride ${t.stride} must be an integer of at least 1`);
29
+ if (!(t.maxJump >= 0)) throw RangeError(`maxJump ${t.maxJump} must not be negative`);
30
+ return t;
31
+ }
32
+ var r = (e, n) => {
33
+ if (!(e.width > 0 && e.height > 0) || e.data.length < e.width * e.height * 4) throw new t("failed", `${n}: the image is empty or shorter than width x height x 4 bytes; pass RGBA pixels`);
34
+ };
35
+ function i(e, t, n) {
36
+ if (r(e, "depth video frame"), n === "separate") {
37
+ if (!t) throw TypeError("layout 'separate' needs the depth picture as the second argument");
38
+ return r(t, "depth picture"), {
39
+ color: {
40
+ x: 0,
41
+ y: 0,
42
+ w: e.width,
43
+ h: e.height,
44
+ image: e
45
+ },
46
+ depth: {
47
+ x: 0,
48
+ y: 0,
49
+ w: t.width,
50
+ h: t.height,
51
+ image: t
52
+ }
53
+ };
54
+ }
55
+ let i = n === "side-by-side" ? e.width : e.height;
56
+ if (i % 2) throw RangeError(`a ${n} frame needs an even ${n === "side-by-side" ? "width" : "height"}; got ${i}`);
57
+ let a = n === "side-by-side" ? e.width / 2 : e.width, o = n === "side-by-side" ? e.height : e.height / 2;
58
+ return {
59
+ color: {
60
+ x: 0,
61
+ y: 0,
62
+ w: a,
63
+ h: o,
64
+ image: e
65
+ },
66
+ depth: {
67
+ x: n === "side-by-side" ? a : 0,
68
+ y: n === "side-by-side" ? 0 : o,
69
+ w: a,
70
+ h: o,
71
+ image: e
72
+ }
73
+ };
74
+ }
75
+ function a(e, t, n, r) {
76
+ let i = ((t.y + r) * t.image.width + t.x + n) * 4, a = t.image.data;
77
+ if (e.encoding === "millimeters") {
78
+ let t = a[i] << 8 | a[i + 1], n = t / 1e3;
79
+ return t !== 0 && n >= e.near && n <= e.far ? n : NaN;
80
+ }
81
+ let o = e.encoding === "gray" ? a[i] : a[i] << 8 | a[i + 1];
82
+ if (o === 0) return NaN;
83
+ let s = o / (e.encoding === "gray" ? 255 : 65535);
84
+ return e.near + (e.invert ? 1 - s : s) * (e.far - e.near);
85
+ }
86
+ function o(e, t, n) {
87
+ let { color: r, depth: o } = i(e, t, n.layout), s = n.intrinsics ?? {
88
+ fx: r.h / 2 / Math.tan(n.fov * Math.PI / 360),
89
+ fy: r.h / 2 / Math.tan(n.fov * Math.PI / 360),
90
+ cx: r.w / 2,
91
+ cy: r.h / 2
92
+ }, c = Math.ceil(r.w / n.stride), l = Math.ceil(r.h / n.stride), u = c * l, d = new Float32Array(u * 3), f = new Uint8Array(u * 3), p = new Uint8Array(u), m = new Float32Array(u);
93
+ for (let e = 0; e < l; e++) for (let t = 0; t < c; t++) {
94
+ let i = Math.min(r.w - 1, t * n.stride), s = Math.min(r.h - 1, e * n.stride);
95
+ m[e * c + t] = a(n, o, Math.min(o.w - 1, Math.floor((i + .5) / r.w * o.w)), Math.min(o.h - 1, Math.floor((s + .5) / r.h * o.h)));
96
+ }
97
+ let h = 0;
98
+ for (let e = 0; e < l; e++) for (let t = 0; t < c; t++) {
99
+ let i = e * c + t, a = m[i];
100
+ if (Number.isNaN(a)) continue;
101
+ if (n.maxJump > 0) {
102
+ let r = !1;
103
+ for (let [i, o] of [
104
+ [-1, 0],
105
+ [1, 0],
106
+ [0, -1],
107
+ [0, 1]
108
+ ]) {
109
+ let s = t + i, u = e + o;
110
+ if (s < 0 || u < 0 || s >= c || u >= l) continue;
111
+ let d = m[u * c + s];
112
+ !Number.isNaN(d) && Math.abs(d - a) > a * n.maxJump && (r = !0);
113
+ }
114
+ if (r) continue;
115
+ }
116
+ let o = Math.min(r.w - 1, t * n.stride), u = Math.min(r.h - 1, e * n.stride), g = ((r.y + u) * r.image.width + r.x + o) * 4;
117
+ d[i * 3] = (o + .5 - s.cx) * a / s.fx, d[i * 3 + 1] = (s.cy - (u + .5)) * a / s.fy, d[i * 3 + 2] = -a, f[i * 3] = r.image.data[g], f[i * 3 + 1] = r.image.data[g + 1], f[i * 3 + 2] = r.image.data[g + 2], p[i] = 1, h++;
118
+ }
119
+ return {
120
+ columns: c,
121
+ rows: l,
122
+ width: r.w,
123
+ height: r.h,
124
+ positions: d,
125
+ colors: f,
126
+ valid: p,
127
+ shown: h
128
+ };
129
+ }
130
+ function s(e, t = {}, r) {
131
+ let i = o(e, r, n(t)), a = new Float32Array(i.shown * 3), s = new Uint8Array(i.shown * 3), c = 0;
132
+ for (let e = 0; e < i.valid.length; e++) i.valid[e] && (a.set(i.positions.subarray(e * 3, e * 3 + 3), c * 3), s.set(i.colors.subarray(e * 3, e * 3 + 3), c * 3), c++);
133
+ return {
134
+ positions: a,
135
+ colors: s,
136
+ count: c,
137
+ width: i.width,
138
+ height: i.height
139
+ };
140
+ }
141
+ var c = class {
142
+ constructor(t) {
143
+ let r = n(t);
144
+ if (!Number.isInteger(t.width) || !Number.isInteger(t.height) || t.width < 1 || t.height < 1) throw RangeError(`width ${t.width} and height ${t.height} must be integers of at least 1`);
145
+ this.options = t, this.count = Math.ceil(t.width / r.stride) * Math.ceil(t.height / r.stride), this.cloud = new e({
146
+ ...t.cloud,
147
+ positions: new Float32Array(this.count * 3),
148
+ colors: new Uint8Array(this.count * 3),
149
+ colorComponents: 3,
150
+ sizes: new Float32Array(this.count)
151
+ });
152
+ }
153
+ update(e, t) {
154
+ let r = o(e, t, n(this.options));
155
+ if (r.width !== this.options.width || r.height !== this.options.height) throw RangeError(`the frame's colour picture is ${r.width} x ${r.height}; this cloud was made for ${this.options.width} x ${this.options.height}`);
156
+ let i = new Float32Array(r.valid);
157
+ return this.cloud.updatePositions(0, r.positions).updateColors(0, r.colors, 3).updateSizes(0, i), r.shown;
158
+ }
159
+ [Symbol.dispose]() {
160
+ this.cloud.dispose();
161
+ }
162
+ };
163
+ //#endregion
164
+ export { c as DepthVideoCloud, s as depthVideoPoints };