forestDataFile.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "forest/forestDataFile.h"
  24. #include "forest/forest.h"
  25. #include "forest/forestCell.h"
  26. #include "T3D/physics/physicsBody.h"
  27. #include "core/stream/fileStream.h"
  28. #include "core/resource.h"
  29. #include "math/mathIO.h"
  30. #include "math/mPoint2.h"
  31. #include "platform/profiler.h"
  32. template<> ResourceBase::Signature Resource<ForestData>::signature()
  33. {
  34. return MakeFourCC('f','k','d','f');
  35. }
  36. template<>
  37. void* Resource<ForestData>::create( const Torque::Path &path )
  38. {
  39. FileStream stream;
  40. stream.open( path.getFullPath(), Torque::FS::File::Read );
  41. if ( stream.getStatus() != Stream::Ok )
  42. return NULL;
  43. ForestData *file = new ForestData();
  44. if ( !file->read( stream ) )
  45. {
  46. delete file;
  47. return NULL;
  48. }
  49. return file;
  50. }
  51. U32 ForestData::smNextItemId = 1;
  52. ForestData::ForestData()
  53. : mIsDirty( false )
  54. {
  55. ForestItemData::getReloadSignal().notify( this, &ForestData::_onItemReload );
  56. }
  57. ForestData::~ForestData()
  58. {
  59. ForestItemData::getReloadSignal().remove( this, &ForestData::_onItemReload );
  60. clear();
  61. }
  62. void ForestData::clear()
  63. {
  64. // We only have to delete the top level cells and ForestCell will
  65. // clean up its sub-cells in its destructor.
  66. BucketTable::Iterator iter = mBuckets.begin();
  67. for (; iter != mBuckets.end(); ++iter) delete iter->value;
  68. mBuckets.clear();
  69. mIsDirty = true;
  70. }
  71. bool ForestData::read( Stream &stream )
  72. {
  73. // Read our identifier... so we know we're
  74. // not reading in pure garbage.
  75. char id[4] = { 0 };
  76. stream.read( 4, id );
  77. if ( dMemcmp( id, "FKDF", 4 ) != 0 )
  78. {
  79. Con::errorf( "ForestDataFile::read() - This is not a Forest planting file!" );
  80. return false;
  81. }
  82. // Empty ourselves before we really begin reading.
  83. clear();
  84. // Now the version number.
  85. U8 version;
  86. stream.read( &version );
  87. if ( version > (U8)FILE_VERSION )
  88. {
  89. Con::errorf( "ForestDataFile::read() - This file was created with an newer version of Forest!" );
  90. return false;
  91. }
  92. // Read in the names of the ForestItemData datablocks
  93. // and recover the datablock.
  94. Vector<ForestItemData*> allDatablocks;
  95. U32 count;
  96. stream.read( &count );
  97. allDatablocks.setSize( count );
  98. for ( U32 i=0; i < count; i++ )
  99. {
  100. StringTableEntry name = stream.readSTString();
  101. ForestItemData* data = ForestItemData::find( name );
  102. // TODO: Change this to instead create a dummy forest data
  103. // for each so that the user can swap it with the right one.
  104. if ( data == NULL )
  105. {
  106. Con::warnf( "ForestData::read - ForestItemData named %s was not found.", name );
  107. Con::warnf( "Note this can occur if you have deleted or renamed datablocks prior to loading this forest and is not an 'error' in this scenario." );
  108. }
  109. allDatablocks[ i ] = data;
  110. }
  111. U8 dataIndex;
  112. Point3F pos;
  113. QuatF rot;
  114. F32 scale;
  115. ForestItemData* data;
  116. MatrixF xfm;
  117. U32 skippedItems = 0;
  118. // Read in the items.
  119. stream.read( &count );
  120. for ( U32 i=0; i < count; i++ )
  121. {
  122. stream.read( &dataIndex );
  123. mathRead( stream, &pos );
  124. mathRead( stream, &rot );
  125. stream.read( &scale );
  126. data = allDatablocks[ dataIndex ];
  127. if ( data )
  128. {
  129. rot.setMatrix( &xfm );
  130. xfm.setPosition( pos );
  131. addItem( smNextItemId++, data, xfm, scale );
  132. }
  133. else
  134. {
  135. skippedItems++;
  136. }
  137. }
  138. if ( skippedItems > 0 )
  139. Con::warnf( "ForestData::read - %i items were skipped because their datablocks were not found.", skippedItems );
  140. // Clear the dirty flag.
  141. mIsDirty = false;
  142. return true;
  143. }
  144. bool ForestData::write( const char *path )
  145. {
  146. // Open the stream.
  147. FileStream stream;
  148. if ( !stream.open( path, Torque::FS::File::Write ) )
  149. {
  150. Con::errorf( "ForestDataFile::write() - Failed opening stream!" );
  151. return false;
  152. }
  153. // Write our identifier... so we have a better
  154. // idea if we're reading pure garbage.
  155. stream.write( 4, "FKDF" );
  156. // Now the version number.
  157. stream.write( (U8)FILE_VERSION );
  158. // First gather all the ForestItemData datablocks
  159. // used by the items in the forest.
  160. Vector<ForestItemData*> allDatablocks;
  161. getDatablocks( &allDatablocks );
  162. // Write out the datablock list.
  163. U32 count = allDatablocks.size();
  164. stream.write( count );
  165. for ( U32 i=0; i < count; i++ )
  166. {
  167. StringTableEntry localName = allDatablocks[i]->getInternalName();
  168. AssertFatal( localName != NULL && localName[0] != '\0', "ForestData::write - ForestItemData had no internal name set!" );
  169. stream.writeString( allDatablocks[i]->getInternalName() );
  170. }
  171. // Get a copy of all the items.
  172. Vector<ForestItem> items;
  173. getItems( &items );
  174. // Save the item count.
  175. stream.write( (U32)items.size() );
  176. // Save the items.
  177. Vector<ForestItem>::const_iterator iter = items.begin();
  178. for ( ; iter != items.end(); iter++ )
  179. {
  180. U8 dataIndex = T3D::find( allDatablocks.begin(), allDatablocks.end(), iter->getData() ) - allDatablocks.begin();
  181. stream.write( dataIndex );
  182. mathWrite( stream, iter->getPosition() );
  183. QuatF quat;
  184. quat.set( iter->getTransform() );
  185. mathWrite( stream, quat );
  186. stream.write( iter->getScale() );
  187. }
  188. // Clear the dirty flag.
  189. mIsDirty = false;
  190. return true;
  191. }
  192. void ForestData::regenCells()
  193. {
  194. Vector<ForestItem> items;
  195. getItems( &items );
  196. clear();
  197. for ( U32 i=0; i < items.size(); i++ )
  198. {
  199. const ForestItem &item = items[i];
  200. addItem( item.getKey(), item.getData(), item.getTransform(), item.getScale() );
  201. }
  202. mIsDirty = true;
  203. }
  204. ForestCell* ForestData::_findBucket( const Point2I &key ) const
  205. {
  206. BucketTable::ConstIterator iter = mBuckets.find( key );
  207. if ( iter != mBuckets.end() )
  208. return iter->value;
  209. else
  210. return NULL;
  211. }
  212. ForestCell* ForestData::_findOrCreateBucket( const Point3F &pos )
  213. {
  214. // Look it up.
  215. const Point2I key = _getBucketKey( pos );
  216. BucketTable::Iterator iter = mBuckets.find( key );
  217. ForestCell *bucket = NULL;
  218. if ( iter != mBuckets.end() )
  219. bucket = iter->value;
  220. else
  221. {
  222. bucket = new ForestCell( RectF( key.x, key.y, BUCKET_DIM, BUCKET_DIM ) );
  223. mBuckets.insertUnique( key, bucket );
  224. mIsDirty = true;
  225. }
  226. return bucket;
  227. }
  228. void ForestData::_onItemReload()
  229. {
  230. // Invalidate cell batches and bounds so they
  231. // will be regenerated next render.
  232. Vector<ForestCell*> stack;
  233. getCells( &stack );
  234. ForestCell *pCell;
  235. while ( !stack.empty() )
  236. {
  237. pCell = stack.last();
  238. stack.pop_back();
  239. if ( !pCell )
  240. continue;
  241. pCell->freeBatches();
  242. pCell->invalidateBounds();
  243. pCell->getChildren( &stack );
  244. }
  245. }
  246. const ForestItem& ForestData::addItem( ForestItemData *data,
  247. const Point3F &position,
  248. F32 rotation,
  249. F32 scale )
  250. {
  251. MatrixF xfm;
  252. xfm.set( EulerF( 0, 0, rotation ), position );
  253. return addItem( smNextItemId++,
  254. data,
  255. xfm,
  256. scale );
  257. }
  258. const ForestItem& ForestData::addItem( ForestItemKey key,
  259. ForestItemData *data,
  260. const MatrixF &xfm,
  261. F32 scale )
  262. {
  263. ForestCell *bucket = _findOrCreateBucket( xfm.getPosition() );
  264. mIsDirty = true;
  265. return bucket->insertItem( key, data, xfm, scale );
  266. }
  267. const ForestItem& ForestData::updateItem( ForestItemKey key,
  268. const Point3F &keyPosition,
  269. ForestItemData *newData,
  270. const MatrixF &newXfm,
  271. F32 newScale )
  272. {
  273. Point2I bucketKey = _getBucketKey( keyPosition );
  274. ForestCell *bucket = _findBucket( bucketKey );
  275. if ( !bucket || !bucket->removeItem( key, keyPosition, true ) )
  276. return ForestItem::Invalid;
  277. if ( bucket->isEmpty() )
  278. {
  279. delete bucket;
  280. mBuckets.erase( bucketKey );
  281. }
  282. return addItem( key, newData, newXfm, newScale );
  283. }
  284. const ForestItem& ForestData::updateItem( ForestItem &item )
  285. {
  286. return updateItem( item.getKey(),
  287. item.getPosition(),
  288. item.getData(),
  289. item.getTransform(),
  290. item.getScale() );
  291. }
  292. bool ForestData::removeItem( ForestItemKey key, const Point3F &keyPosition )
  293. {
  294. Point2I bucketkey = _getBucketKey( keyPosition );
  295. ForestCell *bucket = _findBucket( keyPosition );
  296. if ( !bucket || !bucket->removeItem( key, keyPosition, true ) )
  297. return false;
  298. if ( bucket->isEmpty() )
  299. {
  300. delete bucket;
  301. mBuckets.erase( bucketkey );
  302. }
  303. mIsDirty = true;
  304. return true;
  305. }
  306. const ForestItem& ForestData::findItem( ForestItemKey key, const Point3F &keyPos ) const
  307. {
  308. PROFILE_SCOPE( ForestData_findItem );
  309. AssertFatal( key != 0, "ForestCell::findItem() - Got null key!" );
  310. ForestCell *cell = _findBucket( keyPos );
  311. while ( cell && !cell->isLeaf() )
  312. cell = cell->getChildAt( keyPos );
  313. U32 index;
  314. if ( cell && cell->findIndexByKey( key, &index ) )
  315. return cell->getItems()[ index ];
  316. return ForestItem::Invalid;
  317. }
  318. const ForestItem& ForestData::findItem( ForestItemKey key ) const
  319. {
  320. PROFILE_SCOPE( ForestData_findItem_Slow );
  321. AssertFatal( key != 0, "ForestData::findItem() - Got null key!" );
  322. // Do an exhaustive search thru all the cells... this
  323. // is really crappy... we shouldn't do this regularly.
  324. Vector<const ForestCell*> stack;
  325. BucketTable::ConstIterator iter = mBuckets.begin();
  326. for (; iter != mBuckets.end(); ++iter)
  327. stack.push_back( iter->value );
  328. // Now loop till we run out of cells.
  329. while ( !stack.empty() )
  330. {
  331. // Pop off the next cell.
  332. const ForestCell *cell = stack.last();
  333. stack.pop_back();
  334. // Recurse thru non-leaf cells.
  335. if ( !cell->isLeaf() )
  336. {
  337. cell->getChildren( &stack );
  338. continue;
  339. }
  340. // Finally search for the item.
  341. U32 index;
  342. if ( cell->findIndexByKey( key, &index ) )
  343. return cell->getItems()[ index ];
  344. }
  345. return ForestItem::Invalid;
  346. }
  347. U32 ForestData::getItems( Vector<ForestItem> *outItems ) const
  348. {
  349. AssertFatal( outItems, "ForestData::getItems() - The output vector was NULL!" );
  350. PROFILE_SCOPE( ForestData_getItems );
  351. Vector<const ForestCell*> stack;
  352. U32 count = 0;
  353. BucketTable::ConstIterator iter = mBuckets.begin();
  354. for (; iter != mBuckets.end(); ++iter)
  355. stack.push_back( iter->value );
  356. // Now loop till we run out of cells.
  357. while ( !stack.empty() )
  358. {
  359. // Pop off the next cell.
  360. const ForestCell *cell = stack.last();
  361. stack.pop_back();
  362. // Recurse thru non-leaf cells.
  363. if ( !cell->isLeaf() )
  364. {
  365. cell->getChildren( &stack );
  366. continue;
  367. }
  368. // Get the items.
  369. count += cell->getItems().size();
  370. outItems->merge( cell->getItems() );
  371. }
  372. return count;
  373. }
  374. U32 ForestData::getItems( const Frustum &culler, Vector<ForestItem> *outItems ) const
  375. {
  376. AssertFatal( outItems, "ForestData::getItems() - The output vector was NULL!" );
  377. PROFILE_SCOPE( ForestData_getItems_ByFrustum );
  378. Vector<ForestCell*> stack;
  379. getCells( &stack );
  380. Vector<ForestItem>::const_iterator iter;
  381. U32 count = 0;
  382. // Now loop till we run out of cells.
  383. while ( !stack.empty() )
  384. {
  385. // Pop off the next cell.
  386. const ForestCell *cell = stack.last();
  387. stack.pop_back();
  388. if ( culler.isCulled( cell->getBounds() ) )
  389. continue;
  390. // Recurse thru non-leaf cells.
  391. if ( cell->isBranch() )
  392. {
  393. cell->getChildren( &stack );
  394. continue;
  395. }
  396. // Get the items.
  397. iter = cell->getItems().begin();
  398. for ( ; iter != cell->getItems().end(); iter++ )
  399. {
  400. if ( !culler.isCulled( iter->getWorldBox() ) )
  401. {
  402. outItems->merge( cell->getItems() );
  403. count++;
  404. }
  405. }
  406. }
  407. return count;
  408. }
  409. U32 ForestData::getItems( const Box3F &box, Vector<ForestItem> *outItems ) const
  410. {
  411. PROFILE_SCOPE( ForestData_getItems_ByBox );
  412. Vector<const ForestCell*> stack;
  413. U32 count = 0;
  414. BucketTable::ConstIterator iter = mBuckets.begin();
  415. for (; iter != mBuckets.end(); ++iter)
  416. stack.push_back( iter->value );
  417. // Now loop till we run out of cells.
  418. while ( !stack.empty() )
  419. {
  420. // Pop off the next cell.
  421. const ForestCell *cell = stack.last();
  422. stack.pop_back();
  423. // If the cell is empty or doesn't overlap the box... skip it.
  424. if ( cell->isEmpty() ||
  425. !cell->getBounds().isOverlapped( box ) )
  426. continue;
  427. // Recurse thru non-leaf cells.
  428. if ( !cell->isLeaf() )
  429. {
  430. cell->getChildren( &stack );
  431. continue;
  432. }
  433. // Finally look thru the items.
  434. const Vector<ForestItem> &items = cell->getItems();
  435. Vector<ForestItem>::const_iterator item = items.begin();
  436. for ( ; item != items.end(); item++ )
  437. {
  438. if ( item->getWorldBox().isOverlapped( box ) )
  439. {
  440. // If we don't have an output vector then the user just
  441. // wanted to know if any object existed... so early out.
  442. if ( !outItems )
  443. return 1;
  444. ++count;
  445. outItems->push_back( *item );
  446. }
  447. }
  448. }
  449. return count;
  450. }
  451. U32 ForestData::getItems( const Point3F &point, F32 radius, Vector<ForestItem> *outItems ) const
  452. {
  453. PROFILE_SCOPE( ForestData_getItems_BySphere );
  454. Vector<const ForestCell*> stack;
  455. U32 count = 0;
  456. BucketTable::ConstIterator iter = mBuckets.begin();
  457. for (; iter != mBuckets.end(); ++iter)
  458. stack.push_back( iter->value );
  459. const F32 radiusSq = radius * radius;
  460. // Now loop till we run out of cells.
  461. while ( !stack.empty() )
  462. {
  463. // Pop off the next cell.
  464. const ForestCell *cell = stack.last();
  465. stack.pop_back();
  466. // TODO: If we could know here that the cell is fully within
  467. // the sphere... we could do a fast gather of all its elements
  468. // without any further testing of it or its children.
  469. // If the cell is empty or doesn't overlap the sphere... skip it.
  470. if ( cell->isEmpty() ||
  471. cell->getBounds().getSqDistanceToPoint( point ) > radiusSq )
  472. continue;
  473. // Recurse thru non-leaf cells.
  474. if ( !cell->isLeaf() )
  475. {
  476. cell->getChildren( &stack );
  477. continue;
  478. }
  479. // Finally look thru the items.
  480. const Vector<ForestItem> &items = cell->getItems();
  481. Vector<ForestItem>::const_iterator item = items.begin();
  482. for ( ; item != items.end(); item++ )
  483. {
  484. if ( item->getWorldBox().getSqDistanceToPoint( point ) < radiusSq )
  485. {
  486. // If we don't have an output vector then the user just
  487. // wanted to know if any object existed... so early out.
  488. if ( !outItems )
  489. return 1;
  490. ++count;
  491. outItems->push_back( *item );
  492. }
  493. }
  494. }
  495. return count;
  496. }
  497. U32 ForestData::getItems( const Point2F &point, F32 radius, Vector<ForestItem> *outItems ) const
  498. {
  499. PROFILE_SCOPE( ForestData_getItems_ByCircle );
  500. Vector<const ForestCell*> stack;
  501. U32 count = 0;
  502. BucketTable::ConstIterator iter = mBuckets.begin();
  503. for (; iter != mBuckets.end(); ++iter)
  504. stack.push_back( iter->value );
  505. const F32 radiusSq = radius * radius;
  506. // Now loop till we run out of cells.
  507. while ( !stack.empty() )
  508. {
  509. // Pop off the next cell.
  510. const ForestCell *cell = stack.last();
  511. stack.pop_back();
  512. // If the cell is empty or doesn't overlap the sphere... skip it.
  513. if ( cell->isEmpty() ||
  514. cell->getRect().getSqDistanceToPoint( point ) > radiusSq )
  515. continue;
  516. // Recurse thru non-leaf cells.
  517. if ( !cell->isLeaf() )
  518. {
  519. cell->getChildren( &stack );
  520. continue;
  521. }
  522. // Finally look thru the items.
  523. const Vector<ForestItem> &items = cell->getItems();
  524. Vector<ForestItem>::const_iterator item = items.begin();
  525. F32 compareDist;
  526. for ( ; item != items.end(); item++ )
  527. {
  528. compareDist = mSquared( radius + item->getData()->mRadius );
  529. if ( item->getSqDistanceToPoint( point ) < compareDist )
  530. {
  531. // If we don't have an output vector then the user just
  532. // wanted to know if any object existed... so early out.
  533. if ( !outItems )
  534. return 1;
  535. ++count;
  536. outItems->push_back( *item );
  537. }
  538. }
  539. }
  540. return count;
  541. }
  542. U32 ForestData::getItems( const ForestItemData *data, Vector<ForestItem> *outItems ) const
  543. {
  544. AssertFatal( outItems, "ForestData::getItems() - The output vector was NULL!" );
  545. PROFILE_SCOPE( ForestData_getItems_ByDatablock );
  546. Vector<const ForestCell*> stack;
  547. U32 count = 0;
  548. BucketTable::ConstIterator iter = mBuckets.begin();
  549. for (; iter != mBuckets.end(); ++iter)
  550. stack.push_back( iter->value );
  551. // Now loop till we run out of cells.
  552. while ( !stack.empty() )
  553. {
  554. // Pop off the next cell.
  555. const ForestCell *cell = stack.last();
  556. stack.pop_back();
  557. // Recurse thru non-leaf cells.
  558. if ( !cell->isLeaf() )
  559. {
  560. cell->getChildren( &stack );
  561. continue;
  562. }
  563. // Get the items.
  564. const Vector<ForestItem> &items = cell->getItems();
  565. Vector<ForestItem>::const_iterator item = items.begin();
  566. for ( ; item != items.end(); item++ )
  567. {
  568. if ( item->getData() == data )
  569. {
  570. ++count;
  571. outItems->push_back( *item );
  572. }
  573. }
  574. }
  575. return count;
  576. }
  577. void ForestData::getCells( const Frustum &frustum, Vector<ForestCell*> *outCells ) const
  578. {
  579. PROFILE_SCOPE( ForestData_getCells_frustum );
  580. BucketTable::ConstIterator iter = mBuckets.begin();
  581. for (; iter != mBuckets.end(); ++iter)
  582. {
  583. if ( !frustum.isCulled( iter->value->getBounds() ) )
  584. outCells->push_back( iter->value );
  585. }
  586. }
  587. void ForestData::getCells( Vector<ForestCell*> *outCells ) const
  588. {
  589. PROFILE_SCOPE( ForestData_getCells_nofrustum );
  590. BucketTable::ConstIterator iter = mBuckets.begin();
  591. for (; iter != mBuckets.end(); ++iter)
  592. outCells->push_back( iter->value );
  593. }
  594. U32 ForestData::getDatablocks( Vector<ForestItemData*> *outVector ) const
  595. {
  596. Vector<const ForestCell*> stack;
  597. U32 count = 0;
  598. BucketTable::ConstIterator iter = mBuckets.begin();
  599. for (; iter != mBuckets.end(); ++iter)
  600. stack.push_back( iter->value );
  601. // Now loop till we run out of cells.
  602. while ( !stack.empty() )
  603. {
  604. // Pop off the next cell.
  605. const ForestCell *cell = stack.last();
  606. stack.pop_back();
  607. // Recurse thru non-leaf cells.
  608. if ( !cell->isLeaf() )
  609. {
  610. cell->getChildren( &stack );
  611. continue;
  612. }
  613. // Go thru the items.
  614. const Vector<ForestItem> &items = cell->getItems();
  615. Vector<ForestItem>::const_iterator item = items.begin();
  616. for ( ; item != items.end(); item++ )
  617. {
  618. ForestItemData *data = item->getData();
  619. if (T3D::find( outVector->begin(), outVector->end(), data ) != outVector->end() )
  620. continue;
  621. count++;
  622. outVector->push_back( data );
  623. }
  624. }
  625. return count;
  626. }
  627. void ForestData::clearPhysicsRep( Forest *forest )
  628. {
  629. Vector<ForestCell*> stack;
  630. BucketTable::Iterator iter = mBuckets.begin();
  631. for (; iter != mBuckets.end(); ++iter)
  632. stack.push_back( iter->value );
  633. // Now loop till we run out of cells.
  634. while ( !stack.empty() )
  635. {
  636. // Pop off the next cell.
  637. ForestCell *cell = stack.last();
  638. stack.pop_back();
  639. // Recurse thru non-leaf cells.
  640. if ( !cell->isLeaf() )
  641. {
  642. cell->getChildren( &stack );
  643. continue;
  644. }
  645. cell->clearPhysicsRep( forest );
  646. }
  647. }
  648. void ForestData::buildPhysicsRep( Forest *forest )
  649. {
  650. Vector<ForestCell*> stack;
  651. BucketTable::Iterator iter = mBuckets.begin();
  652. for (; iter != mBuckets.end(); ++iter)
  653. stack.push_back( iter->value );
  654. // Now loop till we run out of cells.
  655. while ( !stack.empty() )
  656. {
  657. // Pop off the next cell.
  658. ForestCell *cell = stack.last();
  659. stack.pop_back();
  660. // Recurse thru non-leaf cells.
  661. if ( !cell->isLeaf() )
  662. {
  663. cell->getChildren( &stack );
  664. continue;
  665. }
  666. cell->buildPhysicsRep( forest );
  667. }
  668. }