UNPKG

ami-cjs.js

Version:

<p align="center"> <img src="https://cloud.githubusercontent.com/assets/214063/23213764/78ade038-f90c-11e6-8208-4fcade5f3832.png" width="60%"> </p>

221 lines (180 loc) 8.1 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _core = require('../core/core.intersections'); var _core2 = _interopRequireDefault(_core); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Imports ***/ /** * * It is typically used for creating an irregular 3D planar shape given a box and the cut-plane. * * Demo: {@link https://fnndsc.github.io/vjs#geometry_slice} * * @module geometries/slice * * @param {THREE.Vector3} halfDimensions - Half-dimensions of the box to be sliced. * @param {THREE.Vector3} center - Center of the box to be sliced. * @param {THREE.Vector3<THREE.Vector3>} orientation - Orientation of the box to be sliced. (might not be necessary..?) * @param {THREE.Vector3} position - Position of the cutting plane. * @param {THREE.Vector3} direction - Cross direction of the cutting plane. * * @example * // Define box to be sliced * let halfDimensions = new THREE.Vector(123, 45, 67); * let center = new THREE.Vector3(0, 0, 0); * let orientation = new THREE.Vector3( * new THREE.Vector3(1, 0, 0), * new THREE.Vector3(0, 1, 0), * new THREE.Vector3(0, 0, 1) * ); * * // Define slice plane * let position = center.clone(); * let direction = new THREE.Vector3(-0.2, 0.5, 0.3); * * // Create the slice geometry & materials * let sliceGeometry = new VJS.geometries.slice(halfDimensions, center, orientation, position, direction); * let sliceMaterial = new THREE.MeshBasicMaterial({ * 'side': THREE.DoubleSide, * 'color': 0xFF5722 * }); * * // Create mesh and add it to the scene * let slice = new THREE.Mesh(sliceGeometry, sliceMaterial); * scene.add(slice); */ var GeometriesSlice = function (_THREE$ShapeGeometry) { _inherits(GeometriesSlice, _THREE$ShapeGeometry); function GeometriesSlice(halfDimensions, center, position, direction) { var toAABB = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : new THREE.Matrix4(); _classCallCheck(this, GeometriesSlice); // // prepare data for the shape! // var aabb = { halfDimensions: halfDimensions, center: center, toAABB: toAABB }; var plane = { position: position, direction: direction }; // BOOM! var intersections = _core2.default.aabbPlane(aabb, plane); // can not exist before calling the constructor if (intersections.length < 3) { window.console.log('WARNING: Less than 3 intersections between AABB and Plane.'); window.console.log('AABB'); window.console.log(aabb); window.console.log('Plane'); window.console.log(plane); window.console.log('exiting...'); // or throw error? throw 'geometries.slice has less than 3 intersections, can not create a valid geometry.'; } var orderedIntersections = GeometriesSlice.orderIntersections(intersections, direction); var sliceShape = GeometriesSlice.shape(orderedIntersections); // // Generate Geometry from shape // It does triangulation for us! // var _this = _possibleConstructorReturn(this, (GeometriesSlice.__proto__ || Object.getPrototypeOf(GeometriesSlice)).call(this, sliceShape)); _this.type = 'SliceGeometry'; // update real position of each vertex! (not in 2d) _this.vertices = orderedIntersections; _this.verticesNeedUpdate = true; return _this; } _createClass(GeometriesSlice, null, [{ key: 'shape', value: function shape(points) { // // Create Shape // var shape = new THREE.Shape(); // move to first point! shape.moveTo(points[0].xy.x, points[0].xy.y); // loop through all points! for (var l = 1; l < points.length; l++) { // project each on plane! shape.lineTo(points[l].xy.x, points[l].xy.y); } // close the shape! shape.lineTo(points[0].xy.x, points[0].xy.y); return shape; } /** * * Convenience function to extract center of mass from list of points. * * @private * * @param {Array<THREE.Vector3>} points - Set of points from which we want to extract the center of mass. * * @returns {THREE.Vector3} Center of mass from given points. */ }, { key: 'centerOfMass', value: function centerOfMass(points) { var centerOfMass = new THREE.Vector3(0, 0, 0); for (var i = 0; i < points.length; i++) { centerOfMass.x += points[i].x; centerOfMass.y += points[i].y; centerOfMass.z += points[i].z; } centerOfMass.divideScalar(points.length); return centerOfMass; } /** * * Order 3D planar points around a refence point. * * @private * * @param {Array<THREE.Vector3>} points - Set of planar 3D points to be ordered. * @param {THREE.Vector3} direction - Direction of the plane in which points and reference are sitting. * * @returns {Array<Object>} Set of object representing the ordered points. */ }, { key: 'orderIntersections', value: function orderIntersections(points, direction) { var reference = GeometriesSlice.centerOfMass(points); // direction from first point to reference var referenceDirection = new THREE.Vector3(points[0].x - reference.x, points[0].y - reference.y, points[0].z - reference.z).normalize(); var base = new THREE.Vector3(0, 0, 0).crossVectors(referenceDirection, direction).normalize(); var orderedpoints = []; // other lines // if inter, return location + angle for (var j = 0; j < points.length; j++) { var point = new THREE.Vector3(points[j].x, points[j].y, points[j].z); point.direction = new THREE.Vector3(points[j].x - reference.x, points[j].y - reference.y, points[j].z - reference.z).normalize(); var x = referenceDirection.dot(point.direction); var y = base.dot(point.direction); point.xy = { x: x, y: y }; var theta = Math.atan2(y, x) * (180 / Math.PI); point.angle = theta; orderedpoints.push(point); } orderedpoints.sort(function (a, b) { return a.angle - b.angle; }); var noDups = [orderedpoints[0]]; var epsilon = 0.0001; for (var i = 1; i < orderedpoints.length; i++) { if (Math.abs(orderedpoints[i - 1].angle - orderedpoints[i].angle) > epsilon) { noDups.push(orderedpoints[i]); } } return noDups; } }]); return GeometriesSlice; }(THREE.ShapeGeometry); exports.default = GeometriesSlice; module.exports = exports['default'];