@math.gl/culling 4.1.0 → 4.2.0-alpha.5
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 +51 -0
- package/README.md +8 -1
- package/dist/index.cjs +660 -83
- package/dist/index.cjs.map +3 -4
- package/dist/index.d.ts +12 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts +1 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -1
- package/dist/lib/bounding-volumes/oriented-bounding-box.js.map +1 -1
- package/dist/lib/plane.d.ts +9 -0
- package/dist/lib/plane.d.ts.map +1 -1
- package/dist/lib/plane.js +22 -1
- package/dist/lib/plane.js.map +1 -1
- package/dist/lib/ray.d.ts +13 -0
- package/dist/lib/ray.d.ts.map +1 -0
- package/dist/lib/ray.js +28 -0
- package/dist/lib/ray.js.map +1 -0
- package/dist/lib/shapes/box-shape.d.ts +19 -0
- package/dist/lib/shapes/box-shape.d.ts.map +1 -0
- package/dist/lib/shapes/box-shape.js +69 -0
- package/dist/lib/shapes/box-shape.js.map +1 -0
- package/dist/lib/shapes/capsule-shape.d.ts +23 -0
- package/dist/lib/shapes/capsule-shape.d.ts.map +1 -0
- package/dist/lib/shapes/capsule-shape.js +151 -0
- package/dist/lib/shapes/capsule-shape.js.map +1 -0
- package/dist/lib/shapes/cylinder-shape.d.ts +22 -0
- package/dist/lib/shapes/cylinder-shape.d.ts.map +1 -0
- package/dist/lib/shapes/cylinder-shape.js +99 -0
- package/dist/lib/shapes/cylinder-shape.js.map +1 -0
- package/dist/lib/shapes/implicit-shape.d.ts +55 -0
- package/dist/lib/shapes/implicit-shape.d.ts.map +1 -0
- package/dist/lib/shapes/implicit-shape.js +143 -0
- package/dist/lib/shapes/implicit-shape.js.map +1 -0
- package/dist/lib/shapes/plane-shape.d.ts +26 -0
- package/dist/lib/shapes/plane-shape.d.ts.map +1 -0
- package/dist/lib/shapes/plane-shape.js +72 -0
- package/dist/lib/shapes/plane-shape.js.map +1 -0
- package/dist/lib/shapes/sphere-shape.d.ts +18 -0
- package/dist/lib/shapes/sphere-shape.d.ts.map +1 -0
- package/dist/lib/shapes/sphere-shape.js +43 -0
- package/dist/lib/shapes/sphere-shape.js.map +1 -0
- package/package.json +4 -4
- package/src/index.ts +13 -0
- package/src/lib/bounding-volumes/oriented-bounding-box.ts +2 -1
- package/src/lib/plane.ts +28 -1
- package/src/lib/ray.ts +31 -0
- package/src/lib/shapes/box-shape.ts +78 -0
- package/src/lib/shapes/capsule-shape.ts +232 -0
- package/src/lib/shapes/cylinder-shape.ts +133 -0
- package/src/lib/shapes/implicit-shape.ts +209 -0
- package/src/lib/shapes/plane-shape.ts +78 -0
- package/src/lib/shapes/sphere-shape.ts +60 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ImplicitShapeBase,
|
|
7
|
+
solveQuadratic,
|
|
8
|
+
type ImplicitShape,
|
|
9
|
+
type LocalRayIntersection
|
|
10
|
+
} from './implicit-shape';
|
|
11
|
+
|
|
12
|
+
const EPSILON = 1e-6;
|
|
13
|
+
|
|
14
|
+
export type SphereShapeProps = {radius?: number; matrix?: readonly number[]};
|
|
15
|
+
|
|
16
|
+
/** Analytic glTF sphere shape. */
|
|
17
|
+
export class SphereShape extends ImplicitShapeBase {
|
|
18
|
+
readonly type = 'sphere' as const;
|
|
19
|
+
readonly radius: number;
|
|
20
|
+
constructor(props: SphereShapeProps = {}) {
|
|
21
|
+
super(props.matrix);
|
|
22
|
+
this.radius = props.radius ?? 0.5;
|
|
23
|
+
if (!Number.isFinite(this.radius) || this.radius <= 0)
|
|
24
|
+
throw new Error('Sphere radius must be greater than zero');
|
|
25
|
+
}
|
|
26
|
+
clone(): SphereShape {
|
|
27
|
+
return new SphereShape({radius: this.radius, matrix: this.matrix});
|
|
28
|
+
}
|
|
29
|
+
override equals(shape: ImplicitShape): boolean {
|
|
30
|
+
return shape instanceof SphereShape && super.equals(shape) && this.radius === shape.radius;
|
|
31
|
+
}
|
|
32
|
+
containsLocalPoint(point: readonly number[]): boolean {
|
|
33
|
+
return Math.hypot(...point) <= this.radius + EPSILON;
|
|
34
|
+
}
|
|
35
|
+
distanceToLocalPoint(point: readonly number[]): number {
|
|
36
|
+
return Math.max(0, Math.hypot(...point) - this.radius);
|
|
37
|
+
}
|
|
38
|
+
supportLocal(direction: readonly number[]): readonly number[] {
|
|
39
|
+
const length = Math.hypot(...direction);
|
|
40
|
+
return length ? direction.map(value => (value / length) * this.radius) : [this.radius, 0, 0];
|
|
41
|
+
}
|
|
42
|
+
intersectLocalRay(
|
|
43
|
+
origin: readonly number[],
|
|
44
|
+
direction: readonly number[]
|
|
45
|
+
): LocalRayIntersection | undefined {
|
|
46
|
+
const roots = solveQuadratic(
|
|
47
|
+
dot(direction, direction),
|
|
48
|
+
2 * dot(origin, direction),
|
|
49
|
+
dot(origin, origin) - this.radius * this.radius
|
|
50
|
+
);
|
|
51
|
+
const distance = roots.find(value => value >= 0);
|
|
52
|
+
if (distance === undefined) return undefined;
|
|
53
|
+
const point = origin.map((value, i) => value + distance * direction[i]);
|
|
54
|
+
return {distance, normal: point.map(value => value / this.radius)};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function dot(a: readonly number[], b: readonly number[]): number {
|
|
59
|
+
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
|
|
60
|
+
}
|