-
-
Notifications
You must be signed in to change notification settings - Fork 267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve ClosestPointToPoint
#699
Draft
agargaro
wants to merge
9
commits into
gkjohnson:master
Choose a base branch
from
agargaro:closestPointToPoint
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b4ad02f
closestPointToPoint
agargaro dd2cec2
removed comment
agargaro 3c5b46a
example
agargaro a8e0ffb
Merge branch 'master' into closestPointToPoint
agargaro 781dede
test edited
agargaro 06e5cc5
Added sort
agargaro c75e9b2
add Hybrid
agargaro 1a7fbcc
Add example config sortedListMaxCount
agargaro 14a7c32
Improvements
agargaro File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>three-mesh-bvh - Complex Geometry Raycasting</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> | ||
|
||
<style type="text/css"> | ||
html, body { | ||
padding: 0; | ||
margin: 0; | ||
overflow: hidden; | ||
} | ||
|
||
canvas { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<script type="module" src="./testToRemove.js"></script> | ||
</body> | ||
</html> |
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,133 @@ | ||
import * as THREE from 'three'; | ||
import { computeBoundsTree, SAH } from '../src'; | ||
|
||
THREE.BufferGeometry.prototype.computeBoundsTree = computeBoundsTree; | ||
|
||
const spawnPointRadius = 100; | ||
const radius = 10; // if radius 100 and tube 0.1 and spawnRadius 100, sort works really good. | ||
const tube = 0.1; | ||
const segmentsMultiplier = 32; | ||
const sortedListMaxCount = 32; | ||
const maxLeafTris = 10; | ||
const strategy = SAH; | ||
|
||
const tries = 1000; | ||
const seed = 20000; | ||
|
||
// const geometry = new THREE.SphereGeometry( radius, 8 * segmentsMultiplier, 4 * segmentsMultiplier ); | ||
const geometry = new THREE.TorusKnotGeometry( radius, tube, 64 * segmentsMultiplier, 8 * segmentsMultiplier ); | ||
|
||
geometry.computeBoundsTree( { maxLeafTris, strategy } ); | ||
|
||
export class PRNG { | ||
|
||
constructor( seed ) { | ||
|
||
this._seed = seed; | ||
|
||
} | ||
|
||
next() { | ||
|
||
let t = ( this._seed += 0x6d2b79f5 ); | ||
t = Math.imul( t ^ ( t >>> 15 ), t | 1 ); | ||
t ^= t + Math.imul( t ^ ( t >>> 7 ), t | 61 ); | ||
return ( ( t ^ ( t >>> 14 ) ) >>> 0 ) / 4294967296; | ||
|
||
} | ||
|
||
range( min, max ) { | ||
|
||
return min + ( max - min ) * this.next(); | ||
|
||
} | ||
|
||
} | ||
|
||
const bvh = geometry.boundsTree; | ||
const target = {}; | ||
|
||
const r = new PRNG( seed ); | ||
const points = new Array( tries ); | ||
|
||
function generatePoints() { | ||
|
||
for ( let i = 0; i < tries; i ++ ) { | ||
|
||
points[ i ] = new THREE.Vector3( r.range( - spawnPointRadius, spawnPointRadius ), r.range( - spawnPointRadius, spawnPointRadius ), r.range( - spawnPointRadius, spawnPointRadius ) ); | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
// // TEST EQUALS RESULTS | ||
|
||
// generatePoints(); | ||
// const target2 = {}; | ||
// for ( let i = 0; i < tries; i ++ ) { | ||
|
||
// bvh.closestPointToPoint( points[ i ], target ); | ||
// bvh.closestPointToPointHybrid( points[ i ], target2 ); | ||
|
||
// if ( target.distance !== target2.distance ) { | ||
|
||
// const diff = target.distance - target2.distance; | ||
// console.error( "error: " + ( diff / target2.distance * 100 ) + "%" ); | ||
|
||
// } | ||
|
||
// } | ||
|
||
// TEST PERFORMANCE | ||
|
||
function benchmark() { | ||
|
||
generatePoints(); | ||
|
||
const startOld = performance.now(); | ||
|
||
for ( let i = 0; i < tries; i ++ ) { | ||
|
||
bvh.closestPointToPointOld( points[ i ], target ); | ||
|
||
} | ||
|
||
const endOld = performance.now() - startOld; | ||
const startNew = performance.now(); | ||
|
||
for ( let i = 0; i < tries; i ++ ) { | ||
|
||
bvh.closestPointToPoint( points[ i ], target ); | ||
|
||
} | ||
|
||
const endNew = performance.now() - startNew; | ||
const startSort = performance.now(); | ||
|
||
for ( let i = 0; i < tries; i ++ ) { | ||
|
||
bvh.closestPointToPointSort( points[ i ], target ); | ||
|
||
} | ||
|
||
const endSort = performance.now() - startSort; | ||
const startHybrid = performance.now(); | ||
|
||
for ( let i = 0; i < tries; i ++ ) { | ||
|
||
bvh.closestPointToPointHybrid( points[ i ], target, sortedListMaxCount ); | ||
|
||
} | ||
|
||
const endHybrid = performance.now() - startHybrid; | ||
|
||
const bestEnd = Math.min( endSort, endNew, endHybrid ); | ||
const best = bestEnd === endSort ? "Sorted" : ( bestEnd === endNew ? "New" : "Hybrid" ); | ||
|
||
console.log( `New: ${endNew.toFixed( 1 )}ms / Sorted: ${endSort.toFixed( 1 )}ms / Hybrid: ${endHybrid.toFixed( 1 )}ms / Old: ${endOld.toFixed( 1 )}ms / Diff: ${( ( 1 - ( endOld / bestEnd ) ) * 100 ).toFixed( 2 )} % / Best: ${best}` ); | ||
|
||
} | ||
|
||
benchmark(); | ||
setInterval( () => benchmark(), 1000 ); |
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,117 @@ | ||
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'; | ||
|
||
const temp = /* @__PURE__ */ new Vector3(); | ||
const temp1 = /* @__PURE__ */ new Vector3(); | ||
|
||
export function closestPointToPoint/* @echo INDIRECT_STRING */( | ||
bvh, | ||
root, | ||
point, | ||
target, | ||
minThreshold, | ||
maxThreshold | ||
) { | ||
|
||
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(); | ||
|
||
BufferStack.setBuffer( bvh._roots[ root ] ); | ||
const { float32Array, uint16Array, uint32Array } = BufferStack; | ||
_closestPointToPoint( root ); | ||
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; | ||
|
||
|
||
// early out if under minThreshold | ||
// skip checking if over maxThreshold | ||
// set minThreshold = maxThreshold to quickly check if a point is within a threshold | ||
// returns Infinity if no value found | ||
function _closestPointToPoint( nodeIndex32 ) { | ||
|
||
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; | ||
|
||
} | ||
|
||
} | ||
|
||
return; | ||
|
||
} | ||
|
||
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 <= rightDistance ) { | ||
|
||
if ( leftDistance < closestDistanceSq && leftDistance < maxThresholdSq ) { | ||
|
||
if ( _closestPointToPoint( leftIndex ) ) return true; | ||
if ( rightDistance < closestDistanceSq ) return _closestPointToPoint( rightIndex ); | ||
|
||
} | ||
|
||
} else if ( rightDistance < closestDistanceSq && rightDistance < maxThresholdSq ) { | ||
|
||
if ( _closestPointToPoint( rightIndex ) ) return true; | ||
if ( leftDistance < closestDistanceSq ) return _closestPointToPoint( leftIndex ); | ||
|
||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we want to check distance to
maxThresholdSq
here, as well?