awv3
Version:
⚡ AWV3 embedded CAD
130 lines (99 loc) • 4.91 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
var _inherits2 = require('babel-runtime/helpers/inherits');
var _inherits3 = _interopRequireDefault(_inherits2);
var _three = require('three');
var THREE = _interopRequireWildcard(_three);
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* @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}.
*/
var Region = function (_THREE$Object3D) {
(0, _inherits3.default)(Region, _THREE$Object3D);
function Region() {
(0, _classCallCheck3.default)(this, Region);
var _this = (0, _possibleConstructorReturn3.default)(this, (Region.__proto__ || (0, _getPrototypeOf2.default)(Region)).call(this));
_this.type = 'Region';
_this.boundingSphere = null;
_this.points = []; // structure: { id, point, meta }
_this.radiussq = 0;
return _this;
}
/**
* This code is mostly copied form THREE.Points.js and adjusted to the fact
* that Region does not have geometry or graphical representation
*/
(0, _createClass3.default)(Region, [{
key: 'raycast',
value: function 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 (var i = 0; i < this.points.length; i++) {
var entry = this.points[i];
if (entry.point) {
var 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).
*/
}, {
key: 'computeBoundingSphere',
value: function computeBoundingSphere() {
if (this.boundingSphere === null) {
this.boundingSphere = new THREE.Sphere();
}
var positions = [];
this.points.forEach(function (entry) {
positions.push(entry.meta.position);
});
this.boundingSphere.setFromPoints(positions);
}
}]);
return Region;
}(THREE.Object3D);
exports.default = Region;