rbush-3d
Version:
High-performance 3D spatial index for cuboids (based on R*-tree with bulk loading and bulk insertion algorithms)
2 lines (1 loc) • 9.18 kB
JavaScript
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n;n="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,n.RBush3D=t()}}(function(){var t;return function(){function t(n,i,r){function e(o,h){if(!i[o]){if(!n[o]){var s="function"==typeof require&&require;if(!h&&s)return s(o,!0);if(a)return a(o,!0);var u=new Error("Cannot find module '"+o+"'");throw u.code="MODULE_NOT_FOUND",u}var f=i[o]={exports:{}};n[o][0].call(f.exports,function(t){return e(n[o][1][t]||t)},f,f.exports,t,n,i,r)}return i[o].exports}for(var a="function"==typeof require&&require,o=0;o<r.length;o++)e(r[o]);return e}return t}()({1:[function(t,n,i){"use strict";Object.defineProperty(i,"__esModule",{value:!0});var r=t("quickselect"),e=[],a=function(t){return e.push(t)},o=function(t){t&&(a(t),m(t)||t.children.forEach(o))},h=function(t){var n=e.pop();return n?(n.children=t,n.height=1,n.leaf=!0,n.minX=1/0,n.minY=1/0,n.minZ=1/0,n.maxX=-1/0,n.maxY=-1/0,n.maxZ=-1/0):n={children:t,height:1,leaf:!0,minX:1/0,minY:1/0,minZ:1/0,maxX:-1/0,maxY:-1/0,maxZ:-1/0},n},s=[],u=function(t){return s.push(t)},f=function(t,n){var i=s.pop();return i?(i.dist=t,i.node=n):i={dist:t,node:n},i},m=function(t){return t.leaf},c=function(t,n){return t.leaf},l=function(t,n,i){if(!i)return n.indexOf(t);for(var r=0;r<n.length;r++)if(i(t,n[r]))return r;return-1},p=function(t){d(t,0,t.children.length,t)},d=function(t,n,i,r){var e=r;e?(e.minX=1/0,e.minY=1/0,e.minZ=1/0,e.maxX=-1/0,e.maxY=-1/0,e.maxZ=-1/0):e=h([]);for(var a=n,o=void 0;a<i;a++)o=t.children[a],x(e,o);return e},x=function(t,n){return t.minX=Math.min(t.minX,n.minX),t.minY=Math.min(t.minY,n.minY),t.minZ=Math.min(t.minZ,n.minZ),t.maxX=Math.max(t.maxX,n.maxX),t.maxY=Math.max(t.maxY,n.maxY),t.maxZ=Math.max(t.maxZ,n.maxZ),t},v=function(t){return(t.maxX-t.minX)*(t.maxY-t.minY)*(t.maxZ-t.minZ)},M=function(t){return t.maxX-t.minX+(t.maxY-t.minY)+(t.maxZ-t.minZ)},g=function(t,n){var i=Math.min(t.minX,n.minX),r=Math.min(t.minY,n.minY),e=Math.min(t.minZ,n.minZ);return(Math.max(t.maxX,n.maxX)-i)*(Math.max(t.maxY,n.maxY)-r)*(Math.max(t.maxZ,n.maxZ)-e)},y=function(t,n){var i=Math.max(t.minX,n.minX),r=Math.max(t.minY,n.minY),e=Math.max(t.minZ,n.minZ),a=Math.min(t.maxX,n.maxX),o=Math.min(t.maxY,n.maxY),h=Math.min(t.maxZ,n.maxZ);return Math.max(0,a-i)*Math.max(0,o-r)*Math.max(0,h-e)},X=function(t,n){return t.minX<=n.minX&&t.minY<=n.minY&&t.minZ<=n.minZ&&n.maxX<=t.maxX&&n.maxY<=t.maxY&&n.maxZ<=t.maxZ};i.intersects=function(t,n){return n.minX<=t.maxX&&n.minY<=t.maxY&&n.minZ<=t.maxZ&&n.maxX>=t.minX&&n.maxY>=t.minY&&n.maxZ>=t.minZ},i.boxRayIntersects=function(t,n,i,r,e,a,o){var h=(t.minX-n)*e,s=(t.maxX-n)*e,u=(t.minY-i)*a,f=(t.maxY-i)*a,m=(t.minZ-r)*o,c=(t.maxZ-r)*o,l=Math.min(m,c),p=Math.max(m,c),d=Math.min(u,f),x=Math.max(u,f),v=Math.min(h,s),M=Math.max(h,s),g=Math.max(0,v,d,l);return Math.min(M,x,p)>=g?g:1/0};var Y=function(t,n,i,e,a){for(var o,h=[n,i];h.length;)i=h.pop(),n=h.pop(),i-n<=e||(o=n+Math.ceil((i-n)/e/2)*e,r(t,o,n,i,a),h.push(n,o,o,i))},Z=function(t,n){return t.minX-n.minX},b=function(t,n){return t.minY-n.minY},w=function(t,n){return t.minZ-n.minZ},E=function(){function t(t){void 0===t&&(t=16),this.maxEntries=Math.max(t,8),this.minEntries=Math.max(4,Math.ceil(.4*this.maxEntries)),this.clear()}return t.alloc=function(){return this.pool.pop()||new this},t.free=function(t){t.clear(),this.pool.push(t)},t.prototype.search=function(t){var n=this.data,r=[];if(!i.intersects(t,n))return r;for(var e=[];n;){for(var a=0,o=n.children.length;a<o;a++){var h=n.children[a];i.intersects(t,h)&&(c(n)?r.push(h):X(t,h)?this._all(h,r):e.push(h))}n=e.pop()}return r},t.prototype.collides=function(t){var n=this.data;if(!i.intersects(t,n))return!1;for(var r=[];n;){for(var e=0,a=n.children.length;e<a;e++){var o=n.children[e];if(i.intersects(t,o)){if(c(n)||X(t,o))return!0;r.push(o)}}n=r.pop()}return!1},t.prototype.raycastInv=function(t,n,r,e,a,o,h){void 0===h&&(h=1/0);var s=this.data;if(e===1/0&&a===1/0&&o===1/0)return f(1/0,void 0);if(i.boxRayIntersects(s,t,n,r,e,a,o)===1/0)return f(1/0,void 0);for(var m,l=[f(0,s)],p=function(t,n){var i=l[t];l[t]=l[n],l[n]=i},d=h;l.length&&l[0].dist<d;){s=function(){var t=l[0],n=l.length-1;l[0]=l[n],l.length=n;for(var i=0;;){var r=i<<1|1;if(r>=n)break;var e=r+1;if(e<n&&l[e].dist<l[r].dist&&(r=e),l[i].dist<l[r].dist)break;p(i,r),i=r}return u(t),t.node}();for(var x=0,v=s.children.length;x<v;x++){var M=s.children[x],g=i.boxRayIntersects(M,t,n,r,e,a,o);if(c(s)){if(g<d){if(0===g)return f(g,M);d=g,m=M}}else!function(t,n){var i=l.length;for(l.push(f(t,n));i>0;){var r=i-1>>1;if(l[r].dist<=l[i].dist)break;p(i,r),i=r}}(g,M)}}return f(d<h?d:1/0,m)},t.prototype.raycast=function(t,n,i,r,e,a,o){return void 0===o&&(o=1/0),this.raycastInv(t,n,i,1/r,1/e,1/a,o)},t.prototype.all=function(){return this._all(this.data,[])},t.prototype.load=function(t){if(!t||!t.length)return this;if(t.length<this.minEntries){for(var n=0,i=t.length;n<i;n++)this.insert(t[n]);return this}var r=this.build(t.slice(),0,t.length-1,0);if(this.data.children.length)if(this.data.height===r.height)this.splitRoot(this.data,r);else{if(this.data.height<r.height){var e=this.data;this.data=r,r=e}this._insert(r,this.data.height-r.height-1,!0)}else this.data=r;return this},t.prototype.insert=function(t){return t&&this._insert(t,this.data.height-1),this},t.prototype.clear=function(){return this.data&&o(this.data),this.data=h([]),this},t.prototype.remove=function(t,n){if(!t)return this;for(var i,r,e=this.data,a=0,o=!1,h=[],s=[];e||h.length;){if(e||(e=h.pop(),a=s.pop(),r=h[h.length-1],o=!0),m(e)&&-1!==(i=l(t,e.children,n)))return e.children.splice(i,1),h.push(e),this.condense(h),this;o||m(e)||!X(e,t)?r?(a++,e=r.children[a],o=!1):e=void 0:(h.push(e),s.push(a),a=0,r=e,e=e.children[0])}return this},t.prototype.toJSON=function(){return this.data},t.prototype.fromJSON=function(t){return o(this.data),this.data=t,this},t.prototype.build=function(t,n,i,r){var e,a=i-n+1,o=this.maxEntries;if(a<=o)return e=h(t.slice(n,i+1)),p(e),e;r||(r=Math.ceil(Math.log(a)/Math.log(o)),o=Math.ceil(a/Math.pow(o,r-1))),e=h([]),e.leaf=!1,e.height=r;var s=Math.ceil(a/o),u=s*Math.ceil(Math.pow(o,2/3)),f=s*Math.ceil(Math.pow(o,1/3));Y(t,n,i,f,Z);for(var m=n;m<=i;m+=f){var c=Math.min(m+f-1,i);Y(t,m,c,u,b);for(var l=m;l<=c;l+=u){var d=Math.min(l+u-1,c);Y(t,l,d,s,w);for(var x=l;x<=d;x+=s){var v=Math.min(x+s-1,d);e.children.push(this.build(t,x,v,r-1))}}}return p(e),e},t.prototype._all=function(t,n){for(var i=[];t;)m(t)?n.push.apply(n,t.children):i.push.apply(i,t.children),t=i.pop();return n},t.prototype.chooseSubtree=function(t,n,i,r){for(var e,a,o;;){if(r.push(n),m(n)||r.length-1===i)break;e=a=1/0;for(var h=0,s=n.children.length;h<s;h++){var u=n.children[h],f=v(u),c=g(t,u)-f;c<a?(a=c,e=f<e?f:e,o=u):c===a&&f<e&&(e=f,o=u)}n=o||n.children[0]}return n},t.prototype.split=function(t,n){var i=t[n],r=i.children.length,e=this.minEntries;this.chooseSplitAxis(i,e,r);var a=this.chooseSplitIndex(i,e,r),o=h(i.children.splice(a,i.children.length-a));o.height=i.height,o.leaf=i.leaf,p(i),p(o),n?t[n-1].children.push(o):this.splitRoot(i,o)},t.prototype.splitRoot=function(t,n){this.data=h([t,n]),this.data.height=t.height+1,this.data.leaf=!1,p(this.data)},t.prototype.chooseSplitIndex=function(t,n,i){for(var r,e=1/0,a=1/0,o=n;o<=i-n;o++){var h=d(t,0,o),s=d(t,o,i),u=y(h,s),f=v(h)+v(s);u<e?(e=u,r=o,a=f<a?f:a):u===e&&f<a&&(a=f,r=o)}return r},t.prototype.chooseSplitAxis=function(t,n,i){var r=this.allDistMargin(t,n,i,Z),e=this.allDistMargin(t,n,i,b),a=this.allDistMargin(t,n,i,w);r<e&&r<a?t.children.sort(Z):e<r&&e<a&&t.children.sort(b)},t.prototype.allDistMargin=function(t,n,i,r){t.children.sort(r);for(var e=d(t,0,n),a=d(t,i-n,i),o=M(e)+M(a),h=n;h<i-n;h++){var s=t.children[h];x(e,s),o+=M(e)}for(var h=i-n-1;h>=n;h--){var s=t.children[h];x(a,s),o+=M(a)}return o},t.prototype.adjustParentBBoxes=function(t,n,i){for(var r=i;r>=0;r--)x(n[r],t)},t.prototype.condense=function(t){for(var n=t.length-1,i=void 0;n>=0;n--)0===t[n].children.length?n>0?(i=t[n-1].children,i.splice(i.indexOf(t[n]),1),a(t[n])):this.clear():p(t[n])},t.prototype._insert=function(t,n,i){var r=[],e=this.chooseSubtree(t,this.data,n,r);for(e.children.push(t),x(e,t);n>=0&&r[n].children.length>this.maxEntries;)this.split(r,n),n--;this.adjustParentBBoxes(t,r,n)},t.pool=[],t}();i.RBush3D=E},{quickselect:2}],2:[function(n,i,r){!function(n,e){"object"==typeof r&&void 0!==i?i.exports=e():"function"==typeof t&&t.amd?t(e):n.quickselect=e()}(this,function(){"use strict";function t(t,i,e,a,o){n(t,i,e||0,a||t.length-1,o||r)}function n(t,r,e,a,o){for(;a>e;){if(a-e>600){var h=a-e+1,s=r-e+1,u=Math.log(h),f=.5*Math.exp(2*u/3),m=.5*Math.sqrt(u*f*(h-f)/h)*(s-h/2<0?-1:1);n(t,r,Math.max(e,Math.floor(r-s*f/h+m)),Math.min(a,Math.floor(r+(h-s)*f/h+m)),o)}var c=t[r],l=e,p=a;for(i(t,e,r),o(t[a],c)>0&&i(t,e,a);l<p;){for(i(t,l,p),l++,p--;o(t[l],c)<0;)l++;for(;o(t[p],c)>0;)p--}0===o(t[e],c)?i(t,e,p):(p++,i(t,p,a)),p<=r&&(e=p+1),r<=p&&(a=p-1)}}function i(t,n,i){var r=t[n];t[n]=t[i],t[i]=r}function r(t,n){return t<n?-1:t>n?1:0}return t})},{}]},{},[1])(1)});