Check if point is inside custom mesh geometry

What would be the easiest way to check if a point is inside user (irregular) mesh geometry?

+5
source share
4 answers

This is the task of computational geometry. You can see the search for points within the geometry . Since your geometry is irregular, the problem is much more complicated.

But if accuracy is not too important, you can check if the point is inside the bounding box of the geometry.

+2
source

If your grid is close-up. You can use the THREE.js built-in beam-caster. Code example as

const point = new THREE.Vector3(2,2,2) // Your point const geometry = new THREE.BoxBufferGeometry( 5, 5, 5 ) const material = new THREE.MeshBasicMaterial( { color: 0xffff00 } ) const mesh = new THREE.Mesh( geometry, material ) const raycaster = new THREE.Raycaster() raycaster.set(point, new THREE.Vector3(1,1,1)) const intersects = raycaster.intersectObject(mesh) if( intersects.length %2 === 1) { // Points is in objet console.log('Point is in object') } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/89/three.js"></script> 
+5
source

Just raycast once from a point in any direction, then check the num intersections, if odd, the point is in geometry, here is a demo

+2
source

It’s better to check this using the point product of the direction of the beam and the normal face

tested on three.js (r103)

 const point = new THREE.Vector3(2, 2, 2) // Your point const direction = new THREE.Vector3(1, 1, 1); const geometry = new THREE.BoxGeometry(5, 5, 5) const material = new THREE.MeshBasicMaterial({ color: 0xffff00, side: THREE.DoubleSide }); const mesh = new THREE.Mesh(geometry, material) const raycaster = new THREE.Raycaster() raycaster.set(point, direction) const intersects = raycaster.intersectObject(mesh); if (intersects.length && direction.dot(intersects[0].face.normal) > 0) { console.log('Point is in object'); } else { console.log('Point is out of object'); } 

In rare cases, you can get an even number of interactions with a point located inside the grid

(try point = new THREE.Vector3(0, 0, 0) , which should give 4 intersections)

-1
source

Source: https://habr.com/ru/post/1479001/


All Articles