forestDataFile.h
Engine/source/forest/forestDataFile.h
Classes:
class
This is the data file for Forests.
Detailed Description
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#ifndef _FORESTDATAFILE_H_ 25#define _FORESTDATAFILE_H_ 26 27#ifndef _FORESTITEM_H_ 28#include "forest/forestItem.h" 29#endif 30#ifndef _TDICTIONARY_H_ 31#include "core/util/tDictionary.h" 32#endif 33 34class ForestCell; 35class Forest; 36class Frustum; 37 38 39/// This is the data file for Forests. 40class ForestData 41{ 42 protected: 43 44 enum { FILE_VERSION = 1 }; 45 46 /// Set the bucket dimensions to 2km x 2km. 47 static const U32 BUCKET_DIM = 2000; 48 49 /// Set to true if the file is dirty and 50 /// needs to be saved before being destroyed. 51 bool mIsDirty; 52 53 /// 54 typedef HashTable<Point2I,ForestCell*> BucketTable; 55 56 /// The top level cell buckets which allows us 57 /// to have virtually unbounded range. 58 BucketTable mBuckets; 59 60 /// The next free item id. 61 static U32 smNextItemId; 62 63 /// Converts a ForestItem's Point3F 'KeyPosition' to a Point2I 64 /// key we index into BucketTable with. 65 static Point2I _getBucketKey( const Point3F &pos ); 66 67 /// Finds the bucket with the given Point2I key or returns NULL. 68 ForestCell* _findBucket( const Point2I &key ) const; 69 70 /// Finds the best top level bucket for the ForestItem 'key' position 71 /// or returns NULL. 72 ForestCell* _findBucket( const Point3F &pos ) const; 73 74 /// Find the best top level bucket for the given position 75 /// or returns a new one. 76 ForestCell* _findOrCreateBucket( const Point3F &pos ); 77 78 void _onItemReload(); 79 80 81 public: 82 83 ForestData(); 84 virtual ~ForestData(); 85 86 bool isDirty() const { return mIsDirty; } 87 88 /// Deletes all the data and resets the 89 /// file to an empty state. 90 void clear(); 91 92 /// Helper for debugging cell generation. 93 void regenCells(); 94 95 /// 96 bool read( Stream &stream ); 97 98 /// 99 bool write( const char *path ); 100 101 const ForestItem& addItem( ForestItemData *data, 102 const Point3F &position, 103 F32 rotation, 104 F32 scale ); 105 106 const ForestItem& addItem( ForestItemKey key, 107 ForestItemData *data, 108 const MatrixF &xfm, 109 F32 scale ); 110 111 const ForestItem& updateItem( ForestItemKey key, 112 const Point3F &keyPosition, 113 ForestItemData *newData, 114 const MatrixF &newXfm, 115 F32 newscale ); 116 117 const ForestItem& updateItem( ForestItem &item ); 118 119 bool removeItem( ForestItemKey key, const Point3F &keyPosition ); 120 121 /// Performs a search using the position to limit tested cells. 122 const ForestItem& findItem( ForestItemKey key, const Point3F &keyPosition ) const; 123 124 /// Does an exhaustive search thru all cells looking for 125 /// the item. This method is slow and should be avoided. 126 const ForestItem& findItem( ForestItemKey key ) const; 127 128 /// Fills a vector with a copy of all the items in the data set. 129 /// 130 /// @param outItems The output vector of items. 131 /// @return The count of items found. 132 /// 133 U32 getItems( Vector<ForestItem> *outItems ) const; 134 135 /// Fills a vector with a copy of all items in the Frustum. 136 /// Note that this IS expensive and this is not how Forest internally 137 /// collects items for rendering. This is here for ForestSelectionTool. 138 /// 139 /// @param The Frustum to cull with. 140 /// @param outItems The output vector of items. 141 /// @return The count of items found. 142 /// 143 U32 getItems( const Frustum &culler, Vector<ForestItem> *outItems ) const; 144 145 /// Returns a copy of all the items that intersect the box. If 146 /// the output vector is NULL then it will early out on the first 147 /// found item returning 1. 148 /// 149 /// @param box The search box. 150 /// @param outItems The output vector of items or NULL. 151 /// @return The count of items found. 152 /// 153 U32 getItems( const Box3F &box, Vector<ForestItem> *outItems ) const; 154 155 /// Returns a copy of all the items that intersect the sphere. If 156 /// the output vector is NULL then it will early out on the first 157 /// found item returning 1. 158 /// 159 /// @param point The center of the search sphere. 160 /// @param radius The radius of the search sphere. 161 /// @param outItems The output vector of items or NULL. 162 /// @return The count of items found. 163 /// 164 U32 getItems( const Point3F &point, F32 radius, Vector<ForestItem> *outItems ) const; 165 166 /// Returns a copy of all the items that intersect the 2D circle ignoring 167 /// the z component. If the output vector is NULL then it will early out 168 /// on the first found item returning 1. 169 /// 170 /// @param point The center point of the search circle. 171 /// @param radius The radius of the search circle. 172 /// @param outItems The output vector of items or NULL. 173 /// @return The count of items found. 174 /// 175 U32 getItems( const Point2F &point, F32 radius, Vector<ForestItem> *outItems ) const; 176 177 /// Returns a copy of all the items that share the input item datablock. 178 /// 179 /// @param data The datablock to search for. 180 /// @param outItems The output vector of items. 181 /// @return The count of items found. 182 /// 183 U32 getItems( const ForestItemData *data, Vector<ForestItem> *outItems ) const; 184 185 /// Returns all the top level cells which intersect the frustum. 186 void getCells( const Frustum &frustum, Vector<ForestCell*> *outCells ) const; 187 188 /// Returns all top level cells. 189 void getCells( Vector<ForestCell*> *outCells ) const; 190 191 /// Gathers all the datablocks used and returns the count. 192 U32 getDatablocks( Vector<ForestItemData*> *outVector ) const; 193 194 /// 195 bool castRay( const Point3F &start, const Point3F &end, RayInfo *outInfo, bool rendered ) const; 196 197 /// 198 void clearPhysicsRep( Forest *forest ); 199 void buildPhysicsRep( Forest *forest ); 200}; 201 202inline Point2I ForestData::_getBucketKey( const Point3F &pos ) 203{ 204 return Point2I ( (S32)mFloor(pos.x / BUCKET_DIM) * BUCKET_DIM, 205 (S32)mFloor(pos.y / BUCKET_DIM) * BUCKET_DIM ); 206} 207 208inline ForestCell* ForestData::_findBucket( const Point3F &pos ) const 209{ 210 return _findBucket( _getBucketKey( pos ) ); 211} 212 213#endif // _FORESTDATAFILE_H_ 214