awv3
Version:
⚡ AWV3 embedded CAD
82 lines (72 loc) • 3.01 kB
JavaScript
import * as THREE from 'three';
/**
* @class Region contains vertices from ClassCAD, which are not visible, but
* can be selected through raycasting and then be created with the given
* information {id, point, meta}.
*/
export default class Region extends THREE.Object3D {
constructor() {
super();
this.type = 'Region';
this.boundingSphere = null;
this.points = []; // structure: { id, point, meta }
this.radiussq = 0;
}
/**
* This code is mostly copied form THREE.Points.js and adjusted to the fact
* that Region does not have geometry or graphical representation
*/
raycast(raycaster, intersects) {
var inverseMatrix = new THREE.Matrix4();
var ray = new THREE.Ray();
var sphere = new THREE.Sphere();
var object = this;
var matrixWorld = this.matrixWorld;
var threshold = raycaster.params.Points.threshold;
// Checking boundingSphere distance to ray
if (this.boundingSphere === null) this.computeBoundingSphere();
sphere.copy(this.boundingSphere);
// sphere.applyMatrix4(matrixWorld); //TODO
if (raycaster.ray.intersectsSphere(sphere) === false) return;
// inverseMatrix.getInverse(matrixWorld);//TODO
ray.copy(raycaster.ray); //.applyMatrix4(inverseMatrix);//TODO
var localThreshold = threshold / ((this.scale.x + this.scale.y + this.scale.z) / 3);
var localThresholdSq = localThreshold * localThreshold;
//test each point
for (let i = 0; i < this.points.length; i++) {
let entry = this.points[i];
if (entry.point) {
let point = entry.point;
var rayPointDistanceSq = ray.distanceSqToPoint(point);
if (rayPointDistanceSq < localThresholdSq + this.radiussq) {
var intersectPoint = ray.closestPointToPoint(point);
// intersectPoint.applyMatrix4(matrixWorld);//TODO
var distance = raycaster.ray.origin.distanceTo(intersectPoint);
if (distance < raycaster.near || distance > raycaster.far) return;
intersects.push({
distance: distance,
distanceToRay: Math.sqrt(rayPointDistanceSq),
point: intersectPoint.clone(),
index: i,
face: null,
object: object,
ref: entry
});
}
}
}
}
/**
* Computes the bounding sphere of the points (in model coordinates).
*/
computeBoundingSphere() {
if (this.boundingSphere === null) {
this.boundingSphere = new THREE.Sphere();
}
let positions = [];
this.points.forEach(entry => {
positions.push(entry.meta.position);
});
this.boundingSphere.setFromPoints(positions);
}
}