🚨 Latest Research:Tanstack npm Packages Compromised in Ongoing Mini Shai-Hulud Supply-Chain Attack.Learn More
Socket
Book a DemoSign in
Socket

sparse-octree

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sparse-octree - npm Package Compare versions

Comparing version
2.4.2
to
2.5.0
+8
src/helpers/index.js
/**
* A collection of helpers.
*
* @module sparse-octree
* @submodule helpers
*/
export { OctreeHelper } from "./octree-helper.js";
import {
BufferAttribute,
BufferGeometry,
LineSegments,
LineBasicMaterial,
Object3D
} from "three";
import { PATTERN, EDGES } from "../core/octant.js";
/**
* An octree helper.
*
* The update method must be called manually to generate the octree geometry.
*
* @class OctreeHelper
* @submodule helpers
* @constructor
* @extends Object3D
* @param {Octree} [tree=null] - The octree to visualise.
*/
export class OctreeHelper extends Object3D {
constructor(octree = null) {
super();
this.name = "OctreeHelper";
/**
* The octree.
*
* @property octree
* @type Octree
*/
this.octree = octree;
this.update();
}
/**
* Creates octant geometry.
*
* @method createLineSegments
* @private
* @param {Array} octants - The octants.
*/
createLineSegments(octants) {
const maxOctants = (Math.pow(2, 16) / 8) - 1;
const group = new Object3D();
const material = new LineBasicMaterial({
color: 0xffffff * Math.random()
});
let octantCount = octants.length;
let vertexCount;
let length;
let indices, positions;
let octant, min, max;
let geometry;
let i, j, c, d, n;
let corner, edge;
// Create the geometry in limited runs - never create too many vertices.
for(i = 0, length = 0, n = Math.ceil(octantCount / maxOctants); n > 0; --n) {
length += (octantCount < maxOctants) ? octantCount : maxOctants;
octantCount -= maxOctants;
vertexCount = length * 8;
indices = new Uint16Array(vertexCount * 3);
positions = new Float32Array(vertexCount * 3);
// Don't reset i, continue where a previous run left off.
for(c = 0, d = 0; i < length; ++i) {
octant = octants[i];
min = octant.min;
max = octant.max;
for(j = 0; j < 12; ++j) {
edge = EDGES[j];
indices[d++] = c + edge[0];
indices[d++] = c + edge[1];
}
for(j = 0; j < 8; ++j, ++c) {
corner = PATTERN[j];
positions[c * 3] = (corner[0] === 0) ? min.x : max.x;
positions[c * 3 + 1] = (corner[1] === 0) ? min.y : max.y;
positions[c * 3 + 2] = (corner[2] === 0) ? min.z : max.z;
}
}
geometry = new BufferGeometry();
geometry.setIndex(new BufferAttribute(indices, 1));
geometry.addAttribute("position", new BufferAttribute(positions, 3));
group.add(new LineSegments(geometry, material));
}
this.add(group);
}
/**
* Updates the helper geometry.
*
* @method update
*/
update() {
const depth = (this.octree !== null) ? this.octree.getDepth() : -1;
let level = 0;
// Remove existing geometry.
this.dispose();
while(level <= depth) {
this.createLineSegments(this.octree.findOctantsByLevel(level));
++level;
}
}
/**
* Destroys this helper.
*
* @method dispose
*/
dispose() {
let child, children;
let i, j, il, jl;
for(i = 0, il = this.children.length; i < il; ++i) {
child = this.children[i];
children = child.children;
for(j = 0, jl = children.length; j < jl; ++j) {
children[j].geometry.dispose();
children[j].material.dispose();
}
while(children.length > 0) {
child.remove(children[0]);
}
}
while(this.children.length > 0) {
this.remove(children[0]);
}
}
}
import { Vector3 } from "./vector3.js";
/**
* A bounding box.
*
* @class Box3
* @submodule math
* @constructor
* @param {Vector3} [min] - The lower bounds.
* @param {Vector3} [max] - The upper bounds.
*/
export class Box3 {
constructor(
min = new Vector3(Infinity, Infinity, Infinity),
max = new Vector3(-Infinity, -Infinity, -Infinity)
) {
/**
* The min bounds.
*
* @property min
* @type Vector3
*/
this.min = min;
/**
* The max bounds.
*
* @property max
* @type Vector3
*/
this.max = max;
}
/**
* Sets the values of this box.
*
* @method set
* @param {Number} min - The min bounds.
* @param {Number} max - The max bounds.
* @return {Matrix3} This box.
*/
set(min, max) {
this.min.copy(min);
this.max.copy(max);
return this;
}
/**
* Copies the values of a given box.
*
* @method copy
* @param {Matrix3} b - A box.
* @return {Box3} This box.
*/
copy(b) {
this.min.copy(b.min);
this.max.copy(b.max);
return this;
}
/**
* Clones this matrix.
*
* @method clone
* @return {Matrix3} A clone of this matrix.
*/
clone() {
return new this.constructor().copy(this);
}
/**
* Expands this box by the given point.
*
* @method expandByPoint
* @param {Matrix3} p - A point.
* @return {Box3} This box.
*/
expandByPoint(p) {
this.min.min(p);
this.max.max(p);
return this;
}
/**
* Expands this box by combining it with the given one.
*
* @method union
* @param {Box3} b - A box.
* @return {Box3} This box.
*/
union(b) {
this.min.min(b.min);
this.max.max(b.max);
return this;
}
/**
* Defines this box by the given points.
*
* @method setFromPoints
* @param {Array} points - The points.
* @return {Box3} This box.
*/
setFromPoints(points) {
let i, l;
for(i = 0, l = points.length; i < l; ++i) {
this.expandByPoint(points[i]);
}
return this;
}
/**
* Defines this box by the given center and size.
*
* @method setFromCenterAndSize
* @param {Vector3} center - The center.
* @param {Number} size - The size.
* @return {Box3} This box.
*/
setFromCenterAndSize(center, size) {
const halfSize = size.clone().multiplyScalar(0.5);
this.min.copy(center).sub(halfSize);
this.max.copy(center).add(halfSize);
return this;
}
/**
* Checks if this box intersects with the given one.
*
* @method intersectsBox
* @param {Matrix3} box - A box.
* @return {Boolean} Whether the boxes intersect.
*/
intersectsBox(box) {
return !(
box.max.x < this.min.x || box.min.x > this.max.x ||
box.max.y < this.min.y || box.min.y > this.max.y ||
box.max.z < this.min.z || box.min.z > this.max.z
);
}
}
/**
* Math components.
*
* @module sparse-octree
* @submodule math
*/
export { Box3 } from "./box3.js";
export { Vector3 } from "./vector3.js";
/**
* A vector with three components.
*
* @class Vector3
* @submodule math
* @constructor
* @param {Number} [x=0] - The x value.
* @param {Number} [y=0] - The y value.
* @param {Number} [z=0] - The z value.
*/
export class Vector3 {
constructor(x = 0, y = 0, z = 0) {
/**
* The x component.
*
* @property x
* @type Number
*/
this.x = x;
/**
* The y component.
*
* @property y
* @type Number
*/
this.y = y;
/**
* The z component.
*
* @property z
* @type Number
*/
this.z = z;
}
/**
* Sets the values of this vector
*
* @method set
* @param {Number} x - The x value.
* @param {Number} y - The y value.
* @param {Number} z - The z value.
* @return {Vector3} This vector.
*/
set(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* Copies the values of another vector.
*
* @method copy
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
copy(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}
/**
* Copies values from an array.
*
* @method fromArray
* @param {Array} array - An array.
* @param {Number} offset - An offset.
* @return {Vector3} This vector.
*/
fromArray(array, offset) {
if(offset === undefined) { offset = 0; }
this.x = array[offset];
this.y = array[offset + 1];
this.z = array[offset + 2];
return this;
}
/**
* Stores this vector in an array.
*
* @method toArray
* @param {Array} [array] - A target array.
* @param {Number} offset - An offset.
* @return {Vector3} The array.
*/
toArray(array, offset) {
if(array === undefined) { array = []; }
if(offset === undefined) { offset = 0; }
array[offset] = this.x;
array[offset + 1] = this.y;
array[offset + 2] = this.z;
return array;
}
/**
* Checks if this vector equals the given one.
*
* @method equals
* @param {Vector3} v - A vector.
* @return {Boolean} Whether this vector equals the given one.
*/
equals(v) {
return (v.x === this.x && v.y === this.y && v.z === this.z);
}
/**
* Clones this vector.
*
* @method clone
* @return {Vector3} A clone of this vector.
*/
clone() {
return new this.constructor(this.x, this.y, this.z);
}
/**
* Adds a vector to this one.
*
* @method add
* @param {Vector3} v - The vector to add.
* @return {Vector3} This vector.
*/
add(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
}
/**
* Adds a scaled vector to this one.
*
* @method addScaledVector
* @param {Vector3} v - The vector to scale and add.
* @param {Number} s - A scalar.
* @return {Vector3} This vector.
*/
addScaledVector(v, s) {
this.x += v.x * s;
this.y += v.y * s;
this.z += v.z * s;
return this;
}
/**
* Adds a scalar to this vector.
*
* @method addScalar
* @param {Number} s - The scalar to add.
* @return {Vector3} This vector.
*/
addScalar(s) {
this.x += s;
this.y += s;
this.z += s;
return this;
}
/**
* Sets this vector to the sum of two given vectors.
*
* @method addVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
addVectors(a, b) {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
}
/**
* Subtracts a vector from this vector.
*
* @method sub
* @param {Vector3} v - The vector to subtract.
* @return {Vector3} This vector.
*/
sub(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}
/**
* Subtracts a scalar to this vector.
*
* @method subScalar
* @param {Number} s - The scalar to subtract.
* @return {Vector3} This vector.
*/
subScalar(s) {
this.x -= s;
this.y -= s;
this.z -= s;
return this;
}
/**
* Sets this vector to the difference between two given vectors.
*
* @method subVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - A second vector.
* @return {Vector3} This vector.
*/
subVectors(a, b) {
this.x = a.x - b.x;
this.y = a.y - b.y;
this.z = a.z - b.z;
return this;
}
/**
* Multiplies this vector with another vector.
*
* @method multiply
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
multiply(v) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
}
/**
* Multiplies this vector with a given scalar.
*
* @method multiplyScalar
* @param {Number} s - A scalar.
* @return {Vector3} This vector.
*/
multiplyScalar(s) {
if(isFinite(s)) {
this.x *= s;
this.y *= s;
this.z *= s;
} else {
this.x = 0;
this.y = 0;
this.z = 0;
}
return this;
}
/**
* Sets this vector to the product of two given vectors.
*
* @method multiplyVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
multiplyVectors(a, b) {
this.x = a.x * b.x;
this.y = a.y * b.y;
this.z = a.z * b.z;
return this;
}
/**
* Divides this vector by another vector.
*
* @method divide
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
divide(v) {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z;
return this;
}
/**
* Divides this vector by a given scalar.
*
* @method divideScalar
* @param {Number} s - A scalar.
* @return {Vector3} This vector.
*/
divideScalar(s) {
return this.multiplyScalar(1 / s);
}
/**
* Sets this vector to the quotient of two given vectors.
*
* @method divideVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
divideVectors(a, b) {
this.x = a.x / b.x;
this.y = a.y / b.y;
this.z = a.z / b.z;
return this;
}
/**
* Calculates the dot product with another vector.
*
* @method dot
* @param {Vector3} v - A vector.
* @return {Number} The dot product.
*/
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
/**
* Calculates the squared length of this vector.
*
* @method lengthSq
* @return {Number} The squared length.
*/
lengthSq() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/**
* Calculates the length of this vector.
*
* @method length
* @return {Number} The length.
*/
length() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/**
* Calculates the distance to a given vector.
*
* @method distanceTo
* @param {Vector3} v - A vector.
* @return {Number} The distance.
*/
distanceTo(v) {
return Math.sqrt(this.distanceToSquared(v));
}
/**
* Calculates the squared distance to a given vector.
*
* @method distanceToSquared
* @param {Vector3} v - A vector.
* @return {Number} The squared distance.
*/
distanceToSquared(v) {
const dx = this.x - v.x;
const dy = this.y - v.y;
const dz = this.z - v.z;
return dx * dx + dy * dy + dz * dz;
}
/**
* Normalizes this vector.
*
* @method normalize
* @return {Vector3} This vector.
*/
normalize() {
return this.divideScalar(this.length());
}
/**
* Adopts the min value for each component of this vector and the given one.
*
* @method min
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
min(v) {
this.x = Math.min(this.x, v.x);
this.y = Math.min(this.y, v.y);
this.z = Math.min(this.z, v.z);
return this;
}
/**
* adopts the max value for each component of this vector and the given one.
*
* @method max
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
max(v) {
this.x = Math.max(this.x, v.x);
this.y = Math.max(this.y, v.y);
this.z = Math.max(this.z, v.z);
return this;
}
/**
* Clamps this vector.
*
* @method clamp
* @param {Vector3} min - A vector, assumed to be smaller than max.
* @param {Vector3} max - A vector, assumed to be greater than min.
* @return {Vector3} This vector.
*/
clamp(min, max) {
this.x = Math.max(min.x, Math.min(max.x, this.x));
this.y = Math.max(min.y, Math.min(max.y, this.y));
this.z = Math.max(min.z, Math.min(max.z, this.z));
return this;
}
/**
* Applies a matrix to this vector.
*
* @method applyMatrix3
* @param {Matrix3} m - A matrix.
* @return {Vector3} This vector.
*/
applyMatrix3(m) {
const x = this.x, y = this.y, z = this.z;
const e = m.elements;
this.x = e[0] * x + e[3] * y + e[6] * z;
this.y = e[1] * x + e[4] * y + e[7] * z;
this.z = e[2] * x + e[5] * y + e[8] * z;
return this;
}
/**
* Applies a matrix to this vector.
*
* @method applyMatrix4
* @param {Matrix4} m - A matrix.
* @return {Vector3} This vector.
*/
applyMatrix4(m) {
const x = this.x, y = this.y, z = this.z;
const e = m.elements;
this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
return this;
}
}
+2
-2
/**
* sparse-octree v2.4.2 build Nov 05 2016
* sparse-octree v2.5.0 build Nov 19 2016
* https://github.com/vanruesc/sparse-octree
* Copyright 2016 Raoul van Rüschen, Zlib
*/
!function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],b):b(a.OCTREE=a.OCTREE||{},a.THREE)}(this,function(a,b){"use strict";function c(a,b,c,d,e,f){var g=0;return a>b&&a>c?(e<a&&(g|=2),f<a&&(g|=1)):b>c?(d<b&&(g|=4),f<b&&(g|=1)):(d<c&&(g|=4),e<c&&(g|=2)),g}function d(a,b,c,d){var e=void 0,f=0;return b<c?(e=b,f=0):(e=c,f=1),d<e&&(f=2),r[a][f]}function e(a,b,f,g,h,i,j,k,l){var m=a.children,n=void 0,o=void 0,p=void 0,r=void 0;if(h>=0&&i>=0&&j>=0)if(null===m)l.push(a);else{o=.5*(b+h),p=.5*(f+i),r=.5*(g+j),n=c(b,f,g,o,p,r);do switch(n){case 0:e(m[q[8]],b,f,g,o,p,r,k,l),n=d(n,o,p,r);break;case 1:e(m[q[8]^q[1]],b,f,r,o,p,j,k,l),n=d(n,o,p,j);break;case 2:e(m[q[8]^q[2]],b,p,g,o,i,r,k,l),n=d(n,o,i,r);break;case 3:e(m[q[8]^q[3]],b,p,r,o,i,j,k,l),n=d(n,o,i,j);break;case 4:e(m[q[8]^q[4]],o,f,g,h,p,r,k,l),n=d(n,h,p,r);break;case 5:e(m[q[8]^q[5]],o,f,r,h,p,j,k,l),n=d(n,h,p,j);break;case 6:e(m[q[8]^q[6]],o,p,g,h,i,r,k,l),n=d(n,h,i,r);break;case 7:e(m[q[8]^q[7]],o,p,r,h,i,j,k,l),n=8}while(n<8)}}var f=(function(){function a(a){this.value=a}function b(b){function c(a,b){return new Promise(function(c,e){var h={key:a,arg:b,resolve:c,reject:e,next:null};g?g=g.next=h:(f=g=h,d(a,b))})}function d(c,f){try{var g=b[c](f),h=g.value;h instanceof a?Promise.resolve(h.value).then(function(a){d("next",a)},function(a){d("throw",a)}):e(g.done?"return":"normal",g.value)}catch(a){e("throw",a)}}function e(a,b){switch(a){case"return":f.resolve({value:b,done:!0});break;case"throw":f.reject(b);break;default:f.resolve({value:b,done:!1})}f=f.next,f?d(f.key,f.arg):g=null}var f,g;this._invoke=c,"function"!=typeof b.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(b.prototype[Symbol.asyncIterator]=function(){return this}),b.prototype.next=function(a){return this._invoke("next",a)},b.prototype.throw=function(a){return this._invoke("throw",a)},b.prototype.return=function(a){return this._invoke("return",a)},{wrap:function(a){return function(){return new b(a.apply(this,arguments))}},await:function(b){return new a(b)}}}(),function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}),g=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),h=function a(b,c,d){null===b&&(b=Function.prototype);var e=Object.getOwnPropertyDescriptor(b,c);if(void 0===e){var f=Object.getPrototypeOf(b);return null===f?void 0:a(f,c,d)}if("value"in e)return e.value;var g=e.get;if(void 0!==g)return g.call(d)},i=function(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)},j=function(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!b||"object"!=typeof b&&"function"!=typeof b?a:b},k=function(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)},l=function(){function a(b,c,d){f(this,a),this.x=b||0,this.y=c||0,this.z=d||0}return g(a,[{key:"set",value:function(a,b,c){return this.x=a,this.y=b,this.z=c,this}},{key:"copy",value:function(a){return this.x=a.x,this.y=a.y,this.z=a.z,this}},{key:"fromArray",value:function(a,b){return void 0===b&&(b=0),this.x=a[b],this.y=a[b+1],this.z=a[b+2],this}},{key:"toArray",value:function(a,b){return void 0===a&&(a=[]),void 0===b&&(b=0),a[b]=this.x,a[b+1]=this.y,a[b+2]=this.z,a}},{key:"equals",value:function(a){return a.x===this.x&&a.y===this.y&&a.z===this.z}},{key:"clone",value:function(){return new this.constructor(this.x,this.y,this.z)}},{key:"add",value:function(a){return this.x+=a.x,this.y+=a.y,this.z+=a.z,this}},{key:"addScalar",value:function(a){return this.x+=a,this.y+=a,this.z+=a,this}},{key:"addVectors",value:function(a,b){return this.x=a.x+b.x,this.y=a.y+b.y,this.z=a.z+b.z,this}},{key:"sub",value:function(a){return this.x-=a.x,this.y-=a.y,this.z-=a.z,this}},{key:"subScalar",value:function(a){return this.x-=a,this.y-=a,this.z-=a,this}},{key:"subVectors",value:function(a,b){return this.x=a.x-b.x,this.y=a.y-b.y,this.z=a.z-b.z,this}},{key:"multiply",value:function(a){return this.x*=a.x,this.y*=a.y,this.z*=a.z,this}},{key:"multiplyScalar",value:function(a){return isFinite(a)?(this.x*=a,this.y*=a,this.z*=a):(this.x=0,this.y=0,this.z=0),this}},{key:"multiplyVectors",value:function(a,b){return this.x=a.x*b.x,this.y=a.y*b.y,this.z=a.z*b.z,this}},{key:"divide",value:function(a){return this.x/=a.x,this.y/=a.y,this.z/=a.z,this}},{key:"divideScalar",value:function(a){return this.multiplyScalar(1/a)}},{key:"divideVectors",value:function(a,b){return this.x=a.x/b.x,this.y=a.y/b.y,this.z=a.z/b.z,this}},{key:"dot",value:function(a){return this.x*a.x+this.y*a.y+this.z*a.z}},{key:"lengthSq",value:function(){return this.x*this.x+this.y*this.y+this.z*this.z}},{key:"length",value:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)}},{key:"distanceTo",value:function(a){return Math.sqrt(this.distanceToSquared(a))}},{key:"distanceToSquared",value:function(a){var b=this.x-a.x,c=this.y-a.y,d=this.z-a.z;return b*b+c*c+d*d}},{key:"normalize",value:function(){return this.divideScalar(this.length())}},{key:"min",value:function(a){return this.x=Math.min(this.x,a.x),this.y=Math.min(this.y,a.y),this.z=Math.min(this.z,a.z),this}},{key:"max",value:function(a){return this.x=Math.max(this.x,a.x),this.y=Math.max(this.y,a.y),this.z=Math.max(this.z,a.z),this}},{key:"clamp",value:function(a,b){return this.x=Math.max(a.x,Math.min(b.x,this.x)),this.y=Math.max(a.y,Math.min(b.y,this.y)),this.z=Math.max(a.z,Math.min(b.z,this.z)),this}},{key:"applyMatrix3",value:function(a){var b=this.x,c=this.y,d=this.z,e=a.elements;return this.x=e[0]*b+e[3]*c+e[6]*d,this.y=e[1]*b+e[4]*c+e[7]*d,this.z=e[2]*b+e[5]*c+e[8]*d,this}},{key:"applyMatrix4",value:function(a){var b=this.x,c=this.y,d=this.z,e=a.elements;return this.x=e[0]*b+e[4]*c+e[8]*d+e[12],this.y=e[1]*b+e[5]*c+e[9]*d+e[13],this.z=e[2]*b+e[6]*c+e[10]*d+e[14],this}}]),a}(),m=function(){function a(b,c){f(this,a),this.min=void 0!==b?b:new l,this.max=void 0!==c?c:new l,this.children=null}return g(a,[{key:"getCenter",value:function(){return this.min.clone().add(this.max).multiplyScalar(.5)}},{key:"getDimensions",value:function(){return this.max.clone().sub(this.min)}},{key:"split",value:function(a){var b=this.min,c=this.max,d=this.getCenter(),e=void 0,f=void 0,g=0,h=void 0,i=void 0,j=void 0,k=void 0,m=void 0;for(Array.isArray(a)&&(i=this.getDimensions().multiplyScalar(.5),j=[new l,new l,new l],g=a.length),this.children=[],e=0;e<8;++e){if(h=n[e],m=null,g>0)for(j[1].addVectors(b,j[0].fromArray(h).multiply(i)),j[2].addVectors(d,j[0].fromArray(h).multiply(i)),f=0;f<g;++f)if(k=a[f],null!==k&&j[1].equals(k.min)&&j[2].equals(k.max)){m=k,a[f]=null;break}this.children.push(null!==m?m:new this.constructor(new l(0===h[0]?b.x:d.x,0===h[1]?b.y:d.y,0===h[2]?b.z:d.z),new l(0===h[0]?d.x:c.x,0===h[1]?d.y:c.y,0===h[2]?d.z:c.z)))}}}]),a}(),n=[new Uint8Array([0,0,0]),new Uint8Array([0,0,1]),new Uint8Array([0,1,0]),new Uint8Array([0,1,1]),new Uint8Array([1,0,0]),new Uint8Array([1,0,1]),new Uint8Array([1,1,0]),new Uint8Array([1,1,1])],o=function(){function a(){var b=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new l,c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;f(this,a),this.min=b,this.size=c,this.children=null}return g(a,[{key:"getCenter",value:function(){return this.min.clone().addScalar(.5*this.size)}},{key:"getDimensions",value:function(){return new l(this.size,this.size,this.size)}},{key:"split",value:function(a){var b=this.min,c=this.getCenter(),d=.5*this.size,e=void 0,f=void 0,g=0,h=void 0,i=void 0,j=void 0,k=void 0;for(Array.isArray(a)&&(i=new l,g=a.length),this.children=[],e=0;e<8;++e){if(h=n[e],k=null,g>0)for(i.fromArray(h).multiplyScalar(d).add(b),f=0;f<g;++f)if(j=a[f],null!==j&&j.size===d&&i.equals(j.min)){k=j,a[f]=null;break}this.children.push(null!==k?k:new this.constructor(new l(0===h[0]?b.x:c.x,0===h[1]?b.y:c.y,0===h[2]?b.z:c.z),d))}}},{key:"max",get:function(){return this.min.clone().addScalar(this.size)}}]),a}(),p=function(){function a(b,c){f(this,a),this.min=void 0!==b?b:new l(1/0,1/0,1/0),this.max=void 0!==c?c:new l(-(1/0),-(1/0),-(1/0))}return g(a,[{key:"set",value:function(a,b){return this.min.copy(a),this.max.copy(b),this}},{key:"copy",value:function(a){return this.min.copy(a.min),this.max.copy(a.max),this}},{key:"clone",value:function(){return(new this.constructor).copy(this)}},{key:"expandByPoint",value:function(a){return this.min.min(a),this.max.max(a),this}},{key:"union",value:function(a){return this.min.min(a.min),this.max.max(a.max),this}},{key:"setFromPoints",value:function(a){var b=void 0,c=void 0;for(b=0,c=a.length;b<c;++b)this.expandByPoint(a[b]);return this}},{key:"setFromCenterAndSize",value:function(a,b){var c=b.clone().multiplyScalar(.5);return this.min.copy(a).sub(c),this.max.copy(a).add(c),this}},{key:"intersectsBox",value:function(a){return!(a.max.x<this.min.x||a.min.x>this.max.x||a.max.y<this.min.y||a.min.y>this.max.y||a.max.z<this.min.z||a.min.z>this.max.z)}}]),a}(),q=new Uint8Array([0,1,2,3,4,5,6,7,0]),r=[new Uint8Array([4,2,1]),new Uint8Array([5,3,8]),new Uint8Array([6,8,3]),new Uint8Array([7,8,8]),new Uint8Array([8,6,5]),new Uint8Array([8,7,8]),new Uint8Array([8,8,7]),new Uint8Array([8,8,8])],s=function(){function a(){f(this,a)}return g(a,null,[{key:"raycast",value:function(a,b,c){var d=a.getDimensions(),f=d.clone().multiplyScalar(.5),g=a.min.clone().sub(a.min),h=a.max.clone().sub(a.min),i=b.ray.direction.clone(),j=b.ray.origin.clone();j.sub(a.getCenter()).add(f);var k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0,r=void 0,s=void 0,t=void 0;q[8]=q[0],i.x<0&&(j.x=d.x-j.x,i.x=-i.x,q[8]|=q[4]),i.y<0&&(j.y=d.y-j.y,i.y=-i.y,q[8]|=q[2]),i.z<0&&(j.z=d.z-j.z,i.z=-i.z,q[8]|=q[1]),k=1/i.x,l=1/i.y,m=1/i.z,n=(g.x-j.x)*k,o=(h.x-j.x)*k,p=(g.y-j.y)*l,r=(h.y-j.y)*l,s=(g.z-j.z)*m,t=(h.z-j.z)*m,Math.max(Math.max(n,p),s)<Math.min(Math.min(o,r),t)&&e(a.root,n,p,s,o,r,t,b,c)}}]),a}(),t=function(){function a(b,c){f(this,a),this.root=void 0!==b&&void 0!==c?new m(b,c):null}return g(a,[{key:"getCenter",value:function(){return this.root.getCenter()}},{key:"getDimensions",value:function(){return this.root.getDimensions()}},{key:"getDepth",value:function(){for(var a=[this.root],b=[],c=0,d=void 0,e=void 0;a.length>0;){if(d=a.pop(),e=d.children,null!==e){var f;(f=b).push.apply(f,k(e))}0===a.length&&(a=b,b=[],a.length>0&&++c)}return c}},{key:"cull",value:function(a){for(var b=[],c=[this.root],d=new p,e=void 0,f=void 0;c.length>0;)e=c.pop(),f=e.children,d.min=e.min,d.max=e.max,a.intersectsBox(d)&&(null!==f?c.push.apply(c,k(f)):b.push(e));return b}},{key:"findOctantsByLevel",value:function(a){for(var b=[],c=[this.root],d=[],e=void 0,f=void 0,g=0;c.length>0;){if(e=c.pop(),f=e.children,g===a)b.push(e);else if(null!==f){var h;(h=d).push.apply(h,k(f))}if(0===c.length&&(c=d,d=[],++g>a))break}return b}},{key:"raycast",value:function(a,b){s.raycast(this,a,b)}},{key:"min",get:function(){return this.root.min}},{key:"max",get:function(){return this.root.max}},{key:"children",get:function(){return this.root.children}}]),a}(),u=function(a){function c(){var a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;f(this,c);var b=j(this,(c.__proto__||Object.getPrototypeOf(c)).call(this));return b.name="OctreeHelper",b.tree=a,b}return i(c,a),g(c,[{key:"update",value:function(){var a=new Map,c=null!==this.tree?this.tree.getDepth():-1,d=[[1,4],[2,5],[3,6],[0,7],[5],[6],[7],[4]],e=void 0,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0,q=void 0,r=void 0,s=void 0,t=void 0,u=null,v=null,w=0;for(this.dispose();w<=c;){for(j=this.tree.findOctantsByLevel(w),t=0,a.clear(),e=0,f=0,h=j.length;e<h;++e)for(k=j[e],l=[[k.max.x,k.max.y,k.max.z],[k.min.x,k.max.y,k.max.z],[k.min.x,k.min.y,k.max.z],[k.max.x,k.min.y,k.max.z],[k.max.x,k.max.y,k.min.z],[k.min.x,k.max.y,k.min.z],[k.min.x,k.min.y,k.min.z],[k.max.x,k.min.y,k.min.z]],f=0;f<8;++f)if(m=l[f],n=d[f],p=m.toString(),o=a.get(p),void 0!==o)for(g=0,i=n.length;g<i;++g)p=l[n[g]].toString(),o.connectionKeys.indexOf(p)<0&&(o.connectionKeys.push(p),++t);else{for(o={position:m,connectionKeys:[],index:a.size},g=0,i=n.length;g<i;++g)o.connectionKeys.push(l[n[g]].toString()),++t;a.set(p,o)}if(!(a.size<65536))throw new Error("Could not create geometry for octree depth level "+w+" (vertex count of "+a.size+" exceeds limit of 65536)");u=new Uint16Array(2*t),v=new Float32Array(3*a.size),e=0,f=0;var x=!0,y=!1,z=void 0;try{for(var A,B=a.values()[Symbol.iterator]();!(x=(A=B.next()).done);x=!0)for(o=A.value,m=o.position,v[e++]=m[0],v[e++]=m[1],v[e++]=m[2],n=o.connectionKeys,g=0,i=n.length;g<i;++g)u[f++]=o.index,u[f++]=a.get(n[g]).index}catch(a){y=!0,z=a}finally{try{!x&&B.return&&B.return()}finally{if(y)throw z}}q=new b.BufferGeometry,q.setIndex(new b.BufferAttribute(u,1)),q.addAttribute("position",new b.BufferAttribute(v,3)),s=new b.LineBasicMaterial({color:16777215*Math.random()}),r=new b.LineSegments(q,s),this.add(r),++w}}},{key:"dispose",value:function(){var a=this.children,b=void 0,c=void 0;for(b=0,c=a.length;b<c;++b)a[b].geometry.dispose(),a[b].material.dispose();for(;a.length>0;)a.remove(a[0])}}]),c}(b.Object3D),v=function(a){function b(a,c){f(this,b);var d=j(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,a,c));return d.points=null,d.data=null,d}return i(b,a),g(b,[{key:"countPoints",value:function(){for(var a=[this],b=0,c=void 0,d=void 0;a.length>0;)c=a.pop(),d=c.children,null!==d?a.push.apply(a,k(d)):null!==c.points&&(b+=c.points.length);return b}},{key:"distanceToSquared",value:function(a){var b=a.clone().clamp(this.min,this.max);return b.sub(a).lengthSq()}},{key:"distanceToCenterSquared",value:function(a){var b=this.getCenter(),c=a.x-b.x,d=a.y-b.x,e=a.z-b.z;return c*c+d*d+e*e}},{key:"contains",value:function(a,b){var c=this.min,d=this.max;return a.x>=c.x-b&&a.y>=c.y-b&&a.z>=c.z-b&&a.x<=d.x+b&&a.y<=d.y+b&&a.z<=d.z+b}},{key:"redistribute",value:function(a){var b=this.children,c=this.points,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0;if(null!==b)for(;c.length>0;)for(g=c.pop(),h=this.data.pop(),d=0,e=b.length;d<e;++d)if(f=b[d],f.contains(g,a)){null===f.points&&(f.points=[],f.data=[]),f.points.push(g),f.data.push(h);break}this.points=null,this.data=null}},{key:"merge",value:function(){var a=this.children,b=void 0,c=void 0,d=void 0;if(null!==a){for(this.points=[],this.data=[],b=0,c=a.length;b<c;++b)if(d=a[b],null!==d.points){var e,f;(e=this.points).push.apply(e,k(d.points)),(f=this.data).push.apply(f,k(d.data))}this.children=null}}},{key:"findNearestPoint",value:function(a,b,c){var d=this.points,e=this.children,f=null,g=b,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0;if(null!==e)for(l=e.map(function(b){return{octant:b,distance:b.distanceToCenterSquared(a)}}).sort(function(a,b){return a.distance-b.distance}),h=0,i=l.length;h<i;++h)m=l[h].octant,m.contains(a,g)&&(n=m.findNearestPoint(a,g,c),null!==n&&(k=n.point.distanceToSquared(a),(!c||k>0)&&k<g&&(g=k,f=n)));else if(null!==d)for(h=0,i=d.length;h<i;++h)j=d[h],k=a.distanceToSquared(j),(!c||k>0)&&k<g&&(g=k,f={point:j.clone(),data:this.data[h]});return f}},{key:"findPoints",value:function(a,b,c,d){var e=this.points,f=this.children,g=b*b,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0;if(null!==f)for(h=0,i=f.length;h<i;++h)l=f[h],l.contains(a,b)&&l.findPoints(a,b,c,d);else if(null!==e)for(h=0,i=e.length;h<i;++h)j=e[h],k=a.distanceToSquared(j),(!c||k>0)&&k<=g&&d.push({point:j.clone(),data:this.data[h]})}}]),b}(m),w=function(a){function b(a,c){var d=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,e=arguments.length>3&&void 0!==arguments[3]?arguments[3]:8,g=arguments.length>4&&void 0!==arguments[4]?arguments[4]:8;f(this,b);var h=j(this,(b.__proto__||Object.getPrototypeOf(b)).call(this));return h.root=new v(a,c),h.bias=Math.max(0,d),h.biasSquared=h.bias*h.bias,h.maxPoints=Math.max(1,Math.round(e)),h.maxDepth=Math.max(0,Math.round(g)),h}return i(b,a),g(b,[{key:"countPoints",value:function(){return this.root.countPoints()}},{key:"add",value:function(a,b){a=a.clone();var c=[this.root],d=0,e=void 0,f=void 0,g=void 0,h=void 0,i=!1;if(void 0!==b&&null!==b)for(;c.length>0;)if(e=c.pop(),f=e.children,e.contains(a,this.bias))if(c=[],null!==f){var j;(j=c).push.apply(j,k(f)),++d}else{if(null===e.points)e.points=[],e.data=[];else for(g=0,h=e.points.length;!i&&g<h;++g)i=e.points[g].equals(a);if(i)e.data[g-1]=b;else if(e.points.length<this.maxPoints||d===this.maxDepth)e.points.push(a),e.data.push(b);else{var l;e.split(),e.redistribute(this.bias),(l=c).push.apply(l,k(e.children)),++d}}}},{key:"remove",value:function(a){for(var b=[this.root],c=this.root,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,l=void 0;b.length>0;)if(d=b.pop(),e=d.children,d.contains(a,this.bias))if(b=[],null!==e){var m;(m=b).push.apply(m,k(e)),c=d}else if(null!==d.points)for(h=d.points,i=d.data,f=0,g=h.length;f<g;++f)if(j=h[f],j.equals(a)){l=g-1,f<l&&(h[f]=h[l],i[f]=i[l]),h.pop(),i.pop(),c.countPoints()<=this.maxPoints&&c.merge();break}}},{key:"fetch",value:function(a){for(var b=[this.root],c=null,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0;b.length>0;)if(d=b.pop(),e=d.children,d.contains(a,this.bias))if(b=[],null!==e){var i;(i=b).push.apply(i,k(e))}else for(f=0,g=d.points.length;f<g;++f)if(h=d.points[f],a.distanceToSquared(h)<=this.biasSquared){c=d.data[f];break}return c}},{key:"findNearestPoint",value:function(a){var b=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1/0,c=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return this.root.findNearestPoint(a,b,c)}},{key:"findPoints",value:function(a,b){var c=arguments.length>2&&void 0!==arguments[2]&&arguments[2],d=[];return this.root.findPoints(a,b,c,d),d}},{key:"raycast",value:function(a,c){var d=[];h(b.prototype.__proto__||Object.getPrototypeOf(b.prototype),"raycast",this).call(this,a,d),d.length>0&&this.testPoints(d,a,c)}},{key:"testPoints",value:function(a,b,c){var d=b.params.Points.threshold,e=d*d,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0;for(j=0,l=a.length;j<l;++j)if(n=a[j],o=n.points,null!==o)for(k=0,m=o.length;k<m;++k)p=o[k],i=b.ray.distanceSqToPoint(p),i<e&&(f=b.ray.closestPointToPoint(p),g=b.ray.origin.distanceTo(f),g>=b.near&&g<=b.far&&(h=Math.sqrt(i),c.push({distance:g,distanceToRay:h,point:f.clone(),object:n.data[k]})))}}]),b}(t);a.CubicOctant=o,a.Octant=m,a.Octree=t,a.OctreeHelper=u,a.PATTERN=n,a.Raycasting=s,a.PointOctant=v,a.PointOctree=w,Object.defineProperty(a,"__esModule",{value:!0})});
!function(a,b){"object"==typeof exports&&"undefined"!=typeof module?b(exports,require("three")):"function"==typeof define&&define.amd?define(["exports","three"],b):b(a.OCTREE=a.OCTREE||{},a.THREE)}(this,function(a,b){"use strict";function c(a,b,c,d,e,f){var g=0;return a>b&&a>c?(e<a&&(g|=2),f<a&&(g|=1)):b>c?(d<b&&(g|=4),f<b&&(g|=1)):(d<c&&(g|=4),e<c&&(g|=2)),g}function d(a,b,c,d){var e=void 0,f=0;return b<c?(e=b,f=0):(e=c,f=1),d<e&&(f=2),s[a][f]}function e(a,b,f,g,h,i,j,k,l){var m=a.children,n=void 0,o=void 0,p=void 0,q=void 0;if(h>=0&&i>=0&&j>=0)if(null===m)l.push(a);else{o=.5*(b+h),p=.5*(f+i),q=.5*(g+j),n=c(b,f,g,o,p,q);do switch(n){case 0:e(m[r[8]],b,f,g,o,p,q,k,l),n=d(n,o,p,q);break;case 1:e(m[r[8]^r[1]],b,f,q,o,p,j,k,l),n=d(n,o,p,j);break;case 2:e(m[r[8]^r[2]],b,p,g,o,i,q,k,l),n=d(n,o,i,q);break;case 3:e(m[r[8]^r[3]],b,p,q,o,i,j,k,l),n=d(n,o,i,j);break;case 4:e(m[r[8]^r[4]],o,f,g,h,p,q,k,l),n=d(n,h,p,q);break;case 5:e(m[r[8]^r[5]],o,f,q,h,p,j,k,l),n=d(n,h,p,j);break;case 6:e(m[r[8]^r[6]],o,p,g,h,i,q,k,l),n=d(n,h,i,q);break;case 7:e(m[r[8]^r[7]],o,p,q,h,i,j,k,l),n=8}while(n<8)}}var f=(function(){function a(a){this.value=a}function b(b){function c(a,b){return new Promise(function(c,e){var h={key:a,arg:b,resolve:c,reject:e,next:null};g?g=g.next=h:(f=g=h,d(a,b))})}function d(c,f){try{var g=b[c](f),h=g.value;h instanceof a?Promise.resolve(h.value).then(function(a){d("next",a)},function(a){d("throw",a)}):e(g.done?"return":"normal",g.value)}catch(a){e("throw",a)}}function e(a,b){switch(a){case"return":f.resolve({value:b,done:!0});break;case"throw":f.reject(b);break;default:f.resolve({value:b,done:!1})}f=f.next,f?d(f.key,f.arg):g=null}var f,g;this._invoke=c,"function"!=typeof b.return&&(this.return=void 0)}return"function"==typeof Symbol&&Symbol.asyncIterator&&(b.prototype[Symbol.asyncIterator]=function(){return this}),b.prototype.next=function(a){return this._invoke("next",a)},b.prototype.throw=function(a){return this._invoke("throw",a)},b.prototype.return=function(a){return this._invoke("return",a)},{wrap:function(a){return function(){return new b(a.apply(this,arguments))}},await:function(b){return new a(b)}}}(),function(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}),g=function(){function a(a,b){for(var c=0;c<b.length;c++){var d=b[c];d.enumerable=d.enumerable||!1,d.configurable=!0,"value"in d&&(d.writable=!0),Object.defineProperty(a,d.key,d)}}return function(b,c,d){return c&&a(b.prototype,c),d&&a(b,d),b}}(),h=function a(b,c,d){null===b&&(b=Function.prototype);var e=Object.getOwnPropertyDescriptor(b,c);if(void 0===e){var f=Object.getPrototypeOf(b);return null===f?void 0:a(f,c,d)}if("value"in e)return e.value;var g=e.get;if(void 0!==g)return g.call(d)},i=function(a,b){if("function"!=typeof b&&null!==b)throw new TypeError("Super expression must either be null or a function, not "+typeof b);a.prototype=Object.create(b&&b.prototype,{constructor:{value:a,enumerable:!1,writable:!0,configurable:!0}}),b&&(Object.setPrototypeOf?Object.setPrototypeOf(a,b):a.__proto__=b)},j=function(a,b){if(!a)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!b||"object"!=typeof b&&"function"!=typeof b?a:b},k=function(a){if(Array.isArray(a)){for(var b=0,c=Array(a.length);b<a.length;b++)c[b]=a[b];return c}return Array.from(a)},l=function(){function a(){var b=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0,d=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;f(this,a),this.x=b,this.y=c,this.z=d}return g(a,[{key:"set",value:function(a,b,c){return this.x=a,this.y=b,this.z=c,this}},{key:"copy",value:function(a){return this.x=a.x,this.y=a.y,this.z=a.z,this}},{key:"fromArray",value:function(a,b){return void 0===b&&(b=0),this.x=a[b],this.y=a[b+1],this.z=a[b+2],this}},{key:"toArray",value:function(a,b){return void 0===a&&(a=[]),void 0===b&&(b=0),a[b]=this.x,a[b+1]=this.y,a[b+2]=this.z,a}},{key:"equals",value:function(a){return a.x===this.x&&a.y===this.y&&a.z===this.z}},{key:"clone",value:function(){return new this.constructor(this.x,this.y,this.z)}},{key:"add",value:function(a){return this.x+=a.x,this.y+=a.y,this.z+=a.z,this}},{key:"addScaledVector",value:function(a,b){return this.x+=a.x*b,this.y+=a.y*b,this.z+=a.z*b,this}},{key:"addScalar",value:function(a){return this.x+=a,this.y+=a,this.z+=a,this}},{key:"addVectors",value:function(a,b){return this.x=a.x+b.x,this.y=a.y+b.y,this.z=a.z+b.z,this}},{key:"sub",value:function(a){return this.x-=a.x,this.y-=a.y,this.z-=a.z,this}},{key:"subScalar",value:function(a){return this.x-=a,this.y-=a,this.z-=a,this}},{key:"subVectors",value:function(a,b){return this.x=a.x-b.x,this.y=a.y-b.y,this.z=a.z-b.z,this}},{key:"multiply",value:function(a){return this.x*=a.x,this.y*=a.y,this.z*=a.z,this}},{key:"multiplyScalar",value:function(a){return isFinite(a)?(this.x*=a,this.y*=a,this.z*=a):(this.x=0,this.y=0,this.z=0),this}},{key:"multiplyVectors",value:function(a,b){return this.x=a.x*b.x,this.y=a.y*b.y,this.z=a.z*b.z,this}},{key:"divide",value:function(a){return this.x/=a.x,this.y/=a.y,this.z/=a.z,this}},{key:"divideScalar",value:function(a){return this.multiplyScalar(1/a)}},{key:"divideVectors",value:function(a,b){return this.x=a.x/b.x,this.y=a.y/b.y,this.z=a.z/b.z,this}},{key:"dot",value:function(a){return this.x*a.x+this.y*a.y+this.z*a.z}},{key:"lengthSq",value:function(){return this.x*this.x+this.y*this.y+this.z*this.z}},{key:"length",value:function(){return Math.sqrt(this.x*this.x+this.y*this.y+this.z*this.z)}},{key:"distanceTo",value:function(a){return Math.sqrt(this.distanceToSquared(a))}},{key:"distanceToSquared",value:function(a){var b=this.x-a.x,c=this.y-a.y,d=this.z-a.z;return b*b+c*c+d*d}},{key:"normalize",value:function(){return this.divideScalar(this.length())}},{key:"min",value:function(a){return this.x=Math.min(this.x,a.x),this.y=Math.min(this.y,a.y),this.z=Math.min(this.z,a.z),this}},{key:"max",value:function(a){return this.x=Math.max(this.x,a.x),this.y=Math.max(this.y,a.y),this.z=Math.max(this.z,a.z),this}},{key:"clamp",value:function(a,b){return this.x=Math.max(a.x,Math.min(b.x,this.x)),this.y=Math.max(a.y,Math.min(b.y,this.y)),this.z=Math.max(a.z,Math.min(b.z,this.z)),this}},{key:"applyMatrix3",value:function(a){var b=this.x,c=this.y,d=this.z,e=a.elements;return this.x=e[0]*b+e[3]*c+e[6]*d,this.y=e[1]*b+e[4]*c+e[7]*d,this.z=e[2]*b+e[5]*c+e[8]*d,this}},{key:"applyMatrix4",value:function(a){var b=this.x,c=this.y,d=this.z,e=a.elements;return this.x=e[0]*b+e[4]*c+e[8]*d+e[12],this.y=e[1]*b+e[5]*c+e[9]*d+e[13],this.z=e[2]*b+e[6]*c+e[10]*d+e[14],this}}]),a}(),m=function(){function a(){var b=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new l,c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new l;f(this,a),this.min=b,this.max=c,this.children=null}return g(a,[{key:"getCenter",value:function(){return this.min.clone().add(this.max).multiplyScalar(.5)}},{key:"getDimensions",value:function(){return this.max.clone().sub(this.min)}},{key:"split",value:function(a){var b=this.min,c=this.max,d=this.getCenter(),e=void 0,f=void 0,g=0,h=void 0,i=void 0,j=void 0,k=void 0,m=void 0;for(Array.isArray(a)&&(i=this.getDimensions().multiplyScalar(.5),j=[new l,new l,new l],g=a.length),this.children=[],e=0;e<8;++e){if(h=n[e],m=null,g>0)for(j[1].addVectors(b,j[0].fromArray(h).multiply(i)),j[2].addVectors(d,j[0].fromArray(h).multiply(i)),f=0;f<g;++f)if(k=a[f],null!==k&&j[1].equals(k.min)&&j[2].equals(k.max)){m=k,a[f]=null;break}this.children.push(null!==m?m:new this.constructor(new l(0===h[0]?b.x:d.x,0===h[1]?b.y:d.y,0===h[2]?b.z:d.z),new l(0===h[0]?d.x:c.x,0===h[1]?d.y:c.y,0===h[2]?d.z:c.z)))}}}]),a}(),n=[new Uint8Array([0,0,0]),new Uint8Array([0,0,1]),new Uint8Array([0,1,0]),new Uint8Array([0,1,1]),new Uint8Array([1,0,0]),new Uint8Array([1,0,1]),new Uint8Array([1,1,0]),new Uint8Array([1,1,1])],o=[new Uint8Array([0,4]),new Uint8Array([1,5]),new Uint8Array([2,6]),new Uint8Array([3,7]),new Uint8Array([0,2]),new Uint8Array([1,3]),new Uint8Array([4,6]),new Uint8Array([5,7]),new Uint8Array([0,1]),new Uint8Array([2,3]),new Uint8Array([4,5]),new Uint8Array([6,7])],p=function(){function a(){var b=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new l,c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;f(this,a),this.min=b,this.size=c,this.children=null}return g(a,[{key:"getCenter",value:function(){return this.min.clone().addScalar(.5*this.size)}},{key:"getDimensions",value:function(){return new l(this.size,this.size,this.size)}},{key:"split",value:function(a){var b=this.min,c=this.getCenter(),d=.5*this.size,e=void 0,f=void 0,g=0,h=void 0,i=void 0,j=void 0,k=void 0;for(Array.isArray(a)&&(i=new l,g=a.length),this.children=[],e=0;e<8;++e){if(h=n[e],k=null,g>0)for(i.fromArray(h).multiplyScalar(d).add(b),f=0;f<g;++f)if(j=a[f],null!==j&&j.size===d&&i.equals(j.min)){k=j,a[f]=null;break}this.children.push(null!==k?k:new this.constructor(new l(0===h[0]?b.x:c.x,0===h[1]?b.y:c.y,0===h[2]?b.z:c.z),d))}}},{key:"max",get:function(){return this.min.clone().addScalar(this.size)}}]),a}(),q=function(){function a(){var b=arguments.length>0&&void 0!==arguments[0]?arguments[0]:new l(1/0,1/0,1/0),c=arguments.length>1&&void 0!==arguments[1]?arguments[1]:new l(-(1/0),-(1/0),-(1/0));f(this,a),this.min=b,this.max=c}return g(a,[{key:"set",value:function(a,b){return this.min.copy(a),this.max.copy(b),this}},{key:"copy",value:function(a){return this.min.copy(a.min),this.max.copy(a.max),this}},{key:"clone",value:function(){return(new this.constructor).copy(this)}},{key:"expandByPoint",value:function(a){return this.min.min(a),this.max.max(a),this}},{key:"union",value:function(a){return this.min.min(a.min),this.max.max(a.max),this}},{key:"setFromPoints",value:function(a){var b=void 0,c=void 0;for(b=0,c=a.length;b<c;++b)this.expandByPoint(a[b]);return this}},{key:"setFromCenterAndSize",value:function(a,b){var c=b.clone().multiplyScalar(.5);return this.min.copy(a).sub(c),this.max.copy(a).add(c),this}},{key:"intersectsBox",value:function(a){return!(a.max.x<this.min.x||a.min.x>this.max.x||a.max.y<this.min.y||a.min.y>this.max.y||a.max.z<this.min.z||a.min.z>this.max.z)}}]),a}(),r=new Uint8Array([0,1,2,3,4,5,6,7,0]),s=[new Uint8Array([4,2,1]),new Uint8Array([5,3,8]),new Uint8Array([6,8,3]),new Uint8Array([7,8,8]),new Uint8Array([8,6,5]),new Uint8Array([8,7,8]),new Uint8Array([8,8,7]),new Uint8Array([8,8,8])],t=function(){function a(){f(this,a)}return g(a,null,[{key:"raycast",value:function(a,b,c){var d=a.getDimensions(),f=d.clone().multiplyScalar(.5),g=a.min.clone().sub(a.min),h=a.max.clone().sub(a.min),i=b.ray.direction.clone(),j=b.ray.origin.clone();j.sub(a.getCenter()).add(f);var k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0,q=void 0,s=void 0,t=void 0;r[8]=r[0],i.x<0&&(j.x=d.x-j.x,i.x=-i.x,r[8]|=r[4]),i.y<0&&(j.y=d.y-j.y,i.y=-i.y,r[8]|=r[2]),i.z<0&&(j.z=d.z-j.z,i.z=-i.z,r[8]|=r[1]),k=1/i.x,l=1/i.y,m=1/i.z,n=(g.x-j.x)*k,o=(h.x-j.x)*k,p=(g.y-j.y)*l,q=(h.y-j.y)*l,s=(g.z-j.z)*m,t=(h.z-j.z)*m,Math.max(Math.max(n,p),s)<Math.min(Math.min(o,q),t)&&e(a.root,n,p,s,o,q,t,b,c)}}]),a}(),u=function(){function a(b,c){f(this,a),this.root=void 0!==b&&void 0!==c?new m(b,c):null}return g(a,[{key:"getCenter",value:function(){return this.root.getCenter()}},{key:"getDimensions",value:function(){return this.root.getDimensions()}},{key:"getDepth",value:function(){for(var a=[this.root],b=[],c=0,d=void 0,e=void 0;a.length>0;){if(d=a.pop(),e=d.children,null!==e){var f;(f=b).push.apply(f,k(e))}0===a.length&&(a=b,b=[],a.length>0&&++c)}return c}},{key:"cull",value:function(a){for(var b=[],c=[this.root],d=new q,e=void 0,f=void 0;c.length>0;)e=c.pop(),f=e.children,d.min=e.min,d.max=e.max,a.intersectsBox(d)&&(null!==f?c.push.apply(c,k(f)):b.push(e));return b}},{key:"findOctantsByLevel",value:function(a){for(var b=[],c=[this.root],d=[],e=void 0,f=void 0,g=0;c.length>0;){if(e=c.pop(),f=e.children,g===a)b.push(e);else if(null!==f){var h;(h=d).push.apply(h,k(f))}if(0===c.length&&(c=d,d=[],++g>a))break}return b}},{key:"raycast",value:function(a,b){t.raycast(this,a,b)}},{key:"min",get:function(){return this.root.min}},{key:"max",get:function(){return this.root.max}},{key:"children",get:function(){return this.root.children}}]),a}(),v=function(a){function c(){var a=arguments.length>0&&void 0!==arguments[0]?arguments[0]:null;f(this,c);var b=j(this,(c.__proto__||Object.getPrototypeOf(c)).call(this));return b.name="OctreeHelper",b.octree=a,b.update(),b}return i(c,a),g(c,[{key:"createLineSegments",value:function(a){var c=Math.pow(2,16)/8-1,d=new b.Object3D,e=new b.LineBasicMaterial({color:16777215*Math.random()}),f=a.length,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,p=void 0,q=void 0,r=void 0,s=void 0,t=void 0,u=void 0,v=void 0,w=void 0;for(q=0,h=0,u=Math.ceil(f/c);u>0;--u){for(h+=f<c?f:c,f-=c,g=8*h,i=new Uint16Array(3*g),j=new Float32Array(3*g),s=0,t=0;q<h;++q){for(k=a[q],l=k.min,m=k.max,r=0;r<12;++r)w=o[r],i[t++]=s+w[0],i[t++]=s+w[1];for(r=0;r<8;++r,++s)v=n[r],j[3*s]=0===v[0]?l.x:m.x,j[3*s+1]=0===v[1]?l.y:m.y,j[3*s+2]=0===v[2]?l.z:m.z}p=new b.BufferGeometry,p.setIndex(new b.BufferAttribute(i,1)),p.addAttribute("position",new b.BufferAttribute(j,3)),d.add(new b.LineSegments(p,e))}this.add(d)}},{key:"update",value:function(){var a=null!==this.octree?this.octree.getDepth():-1,b=0;for(this.dispose();b<=a;)this.createLineSegments(this.octree.findOctantsByLevel(b)),++b}},{key:"dispose",value:function(){var a=void 0,b=void 0,c=void 0,d=void 0,e=void 0,f=void 0;for(c=0,e=this.children.length;c<e;++c){for(a=this.children[c],b=a.children,d=0,f=b.length;d<f;++d)b[d].geometry.dispose(),b[d].material.dispose();for(;b.length>0;)a.remove(b[0])}for(;this.children.length>0;)this.remove(b[0])}}]),c}(b.Object3D),w=function(a){function b(a,c){f(this,b);var d=j(this,(b.__proto__||Object.getPrototypeOf(b)).call(this,a,c));return d.points=null,d.data=null,d}return i(b,a),g(b,[{key:"countPoints",value:function(){for(var a=[this],b=0,c=void 0,d=void 0;a.length>0;)c=a.pop(),d=c.children,null!==d?a.push.apply(a,k(d)):null!==c.points&&(b+=c.points.length);return b}},{key:"distanceToSquared",value:function(a){var b=a.clone().clamp(this.min,this.max);return b.sub(a).lengthSq()}},{key:"distanceToCenterSquared",value:function(a){var b=this.getCenter(),c=a.x-b.x,d=a.y-b.x,e=a.z-b.z;return c*c+d*d+e*e}},{key:"contains",value:function(a,b){var c=this.min,d=this.max;return a.x>=c.x-b&&a.y>=c.y-b&&a.z>=c.z-b&&a.x<=d.x+b&&a.y<=d.y+b&&a.z<=d.z+b}},{key:"redistribute",value:function(a){var b=this.children,c=this.points,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0;if(null!==b)for(;c.length>0;)for(g=c.pop(),h=this.data.pop(),d=0,e=b.length;d<e;++d)if(f=b[d],f.contains(g,a)){null===f.points&&(f.points=[],f.data=[]),f.points.push(g),f.data.push(h);break}this.points=null,this.data=null}},{key:"merge",value:function(){var a=this.children,b=void 0,c=void 0,d=void 0;if(null!==a){for(this.points=[],this.data=[],b=0,c=a.length;b<c;++b)if(d=a[b],null!==d.points){var e,f;(e=this.points).push.apply(e,k(d.points)),(f=this.data).push.apply(f,k(d.data))}this.children=null}}},{key:"findNearestPoint",value:function(a,b,c){var d=this.points,e=this.children,f=null,g=b,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0;if(null!==e)for(l=e.map(function(b){return{octant:b,distance:b.distanceToCenterSquared(a)}}).sort(function(a,b){return a.distance-b.distance}),h=0,i=l.length;h<i;++h)m=l[h].octant,m.contains(a,g)&&(n=m.findNearestPoint(a,g,c),null!==n&&(k=n.point.distanceToSquared(a),(!c||k>0)&&k<g&&(g=k,f=n)));else if(null!==d)for(h=0,i=d.length;h<i;++h)j=d[h],k=a.distanceToSquared(j),(!c||k>0)&&k<g&&(g=k,f={point:j.clone(),data:this.data[h]});return f}},{key:"findPoints",value:function(a,b,c,d){var e=this.points,f=this.children,g=b*b,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0;if(null!==f)for(h=0,i=f.length;h<i;++h)l=f[h],l.contains(a,b)&&l.findPoints(a,b,c,d);else if(null!==e)for(h=0,i=e.length;h<i;++h)j=e[h],k=a.distanceToSquared(j),(!c||k>0)&&k<=g&&d.push({point:j.clone(),data:this.data[h]})}}]),b}(m),x=function(a){function b(a,c){var d=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0,e=arguments.length>3&&void 0!==arguments[3]?arguments[3]:8,g=arguments.length>4&&void 0!==arguments[4]?arguments[4]:8;f(this,b);var h=j(this,(b.__proto__||Object.getPrototypeOf(b)).call(this));return h.root=new w(a,c),h.bias=Math.max(0,d),h.biasSquared=h.bias*h.bias,h.maxPoints=Math.max(1,Math.round(e)),h.maxDepth=Math.max(0,Math.round(g)),h}return i(b,a),g(b,[{key:"countPoints",value:function(){return this.root.countPoints()}},{key:"add",value:function(a,b){a=a.clone();var c=[this.root],d=0,e=void 0,f=void 0,g=void 0,h=void 0,i=!1;if(void 0!==b&&null!==b)for(;c.length>0;)if(e=c.pop(),f=e.children,e.contains(a,this.bias))if(c=[],null!==f){var j;(j=c).push.apply(j,k(f)),++d}else{if(null===e.points)e.points=[],e.data=[];else for(g=0,h=e.points.length;!i&&g<h;++g)i=e.points[g].equals(a);if(i)e.data[g-1]=b;else if(e.points.length<this.maxPoints||d===this.maxDepth)e.points.push(a),e.data.push(b);else{var l;e.split(),e.redistribute(this.bias),(l=c).push.apply(l,k(e.children)),++d}}}},{key:"remove",value:function(a){for(var b=[this.root],c=this.root,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,l=void 0;b.length>0;)if(d=b.pop(),e=d.children,d.contains(a,this.bias))if(b=[],null!==e){var m;(m=b).push.apply(m,k(e)),c=d}else if(null!==d.points)for(h=d.points,i=d.data,f=0,g=h.length;f<g;++f)if(j=h[f],j.equals(a)){l=g-1,f<l&&(h[f]=h[l],i[f]=i[l]),h.pop(),i.pop(),c.countPoints()<=this.maxPoints&&c.merge();break}}},{key:"fetch",value:function(a){for(var b=[this.root],c=null,d=void 0,e=void 0,f=void 0,g=void 0,h=void 0;b.length>0;)if(d=b.pop(),e=d.children,d.contains(a,this.bias))if(b=[],null!==e){var i;(i=b).push.apply(i,k(e))}else for(f=0,g=d.points.length;f<g;++f)if(h=d.points[f],a.distanceToSquared(h)<=this.biasSquared){c=d.data[f];break}return c}},{key:"findNearestPoint",value:function(a){var b=arguments.length>1&&void 0!==arguments[1]?arguments[1]:1/0,c=arguments.length>2&&void 0!==arguments[2]&&arguments[2];return this.root.findNearestPoint(a,b,c)}},{key:"findPoints",value:function(a,b){var c=arguments.length>2&&void 0!==arguments[2]&&arguments[2],d=[];return this.root.findPoints(a,b,c,d),d}},{key:"raycast",value:function(a,c){var d=[];h(b.prototype.__proto__||Object.getPrototypeOf(b.prototype),"raycast",this).call(this,a,d),d.length>0&&this.testPoints(d,a,c)}},{key:"testPoints",value:function(a,b,c){var d=b.params.Points.threshold,e=d*d,f=void 0,g=void 0,h=void 0,i=void 0,j=void 0,k=void 0,l=void 0,m=void 0,n=void 0,o=void 0,p=void 0;for(j=0,l=a.length;j<l;++j)if(n=a[j],o=n.points,null!==o)for(k=0,m=o.length;k<m;++k)p=o[k],i=b.ray.distanceSqToPoint(p),i<e&&(f=b.ray.closestPointToPoint(p),g=b.ray.origin.distanceTo(f),g>=b.near&&g<=b.far&&(h=Math.sqrt(i),c.push({distance:g,distanceToRay:h,point:f.clone(),object:n.data[k]})))}}]),b}(u);a.CubicOctant=p,a.EDGES=o,a.Octant=m,a.Octree=u,a.PATTERN=n,a.Raycasting=t,a.OctreeHelper=v,a.Box3=q,a.Vector3=l,a.PointOctant=w,a.PointOctree=x,Object.defineProperty(a,"__esModule",{value:!0})});
{
"name": "sparse-octree",
"version": "2.4.2",
"version": "2.5.0",
"description": "A sparse octree data structure.",

@@ -34,8 +34,13 @@ "homepage": "https://github.com/vanruesc/sparse-octree",

"files": [
"src",
"build"
],
"scripts": {
"test": "node grunt-cli.js"
"test": "grunt"
},
"engines": {
"node": ">= 4.0.0"
"node": ">=4.0.0"
},

@@ -48,3 +53,5 @@

"devDependencies": {
"grunt": ">= 0.4.0",
"babel-preset-es2015-rollup": "1.2.x",
"grunt": "1.x.x",
"grunt-cli": "1.2.x",
"grunt-contrib-nodeunit": "1.0.x",

@@ -56,10 +63,12 @@ "grunt-contrib-uglify": "2.0.x",

"grunt-rollup": "0.8.x",
"babel-preset-es2015-rollup": "1.2.x",
"jit-grunt": "0.10.x",
"load-grunt-config": "0.19.x",
"rollup-plugin-babel": "2.6.x",
"rollup-plugin-node-resolve": "2.0.x",
"rollup-plugin-babel": "2.6.x"
"time-grunt": "1.4.x"
},
"peerDependencies": {
"grunt": ">= 0.4.0"
"grunt": "1.x.x"
}
}

