-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
259 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { Vector3 } from 'three'; | ||
import { COUNT, OFFSET, LEFT_NODE, RIGHT_NODE, IS_LEAF } from '../utils/nodeBufferUtils.js'; | ||
import { BufferStack } from '../utils/BufferStack.js'; | ||
import { ExtendedTrianglePool } from '../../utils/ExtendedTrianglePool.js'; | ||
import { setTriangle } from '../../utils/TriangleUtilities.js'; | ||
import { closestDistanceSquaredPointToBox } from '../utils/distanceUtils.js'; | ||
import { SortedListDesc } from '../utils/SortedListDesc.js'; | ||
|
||
const temp = /* @__PURE__ */ new Vector3(); | ||
const temp1 = /* @__PURE__ */ new Vector3(); | ||
const sortedList = new SortedListDesc(); | ||
|
||
export function closestPointToPointSort/* @echo INDIRECT_STRING */( | ||
bvh, | ||
root, | ||
point, | ||
target = { }, | ||
minThreshold = 0, | ||
maxThreshold = Infinity | ||
) { | ||
|
||
const minThresholdSq = minThreshold * minThreshold; | ||
const maxThresholdSq = maxThreshold * maxThreshold; | ||
let closestDistanceSq = Infinity; | ||
let closestDistanceTriIndex = null; | ||
|
||
const { geometry } = bvh; | ||
const { index } = geometry; | ||
const pos = geometry.attributes.position; | ||
const triangle = ExtendedTrianglePool.getPrimitive(); | ||
|
||
sortedList.clear(); | ||
BufferStack.setBuffer( bvh._roots[ root ] ); | ||
const { float32Array, uint16Array, uint32Array } = BufferStack; | ||
|
||
let node = { nodeIndex32: 0, distance: closestDistanceSquaredPointToBox( 0, float32Array, point ) }; | ||
|
||
do { | ||
|
||
const { distance } = node; | ||
|
||
if ( distance >= closestDistanceSq ) return; | ||
|
||
const { nodeIndex32 } = node; | ||
|
||
const nodeIndex16 = nodeIndex32 * 2; | ||
const isLeaf = IS_LEAF( nodeIndex16, uint16Array ); | ||
if ( isLeaf ) { | ||
|
||
const offset = OFFSET( nodeIndex32, uint32Array ); | ||
const count = COUNT( nodeIndex16, uint16Array ); | ||
|
||
for ( let i = offset, l = count + offset; i < l; i ++ ) { | ||
|
||
/* @if INDIRECT */ | ||
|
||
const ti = bvh.resolveTriangleIndex( i ); | ||
setTriangle( triangle, 3 * ti, index, pos ); | ||
|
||
/* @else */ | ||
|
||
setTriangle( triangle, i * 3, index, pos ); | ||
|
||
/* @endif */ | ||
|
||
triangle.needsUpdate = true; | ||
|
||
triangle.closestPointToPoint( point, temp ); | ||
const distSq = point.distanceToSquared( temp ); | ||
if ( distSq < closestDistanceSq ) { | ||
|
||
temp1.copy( temp ); | ||
closestDistanceSq = distSq; | ||
closestDistanceTriIndex = i; | ||
|
||
if ( distSq < minThresholdSq ) return true; | ||
|
||
} | ||
|
||
} | ||
|
||
continue; | ||
|
||
} | ||
|
||
const leftIndex = LEFT_NODE( nodeIndex32 ); | ||
const rightIndex = RIGHT_NODE( nodeIndex32, uint32Array ); | ||
|
||
const leftDistance = closestDistanceSquaredPointToBox( leftIndex, float32Array, point ); | ||
const rightDistance = closestDistanceSquaredPointToBox( rightIndex, float32Array, point ); | ||
|
||
if ( leftDistance < closestDistanceSq && leftDistance < maxThresholdSq ) { | ||
|
||
sortedList.push( { nodeIndex32: leftIndex, distance: leftDistance } ); | ||
|
||
} | ||
|
||
if ( rightDistance < closestDistanceSq && rightDistance < maxThresholdSq ) { | ||
|
||
sortedList.push( { nodeIndex32: rightIndex, distance: rightDistance } ); | ||
|
||
} | ||
|
||
} while ( node = sortedList.pop() ); | ||
|
||
BufferStack.clearBuffer(); | ||
|
||
if ( closestDistanceSq === Infinity ) return null; | ||
|
||
const closestDistance = Math.sqrt( closestDistanceSq ); | ||
|
||
if ( ! target.point ) target.point = temp1.clone(); | ||
else target.point.copy( temp1 ); | ||
target.distance = closestDistance; | ||
target.faceIndex = closestDistanceTriIndex; | ||
|
||
return target; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export class SortedListDesc { | ||
|
||
constructor() { | ||
|
||
this.array = []; | ||
|
||
} | ||
|
||
clear() { | ||
|
||
this.array = []; | ||
|
||
} | ||
|
||
|
||
push( node ) { | ||
|
||
const index = this.binarySearch( node.distance ); | ||
this.array.splice( index, 0, node ); | ||
|
||
} | ||
|
||
pop() { | ||
|
||
return this.array.pop(); | ||
|
||
} | ||
|
||
binarySearch( value ) { | ||
|
||
const array = this.array; | ||
|
||
let start = 0; | ||
let end = array.length - 1; | ||
let index = 0; | ||
|
||
while ( start <= end ) { | ||
|
||
index = Math.ceil( ( start + end ) / 2 ); | ||
|
||
if ( index === 0 ) break; | ||
if ( array[ index ].distance <= value && array[ index - 1 ].distance >= value ) return index; | ||
|
||
if ( value > array[ index ].distance ) end = index - 1; | ||
else start = index + 1; | ||
|
||
} | ||
|
||
return value < array[ index ]?.distance ? index + 1 : index; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export function closestDistanceSquaredPointToBox( nodeIndex32, array, point ) { | ||
|
||
const xMin = array[ nodeIndex32 + 0 ] - point.x; | ||
const xMax = point.x - array[ nodeIndex32 + 3 ]; | ||
let dx = xMin > xMax ? xMin : xMax; | ||
dx = dx > 0 ? dx : 0; | ||
|
||
const yMin = array[ nodeIndex32 + 1 ] - point.y; | ||
const yMax = point.y - array[ nodeIndex32 + 4 ]; | ||
let dy = yMin > yMax ? yMin : yMax; | ||
dy = dy > 0 ? dy : 0; | ||
|
||
const zMin = array[ nodeIndex32 + 2 ] - point.z; | ||
const zMax = point.z - array[ nodeIndex32 + 5 ]; | ||
let dz = zMin > zMax ? zMin : zMax; | ||
dz = dz > 0 ? dz : 0; | ||
|
||
return dx * dx + dy * dy + dz * dz; | ||
|
||
} |