Skip to content

Commit

Permalink
Add functions
Browse files Browse the repository at this point in the history
  • Loading branch information
gkjohnson committed Aug 21, 2023
1 parent 79797dd commit da90d5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/core/build/computeBoundsUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FLOAT32_EPSILON } from '../Constants.js';
import { makeEmptyBounds } from '../../utils/ArrayBoxUtilities.js';
import { getTriCount } from './geometryUtils.js';

// computes the union of the bounds of all of the given triangles and puts the resulting box in target. If
// centroidTarget is provided then a bounding box is computed for the centroids of the triangles, as well.
Expand Down Expand Up @@ -122,7 +123,7 @@ export function computeTriangleBounds( geo, fullBounds ) {

const posAttr = geo.attributes.position;
const index = geo.index.array;
const triCount = index.length / 3;
const triCount = getTriCount( geo );
const triangleBounds = new Float32Array( triCount * 6 );
const normalized = posAttr.normalized;

Expand Down
15 changes: 14 additions & 1 deletion src/core/build/geometryUtils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { BufferAttribute } from 'three';

export function getTriCount( geo ) {

return ( geo.index ? geo.index.count : geo.attributes.position.count ) / 3;

}

// ensures that an index is present on the geometry
export function ensureIndex( geo, options ) {

Expand Down Expand Up @@ -41,11 +47,18 @@ export function ensureIndex( geo, options ) {
// g1 = [16, 40] g2 = [41, 60]
//
// we would need four BVH roots: [0, 15], [16, 20], [21, 40], [41, 60].
export function getFullGeometryRange( geo ) {

const triCount = getTriCount( geo );
return [ { offset: 0, count: triCount } ];

}

export function getRootIndexRanges( geo ) {

if ( ! geo.groups || ! geo.groups.length ) {

return [ { offset: 0, count: geo.index.count / 3 } ];
return getFullGeometryRange( geo );

}

Expand Down
3 changes: 2 additions & 1 deletion src/core/cast/closestPointToGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Vector3, Matrix4 } from 'three';
import { OrientedBox } from '../../math/OrientedBox.js';
import { setTriangle } from '../../utils/TriangleUtilities.js';
import { ExtendedTrianglePool } from '../../utils/ExtendedTrianglePool.js';
import { getTriCount } from '../build/geometryUtils.js';

const tempMatrix = /* @__PURE__ */ new Matrix4();
const obb = /* @__PURE__ */ new OrientedBox();
Expand Down Expand Up @@ -154,7 +155,7 @@ export function closestPointToGeometry(
} else {

// If no bounds tree then we'll just check every triangle.
const triCount = otherIndex ? otherIndex.count : otherPos.count;
const triCount = getTriCount( otherGeometry );
for ( let i2 = 0, l2 = triCount; i2 < l2; i2 += 3 ) {

setTriangle( triangle2, i2, otherIndex, otherPos );
Expand Down

0 comments on commit da90d5d

Please sign in to comment.