Octree.js 43 KB

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