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>
330 lines (279 loc) • 11 kB
JavaScript
'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 _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
var _widgets = require('../widgets/widgets.base');
var _widgets2 = _interopRequireDefault(_widgets);
var _helpers = require('../helpers/helpers.voxel');
var _helpers2 = _interopRequireDefault(_helpers);
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 ***/
/**
* @module widgets/voxelProbe
*/
var WidgetsVoxelProbe = function (_WidgetsBase) {
_inherits(WidgetsVoxelProbe, _WidgetsBase);
function WidgetsVoxelProbe(stack, targetMesh, controls, camera, container) {
_classCallCheck(this, WidgetsVoxelProbe);
var _this = _possibleConstructorReturn(this, (WidgetsVoxelProbe.__proto__ || Object.getPrototypeOf(WidgetsVoxelProbe)).call(this, container));
_this._enabled = true;
_this._targetMesh = targetMesh;
_this._stack = stack;
// this._container = container;
_this._controls = controls;
_this._camera = camera;
_this._mouse = {
x: 0,
y: 0,
screenX: 0,
screenY: 0
};
// show only voxels that interesect the mesh
_this._showFrame = -1;
_this._raycaster = new THREE.Raycaster();
_this._active = true;
_this._hover = false;
_this._selected = false;
// event listeners
//Store the event handlers so that we can remove them in the destroy method.
//If we don't do like that, since bind creates a new function, the function we would pass to addEventListener
//would not be the same the we would pass to removeEventListener
_this.onMouseMoveHandler = _this.onMouseMove.bind(_this);
_this.onMouseUpHandler = _this.onMouseUp.bind(_this);
_this._container.addEventListener('mouseup', _this.onMouseUpHandler, false);
_this._container.addEventListener('mousemove', _this.onMouseMoveHandler, false);
_this._container.addEventListener('mousewheel', _this.onMouseMoveHandler, false);
_this._container.addEventListener('DOMMouseScroll', _this.onMouseMoveHandler, false); // firefox
_this._defaultColor = '#00B0FF';
_this._activeColor = '#FFEB3B';
_this._hoverColor = '#F50057';
_this._selectedColor = '#76FF03';
_this._showVoxel = true;
_this._showDomSVG = true;
_this._showDomMeasurements = true;
_this.createVoxel();
_this.initOffsets();
return _this;
}
_createClass(WidgetsVoxelProbe, [{
key: 'isEnabled',
value: function isEnabled() {}
}, {
key: 'onMouseMove',
value: function onMouseMove(event) {
if (this._enabled === false) {
return;
}
this._mouse = this.getMouseOffsets(event, this._container);
this.updateRaycaster();
}
}, {
key: 'onMouseUp',
value: function onMouseUp(event) {
if (this._enabled === false) {
return;
}
if (!this._active) {
if (this._hover) {
this.activateVoxel();
}
} else {
this.deactivateVoxel();
}
}
}, {
key: 'updateRaycaster',
value: function updateRaycaster() {
// update the raycaster
if (this._active) {
this._raycaster.setFromCamera(this._mouse, this._camera);
}
this.update();
}
}, {
key: 'updateColor',
value: function updateColor() {
if (this._active) {
this._voxel.color = this._activeColor;
} else if (this._hover) {
this._voxel.color = this._hoverColor;
} else if (this._selected) {
this._voxel.color = this._selectedColor;
} else {
this._voxel.color = this._defaultColor;
}
}
}, {
key: 'activateVoxel',
value: function activateVoxel() {
if (!this._active && this._hover) {
// Look for intersection against target mesh
var intersects = this._raycaster.intersectObject(this._targetMesh);
if (intersects.length > 0) {
// Active voxel
this._active = true;
this.updateColor();
// Disable controls
this._controls.enabled = false;
}
}
}
}, {
key: 'deactivateVoxel',
value: function deactivateVoxel() {
if (this._active) {
// change color + select it and nothing else selected
this._active = false;
// Enable controls
this._controls.enabled = true;
this.updateColor();
}
}
}, {
key: 'createVoxel',
value: function createVoxel() {
this._voxel = new _helpers2.default(this._stack.worldCenter(), this._stack);
this._voxel._showVoxel = true;
this._voxel._showDomSVG = true;
this._voxel._showDomMeasurements = true;
this.add(this._voxel);
}
}, {
key: 'update',
value: function update() {
// good to go
if (!this._targetMesh) {
return;
}
// Look for intersection against target mesh
var intersects = this._raycaster.intersectObject(this._targetMesh);
if (intersects.length > 0) {
// modify world position with getter/setter
this._voxel.worldCoordinates = intersects[0].point;
// create voxel helper
this._voxel.updateVoxel(intersects[0].point);
// add hover colors
this._voxel.updateVoxelScreenCoordinates(this._camera, this._container);
this.hoverVoxel(this._mouse, this._voxel.dataCoordinates);
this.updateColor();
// only works if slice is a frame...
// should test intersection of voxel with target mesh (i.e. plane, box, sphere, etc...)
// maybe use the raycasting somehow....
this.showOfIntersectsFrame(this._voxel, this._showFrame);
this._voxel.updateDom(this._container);
// show/hide mesh
this._voxel.showVoxel = this._showVoxel;
// show/hide dom stuff
this._voxel.showDomSVG = this._showDomSVG;
this._voxel.showDomMeasurements = this._showDomMeasurements;
}
}
}, {
key: 'free',
value: function free() {
this._container.removeEventListener('mouseup', this.onMouseUpHandler, false);
this._container.removeEventListener('mousemove', this.onMouseMoveHandler, false);
this._container.removeEventListener('mousewheel', this.onMouseMoveHandler, false);
this._container.removeEventListener('DOMMouseScroll', this.onMouseMoveHandler, false); // firefox
this._voxel.removeTest();
this.remove(this._voxel);
this._voxel = null;
_get(WidgetsVoxelProbe.prototype.__proto__ || Object.getPrototypeOf(WidgetsVoxelProbe.prototype), 'free', this).call(this);
}
}, {
key: 'hoverVoxel',
value: function hoverVoxel(mouseScreenCoordinates, currentDataCoordinates) {
// update distance mouse/this._voxel
var dx = mouseScreenCoordinates.screenX - this._voxel.voxel.screenCoordinates.x;
var dy = mouseScreenCoordinates.screenY - this._voxel.voxel.screenCoordinates.y;
var distance = Math.sqrt(dx * dx + dy * dy);
this._voxel.distance = distance;
if (distance >= 0 && distance < 10) {
this._hover = true;
} else {
this._hover = false;
}
}
}, {
key: 'showOfIntersectsFrame',
value: function showOfIntersectsFrame(voxelHelper, frameIndex) {
if (frameIndex === voxelHelper.voxel.dataCoordinates.z || frameIndex === -1) {
voxelHelper._showDomSVG = true;
voxelHelper._showDomMeasurements = true;
} else {
voxelHelper._showDomSVG = false;
voxelHelper._showDomMeasurements = false;
}
}
}, {
key: 'defaultColor',
set: function set(defaultColor) {
this._defaultColor = defaultColor;
this.update();
},
get: function get() {
return this._defaultColor;
}
}, {
key: 'activeColor',
set: function set(activeColor) {
this._activeColor = activeColor;
this.update();
},
get: function get() {
return this._activeColor;
}
}, {
key: 'hoverColor',
set: function set(hoverColor) {
this._hoverColor = hoverColor;
this.update();
},
get: function get() {
return this._hoverColor;
}
}, {
key: 'selectedColor',
set: function set(selectedColor) {
this._selectedColor = selectedColor;
this.update();
},
get: function get() {
return this._selectedColor;
}
}, {
key: 'showVoxel',
set: function set(showVoxel) {
this._showVoxel = showVoxel;
this.update();
},
get: function get() {
return this._showVoxel;
}
}, {
key: 'showDomSVG',
set: function set(showDomSVG) {
this._showDomSVG = showDomSVG;
this.update();
},
get: function get() {
return this._showDomSVG;
}
}, {
key: 'showDomMeasurements',
set: function set(showDomMeasurements) {
this._showDomMeasurements = showDomMeasurements;
this.update();
},
get: function get() {
return this._showDomMeasurements;
}
}]);
return WidgetsVoxelProbe;
}(_widgets2.default);
exports.default = WidgetsVoxelProbe;
module.exports = exports['default'];