@geoblocks/cesium-bvh 0.0.0-stage → 0.3.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/LICENSE ADDED
@@ -0,0 +1,29 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2020-present, camptocamp SA
4
+ All rights reserved.
5
+
6
+ Redistribution and use in source and binary forms, with or without
7
+ modification, are permitted provided that the following conditions are met:
8
+
9
+ 1. Redistributions of source code must retain the above copyright notice, this
10
+ list of conditions and the following disclaimer.
11
+
12
+ 2. Redistributions in binary form must reproduce the above copyright notice,
13
+ this list of conditions and the following disclaimer in the documentation
14
+ and/or other materials provided with the distribution.
15
+
16
+ 3. Neither the name of the copyright holder nor the names of its
17
+ contributors may be used to endorse or promote products derived from
18
+ this software without specific prior written permission.
19
+
20
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/README.md CHANGED
@@ -1,3 +1,60 @@
1
- # Temporary Holding Version
1
+ # Bounding volume hierarchy for triangles
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Rays and nearest-point queries against a triangle soup: the nearest hit along a ray, whether any lies within a distance, how many, every
4
+ one, and the closest triangle to a point; and, over line segments, those near a point or in a box. Flat typed arrays (a worker takes them as they are), no dependency: not Cesium, not three.js, not the
5
+ DOM. It serves a CesiumJS scene's picking, collisions and measuring, and anything else that asks questions of triangles.
6
+
7
+ - nodes packed depth first in typed arrays, the left child right after its parent
8
+ - a split chosen over binned surface areas (SAH); a ray walks the near child first, and a query that wants any hit stops at the first
9
+ - a ray hits both sides of a face; the least distance from its start, 0.1 mm by default, is yours to set (a negative one, `-Infinity`, counts a hit at the start)
10
+ - a hint (the triangle that blocked the last ray) is tried first: the rays of a point toward a sun five minutes on are near each other
11
+
12
+ ## Installation
13
+
14
+ ```bash
15
+ npm i --save @geoblocks/cesium-bvh
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ ```js
21
+ import {buildBvh, bvhQueries} from '@geoblocks/cesium-bvh';
22
+
23
+ // a triangle each, nine numbers: its first corner, then its two edges from it (`trianglesOf(groups)` makes them of groups
24
+ // of `{vertices, triangles}`: x, y, z a vertex, three indices a triangle)
25
+ const tris = Float64Array.of(-5, -5, 0, 10, 0, 0, 0, 10, 0);
26
+ const bvh = buildBvh(tris); // a subset of the triangles by number: buildBvh(tris, [0, 2, 5])
27
+ const queries = bvhQueries(bvh, {min: 0.01}); // a ray starting on a surface is not to hit it
28
+
29
+ queries.nearest(0, 0, 3, 0, 0, -1, 100); // 3, the distance of the nearest hit within 100 m, or Infinity
30
+ queries.nearestHit(0, 0, 3, 0, 0, -1, 100); // {triangle: 0, distance: 3}, the same with its triangle, or undefined
31
+ queries.any(0, 0, 3, 0, 0, -1, 100); // 0, the number of a triangle hit, or -1
32
+ queries.count(0, 0, 3, 0, 0, -1, 100, 6); // 1, the hits, counted to 6
33
+ queries.hits(0, 0, 3, 0, 0, -1, 100); // [3], every hit, nearest first
34
+ queries.closest(0, 0, 0.5, 2); // {triangle: 0, distance: 0.5, point: [0, 0, 0]}, the nearest triangle within 2 m and its nearest point, or undefined
35
+ ```
36
+
37
+ Line segments (edges), a tree of their own:
38
+
39
+ ```js
40
+ import {buildSegmentBvh, segmentQueries} from '@geoblocks/cesium-bvh';
41
+
42
+ const segments = Float64Array.of(0, 0, 0, 1, 0, 0, 5, 5, 5, 6, 5, 5); // a segment each, six numbers: its two ends
43
+ const edges = segmentQueries(buildSegmentBvh(segments));
44
+
45
+ edges.near(5.5, 5, 5.05, 0.1); // [1], the segments within 0.1 m of a point
46
+ edges.overlapping(-1, -1, -1, 0.5, 1, 1); // [0], those whose boxes meet a box
47
+ ```
48
+
49
+ Triangles that moved (a piece dragged, a door opened), the tree kept: its boxes made again from the leaves up, in place, a few milliseconds for 45,000 triangles (a build takes 130):
50
+
51
+ ```js
52
+ refitBvh(bvh, movedTris); // the same triangles, the same numbers, new places; `queries` made of `bvh` follow
53
+ ```
54
+
55
+ The tree is plain arrays (`bounds`, `meta`, `data`, `ids`): send them to a worker, and make the queries there.
56
+
57
+ ## Not here
58
+
59
+ - A tree that restructures as triangles move: `refitBvh` keeps the shape, so it answers right but slower the farther triangles have gone; rebuild it then.
60
+ - Triangles by a box, a sphere or a capsule: the arrays are laid out to walk, as `closest` and the segments' queries do.
@@ -0,0 +1,100 @@
1
+ /**
2
+ * The tree over some triangles.
3
+ * @param {Float64Array} tris A triangle each, nine numbers: its first corner, then its two edges from it.
4
+ * @param {Int32Array | number[]} [ids] The triangles to take; all by default.
5
+ * @param {{leaf?: number}} [options] `leaf`: the most triangles a leaf holds.
6
+ * @return {{bounds: Float64Array, meta: Int32Array, data: Float64Array, ids: Int32Array, nodes: number}} A node's box (six numbers) and
7
+ * `meta` (three: a leaf's first triangle and its count, else its right child, 0, its split axis); `data` the triangles in leaf order
8
+ * (the tris' layout), `ids` their numbers in `tris`.
9
+ */
10
+ export declare function buildBvh(tris: Float64Array, ids?: Int32Array | number[], { leaf }?: {
11
+ leaf?: number;
12
+ }): {
13
+ bounds: Float64Array;
14
+ meta: Int32Array;
15
+ data: Float64Array;
16
+ ids: Int32Array;
17
+ nodes: number;
18
+ };
19
+ /**
20
+ * The triangles of groups of vertices and indices, as `buildBvh` takes them: nine numbers each, a corner and its two edges from it, the
21
+ * groups' triangles one after the other, so a triangle's number is its place in the groups' order.
22
+ * @param {{vertices: ArrayLike<number>, triangles: ArrayLike<number>}[]} groups x, y, z a vertex; three indices a triangle.
23
+ * @param {Float64Array} [result] Of the right size: filled instead of a new array, for triangles that moved (`refitBvh`).
24
+ * @return {Float64Array}
25
+ */
26
+ export declare function trianglesOf(groups: {
27
+ vertices: ArrayLike<number>;
28
+ triangles: ArrayLike<number>;
29
+ }[], result?: Float64Array): Float64Array;
30
+ /**
31
+ * The tree kept for triangles that have moved: their new corners and edges taken, and every box made again from the leaves up, the
32
+ * tree's shape as it was. A few milliseconds for 45,000 triangles, where a build takes 130; the tree answers right at once, and
33
+ * the more the triangles have gone from where it was built for them the slower it answers (boxes grown to cover both places): build it
34
+ * again when many have moved far.
35
+ * @param {ReturnType<typeof buildBvh>} bvh A tree made by `buildBvh`, changed in place: the queries made of it follow.
36
+ * @param {Float64Array} tris The triangles the tree was built from, moved: the same layout and numbers.
37
+ * @return {ReturnType<typeof buildBvh>} The same tree.
38
+ */
39
+ export declare function refitBvh(bvh: ReturnType<typeof buildBvh>, tris: Float64Array): ReturnType<typeof buildBvh>;
40
+ /**
41
+ * The tree over some line segments, for `segmentQueries`.
42
+ * @param {Float64Array} segments A segment each, six numbers: its two ends.
43
+ * @param {Int32Array | number[]} [ids] The segments to take; all by default.
44
+ * @param {{leaf?: number}} [options] `leaf`: the most segments a leaf holds.
45
+ * @return {{bounds: Float64Array, meta: Int32Array, data: Float64Array, ids: Int32Array, nodes: number}} As `buildBvh`'s, `data` six numbers a segment.
46
+ */
47
+ export declare function buildSegmentBvh(segments: Float64Array, ids?: Int32Array | number[], { leaf }?: {
48
+ leaf?: number;
49
+ }): {
50
+ bounds: Float64Array;
51
+ meta: Int32Array;
52
+ data: Float64Array;
53
+ ids: Int32Array;
54
+ nodes: number;
55
+ };
56
+ /**
57
+ * The queries of a tree built by `buildBvh`: a ray from (px, py, pz) along the unit (dx, dy, dz), the triangles hit within `reach` meters,
58
+ * both sides of a face (a hit nearer than `min` is not one); the triangle nearest a point.
59
+ * @param {ReturnType<typeof buildBvh>} bvh
60
+ * @param {{min?: number}} [options] `min`: the least distance a hit is from the ray's start, 0.1 mm by default: a ray starting on a surface is
61
+ * not to hit it. A negative one (`-Infinity`) lets a hit at the start count: a mover standing on a floor.
62
+ */
63
+ export declare function bvhQueries({ bounds, meta, data, ids, nodes }: ReturnType<typeof buildBvh>, { min }?: {
64
+ min?: number;
65
+ }): {
66
+ /** The distance to the nearest hit within `reach`, or Infinity. */
67
+ nearest(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ dx: number, /** @type {number} */ dy: number, /** @type {number} */ dz: number, /** @type {number} */ reach: number): number;
68
+ /** The nearest hit within `reach`: its triangle's number (in the `tris` the tree was built from) and its distance; none: undefined. */
69
+ nearestHit(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ dx: number, /** @type {number} */ dy: number, /** @type {number} */ dz: number, /** @type {number} */ reach: number): {
70
+ triangle: number;
71
+ distance: number;
72
+ } | undefined;
73
+ /**
74
+ * A triangle hit within `reach`, its number, or -1 if none is. `hint`, a triangle that blocked a ray before, is tried first: the rays of
75
+ * one point toward a sun five minutes on are near the last, and the same triangle most often stops them.
76
+ */
77
+ any(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ dx: number, /** @type {number} */ dy: number, /** @type {number} */ dz: number, /** @type {number} */ reach: number, /** @type {number} */ hint?: number): number;
78
+ /** How many triangles are hit within `reach`, to `cap`. */
79
+ count(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ dx: number, /** @type {number} */ dy: number, /** @type {number} */ dz: number, /** @type {number} */ reach: number, /** @type {number} */ cap?: number): number;
80
+ /** The distances of every hit within `reach`, nearest first. */
81
+ hits(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ dx: number, /** @type {number} */ dy: number, /** @type {number} */ dz: number, /** @type {number} */ reach: number): number[];
82
+ /** The triangle nearest a point within `radius` meters, its number (in the `tris` the tree was built from), its distance and the nearest point of it; none: undefined. */
83
+ closest(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ radius: number): {
84
+ triangle: number;
85
+ distance: number;
86
+ point: number[];
87
+ } | undefined;
88
+ };
89
+ /**
90
+ * The queries of a tree built by `buildSegmentBvh`: the segments within a distance of a point, and those whose boxes meet a box. Each gives the
91
+ * segments' numbers in the array the tree was built from, in no order.
92
+ * @param {ReturnType<typeof buildSegmentBvh>} bvh
93
+ */
94
+ export declare function segmentQueries({ bounds, meta, data, ids, nodes }: ReturnType<typeof buildSegmentBvh>): {
95
+ /** The segments within `radius` meters of a point. */
96
+ near(/** @type {number} */ px: number, /** @type {number} */ py: number, /** @type {number} */ pz: number, /** @type {number} */ radius: number): number[];
97
+ /** The segments whose boxes (an end to the other) meet the box from (x0, y0, z0) to (x1, y1, z1). */
98
+ overlapping(/** @type {number} */ x0: number, /** @type {number} */ y0: number, /** @type {number} */ z0: number, /** @type {number} */ x1: number, /** @type {number} */ y1: number, /** @type {number} */ z1: number): number[];
99
+ };
100
+ //# sourceMappingURL=cesium-bvh.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cesium-bvh.d.ts","sourceRoot":"","sources":["../src/cesium-bvh.js"],"names":[],"mappings":"AA0JA;;;;;;;;GAQG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAPlB,YAOkB,EAAE,GAAG,AAN/B,C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