AGX Dynamics  2.35.0.1
TerrainPager.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/HashSet.h>
20 #include <agx/HashTable.h>
21 
22 #include <agx/config/AGX_USE_AGXTERRAIN.h>
23 #include <agxTerrain/export.h>
24 
26 
29 
31 
32 namespace agxTerrain
33 {
34 
35  class TerrainCache;
36  class TerrainDataSource;
37 
38 
39 
98  {
99  public:
100 
103 
107 
113 
115  };
116 
118 
143  TerrainPager(size_t tileResolution, size_t tileOverlap, agx::Real tileElementSize, agx::Real maximumDepth,
144  agx::Vec3 refPoint, agx::Quat refRotation,
145  agxTerrain::Terrain* templateTerrain );
146 
147 
154  //void setTerrainPlaneLocation( agx::Vec3 referencePoint, agx::Quat referenceRotation );
155 
161 
162 
167 
172 
179  void setShouldStoreCompaction(bool shouldStoreCompaction, size_t maximumCompactionDepth);
180 
185 
192  void setDeformable( bool deformable );
193 
199  inline bool getDeformable() const;
200 
205  void setEmbedCacheFilesInArchive( bool embed );
206 
211  bool getEmbedCacheFilesInArhive() const;
212 
217  void setShouldStoreDeltas(bool shouldStoreDeltas);
218 
223 
228 
233 
239  bool add( agx::RigidBody* body, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
240 
246  bool add( agxTerrain::Shovel* shovel, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
247 
248 
254  bool remove( agx::RigidBody* body );
255 
261  bool remove( agxTerrain::Shovel* shovel );
262 
271 
278 
279 
280 
286  bool setTileLoadRadiuses( agx::RigidBody* body, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
287 
293 
298 
304 
305 
306 
310  virtual void addNotification() override;
311 
315  virtual void removeNotification() override;
316 
317 
318 
322  virtual void preCollide( const agx::TimeStamp& time ) override;
323 
324 
330  virtual void last( const agx::TimeStamp& time ) override;
331 
332 
337 
338  RAYCAST_NO_RESULT, // No hit
339  RAYCAST_HIT_ACTIVE_TERRAIN_TILE, // Hit active paged in tile
340  RAYCAST_RESULT_FROM_DATASOURCE // Hit via TerrainDataSource
341  };
342 
357 
358 
359  using StepEventListener::store;
360 
361  using StepEventListener::restore;
362 
366  void store(/*agxStream::StorageStream& str*/agxTerrain::TileModificationVector& storeData);
367 
368 
372  void restore(/*agxStream::StorageStream& str*/ const agxTerrain::TileModificationVector& restoreData);
373 
374 
380 
386 
396 
400 
401  protected:
406 
407  virtual ~TerrainPager();
408 
409  void updateActiveTiles();
410 
411  void unloadUnusedTiles( bool partOfRemoveNotification );
412 
413  void handleNewTerrainTile( agxTerrain::TerrainTile );
414 
415  void unloadAllTiles();
416 
417  void prepareSimulation(bool fromRestore);
418 
419  agx::Real getOverlappingAreaSolidMass(agxTerrain::TileId, agxTerrain::TileId);
420 
421  private:
422  mutable TerrainCache m_cache;
423 
424  TileSpecification m_tileSpec;
425 
426  bool m_shouldStoreDeltas;
427 
428  bool m_justRestored;
429 
430  struct BodyAttachments {
431  //agx::RigidBodyRef m_body;
432  agx::Real m_requiredRadius;
433  agx::Real m_preloadRadius;
434  agxTerrain::ShovelRef m_shovel;
435  };
436 
437  typedef std::pair<TileId, agx::Vec2i> GlobalTerrainIndex;
438 
439  // Bodies this TerrainPager keeps track of
441 
442  // Tiles this TerrainPager keeps track of
444 
445  // Stores heightfield changes since the last call to the store method
447  //agx::HashTable< TileId, TerrainIndexHeight > m_heightFieldDelta;
448 
450 
451  agx::Vector<agxTerrain::TileId> m_missingRequiredTiles;
452  agx::HashSet<agxTerrain::TileId> m_pendingRequests;
453 
454 
455 
456  void fillTileAttachment( agxTerrain::TerrainTile tile, agxTerrain::TerrainPager::TileAttachments& ta );
457 
458  void getCompactionFromNeighbourTiles(agxTerrain::TerrainTile tile);
459 
460  void fillHeightDelta(const GlobalTerrainIndex& gtid, agx::Real height);
461 
462  };
463 
464 
465  inline void TerrainPager::fillHeightDelta(const GlobalTerrainIndex& gtid, agx::Real height)
466  {
467  m_heightFieldDelta[gtid] = height;
468  }
469 
470 
472  {
473  m_cache.setEmbedCacheFilesInArchive( embed );
474  }
475 
476 
478  {
479  return m_cache.getEmbedCacheFilesInArhive();
480  }
481 
482  inline bool TerrainPager::getDeformable() const
483  {
484  return m_cache.getTemplateTerrain()->getDeformable();
485  }
486 }
487 
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
#define AGXTERRAIN_EXPORT
Derive from this class to implement a listener for simulation step events.
Interface class for accessing data and functions for the internal soil particle simulation used in ag...
The TerrainCache is used internally by the TerrainPager to provide requested terrain tiles.
Definition: TerrainCache.h:54
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
agxTerrain::TerrainRef getTemplateTerrain()
Returns the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:378
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainCache.h:400
Abstract interface for requesting height data for a Terrain tile.
A pager that will dynamically handle terrain tiles in a agxSDK::Simulation.
Definition: TerrainPager.h:98
TileUnloadEvent tileUnloadEvent
An event that the TerrainPager will trigger just before it removes a tile from the simulation.
Definition: TerrainPager.h:385
bool add(agx::RigidBody *body, agx::Real requiredTileRadius, agx::Real preloadTileRadius)
Adds a RigidBody to the terrain pager.
const TileSpecification & getTileSpecification() const
Specifies a new reference point for the terrain grid tiles.
TerrainPager(size_t tileResolution, size_t tileOverlap, agx::Real tileElementSize, agx::Real maximumDepth, agx::Vec3 refPoint, agx::Quat refRotation, agxTerrain::Terrain *templateTerrain)
Constructor.
agx::String getFileCacheDirectory() const
Return the location for where files should be temporary cached when not needed in memory.
agx::Callback2< agxTerrain::TileId, agxTerrain::Terrain * > TileCallback
Callback type.
Definition: TerrainPager.h:102
bool remove(agxTerrain::Shovel *shovel)
Removes a shovel object from the terrain pager, if it exists.
TerrainRaycastResult raycast(agx::Vec3 start, agx::Vec3 end, agx::Vec3 &result)
Perform raycasting against the paged terrain.
virtual void removeNotification() override
Performs cleanup when removed from the simulation.
agx::Event2< agxTerrain::TileId, agxTerrain::Terrain * > TileUnloadEvent
Definition: TerrainPager.h:106
virtual void preCollide(const agx::TimeStamp &time) override
Callback from Simulation.
TileAttachmentPtrVector getActiveTileAttachments()
Returns a vector of the terrains currently tracked by this pager.
void setTerrainDataSource(TerrainDataSource *tds)
Sets the data source that the pager needs to fetch terrain tile data.
void restore(const agxTerrain::TileModificationVector &restoreData)
Restores internal data from stream.
bool setTileLoadRadiuses(agx::RigidBody *body, agx::Real requiredTileRadius, agx::Real preloadTileRadius)
Set tile radiuses for a already added RigidBody.
agx::Real getTotalTerrainMass()
Calculates the total mass of all currently paged in terrain tiles.
bool getDeformable() const
Gets whether or not deformations are enabled for the tiles managed by this pager.
Definition: TerrainPager.h:482
bool getShouldStoreCompaction() const
Returns whether the terrain pager should store compaction data for tiles that are paged out.
agx::Vector< TerrainPager::TileAttachments * > TileAttachmentPtrVector
Definition: TerrainPager.h:117
TerrainDataSource * getTerrainDataSource()
Returns the data source the pager uses for fetching height data for tiles.
agxTerrain::Terrain * getTemplateTerrain()
Fetches the template terrain.
TerrainRaycastResult
Enum with different types of result from the raycast method.
Definition: TerrainPager.h:336
void applyChangesToTemplateTerrain()
Signals to the TerrainPager that the template terrain has been updated and that any changes to the te...
bool add(agxTerrain::Shovel *shovel, agx::Real requiredTileRadius, agx::Real preloadTileRadius)
Adds a Shovel and its RigidBody to the terrain pager.
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainPager.h:477
SoilSimulationInterface * getSoilSimulationInterface()
Returns the soil simulation interface used by the terrains tracked by this pager or nullptr if there ...
void setShouldStoreDeltas(bool shouldStoreDeltas)
Specify whether the terrain pager should store changes to the terrain every time step.
agx::Event2< agxTerrain::TileId, agxTerrain::Terrain * > TileLoadEvent
Event types.
Definition: TerrainPager.h:105
void store(agxTerrain::TileModificationVector &storeData)
Stores internal data into stream.
TileLoadEvent tileLoadEvent
An event that the TerrainPager will trigger after a new tile as been loaded and added to the simulati...
Definition: TerrainPager.h:379
bool remove(agx::RigidBody *body)
Remove body so that the terrain pager no longer uses the body when determining which terrain tiles th...
void setShouldStoreCompaction(bool shouldStoreCompaction, size_t maximumCompactionDepth)
Sets if the terrain pager should store compaction data for tiles that are paged out.
agx::RealPair getTileLoadRadius(agx::RigidBody *body)
Get the tile readiuses for a body.
bool setFileCacheDirectory(agx::String dir)
Specify the location for there terrain tiles should be stored when cached to disk.
virtual void addNotification() override
Notification to the TerrainPager when being added to a Simulation.
bool getShouldStoreDeltas()
Return whether the terrain pager should store changes to the terrain every time step.
virtual void last(const agx::TimeStamp &time) override
Callback from Simulation.
void setEmbedCacheFilesInArchive(bool embed)
Sets if cached files written out to disk should be embeded in AGX Archives when store is called.
Definition: TerrainPager.h:471
void setDeformable(bool deformable)
Sets if deformation should be enabled for terrains managed by this pager.
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...
Templated callback with two arguments.
Definition: Callback.h:140
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:666
Inheritance with partial specialization due to bug with ref_ptr containers.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
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
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:60
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
double Real
Definition: Real.h:42
std::pair< Real, Real > RealPair
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
agx::Real TimeStamp
Definition: TimeStamp.h:26
Struct with data to describe a terrain tile.
Definition: TerrainCache.h:38