Octree.js 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056
  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. radius;
  323. // handle face type
  324. if ( face instanceof THREE.Face4 ) {
  325. vd = vertices[ face.d ];
  326. centroid.addVectors( va, vb ).add( vc ).add( vd ).divideScalar( 4 );
  327. radius = Math.max( centroidToVert.subVectors( centroid, va ).length(), centroidToVert.subVectors( centroid, vb ).length(), centroidToVert.subVectors( centroid, vc ).length(), centroidToVert.subVectors( centroid, vd ).length() );
  328. }
  329. else {
  330. centroid.addVectors( va, vb ).add( vc ).divideScalar( 3 );
  331. radius = Math.max( centroidToVert.subVectors( centroid, va ).length(), centroidToVert.subVectors( centroid, vb ).length(), centroidToVert.subVectors( centroid, vc ).length() );
  332. }
  333. return radius;
  334. },
  335. usesFaces: function () {
  336. return this.faces instanceof THREE.Face3 || this.faces instanceof THREE.Face4;
  337. }
  338. };
  339. /*===================================================
  340. node
  341. =====================================================*/
  342. THREE.OctreeNode = function ( parameters ) {
  343. // utility
  344. this.utilVec31Branch = new THREE.Vector3();
  345. this.utilVec31Expand = new THREE.Vector3();
  346. this.utilVec31Ray = new THREE.Vector3();
  347. // handle parameters
  348. parameters = parameters || {};
  349. // store or create tree
  350. if ( parameters.tree instanceof THREE.Octree ) {
  351. this.tree = parameters.tree;
  352. }
  353. else if ( parameters.parent instanceof THREE.OctreeNode !== true ) {
  354. parameters.root = this;
  355. this.tree = new THREE.Octree( parameters );
  356. }
  357. // basic properties
  358. this.id = this.tree.nodeCount++;
  359. this.position = parameters.position instanceof THREE.Vector3 ? parameters.position : new THREE.Vector3();
  360. this.radius = isNumber( parameters.radius ) && parameters.radius > 0 ? parameters.radius : 1;
  361. this.indexOctant = parameters.indexOctant;
  362. this.depth = 0;
  363. // reset and assign parent
  364. this.reset();
  365. this.setParent( parameters.parent );
  366. // additional properties
  367. this.overlap = this.radius * this.tree.overlapPct;
  368. this.radiusOverlap = this.radius + this.overlap;
  369. this.left = this.position.x - this.radiusOverlap;
  370. this.right = this.position.x + this.radiusOverlap;
  371. this.bottom = this.position.y - this.radiusOverlap;
  372. this.top = this.position.y + this.radiusOverlap;
  373. this.back = this.position.z - this.radiusOverlap;
  374. this.front = this.position.z + this.radiusOverlap;
  375. // visual
  376. if ( this.tree.scene ) {
  377. 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 } ) );
  378. this.visual.position.copy( this.position );
  379. this.tree.scene.add( this.visual );
  380. }
  381. };
  382. THREE.OctreeNode.prototype = {
  383. setParent: function ( parent ) {
  384. // store new parent
  385. if ( parent !== this && this.parent !== parent ) {
  386. this.parent = parent;
  387. // update properties
  388. this.updateProperties();
  389. }
  390. },
  391. updateProperties: function () {
  392. var i, l;
  393. // properties
  394. if ( this.parent instanceof THREE.OctreeNode ) {
  395. this.tree = this.parent.tree;
  396. this.depth = this.parent.depth + 1;
  397. }
  398. else {
  399. this.depth = 0;
  400. }
  401. // cascade
  402. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  403. this.nodesByIndex[ this.nodesIndices[ i ] ].updateProperties();
  404. }
  405. },
  406. reset: function ( cascade, removeVisual ) {
  407. var i, l,
  408. node,
  409. nodesIndices = this.nodesIndices || [],
  410. nodesByIndex = this.nodesByIndex;
  411. this.objects = [];
  412. this.nodesIndices = [];
  413. this.nodesByIndex = {};
  414. // unset parent in nodes
  415. for ( i = 0, l = nodesIndices.length; i < l; i++ ) {
  416. node = nodesByIndex[ nodesIndices[ i ] ];
  417. node.setParent( undefined );
  418. if ( cascade === true ) {
  419. node.reset( cascade, removeVisual );
  420. }
  421. }
  422. // visual
  423. if ( removeVisual === true && this.visual && this.visual.parent ) {
  424. this.visual.parent.remove( this.visual );
  425. }
  426. },
  427. addNode: function ( node, indexOctant ) {
  428. indexOctant = node.indexOctant = isNumber( indexOctant ) ? indexOctant : isNumber( node.indexOctant ) ? node.indexOctant : this.getOctantIndex( node );
  429. if ( this.nodesIndices.indexOf( indexOctant ) === -1 ) {
  430. this.nodesIndices.push( indexOctant );
  431. }
  432. this.nodesByIndex[ indexOctant ] = node;
  433. if ( node.parent !== this ) {
  434. node.setParent( this );
  435. }
  436. },
  437. removeNode: function ( identifier ) {
  438. var indexOctant = -1,
  439. index,
  440. node;
  441. // if identifier is node
  442. if ( identifier instanceof THREE.OctreeNode && this.nodesByIndex[ identifier.indexOctant ] === identifier ) {
  443. node = identifier;
  444. indexOctant = node.indexOctant;
  445. }
  446. // if identifier is number
  447. else if ( isNumber( identifier ) ) {
  448. indexOctant = identifier;
  449. }
  450. // else search all nodes for identifier (slow)
  451. else {
  452. for ( index in this.nodesByIndex ) {
  453. node = this.nodesByIndex[ index ];
  454. if ( node === identifier ) {
  455. indexOctant = index;
  456. break;
  457. }
  458. }
  459. }
  460. // if indexOctant found
  461. if ( indexOctant !== -1 ) {
  462. index = this.nodesIndices.indexOf( indexOctant );
  463. this.nodesIndices.splice( index, 1 );
  464. node = node || this.nodesByIndex[ indexOctant ];
  465. delete this.nodesByIndex[ indexOctant ];
  466. if ( node.parent === this ) {
  467. node.setParent( undefined );
  468. }
  469. }
  470. },
  471. addObject: function ( object ) {
  472. var index,
  473. indexOctant,
  474. node;
  475. // get object octant index
  476. indexOctant = this.getOctantIndex( object );
  477. // if object fully contained by an octant, add to subtree
  478. if ( indexOctant > -1 && this.nodesIndices.length > 0 ) {
  479. node = this.branch( indexOctant );
  480. node.addObject( object );
  481. }
  482. // if object lies outside bounds, add to parent node
  483. else if ( indexOctant < -1 && this.parent instanceof THREE.OctreeNode ) {
  484. this.parent.addObject( object );
  485. }
  486. // else add to self
  487. else {
  488. // add to this objects list
  489. index = this.objects.indexOf( object );
  490. if ( index === -1 ) {
  491. this.objects.push( object );
  492. }
  493. // node reference
  494. object.node = this;
  495. // check if need to expand, split, or both
  496. this.checkGrow();
  497. }
  498. },
  499. addObjectWithoutCheck: function ( objects ) {
  500. var i, l,
  501. object;
  502. for ( i = 0, l = objects.length; i < l; i++ ) {
  503. object = objects[ i ];
  504. this.objects.push( object );
  505. object.node = this;
  506. }
  507. },
  508. removeObject: function ( object ) {
  509. var i, l,
  510. nodesRemovedFrom,
  511. removeData;
  512. // cascade through tree to find and remove object
  513. removeData = this.removeObjectRecursive( object, { searchComplete: false, nodesRemovedFrom: [], objectsDataRemoved: [] } );
  514. // if object removed, try to shrink the nodes it was removed from
  515. nodesRemovedFrom = removeData.nodesRemovedFrom;
  516. if ( nodesRemovedFrom.length > 0 ) {
  517. for ( i = 0, l = nodesRemovedFrom.length; i < l; i++ ) {
  518. nodesRemovedFrom[ i ].shrink();
  519. }
  520. }
  521. return removeData.objectsDataRemoved;
  522. },
  523. removeObjectRecursive: function ( object, removeData ) {
  524. var i, l,
  525. index = -1,
  526. objectData,
  527. node,
  528. objectRemoved;
  529. // find index of object in objects list
  530. // search and remove object data (fast)
  531. if ( object instanceof THREE.OctreeObjectData ) {
  532. // remove from this objects list
  533. index = this.objects.indexOf( object );
  534. if ( index !== -1 ) {
  535. this.objects.splice( index, 1 );
  536. object.node = undefined;
  537. removeData.objectsDataRemoved.push( object );
  538. removeData.searchComplete = objectRemoved = true;
  539. }
  540. }
  541. // search each object data for object and remove (slow)
  542. else {
  543. for ( i = this.objects.length - 1; i >= 0; i-- ) {
  544. objectData = this.objects[ i ];
  545. if ( objectData.object === object ) {
  546. this.objects.splice( i, 1 );
  547. objectData.node = undefined;
  548. removeData.objectsDataRemoved.push( objectData );
  549. objectRemoved = true;
  550. if ( typeof objectData.faces === 'undefined' ) {
  551. removeData.searchComplete = true;
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. // if object data removed and this is not on nodes removed from
  558. if ( objectRemoved === true ) {//&& removeData.nodesRemovedFrom.indexOf( this ) === -1 ) {
  559. removeData.nodesRemovedFrom.push( this );
  560. }
  561. // if search not complete, search nodes
  562. if ( removeData.searchComplete !== true ) {
  563. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  564. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  565. // try removing object from node
  566. removeData = node.removeObjectRecursive( object, removeData );
  567. if ( removeData.searchComplete === true ) {
  568. break;
  569. }
  570. }
  571. }
  572. return removeData;
  573. },
  574. checkGrow: function () {
  575. // if object count above max
  576. if ( this.objects.length > this.tree.objectsThreshold && this.tree.objectsThreshold > 0 ) {
  577. this.grow();
  578. }
  579. },
  580. grow: function () {
  581. var indexOctant,
  582. object,
  583. objectsExpand = [],
  584. objectsExpandOctants = [],
  585. objectsSplit = [],
  586. objectsSplitOctants = [],
  587. objectsRemaining = [],
  588. i, l;
  589. // for each object
  590. for ( i = 0, l = this.objects.length; i < l; i++ ) {
  591. object = this.objects[ i ];
  592. // get object octant index
  593. indexOctant = this.getOctantIndex( object );
  594. // if lies within octant
  595. if ( indexOctant > -1 ) {
  596. objectsSplit.push( object );
  597. objectsSplitOctants.push( indexOctant );
  598. }
  599. // if lies outside radius
  600. else if ( indexOctant < -1 ) {
  601. objectsExpand.push( object );
  602. objectsExpandOctants.push( indexOctant );
  603. }
  604. // else if lies across bounds between octants
  605. else {
  606. objectsRemaining.push( object );
  607. }
  608. }
  609. // if has objects to split
  610. if ( objectsSplit.length > 0) {
  611. objectsRemaining = objectsRemaining.concat( this.split( objectsSplit, objectsSplitOctants ) );
  612. }
  613. // if has objects to expand
  614. if ( objectsExpand.length > 0) {
  615. objectsRemaining = objectsRemaining.concat( this.expand( objectsExpand, objectsExpandOctants ) );
  616. }
  617. // store remaining
  618. this.objects = objectsRemaining;
  619. // merge check
  620. this.checkMerge();
  621. },
  622. split: function ( objects, octants ) {
  623. var i, l,
  624. indexOctant,
  625. object,
  626. node,
  627. objectsRemaining;
  628. // if not at max depth
  629. if ( this.tree.depthMax < 0 || this.depth < this.tree.depthMax ) {
  630. objects = objects || this.objects;
  631. octants = octants || [];
  632. objectsRemaining = [];
  633. // for each object
  634. for ( i = 0, l = objects.length; i < l; i++ ) {
  635. object = objects[ i ];
  636. // get object octant index
  637. indexOctant = isNumber( octants[ i ] ) ? octants[ i ] : this.getOctantIndex( object );
  638. // if object contained by octant, branch this tree
  639. if ( indexOctant > -1 ) {
  640. node = this.branch( indexOctant );
  641. node.addObject( object );
  642. }
  643. // else add to remaining
  644. else {
  645. objectsRemaining.push( object );
  646. }
  647. }
  648. // if all objects, set remaining as new objects
  649. if ( objects === this.objects ) {
  650. this.objects = objectsRemaining;
  651. }
  652. }
  653. else {
  654. objectsRemaining = this.objects;
  655. }
  656. return objectsRemaining;
  657. },
  658. branch: function ( indexOctant ) {
  659. var node,
  660. overlap,
  661. radius,
  662. radiusOffset,
  663. offset,
  664. position;
  665. // node exists
  666. if ( this.nodesByIndex[ indexOctant ] instanceof THREE.OctreeNode ) {
  667. node = this.nodesByIndex[ indexOctant ];
  668. }
  669. // create new
  670. else {
  671. // properties
  672. radius = ( this.radiusOverlap ) * 0.5;
  673. overlap = radius * this.tree.overlapPct;
  674. radiusOffset = radius - overlap;
  675. offset = this.utilVec31Branch.set( indexOctant & 1 ? radiusOffset : -radiusOffset, indexOctant & 2 ? radiusOffset : -radiusOffset, indexOctant & 4 ? radiusOffset : -radiusOffset );
  676. position = new THREE.Vector3().addVectors( this.position, offset );
  677. // node
  678. node = new THREE.OctreeNode( {
  679. tree: this.tree,
  680. parent: this,
  681. position: position,
  682. radius: radius,
  683. indexOctant: indexOctant
  684. } );
  685. // store
  686. this.addNode( node, indexOctant );
  687. }
  688. return node;
  689. },
  690. expand: function ( objects, octants ) {
  691. var i, l,
  692. object,
  693. objectsRemaining,
  694. objectsExpand,
  695. indexOctant,
  696. flagsOutside,
  697. indexOutside,
  698. indexOctantInverse,
  699. iom = this.tree.INDEX_OUTSIDE_MAP,
  700. indexOutsideCounts,
  701. infoIndexOutside1,
  702. infoIndexOutside2,
  703. infoIndexOutside3,
  704. indexOutsideBitwise1,
  705. indexOutsideBitwise2,
  706. infoPotential1,
  707. infoPotential2,
  708. infoPotential3,
  709. indexPotentialBitwise1,
  710. indexPotentialBitwise2,
  711. octantX, octantY, octantZ,
  712. overlap,
  713. radius,
  714. radiusOffset,
  715. radiusParent,
  716. overlapParent,
  717. offset = this.utilVec31Expand,
  718. position,
  719. parent;
  720. // handle max depth down tree
  721. if ( this.tree.depthMax < 0 || this.tree.root.getDepthEnd() < this.tree.depthMax ) {
  722. objects = objects || this.objects;
  723. octants = octants || [];
  724. objectsRemaining = [];
  725. objectsExpand = [];
  726. // reset counts
  727. for ( i = 0, l = iom.length; i < l; i++ ) {
  728. iom[ i ].count = 0;
  729. }
  730. // for all outside objects, find outside octants containing most objects
  731. for ( i = 0, l = objects.length; i < l; i++ ) {
  732. object = objects[ i ];
  733. // get object octant index
  734. indexOctant = isNumber( octants[ i ] ) ? octants[ i ] : this.getOctantIndex( object );
  735. // if object outside this, include in calculations
  736. if ( indexOctant < -1 ) {
  737. // convert octant index to outside flags
  738. flagsOutside = -indexOctant - this.tree.INDEX_OUTSIDE_OFFSET;
  739. // check against bitwise flags
  740. // x
  741. if ( flagsOutside & this.tree.FLAG_POS_X ) {
  742. iom[ this.tree.INDEX_OUTSIDE_POS_X ].count++;
  743. }
  744. else if ( flagsOutside & this.tree.FLAG_NEG_X ) {
  745. iom[ this.tree.INDEX_OUTSIDE_NEG_X ].count++;
  746. }
  747. // y
  748. if ( flagsOutside & this.tree.FLAG_POS_Y ) {
  749. iom[ this.tree.INDEX_OUTSIDE_POS_Y ].count++;
  750. }
  751. else if ( flagsOutside & this.tree.FLAG_NEG_Y ) {
  752. iom[ this.tree.INDEX_OUTSIDE_NEG_Y ].count++;
  753. }
  754. // z
  755. if ( flagsOutside & this.tree.FLAG_POS_Z ) {
  756. iom[ this.tree.INDEX_OUTSIDE_POS_Z ].count++;
  757. }
  758. else if ( flagsOutside & this.tree.FLAG_NEG_Z ) {
  759. iom[ this.tree.INDEX_OUTSIDE_NEG_Z ].count++;
  760. }
  761. // store in expand list
  762. objectsExpand.push( object );
  763. }
  764. // else add to remaining
  765. else {
  766. objectsRemaining.push( object );
  767. }
  768. }
  769. // if objects to expand
  770. if ( objectsExpand.length > 0 ) {
  771. // shallow copy index outside map
  772. indexOutsideCounts = iom.slice( 0 );
  773. // sort outside index count so highest is first
  774. indexOutsideCounts.sort( function ( a, b ) {
  775. return b.count - a.count;
  776. } );
  777. // get highest outside indices
  778. // first is first
  779. infoIndexOutside1 = indexOutsideCounts[ 0 ];
  780. indexOutsideBitwise1 = infoIndexOutside1.index | 1;
  781. // second is ( one of next two bitwise OR 1 ) that is not opposite of ( first bitwise OR 1 )
  782. infoPotential1 = indexOutsideCounts[ 1 ];
  783. infoPotential2 = indexOutsideCounts[ 2 ];
  784. infoIndexOutside2 = ( infoPotential1.index | 1 ) !== indexOutsideBitwise1 ? infoPotential1 : infoPotential2;
  785. indexOutsideBitwise2 = infoIndexOutside2.index | 1;
  786. // third is ( one of next three bitwise OR 1 ) that is not opposite of ( first or second bitwise OR 1 )
  787. infoPotential1 = indexOutsideCounts[ 2 ];
  788. infoPotential2 = indexOutsideCounts[ 3 ];
  789. infoPotential3 = indexOutsideCounts[ 4 ];
  790. indexPotentialBitwise1 = infoPotential1.index | 1;
  791. indexPotentialBitwise2 = infoPotential2.index | 1;
  792. infoIndexOutside3 = indexPotentialBitwise1 !== indexOutsideBitwise1 && indexPotentialBitwise1 !== indexOutsideBitwise2 ? infoPotential1 : indexPotentialBitwise2 !== indexOutsideBitwise1 && indexPotentialBitwise2 !== indexOutsideBitwise2 ? infoPotential2 : infoPotential3;
  793. // get this octant normal based on outside octant indices
  794. octantX = infoIndexOutside1.x + infoIndexOutside2.x + infoIndexOutside3.x;
  795. octantY = infoIndexOutside1.y + infoIndexOutside2.y + infoIndexOutside3.y;
  796. octantZ = infoIndexOutside1.z + infoIndexOutside2.z + infoIndexOutside3.z;
  797. // get this octant indices based on octant normal
  798. indexOctant = this.getOctantIndexFromPosition( octantX, octantY, octantZ );
  799. indexOctantInverse = this.getOctantIndexFromPosition( -octantX, -octantY, -octantZ );
  800. // properties
  801. overlap = this.overlap;
  802. radius = this.radius;
  803. // radius of parent comes from reversing overlap of this, unless overlap percent is 0
  804. radiusParent = this.tree.overlapPct > 0 ? overlap / ( ( 0.5 * this.tree.overlapPct ) * ( 1 + this.tree.overlapPct ) ) : radius * 2;
  805. overlapParent = radiusParent * this.tree.overlapPct;
  806. // parent offset is difference between radius + overlap of parent and child
  807. radiusOffset = ( radiusParent + overlapParent ) - ( radius + overlap );
  808. offset.set( indexOctant & 1 ? radiusOffset : -radiusOffset, indexOctant & 2 ? radiusOffset : -radiusOffset, indexOctant & 4 ? radiusOffset : -radiusOffset );
  809. position = new THREE.Vector3().addVectors( this.position, offset );
  810. // parent
  811. parent = new THREE.OctreeNode( {
  812. tree: this.tree,
  813. position: position,
  814. radius: radiusParent
  815. } );
  816. // set self as node of parent
  817. parent.addNode( this, indexOctantInverse );
  818. // set parent as root
  819. this.tree.setRoot( parent );
  820. // add all expand objects to parent
  821. for ( i = 0, l = objectsExpand.length; i < l; i++ ) {
  822. this.tree.root.addObject( objectsExpand[ i ] );
  823. }
  824. }
  825. // if all objects, set remaining as new objects
  826. if ( objects === this.objects ) {
  827. this.objects = objectsRemaining;
  828. }
  829. }
  830. else {
  831. objectsRemaining = objects;
  832. }
  833. return objectsRemaining;
  834. },
  835. shrink: function () {
  836. // merge check
  837. this.checkMerge();
  838. // contract check
  839. this.tree.root.checkContract();
  840. },
  841. checkMerge: function () {
  842. var nodeParent = this,
  843. nodeMerge;
  844. // traverse up tree as long as node + entire subtree's object count is under minimum
  845. while ( nodeParent.parent instanceof THREE.OctreeNode && nodeParent.getObjectCountEnd() < this.tree.objectsThreshold ) {
  846. nodeMerge = nodeParent;
  847. nodeParent = nodeParent.parent;
  848. }
  849. // if parent node is not this, merge entire subtree into merge node
  850. if ( nodeParent !== this ) {
  851. nodeParent.merge( nodeMerge );
  852. }
  853. },
  854. merge: function ( nodes ) {
  855. var i, l,
  856. j, k,
  857. node;
  858. // handle nodes
  859. nodes = toArray( nodes );
  860. for ( i = 0, l = nodes.length; i < l; i++ ) {
  861. node = nodes[ i ];
  862. // gather node + all subtree objects
  863. this.addObjectWithoutCheck( node.getObjectsEnd() );
  864. // reset node + entire subtree
  865. node.reset( true, true );
  866. // remove node
  867. this.removeNode( node.indexOctant, node );
  868. }
  869. // merge check
  870. this.checkMerge();
  871. },
  872. checkContract: function () {
  873. var i, l,
  874. node,
  875. nodeObjectsCount,
  876. nodeHeaviest,
  877. nodeHeaviestObjectsCount,
  878. outsideHeaviestObjectsCount;
  879. // find node with highest object count
  880. if ( this.nodesIndices.length > 0 ) {
  881. nodeHeaviestObjectsCount = 0;
  882. outsideHeaviestObjectsCount = this.objects.length;
  883. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  884. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  885. nodeObjectsCount = node.getObjectCountEnd();
  886. outsideHeaviestObjectsCount += nodeObjectsCount;
  887. if ( nodeHeaviest instanceof THREE.OctreeNode === false || nodeObjectsCount > nodeHeaviestObjectsCount ) {
  888. nodeHeaviest = node;
  889. nodeHeaviestObjectsCount = nodeObjectsCount;
  890. }
  891. }
  892. // subtract heaviest count from outside count
  893. outsideHeaviestObjectsCount -= nodeHeaviestObjectsCount;
  894. // if should contract
  895. if ( outsideHeaviestObjectsCount < this.tree.objectsThreshold && nodeHeaviest instanceof THREE.OctreeNode ) {
  896. this.contract( nodeHeaviest );
  897. }
  898. }
  899. },
  900. contract: function ( nodeRoot ) {
  901. var i, l,
  902. node;
  903. // handle all nodes
  904. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  905. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  906. // if node is not new root
  907. if ( node !== nodeRoot ) {
  908. // add node + all subtree objects to root
  909. nodeRoot.addObjectWithoutCheck( node.getObjectsEnd() );
  910. // reset node + entire subtree
  911. node.reset( true, true );
  912. }
  913. }
  914. // add own objects to root
  915. nodeRoot.addObjectWithoutCheck( this.objects );
  916. // reset self
  917. this.reset( false, true );
  918. // set new root
  919. this.tree.setRoot( nodeRoot );
  920. // contract check on new root
  921. nodeRoot.checkContract();
  922. },
  923. getOctantIndex: function ( objectData ) {
  924. var i, l,
  925. positionObj,
  926. radiusObj,
  927. position = this.position,
  928. radiusOverlap = this.radiusOverlap,
  929. overlap = this.overlap,
  930. deltaX, deltaY, deltaZ,
  931. distX, distY, distZ,
  932. distance,
  933. indexOctant = 0;
  934. // handle type
  935. // object data
  936. if ( objectData instanceof THREE.OctreeObjectData ) {
  937. radiusObj = objectData.radius;
  938. positionObj = objectData.position;
  939. // update object data position last
  940. objectData.positionLast.copy( positionObj );
  941. }
  942. // node
  943. else if ( objectData instanceof THREE.OctreeNode ) {
  944. positionObj = objectData.position;
  945. radiusObj = 0;
  946. }
  947. // find delta and distance
  948. deltaX = positionObj.x - position.x;
  949. deltaY = positionObj.y - position.y;
  950. deltaZ = positionObj.z - position.z;
  951. distX = Math.abs( deltaX );
  952. distY = Math.abs( deltaY );
  953. distZ = Math.abs( deltaZ );
  954. distance = Math.max( distX, distY, distZ );
  955. // if outside, use bitwise flags to indicate on which sides object is outside of
  956. if ( distance + radiusObj > radiusOverlap ) {
  957. // x
  958. if ( distX + radiusObj > radiusOverlap ) {
  959. indexOctant = indexOctant ^ ( deltaX > 0 ? this.tree.FLAG_POS_X : this.tree.FLAG_NEG_X );
  960. }
  961. // y
  962. if ( distY + radiusObj > radiusOverlap ) {
  963. indexOctant = indexOctant ^ ( deltaY > 0 ? this.tree.FLAG_POS_Y : this.tree.FLAG_NEG_Y );
  964. }
  965. // z
  966. if ( distZ + radiusObj > radiusOverlap ) {
  967. indexOctant = indexOctant ^ ( deltaZ > 0 ? this.tree.FLAG_POS_Z : this.tree.FLAG_NEG_Z );
  968. }
  969. objectData.indexOctant = -indexOctant - this.tree.INDEX_OUTSIDE_OFFSET;
  970. return objectData.indexOctant;
  971. }
  972. // return octant index from delta xyz
  973. // x right
  974. if ( deltaX - radiusObj > -overlap ) {
  975. indexOctant = indexOctant | 1;
  976. }
  977. // x left
  978. else if ( !( deltaX + radiusObj < overlap ) ) {
  979. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  980. return objectData.indexOctant;
  981. }
  982. // y right
  983. if ( deltaY - radiusObj > -overlap ) {
  984. indexOctant = indexOctant | 2;
  985. }
  986. // y left
  987. else if ( !( deltaY + radiusObj < overlap ) ) {
  988. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  989. return objectData.indexOctant;
  990. }
  991. // z right
  992. if ( deltaZ - radiusObj > -overlap ) {
  993. indexOctant = indexOctant | 4;
  994. }
  995. // z left
  996. else if ( !( deltaZ + radiusObj < overlap ) ) {
  997. objectData.indexOctant = this.tree.INDEX_INSIDE_CROSS;
  998. return objectData.indexOctant;
  999. }
  1000. objectData.indexOctant = indexOctant;
  1001. return objectData.indexOctant;
  1002. },
  1003. getOctantIndexFromPosition: function ( x, y, z ) {
  1004. var indexOctant = 0;
  1005. if ( x > 0 ) {
  1006. indexOctant = indexOctant | 1;
  1007. }
  1008. if ( y > 0 ) {
  1009. indexOctant = indexOctant | 2;
  1010. }
  1011. if ( z > 0 ) {
  1012. indexOctant = indexOctant | 4;
  1013. }
  1014. return indexOctant;
  1015. },
  1016. search: function ( position, radius, objects, direction, directionPct ) {
  1017. var i, l,
  1018. node,
  1019. intersects;
  1020. // test intersects by parameters
  1021. if ( direction ) {
  1022. intersects = this.intersectRay( position, direction, radius, directionPct );
  1023. }
  1024. else {
  1025. intersects = this.intersectSphere( position, radius );
  1026. }
  1027. // if intersects
  1028. if ( intersects === true ) {
  1029. // gather objects
  1030. objects = objects.concat( this.objects );
  1031. // search subtree
  1032. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1033. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1034. objects = node.search( position, radius, objects, direction );
  1035. }
  1036. }
  1037. return objects;
  1038. },
  1039. intersectSphere: function ( position, radius ) {
  1040. var distance = radius * radius,
  1041. px = position.x,
  1042. py = position.y,
  1043. pz = position.z;
  1044. if ( px < this.left ) {
  1045. distance -= Math.pow( px - this.left, 2 );
  1046. }
  1047. else if ( px > this.right ) {
  1048. distance -= Math.pow( px - this.right, 2 );
  1049. }
  1050. if ( py < this.bottom ) {
  1051. distance -= Math.pow( py - this.bottom, 2 );
  1052. }
  1053. else if ( py > this.top ) {
  1054. distance -= Math.pow( py - this.top, 2 );
  1055. }
  1056. if ( pz < this.back ) {
  1057. distance -= Math.pow( pz - this.back, 2 );
  1058. }
  1059. else if ( pz > this.front ) {
  1060. distance -= Math.pow( pz - this.front, 2 );
  1061. }
  1062. return distance >= 0;
  1063. },
  1064. intersectRay: function ( origin, direction, distance, directionPct ) {
  1065. if ( typeof directionPct === 'undefined' ) {
  1066. directionPct = this.utilVec31Ray.set( 1, 1, 1 ).divide( direction );
  1067. }
  1068. var t1 = ( this.left - origin.x ) * directionPct.x,
  1069. t2 = ( this.right - origin.x ) * directionPct.x,
  1070. t3 = ( this.bottom - origin.y ) * directionPct.y,
  1071. t4 = ( this.top - origin.y ) * directionPct.y,
  1072. t5 = ( this.back - origin.z ) * directionPct.z,
  1073. t6 = ( this.front - origin.z ) * directionPct.z,
  1074. tmax = Math.min( Math.min( Math.max( t1, t2), Math.max( t3, t4) ), Math.max( t5, t6) ),
  1075. tmin;
  1076. // ray would intersect in reverse direction, i.e. this is behind ray
  1077. if (tmax < 0)
  1078. {
  1079. return false;
  1080. }
  1081. tmin = Math.max( Math.max( Math.min( t1, t2), Math.min( t3, t4)), Math.min( t5, t6));
  1082. // if tmin > tmax or tmin > ray distance, ray doesn't intersect AABB
  1083. if( tmin > tmax || tmin > distance ) {
  1084. return false;
  1085. }
  1086. return true;
  1087. },
  1088. getDepthEnd: function ( depth ) {
  1089. var i, l,
  1090. node;
  1091. if ( this.nodesIndices.length > 0 ) {
  1092. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1093. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1094. depth = node.getDepthEnd( depth );
  1095. }
  1096. }
  1097. else {
  1098. depth = !depth || this.depth > depth ? this.depth : depth;
  1099. }
  1100. return depth;
  1101. },
  1102. getNodeCountEnd: function () {
  1103. return this.tree.root.getNodeCountRecursive() + 1;
  1104. },
  1105. getNodeCountRecursive: function () {
  1106. var i, l,
  1107. count = this.nodesIndices.length;
  1108. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1109. count += this.nodesByIndex[ this.nodesIndices[ i ] ].getNodeCountRecursive();
  1110. }
  1111. return count;
  1112. },
  1113. getObjectsEnd: function ( objects ) {
  1114. var i, l,
  1115. node;
  1116. objects = ( objects || [] ).concat( this.objects );
  1117. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1118. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1119. objects = node.getObjectsEnd( objects );
  1120. }
  1121. return objects;
  1122. },
  1123. getObjectCountEnd: function () {
  1124. var i, l,
  1125. count = this.objects.length;
  1126. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1127. count += this.nodesByIndex[ this.nodesIndices[ i ] ].getObjectCountEnd();
  1128. }
  1129. return count;
  1130. },
  1131. getObjectCountStart: function () {
  1132. var count = this.objects.length,
  1133. parent = this.parent;
  1134. while( parent instanceof THREE.OctreeNode ) {
  1135. count += parent.objects.length;
  1136. parent = parent.parent;
  1137. }
  1138. return count;
  1139. },
  1140. toConsole: function ( space ) {
  1141. var i, l,
  1142. node,
  1143. spaceAddition = ' ';
  1144. space = typeof space === 'string' ? space : spaceAddition;
  1145. 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 );
  1146. console.log( ( this.parent ? space + ' ' : ' ' ), '+ objects ( ', this.objects.length, ' ) ', this.objects );
  1147. console.log( ( this.parent ? space + ' ' : ' ' ), '+ children ( ', this.nodesIndices.length, ' )', this.nodesIndices, this.nodesByIndex );
  1148. for ( i = 0, l = this.nodesIndices.length; i < l; i++ ) {
  1149. node = this.nodesByIndex[ this.nodesIndices[ i ] ];
  1150. node.toConsole( space + spaceAddition );
  1151. }
  1152. }
  1153. };
  1154. /*===================================================
  1155. raycaster additional functionality
  1156. =====================================================*/
  1157. THREE.Raycaster.prototype.intersectOctreeObject = function ( object, recursive ) {
  1158. var intersects,
  1159. octreeObject,
  1160. facesAll,
  1161. facesSearch;
  1162. if ( object.object instanceof THREE.Object3D ) {
  1163. octreeObject = object;
  1164. object = octreeObject.object;
  1165. // temporarily replace object geometry's faces with octree object faces
  1166. facesSearch = octreeObject.faces;
  1167. facesAll = object.geometry.faces;
  1168. if ( facesSearch.length > 0 ) {
  1169. object.geometry.faces = facesSearch;
  1170. }
  1171. // intersect
  1172. intersects = this.intersectObject( object, recursive );
  1173. // revert object geometry's faces
  1174. if ( facesSearch.length > 0 ) {
  1175. object.geometry.faces = facesAll;
  1176. }
  1177. }
  1178. else {
  1179. intersects = this.intersectObject( object, recursive );
  1180. }
  1181. return intersects;
  1182. };
  1183. THREE.Raycaster.prototype.intersectOctreeObjects = function ( objects, recursive ) {
  1184. var i, il,
  1185. intersects = [];
  1186. for ( i = 0, il = objects.length; i < il; i++ ) {
  1187. intersects = intersects.concat( this.intersectOctreeObject( objects[ i ], recursive ) );
  1188. }
  1189. return intersects;
  1190. };
  1191. }( THREE ) );