AGX Dynamics  2.35.0.1
TerrainCache.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. 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 
85 
89  virtual ~TerrainCache();
90 
96  void prepareTerrainInstances( const TileSpecification& ts, size_t poolSize );
97 
98 
105 
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 
163 
164 
168 
169 
174 
178  agx::String getFileCacheDirectory() const;
179 
183 
187  void waitForTile();
188 
189 
194 
195 
201 
202 
212 
213 
214 
215 
220  void clearCache();
221 
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 {
252  agx::RealPair zMinMax;
253  agx::RealVector heights;
254  size_t numberOfModifiedVoxels;
255  agx::Vector<agx::RealVector> compactionVector;
256  //modifiedCompactionVector compactionVector;
257  agx::Vec2iVector compactedIndices;
258  };
259 
260 
261  size_t readTerrainHeightsAndCompaction(agxTerrain::Terrain* t, agx::RealVector& heightsOutputVector, agx::Vector<agx::RealVector>& compactionOutputVector, agx::Vec2iVector& compactedIndices, agxTerrain::TileId id);
262 
263  void readTerrainHeights( agxTerrain::Terrain* t, agx::RealVector& outputVector );
264 
265  //
266  void backgroundOperations();
267 
268 
269  void performLoadTile( agxTerrain::TileSpecification ts, agxTerrain::TileId id );
270 
271  void performUnloadTile( TerrainTile tile );
272 
273  agx::String getFilenameForTile( agxTerrain::TileId id );
274 
275  bool writeCacheFile( agx::String filename, const CachedTile& tile );
276 
277  bool readCacheFile( agx::String filename, CachedTile& output );
278 
279 
280  void storeCachedTile( agxStream::OutputArchive& out, const TileId& tileId, const CachedTile& tile );
281  void restoreCachedTile( agxStream::InputArchive& in, TileId& tileId, CachedTile& tile );
282 
294  agxTerrain::TerrainRef getTerrainInstance( const TileSpecification& ts );
295 
296  agxTerrain::Terrain* createTerrainInstance( const TileSpecification& ts );
297 
298 
299  // Terrain instances held in the pool must have their data in storages
300  // belonging to the simulation or main thread
301  agxTerrain::TerrainRefVector m_terrainPool;
302 
303  // These must be moved to the pool in a thread safe way
304  agxTerrain::TerrainRefVector m_terrainStaging;
305 
306  agxTerrain::TerrainDataSourceRef m_terrainDataSource;
307  agxTerrain::TerrainRef m_templateTerrain;
308 
311 
312  agx::String m_fileCacheDir;
313 
314 
315  std::thread m_backgroundThread;
316 
317  std::mutex m_mutex;
318  std::mutex m_createTerrainMutex;
319 
320  std::condition_variable m_producerCondition; // background thread waits on this condition when it's pause or has nothing to do
321 
322  std::condition_variable m_consumerCondition; // main thread waits on this condition when it requires a tile to consume
323 
324  std::condition_variable m_clearCondition; // main thread waits on this condition when the background thread should clear stuff
325 
326  bool m_backgroundThreadPaused; // Flag to indicate that the background thread has reached the waiting/pause block.
327  // There is more than one reason why the background thread could wake the main thread
328  // and it must be possible to see why.
329 
330  bool m_shutdownThread;
331  bool m_pauseThread;
332  bool m_performClear;
333 
334  bool m_shouldStoreCompaction;
335  bool m_shouldEmbedCachedFiles;
336 
337  size_t m_maximumCompactionDepth;
338 
339 
340  // Shared structures:
342 
343 
344  std::deque< std::pair<agxTerrain::TileSpecification, agxTerrain::TileId> > m_loadRequests;
345  //std::deque< std::pair<agxTerrain::TerrainTile, agxTerrain::TileId> > m_unloadRequests;
346  std::deque< agxTerrain::TerrainTile > m_unloadRequests;
347 
348  std::queue< agxTerrain::TerrainTile > m_readyTiles;
349  };
350 
351 
352 
354  {
355  m_shouldStoreCompaction = shouldStoreCompaction;
356  }
357 
359  {
360  return m_shouldStoreCompaction;
361  }
362 
364  {
365  m_maximumCompactionDepth = maximumCompactionDepth;
366  }
367 
369  {
370  return &m_compressedIndices;
371  }
372 
374  {
375  return m_terrainDataSource;
376  }
377 
379  {
380  return m_templateTerrain;
381  }
382 
384  {
385  m_templateTerrain = templateTerrain;
386  }
387 
388 
390  {
391  return m_fileCacheDir;
392  }
393 
394 
396  {
397  m_shouldEmbedCachedFiles = embed;
398  }
399 
401  {
402  return m_shouldEmbedCachedFiles;
403  }
404 
405 
406 
407 }
408 
#define AGXTERRAIN_EXPORT
Class for reading a binary stream of serialized data.
Definition: InputArchive.h:52
Class for writing serialized data in binary format to a stream.
Definition: OutputArchive.h:57
The TerrainCache is used internally by the TerrainPager to provide requested terrain tiles.
Definition: TerrainCache.h:54
void setShouldStoreCompaction(bool shouldStoreCompaction)
Sets if the terrain pager should store compaction data for tiles that are paged out.
Definition: TerrainCache.h:353
void unloadTerrainTile(TerrainTile tile)
Schedule a tile for unloading.
agx::String getFileCacheDirectory() const
Definition: TerrainCache.h:389
bool getShouldStoreCompaction() const
Returns whether the terrain pager should store compaction data for tiles that are paged out.
Definition: TerrainCache.h:358
void prepareTerrainInstances(const TileSpecification &ts, size_t poolSize)
Create agxTerrain::Terrain instances so that there are poolSize number of Terrains in the pool for fu...
@ TS_RETURNED
Tile has been fetched by the TerrainPager.
Definition: TerrainCache.h:74
@ TS_LOADED
Tile not seen before, must be loaded from data source.
Definition: TerrainCache.h:72
@ TS_ACTIVE
Tile is loaded and ready to be used by the TerrainPager.
Definition: TerrainCache.h:73
@ TS_IN_RAM
Tile has been returned to the Cache by the TerrainPager.
Definition: TerrainCache.h:75
void setMaximumCompactionDepth(size_t maximumCompactionDepth)
Definition: TerrainCache.h:363
void setTerrainDataSource(TerrainDataSource *tds)
Sets the data source for height data.
void setRestoreData(const TileModificationVector &state)
Specifies the restore data that is used to apply changes the first time they are needed after restore...
virtual ~TerrainCache()
Destructor.
agxTerrain::TerrainTile getReadyTile()
Without blocking, this method tries to return a ready processed terrain tile.
TerrainCache()
Constructor.
void waitForTile()
Waits until one tile is ready.
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:395
void clearCache()
Removes all internal state.
agxTerrain::TerrainRef getTemplateTerrain()
Returns the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:378
TerrainDataSource * getTerrainDataSource() const
Returns the data source used for height data.
Definition: TerrainCache.h:373
void applyChangesToTemplateTerrain(agxTerrain::Terrain *t)
bool setFileCacheDirectory(agx::String dir)
Specifies where paged out height data for terrain should be stored.
TileStatus getTileStatus(TileId id) const
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainCache.h:400
void handleStagedTerrain()
Checks if there is staged terrain which can be moved to the pool for future usage.
bool haveReadyTile()
Checks if one or more tiles are ready.
agx::HashTable< agxTerrain::TileId, agx::Vec2iVector > * getCompressedIndices()
Definition: TerrainCache.h:368
void setTemplateTerrain(agxTerrain::TerrainRef templateTerrain)
Sets the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:383
void loadTerrainTile(const TileSpecification &ts, TileId id, TilePriority prio)
Load a terrain tile, either from cache (memory or disk) or request the tile from a data source.
Abstract interface for requesting height data for a Terrain tile.
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
A TileSpecification determines where in the world a 2D TileId is located and contains utility methods...
Inheritance with partial specialization due to bug with ref_ptr containers.
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
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
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.
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:60
agx::VectorPOD< agx::RealVector > modifiedCompactionVector
Definition: TerrainCache.h:44
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
LinearProbingHashTable< KeyT, DataT, HashT, AllocatorT > HashTable
Definition: HashTable.h:24
std::pair< Real, Real > RealPair
Struct with data to describe a terrain tile.
Definition: TerrainCache.h:38
agx::RealPair zMinMax
Definition: TerrainCache.h:41
agxTerrain::TileId id
Definition: TerrainCache.h:39
agxTerrain::TerrainRef terrain
Definition: TerrainCache.h:40