AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
TerrainCache.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or
9having been advised so by Algoryx Simulation AB for a time limited evaluation,
10or having purchased a valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx 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
32namespace agxTerrain
33{
34
43
45
54 {
55 public:
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 */
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
188
189
194
195
201
202
212
213
214
215
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 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
287 void restoreTileCompaction( const CachedTile& ct, agxTerrain::Terrain* terrain );
288
300 agxTerrain::TerrainRef getTerrainInstance( const TileSpecification& ts );
301
302 agxTerrain::Terrain* createTerrainInstance( const TileSpecification& ts );
303
304
305 // Terrain instances held in the pool must have their data in storages
306 // belonging to the simulation or main thread
307 agxTerrain::TerrainRefVector m_terrainPool;
308
309 // These must be moved to the pool in a thread safe way
310 agxTerrain::TerrainRefVector m_terrainStaging;
311
312 agxTerrain::TerrainDataSourceRef m_terrainDataSource;
313 agxTerrain::TerrainRef m_templateTerrain;
314
317
318 agx::String m_fileCacheDir;
319
320
321 std::thread m_backgroundThread;
322
323 std::mutex m_mutex;
324 std::mutex m_createTerrainMutex;
325
326 std::condition_variable m_producerCondition; // background thread waits on this condition when it's pause or has nothing to do
327
328 std::condition_variable m_consumerCondition; // main thread waits on this condition when it requires a tile to consume
329
330 std::condition_variable m_clearCondition; // main thread waits on this condition when the background thread should clear stuff
331
332 bool m_backgroundThreadPaused; // Flag to indicate that the background thread has reached the waiting/pause block.
333 // There is more than one reason why the background thread could wake the main thread
334 // and it must be possible to see why.
335
336 bool m_shutdownThread;
337 bool m_pauseThread;
338 bool m_performClear;
339
340 bool m_shouldStoreCompaction;
341 bool m_shouldEmbedCachedFiles;
342
343 size_t m_maximumCompactionDepth;
344
345
346 // Shared structures:
348
349
350 std::deque< std::pair<agxTerrain::TileSpecification, agxTerrain::TileId> > m_loadRequests;
351 //std::deque< std::pair<agxTerrain::TerrainTile, agxTerrain::TileId> > m_unloadRequests;
352 std::deque< agxTerrain::TerrainTile > m_unloadRequests;
353
354 std::queue< agxTerrain::TerrainTile > m_readyTiles;
355 };
356
357
358
360 {
361 m_shouldStoreCompaction = shouldStoreCompaction;
362 }
363
365 {
366 return m_shouldStoreCompaction;
367 }
368
370 {
371 m_maximumCompactionDepth = maximumCompactionDepth;
372 }
373
378
380 {
381 return m_terrainDataSource;
382 }
383
388
390 {
391 m_templateTerrain = templateTerrain;
392 }
393
394
396 {
397 return m_fileCacheDir;
398 }
399
400
402 {
403 m_shouldEmbedCachedFiles = embed;
404 }
405
407 {
408 return m_shouldEmbedCachedFiles;
409 }
410
411
412
413}
414
#define AGXTERRAIN_EXPORT
Class for reading a binary stream of serialized data.
Class for writing serialized data in binary format to a stream.
The TerrainCache is used internally by the TerrainPager to provide requested terrain tiles.
void setShouldStoreCompaction(bool shouldStoreCompaction)
Sets if the terrain pager should store compaction data for tiles that are paged out.
void unloadTerrainTile(TerrainTile tile)
Schedule a tile for unloading.
agx::String getFileCacheDirectory() const
bool getShouldStoreCompaction() const
Returns whether the terrain pager should store compaction data for tiles that are paged out.
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.
@ TS_LOADED
Tile not seen before, must be loaded from data source.
@ TS_ACTIVE
Tile is loaded and ready to be used by the TerrainPager.
@ TS_IN_RAM
Tile has been returned to the Cache by the TerrainPager.
void setMaximumCompactionDepth(size_t maximumCompactionDepth)
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.
void clearCache()
Removes all internal state.
agxTerrain::TerrainRef getTemplateTerrain()
Returns the template terrain used for properties of new terrain tiles.
TerrainDataSource * getTerrainDataSource() const
Returns the data source used for height data.
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.
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()
void setTemplateTerrain(agxTerrain::TerrainRef templateTerrain)
Sets the template terrain used for properties of new terrain tiles.
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...
Definition Terrain.h:86
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...
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition Geometry.h:59
agx::VectorPOD< agx::RealVector > modifiedCompactionVector
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
std::pair< Real, Real > RealPair
STL namespace.
Struct with data to describe a terrain tile.
agxTerrain::TileId id
agxTerrain::TerrainRef terrain