aabtreecull.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWMath *
  23. * *
  24. * $Archive:: /Commando/Code/wwmath/aabtreecull.cpp $*
  25. * *
  26. * Author:: Greg Hjelstrom *
  27. * *
  28. * $Modtime:: 8/26/01 2:18p $*
  29. * *
  30. * $Revision:: 25 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "aabtreecull.h"
  36. #include "chunkio.h"
  37. #include "iostruct.h"
  38. #include <string.h>
  39. #include "sphere.h"
  40. #include "colmath.h"
  41. #include "colmathinlines.h"
  42. /*
  43. ** Declare the pools
  44. */
  45. DEFINE_AUTO_POOL(AABTreeLinkClass,256);
  46. DEFINE_AUTO_POOL(AABTreeNodeClass,256);
  47. /*
  48. ** Current version of the file format
  49. */
  50. const uint32 AABTREE_CURRENT_VERSION = 0x00010000;
  51. /*
  52. ** Chunk Id's used by the aabtree code to save itself into a file
  53. */
  54. enum
  55. {
  56. AABTREE_CHUNK_VERSION = 0x00000001, // version wrapper, contains 32bit version #
  57. AABTREE_CHUNK_AABNODE = 0x00000101, // generic aab-node wrapper
  58. AABTREE_CHUNK_AABNODE_INFO, // OBSOLETE! generic aab-node definition (IOAABNodeStruct)
  59. AABTREE_CHUNK_AABNODE_CONTENTS, // wrapper around contents of the node
  60. AABTREE_CHUNK_AABNODE_VARIABLES, // wrapper around variables for a node
  61. AABTREE_CHUNK_NODE_INDEX = 0x00000200, // wrapper around the node index for an object
  62. AABTREE_VARIABLE_NODESTRUCT = 0x00,
  63. AABTREE_VARIABLE_USERDATA
  64. };
  65. /*
  66. ** IOAABNodeStruct
  67. ** Data structure for the contents of a node in the AAB-Tree
  68. */
  69. #define AABNODE_ATTRIBUTE_FRONT_CHILD 0x00000001
  70. #define AABNODE_ATTRIBUTE_BACK_CHILD 0x00000002
  71. struct IOAABNodeStruct
  72. {
  73. IOVector3Struct Center;
  74. IOVector3Struct Extent;
  75. uint32 Attributes;
  76. };
  77. /*************************************************************************
  78. **
  79. ** Utility functions for walking the object list in an AABTree Node
  80. **
  81. *************************************************************************/
  82. static inline CullableClass * get_first_object(AABTreeNodeClass * node)
  83. {
  84. return node->Object;
  85. }
  86. static inline CullableClass * get_next_object(CullableClass * obj)
  87. {
  88. return ((AABTreeLinkClass *)obj->Get_Cull_Link())->NextObject;
  89. }
  90. /*************************************************************************
  91. **
  92. ** AABTreeCullSystemClass Implementation
  93. **
  94. *************************************************************************/
  95. AABTreeCullSystemClass::AABTreeCullSystemClass(void) :
  96. ObjectCount(0),
  97. NodeCount(0),
  98. IndexedNodes(NULL)
  99. {
  100. RootNode = new AABTreeNodeClass;
  101. Re_Index_Nodes();
  102. }
  103. AABTreeCullSystemClass::~AABTreeCullSystemClass(void)
  104. {
  105. // Delete all links and release-ref all cullables:
  106. int nidx;
  107. for (nidx = 0; nidx < NodeCount; nidx++) {
  108. while(IndexedNodes[nidx]->Object) Remove_Object_Internal(IndexedNodes[nidx]->Object);
  109. }
  110. // Delete node tree (deleting the root recursively deletes all nodes)
  111. delete RootNode;
  112. // Delete indexed node pointer array
  113. if (IndexedNodes) {
  114. delete[] IndexedNodes;
  115. IndexedNodes = NULL;
  116. }
  117. }
  118. void AABTreeCullSystemClass::Add_Object_Internal(CullableClass * obj,int node_index)
  119. {
  120. WWASSERT_PRINT
  121. (
  122. (obj->Get_Culling_System() == NULL),
  123. "AABTreeCullSystemClass::Add -- Object is already in another culling system!\n"
  124. );
  125. AABTreeLinkClass * new_link = new AABTreeLinkClass(this);
  126. obj->Set_Cull_Link(new_link);
  127. if (node_index == -1) {
  128. Add_Object_Recursive(RootNode,obj);
  129. } else {
  130. WWASSERT(node_index < NodeCount);
  131. IndexedNodes[node_index]->Add_Object(obj,false);
  132. ObjectCount++;
  133. }
  134. obj->Add_Ref();
  135. }
  136. void AABTreeCullSystemClass::Remove_Object_Internal(CullableClass * obj)
  137. {
  138. WWASSERT(obj);
  139. WWASSERT(obj->Get_Culling_System() == this);
  140. AABTreeLinkClass * link = (AABTreeLinkClass *)obj->Get_Cull_Link();
  141. WWASSERT(link);
  142. AABTreeNodeClass * node = link->Node;
  143. WWASSERT(node);
  144. node->Remove_Object(obj);
  145. link->Set_Culling_System(NULL);
  146. delete link;
  147. obj->Set_Cull_Link(NULL);
  148. ObjectCount--;
  149. WWASSERT(ObjectCount >= 0);
  150. obj->Release_Ref();
  151. }
  152. void AABTreeCullSystemClass::Update_Culling(CullableClass * obj)
  153. {
  154. WWASSERT(obj);
  155. WWASSERT(obj->Get_Culling_System() == this);
  156. // unlink it from the node it is currently in
  157. AABTreeLinkClass * link = (AABTreeLinkClass *)obj->Get_Cull_Link();
  158. WWASSERT(link);
  159. AABTreeNodeClass * node = link->Node;
  160. WWASSERT(node);
  161. node->Remove_Object(obj);
  162. // decrement the object counter, the node can't
  163. // decrement it for us...
  164. ObjectCount--;
  165. // drop it into the tree again
  166. Add_Object_Recursive(RootNode,obj);
  167. }
  168. void AABTreeCullSystemClass::Collect_Objects(const Vector3 & point)
  169. {
  170. Collect_Objects_Recursive(RootNode,point);
  171. }
  172. void AABTreeCullSystemClass::Collect_Objects(const AABoxClass & box)
  173. {
  174. Collect_Objects_Recursive(RootNode,box);
  175. }
  176. void AABTreeCullSystemClass::Collect_Objects(const OBBoxClass & box)
  177. {
  178. Collect_Objects_Recursive(RootNode,box);
  179. }
  180. void AABTreeCullSystemClass::Collect_Objects(const FrustumClass & frustum)
  181. {
  182. Collect_Objects_Recursive(RootNode,frustum,0);
  183. }
  184. void AABTreeCullSystemClass::Collect_Objects(const SphereClass & sphere)
  185. {
  186. Collect_Objects_Recursive(RootNode,sphere);
  187. }
  188. int AABTreeCullSystemClass::Partition_Node_Count(void) const
  189. {
  190. return Partition_Node_Count_Recursive(RootNode);
  191. }
  192. int AABTreeCullSystemClass::Partition_Tree_Depth(void) const
  193. {
  194. int get_max_depth = 0;
  195. Partition_Tree_Depth_Recursive(RootNode,0,get_max_depth);
  196. return get_max_depth;
  197. }
  198. int AABTreeCullSystemClass::Object_Count(void) const
  199. {
  200. return ObjectCount;
  201. }
  202. int AABTreeCullSystemClass::Partition_Node_Count_Recursive(AABTreeNodeClass * node) const
  203. {
  204. int curcount = 1;
  205. if (node->Front) {
  206. curcount += Partition_Node_Count_Recursive(node->Front);
  207. }
  208. if (node->Back) {
  209. curcount += Partition_Node_Count_Recursive(node->Back);
  210. }
  211. return curcount;
  212. }
  213. void AABTreeCullSystemClass::Partition_Tree_Depth_Recursive(AABTreeNodeClass * node,int cur_depth,int & max_depth) const
  214. {
  215. cur_depth++;
  216. if (cur_depth > max_depth) {
  217. max_depth = cur_depth;
  218. }
  219. if (node->Front) {
  220. Partition_Tree_Depth_Recursive(node->Front,cur_depth,max_depth);
  221. }
  222. if (node->Back) {
  223. Partition_Tree_Depth_Recursive(node->Back,cur_depth,max_depth);
  224. }
  225. }
  226. void AABTreeCullSystemClass::Add_Object_Recursive(AABTreeNodeClass * node,CullableClass * obj)
  227. {
  228. // order the children in terms of size
  229. AABTreeNodeClass * big_child = node->Front;
  230. AABTreeNodeClass * small_child = node->Back;
  231. if (big_child && small_child && (big_child->Compute_Volume() < small_child->Compute_Volume())) {
  232. AABTreeNodeClass * tmp = big_child;
  233. big_child = small_child;
  234. small_child = tmp;
  235. }
  236. // Can we fit in the smaller child?
  237. if (small_child && small_child->Box.Contains(obj->Get_Cull_Box())) {
  238. Add_Object_Recursive(small_child,obj);
  239. return;
  240. }
  241. // Can we fit in the bigger child?
  242. if (big_child && big_child->Box.Contains(obj->Get_Cull_Box())) {
  243. Add_Object_Recursive(big_child,obj);
  244. return;
  245. }
  246. // Ok, we have to fit in this node.
  247. node->Add_Object(obj);
  248. ObjectCount++;
  249. }
  250. void AABTreeCullSystemClass::Add_Loaded_Object(AABTreeNodeClass * node,CullableClass * obj)
  251. {
  252. WWASSERT(node);
  253. WWASSERT(obj);
  254. WWASSERT_PRINT
  255. (
  256. (obj->Get_Culling_System() == NULL),
  257. "AABTreeCullSystemClass::Add_Loaded_Object -- Object is already in another culling system!\n"
  258. );
  259. AABTreeLinkClass * new_link = new AABTreeLinkClass(this);
  260. obj->Set_Cull_Link(new_link);
  261. node->Add_Object(obj);
  262. ObjectCount++;
  263. obj->Add_Ref();
  264. }
  265. void AABTreeCullSystemClass::Re_Partition(void)
  266. {
  267. /*
  268. ** transfer all objects to a temporary node
  269. */
  270. AABTreeNodeClass * dummy_node = new AABTreeNodeClass;
  271. RootNode->Transfer_Objects(dummy_node);
  272. /*
  273. ** delete the old tree and make the dummy node the new root
  274. */
  275. delete RootNode;
  276. RootNode = dummy_node;
  277. /*
  278. ** partition the objects
  279. */
  280. RootNode->Partition();
  281. /*
  282. ** re-index the nodes
  283. */
  284. Re_Index_Nodes();
  285. /*
  286. ** reset the statistics
  287. */
  288. Reset_Statistics();
  289. }
  290. void AABTreeCullSystemClass::Re_Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes)
  291. {
  292. /*
  293. ** transfer all objects to a temporary node
  294. */
  295. AABTreeNodeClass * dummy_node = new AABTreeNodeClass;
  296. RootNode->Transfer_Objects(dummy_node);
  297. /*
  298. ** delete the old tree
  299. */
  300. delete RootNode;
  301. /*
  302. ** allocate a new root node and tell it to partition the given array of boxes
  303. */
  304. RootNode = new AABTreeNodeClass;
  305. RootNode->Partition(bounds,boxes);
  306. /*
  307. ** re-index the nodes
  308. */
  309. Re_Index_Nodes();
  310. /*
  311. ** reset statistics
  312. */
  313. Reset_Statistics();
  314. /*
  315. ** re-insert all objects and delete the temporary node
  316. */
  317. dummy_node->Box.Extent.Set(0,0,0);
  318. CullableClass * obj = get_first_object(dummy_node);
  319. while (obj != NULL) {
  320. Update_Culling(obj);
  321. obj = get_first_object(dummy_node);
  322. }
  323. delete dummy_node;
  324. /*
  325. ** Modify the root node so that any object can be added into the tree
  326. */
  327. RootNode->Box.Extent.Set(FLT_MAX,FLT_MAX,FLT_MAX);
  328. }
  329. void AABTreeCullSystemClass::Update_Bounding_Boxes(void)
  330. {
  331. Update_Bounding_Boxes_Recursive(RootNode);
  332. }
  333. const AABoxClass & AABTreeCullSystemClass::Get_Bounding_Box(void)
  334. {
  335. WWASSERT(RootNode);
  336. return RootNode->Box;
  337. }
  338. void AABTreeCullSystemClass::Get_Node_Bounds(int node_id,AABoxClass * set_bounds)
  339. {
  340. if ((node_id >= 0) && (node_id < NodeCount)) {
  341. *set_bounds = IndexedNodes[node_id]->Box;
  342. } else {
  343. *set_bounds = IndexedNodes[0]->Box;
  344. }
  345. }
  346. void AABTreeCullSystemClass::Reset_Statistics(void)
  347. {
  348. Stats.NodeCount = NodeCount;
  349. Stats.NodesAccepted = 0;
  350. Stats.NodesTriviallyAccepted = 0;
  351. Stats.NodesRejected = 0;
  352. }
  353. const AABTreeCullSystemClass::StatsStruct & AABTreeCullSystemClass::Get_Statistics(void)
  354. {
  355. return Stats;
  356. }
  357. void AABTreeCullSystemClass::Collect_Objects_Recursive(AABTreeNodeClass * node)
  358. {
  359. /*
  360. ** Collect any objects in this node
  361. */
  362. if (node->Object) {
  363. CullableClass * obj = get_first_object(node);
  364. while (obj) {
  365. Add_To_Collection(obj);
  366. obj = get_next_object(obj);
  367. }
  368. }
  369. /*
  370. ** Statistics
  371. */
  372. NODE_TRIVIALLY_ACCEPTED();
  373. /*
  374. ** Descend into the children
  375. */
  376. if (node->Back) {
  377. Collect_Objects_Recursive(node->Back);
  378. }
  379. if (node->Front) {
  380. Collect_Objects_Recursive(node->Front);
  381. }
  382. }
  383. void AABTreeCullSystemClass::Collect_Objects_Recursive(AABTreeNodeClass * node,const Vector3 & point)
  384. {
  385. /*
  386. ** Is the point inside this volume?
  387. */
  388. if (node->Box.Contains(point) == false) {
  389. NODE_REJECTED();
  390. return;
  391. }
  392. NODE_ACCEPTED();
  393. /*
  394. ** Collect any objects in this node
  395. */
  396. if (node->Object) {
  397. CullableClass * obj = get_first_object(node);
  398. while (obj) {
  399. if (obj->Get_Cull_Box().Contains(point)) {
  400. Add_To_Collection(obj);
  401. }
  402. obj = get_next_object(obj);
  403. }
  404. }
  405. /*
  406. ** Descend into the children
  407. */
  408. if (node->Back) {
  409. Collect_Objects_Recursive(node->Back,point);
  410. }
  411. if (node->Front) {
  412. Collect_Objects_Recursive(node->Front,point);
  413. }
  414. }
  415. void AABTreeCullSystemClass::Collect_Objects_Recursive(AABTreeNodeClass * node,const AABoxClass & box)
  416. {
  417. /*
  418. ** Cull the given box against the bounding volume of this node
  419. ** If it is culled, stop descending the tree. If the current node is
  420. ** completely contained inside the given box, jump into the collection function
  421. ** that doesn't do any more volume checking...
  422. */
  423. CollisionMath::OverlapType overlap = CollisionMath::Overlap_Test(box,node->Box);
  424. if (overlap == CollisionMath::OUTSIDE) {
  425. NODE_REJECTED();
  426. return;
  427. } else if (overlap == CollisionMath::INSIDE) {
  428. Collect_Objects_Recursive(node);
  429. return;
  430. }
  431. NODE_ACCEPTED();
  432. /*
  433. ** Test any objects in this node
  434. */
  435. if (node->Object) {
  436. CullableClass * obj = get_first_object(node);
  437. while (obj) {
  438. if (CollisionMath::Overlap_Test(box,obj->Get_Cull_Box()) != CollisionMath::OUTSIDE) {
  439. Add_To_Collection(obj);
  440. }
  441. obj = get_next_object(obj);
  442. }
  443. }
  444. /*
  445. ** Recurse into any children
  446. */
  447. if (node->Back) {
  448. Collect_Objects_Recursive(node->Back,box);
  449. }
  450. if (node->Front) {
  451. Collect_Objects_Recursive(node->Front,box);
  452. }
  453. }
  454. void AABTreeCullSystemClass::Collect_Objects_Recursive(AABTreeNodeClass * node,const OBBoxClass & box)
  455. {
  456. /*
  457. ** Cull the given box against the bounding volume of this node
  458. ** If it is culled, stop descending the tree. If the current node is
  459. ** completely contained inside the given box, jump into the collection function
  460. ** that doesn't do any more volume checking...
  461. */
  462. CollisionMath::OverlapType overlap = CollisionMath::Overlap_Test(box,node->Box);
  463. if (overlap == CollisionMath::OUTSIDE) {
  464. NODE_REJECTED();
  465. return;
  466. } else if (overlap == CollisionMath::INSIDE) {
  467. Collect_Objects_Recursive(node);
  468. return;
  469. }
  470. NODE_ACCEPTED();
  471. /*
  472. ** Test any objects in this node
  473. */
  474. if (node->Object) {
  475. CullableClass * obj = get_first_object(node);
  476. while (obj) {
  477. if (CollisionMath::Overlap_Test(box,obj->Get_Cull_Box()) != CollisionMath::OUTSIDE) {
  478. Add_To_Collection(obj);
  479. }
  480. obj = get_next_object(obj);
  481. }
  482. }
  483. /*
  484. ** Recurse into any children
  485. */
  486. if (node->Back) {
  487. Collect_Objects_Recursive(node->Back,box);
  488. }
  489. if (node->Front) {
  490. Collect_Objects_Recursive(node->Front,box);
  491. }
  492. }
  493. void AABTreeCullSystemClass::Collect_Objects_Recursive
  494. (
  495. AABTreeNodeClass * node,
  496. const FrustumClass & frustum,
  497. int planes_passed
  498. )
  499. {
  500. /*
  501. ** Cull the bounding volume of this node against the frustum.
  502. ** If it is culled, stop descending the tree.
  503. */
  504. CollisionMath::OverlapType overlap = CollisionMath::Overlap_Test(frustum,node->Box,planes_passed);
  505. if (overlap == CollisionMath::OUTSIDE) {
  506. NODE_REJECTED();
  507. return;
  508. } else if (overlap == CollisionMath::INSIDE) {
  509. Collect_Objects_Recursive(node);
  510. return;
  511. }
  512. NODE_ACCEPTED();
  513. /*
  514. ** Test any objects in this node
  515. */
  516. if (node->Object) {
  517. CullableClass * obj = get_first_object(node);
  518. while (obj) {
  519. if (CollisionMath::Overlap_Test(frustum,obj->Get_Cull_Box()) != CollisionMath::OUTSIDE) {
  520. Add_To_Collection(obj);
  521. }
  522. obj = get_next_object(obj);
  523. }
  524. }
  525. /*
  526. ** Recurse into any children
  527. */
  528. if (node->Back) {
  529. Collect_Objects_Recursive(node->Back,frustum,planes_passed);
  530. }
  531. if (node->Front) {
  532. Collect_Objects_Recursive(node->Front,frustum,planes_passed);
  533. }
  534. }
  535. void AABTreeCullSystemClass::Collect_Objects_Recursive(AABTreeNodeClass * node,const SphereClass & sphere)
  536. {
  537. /*
  538. ** Is the point inside this volume?
  539. */
  540. if (CollisionMath::Overlap_Test (node->Box, sphere) == CollisionMath::OUTSIDE) {
  541. NODE_REJECTED();
  542. return;
  543. }
  544. NODE_ACCEPTED();
  545. /*
  546. ** Collect any objects in this node
  547. */
  548. if (node->Object) {
  549. CullableClass * obj = get_first_object(node);
  550. while (obj) {
  551. if (CollisionMath::Overlap_Test (obj->Get_Cull_Box(), sphere) != CollisionMath::OUTSIDE) {
  552. Add_To_Collection(obj);
  553. }
  554. obj = get_next_object(obj);
  555. }
  556. }
  557. /*
  558. ** Descend into the children
  559. */
  560. if (node->Back) {
  561. Collect_Objects_Recursive(node->Back,sphere);
  562. }
  563. if (node->Front) {
  564. Collect_Objects_Recursive(node->Front,sphere);
  565. }
  566. }
  567. void AABTreeCullSystemClass::Update_Bounding_Boxes_Recursive(AABTreeNodeClass * node)
  568. {
  569. MinMaxAABoxClass minmaxbox(node->Box);
  570. /*
  571. ** Update child boxes first and ensure that we bound them
  572. */
  573. if (node->Front) {
  574. Update_Bounding_Boxes_Recursive(node->Front);
  575. minmaxbox.Add_Box(node->Front->Box);
  576. }
  577. if (node->Back) {
  578. Update_Bounding_Boxes_Recursive(node->Back);
  579. minmaxbox.Add_Box(node->Back->Box);
  580. }
  581. /*
  582. ** Make sure we bound our contained objects
  583. */
  584. if (node->Object) {
  585. CullableClass * obj = get_first_object(node);
  586. while (obj) {
  587. minmaxbox.Add_Box(obj->Get_Cull_Box());
  588. obj = get_next_object(obj);
  589. }
  590. }
  591. node->Box.Init_Min_Max(minmaxbox.MinCorner,minmaxbox.MaxCorner);
  592. }
  593. void AABTreeCullSystemClass::Load(ChunkLoadClass & cload)
  594. {
  595. WWASSERT_PRINT(Object_Count() == 0, "Remove all objects from AAB-Culling system before loading!\n");
  596. delete RootNode;
  597. RootNode = new AABTreeNodeClass;
  598. // The first chunk should be a version chunk
  599. cload.Open_Chunk();
  600. if (cload.Cur_Chunk_ID() != AABTREE_CHUNK_VERSION) {
  601. WWDEBUG_ERROR(("Attempting to read an obsolete AAB-Tree!"));
  602. cload.Close_Chunk();
  603. return;
  604. }
  605. // read in the version and verify that it is the current version
  606. uint32 version;
  607. cload.Read(&version,sizeof(version));
  608. if (version != AABTREE_CURRENT_VERSION) {
  609. WWDEBUG_ERROR(("Attempting to read an obsolete AAB-Tree!"));
  610. cload.Close_Chunk();
  611. return;
  612. }
  613. cload.Close_Chunk();
  614. // read in the tree
  615. Load_Nodes(RootNode,cload);
  616. // re-index all nodes
  617. Re_Index_Nodes();
  618. // reset the statistics
  619. Reset_Statistics();
  620. }
  621. void AABTreeCullSystemClass::Load_Nodes(AABTreeNodeClass * node,ChunkLoadClass & cload)
  622. {
  623. // Open the node description
  624. cload.Open_Chunk();
  625. WWASSERT(cload.Cur_Chunk_ID() == AABTREE_CHUNK_AABNODE);
  626. // Load the node description.
  627. // Older files will contain a chunk named AABTREE_CHUNK_AABNODE_INFO while newer
  628. // files will contain AABTREE_CHUNK_AABNODE_VARIABLES which contains the IOAABNodeStruct
  629. // in a micro-chunk.
  630. IOAABNodeStruct node_desc;
  631. memset(&node_desc,0,sizeof(IOAABNodeStruct));
  632. cload.Open_Chunk();
  633. if (cload.Cur_Chunk_ID() == AABTREE_CHUNK_AABNODE_INFO) {
  634. // Loading the legacy format...
  635. WWASSERT(cload.Cur_Chunk_ID() == AABTREE_CHUNK_AABNODE_INFO);
  636. cload.Read(&node_desc,sizeof(node_desc));
  637. } else if (cload.Cur_Chunk_ID() == AABTREE_CHUNK_AABNODE_VARIABLES) {
  638. // Loading the new format, contains micro chunks...
  639. while (cload.Open_Micro_Chunk()) {
  640. switch(cload.Cur_Micro_Chunk_ID()) {
  641. READ_MICRO_CHUNK(cload,AABTREE_VARIABLE_NODESTRUCT,node_desc);
  642. READ_MICRO_CHUNK(cload,AABTREE_VARIABLE_USERDATA,node->UserData);
  643. }
  644. cload.Close_Micro_Chunk();
  645. }
  646. }
  647. cload.Close_Chunk();
  648. // Initialize the node bounds.
  649. node->Box.Center.X = node_desc.Center.X;
  650. node->Box.Center.Y = node_desc.Center.Y;
  651. node->Box.Center.Z = node_desc.Center.Z;
  652. node->Box.Extent.X = node_desc.Extent.X;
  653. node->Box.Extent.Y = node_desc.Extent.Y;
  654. node->Box.Extent.Z = node_desc.Extent.Z;
  655. // Load the contents of the node.
  656. cload.Open_Chunk();
  657. WWASSERT(cload.Cur_Chunk_ID() == AABTREE_CHUNK_AABNODE_CONTENTS);
  658. Load_Node_Contents(node,cload);
  659. cload.Close_Chunk();
  660. // Close the node description
  661. cload.Close_Chunk();
  662. // if we are supposed to have a front child, load it
  663. if (node_desc.Attributes & AABNODE_ATTRIBUTE_FRONT_CHILD) {
  664. WWASSERT(node->Front == NULL);
  665. node->Front = new AABTreeNodeClass();
  666. node->Front->Parent = node;
  667. Load_Nodes(node->Front,cload);
  668. }
  669. // if we have a back child, load it
  670. if (node_desc.Attributes & AABNODE_ATTRIBUTE_BACK_CHILD) {
  671. WWASSERT(node->Back == NULL);
  672. node->Back = new AABTreeNodeClass();
  673. node->Back->Parent = node;
  674. Load_Nodes(node->Back,cload);
  675. }
  676. }
  677. void AABTreeCullSystemClass::Save(ChunkSaveClass & csave)
  678. {
  679. csave.Begin_Chunk(AABTREE_CHUNK_VERSION);
  680. uint32 version = AABTREE_CURRENT_VERSION;
  681. csave.Write(&version,sizeof(uint32));
  682. csave.End_Chunk();
  683. Save_Nodes(RootNode,csave);
  684. }
  685. void AABTreeCullSystemClass::Save_Nodes(AABTreeNodeClass * node,ChunkSaveClass & csave)
  686. {
  687. WWASSERT(node);
  688. csave.Begin_Chunk(AABTREE_CHUNK_AABNODE);
  689. csave.Begin_Chunk(AABTREE_CHUNK_AABNODE_VARIABLES);
  690. IOAABNodeStruct node_desc;
  691. memset(&node_desc,0,sizeof(node_desc));
  692. node_desc.Center.X = node->Box.Center.X;
  693. node_desc.Center.Y = node->Box.Center.Y;
  694. node_desc.Center.Z = node->Box.Center.Z;
  695. node_desc.Extent.X = node->Box.Extent.X;
  696. node_desc.Extent.Y = node->Box.Extent.Y;
  697. node_desc.Extent.Z = node->Box.Extent.Z;
  698. if (node->Front) {
  699. node_desc.Attributes |= AABNODE_ATTRIBUTE_FRONT_CHILD;
  700. }
  701. if (node->Back) {
  702. node_desc.Attributes |= AABNODE_ATTRIBUTE_BACK_CHILD;
  703. }
  704. WRITE_MICRO_CHUNK(csave,AABTREE_VARIABLE_NODESTRUCT,node_desc);
  705. WRITE_MICRO_CHUNK(csave,AABTREE_VARIABLE_USERDATA,node->UserData);
  706. csave.End_Chunk();
  707. csave.Begin_Chunk(AABTREE_CHUNK_AABNODE_CONTENTS);
  708. Save_Node_Contents(node,csave);
  709. csave.End_Chunk();
  710. csave.End_Chunk();
  711. if (node->Front) {
  712. Save_Nodes(node->Front,csave);
  713. }
  714. if (node->Back) {
  715. Save_Nodes(node->Back,csave);
  716. }
  717. }
  718. void AABTreeCullSystemClass::Load_Object_Linkage(ChunkLoadClass & cload,CullableClass * obj)
  719. {
  720. uint32 index;
  721. cload.Open_Chunk();
  722. WWASSERT(cload.Cur_Chunk_ID() == AABTREE_CHUNK_NODE_INDEX);
  723. cload.Read(&index,sizeof(index));
  724. cload.Close_Chunk();
  725. Add_Object_Internal(obj,index);
  726. }
  727. void AABTreeCullSystemClass::Save_Object_Linkage(ChunkSaveClass & csave,CullableClass * obj)
  728. {
  729. WWASSERT(obj);
  730. WWASSERT(obj->Get_Culling_System() == this);
  731. AABTreeLinkClass * link = (AABTreeLinkClass *)obj->Get_Cull_Link();
  732. WWASSERT(link);
  733. AABTreeNodeClass * node = link->Node;
  734. WWASSERT(node);
  735. uint32 index = node->Index;
  736. csave.Begin_Chunk(AABTREE_CHUNK_NODE_INDEX);
  737. csave.Write(&index,sizeof(index));
  738. csave.End_Chunk();
  739. }
  740. void AABTreeCullSystemClass::Re_Index_Nodes(void)
  741. {
  742. if (IndexedNodes != NULL) {
  743. delete[] IndexedNodes;
  744. IndexedNodes = NULL;
  745. }
  746. NodeCount = Partition_Node_Count();
  747. WWASSERT(NodeCount > 0);
  748. IndexedNodes = new AABTreeNodeClass *[NodeCount];
  749. int counter = 0;
  750. Re_Index_Nodes_Recursive(RootNode,counter);
  751. WWASSERT(counter == NodeCount);
  752. }
  753. void AABTreeCullSystemClass::Re_Index_Nodes_Recursive(AABTreeNodeClass * node,int & counter)
  754. {
  755. node->Index = counter;
  756. IndexedNodes[counter] = node;
  757. counter++;
  758. if (node->Front) {
  759. Re_Index_Nodes_Recursive(node->Front,counter);
  760. }
  761. if (node->Back) {
  762. Re_Index_Nodes_Recursive(node->Back,counter);
  763. }
  764. }
  765. /*************************************************************************
  766. **
  767. ** AABTreeNodeClass Implementation
  768. **
  769. *************************************************************************/
  770. AABTreeNodeClass::AABTreeNodeClass(void) :
  771. Index(0),
  772. Box(Vector3(0,0,0),Vector3(0,0,0)),
  773. Parent(NULL),
  774. Front(NULL),
  775. Back(NULL),
  776. Object(NULL),
  777. UserData(0)
  778. {
  779. }
  780. AABTreeNodeClass::~AABTreeNodeClass(void)
  781. {
  782. // objects should be removed before deleting the partition tree
  783. WWASSERT(Object == NULL);
  784. // delete our children
  785. if (Front) {
  786. delete Front;
  787. Front = NULL;
  788. }
  789. if (Back) {
  790. delete Back;
  791. Back = NULL;
  792. }
  793. }
  794. void AABTreeNodeClass::Compute_Bounding_Box(void)
  795. {
  796. /*
  797. ** make the children update their boxes first
  798. */
  799. if (Front) {
  800. Front->Compute_Bounding_Box();
  801. }
  802. if (Back) {
  803. Back->Compute_Bounding_Box();
  804. }
  805. Compute_Local_Bounding_Box();
  806. }
  807. void AABTreeNodeClass::Compute_Local_Bounding_Box(void)
  808. {
  809. /*
  810. ** Now make sure we bound our children
  811. */
  812. MinMaxAABoxClass box(Vector3(FLT_MAX,FLT_MAX,FLT_MAX),Vector3(-FLT_MAX,-FLT_MAX,-FLT_MAX));
  813. if (Front) {
  814. box.Add_Box(Front->Box);
  815. }
  816. if (Back) {
  817. box.Add_Box(Back->Box);
  818. }
  819. /*
  820. ** bound the objects in this node
  821. */
  822. CullableClass * obj = Object;
  823. while (obj) {
  824. box.Add_Box(obj->Get_Cull_Box());
  825. AABTreeLinkClass * link = (AABTreeLinkClass *)obj->Get_Cull_Link();
  826. obj = link->NextObject;
  827. }
  828. Box.Init(box);
  829. }
  830. float AABTreeNodeClass::Compute_Volume(void)
  831. {
  832. return Box.Volume();
  833. }
  834. void AABTreeNodeClass::Add_Object(CullableClass * obj,bool update_bounds)
  835. {
  836. AABTreeLinkClass * link = (AABTreeLinkClass *)obj->Get_Cull_Link();
  837. WWASSERT(link);
  838. link->Node = this;
  839. link->NextObject = Object;
  840. Object = obj;
  841. if (update_bounds) {
  842. // if this is the only object and we have no children, just copy
  843. // the object's bounding box, otherwise, add it to what we have
  844. if ((Object_Count() == 1) && (Front == NULL) && (Back == NULL)) {
  845. Box = obj->Get_Cull_Box();
  846. } else {
  847. Box.Add_Box(obj->Get_Cull_Box());
  848. }
  849. }
  850. }
  851. void AABTreeNodeClass::Remove_Object(CullableClass * obj)
  852. {
  853. WWASSERT(obj);
  854. // find the given object in our linked list
  855. CullableClass * prevobj = NULL;
  856. CullableClass * curobj = Object;
  857. while (curobj) {
  858. AABTreeLinkClass * link = (AABTreeLinkClass *)curobj->Get_Cull_Link();
  859. if (curobj == obj) {
  860. // found the object, unlink it.
  861. if (prevobj) {
  862. AABTreeLinkClass * prevlink = (AABTreeLinkClass *)prevobj->Get_Cull_Link();
  863. prevlink->NextObject = link->NextObject;
  864. } else {
  865. Object = link->NextObject;
  866. }
  867. link->NextObject = NULL;
  868. link->Node = NULL;
  869. return;
  870. }
  871. // move to the next object
  872. prevobj = curobj;
  873. curobj = link->NextObject;
  874. }
  875. }
  876. void AABTreeNodeClass::Transfer_Objects(AABTreeNodeClass * dummy_node)
  877. {
  878. // unlink all of our objects, relinking them to the dummy_node
  879. while (Object) {
  880. CullableClass * obj = Object;
  881. Remove_Object(obj);
  882. dummy_node->Add_Object(obj);
  883. }
  884. // do the same with our children
  885. if (Front) {
  886. Front->Transfer_Objects(dummy_node);
  887. }
  888. if (Back) {
  889. Back->Transfer_Objects(dummy_node);
  890. }
  891. }
  892. int AABTreeNodeClass::Object_Count(void)
  893. {
  894. CullableClass * obj = Object;
  895. int count = 0;
  896. while (obj) {
  897. count++;
  898. obj = ((AABTreeLinkClass *)obj->Get_Cull_Link())->NextObject;
  899. }
  900. return count;
  901. }
  902. CullableClass * AABTreeNodeClass::Peek_Object(int index)
  903. {
  904. int count = 0;
  905. CullableClass * obj = Object;
  906. WWASSERT(obj != NULL);
  907. while (obj && (count != index)) {
  908. count++;
  909. obj = ((AABTreeLinkClass *)obj->Get_Cull_Link())->NextObject;
  910. }
  911. WWASSERT(count == index);
  912. return obj;
  913. }
  914. /******************************************************************************************
  915. **
  916. ** Partitioning code which partitions the objects in the tree and passes them into
  917. ** the new children
  918. **
  919. ******************************************************************************************/
  920. void AABTreeNodeClass::Partition(void)
  921. {
  922. /*
  923. ** if we're down to only 2 objects, we're done
  924. */
  925. int obj_count = Object_Count();
  926. if (obj_count <= 2) return;
  927. /*
  928. ** Create an array of the bounding boxes of our objects
  929. */
  930. SimpleDynVecClass<AABoxClass> boxes(obj_count);
  931. CullableClass * obj = Object;
  932. while (obj != NULL) {
  933. boxes.Add(obj->Get_Cull_Box());
  934. obj = get_next_object(obj);
  935. }
  936. /*
  937. ** Select and assign the splitting plane
  938. ** De-allocate the array of boxes to conserve memory
  939. */
  940. SplitChoiceStruct sc;
  941. Select_Splitting_Plane(&sc,boxes);
  942. boxes.Resize(0);
  943. /*
  944. ** If there was no good split, just leave all of
  945. ** the objects in this node
  946. */
  947. if (sc.Cost == FLT_MAX) {
  948. return;
  949. }
  950. /*
  951. ** Split the tiles
  952. */
  953. AABTreeNodeClass * front = new AABTreeNodeClass;
  954. AABTreeNodeClass * back = new AABTreeNodeClass;
  955. Split_Objects(sc,front,back);
  956. /*
  957. ** Build a front tree if necessary.
  958. */
  959. if (front->Object_Count() > 0) {
  960. Front = front;
  961. Front->Parent = this;
  962. Front->Partition();
  963. } else {
  964. delete front;
  965. front = NULL;
  966. }
  967. /*
  968. ** Build a back tree if necessary.
  969. */
  970. if (back->Object_Count() > 0) {
  971. Back = back;
  972. Back->Parent = this;
  973. Back->Partition();
  974. } else {
  975. delete back;
  976. back = NULL;
  977. }
  978. }
  979. void AABTreeNodeClass::Split_Objects(const AABTreeNodeClass::SplitChoiceStruct & sc,AABTreeNodeClass * front,AABTreeNodeClass * back)
  980. {
  981. // This function assumes that this node is a leaf
  982. WWASSERT(Front == NULL);
  983. WWASSERT(Back == NULL);
  984. WWASSERT(Object_Count() == sc.FrontCount + sc.BackCount);
  985. int fcount = 0;
  986. int bcount = 0;
  987. // unlink all of our objects, relinking them to the appropriate node
  988. while (Object) {
  989. // pull the object out of this node
  990. CullableClass * obj = Object;
  991. Remove_Object(Object);
  992. // decide which node to add the object to,
  993. // NOTE: we have to use the same convention as Compute_Score!
  994. const AABoxClass & box = obj->Get_Cull_Box();
  995. if (CollisionMath::Overlap_Test(sc.Plane,box.Center) == CollisionMath::FRONT) {
  996. front->Add_Object(obj);
  997. fcount++;
  998. } else {
  999. back->Add_Object(obj);
  1000. bcount++;
  1001. }
  1002. }
  1003. // copy the bounding boxes
  1004. front->Box = sc.FrontBox;
  1005. front->Box.Extent += Vector3(WWMATH_EPSILON,WWMATH_EPSILON,WWMATH_EPSILON);
  1006. back->Box = sc.BackBox;
  1007. back->Box.Extent += Vector3(WWMATH_EPSILON,WWMATH_EPSILON,WWMATH_EPSILON);
  1008. // when we are all done, the counts should match.
  1009. WWASSERT(fcount == sc.FrontCount);
  1010. WWASSERT(bcount == sc.BackCount);
  1011. }
  1012. /******************************************************************************************
  1013. **
  1014. ** Partitioning code which generates the tree based on a set of input boxes
  1015. **
  1016. ******************************************************************************************/
  1017. void AABTreeNodeClass::Partition(const AABoxClass & bounds,SimpleDynVecClass<AABoxClass> & boxes)
  1018. {
  1019. Box = bounds;
  1020. /*
  1021. ** if we're down to only 1 box, we're done
  1022. */
  1023. if (boxes.Count() <= 1) return;
  1024. /*
  1025. ** Select and assign the splitting plane
  1026. */
  1027. SplitChoiceStruct sc;
  1028. Select_Splitting_Plane(&sc,boxes);
  1029. /*
  1030. ** If there was no good split, just leave all of
  1031. ** the objects in this node
  1032. */
  1033. if (sc.Cost == FLT_MAX) {
  1034. return;
  1035. }
  1036. /*
  1037. ** Split the boxes
  1038. ** Deallocate the input box array to conserve RAM
  1039. */
  1040. SimpleDynVecClass<AABoxClass> frontboxes(sc.FrontCount);
  1041. SimpleDynVecClass<AABoxClass> backboxes(sc.BackCount);
  1042. Split_Boxes(sc,boxes,frontboxes,backboxes);
  1043. boxes.Delete_All();
  1044. /*
  1045. ** Build a front tree if necessary.
  1046. */
  1047. if (frontboxes.Count() > 0) {
  1048. Front = new AABTreeNodeClass;
  1049. Front->Parent = this;
  1050. Front->Partition(sc.FrontBox,frontboxes);
  1051. } else {
  1052. Front = NULL;
  1053. }
  1054. /*
  1055. ** Build a back tree if necessary.
  1056. */
  1057. if (backboxes.Count() > 0) {
  1058. Back = new AABTreeNodeClass;
  1059. Back->Parent = this;
  1060. Back->Partition(sc.BackBox,backboxes);
  1061. } else {
  1062. Back = NULL;
  1063. }
  1064. }
  1065. void AABTreeNodeClass::Split_Boxes
  1066. (
  1067. const AABTreeNodeClass::SplitChoiceStruct & sc,
  1068. SimpleDynVecClass<AABoxClass> & boxes,
  1069. SimpleDynVecClass<AABoxClass> & frontboxes,
  1070. SimpleDynVecClass<AABoxClass> & backboxes
  1071. )
  1072. {
  1073. WWASSERT(boxes.Count() == sc.FrontCount + sc.BackCount);
  1074. // copy each box in the input array into the appropriate output array
  1075. for (int i=0; i<boxes.Count(); i++) {
  1076. const AABoxClass & box = boxes[i];
  1077. if (CollisionMath::Overlap_Test(sc.Plane,box.Center) == CollisionMath::FRONT) {
  1078. frontboxes.Add(box);
  1079. } else {
  1080. backboxes.Add(box);
  1081. }
  1082. }
  1083. // when we are all done, the counts should match.
  1084. WWASSERT(frontboxes.Count() == sc.FrontCount);
  1085. WWASSERT(backboxes.Count() == sc.BackCount);
  1086. }
  1087. /******************************************************************************************
  1088. **
  1089. ** Partitioning code which searches for the best split for a given set of boxes. This
  1090. ** code is re-used by both types of partitioning (partitioning contained objects or
  1091. ** partitioning based on a set of "seed" boxes.)
  1092. **
  1093. ******************************************************************************************/
  1094. void AABTreeNodeClass::Select_Splitting_Plane
  1095. (
  1096. SplitChoiceStruct * sc,
  1097. SimpleDynVecClass<AABoxClass> & boxes
  1098. )
  1099. {
  1100. const int NUM_TRYS = 300;
  1101. /*
  1102. ** Try putting axis-aligned planes through some random vertices
  1103. */
  1104. int objcount = boxes.Count();
  1105. int trys = 0;
  1106. for (trys = 0; trys < MIN(NUM_TRYS,objcount); trys++) {
  1107. int obj_index;
  1108. SplitChoiceStruct test;
  1109. /*
  1110. ** Select a random object
  1111. */
  1112. obj_index = rand() % objcount;
  1113. const AABoxClass & box = boxes[obj_index];
  1114. /*
  1115. ** Select a random plane which co-incides with one of the faces
  1116. ** of the object's bounding box
  1117. */
  1118. switch(rand() % 6) {
  1119. case 0: test.Plane.Set(AAPlaneClass::XNORMAL,box.Center.X + box.Extent.X); break;
  1120. case 1: test.Plane.Set(AAPlaneClass::XNORMAL,box.Center.X - box.Extent.X); break;
  1121. case 2: test.Plane.Set(AAPlaneClass::YNORMAL,box.Center.Y + box.Extent.Y); break;
  1122. case 3: test.Plane.Set(AAPlaneClass::YNORMAL,box.Center.Y - box.Extent.Y); break;
  1123. case 4: test.Plane.Set(AAPlaneClass::ZNORMAL,box.Center.Z + box.Extent.Z); break;
  1124. case 5: test.Plane.Set(AAPlaneClass::ZNORMAL,box.Center.Z - box.Extent.Z); break;
  1125. };
  1126. /*
  1127. ** Get the score for this plane
  1128. */
  1129. Compute_Score(&test,boxes);
  1130. if (test.Cost < sc->Cost) {
  1131. *sc = test;
  1132. }
  1133. }
  1134. /*
  1135. ** Still haven't found a valid splitting plane, uh-oh.
  1136. */
  1137. if ((trys >= MIN(NUM_TRYS,objcount)) && (sc->Cost == FLT_MAX)) {
  1138. Select_Splitting_Plane_Brute_Force(sc,boxes);
  1139. return;
  1140. }
  1141. }
  1142. void AABTreeNodeClass::Select_Splitting_Plane_Brute_Force
  1143. (
  1144. AABTreeNodeClass::SplitChoiceStruct * sc,
  1145. SimpleDynVecClass<AABoxClass> & boxes
  1146. )
  1147. {
  1148. /*
  1149. ** Try putting axis-aligned planes along each face of each box
  1150. */
  1151. int objcount = boxes.Count();
  1152. for (int obj_index = 0; obj_index < objcount; obj_index++) {
  1153. AAPlaneClass plane;
  1154. const AABoxClass & box = boxes[obj_index];
  1155. /*
  1156. ** Try each face of this box
  1157. */
  1158. for (int plane_index = 0; plane_index < 6; plane_index++) {
  1159. SplitChoiceStruct test;
  1160. switch(plane_index % 6) {
  1161. case 0: test.Plane.Set(AAPlaneClass::XNORMAL,box.Center.X + box.Extent.X); break;
  1162. case 1: test.Plane.Set(AAPlaneClass::XNORMAL,box.Center.X - box.Center.Y); break;
  1163. case 2: test.Plane.Set(AAPlaneClass::YNORMAL,box.Center.Y + box.Center.Y); break;
  1164. case 3: test.Plane.Set(AAPlaneClass::YNORMAL,box.Center.Y - box.Center.Y); break;
  1165. case 4: test.Plane.Set(AAPlaneClass::ZNORMAL,box.Center.Z + box.Center.Z); break;
  1166. case 5: test.Plane.Set(AAPlaneClass::ZNORMAL,box.Center.Z - box.Center.Z); break;
  1167. };
  1168. test.FrontBox.Init_Empty();
  1169. test.BackBox.Init_Empty();
  1170. /*
  1171. ** Get the score for this plane
  1172. */
  1173. Compute_Score(&test,boxes);
  1174. if (test.Cost < sc->Cost) {
  1175. *sc = test;
  1176. }
  1177. }
  1178. }
  1179. /*
  1180. ** Notify user that we couldn't split this node
  1181. */
  1182. #ifdef WWDEBUG
  1183. if (sc->Cost == FLT_MAX) {
  1184. WWDEBUG_SAY(("Unable to split node! objcount = %d. (%.2f,%.2f,%.2f)\r\n",objcount,Box.Center.X, Box.Center.Y, Box.Center.Z));
  1185. }
  1186. #endif
  1187. }
  1188. void AABTreeNodeClass::Compute_Score
  1189. (
  1190. AABTreeNodeClass::SplitChoiceStruct * sc,
  1191. SimpleDynVecClass<AABoxClass> & boxes
  1192. )
  1193. {
  1194. /*
  1195. ** Suitability of a plane as a partition plane is based on the following factors:
  1196. ** - How many "tiles" are on each side of the plane,
  1197. */
  1198. for (int i=0; i<boxes.Count(); i++) {
  1199. const AABoxClass & box = boxes[i];
  1200. if (CollisionMath::Overlap_Test(sc->Plane,box.Center) == CollisionMath::FRONT) {
  1201. sc->FrontCount++;
  1202. sc->FrontBox.Add_Box(box);
  1203. } else {
  1204. sc->BackCount++;
  1205. sc->BackBox.Add_Box(box);
  1206. }
  1207. }
  1208. /*
  1209. ** Compute the cost.
  1210. */
  1211. float back_cost = sc->BackBox.Volume() * sc->BackCount;
  1212. float front_cost = sc->FrontBox.Volume() * sc->FrontCount;
  1213. sc->Cost = front_cost + back_cost;
  1214. if ((sc->FrontCount == 0) || (sc->BackCount == 0)) {
  1215. sc->Cost = FLT_MAX;
  1216. }
  1217. }
  1218. /**************************************************************************************
  1219. AABTreeIterator Implemenation
  1220. **************************************************************************************/
  1221. AABTreeIterator::AABTreeIterator(AABTreeCullSystemClass * tree) :
  1222. Tree(tree),
  1223. CurNodeIndex(0)
  1224. {
  1225. WWASSERT(Tree != NULL);
  1226. }
  1227. void AABTreeIterator::Reset(void)
  1228. {
  1229. CurNodeIndex = 0;
  1230. }
  1231. bool AABTreeIterator::Enter_Parent(void)
  1232. {
  1233. validate();
  1234. if (CurNodeIndex != 0) {
  1235. CurNodeIndex = Tree->IndexedNodes[CurNodeIndex]->Parent->Index;
  1236. return true;
  1237. } else {
  1238. return false;
  1239. }
  1240. }
  1241. bool AABTreeIterator::Enter_Sibling(void)
  1242. {
  1243. validate();
  1244. if (CurNodeIndex != 0) {
  1245. /*
  1246. ** find which child of our parent we are
  1247. */
  1248. AABTreeNodeClass * parent = Tree->IndexedNodes[CurNodeIndex]->Parent;
  1249. AABTreeNodeClass * parent_front = parent->Front;
  1250. AABTreeNodeClass * parent_back = parent->Back;
  1251. /*
  1252. ** if our parent doesn't have two children, we don't have a sibling
  1253. */
  1254. if ((parent_front == NULL) || (parent_back == NULL)) {
  1255. return false;
  1256. }
  1257. /*
  1258. ** if we our our parent's front child, go to its back child
  1259. */
  1260. if ((int)parent_front->Index == CurNodeIndex) {
  1261. CurNodeIndex = parent_back->Index;
  1262. return true;
  1263. }
  1264. /*
  1265. ** if we our our parent's back child, go to its front child
  1266. */
  1267. if ((int)parent_back->Index == (int)CurNodeIndex) {
  1268. CurNodeIndex = parent_front->Index;
  1269. return true;
  1270. }
  1271. }
  1272. return false;
  1273. }
  1274. bool AABTreeIterator::Has_Front_Child(void)
  1275. {
  1276. validate();
  1277. return (Tree->IndexedNodes[CurNodeIndex]->Front != NULL);
  1278. }
  1279. bool AABTreeIterator::Enter_Front_Child(void)
  1280. {
  1281. validate();
  1282. if (Has_Front_Child()) {
  1283. CurNodeIndex = Tree->IndexedNodes[CurNodeIndex]->Front->Index;
  1284. return true;
  1285. }
  1286. return false;
  1287. }
  1288. bool AABTreeIterator::Has_Back_Child(void)
  1289. {
  1290. validate();
  1291. return (Tree->IndexedNodes[CurNodeIndex]->Back != NULL);
  1292. }
  1293. bool AABTreeIterator::Enter_Back_Child(void)
  1294. {
  1295. if (Has_Back_Child()) {
  1296. CurNodeIndex = Tree->IndexedNodes[CurNodeIndex]->Back->Index;
  1297. return true;
  1298. }
  1299. return false;
  1300. }
  1301. int AABTreeIterator::Get_Current_Node_Index(void)
  1302. {
  1303. return CurNodeIndex;
  1304. }
  1305. void AABTreeIterator::Get_Current_Box(AABoxClass * set_box)
  1306. {
  1307. Tree->Get_Node_Bounds(CurNodeIndex,set_box);
  1308. }
  1309. void AABTreeIterator::validate(void)
  1310. {
  1311. if ((CurNodeIndex < 0) || (CurNodeIndex >= Tree->NodeCount)) {
  1312. CurNodeIndex = 0;
  1313. }
  1314. }
  1315. /*
  1316. Can we make a more compact AABTree?
  1317. Here is the existing data in each Node:
  1318. uint32 Index; // Index of this node
  1319. AABoxClass Box; // Bounding box of the node
  1320. AABTreeNodeClass * Parent; // parent of this node
  1321. AABTreeNodeClass * Front; // front node
  1322. AABTreeNodeClass * Back; // back node
  1323. CullableClass * Object; // objects in this node
  1324. uint32 UserData; // 32bit field for the user, initialized to 0
  1325. Total Size: 48 bytes
  1326. Here is a possible replacement:
  1327. uint16 Index;
  1328. short x,y,z,w,l,h; // Need origin and scale factors for the boxes stored in tree
  1329. uint16 ParentIndex;
  1330. uint16 FrontIndex;
  1331. uint16 BackIndex;
  1332. CullableClass * Object;
  1333. uint32 UserData;
  1334. Total Size: 28 bytes
  1335. */