forestDataFile.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef _FORESTDATAFILE_H_
  23. #define _FORESTDATAFILE_H_
  24. #ifndef _FORESTITEM_H_
  25. #include "forest/forestItem.h"
  26. #endif
  27. #ifndef _TDICTIONARY_H_
  28. #include "core/util/tDictionary.h"
  29. #endif
  30. class ForestCell;
  31. class Forest;
  32. class Frustum;
  33. /// This is the data file for Forests.
  34. class ForestData
  35. {
  36. protected:
  37. enum { FILE_VERSION = 1 };
  38. /// Set the bucket dimensions to 2km x 2km.
  39. static const U32 BUCKET_DIM = 2000;
  40. /// Set to true if the file is dirty and
  41. /// needs to be saved before being destroyed.
  42. bool mIsDirty;
  43. ///
  44. typedef HashTable<Point2I,ForestCell*> BucketTable;
  45. /// The top level cell buckets which allows us
  46. /// to have virtually unbounded range.
  47. BucketTable mBuckets;
  48. /// The next free item id.
  49. static U32 smNextItemId;
  50. /// Converts a ForestItem's Point3F 'KeyPosition' to a Point2I
  51. /// key we index into BucketTable with.
  52. static Point2I _getBucketKey( const Point3F &pos );
  53. /// Finds the bucket with the given Point2I key or returns NULL.
  54. ForestCell* _findBucket( const Point2I &key ) const;
  55. /// Finds the best top level bucket for the ForestItem 'key' position
  56. /// or returns NULL.
  57. ForestCell* _findBucket( const Point3F &pos ) const;
  58. /// Find the best top level bucket for the given position
  59. /// or returns a new one.
  60. ForestCell* _findOrCreateBucket( const Point3F &pos );
  61. void _onItemReload();
  62. public:
  63. ForestData();
  64. virtual ~ForestData();
  65. bool isDirty() const { return mIsDirty; }
  66. /// Deletes all the data and resets the
  67. /// file to an empty state.
  68. void clear();
  69. /// Helper for debugging cell generation.
  70. void regenCells();
  71. ///
  72. bool read( Stream &stream );
  73. ///
  74. bool write( const char *path );
  75. const ForestItem& addItem( ForestItemData *data,
  76. const Point3F &position,
  77. F32 rotation,
  78. F32 scale );
  79. const ForestItem& addItem( ForestItemKey key,
  80. ForestItemData *data,
  81. const MatrixF &xfm,
  82. F32 scale );
  83. const ForestItem& updateItem( ForestItemKey key,
  84. const Point3F &keyPosition,
  85. ForestItemData *newData,
  86. const MatrixF &newXfm,
  87. F32 newscale );
  88. const ForestItem& updateItem( ForestItem &item );
  89. bool removeItem( ForestItemKey key, const Point3F &keyPosition );
  90. /// Performs a search using the position to limit tested cells.
  91. const ForestItem& findItem( ForestItemKey key, const Point3F &keyPosition ) const;
  92. /// Does an exhaustive search thru all cells looking for
  93. /// the item. This method is slow and should be avoided.
  94. const ForestItem& findItem( ForestItemKey key ) const;
  95. /// Fills a vector with a copy of all the items in the data set.
  96. ///
  97. /// @param outItems The output vector of items.
  98. /// @return The count of items found.
  99. ///
  100. U32 getItems( Vector<ForestItem> *outItems ) const;
  101. /// Fills a vector with a copy of all items in the Frustum.
  102. /// Note that this IS expensive and this is not how Forest internally
  103. /// collects items for rendering. This is here for ForestSelectionTool.
  104. ///
  105. /// @param The Frustum to cull with.
  106. /// @param outItems The output vector of items.
  107. /// @return The count of items found.
  108. ///
  109. U32 getItems( const Frustum &culler, Vector<ForestItem> *outItems ) const;
  110. /// Returns a copy of all the items that intersect the box. If
  111. /// the output vector is NULL then it will early out on the first
  112. /// found item returning 1.
  113. ///
  114. /// @param box The search box.
  115. /// @param outItems The output vector of items or NULL.
  116. /// @return The count of items found.
  117. ///
  118. U32 getItems( const Box3F &box, Vector<ForestItem> *outItems ) const;
  119. /// Returns a copy of all the items that intersect the sphere. If
  120. /// the output vector is NULL then it will early out on the first
  121. /// found item returning 1.
  122. ///
  123. /// @param point The center of the search sphere.
  124. /// @param radius The radius of the search sphere.
  125. /// @param outItems The output vector of items or NULL.
  126. /// @return The count of items found.
  127. ///
  128. U32 getItems( const Point3F &point, F32 radius, Vector<ForestItem> *outItems ) const;
  129. /// Returns a copy of all the items that intersect the 2D circle ignoring
  130. /// the z component. If the output vector is NULL then it will early out
  131. /// on the first found item returning 1.
  132. ///
  133. /// @param point The center point of the search circle.
  134. /// @param radius The radius of the search circle.
  135. /// @param outItems The output vector of items or NULL.
  136. /// @return The count of items found.
  137. ///
  138. U32 getItems( const Point2F &point, F32 radius, Vector<ForestItem> *outItems ) const;
  139. /// Returns a copy of all the items that share the input item datablock.
  140. ///
  141. /// @param data The datablock to search for.
  142. /// @param outItems The output vector of items.
  143. /// @return The count of items found.
  144. ///
  145. U32 getItems( const ForestItemData *data, Vector<ForestItem> *outItems ) const;
  146. /// Returns all the top level cells which intersect the frustum.
  147. void getCells( const Frustum &frustum, Vector<ForestCell*> *outCells ) const;
  148. /// Returns all top level cells.
  149. void getCells( Vector<ForestCell*> *outCells ) const;
  150. /// Gathers all the datablocks used and returns the count.
  151. U32 getDatablocks( Vector<ForestItemData*> *outVector ) const;
  152. ///
  153. bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const;
  154. ///
  155. void clearPhysicsRep( Forest *forest );
  156. void buildPhysicsRep( Forest *forest );
  157. };
  158. inline Point2I ForestData::_getBucketKey( const Point3F &pos )
  159. {
  160. return Point2I ( (S32)mFloor(pos.x / BUCKET_DIM) * BUCKET_DIM,
  161. (S32)mFloor(pos.y / BUCKET_DIM) * BUCKET_DIM );
  162. }
  163. inline ForestCell* ForestData::_findBucket( const Point3F &pos ) const
  164. {
  165. return _findBucket( _getBucketKey( pos ) );
  166. }
  167. #endif // _FORESTDATAFILE_H_