Octree.js 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /*!
  2. *
  3. * threeoctree.js (r60) / https://github.com/collinhover/threeoctree
  4. * (sparse) dynamic 3D spatial representation structure for fast searches.
  5. *
  6. * @author Collin Hover / http://collinhover.com/
  7. * based on Dynamic Octree by Piko3D @ http://www.piko3d.com/ and Octree by Marek Pawlowski @ pawlowski.it
  8. *
  9. */
  10. ( function ( THREE ) { "use strict";
  11. /*===================================================
  12. utility
  13. =====================================================*/
  14. function isNumber ( n ) {
  15. return !isNaN( n ) && isFinite( n );
  16. }
  17. function isArray ( target ) {
  18. return Object.prototype.toString.call( target ) === '[object Array]';
  19. }
  20. function toArray ( target ) {
  21. return target ? ( isArray ( target ) !== true ? [ target ] : target ) : [];
  22. }
  23. function indexOfValue( array, value ) {
  24. for ( var i = 0, il = array.length; i < il; i++ ) {
  25. if ( array[ i ] === value ) {
  26. return i;
  27. }
  28. }
  29. return -1;
  30. }
  31. function indexOfPropertyWithValue( array, property, value ) {
  32. for ( var i = 0, il = array.length; i < il; i++ ) {
  33. if ( array[ i ][ property ] === value ) {
  34. return i;
  35. }
  36. }
  37. return -1;
  38. }
  39. /*===================================================
  40. octree
  41. =====================================================*/
  42. THREE.Octree = function ( parameters ) {
  43. // handle parameters
  44. parameters = parameters || {};
  45. parameters.tree = this;
  46. // static properties ( modification is not recommended )
  47. this.nodeCount = 0;
  48. this.INDEX_INSIDE_CROSS = -1;
  49. this.INDEX_OUTSIDE_OFFSET = 2;
  50. this.INDEX_OUTSIDE_POS_X = isNumber( parameters.INDEX_OUTSIDE_POS_X ) ? parameters.INDEX_OUTSIDE_POS_X : 0;
  51. this.INDEX_OUTSIDE_NEG_X = isNumber( parameters.INDEX_OUTSIDE_NEG_X ) ? parameters.INDEX_OUTSIDE_NEG_X : 1;
  52. this.INDEX_OUTSIDE_POS_Y = isNumber( parameters.INDEX_OUTSIDE_POS_Y ) ? parameters.INDEX_OUTSIDE_POS_Y : 2;
  53. this.INDEX_OUTSIDE_NEG_Y = isNumber( parameters.INDEX_OUTSIDE_NEG_Y ) ? parameters.INDEX_OUTSIDE_NEG_Y : 3;
  54. this.INDEX_OUTSIDE_POS_Z = isNumber( parameters.INDEX_OUTSIDE_POS_Z ) ? parameters.INDEX_OUTSIDE_POS_Z : 4;
  55. this.INDEX_OUTSIDE_NEG_Z = isNumber( parameters.INDEX_OUTSIDE_NEG_Z ) ? parameters.INDEX_OUTSIDE_NEG_Z : 5;
  56. this.INDEX_OUTSIDE_MAP = [];
  57. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_POS_X ] = { index: this.INDEX_OUTSIDE_POS_X, count: 0, x: 1, y: 0, z: 0 };
  58. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_NEG_X ] = { index: this.INDEX_OUTSIDE_NEG_X, count: 0, x: -1, y: 0, z: 0 };
  59. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_POS_Y ] = { index: this.INDEX_OUTSIDE_POS_Y, count: 0, x: 0, y: 1, z: 0 };
  60. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_NEG_Y ] = { index: this.INDEX_OUTSIDE_NEG_Y, count: 0, x: 0, y: -1, z: 0 };
  61. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_POS_Z ] = { index: this.INDEX_OUTSIDE_POS_Z, count: 0, x: 0, y: 0, z: 1 };
  62. this.INDEX_OUTSIDE_MAP[ this.INDEX_OUTSIDE_NEG_Z ] = { index: this.INDEX_OUTSIDE_NEG_Z, count: 0, x: 0, y: 0, z: -1 };
  63. this.FLAG_POS_X = 1 << ( this.INDEX_OUTSIDE_POS_X + 1 );
  64. this.FLAG_NEG_X = 1 << ( this.INDEX_OUTSIDE_NEG_X + 1 );
  65. this.FLAG_POS_Y = 1 << ( this.INDEX_OUTSIDE_POS_Y + 1 );
  66. this.FLAG_NEG_Y = 1 << ( this.INDEX_OUTSIDE_NEG_Y + 1 );
  67. this.FLAG_POS_Z = 1 << ( this.INDEX_OUTSIDE_POS_Z + 1 );
  68. this.FLAG_NEG_Z = 1 << ( this.INDEX_OUTSIDE_NEG_Z + 1 );
  69. this.utilVec31Search = new THREE.Vector3();
  70. this.utilVec32Search = new THREE.Vector3();
  71. // pass scene to see octree structure
  72. this.scene = parameters.scene;
  73. if ( this.scene ) {
  74. this.visualGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  75. this.visualMaterial = new THREE.MeshBasicMaterial( { color: 0xFF0066, wireframe: true, wireframeLinewidth: 1 } );
  76. }
  77. // properties
  78. this.objects = [];
  79. this.objectsMap = {};
  80. this.objectsData = [];
  81. this.objectsDeferred = [];
  82. this.depthMax = isNumber( parameters.depthMax ) ? parameters.depthMax : Infinity;
  83. this.objectsThreshold = isNumber( parameters.objectsThreshold ) ? parameters.objectsThreshold : 8;
  84. this.overlapPct = isNumber( parameters.overlapPct ) ? parameters.overlapPct : 0.15;
  85. this.undeferred = parameters.undeferred || false;
  86. this.root = parameters.root instanceof THREE.OctreeNode ? parameters.root : new THREE.OctreeNode( parameters );
  87. };
  88. THREE.Octree.prototype = {
  89. update: function () {
  90. // add any deferred objects that were waiting for render cycle
  91. if ( this.objectsDeferred.length > 0 ) {
  92. for ( var i = 0, il = this.objectsDeferred.length; i < il; i++ ) {
  93. var deferred = this.objectsDeferred[ i ];
  94. this.addDeferred( deferred.object, deferred.options );
  95. }
  96. this.objectsDeferred.length = 0;
  97. }
  98. },
  99. add: function ( object, options ) {
  100. // add immediately
  101. if ( this.undeferred ) {
  102. this.updateObject( object );
  103. this.addDeferred( object, options );
  104. } else {
  105. // defer add until update called
  106. this.objectsDeferred.push( { object: object, options: options } );
  107. }
  108. },
  109. addDeferred: function ( object, options ) {
  110. var i, l,
  111. geometry,
  112. faces,
  113. useFaces,
  114. vertices,
  115. useVertices,
  116. objectData;
  117. // ensure object is not object data
  118. if ( object instanceof THREE.OctreeObjectData ) {
  119. object = object.object;
  120. }
  121. // check uuid to avoid duplicates
  122. if ( !object.uuid ) {
  123. object.uuid = THREE.Math.generateUUID();
  124. }
  125. if ( !this.objectsMap[ object.uuid ] ) {
  126. // store
  127. this.objects.push( object );
  128. this.objectsMap[ object.uuid ] = object;
  129. // check options
  130. if ( options ) {
  131. useFaces = options.useFaces;
  132. useVertices = options.useVertices;
  133. }
  134. if ( useVertices === true ) {
  135. geometry = object.geometry;
  136. vertices = geometry.vertices;
  137. for ( i = 0, l = vertices.length; i < l; i++ ) {
  138. this.addObjectData( object, vertices[ i ] );
  139. }
  140. } else if ( useFaces === true ) {
  141. geometry = object.geometry;
  142. faces = geometry.faces;
  143. for ( i = 0, l = faces.length; i < l; i++ ) {
  144. this.addObjectData( object, faces[ i ] );
  145. }
  146. } else {
  147. this.addObjectData( object );
  148. }
  149. }
  150. },
  151. addObjectData: function ( object, part ) {
  152. var objectData = new THREE.OctreeObjectData( object, part );
  153. // add to tree objects data list
  154. this.objectsData.push( objectData );
  155. // add to nodes
  156. this.root.addObject( objectData );
  157. },
  158. remove: function ( object ) {
  159. var i, l,
  160. objectData = object,
  161. index,
  162. objectsDataRemoved;
  163. // ensure object is not object data for index search
  164. if ( object instanceof THREE.OctreeObjectData ) {
  165. object = object.object;
  166. }
  167. // check uuid
  168. if ( this.objectsMap[ object.uuid ] ) {
  169. this.objectsMap[ object.uuid ] = undefined;
  170. // check and remove from objects, nodes, and data lists
  171. index = indexOfValue( this.objects, object );
  172. if ( index !== -1 ) {
  173. this.objects.splice( index, 1 );
  174. // remove from nodes
  175. objectsDataRemoved = this.root.removeObject( objectData );
  176. // remove from objects data list
  177. for ( i = 0, l = objectsDataRemoved.length; i < l; i++ ) {
  178. objectData = objectsDataRemoved[ i ];
  179. index = indexOfValue( this.objectsData, objectData );
  180. if ( index !== -1 ) {
  181. this.objectsData.splice( index, 1 );
  182. }
  183. }
  184. }
  185. } else if ( this.objectsDeferred.length > 0 ) {
  186. // check and remove from deferred
  187. index = indexOfPropertyWithValue( this.objectsDeferred, 'object', object );
  188. if ( index !== -1 ) {
  189. this.objectsDeferred.splice( index, 1 );
  190. }
  191. }
  192. },
  193. extend: function ( octree ) {
  194. var i, l,
  195. objectsData,
  196. objectData;
  197. if ( octree instanceof THREE.Octree ) {
  198. // for each object data
  199. objectsData = octree.objectsData;
  200. for ( i = 0, l = objectsData.length; i < l; i++ ) {
  201. objectData = objectsData[ i ];
  202. this.add( objectData, { useFaces: objectData.faces, useVertices: objectData.vertices } );
  203. }
  204. }
  205. },
  206. rebuild: function () {
  207. var i, l,
  208. node,
  209. object,
  210. objectData,
  211. indexOctant,
  212. indexOctantLast,
  213. objectsUpdate = [];
  214. // check all object data for changes in position
  215. // assumes all object matrices are up to date
  216. for ( i = 0, l = this.objectsData.length; i < l; i++ ) {
  217. objectData = this.objectsData[ i ];
  218. node = objectData.node;
  219. // update object
  220. objectData.update();
  221. // if position has changed since last organization of object in tree
  222. if ( node instanceof THREE.OctreeNode && !objectData.positionLast.equals( objectData.position ) ) {
  223. // get octant index of object within current node
  224. indexOctantLast = objectData.indexOctant;
  225. indexOctant = node.getOctantIndex( objectData );
  226. // if object octant index has changed
  227. if ( indexOctant !== indexOctantLast ) {
  228. // add to update list
  229. objectsUpdate.push( objectData );
  230. }
  231. }
  232. }
  233. // update changed objects
  234. for ( i = 0, l = objectsUpdate.length; i < l; i++ ) {
  235. objectData = objectsUpdate[ i ];
  236. // remove object from current node
  237. objectData.node.removeObject( objectData );
  238. // add object to tree root
  239. this.root.addObject( objectData );
  240. }
  241. },
  242. updateObject: function ( object ) {
  243. var i, l,
  244. parentCascade = [ object ],
  245. parent,
  246. parentUpdate;
  247. // search all parents between object and root for world matrix update
  248. parent = object.parent;
  249. while( parent ) {
  250. parentCascade.push( parent );
  251. parent = parent.parent;
  252. }
  253. for ( i = 0, l = parentCascade.length; i < l; i++ ) {
  254. parent = parentCascade[ i ];
  255. if ( parent.matrixWorldNeedsUpdate === true ) {
  256. parentUpdate = parent;
  257. }
  258. }
  259. // update world matrix starting at uppermost parent that needs update
  260. if ( typeof parentUpdate !== 'undefined' ) {
  261. parentUpdate.updateMatrixWorld();
  262. }
  263. },
  264. search: function ( position, radius, organizeByObject, direction ) {
  265. var i, l,
  266. node,
  267. objects,
  268. objectData,
  269. object,
  270. results,
  271. resultData,
  272. resultsObjectsIndices,
  273. resultObjectIndex,
  274. directionPct;
  275. // add root objects
  276. objects = [].concat( this.root.objects );
  277. // ensure radius (i.e. distance of ray) is a number
  278. if ( !( radius > 0 ) ) {
  279. radius = Number.MAX_VALUE;
  280. }
  281. // if direction passed, normalize and find pct
  282. if ( direction instanceof THREE.Vector3 ) {
  283. direction = this.utilVec31Search.copy( direction ).normalize();
  284. directionPct = this.utilVec32Search.set( 1, 1, 1 ).divide( direction );
  285. }
  286. // search each node of root
  287. for ( i = 0, l = this.root.nodesIndices.length; i < l; i++ ) {
  288. node = this.root.nodesByIndex[ this.root.nodesIndices[ i ] ];
  289. objects = node.search( position, radius, objects, direction, directionPct );
  290. }
  291. // if should organize results by object
  292. if ( organizeByObject === true ) {
  293. results = [];
  294. resultsObjectsIndices = [];
  295. // for each object data found
  296. for ( i = 0, l = objects.length; i < l; i++ ) {
  297. objectData = objects[ i ];
  298. object = objectData.object;
  299. resultObjectIndex = indexOfValue( resultsObjectsIndices, object );
  300. // if needed, create new result data
  301. if ( resultObjectIndex === -1 ) {
  302. resultData = {
  303. object: object,
  304. faces: [],
  305. vertices: []
  306. };
  307. results.push( resultData );
  308. resultsObjectsIndices.push( object );
  309. } else {
  310. resultData = results[ resultObjectIndex ];
  311. }
  312. // object data has faces or vertices, add to list
  313. if ( objectData.faces ) {
  314. resultData.faces.push( objectData.faces );
  315. } else if ( objectData.vertices ) {
  316. resultData.vertices.push( objectData.vertices );
  317. }
  318. }
  319. } else {
  320. results = objects;
  321. }
  322. return results;
  323. },
  324. setRoot: function ( root ) {
  325. if ( root instanceof THREE.OctreeNode ) {
  326. // store new root
  327. this.root = root;
  328. // update properties
  329. this.root.updateProperties();
  330. }
  331. },
  332. getDepthEnd: function () {
  333. return this.root.getDepthEnd();
  334. },
  335. getNodeCountEnd: function () {
  336. return this.root.getNodeCountEnd();
  337. },
  338. getObjectCountEnd: function () {
  339. return this.root.getObjectCountEnd();
  340. },
  341. toConsole: function () {
  342. this.root.toConsole();
  343. }
  344. };
  345. /*===================================================
  346. object data
  347. =====================================================*/
  348. THREE.OctreeObjectData = function ( object, part ) {
  349. // properties
  350. this.object = object;
  351. // handle part by type
  352. if ( part instanceof THREE.Face3 ) {
  353. this.faces = part;
  354. this.face3 = true;
  355. this.utilVec31FaceBounds = new THREE.Vector3();
  356. } else if ( part instanceof THREE.Vector3 ) {
  357. this.vertices = part;
  358. }
  359. this.radius = 0;
  360. this.position = new THREE.Vector3();
  361. // initial update
  362. if ( this.object instanceof THREE.Object3D ) {
  363. this.update();
  364. }
  365. this.positionLast = this.position.clone();
  366. };
  367. THREE.OctreeObjectData.prototype = {
  368. update: function () {
  369. if ( this.face3 ) {
  370. this.radius = this.getFace3BoundingRadius( this.object, this.faces );
  371. this.position.copy( this.faces.centroid ).applyMatrix4( this.object.matrixWorld );
  372. } else if ( this.vertices ) {
  373. this.radius = this.object.material.size || 1;
  374. this.position.copy( this.vertices ).applyMatrix4( this.object.matrixWorld );
  375. } else {
  376. if ( this.object.geometry ) {
  377. if ( this.object.geometry.boundingSphere === null ) {
  378. this.object.geometry.computeBoundingSphere();
  379. }
  380. this.radius = this.object.geometry.boundingSphere.radius;
  381. this.position.copy( this.object.geometry.boundingSphere.center ).applyMatrix4( this.object.matrixWorld );
  382. } else {
  383. this.radius = this.object.boundRadius;
  384. this.position.setFromMatrixPosition( this.object.matrixWorld );
  385. }
  386. }
  387. this.radius = this.radius * Math.max( this.object.scale.x, this.object.scale.y, this.object.scale.z );
  388. },
  389. getFace3BoundingRadius: function ( object, face ) {
  390. if ( face.centroid === undefined ) face.centroid = new THREE.Vector3();
  391. var geometry = object.geometry || object,
  392. vertices = geometry.vertices,
  393. centroid = face.centroid,
  394. va = vertices[ face.a ], vb = vertices[ face.b ], vc = vertices[ face.c ],
  395. centroidToVert = this.utilVec31FaceBounds,
  396. radius;
  397. centroid.addVectors( va, vb ).add( vc ).divideScalar( 3 );
  398. radius = Math.max( centroidToVert.subVectors( centroid, va ).length(), centroidToVert.subVectors( centroid, vb ).length(), centroidToVert.subVectors( centroid, vc ).length() );
  399. return radius;
  400. }
  401. };
  402. /*===================================================
  403. node
  404. =====================================================*/
  405. THREE.OctreeNode = function ( parameters ) {
  406. // utility
  407. this.utilVec31Branch = new THREE.Vector3();
  408. this.utilVec31Expand = new THREE.Vector3();
  409. this.utilVec31Ray = new THREE.Vector3();
  410. // handle parameters
  411. parameters = parameters || {};
  412. // store or create tree
  413. if ( parameters.tree instanceof THREE.Octree ) {
  414. this.tree = parameters.tree;
  415. } else if ( parameters.parent instanceof THREE.OctreeNode !== true ) {
  416. parameters.root = this;
  417. this.tree = new THREE.Octree( parameters );
  418. }
  419. // basic properties
  420. this.id = this.tree.nodeCount++;
  421. this.position = parameters.position instanceof THREE.Vector3 ? parameters.position : new THREE.Vector3();
  422. this.radius = parameters.radius > 0 ? parameters.radius : 1;
  423. this.indexOctant = parameters.indexOctant;
  424. this.depth = 0;
  425. // reset and assign parent
  426. this.reset();
  427. this.setParent( parameters.parent );
  428. // additional properties
  429. this.overlap = this.radius * this.tree.overlapPct;
  430. this.radiusOverlap = this.radius + this.overlap;
  431. this.left = this.position.x - this.radiusOverlap;
  432. this.right = this.position.x + this.radiusOverlap;
  433. this.bottom = this.position.y - this.radiusOverlap;
  434. this.top = this.position.y + this.radiusOverlap;
  435. this.back = this.position.z - this.radiusOverlap;
  436. this.front = this.position.z + this.radiusOverlap;
  437. // visual
  438. if ( this.tree.scene ) {
  439. this.visual = new THREE.Mesh( this.tree.visualGeometry, this.tree.visualMaterial );
  440. this.visual.scale.set( this.radiusOverlap * 2, this.radiusOverlap * 2, this.radiusOverlap * 2 );
  441. this.visual.position.copy( this.position );
  442. this.tree.scene.add( this.visual );
  443. }
  444. };
  445. THREE.OctreeNode.prototype = {
  446. setParent: function ( parent ) {
  447. // store new parent
  448. if ( parent !== this && this.parent !== parent ) {
  449. this.parent = parent;
  450. // update properties
  451. this.updateProperties();
  452. }
  453. },
  454. updateProperties: function () {
  455. var i, l;
  456. // properties
  457. if ( this.parent instanceof THREE.OctreeNode ) {
  458. this.tree = this.parent.tree;
  459. this.depth = this.parent.depth + 1;
  460. } else {
  461. this.depth = 0;
  462. }
  463. // cascade
  464. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  465. this.nodesByIndex[ this.nodesIndices[ i ] ].updateProperties();
  466. }
  467. },
  468. reset: function ( cascade, removeVisual ) {
  469. var i, l,
  470. node,
  471. nodesIndices = this.nodesIndices || [],
  472. nodesByIndex = this.nodesByIndex;
  473. this.objects = [];
  474. this.nodesIndices = [];
  475. this.nodesByIndex = {};
  476. // unset parent in nodes
  477. for ( i = 0, l = nodesIndices.length; i < l; i++ ) {
  478. node = nodesByIndex[ nodesIndices[ i ] ];
  479. node.setParent( undefined );
  480. if ( cascade === true ) {
  481. node.reset( cascade, removeVisual );
  482. }
  483. }
  484. // visual
  485. if ( removeVisual === true && this.visual && this.visual.parent ) {
  486. this.visual.parent.remove( this.visual );
  487. }
  488. },
  489. addNode: function ( node, indexOctant ) {
  490. node.indexOctant = indexOctant;
  491. if ( indexOfValue( this.nodesIndices, indexOctant ) === -1 ) {
  492. this.nodesIndices.push( indexOctant );
  493. }
  494. this.nodesByIndex[ indexOctant ] = node;
  495. if ( node.parent !== this ) {
  496. node.setParent( this );
  497. }
  498. },
  499. removeNode: function ( indexOctant ) {
  500. var index,
  501. node;
  502. index = indexOfValue( this.nodesIndices, indexOctant );
  503. this.nodesIndices.splice( index, 1 );
  504. node = node || this.nodesByIndex[ indexOctant ];
  505. delete this.nodesByIndex[ indexOctant ];
  506. if ( node.parent === this ) {
  507. node.setParent( undefined );
  508. }
  509. },
  510. addObject: function ( object ) {
  511. var index,
  512. indexOctant,
  513. node;
  514. // get object octant index
  515. indexOctant = this.getOctantIndex( object );
  516. // if object fully contained by an octant, add to subtree
  517. if ( indexOctant > -1 && this.nodesIndices.length > 0 ) {
  518. node = this.branch( indexOctant );
  519. node.addObject( object );
  520. } else if ( indexOctant < -1 && this.parent instanceof THREE.OctreeNode ) {
  521. // if object lies outside bounds, add to parent node
  522. this.parent.addObject( object );
  523. } else {
  524. // add to this objects list
  525. index = indexOfValue( this.objects, object );
  526. if ( index === -1 ) {
  527. this.objects.push( object );
  528. }
  529. // node reference
  530. object.node = this;
  531. // check if need to expand, split, or both
  532. this.checkGrow();
  533. }
  534. },
  535. addObjectWithoutCheck: function ( objects ) {
  536. var i, l,
  537. object;
  538. for ( i = 0, l = objects.length; i < l; i++ ) {
  539. object = objects[ i ];
  540. this.objects.push( object );
  541. object.node = this;
  542. }
  543. },
  544. removeObject: function ( object ) {
  545. var i, l,
  546. nodesRemovedFrom,
  547. removeData;
  548. // cascade through tree to find and remove object
  549. removeData = this.removeObjectRecursive( object, { searchComplete: false, nodesRemovedFrom: [], objectsDataRemoved: [] } );
  550. // if object removed, try to shrink the nodes it was removed from
  551. nodesRemovedFrom = removeData.nodesRemovedFrom;
  552. if ( nodesRemovedFrom.length > 0 ) {
  553. for ( i = 0, l = nodesRemovedFrom.length; i < l; i++ ) {
  554. nodesRemovedFrom[ i ].shrink();
  555. }
  556. }
  557. return removeData.objectsDataRemoved;
  558. },
  559. removeObjectRecursive: function ( object, removeData ) {
  560. var i, l,
  561. index = -1,
  562. objectData,
  563. node,
  564. objectRemoved;
  565. // find index of object in objects list
  566. // search and remove object data (fast)
  567. if ( object instanceof THREE.OctreeObjectData ) {
  568. // remove from this objects list
  569. index = indexOfValue( this.objects, object );
  570. if ( index !== -1 ) {
  571. this.objects.splice( index, 1 );
  572. object.node = undefined;
  573. removeData.objectsDataRemoved.push( object );
  574. removeData.searchComplete = objectRemoved = true;
  575. }
  576. } else {
  577. // search each object data for object and remove (slow)
  578. for ( i = this.objects.length - 1; i >= 0; i-- ) {
  579. objectData = this.objects[ i ];
  580. if ( objectData.object === object ) {
  581. this.objects.splice( i, 1 );
  582. objectData.node = undefined;
  583. removeData.objectsDataRemoved.push( objectData );
  584. objectRemoved = true;
  585. if ( !objectData.faces && !objectData.vertices ) {
  586. removeData.searchComplete = true;
  587. break;
  588. }
  589. }
  590. }
  591. }
  592. // if object data removed and this is not on nodes removed from
  593. if ( objectRemoved === true ) {
  594. removeData.nodesRemovedFrom.push( this );
  595. }
  596. // if search not complete, search nodes
  597. if ( removeData.searchComplete !== true ) {
  598. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  599. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  600. // try removing object from node
  601. removeData = node.removeObjectRecursive( object, removeData );
  602. if ( removeData.searchComplete === true ) {
  603. break;
  604. }
  605. }
  606. }
  607. return removeData;
  608. },
  609. checkGrow: function () {
  610. // if object count above max
  611. if ( this.objects.length > this.tree.objectsThreshold && this.tree.objectsThreshold > 0 ) {
  612. this.grow();
  613. }
  614. },
  615. grow: function () {
  616. var indexOctant,
  617. object,
  618. objectsExpand = [],
  619. objectsExpandOctants = [],
  620. objectsSplit = [],
  621. objectsSplitOctants = [],
  622. objectsRemaining = [],
  623. i, l;
  624. // for each object
  625. for ( i = 0, l = this.objects.length; i < l; i++ ) {
  626. object = this.objects[ i ];
  627. // get object octant index
  628. indexOctant = this.getOctantIndex( object );
  629. // if lies within octant
  630. if ( indexOctant > -1 ) {
  631. objectsSplit.push( object );
  632. objectsSplitOctants.push( indexOctant );
  633. } else if ( indexOctant < -1 ) {
  634. // lies outside radius
  635. objectsExpand.push( object );
  636. objectsExpandOctants.push( indexOctant );
  637. } else {
  638. // lies across bounds between octants
  639. objectsRemaining.push( object );
  640. }
  641. }
  642. // if has objects to split
  643. if ( objectsSplit.length > 0) {
  644. objectsRemaining = objectsRemaining.concat( this.split( objectsSplit, objectsSplitOctants ) );
  645. }
  646. // if has objects to expand
  647. if ( objectsExpand.length > 0) {
  648. objectsRemaining = objectsRemaining.concat( this.expand( objectsExpand, objectsExpandOctants ) );
  649. }
  650. // store remaining
  651. this.objects = objectsRemaining;
  652. // merge check
  653. this.checkMerge();
  654. },
  655. split: function ( objects, octants ) {
  656. var i, l,
  657. indexOctant,
  658. object,
  659. node,
  660. objectsRemaining;
  661. // if not at max depth
  662. if ( this.depth < this.tree.depthMax ) {
  663. objects = objects || this.objects;
  664. octants = octants || [];
  665. objectsRemaining = [];
  666. // for each object
  667. for ( i = 0, l = objects.length; i < l; i++ ) {
  668. object = objects[ i ];
  669. // get object octant index
  670. indexOctant = octants[ i ];
  671. // if object contained by octant, branch this tree
  672. if ( indexOctant > -1 ) {
  673. node = this.branch( indexOctant );
  674. node.addObject( object );
  675. } else {
  676. objectsRemaining.push( object );
  677. }
  678. }
  679. // if all objects, set remaining as new objects
  680. if ( objects === this.objects ) {
  681. this.objects = objectsRemaining;
  682. }
  683. } else {
  684. objectsRemaining = this.objects;
  685. }
  686. return objectsRemaining;
  687. },
  688. branch: function ( indexOctant ) {
  689. var node,
  690. overlap,
  691. radius,
  692. radiusOffset,
  693. offset,
  694. position;
  695. // node exists
  696. if ( this.nodesByIndex[ indexOctant ] instanceof THREE.OctreeNode ) {
  697. node = this.nodesByIndex[ indexOctant ];
  698. } else {
  699. // properties
  700. radius = ( this.radiusOverlap ) * 0.5;
  701. overlap = radius * this.tree.overlapPct;
  702. radiusOffset = radius - overlap;
  703. offset = this.utilVec31Branch.set( indexOctant & 1 ? radiusOffset : -radiusOffset, indexOctant & 2 ? radiusOffset : -radiusOffset, indexOctant & 4 ? radiusOffset : -radiusOffset );
  704. position = new THREE.Vector3().addVectors( this.position, offset );
  705. // node
  706. node = new THREE.OctreeNode( {
  707. tree: this.tree,
  708. parent: this,
  709. position: position,
  710. radius: radius,
  711. indexOctant: indexOctant
  712. } );
  713. // store
  714. this.addNode( node, indexOctant );
  715. }
  716. return node;
  717. },
  718. expand: function ( objects, octants ) {
  719. var i, l,
  720. object,
  721. objectsRemaining,
  722. objectsExpand,
  723. indexOctant,
  724. flagsOutside,
  725. indexOutside,
  726. indexOctantInverse,
  727. iom = this.tree.INDEX_OUTSIDE_MAP,
  728. indexOutsideCounts,
  729. infoIndexOutside1,
  730. infoIndexOutside2,
  731. infoIndexOutside3,
  732. indexOutsideBitwise1,
  733. indexOutsideBitwise2,
  734. infoPotential1,
  735. infoPotential2,
  736. infoPotential3,
  737. indexPotentialBitwise1,
  738. indexPotentialBitwise2,
  739. octantX, octantY, octantZ,
  740. overlap,
  741. radius,
  742. radiusOffset,
  743. radiusParent,
  744. overlapParent,
  745. offset = this.utilVec31Expand,
  746. position,
  747. parent;
  748. // handle max depth down tree
  749. if ( this.tree.root.getDepthEnd() < this.tree.depthMax ) {
  750. objects = objects || this.objects;
  751. octants = octants || [];
  752. objectsRemaining = [];
  753. objectsExpand = [];
  754. // reset counts
  755. for ( i = 0, l = iom.length; i < l; i++ ) {
  756. iom[ i ].count = 0;
  757. }
  758. // for all outside objects, find outside octants containing most objects
  759. for ( i = 0, l = objects.length; i < l; i++ ) {
  760. object = objects[ i ];
  761. // get object octant index
  762. indexOctant = octants[ i ] ;
  763. // if object outside this, include in calculations
  764. if ( indexOctant < -1 ) {
  765. // convert octant index to outside flags
  766. flagsOutside = -indexOctant - this.tree.INDEX_OUTSIDE_OFFSET;
  767. // check against bitwise flags
  768. // x
  769. if ( flagsOutside & this.tree.FLAG_POS_X ) {
  770. iom[ this.tree.INDEX_OUTSIDE_POS_X ].count++;
  771. } else if ( flagsOutside & this.tree.FLAG_NEG_X ) {
  772. iom[ this.tree.INDEX_OUTSIDE_NEG_X ].count++;
  773. }
  774. // y
  775. if ( flagsOutside & this.tree.FLAG_POS_Y ) {
  776. iom[ this.tree.INDEX_OUTSIDE_POS_Y ].count++;
  777. } else if ( flagsOutside & this.tree.FLAG_NEG_Y ) {
  778. iom[ this.tree.INDEX_OUTSIDE_NEG_Y ].count++;
  779. }
  780. // z
  781. if ( flagsOutside & this.tree.FLAG_POS_Z ) {
  782. iom[ this.tree.INDEX_OUTSIDE_POS_Z ].count++;
  783. } else if ( flagsOutside & this.tree.FLAG_NEG_Z ) {
  784. iom[ this.tree.INDEX_OUTSIDE_NEG_Z ].count++;
  785. }
  786. // store in expand list
  787. objectsExpand.push( object );
  788. } else {
  789. objectsRemaining.push( object );
  790. }
  791. }
  792. // if objects to expand
  793. if ( objectsExpand.length > 0 ) {
  794. // shallow copy index outside map
  795. indexOutsideCounts = iom.slice( 0 );
  796. // sort outside index count so highest is first
  797. indexOutsideCounts.sort( function ( a, b ) {
  798. return b.count - a.count;
  799. } );
  800. // get highest outside indices
  801. // first is first
  802. infoIndexOutside1 = indexOutsideCounts[ 0 ];
  803. indexOutsideBitwise1 = infoIndexOutside1.index | 1;
  804. // second is ( one of next two bitwise OR 1 ) that is not opposite of ( first bitwise OR 1 )
  805. infoPotential1 = indexOutsideCounts[ 1 ];
  806. infoPotential2 = indexOutsideCounts[ 2 ];
  807. infoIndexOutside2 = ( infoPotential1.index | 1 ) !== indexOutsideBitwise1 ? infoPotential1 : infoPotential2;
  808. indexOutsideBitwise2 = infoIndexOutside2.index | 1;
  809. // third is ( one of next three bitwise OR 1 ) that is not opposite of ( first or second bitwise OR 1 )
  810. infoPotential1 = indexOutsideCounts[ 2 ];
  811. infoPotential2 = indexOutsideCounts[ 3 ];
  812. infoPotential3 = indexOutsideCounts[ 4 ];
  813. indexPotentialBitwise1 = infoPotential1.index | 1;
  814. indexPotentialBitwise2 = infoPotential2.index | 1;
  815. infoIndexOutside3 = indexPotentialBitwise1 !== indexOutsideBitwise1 && indexPotentialBitwise1 !== indexOutsideBitwise2 ? infoPotential1 : indexPotentialBitwise2 !== indexOutsideBitwise1 && indexPotentialBitwise2 !== indexOutsideBitwise2 ? infoPotential2 : infoPotential3;
  816. // get this octant normal based on outside octant indices
  817. octantX = infoIndexOutside1.x + infoIndexOutside2.x + infoIndexOutside3.x;
  818. octantY = infoIndexOutside1.y + infoIndexOutside2.y + infoIndexOutside3.y;
  819. octantZ = infoIndexOutside1.z + infoIndexOutside2.z + infoIndexOutside3.z;
  820. // get this octant indices based on octant normal
  821. indexOctant = this.getOctantIndexFromPosition( octantX, octantY, octantZ );
  822. indexOctantInverse = this.getOctantIndexFromPosition( -octantX, -octantY, -octantZ );
  823. // properties
  824. overlap = this.overlap;
  825. radius = this.radius;
  826. // radius of parent comes from reversing overlap of this, unless overlap percent is 0
  827. radiusParent = this.tree.overlapPct > 0 ? overlap / ( ( 0.5 * this.tree.overlapPct ) * ( 1 + this.tree.overlapPct ) ) : radius * 2;
  828. overlapParent = radiusParent * this.tree.overlapPct;
  829. // parent offset is difference between radius + overlap of parent and child
  830. radiusOffset = ( radiusParent + overlapParent ) - ( radius + overlap );
  831. offset.set( indexOctant & 1 ? radiusOffset : -radiusOffset, indexOctant & 2 ? radiusOffset : -radiusOffset, indexOctant & 4 ? radiusOffset : -radiusOffset );
  832. position = new THREE.Vector3().addVectors( this.position, offset );
  833. // parent
  834. parent = new THREE.OctreeNode( {
  835. tree: this.tree,
  836. position: position,
  837. radius: radiusParent
  838. } );
  839. // set self as node of parent
  840. parent.addNode( this, indexOctantInverse );
  841. // set parent as root
  842. this.tree.setRoot( parent );
  843. // add all expand objects to parent
  844. for ( i = 0, l = objectsExpand.length; i < l; i++ ) {
  845. this.tree.root.addObject( objectsExpand[ i ] );
  846. }
  847. }
  848. // if all objects, set remaining as new objects
  849. if ( objects === this.objects ) {
  850. this.objects = objectsRemaining;
  851. }
  852. } else {
  853. objectsRemaining = objects;
  854. }
  855. return objectsRemaining;
  856. },
  857. shrink: function () {
  858. // merge check
  859. this.checkMerge();
  860. // contract check
  861. this.tree.root.checkContract();
  862. },
  863. checkMerge: function () {
  864. var nodeParent = this,
  865. nodeMerge;
  866. // traverse up tree as long as node + entire subtree's object count is under minimum
  867. while ( nodeParent.parent instanceof THREE.OctreeNode && nodeParent.getObjectCountEnd() < this.tree.objectsThreshold ) {
  868. nodeMerge = nodeParent;
  869. nodeParent = nodeParent.parent;
  870. }
  871. // if parent node is not this, merge entire subtree into merge node
  872. if ( nodeParent !== this ) {
  873. nodeParent.merge( nodeMerge );
  874. }
  875. },
  876. merge: function ( nodes ) {
  877. var i, l,
  878. j, k,
  879. node;
  880. // handle nodes
  881. nodes = toArray( nodes );
  882. for ( i = 0, l = nodes.length; i < l; i++ ) {
  883. node = nodes[ i ];
  884. // gather node + all subtree objects
  885. this.addObjectWithoutCheck( node.getObjectsEnd() );
  886. // reset node + entire subtree
  887. node.reset( true, true );
  888. // remove node
  889. this.removeNode( node.indexOctant, node );
  890. }
  891. // merge check
  892. this.checkMerge();
  893. },
  894. checkContract: function () {
  895. var i, l,
  896. node,
  897. nodeObjectsCount,
  898. nodeHeaviest,
  899. nodeHeaviestObjectsCount,
  900. outsideHeaviestObjectsCount;
  901. // find node with highest object count
  902. if ( this.nodesIndices.length > 0 ) {
  903. nodeHeaviestObjectsCount = 0;
  904. outsideHeaviestObjectsCount = this.objects.length;
  905. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  906. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  907. nodeObjectsCount = node.getObjectCountEnd();
  908. outsideHeaviestObjectsCount += nodeObjectsCount;
  909. if ( nodeHeaviest instanceof THREE.OctreeNode === false || nodeObjectsCount > nodeHeaviestObjectsCount ) {
  910. nodeHeaviest = node;
  911. nodeHeaviestObjectsCount = nodeObjectsCount;
  912. }
  913. }
  914. // subtract heaviest count from outside count
  915. outsideHeaviestObjectsCount -= nodeHeaviestObjectsCount;
  916. // if should contract
  917. if ( outsideHeaviestObjectsCount < this.tree.objectsThreshold && nodeHeaviest instanceof THREE.OctreeNode ) {
  918. this.contract( nodeHeaviest );
  919. }
  920. }
  921. },
  922. contract: function ( nodeRoot ) {
  923. var i, l,
  924. node;
  925. // handle all nodes
  926. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  927. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  928. // if node is not new root
  929. if ( node !== nodeRoot ) {
  930. // add node + all subtree objects to root
  931. nodeRoot.addObjectWithoutCheck( node.getObjectsEnd() );
  932. // reset node + entire subtree
  933. node.reset( true, true );
  934. }
  935. }
  936. // add own objects to root
  937. nodeRoot.addObjectWithoutCheck( this.objects );
  938. // reset self
  939. this.reset( false, true );
  940. // set new root
  941. this.tree.setRoot( nodeRoot );
  942. // contract check on new root
  943. nodeRoot.checkContract();
  944. },
  945. getOctantIndex: function ( objectData ) {
  946. var i, l,
  947. positionObj,
  948. radiusObj,
  949. position = this.position,
  950. radiusOverlap = this.radiusOverlap,
  951. overlap = this.overlap,
  952. deltaX, deltaY, deltaZ,
  953. distX, distY, distZ,
  954. distance,
  955. indexOctant = 0;
  956. // handle type
  957. if ( objectData instanceof THREE.OctreeObjectData ) {
  958. radiusObj = objectData.radius;
  959. positionObj = objectData.position;
  960. // update object data position last
  961. objectData.positionLast.copy( positionObj );
  962. } else if ( objectData instanceof THREE.OctreeNode ) {
  963. positionObj = objectData.position;
  964. radiusObj = 0;
  965. }
  966. // find delta and distance
  967. deltaX = positionObj.x - position.x;
  968. deltaY = positionObj.y - position.y;
  969. deltaZ = positionObj.z - position.z;
  970. distX = Math.abs( deltaX );
  971. distY = Math.abs( deltaY );
  972. distZ = Math.abs( deltaZ );
  973. distance = Math.max( distX, distY, distZ );
  974. // if outside, use bitwise flags to indicate on which sides object is outside of
  975. if ( distance + radiusObj > radiusOverlap ) {
  976. // x
  977. if ( distX + radiusObj > radiusOverlap ) {
  978. indexOctant = indexOctant ^ ( deltaX > 0 ? this.tree.FLAG_POS_X : this.tree.FLAG_NEG_X );
  979. }
  980. // y
  981. if ( distY + radiusObj > radiusOverlap ) {
  982. indexOctant = indexOctant ^ ( deltaY > 0 ? this.tree.FLAG_POS_Y : this.tree.FLAG_NEG_Y );
  983. }
  984. // z
  985. if ( distZ + radiusObj > radiusOverlap ) {
  986. indexOctant = indexOctant ^ ( deltaZ > 0 ? this.tree.FLAG_POS_Z : this.tree.FLAG_NEG_Z );
  987. }
  988. objectData.indexOctant = -indexOctant - this.tree.INDEX_OUTSIDE_OFFSET;
  989. return objectData.indexOctant;
  990. }
  991. // return octant index from delta xyz
  992. if ( deltaX - radiusObj > -overlap ) {
  993. // x right
  994. indexOctant = indexOctant | 1;
  995. } else if ( !( deltaX + radiusObj < overlap ) ) {
  996. // x left
  997. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  998. return objectData.indexOctant;
  999. }
  1000. if ( deltaY - radiusObj > -overlap ) {
  1001. // y right
  1002. indexOctant = indexOctant | 2;
  1003. } else if ( !( deltaY + radiusObj < overlap ) ) {
  1004. // y left
  1005. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  1006. return objectData.indexOctant;
  1007. }
  1008. if ( deltaZ - radiusObj > -overlap ) {
  1009. // z right
  1010. indexOctant = indexOctant | 4;
  1011. } else if ( !( deltaZ + radiusObj < overlap ) ) {
  1012. // z left
  1013. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  1014. return objectData.indexOctant;
  1015. }
  1016. objectData.indexOctant = indexOctant;
  1017. return objectData.indexOctant;
  1018. },
  1019. getOctantIndexFromPosition: function ( x, y, z ) {
  1020. var indexOctant = 0;
  1021. if ( x > 0 ) {
  1022. indexOctant = indexOctant | 1;
  1023. }
  1024. if ( y > 0 ) {
  1025. indexOctant = indexOctant | 2;
  1026. }
  1027. if ( z > 0 ) {
  1028. indexOctant = indexOctant | 4;
  1029. }
  1030. return indexOctant;
  1031. },
  1032. search: function ( position, radius, objects, direction, directionPct ) {
  1033. var i, l,
  1034. node,
  1035. intersects;
  1036. // test intersects by parameters
  1037. if ( direction ) {
  1038. intersects = this.intersectRay( position, direction, radius, directionPct );
  1039. } else {
  1040. intersects = this.intersectSphere( position, radius );
  1041. }
  1042. // if intersects
  1043. if ( intersects === true ) {
  1044. // gather objects
  1045. objects = objects.concat( this.objects );
  1046. // search subtree
  1047. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1048. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1049. objects = node.search( position, radius, objects, direction );
  1050. }
  1051. }
  1052. return objects;
  1053. },
  1054. intersectSphere: function ( position, radius ) {
  1055. var distance = radius * radius,
  1056. px = position.x,
  1057. py = position.y,
  1058. pz = position.z;
  1059. if ( px < this.left ) {
  1060. distance -= Math.pow( px - this.left, 2 );
  1061. } else if ( px > this.right ) {
  1062. distance -= Math.pow( px - this.right, 2 );
  1063. }
  1064. if ( py < this.bottom ) {
  1065. distance -= Math.pow( py - this.bottom, 2 );
  1066. } else if ( py > this.top ) {
  1067. distance -= Math.pow( py - this.top, 2 );
  1068. }
  1069. if ( pz < this.back ) {
  1070. distance -= Math.pow( pz - this.back, 2 );
  1071. } else if ( pz > this.front ) {
  1072. distance -= Math.pow( pz - this.front, 2 );
  1073. }
  1074. return distance >= 0;
  1075. },
  1076. intersectRay: function ( origin, direction, distance, directionPct ) {
  1077. if ( typeof directionPct === 'undefined' ) {
  1078. directionPct = this.utilVec31Ray.set( 1, 1, 1 ).divide( direction );
  1079. }
  1080. var t1 = ( this.left - origin.x ) * directionPct.x,
  1081. t2 = ( this.right - origin.x ) * directionPct.x,
  1082. t3 = ( this.bottom - origin.y ) * directionPct.y,
  1083. t4 = ( this.top - origin.y ) * directionPct.y,
  1084. t5 = ( this.back - origin.z ) * directionPct.z,
  1085. t6 = ( this.front - origin.z ) * directionPct.z,
  1086. tmax = Math.min( Math.min( Math.max( t1, t2), Math.max( t3, t4) ), Math.max( t5, t6) ),
  1087. tmin;
  1088. // ray would intersect in reverse direction, i.e. this is behind ray
  1089. if (tmax < 0)
  1090. {
  1091. return false;
  1092. }
  1093. tmin = Math.max( Math.max( Math.min( t1, t2), Math.min( t3, t4)), Math.min( t5, t6));
  1094. // if tmin > tmax or tmin > ray distance, ray doesn't intersect AABB
  1095. if( tmin > tmax || tmin > distance ) {
  1096. return false;
  1097. }
  1098. return true;
  1099. },
  1100. getDepthEnd: function ( depth ) {
  1101. var i, l,
  1102. node;
  1103. if ( this.nodesIndices.length > 0 ) {
  1104. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1105. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1106. depth = node.getDepthEnd( depth );
  1107. }
  1108. } else {
  1109. depth = !depth || this.depth > depth ? this.depth : depth;
  1110. }
  1111. return depth;
  1112. },
  1113. getNodeCountEnd: function () {
  1114. return this.tree.root.getNodeCountRecursive() + 1;
  1115. },
  1116. getNodeCountRecursive: function () {
  1117. var i, l,
  1118. count = this.nodesIndices.length;
  1119. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1120. count += this.nodesByIndex[ this.nodesIndices[ i ] ].getNodeCountRecursive();
  1121. }
  1122. return count;
  1123. },
  1124. getObjectsEnd: function ( objects ) {
  1125. var i, l,
  1126. node;
  1127. objects = ( objects || [] ).concat( this.objects );
  1128. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1129. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1130. objects = node.getObjectsEnd( objects );
  1131. }
  1132. return objects;
  1133. },
  1134. getObjectCountEnd: function () {
  1135. var i, l,
  1136. count = this.objects.length;
  1137. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1138. count += this.nodesByIndex[ this.nodesIndices[ i ] ].getObjectCountEnd();
  1139. }
  1140. return count;
  1141. },
  1142. getObjectCountStart: function () {
  1143. var count = this.objects.length,
  1144. parent = this.parent;
  1145. while( parent instanceof THREE.OctreeNode ) {
  1146. count += parent.objects.length;
  1147. parent = parent.parent;
  1148. }
  1149. return count;
  1150. },
  1151. toConsole: function ( space ) {
  1152. var i, l,
  1153. node,
  1154. spaceAddition = ' ';
  1155. space = typeof space === 'string' ? space : spaceAddition;
  1156. console.log( ( this.parent ? space + ' octree NODE > ' : ' octree ROOT > ' ), this, ' // id: ', this.id, ' // indexOctant: ', this.indexOctant, ' // position: ', this.position.x, this.position.y, this.position.z, ' // radius: ', this.radius, ' // depth: ', this.depth );
  1157. console.log( ( this.parent ? space + ' ' : ' ' ), '+ objects ( ', this.objects.length, ' ) ', this.objects );
  1158. console.log( ( this.parent ? space + ' ' : ' ' ), '+ children ( ', this.nodesIndices.length, ' )', this.nodesIndices, this.nodesByIndex );
  1159. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1160. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1161. node.toConsole( space + spaceAddition );
  1162. }
  1163. }
  1164. };
  1165. /*===================================================
  1166. raycaster additional functionality
  1167. =====================================================*/
  1168. THREE.Raycaster.prototype.intersectOctreeObject = function ( object, recursive ) {
  1169. var intersects,
  1170. octreeObject,
  1171. facesAll,
  1172. facesSearch;
  1173. if ( object.object instanceof THREE.Object3D ) {
  1174. octreeObject = object;
  1175. object = octreeObject.object;
  1176. // temporarily replace object geometry's faces with octree object faces
  1177. facesSearch = octreeObject.faces;
  1178. facesAll = object.geometry.faces;
  1179. if ( facesSearch.length > 0 ) {
  1180. object.geometry.faces = facesSearch;
  1181. }
  1182. // intersect
  1183. intersects = this.intersectObject( object, recursive );
  1184. // revert object geometry's faces
  1185. if ( facesSearch.length > 0 ) {
  1186. object.geometry.faces = facesAll;
  1187. }
  1188. } else {
  1189. intersects = this.intersectObject( object, recursive );
  1190. }
  1191. return intersects;
  1192. };
  1193. THREE.Raycaster.prototype.intersectOctreeObjects = function ( objects, recursive ) {
  1194. var i, il,
  1195. intersects = [];
  1196. for ( i = 0, il = objects.length; i < il; i++ ) {
  1197. intersects = intersects.concat( this.intersectOctreeObject( objects[ i ], recursive ) );
  1198. }
  1199. return intersects;
  1200. };
  1201. }( THREE ) );