@math.gl/geometry 5.0.0-alpha.8 → 5.0.0-beta.1
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 +97 -49
- package/README.md +16 -1
- package/THIRD-PARTY-NOTICES.md +18 -0
- package/dist/culling/constants.d.ts +3 -0
- package/dist/culling/constants.d.ts.map +1 -0
- package/dist/culling/constants.js +5 -0
- package/dist/culling/constants.js.map +1 -0
- package/dist/culling/index.cjs +2124 -0
- package/dist/culling/index.cjs.map +6 -0
- package/dist/culling/index.d.ts +31 -0
- package/dist/culling/index.d.ts.map +1 -0
- package/dist/culling/index.js +25 -0
- package/dist/culling/index.js.map +1 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts +15 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.js +137 -0
- package/dist/culling/lib/algorithms/bounding-box-from-points.js.map +1 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts +14 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.js +146 -0
- package/dist/culling/lib/algorithms/bounding-sphere-from-points.js.map +1 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts +37 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.js +137 -0
- package/dist/culling/lib/algorithms/compute-eigen-decomposition.js.map +1 -0
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts +7 -0
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.d.ts.map +1 -0
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js +26 -0
- package/dist/culling/lib/algorithms/oriented-bounding-box-quaternion.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts +56 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js +116 -0
- package/dist/culling/lib/bounding-volumes/axis-aligned-bounding-box.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts +40 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.js +123 -0
- package/dist/culling/lib/bounding-volumes/bounding-sphere.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts +30 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.js +7 -0
- package/dist/culling/lib/bounding-volumes/bounding-volume.js.map +1 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts +62 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.d.ts.map +1 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js +230 -0
- package/dist/culling/lib/bounding-volumes/oriented-bounding-box.js.map +1 -0
- package/dist/culling/lib/culling-volume.d.ts +46 -0
- package/dist/culling/lib/culling-volume.d.ts.map +1 -0
- package/dist/culling/lib/culling-volume.js +126 -0
- package/dist/culling/lib/culling-volume.js.map +1 -0
- package/dist/culling/lib/perspective-frustum.d.ts +153 -0
- package/dist/culling/lib/perspective-frustum.d.ts.map +1 -0
- package/dist/culling/lib/perspective-frustum.js +191 -0
- package/dist/culling/lib/perspective-frustum.js.map +1 -0
- package/dist/culling/lib/perspective-off-center-frustum.d.ts +158 -0
- package/dist/culling/lib/perspective-off-center-frustum.d.ts.map +1 -0
- package/dist/culling/lib/perspective-off-center-frustum.js +276 -0
- package/dist/culling/lib/perspective-off-center-frustum.js.map +1 -0
- package/dist/culling/lib/plane.d.ts +35 -0
- package/dist/culling/lib/plane.d.ts.map +1 -0
- package/dist/culling/lib/plane.js +90 -0
- package/dist/culling/lib/plane.js.map +1 -0
- package/dist/culling/lib/ray.d.ts +13 -0
- package/dist/culling/lib/ray.d.ts.map +1 -0
- package/dist/culling/lib/ray.js +30 -0
- package/dist/culling/lib/ray.js.map +1 -0
- package/dist/culling/lib/shapes/box-shape.d.ts +19 -0
- package/dist/culling/lib/shapes/box-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/box-shape.js +69 -0
- package/dist/culling/lib/shapes/box-shape.js.map +1 -0
- package/dist/culling/lib/shapes/capsule-shape.d.ts +23 -0
- package/dist/culling/lib/shapes/capsule-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/capsule-shape.js +151 -0
- package/dist/culling/lib/shapes/capsule-shape.js.map +1 -0
- package/dist/culling/lib/shapes/cylinder-shape.d.ts +22 -0
- package/dist/culling/lib/shapes/cylinder-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/cylinder-shape.js +99 -0
- package/dist/culling/lib/shapes/cylinder-shape.js.map +1 -0
- package/dist/culling/lib/shapes/implicit-shape.d.ts +56 -0
- package/dist/culling/lib/shapes/implicit-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/implicit-shape.js +142 -0
- package/dist/culling/lib/shapes/implicit-shape.js.map +1 -0
- package/dist/culling/lib/shapes/plane-shape.d.ts +27 -0
- package/dist/culling/lib/shapes/plane-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/plane-shape.js +71 -0
- package/dist/culling/lib/shapes/plane-shape.js.map +1 -0
- package/dist/culling/lib/shapes/sphere-shape.d.ts +18 -0
- package/dist/culling/lib/shapes/sphere-shape.d.ts.map +1 -0
- package/dist/culling/lib/shapes/sphere-shape.js +43 -0
- package/dist/culling/lib/shapes/sphere-shape.js.map +1 -0
- package/dist/culling/oriented-box-2d.d.ts +22 -0
- package/dist/culling/oriented-box-2d.d.ts.map +1 -0
- package/dist/culling/oriented-box-2d.js +86 -0
- package/dist/culling/oriented-box-2d.js.map +1 -0
- package/dist/culling/queries.d.ts +44 -0
- package/dist/culling/queries.d.ts.map +1 -0
- package/dist/culling/queries.js +259 -0
- package/dist/culling/queries.js.map +1 -0
- package/dist/curves/bezier-curves.d.ts +18 -0
- package/dist/curves/bezier-curves.d.ts.map +1 -0
- package/dist/curves/bezier-curves.js +54 -0
- package/dist/curves/bezier-curves.js.map +1 -0
- package/dist/curves/catmull-rom-curve.d.ts +15 -0
- package/dist/curves/catmull-rom-curve.d.ts.map +1 -0
- package/dist/curves/catmull-rom-curve.js +77 -0
- package/dist/curves/catmull-rom-curve.js.map +1 -0
- package/dist/curves/curve-arc-length.d.ts +17 -0
- package/dist/curves/curve-arc-length.d.ts.map +1 -0
- package/dist/curves/curve-arc-length.js +58 -0
- package/dist/curves/curve-arc-length.js.map +1 -0
- package/dist/curves/curve.d.ts +23 -0
- package/dist/curves/curve.d.ts.map +1 -0
- package/dist/curves/curve.js +77 -0
- package/dist/curves/curve.js.map +1 -0
- package/dist/curves/hermite-curve.d.ts +9 -0
- package/dist/curves/hermite-curve.d.ts.map +1 -0
- package/dist/curves/hermite-curve.js +24 -0
- package/dist/curves/hermite-curve.js.map +1 -0
- package/dist/curves/index.d.ts +8 -0
- package/dist/curves/index.d.ts.map +1 -0
- package/dist/curves/index.js +9 -0
- package/dist/curves/index.js.map +1 -0
- package/dist/geometries/box-geometry.js +1 -1
- package/dist/geometries/box-geometry.js.map +1 -1
- package/dist/geometries/capsule-geometry.js +1 -1
- package/dist/geometries/capsule-geometry.js.map +1 -1
- package/dist/geometries/cylinder-geometry.js +1 -1
- package/dist/geometries/cylinder-geometry.js.map +1 -1
- package/dist/geometries/geometry-helpers.js +1 -1
- package/dist/geometries/geometry-helpers.js.map +1 -1
- package/dist/geometries/ico-sphere-geometry.js +1 -1
- package/dist/geometries/ico-sphere-geometry.js.map +1 -1
- package/dist/geometries/plane-geometry.js +1 -1
- package/dist/geometries/plane-geometry.js.map +1 -1
- package/dist/geometries/primitive-mesh-builder.d.ts +24 -0
- package/dist/geometries/primitive-mesh-builder.d.ts.map +1 -0
- package/dist/geometries/primitive-mesh-builder.js +74 -0
- package/dist/geometries/primitive-mesh-builder.js.map +1 -0
- package/dist/geometries/prism-geometry.d.ts +13 -0
- package/dist/geometries/prism-geometry.d.ts.map +1 -0
- package/dist/geometries/prism-geometry.js +34 -0
- package/dist/geometries/prism-geometry.js.map +1 -0
- package/dist/geometries/ring-geometry.d.ts +15 -0
- package/dist/geometries/ring-geometry.d.ts.map +1 -0
- package/dist/geometries/ring-geometry.js +64 -0
- package/dist/geometries/ring-geometry.js.map +1 -0
- package/dist/geometries/sphere-geometry.js +1 -1
- package/dist/geometries/sphere-geometry.js.map +1 -1
- package/dist/geometries/surface-of-revolution.js +1 -1
- package/dist/geometries/surface-of-revolution.js.map +1 -1
- package/dist/geometries/truncated-cone-geometry.js +1 -1
- package/dist/geometries/truncated-cone-geometry.js.map +1 -1
- package/dist/index.cjs +5867 -16
- package/dist/index.cjs.map +3 -3
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/dist/lib/geometry-utils.js +1 -1
- package/dist/lib/geometry-utils.js.map +1 -1
- package/dist/lib/geometry.d.ts +1 -1
- package/dist/lib/geometry.d.ts.map +1 -1
- package/dist/lib/geometry.js +1 -1
- package/dist/lib/geometry.js.map +1 -1
- package/dist/lib/hinge-tree.d.ts +33 -0
- package/dist/lib/hinge-tree.d.ts.map +1 -0
- package/dist/lib/hinge-tree.js +144 -0
- package/dist/lib/hinge-tree.js.map +1 -0
- package/dist/parametric/lathe-geometry.d.ts +14 -0
- package/dist/parametric/lathe-geometry.d.ts.map +1 -0
- package/dist/parametric/lathe-geometry.js +48 -0
- package/dist/parametric/lathe-geometry.js.map +1 -0
- package/dist/parametric/parametric-geometry.d.ts +20 -0
- package/dist/parametric/parametric-geometry.d.ts.map +1 -0
- package/dist/parametric/parametric-geometry.js +88 -0
- package/dist/parametric/parametric-geometry.js.map +1 -0
- package/dist/parametric/torus-geometry.d.ts +19 -0
- package/dist/parametric/torus-geometry.d.ts.map +1 -0
- package/dist/parametric/torus-geometry.js +89 -0
- package/dist/parametric/torus-geometry.js.map +1 -0
- package/dist/parametric.d.ts +7 -0
- package/dist/parametric.d.ts.map +1 -0
- package/dist/parametric.js +7 -0
- package/dist/parametric.js.map +1 -0
- package/dist/polygon/clip-triangle-mesh.d.ts +41 -0
- package/dist/polygon/clip-triangle-mesh.d.ts.map +1 -0
- package/dist/polygon/clip-triangle-mesh.js +155 -0
- package/dist/polygon/clip-triangle-mesh.js.map +1 -0
- package/dist/polygon/cut-by-grid.d.ts +24 -0
- package/dist/polygon/cut-by-grid.d.ts.map +1 -0
- package/dist/polygon/cut-by-grid.js +221 -0
- package/dist/polygon/cut-by-grid.js.map +1 -0
- package/dist/polygon/cut-by-mercator-bounds.d.ts +17 -0
- package/dist/polygon/cut-by-mercator-bounds.d.ts.map +1 -0
- package/dist/polygon/cut-by-mercator-bounds.js +149 -0
- package/dist/polygon/cut-by-mercator-bounds.js.map +1 -0
- package/dist/polygon/earcut.d.ts +13 -0
- package/dist/polygon/earcut.d.ts.map +1 -0
- package/dist/polygon/earcut.js +627 -0
- package/dist/polygon/earcut.js.map +1 -0
- package/dist/polygon/index.d.ts +14 -0
- package/dist/polygon/index.d.ts.map +1 -0
- package/dist/polygon/index.js +13 -0
- package/dist/polygon/index.js.map +1 -0
- package/dist/polygon/lineclip.d.ts +31 -0
- package/dist/polygon/lineclip.d.ts.map +1 -0
- package/dist/polygon/lineclip.js +184 -0
- package/dist/polygon/lineclip.js.map +1 -0
- package/dist/polygon/polygon-utils.d.ts +103 -0
- package/dist/polygon/polygon-utils.d.ts.map +1 -0
- package/dist/polygon/polygon-utils.js +155 -0
- package/dist/polygon/polygon-utils.js.map +1 -0
- package/dist/polygon/polygon.d.ts +41 -0
- package/dist/polygon/polygon.d.ts.map +1 -0
- package/dist/polygon/polygon.js +76 -0
- package/dist/polygon/polygon.js.map +1 -0
- package/dist/polygon/subdivide-polyline.d.ts +56 -0
- package/dist/polygon/subdivide-polyline.d.ts.map +1 -0
- package/dist/polygon/subdivide-polyline.js +141 -0
- package/dist/polygon/subdivide-polyline.js.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts +68 -0
- package/dist/polygon/subdivide-triangle-mesh.d.ts.map +1 -0
- package/dist/polygon/subdivide-triangle-mesh.js +228 -0
- package/dist/polygon/subdivide-triangle-mesh.js.map +1 -0
- package/dist/polygon/utils.d.ts +5 -0
- package/dist/polygon/utils.d.ts.map +1 -0
- package/dist/polygon/utils.js +38 -0
- package/dist/polygon/utils.js.map +1 -0
- package/dist/spatial-index/box-index.d.ts +86 -0
- package/dist/spatial-index/box-index.d.ts.map +1 -0
- package/dist/spatial-index/box-index.js +335 -0
- package/dist/spatial-index/box-index.js.map +1 -0
- package/dist/spatial-index/index.d.ts +7 -0
- package/dist/spatial-index/index.d.ts.map +1 -0
- package/dist/spatial-index/index.js +6 -0
- package/dist/spatial-index/index.js.map +1 -0
- package/dist/spatial-index/triangle-bvh.d.ts +74 -0
- package/dist/spatial-index/triangle-bvh.d.ts.map +1 -0
- package/dist/spatial-index/triangle-bvh.js +221 -0
- package/dist/spatial-index/triangle-bvh.js.map +1 -0
- package/dist/utils/attribute-compression.d.ts +50 -0
- package/dist/utils/attribute-compression.d.ts.map +1 -0
- package/dist/utils/attribute-compression.js +186 -0
- package/dist/utils/attribute-compression.js.map +1 -0
- package/dist/utils/attribute-iterator.d.ts +8 -0
- package/dist/utils/attribute-iterator.d.ts.map +1 -0
- package/dist/utils/attribute-iterator.js +25 -0
- package/dist/utils/attribute-iterator.js.map +1 -0
- package/dist/utils/compute-vertex-normals.d.ts +4 -0
- package/dist/utils/compute-vertex-normals.d.ts.map +1 -0
- package/dist/utils/compute-vertex-normals.js +54 -0
- package/dist/utils/compute-vertex-normals.js.map +1 -0
- package/dist/utils/constants.d.ts +52 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +39 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/coordinates.d.ts +3 -0
- package/dist/utils/coordinates.d.ts.map +1 -0
- package/dist/utils/coordinates.js +8 -0
- package/dist/utils/coordinates.js.map +1 -0
- package/dist/utils/geometry.d.ts +20 -0
- package/dist/utils/geometry.d.ts.map +1 -0
- package/dist/utils/geometry.js +31 -0
- package/dist/utils/geometry.js.map +1 -0
- package/dist/utils/gl-type.d.ts +17 -0
- package/dist/utils/gl-type.d.ts.map +1 -0
- package/dist/utils/gl-type.js +69 -0
- package/dist/utils/gl-type.js.map +1 -0
- package/dist/utils/index.d.ts +18 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +16 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/interpolate-packed-attributes.d.ts +43 -0
- package/dist/utils/interpolate-packed-attributes.d.ts.map +1 -0
- package/dist/utils/interpolate-packed-attributes.js +192 -0
- package/dist/utils/interpolate-packed-attributes.js.map +1 -0
- package/dist/utils/mesh-processing.d.ts +18 -0
- package/dist/utils/mesh-processing.d.ts.map +1 -0
- package/dist/utils/mesh-processing.js +300 -0
- package/dist/utils/mesh-processing.js.map +1 -0
- package/dist/utils/primitive-iterator.d.ts +21 -0
- package/dist/utils/primitive-iterator.d.ts.map +1 -0
- package/dist/utils/primitive-iterator.js +117 -0
- package/dist/utils/primitive-iterator.js.map +1 -0
- package/dist/utils/rgb565.d.ts +5 -0
- package/dist/utils/rgb565.d.ts.map +1 -0
- package/dist/utils/rgb565.js +24 -0
- package/dist/utils/rgb565.js.map +1 -0
- package/dist/utils/typed-array-utils.d.ts +3 -0
- package/dist/utils/typed-array-utils.d.ts.map +1 -0
- package/dist/utils/typed-array-utils.js +16 -0
- package/dist/utils/typed-array-utils.js.map +1 -0
- package/package.json +12 -4
- package/src/culling/constants.ts +6 -0
- package/src/culling/index.ts +55 -0
- package/src/culling/lib/algorithms/bounding-box-from-points.ts +174 -0
- package/src/culling/lib/algorithms/bounding-sphere-from-points.ts +175 -0
- package/src/culling/lib/algorithms/compute-eigen-decomposition.ts +171 -0
- package/src/culling/lib/algorithms/oriented-bounding-box-quaternion.ts +32 -0
- package/src/culling/lib/bounding-volumes/axis-aligned-bounding-box.ts +153 -0
- package/src/culling/lib/bounding-volumes/bounding-sphere.ts +154 -0
- package/src/culling/lib/bounding-volumes/bounding-volume.ts +38 -0
- package/src/culling/lib/bounding-volumes/oriented-bounding-box.ts +320 -0
- package/src/culling/lib/culling-volume.ts +163 -0
- package/src/culling/lib/perspective-frustum.ts +288 -0
- package/src/culling/lib/perspective-off-center-frustum.ts +381 -0
- package/src/culling/lib/plane.ts +118 -0
- package/src/culling/lib/ray.ts +33 -0
- package/src/culling/lib/shapes/box-shape.ts +78 -0
- package/src/culling/lib/shapes/capsule-shape.ts +232 -0
- package/src/culling/lib/shapes/cylinder-shape.ts +133 -0
- package/src/culling/lib/shapes/implicit-shape.ts +209 -0
- package/src/culling/lib/shapes/plane-shape.ts +78 -0
- package/src/culling/lib/shapes/sphere-shape.ts +60 -0
- package/src/culling/oriented-box-2d.ts +165 -0
- package/src/culling/queries.ts +332 -0
- package/src/curves/bezier-curves.ts +62 -0
- package/src/curves/catmull-rom-curve.ts +89 -0
- package/src/curves/curve-arc-length.ts +70 -0
- package/src/curves/curve.ts +96 -0
- package/src/curves/hermite-curve.ts +34 -0
- package/src/curves/index.ts +15 -0
- package/src/geometries/box-geometry.ts +1 -1
- package/src/geometries/capsule-geometry.ts +1 -1
- package/src/geometries/cylinder-geometry.ts +1 -1
- package/src/geometries/geometry-helpers.ts +1 -1
- package/src/geometries/ico-sphere-geometry.ts +1 -1
- package/src/geometries/plane-geometry.ts +1 -1
- package/src/geometries/primitive-mesh-builder.ts +84 -0
- package/src/geometries/prism-geometry.ts +57 -0
- package/src/geometries/ring-geometry.ts +102 -0
- package/src/geometries/sphere-geometry.ts +1 -1
- package/src/geometries/surface-of-revolution.ts +1 -1
- package/src/geometries/truncated-cone-geometry.ts +1 -1
- package/src/index.ts +175 -1
- package/src/lib/geometry-utils.ts +2 -2
- package/src/lib/geometry.ts +2 -2
- package/src/lib/hinge-tree.ts +170 -0
- package/src/parametric/lathe-geometry.ts +70 -0
- package/src/parametric/parametric-geometry.ts +118 -0
- package/src/parametric/torus-geometry.ts +118 -0
- package/src/parametric.ts +9 -0
- package/src/polygon/clip-triangle-mesh.ts +218 -0
- package/src/polygon/cut-by-grid.ts +313 -0
- package/src/polygon/cut-by-mercator-bounds.ts +204 -0
- package/src/polygon/earcut.ts +798 -0
- package/src/polygon/index.ts +42 -0
- package/src/polygon/lineclip.ts +218 -0
- package/src/polygon/polygon-utils.ts +273 -0
- package/src/polygon/polygon.ts +105 -0
- package/src/polygon/subdivide-polyline.ts +223 -0
- package/src/polygon/subdivide-triangle-mesh.ts +322 -0
- package/src/polygon/utils.ts +50 -0
- package/src/spatial-index/box-index.ts +452 -0
- package/src/spatial-index/index.ts +20 -0
- package/src/spatial-index/triangle-bvh.ts +345 -0
- package/src/utils/attribute-compression.ts +234 -0
- package/src/utils/attribute-iterator.ts +28 -0
- package/src/utils/compute-vertex-normals.ts +63 -0
- package/src/utils/constants.ts +42 -0
- package/src/utils/coordinates.ts +8 -0
- package/src/utils/geometry.ts +49 -0
- package/src/utils/gl-type.ts +87 -0
- package/src/utils/index.ts +55 -0
- package/src/utils/interpolate-packed-attributes.ts +284 -0
- package/src/utils/mesh-processing.ts +377 -0
- package/src/utils/primitive-iterator.ts +145 -0
- package/src/utils/rgb565.ts +26 -0
- package/src/utils/typed-array-utils.ts +16 -0
|
@@ -0,0 +1,377 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import {Matrix4} from '@math.gl/core';
|
|
6
|
+
import type {TypedArray, TypedArrayConstructor} from '@math.gl/core';
|
|
7
|
+
import {GL} from './constants';
|
|
8
|
+
import {getAttributeValues, type Geometry, type GeometryAttribute} from './geometry';
|
|
9
|
+
|
|
10
|
+
type MeshData = {
|
|
11
|
+
attributes: Record<string, {value: TypedArray; size: number}>;
|
|
12
|
+
position: string;
|
|
13
|
+
count: number;
|
|
14
|
+
indices: Uint32Array;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
/** Fresh, indexed triangle-list geometry. No input buffers are changed. */
|
|
18
|
+
export function transformGeometry(
|
|
19
|
+
geometry: Geometry,
|
|
20
|
+
matrix: Readonly<ArrayLike<number>>
|
|
21
|
+
): Geometry {
|
|
22
|
+
const mesh = readMesh(geometry);
|
|
23
|
+
if (
|
|
24
|
+
matrix.length !== 16 ||
|
|
25
|
+
Array.from(matrix).some(v => !Number.isFinite(v)) ||
|
|
26
|
+
matrix[3] !== 0 ||
|
|
27
|
+
matrix[7] !== 0 ||
|
|
28
|
+
matrix[11] !== 0 ||
|
|
29
|
+
matrix[15] !== 1
|
|
30
|
+
) {
|
|
31
|
+
throw new RangeError('Transform must be a finite affine column-major matrix');
|
|
32
|
+
}
|
|
33
|
+
const transform = new Matrix4(Array.from(matrix));
|
|
34
|
+
// Factor out independent column scales before testing orientation/invertibility.
|
|
35
|
+
// A raw determinant can underflow or overflow for finite invertible matrices.
|
|
36
|
+
const columns = [0, 4, 8].map(offset => Array.from(matrix).slice(offset, offset + 3));
|
|
37
|
+
const scales = columns.map(column => Math.max(...column.map(Math.abs)));
|
|
38
|
+
const basis = columns.map((column, i) => column.map(v => (scales[i] ? v / scales[i] : 0)));
|
|
39
|
+
const cofactors = [
|
|
40
|
+
cross(basis[1], basis[2]),
|
|
41
|
+
cross(basis[2], basis[0]),
|
|
42
|
+
cross(basis[0], basis[1])
|
|
43
|
+
];
|
|
44
|
+
const determinant = basis[0].reduce((sum, v, i) => sum + v * cofactors[0][i], 0);
|
|
45
|
+
const attributes = copyAttributes(mesh.attributes);
|
|
46
|
+
const normalName = attributes['NORMAL'] ? 'NORMAL' : attributes['normals'] ? 'normals' : null;
|
|
47
|
+
const tangentName = attributes['TANGENT'] ? 'TANGENT' : null;
|
|
48
|
+
if ((normalName || tangentName) && (!Number.isFinite(determinant) || determinant === 0))
|
|
49
|
+
throw new RangeError('Normals and tangents require an invertible transform');
|
|
50
|
+
const positions = new Float64Array(mesh.count * 3);
|
|
51
|
+
const source = mesh.attributes[mesh.position].value;
|
|
52
|
+
for (let i = 0; i < mesh.count; i++) {
|
|
53
|
+
const point = transform.transformAsPoint([source[i * 3], source[i * 3 + 1], source[i * 3 + 2]]);
|
|
54
|
+
positions.set(point, i * 3);
|
|
55
|
+
}
|
|
56
|
+
attributes[mesh.position] = {size: 3, value: positions};
|
|
57
|
+
if (normalName) {
|
|
58
|
+
const input = mesh.attributes[normalName];
|
|
59
|
+
if (input.size !== 3) throw new RangeError('Normals must have size 3');
|
|
60
|
+
const output = new Float64Array(mesh.count * 3);
|
|
61
|
+
for (let i = 0; i < mesh.count; i++) {
|
|
62
|
+
const value = normalizedLinearCombination(
|
|
63
|
+
cofactors,
|
|
64
|
+
Array.from(input.value.slice(i * 3, i * 3 + 3)),
|
|
65
|
+
scales.map(v => -Math.log(v))
|
|
66
|
+
);
|
|
67
|
+
output.set(
|
|
68
|
+
value.map(v => (determinant < 0 ? -v : v)),
|
|
69
|
+
i * 3
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
attributes[normalName] = {size: 3, value: output};
|
|
73
|
+
}
|
|
74
|
+
if (tangentName) {
|
|
75
|
+
const input = mesh.attributes[tangentName];
|
|
76
|
+
if (input.size !== 4) throw new RangeError('Tangents must have size 4');
|
|
77
|
+
const output = new Float64Array(mesh.count * 4);
|
|
78
|
+
for (let i = 0; i < mesh.count; i++) {
|
|
79
|
+
const value = normalizedLinearCombination(
|
|
80
|
+
basis,
|
|
81
|
+
Array.from(input.value.slice(i * 4, i * 4 + 3)),
|
|
82
|
+
scales.map(Math.log)
|
|
83
|
+
);
|
|
84
|
+
if (normalName) {
|
|
85
|
+
const n = attributes[normalName].value.slice(i * 3, i * 3 + 3);
|
|
86
|
+
const projection = value[0] * n[0] + value[1] * n[1] + value[2] * n[2];
|
|
87
|
+
for (let axis = 0; axis < 3; axis++) value[axis] -= projection * n[axis];
|
|
88
|
+
}
|
|
89
|
+
const length = Math.hypot(...value);
|
|
90
|
+
output.set(
|
|
91
|
+
value.map(v => (length ? v / length : 0)),
|
|
92
|
+
i * 4
|
|
93
|
+
);
|
|
94
|
+
output[i * 4 + 3] = input.value[i * 4 + 3] * (determinant < 0 ? -1 : 1);
|
|
95
|
+
}
|
|
96
|
+
attributes[tangentName] = {size: 4, value: output};
|
|
97
|
+
}
|
|
98
|
+
const indices = mesh.indices.slice();
|
|
99
|
+
if (determinant < 0)
|
|
100
|
+
for (let i = 0; i < indices.length; i += 3)
|
|
101
|
+
[indices[i + 1], indices[i + 2]] = [indices[i + 2], indices[i + 1]];
|
|
102
|
+
return {mode: GL.TRIANGLES, attributes, indices};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Concatenates compatible triangle meshes and offsets vertex indices. */
|
|
106
|
+
export function mergeGeometries(geometries: readonly Geometry[]): Geometry {
|
|
107
|
+
if (!geometries.length) throw new RangeError('At least one geometry is required');
|
|
108
|
+
const meshes = geometries.map(readMesh),
|
|
109
|
+
first = meshes[0],
|
|
110
|
+
names = Object.keys(first.attributes).sort();
|
|
111
|
+
let count = 0,
|
|
112
|
+
indexCount = 0;
|
|
113
|
+
for (const mesh of meshes) {
|
|
114
|
+
if (
|
|
115
|
+
mesh.position !== first.position ||
|
|
116
|
+
Object.keys(mesh.attributes).length !== names.length ||
|
|
117
|
+
names.some(name => !Object.prototype.hasOwnProperty.call(mesh.attributes, name))
|
|
118
|
+
)
|
|
119
|
+
throw new RangeError('Attribute names must match');
|
|
120
|
+
for (const name of names) {
|
|
121
|
+
if (
|
|
122
|
+
mesh.attributes[name].size !== first.attributes[name].size ||
|
|
123
|
+
mesh.attributes[name].value.constructor !== first.attributes[name].value.constructor
|
|
124
|
+
) {
|
|
125
|
+
throw new RangeError('Attribute sizes and storage types must match');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
count += mesh.count;
|
|
129
|
+
indexCount += mesh.indices.length;
|
|
130
|
+
}
|
|
131
|
+
if (count > 0xffffffff) throw new RangeError('Geometry exceeds 32-bit indexing');
|
|
132
|
+
const attributes: Record<string, GeometryAttribute> = {};
|
|
133
|
+
for (const name of names) {
|
|
134
|
+
const attribute = first.attributes[name],
|
|
135
|
+
Constructor = attribute.value.constructor as TypedArrayConstructor;
|
|
136
|
+
const value = new Constructor(count * attribute.size);
|
|
137
|
+
let offset = 0;
|
|
138
|
+
for (const mesh of meshes) {
|
|
139
|
+
value.set(mesh.attributes[name].value, offset);
|
|
140
|
+
offset += mesh.attributes[name].value.length;
|
|
141
|
+
}
|
|
142
|
+
attributes[name] = {size: attribute.size, value};
|
|
143
|
+
}
|
|
144
|
+
const indices = new Uint32Array(indexCount);
|
|
145
|
+
let vertexOffset = 0,
|
|
146
|
+
indexOffset = 0;
|
|
147
|
+
for (const mesh of meshes) {
|
|
148
|
+
for (const index of mesh.indices) indices[indexOffset++] = index + vertexOffset;
|
|
149
|
+
vertexOffset += mesh.count;
|
|
150
|
+
}
|
|
151
|
+
return {mode: GL.TRIANGLES, attributes, indices};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export type WeldGeometryOptions = {
|
|
155
|
+
/** Position quantization step; zero means exact matching. */ positionGridSize?: number;
|
|
156
|
+
};
|
|
157
|
+
export type WeldGeometryResult = {
|
|
158
|
+
geometry: Geometry;
|
|
159
|
+
vertexMap: Uint32Array;
|
|
160
|
+
removedVertices: number;
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
/** Welds full attribute tuples, preserving UV seams and hard normal edges. */
|
|
164
|
+
export function weldGeometry(
|
|
165
|
+
geometry: Geometry,
|
|
166
|
+
options: WeldGeometryOptions = {}
|
|
167
|
+
): WeldGeometryResult {
|
|
168
|
+
const mesh = readMesh(geometry),
|
|
169
|
+
grid = options.positionGridSize ?? 0;
|
|
170
|
+
if (!Number.isFinite(grid) || grid < 0)
|
|
171
|
+
throw new RangeError('positionGridSize must be finite and nonnegative');
|
|
172
|
+
const names = Object.keys(mesh.attributes).sort(),
|
|
173
|
+
keys = new Map<string, number>(),
|
|
174
|
+
rows: number[] = [],
|
|
175
|
+
vertexMap = new Uint32Array(mesh.count);
|
|
176
|
+
for (let i = 0; i < mesh.count; i++) {
|
|
177
|
+
const tuple: number[] = [];
|
|
178
|
+
for (const name of names) {
|
|
179
|
+
const attribute = mesh.attributes[name];
|
|
180
|
+
for (let component = 0; component < attribute.size; component++) {
|
|
181
|
+
const value = attribute.value[i * attribute.size + component];
|
|
182
|
+
const quantized = name === mesh.position && grid ? Math.round(value / grid) : value;
|
|
183
|
+
if (!Number.isFinite(quantized)) throw new RangeError('Position quantization overflow');
|
|
184
|
+
tuple.push(quantized);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
const key = tuple.join(',');
|
|
188
|
+
let row = keys.get(key);
|
|
189
|
+
if (row === undefined) {
|
|
190
|
+
row = rows.length;
|
|
191
|
+
keys.set(key, row);
|
|
192
|
+
rows.push(i);
|
|
193
|
+
}
|
|
194
|
+
vertexMap[i] = row;
|
|
195
|
+
}
|
|
196
|
+
const attributes: Record<string, GeometryAttribute> = {};
|
|
197
|
+
for (const name of names) {
|
|
198
|
+
const attribute = mesh.attributes[name],
|
|
199
|
+
Constructor = attribute.value.constructor as TypedArrayConstructor;
|
|
200
|
+
const value = new Constructor(rows.length * attribute.size);
|
|
201
|
+
rows.forEach((row, i) =>
|
|
202
|
+
value.set(
|
|
203
|
+
attribute.value.subarray(row * attribute.size, (row + 1) * attribute.size),
|
|
204
|
+
i * attribute.size
|
|
205
|
+
)
|
|
206
|
+
);
|
|
207
|
+
attributes[name] = {size: attribute.size, value};
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
geometry: {
|
|
211
|
+
mode: GL.TRIANGLES,
|
|
212
|
+
attributes,
|
|
213
|
+
indices: Uint32Array.from(mesh.indices, index => vertexMap[index])
|
|
214
|
+
},
|
|
215
|
+
vertexMap,
|
|
216
|
+
removedVertices: mesh.count - rows.length
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Returns triangle numbers with area <= areaEpsilon, in original draw order. */
|
|
221
|
+
export function getDegenerateTriangles(geometry: Geometry, areaEpsilon = 0): Uint32Array {
|
|
222
|
+
if (!Number.isFinite(areaEpsilon) || areaEpsilon < 0)
|
|
223
|
+
throw new RangeError('Area epsilon must be finite and nonnegative');
|
|
224
|
+
const mesh = readMesh(geometry),
|
|
225
|
+
positions = mesh.attributes[mesh.position].value,
|
|
226
|
+
result: number[] = [];
|
|
227
|
+
for (let i = 0; i < mesh.indices.length; i += 3) {
|
|
228
|
+
const [a, b, c] = Array.from(mesh.indices.subarray(i, i + 3), index => index * 3);
|
|
229
|
+
const ab = [
|
|
230
|
+
positions[b] - positions[a],
|
|
231
|
+
positions[b + 1] - positions[a + 1],
|
|
232
|
+
positions[b + 2] - positions[a + 2]
|
|
233
|
+
];
|
|
234
|
+
const ac = [
|
|
235
|
+
positions[c] - positions[a],
|
|
236
|
+
positions[c + 1] - positions[a + 1],
|
|
237
|
+
positions[c + 2] - positions[a + 2]
|
|
238
|
+
];
|
|
239
|
+
// Keep the common path in floating point, but use exact dyadic arithmetic
|
|
240
|
+
// when normalization loses a component or cannot distinguish collinearity.
|
|
241
|
+
const scaleAB = Math.max(...ab.map(Math.abs));
|
|
242
|
+
const scaleAC = Math.max(...ac.map(Math.abs));
|
|
243
|
+
const normalizedAB = ab.map(v => (scaleAB ? v / scaleAB : 0));
|
|
244
|
+
const normalizedAC = ac.map(v => (scaleAC ? v / scaleAC : 0));
|
|
245
|
+
const crossLength = Math.hypot(...cross(normalizedAB, normalizedAC));
|
|
246
|
+
const lostComponent = [ab, ac].some((edge, i) =>
|
|
247
|
+
edge.some((v, axis) => v !== 0 && (i ? normalizedAC : normalizedAB)[axis] === 0)
|
|
248
|
+
);
|
|
249
|
+
if (!Number.isFinite(crossLength) || crossLength === 0 || lostComponent) {
|
|
250
|
+
if (exactAreaAtMost(positions, a, b, c, areaEpsilon)) result.push(i / 3);
|
|
251
|
+
} else {
|
|
252
|
+
const logArea = Math.log(crossLength) + Math.log(scaleAB) + Math.log(scaleAC) - Math.log(2);
|
|
253
|
+
if (areaEpsilon > 0 && logArea <= Math.log(areaEpsilon)) result.push(i / 3);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return Uint32Array.from(result);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function readMesh(geometry: Geometry): MeshData {
|
|
260
|
+
if (geometry.mode !== GL.TRIANGLES)
|
|
261
|
+
throw new RangeError('Packed triangle-list geometry is required');
|
|
262
|
+
const position = geometry.attributes['POSITION']
|
|
263
|
+
? 'POSITION'
|
|
264
|
+
: geometry.attributes['positions']
|
|
265
|
+
? 'positions'
|
|
266
|
+
: null;
|
|
267
|
+
if (!position) throw new RangeError('POSITION or positions is required');
|
|
268
|
+
const attributes: MeshData['attributes'] = {};
|
|
269
|
+
let count: number;
|
|
270
|
+
for (const [name, attribute] of Object.entries(geometry.attributes)) {
|
|
271
|
+
const value = getAttributeValues(attribute),
|
|
272
|
+
size = attribute.size ?? (name === position ? 3 : undefined);
|
|
273
|
+
if (!Number.isSafeInteger(size) || size < 1 || value.length % size)
|
|
274
|
+
throw new RangeError('Attributes need explicit packed sizes');
|
|
275
|
+
const length = value.length / size;
|
|
276
|
+
if (name === position) {
|
|
277
|
+
if (size !== 3) throw new RangeError('Positions must have size 3');
|
|
278
|
+
count = length;
|
|
279
|
+
}
|
|
280
|
+
for (const component of value)
|
|
281
|
+
if (!Number.isFinite(component)) throw new RangeError('Attributes must be finite');
|
|
282
|
+
attributes[name] = {value, size};
|
|
283
|
+
}
|
|
284
|
+
for (const attribute of Object.values(attributes))
|
|
285
|
+
if (attribute.value.length / attribute.size !== count)
|
|
286
|
+
throw new RangeError('Attribute vertex counts must match');
|
|
287
|
+
if (count > 0xffffffff) throw new RangeError('Geometry exceeds 32-bit indexing');
|
|
288
|
+
const input = geometry.indices
|
|
289
|
+
? getAttributeValues(geometry.indices)
|
|
290
|
+
: Uint32Array.from({length: count}, (_, i) => i);
|
|
291
|
+
if (input.length % 3) throw new RangeError('Indices must form triangles');
|
|
292
|
+
for (const index of input)
|
|
293
|
+
if (!Number.isSafeInteger(index) || index < 0 || index >= count)
|
|
294
|
+
throw new RangeError('Vertex index is out of bounds');
|
|
295
|
+
return {attributes, position, count, indices: Uint32Array.from(input)};
|
|
296
|
+
}
|
|
297
|
+
function copyAttributes(attributes: MeshData['attributes']): Record<string, GeometryAttribute> {
|
|
298
|
+
return Object.fromEntries(
|
|
299
|
+
Object.entries(attributes).map(([name, attribute]) => [
|
|
300
|
+
name,
|
|
301
|
+
{size: attribute.size, value: attribute.value.slice()}
|
|
302
|
+
])
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function cross(a: number[], b: number[]): number[] {
|
|
307
|
+
return [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** Direction-only multiplication, avoiding overflow in scale-weighted components. */
|
|
311
|
+
function normalizedLinearCombination(
|
|
312
|
+
columns: number[][],
|
|
313
|
+
vector: number[],
|
|
314
|
+
logScales: number[]
|
|
315
|
+
): number[] {
|
|
316
|
+
const logs = vector.map((v, i) => (v ? Math.log(Math.abs(v)) + logScales[i] : -Infinity));
|
|
317
|
+
const largest = Math.max(...logs);
|
|
318
|
+
if (largest === -Infinity) return [0, 0, 0];
|
|
319
|
+
const weights = logs.map((v, i) => Math.sign(vector[i]) * Math.exp(v - largest));
|
|
320
|
+
const result = [0, 1, 2].map(axis =>
|
|
321
|
+
columns.reduce((sum, column, i) => sum + column[axis] * weights[i], 0)
|
|
322
|
+
);
|
|
323
|
+
const length = Math.hypot(...result);
|
|
324
|
+
return result.map(v => (length ? v / length : 0));
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Every finite binary64 value is an integer times a power of two. The fallback
|
|
328
|
+
// compares squared cross lengths to 4*epsilon² without floating-point products.
|
|
329
|
+
type Dyadic = {n: bigint; exponent: number};
|
|
330
|
+
function exactAreaAtMost(
|
|
331
|
+
positions: TypedArray,
|
|
332
|
+
a: number,
|
|
333
|
+
b: number,
|
|
334
|
+
c: number,
|
|
335
|
+
epsilon: number
|
|
336
|
+
): boolean {
|
|
337
|
+
const edge = (end: number) =>
|
|
338
|
+
[0, 1, 2].map(axis =>
|
|
339
|
+
subtractDyadic(toDyadic(positions[end + axis]), toDyadic(positions[a + axis]))
|
|
340
|
+
);
|
|
341
|
+
const ab = edge(b),
|
|
342
|
+
ac = edge(c);
|
|
343
|
+
const components = [0, 1, 2].map(axis => {
|
|
344
|
+
const j = (axis + 1) % 3,
|
|
345
|
+
k = (axis + 2) % 3;
|
|
346
|
+
return subtractDyadic(multiplyDyadic(ab[j], ac[k]), multiplyDyadic(ab[k], ac[j]));
|
|
347
|
+
});
|
|
348
|
+
if (epsilon === 0) return components.every(v => v.n === 0n);
|
|
349
|
+
const squared = components.map(v => multiplyDyadic(v, v));
|
|
350
|
+
const sum = squared.reduce((total, v) => subtractDyadic(total, {...v, n: -v.n}), {
|
|
351
|
+
n: 0n,
|
|
352
|
+
exponent: 0
|
|
353
|
+
});
|
|
354
|
+
const threshold = multiplyDyadic(toDyadic(epsilon), toDyadic(epsilon));
|
|
355
|
+
threshold.exponent += 2;
|
|
356
|
+
return subtractDyadic(sum, threshold).n <= 0n;
|
|
357
|
+
}
|
|
358
|
+
function toDyadic(value: number): Dyadic {
|
|
359
|
+
const bits = new DataView(new ArrayBuffer(8));
|
|
360
|
+
bits.setFloat64(0, value);
|
|
361
|
+
const high = bits.getUint32(0),
|
|
362
|
+
low = bits.getUint32(4);
|
|
363
|
+
const exponentBits = (high >>> 20) & 0x7ff;
|
|
364
|
+
const fraction = (BigInt(high & 0xfffff) << 32n) | BigInt(low);
|
|
365
|
+
const n = exponentBits ? (1n << 52n) | fraction : fraction;
|
|
366
|
+
return {n: high >>> 31 ? -n : n, exponent: exponentBits ? exponentBits - 1075 : -1074};
|
|
367
|
+
}
|
|
368
|
+
function subtractDyadic(a: Dyadic, b: Dyadic): Dyadic {
|
|
369
|
+
const exponent = Math.min(a.exponent, b.exponent);
|
|
370
|
+
return {
|
|
371
|
+
n: (a.n << BigInt(a.exponent - exponent)) - (b.n << BigInt(b.exponent - exponent)),
|
|
372
|
+
exponent
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
function multiplyDyadic(a: Dyadic, b: Dyadic): Dyadic {
|
|
376
|
+
return {n: a.n * b.n, exponent: a.exponent + b.exponent};
|
|
377
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
import type {TypedArray} from '@math.gl/core';
|
|
6
|
+
import {GL} from './constants';
|
|
7
|
+
import type {Geometry} from './geometry';
|
|
8
|
+
import {getAttributeValues, getPositionAttribute, isGeometry} from './geometry';
|
|
9
|
+
|
|
10
|
+
/** One dereferenced point, line, or triangle primitive. */
|
|
11
|
+
export type Primitive = {
|
|
12
|
+
attributes: object;
|
|
13
|
+
type: number;
|
|
14
|
+
i1: number;
|
|
15
|
+
i2?: number;
|
|
16
|
+
i3?: number;
|
|
17
|
+
primitiveIndex: number;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Iterates through a geometry's primitives and dereferences optional indices.
|
|
22
|
+
*
|
|
23
|
+
* The legacy argument form `(indices, attributes, mode, start, end)` is also supported.
|
|
24
|
+
*/
|
|
25
|
+
export function* makePrimitiveIterator(
|
|
26
|
+
geometryOrIndices?: Geometry | TypedArray | {value?: TypedArray; values?: TypedArray},
|
|
27
|
+
attributes: object = {},
|
|
28
|
+
mode?: number,
|
|
29
|
+
start = 0,
|
|
30
|
+
end?: number
|
|
31
|
+
): Iterable<Primitive> {
|
|
32
|
+
let indices: TypedArray | undefined;
|
|
33
|
+
if (isGeometry(geometryOrIndices)) {
|
|
34
|
+
const geometry = geometryOrIndices;
|
|
35
|
+
indices = geometry.indices ? getAttributeValues(geometry.indices) : undefined;
|
|
36
|
+
attributes = geometry.attributes;
|
|
37
|
+
mode = geometry.mode;
|
|
38
|
+
if (end === undefined) {
|
|
39
|
+
const position = getPositionAttribute(geometry);
|
|
40
|
+
end = indices?.length ?? getAttributeValues(position).length / (position.size || 3);
|
|
41
|
+
}
|
|
42
|
+
} else if (geometryOrIndices) {
|
|
43
|
+
indices = getAttributeValues(geometryOrIndices);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (mode === undefined) {
|
|
47
|
+
throw new Error('Primitive mode is required');
|
|
48
|
+
}
|
|
49
|
+
end ??= indices?.length ?? start;
|
|
50
|
+
|
|
51
|
+
const dereference = (index: number): number => Number(indices ? indices[index] : index);
|
|
52
|
+
let primitiveIndex = 0;
|
|
53
|
+
|
|
54
|
+
switch (mode) {
|
|
55
|
+
case GL.POINTS:
|
|
56
|
+
for (let index = start; index < end; index++) {
|
|
57
|
+
yield {
|
|
58
|
+
attributes,
|
|
59
|
+
type: GL.POINTS,
|
|
60
|
+
i1: dereference(index),
|
|
61
|
+
primitiveIndex: primitiveIndex++
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return;
|
|
65
|
+
|
|
66
|
+
case GL.LINES:
|
|
67
|
+
for (let index = start; index + 1 < end; index += 2) {
|
|
68
|
+
yield {
|
|
69
|
+
attributes,
|
|
70
|
+
type: GL.LINES,
|
|
71
|
+
i1: dereference(index),
|
|
72
|
+
i2: dereference(index + 1),
|
|
73
|
+
primitiveIndex: primitiveIndex++
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
return;
|
|
77
|
+
|
|
78
|
+
case GL.LINE_STRIP:
|
|
79
|
+
for (let index = start; index + 1 < end; index++) {
|
|
80
|
+
yield {
|
|
81
|
+
attributes,
|
|
82
|
+
type: GL.LINES,
|
|
83
|
+
i1: dereference(index),
|
|
84
|
+
i2: dereference(index + 1),
|
|
85
|
+
primitiveIndex: primitiveIndex++
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return;
|
|
89
|
+
|
|
90
|
+
case GL.LINE_LOOP:
|
|
91
|
+
for (let index = start; index < end; index++) {
|
|
92
|
+
yield {
|
|
93
|
+
attributes,
|
|
94
|
+
type: GL.LINES,
|
|
95
|
+
i1: dereference(index),
|
|
96
|
+
i2: dereference(index + 1 < end ? index + 1 : start),
|
|
97
|
+
primitiveIndex: primitiveIndex++
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
return;
|
|
101
|
+
|
|
102
|
+
case GL.TRIANGLES:
|
|
103
|
+
for (let index = start; index + 2 < end; index += 3) {
|
|
104
|
+
yield {
|
|
105
|
+
attributes,
|
|
106
|
+
type: GL.TRIANGLES,
|
|
107
|
+
i1: dereference(index),
|
|
108
|
+
i2: dereference(index + 1),
|
|
109
|
+
i3: dereference(index + 2),
|
|
110
|
+
primitiveIndex: primitiveIndex++
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
return;
|
|
114
|
+
|
|
115
|
+
case GL.TRIANGLE_STRIP:
|
|
116
|
+
for (let index = start; index + 2 < end; index++) {
|
|
117
|
+
const odd = (index - start) % 2 === 1;
|
|
118
|
+
yield {
|
|
119
|
+
attributes,
|
|
120
|
+
type: GL.TRIANGLES,
|
|
121
|
+
i1: dereference(odd ? index + 1 : index),
|
|
122
|
+
i2: dereference(odd ? index : index + 1),
|
|
123
|
+
i3: dereference(index + 2),
|
|
124
|
+
primitiveIndex: primitiveIndex++
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return;
|
|
128
|
+
|
|
129
|
+
case GL.TRIANGLE_FAN:
|
|
130
|
+
for (let index = start + 1; index + 1 < end; index++) {
|
|
131
|
+
yield {
|
|
132
|
+
attributes,
|
|
133
|
+
type: GL.TRIANGLES,
|
|
134
|
+
i1: dereference(start),
|
|
135
|
+
i2: dereference(index),
|
|
136
|
+
i3: dereference(index + 1),
|
|
137
|
+
primitiveIndex: primitiveIndex++
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
return;
|
|
141
|
+
|
|
142
|
+
default:
|
|
143
|
+
throw new Error(`Unknown primitive mode ${mode}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Decodes a packed RGB565 value into 8-bit RGB components. */
|
|
6
|
+
export function decodeRGB565(rgb565: number, target: number[] = [0, 0, 0]): number[] {
|
|
7
|
+
const red = (rgb565 >> 11) & 31;
|
|
8
|
+
const green = (rgb565 >> 5) & 63;
|
|
9
|
+
const blue = rgb565 & 31;
|
|
10
|
+
target[0] = (red << 3) | (red >> 2);
|
|
11
|
+
target[1] = (green << 2) | (green >> 4);
|
|
12
|
+
target[2] = (blue << 3) | (blue >> 2);
|
|
13
|
+
return target;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Encodes 8-bit RGB components into a packed RGB565 value. */
|
|
17
|
+
export function encodeRGB565(rgb: ArrayLike<number>): number {
|
|
18
|
+
const red = Math.round(clampByte(rgb[0] ?? 0) * (31 / 255));
|
|
19
|
+
const green = Math.round(clampByte(rgb[1] ?? 0) * (63 / 255));
|
|
20
|
+
const blue = Math.round(clampByte(rgb[2] ?? 0) * (31 / 255));
|
|
21
|
+
return (red << 11) | (green << 5) | blue;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function clampByte(value: number): number {
|
|
25
|
+
return Math.max(0, Math.min(255, value));
|
|
26
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// math.gl
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
// SPDX-FileCopyrightText: Copyright (c) vis.gl contributors
|
|
4
|
+
|
|
5
|
+
/** Concatenates the visible bytes of multiple typed-array or DataView instances. */
|
|
6
|
+
export function concatTypedArrays(arrays: readonly ArrayBufferView[] = []): Uint8Array {
|
|
7
|
+
const byteLength = arrays.reduce((length, array) => length + array.byteLength, 0);
|
|
8
|
+
const result = new Uint8Array(byteLength);
|
|
9
|
+
let byteOffset = 0;
|
|
10
|
+
for (const array of arrays) {
|
|
11
|
+
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
12
|
+
result.set(bytes, byteOffset);
|
|
13
|
+
byteOffset += bytes.byteLength;
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|