@@ -6,3 +6,3 @@ # Sparse Octree

A sparse octree data structure for three.js.
A sparse octree data structure.

@@ -46,15 +46,5 @@ *[Extensive Demo](http://vanruesc.github.io/sparse-octree/public/index.html) ::

const octree = new Octree(...);
const octree = new Octree();
const octreeHelper = new OctreeHelper(octree);
try {
octreeHelper.update();
} catch(error) {
console.warn(error.message);
}
scene.add(octreeHelper);

@@ -75,3 +65,3 @@ ```

octree.add(new Vector3(0, 0, 0), {});
octree.fetch(new Vector3(0, 0, 0)); // {}
octree.fetch(new Vector3(0, 0, 0)); // => {}
```

@@ -83,4 +73,5 @@

- Base Functionality
- Pointer-based structure
- Handles octant splitting
- Adheres to a common octant layout
- Adheres to a [common octant layout](http://vanruesc.github.io/sparse-octree/docs/classes/Octant.html#property_PATTERN)
- Supports raycasting

@@ -87,0 +78,0 @@ - Supports culling

@@ -0,3 +1,3 @@

import { Vector3 } from "../math/vector3.js";
import { PATTERN } from "./octant.js";
import { Vector3 } from "../vector3.js";

@@ -4,0 +4,0 @@ /**

/**
* Core components.
*
* @module octree
* @module sparse-octree
* @submodule core

@@ -10,4 +10,3 @@ */

export { Octree } from "./octree.js";
export { Octant, PATTERN } from "./octant.js";
export { OctreeHelper } from "./helper.js";
export { Octant, PATTERN, EDGES } from "./octant.js";
export { Raycasting } from "./raycasting.js";

@@ -1,2 +0,2 @@

import { Vector3 } from "../vector3.js";
import { Vector3 } from "../math/vector3.js";

@@ -15,3 +15,3 @@ /**

constructor(min, max) {
constructor(min = new Vector3(), max = new Vector3()) {

@@ -25,3 +25,3 @@ /**

this.min = (min !== undefined) ? min : new Vector3();
this.min = min;

@@ -35,3 +35,3 @@ /**

this.max = (max !== undefined) ? max : new Vector3();
this.max = max;

@@ -180,1 +180,31 @@ /**

/**
* Describes all possible octant corner connections.
*
* @property EDGES
* @type Array
* @static
* @final
*/
export const EDGES = [
// X-Axis.
new Uint8Array([0, 4]),
new Uint8Array([1, 5]),
new Uint8Array([2, 6]),
new Uint8Array([3, 7]),
// Y-Axis.
new Uint8Array([0, 2]),
new Uint8Array([1, 3]),
new Uint8Array([4, 6]),
new Uint8Array([5, 7]),
// Z-Axis.
new Uint8Array([0, 1]),
new Uint8Array([2, 3]),
new Uint8Array([4, 5]),
new Uint8Array([6, 7])
];

@@ -1,2 +0,2 @@

import { Box3 } from "../box3.js";
import { Box3 } from "../math/box3.js";
import { Octant } from "./octant.js";

@@ -3,0 +3,0 @@ import { Raycasting } from "./raycasting.js";

/**
* Exposure of the library components.
*
* @module octree
* @main octree
* @module sparse-octree
* @main sparse-octree
*/

@@ -10,5 +10,5 @@

CubicOctant,
EDGES,
Octant,
Octree,
OctreeHelper,
PATTERN,

@@ -19,4 +19,13 @@ Raycasting

export {
OctreeHelper
} from "./helpers";
export {
Box3,
Vector3
} from "./math";
export {
PointOctant,
PointOctree
} from "./points";
/**
* Point-oriented octree components.
*
* @module octree
* @module sparse-octree
* @submodule points

@@ -6,0 +6,0 @@ */

@@ -1,2 +0,2 @@

import { Octant } from "../core";
import { Octant } from "../core/octant.js";

@@ -3,0 +3,0 @@ /**

@@ -1,2 +0,2 @@

import { Octree } from "../core";
import { Octree } from "../core/octree.js";
import { PointOctant } from "./point-octant.js";

@@ -3,0 +3,0 @@

import { Vector3 } from "./vector3";
/**
* A bounding box.
*
* This class is a copy of THREE.Box3. It can be removed as soon as three.js
* starts supporting ES6 modules.
*
* @class Box3
* @constructor
*/
export class Box3 {
constructor(min, max) {
/**
* The min bounds.
*
* @property min
* @type Vector3
*/
this.min = (min !== undefined) ? min : new Vector3(Infinity, Infinity, Infinity);
/**
* The max bounds.
*
* @property max
* @type Vector3
*/
this.max = (max !== undefined) ? max : new Vector3(-Infinity, -Infinity, -Infinity);
}
/**
* Sets the values of this box.
*
* @method set
* @param {Number} min - The min bounds.
* @param {Number} max - The max bounds.
* @return {Matrix3} This box.
*/
set(min, max) {
this.min.copy(min);
this.max.copy(max);
return this;
}
/**
* Copies the values of a given box.
*
* @method copy
* @param {Matrix3} b - A box.
* @return {Box3} This box.
*/
copy(b) {
this.min.copy(b.min);
this.max.copy(b.max);
return this;
}
/**
* Clones this matrix.
*
* @method clone
* @return {Matrix3} A clone of this matrix.
*/
clone() {
return new this.constructor().copy(this);
}
/**
* Expands this box by the given point.
*
* @method expandByPoint
* @param {Matrix3} p - A point.
* @return {Box3} This box.
*/
expandByPoint(p) {
this.min.min(p);
this.max.max(p);
return this;
}
/**
* Expands this box by combining it with the given one.
*
* @method union
* @param {Box3} b - A box.
* @return {Box3} This box.
*/
union(b) {
this.min.min(b.min);
this.max.max(b.max);
return this;
}
/**
* Defines this box by the given points.
*
* @method setFromPoints
* @param {Array} points - The points.
* @return {Box3} This box.
*/
setFromPoints(points) {
let i, l;
for(i = 0, l = points.length; i < l; ++i) {
this.expandByPoint(points[i]);
}
return this;
}
/**
* Defines this box by the given center and size.
*
* @method setFromCenterAndSize
* @param {Vector3} center - The center.
* @param {Number} size - The size.
* @return {Box3} This box.
*/
setFromCenterAndSize(center, size) {
const halfSize = size.clone().multiplyScalar(0.5);
this.min.copy(center).sub(halfSize);
this.max.copy(center).add(halfSize);
return this;
}
/**
* Checks if this box intersects with the given one.
*
* @method intersectsBox
* @param {Matrix3} box - A box.
* @return {Boolean} Whether the boxes intersect.
*/
intersectsBox(box) {
return !(
box.max.x < this.min.x || box.min.x > this.max.x ||
box.max.y < this.min.y || box.min.y > this.max.y ||
box.max.z < this.min.z || box.min.z > this.max.z
);
}
}
import {
BufferAttribute,
BufferGeometry,
LineSegments,
LineBasicMaterial,
Object3D
} from "three";
/**
* An octree helper.
*
* The update method must be called manually to generate the octree geometry.
*
* @class OctreeHelper
* @submodule core
* @constructor
* @extends Object3D
* @param {Octree} [tree=null] - The octree to visualise.
*/
export class OctreeHelper extends Object3D {
constructor(tree = null) {
super();
this.name = "OctreeHelper";
/**
* The octree.
*
* @property tree
* @type Octree
*/
this.tree = tree;
}
/**
* Creates the octree geometry.
*
* @method update
* @throws {Error} An error is thrown if too many vertices are created.
*/
update() {
const vertexMap = new Map();
const depth = (this.tree !== null) ? this.tree.getDepth() : -1;
const connections = [
/* 0 */ [1, 4],
/* 1 */ [2, 5],
/* 2 */ [3, 6],
/* 3 */ [0, 7],
/* 4 */ [5],
/* 5 */ [6],
/* 6 */ [7],
/* 7 */ [4]
];
let i, j, k, il, kl;
let octants, octant;
let vertices, v, c;
let entry, key;
let geometry, lineSegments, material;
let indexCount;
let indices = null;
let positions = null;
let level = 0;
// Remove existing geometry.
this.dispose();
while(level <= depth) {
octants = this.tree.findOctantsByLevel(level);
indexCount = 0;
vertexMap.clear();
for(i = 0, j = 0, il = octants.length; i < il; ++i) {
octant = octants[i];
vertices = [
/* 0 */ [octant.max.x, octant.max.y, octant.max.z],
/* 1 */ [octant.min.x, octant.max.y, octant.max.z],
/* 2 */ [octant.min.x, octant.min.y, octant.max.z],
/* 3 */ [octant.max.x, octant.min.y, octant.max.z],
/* 4 */ [octant.max.x, octant.max.y, octant.min.z],
/* 5 */ [octant.min.x, octant.max.y, octant.min.z],
/* 6 */ [octant.min.x, octant.min.y, octant.min.z],
/* 7 */ [octant.max.x, octant.min.y, octant.min.z]
];
// Update the vertex map.
for(j = 0; j < 8; ++j) {
v = vertices[j];
c = connections[j];
key = v.toString();
entry = vertexMap.get(key);
// Prevent duplicates.
if(entry !== undefined) {
// Adopt unique connections.
for(k = 0, kl = c.length; k < kl; ++k) {
key = vertices[c[k]].toString();
if(entry.connectionKeys.indexOf(key) < 0) {
entry.connectionKeys.push(key);
++indexCount;
}
}
} else {
// No duplicate, create new entry.
entry = {
position: v,
connectionKeys: [],
index: vertexMap.size
};
for(k = 0, kl = c.length; k < kl; ++k) {
entry.connectionKeys.push(vertices[c[k]].toString());
++indexCount;
}
vertexMap.set(key, entry);
}
}
}
// Create the geometry for this level.
if(vertexMap.size < 65536) {
indices = new Uint16Array(indexCount * 2);
positions = new Float32Array(vertexMap.size * 3);
i = 0; j = 0;
for(entry of vertexMap.values()) {
v = entry.position;
positions[i++] = v[0];
positions[i++] = v[1];
positions[i++] = v[2];
c = entry.connectionKeys;
// Add the index pairs that describe the lines.
for(k = 0, kl = c.length; k < kl; ++k) {
indices[j++] = entry.index;
indices[j++] = vertexMap.get(c[k]).index;
}
}
geometry = new BufferGeometry();
geometry.setIndex(new BufferAttribute(indices, 1));
geometry.addAttribute("position", new BufferAttribute(positions, 3));
material = new LineBasicMaterial({
color: 0xffffff * Math.random()
});
lineSegments = new LineSegments(geometry, material);
this.add(lineSegments);
} else {
throw new Error(
"Could not create geometry for octree depth level " + level +
" (vertex count of " + vertexMap.size + " exceeds limit of 65536)"
);
}
++level;
}
}
/**
* Destroys this helper.
*
* @method dispose
*/
dispose() {
const children = this.children;
let i, l;
for(i = 0, l = children.length; i < l; ++i) {
children[i].geometry.dispose();
children[i].material.dispose();
}
while(children.length > 0) {
children.remove(children[0]);
}
}
}
/**
* A vector with three components.
*
* This class is a copy of THREE.Vector3. It can be removed as soon as three.js
* starts supporting ES6 modules.
*
* @class Vector3
* @constructor
* @param {Number} [x=0] - The x value.
* @param {Number} [y=0] - The y value.
* @param {Number} [z=0] - The z value.
*/
export class Vector3 {
constructor(x, y, z) {
/**
* The x component.
*
* @property x
* @type Number
*/
this.x = x || 0;
/**
* The y component.
*
* @property y
* @type Number
*/
this.y = y || 0;
/**
* The z component.
*
* @property z
* @type Number
*/
this.z = z || 0;
}
/**
* Sets the values of this vector
*
* @method set
* @param {Number} x - The x value.
* @param {Number} y - The y value.
* @param {Number} z - The z value.
* @return {Vector3} This vector.
*/
set(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
return this;
}
/**
* Copies the values of another vector.
*
* @method copy
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
copy(v) {
this.x = v.x;
this.y = v.y;
this.z = v.z;
return this;
}
/**
* Copies values from an array.
*
* @method fromArray
* @param {Array} array - An array.
* @param {Number} offset - An offset.
* @return {Vector3} This vector.
*/
fromArray(array, offset) {
if(offset === undefined) { offset = 0; }
this.x = array[offset];
this.y = array[offset + 1];
this.z = array[offset + 2];
return this;
}
/**
* Stores this vector in an array.
*
* @method toArray
* @param {Array} [array] - A target array.
* @param {Number} offset - An offset.
* @return {Vector3} The array.
*/
toArray(array, offset) {
if(array === undefined) { array = []; }
if(offset === undefined) { offset = 0; }
array[offset] = this.x;
array[offset + 1] = this.y;
array[offset + 2] = this.z;
return array;
}
/**
* Checks if this vector equals the given one.
*
* @method equals
* @param {Vector3} v - A vector.
* @return {Boolean} Whether this vector equals the given one.
*/
equals(v) {
return (v.x === this.x && v.y === this.y && v.z === this.z);
}
/**
* Clones this vector.
*
* @method clone
* @return {Vector3} A clone of this vector.
*/
clone() {
return new this.constructor(this.x, this.y, this.z);
}
/**
* Adds a vector to this one.
*
* @method add
* @param {Vector3} v - The vector to add.
* @return {Vector3} This vector.
*/
add(v) {
this.x += v.x;
this.y += v.y;
this.z += v.z;
return this;
}
/**
* Adds a scalar to this vector.
*
* @method addScalar
* @param {Vector3} s - The scalar to add.
* @return {Vector3} This vector.
*/
addScalar(s) {
this.x += s;
this.y += s;
this.z += s;
return this;
}
/**
* Sets this vector to the sum of two given vectors.
*
* @method addVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
addVectors(a, b) {
this.x = a.x + b.x;
this.y = a.y + b.y;
this.z = a.z + b.z;
return this;
}
/**
* Subtracts a vector from this vector.
*
* @method sub
* @param {Vector3} v - The vector to subtract.
* @return {Vector3} This vector.
*/
sub(v) {
this.x -= v.x;
this.y -= v.y;
this.z -= v.z;
return this;
}
/**
* Subtracts a scalar to this vector.
*
* @method subScalar
* @param {Vector3} s - The scalar to subtract.
* @return {Vector3} This vector.
*/
subScalar(s) {
this.x -= s;
this.y -= s;
this.z -= s;
return this;
}
/**
* Sets this vector to the difference between two given vectors.
*
* @method subVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - A second vector.
* @return {Vector3} This vector.
*/
subVectors(a, b) {
this.x = a.x - b.x;
this.y = a.y - b.y;
this.z = a.z - b.z;
return this;
}
/**
* Multiplies this vector with another vector.
*
* @method multiply
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
multiply(v) {
this.x *= v.x;
this.y *= v.y;
this.z *= v.z;
return this;
}
/**
* Multiplies this vector with a given scalar.
*
* @method multiplyScalar
* @param {Vector3} s - A scalar.
* @return {Vector3} This vector.
*/
multiplyScalar(s) {
if(isFinite(s)) {
this.x *= s;
this.y *= s;
this.z *= s;
} else {
this.x = 0;
this.y = 0;
this.z = 0;
}
return this;
}
/**
* Sets this vector to the product of two given vectors.
*
* @method multiplyVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
multiplyVectors(a, b) {
this.x = a.x * b.x;
this.y = a.y * b.y;
this.z = a.z * b.z;
return this;
}
/**
* Divides this vector by another vector.
*
* @method divide
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
divide(v) {
this.x /= v.x;
this.y /= v.y;
this.z /= v.z;
return this;
}
/**
* Divides this vector by a given scalar.
*
* @method divideScalar
* @param {Vector3} s - A scalar.
* @return {Vector3} This vector.
*/
divideScalar(s) {
return this.multiplyScalar(1 / s);
}
/**
* Sets this vector to the quotient of two given vectors.
*
* @method divideVectors
* @param {Vector3} a - A vector.
* @param {Vector3} b - Another vector.
* @return {Vector3} This vector.
*/
divideVectors(a, b) {
this.x = a.x / b.x;
this.y = a.y / b.y;
this.z = a.z / b.z;
return this;
}
/**
* Calculates the dot product with another vector.
*
* @method dot
* @param {Vector3} v - A vector.
* @return {Number} The dot product.
*/
dot(v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
/**
* Calculates the length squared of this vector.
*
* @method lengthSq
* @return {Vector3} This vector.
*/
lengthSq() {
return this.x * this.x + this.y * this.y + this.z * this.z;
}
/**
* Calculates the length of this vector.
*
* @method length
* @return {Vector3} This vector.
*/
length() {
return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
/**
* Calculates the distance to a given vector.
*
* @method distanceTo
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
distanceTo(v) {
return Math.sqrt(this.distanceToSquared(v));
}
/**
* Calculates the distance squared to a given vector.
*
* @method distanceToSquared
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
distanceToSquared(v) {
const dx = this.x - v.x;
const dy = this.y - v.y;
const dz = this.z - v.z;
return dx * dx + dy * dy + dz * dz;
}
/**
* Normalizes this vector.
*
* @method normalize
* @return {Vector3} This vector.
*/
normalize() {
return this.divideScalar(this.length());
}
/**
* Adopts the min value for each component of this vector and the given one.
*
* @method min
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
min(v) {
this.x = Math.min(this.x, v.x);
this.y = Math.min(this.y, v.y);
this.z = Math.min(this.z, v.z);
return this;
}
/**
* adopts the max value for each component of this vector and the given one.
*
* @method max
* @param {Vector3} v - A vector.
* @return {Vector3} This vector.
*/
max(v) {
this.x = Math.max(this.x, v.x);
this.y = Math.max(this.y, v.y);
this.z = Math.max(this.z, v.z);
return this;
}
/**
* Clamps this vector.
*
* @method clamp
* @param {Vector3} min - A vector, assumed to be smaller than max.
* @param {Vector3} max - A vector, assumed to be greater than min.
* @return {Vector3} This vector.
*/
clamp(min, max) {
this.x = Math.max(min.x, Math.min(max.x, this.x));
this.y = Math.max(min.y, Math.min(max.y, this.y));
this.z = Math.max(min.z, Math.min(max.z, this.z));
return this;
}
/**
* Applies a matrix to this vector.
*
* @method applyMatrix3
* @param {Matrix3} m - A matrix.
* @return {Vector3} This vector.
*/
applyMatrix3(m) {
const x = this.x, y = this.y, z = this.z;
const e = m.elements;
this.x = e[0] * x + e[3] * y + e[6] * z;
this.y = e[1] * x + e[4] * y + e[7] * z;
this.z = e[2] * x + e[5] * y + e[8] * z;
return this;
}
/**
* Applies a matrix to this vector.
*
* @method applyMatrix4
* @param {Matrix4} m - A matrix.
* @return {Vector3} This vector.
*/
applyMatrix4(m) {
const x = this.x, y = this.y, z = this.z;
const e = m.elements;
this.x = e[0] * x + e[4] * y + e[8] * z + e[12];
this.y = e[1] * x + e[5] * y + e[9] * z + e[13];
this.z = e[2] * x + e[6] * y + e[10] * z + e[14];
return this;
}
}

Sorry, the diff of this file is too big to display