AGX Dynamics  2.37.2.0
TerrainCache.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or
9 having been advised so by Algoryx Simulation AB for a time limited evaluation,
10 or having purchased a valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/config/AGX_USE_AGXTERRAIN.h>
20 
21 #include <agx/Thread.h>
22 
23 #include <agxUtil/agxUtil.h>
24 
25 #include <agxTerrain/export.h>
26 #include <agxTerrain/Terrain.h>
28 
30 
31 
32 namespace agxTerrain
33 {
34 
38  struct TerrainTile {
42  };
43 
45 
54  {
55  public:
56  enum TilePriority {
58  TP_LOW
59  };
60 
61 
62  /*
63  Possible state transitions:
64 
65  MISSING -> LOADED -> ACTIVE -> RETURNED
66  RETURNED -> LOADED
67  RETURNED -> ON_DISK -> LOADED
68  RETURNED -> IN_RAM -> LOADED
69  */
70  enum TileStatus {
76  TS_ON_DISK
77  };
78 
79 
80 
84  TerrainCache();
85 
89  virtual ~TerrainCache();
90 
96  void prepareTerrainInstances( const TileSpecification& ts, size_t poolSize );
97 
98 
104  void loadTerrainTile( const TileSpecification& ts, TileId id, TilePriority prio);
105 
111  void unloadTerrainTile( TerrainTile tile );
112 
116  agxTerrain::TerrainRef getTemplateTerrain();
117 
118 
122  void setTemplateTerrain(agxTerrain::TerrainRef templateTerrain);
123 
127  void setShouldStoreCompaction(bool shouldStoreCompaction);
128 
132  bool getShouldStoreCompaction() const;
133 
134  void setMaximumCompactionDepth(size_t maximumCompactionDepth);
135 
137 
138 
143  void setEmbedCacheFilesInArchive( bool embed );
144 
149  bool getEmbedCacheFilesInArhive() const;
150 
151 
152 
156  TerrainDataSource* getTerrainDataSource() const;
157 
162  void setTerrainDataSource( TerrainDataSource* tds );
163 
164 
167  TileStatus getTileStatus( TileId id ) const;
168 
169 
173  bool setFileCacheDirectory( agx::String dir );
174 
178  agx::String getFileCacheDirectory() const;
179 
182  void applyChangesToTemplateTerrain(agxTerrain::Terrain* t);
183 
187  void waitForTile();
188 
189 
193  bool haveReadyTile();
194 
195 
200  agxTerrain::TerrainTile getReadyTile();
201 
202 
211  void setRestoreData( const TileModificationVector& state );
212 
213 
214 
215 
220  void clearCache();
221 
225  void handleStagedTerrain();
226 
227 
228  public:
229 
231  void store( agxStream::OutputArchive& out );
232  void restore( agxStream::InputArchive& in, const agxTerrain::TileSpecification& tileSpec );
234 
235 
236  protected:
237  void pauseBackgroundThread();
238 
239  void resumeBackgroundThread();
240 
241  private:
242 
243  TileSpecification m_tileSpec; // we need our own copy
244 
245  TileModificationVector m_restoreData;
246  agx::HashTable < TileId, std::pair<size_t, size_t>> m_restoreDataTileIdTable;
247 
248  /*
249 
250  */
251  struct CachedTile {
253  agx::RealVector heights;
254  size_t numberOfModifiedVoxels;
255  agx::Vector<agx::RealVector> compactionVector;
256  agx::Vec2iVector compactedIndices;
257  size_t numberOfInhomogeneousVoxels;
258  agx::Vec3iVector voxelIndices;
259  agx::UInt32Vector terrainMaterialIndices;
260  };
261 
262 
263  size_t readTerrainHeightsAndCompaction(agxTerrain::Terrain* t, agx::RealVector& heightsOutputVector, agx::Vector<agx::RealVector>& compactionOutputVector, agx::Vec2iVector& compactedIndices, agxTerrain::TileId id);
264 
265  void readTerrainHeights( agxTerrain::Terrain* t, agx::RealVector& outputVector );
266 
267  void readTerrainMaterials(agxTerrain::Terrain* t, agx::Vec3iVector& outputVoxelIndices, agx::UInt32Vector& outputTerrainMaterialIndices);
268 
269  //
270  void backgroundOperations();
271 
272 
273  void performLoadTile( agxTerrain::TileSpecification ts, agxTerrain::TileId id );
274 
275  void performUnloadTile( TerrainTile tile );
276 
277  agx::String getFilenameForTile( agxTerrain::TileId id );
278 
279  bool writeCacheFile( agx::String filename, const CachedTile& tile );
280 
281  bool readCacheFile( agx::String filename, CachedTile& output );
282 
283 
284  void storeCachedTile( agxStream::OutputArchive& out, const TileId& tileId, const CachedTile& tile );
285  void restoreCachedTile( agxStream::InputArchive& in, TileId& tileId, CachedTile& tile );
286 
298  agxTerrain::TerrainRef getTerrainInstance( const TileSpecification& ts );
299 
300  agxTerrain::Terrain* createTerrainInstance( const TileSpecification& ts );
301 
302 
303  // Terrain instances held in the pool must have their data in storages
304  // belonging to the simulation or main thread
305  agxTerrain::TerrainRefVector m_terrainPool;
306 
307  // These must be moved to the pool in a thread safe way
308  agxTerrain::TerrainRefVector m_terrainStaging;
309 
310  agxTerrain::TerrainDataSourceRef m_terrainDataSource;
311  agxTerrain::TerrainRef m_templateTerrain;
312 
315 
316  agx::String m_fileCacheDir;
317 
318 
319  std::thread m_backgroundThread;
320 
321  std::mutex m_mutex;
322  std::mutex m_createTerrainMutex;
323 
324  std::condition_variable m_producerCondition; // background thread waits on this condition when it's pause or has nothing to do
325 
326  std::condition_variable m_consumerCondition; // main thread waits on this condition when it requires a tile to consume
327 
328  std::condition_variable m_clearCondition; // main thread waits on this condition when the background thread should clear stuff
329 
330  bool m_backgroundThreadPaused; // Flag to indicate that the background thread has reached the waiting/pause block.
331  // There is more than one reason why the background thread could wake the main thread
332  // and it must be possible to see why.
333 
334  bool m_shutdownThread;
335  bool m_pauseThread;
336  bool m_performClear;
337 
338  bool m_shouldStoreCompaction;
339  bool m_shouldEmbedCachedFiles;
340 
341  size_t m_maximumCompactionDepth;
342 
343 
344  // Shared structures:
346 
347 
348  std::deque< std::pair<agxTerrain::TileSpecification, agxTerrain::TileId> > m_loadRequests;
349  //std::deque< std::pair<agxTerrain::TerrainTile, agxTerrain::TileId> > m_unloadRequests;
350  std::deque< agxTerrain::TerrainTile > m_unloadRequests;
351 
352  std::queue< agxTerrain::TerrainTile > m_readyTiles;
353  };
354 
355 
356 
358  {
359  m_shouldStoreCompaction = shouldStoreCompaction;
360  }
361 
363  {
364  return m_shouldStoreCompaction;
365  }
366 
368  {
369  m_maximumCompactionDepth = maximumCompactionDepth;
370  }
371 
373  {
374  return &m_compressedIndices;
375  }
376 
378  {
379  return m_terrainDataSource;
380  }
381 
383  {
384  return m_templateTerrain;
385  }
386 
388  {
389  m_templateTerrain = templateTerrain;
390  }
391 
392 
394  {
395  return m_fileCacheDir;
396  }
397 
398 
400  {
401  m_shouldEmbedCachedFiles = embed;
402  }
403 
405  {
406  return m_shouldEmbedCachedFiles;
407  }
408 
409 
410 
411 }
412 
std::pair< Real, Real > RealPair
Struct with data to describe a terrain tile.
Definition: TerrainCache.h:38
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
A TileSpecification determines where in the world a 2D TileId is located and contains utility methods...
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
agx::RealPair zMinMax
Definition: TerrainCache.h:41
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes...
Definition: Geometry.h:59
agxTerrain::TileId id
Definition: TerrainCache.h:39
STL namespace.
Tile is loaded and ready to be used by the TerrainPager.
Definition: TerrainCache.h:73
bool getShouldStoreCompaction() const
Returns whether the terrain pager should store compaction data for tiles that are paged out...
Definition: TerrainCache.h:362
#define AGX_FORCE_INLINE
Definition: Object.h:59
agxTerrain::TerrainRef getTemplateTerrain()
Returns the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:382
Templated vector class.
Definition: Vector.h:52
agx::VectorPOD< agx::RealVector > modifiedCompactionVector
Definition: TerrainCache.h:44
Tile has been fetched by the TerrainPager.
Definition: TerrainCache.h:74
Tile has been returned to the Cache by the TerrainPager.
Definition: TerrainCache.h:75
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
Tile not seen before, must be loaded from data source.
Definition: TerrainCache.h:72
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void setTemplateTerrain(agxTerrain::TerrainRef templateTerrain)
Sets the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:387
The TerrainCache is used internally by the TerrainPager to provide requested terrain tiles...
Definition: TerrainCache.h:53
void setEmbedCacheFilesInArchive(bool embed)
Sets if cached files written out to disk should be embeded in AGX Archives when store is called...
Definition: TerrainCache.h:399
#define AGXTERRAIN_EXPORT
void setMaximumCompactionDepth(size_t maximumCompactionDepth)
Definition: TerrainCache.h:367
agx::HashTable< agxTerrain::TileId, agx::Vec2iVector > * getCompressedIndices()
Definition: TerrainCache.h:372
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:51
TerrainDataSource * getTerrainDataSource() const
Returns the data source used for height data.
Definition: TerrainCache.h:377
Abstract interface for requesting height data for a Terrain tile.
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:56
agx::String getFileCacheDirectory() const
Definition: TerrainCache.h:393
void setShouldStoreCompaction(bool shouldStoreCompaction)
Sets if the terrain pager should store compaction data for tiles that are paged out.
Definition: TerrainCache.h:357
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
Inheritance with partial specialization due to bug with ref_ptr containers.
LinearProbingHashTable< KeyT, DataT, HashT, AllocatorT > HashTable
Definition: HashTable.h:24
agxTerrain::TerrainRef terrain
Definition: TerrainCache.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainCache.h:404
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97