AGX Dynamics 2.42.1.1
Loading...
Searching...
No Matches
TerrainPager.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. 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/HashSet.h>
20#include <agx/HashTable.h>
21
23#include <agxTerrain/export.h>
24
26
30
32
33namespace agxTerrain
34{
35
36 class TerrainCache;
37 class TerrainDataSource;
38
41
100 {
101 public:
102
105
109
115
117 };
118
121
149 TerrainPager(size_t tileResolution, size_t tileOverlap, agx::Real tileElementSize, agx::Real maximumDepth,
150 agx::Vec3 refPoint, agx::Quat refRotation,
151 agxTerrain::Terrain* templateTerrain );
152
153
160 //void setTerrainPlaneLocation( agx::Vec3 referencePoint, agx::Quat referenceRotation );
161
167
168
173
178
185 void setShouldStoreCompaction(bool shouldStoreCompaction, size_t maximumCompactionDepth);
186
191
200 void setShouldSynchronizeOverlapCompaction( bool shouldSynchronizeCompaction );
201
206
213 void setDeformable( bool deformable );
214
220 inline bool getDeformable() const;
221
226 void setEmbedCacheFilesInArchive( bool embed );
227
232 bool getEmbedCacheFilesInArhive() const;
233
238 void setShouldStoreDeltas(bool shouldStoreDeltas);
239
244
249
254
259
265 bool add( agx::RigidBody* body, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
266
272 bool add( agxTerrain::Shovel* shovel, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
273
279 bool add(agxTerrain::TerrainWheel* TerrainWheel, agx::Real requiredTileRadius, agx::Real preloadTileRadius);
280
281
287 bool remove( agx::RigidBody* body );
288
294 bool remove( agxTerrain::Shovel* shovel );
295
304
311
312
318 bool setTileLoadRadiuses( agx::RigidBody* body, agx::Real requiredTileRadius, agx::Real preloadTileRadius );
319
325
330
336
342
343
347 virtual void addNotification() override;
348
352 virtual void removeNotification() override;
353
354
355
359 virtual void preCollide( const agx::TimeStamp& time ) override;
360
361
367 virtual void last(const agx::TimeStamp& time) override;
368
369
378 void syncOverlap( const agxTerrain::TileId& tileIndex,
379 const agx::Vec2i& sourceIndex, bool shouldSynchronizeCompaction);
380
381
387
388
393
395 RAYCAST_HIT_ACTIVE_TERRAIN_TILE, // Hit active paged in tile
396 RAYCAST_RESULT_FROM_DATASOURCE // Hit via TerrainDataSource
397 };
398
413
414
415 using StepEventListener::store;
416
417 using StepEventListener::restore;
418
422 void store(/*agxStream::StorageStream& str*/agxTerrain::TileModificationVector& storeData);
423
427 void reset();
428
432 void restore(/*agxStream::StorageStream& str*/ const agxTerrain::TileModificationVector& restoreData);
433
434
440
446
456
463 void setBlockingCallbacks(
466
470
471 protected:
476
477 virtual ~TerrainPager();
478
479 void updateActiveTiles();
480
481 void unloadUnusedTiles( bool partOfRemoveNotification );
482
483 void handleNewTerrainTile( agxTerrain::TerrainTile );
484
485 void unloadAllTiles();
486
487 void prepareSimulation(bool fromRestore);
488
489 agx::Real getOverlappingAreaSolidMass(agxTerrain::TileId, agxTerrain::TileId);
490
491 agx::Real getSolidMassBeneathSurfaceCoord(const size_t& x, const size_t& y, agxTerrain::TerrainRef& terrainTile);
492
493 void synchronizeCompactionInColumn( agxTerrain::Terrain* sourceTile, agx::Vec2i sourceIndex,
494 agxTerrain::Terrain* targetTile, agx::Vec2i targetIndex);
495
496 void synchronizeTerrainMaterialInColumn( agxTerrain::Terrain* sourceTile, agx::Vec2i sourceIndex,
497 agxTerrain::Terrain* targetTile, agx::Vec2i targetIndex );
498
499 void synchronizeDataInColumn( agxTerrain::TileId sourceTileId, agxTerrain::TileId tileIdOffset, agx::Vec2i sourceIndex,
500 bool shouldSynchronizeCompaction);
501
502 bool isIndexWithinTileMargin(const agx::Vec2i& terrainIndex) const;
503
504
505 private:
507 friend class agx::InternalData;
511 agx::Referenced* getInternalData() const;
512
516 void setInternalData( agx::Referenced* data );
517
518 agx::ref_ptr<agx::Referenced> m_internalData;
520
521 private:
522 mutable TerrainCache m_cache;
523
524 TileSpecification m_tileSpec;
525
526 bool m_shouldStoreDeltas;
527
528 bool m_justRestored;
529
530 bool m_shouldSynchronizeCompaction;
531
532 struct BodyAttachments {
533 agx::RigidBodyObserver m_bodyObserver;
534 agx::Real m_requiredRadius;
535 agx::Real m_preloadRadius;
537 agxTerrain::TerrainWheelRefVector m_terrainWheels;
538 };
539
540 typedef std::pair<TileId, agx::Vec2i> GlobalTerrainIndex;
541
542 // Bodies this TerrainPager keeps track of
543 agx::HashTable < agx::RigidBody *, BodyAttachments > m_bodies;
544
545 // Tiles this TerrainPager keeps track of
547
548 // Stores heightfield changes since the last call to the store method
550 //agx::HashTable< TileId, TerrainIndexHeight > m_heightFieldDelta;
551
553
554 agx::Vector<agxTerrain::TileId> m_missingRequiredTiles;
555 agx::HashSet<agxTerrain::TileId> m_pendingRequests;
556
557
558
559 void fillTileAttachment( agxTerrain::TerrainTile tile, agxTerrain::TerrainPager::TileAttachments& ta );
560
561 void getCompactionFromNeighbourTiles(agxTerrain::TerrainTile tile);
562
563 void fillHeightDelta(const GlobalTerrainIndex& gtid, agx::Real height);
564
565 };
566
567
568 inline void TerrainPager::fillHeightDelta(const GlobalTerrainIndex& gtid, agx::Real height)
569 {
570 m_heightFieldDelta[gtid] = height;
571 }
572
573
575 {
576 m_cache.setEmbedCacheFilesInArchive( embed );
577 }
578
582 {
583 m_cache.setBlockingCallbacks(std::move(preBlock), std::move(postBlock));
584 }
585
586
588 {
589 return m_cache.getEmbedCacheFilesInArhive();
590 }
591
592 inline bool TerrainPager::getDeformable() const
593 {
594 return m_cache.getTemplateTerrain()->getDeformable();
595 }
596}
597
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#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
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: agx/Vector.h:34
Derive from this class to implement a listener for simulation step events.
Shovel object used to interact with a terrain via an active zone that converts solid terrain to dynam...
Definition: Shovel.h:63
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:58
std::function< void *()> PreBlockFunc
Definition: TerrainCache.h:60
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:437
agxTerrain::TerrainRef getTemplateTerrain()
Returns the template terrain used for properties of new terrain tiles.
Definition: TerrainCache.h:420
void setBlockingCallbacks(PreBlockFunc preBlock, PostBlockFunc postBlock)
Set callbacks invoked before and after the main thread blocks waiting for the background thread (wait...
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainCache.h:442
std::function< void(void *)> PostBlockFunc
Definition: TerrainCache.h:61
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:100
TileUnloadEvent tileUnloadEvent
An event that the TerrainPager will trigger just before it removes a tile from the simulation.
Definition: TerrainPager.h:445
agx::Physics::GranularBodySystem * getGranularBodySystem() const
Returns the internal GranularBodySystem used to simulate the soil particles.
void setBlockingCallbacks(TerrainCache::PreBlockFunc preBlock, TerrainCache::PostBlockFunc postBlock)
Set callbacks invoked before and after the main thread blocks waiting for the background tile-loading...
Definition: TerrainPager.h:579
TerrainDataSource * getTerrainDataSource()
Returns the data source the pager uses for fetching height data for tiles.
TileAttachmentConstPtrVector getActiveTileAttachments() const
Returns a vector of the terrains currently tracked by this pager.
bool add(agx::RigidBody *body, agx::Real requiredTileRadius, agx::Real preloadTileRadius)
Adds a RigidBody to the terrain pager.
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.
const TileSpecification & getTileSpecification() const
Specifies a new reference point for the terrain grid tiles.
agx::Callback2< agxTerrain::TileId, agxTerrain::Terrain * > TileCallback
Callback type.
Definition: TerrainPager.h:104
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.
agxTerrain::Terrain * getTemplateTerrain()
Fetches the template terrain.
virtual void removeNotification() override
Performs cleanup when removed from the simulation.
agx::Vector< const TerrainPager::TileAttachments * > TileAttachmentConstPtrVector
Definition: TerrainPager.h:120
agx::Event2< agxTerrain::TileId, agxTerrain::Terrain * > TileUnloadEvent
Definition: TerrainPager.h:108
void reset()
Resets paged terrain.
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.
void syncOverlap(const agxTerrain::TileId &tileIndex, const agx::Vec2i &sourceIndex, bool shouldSynchronizeCompaction)
Sync all terrain data from the modifiedVertices in the tile given by the tileIndex to any surrounding...
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:592
bool getShouldStoreCompaction() const
Returns whether the terrain pager should store compaction data for tiles that are paged out.
bool getShouldSynchronizeOverlapCompaction()
Return whether the terrain pager should synchronize compaction between tiles if shouldStoreCompaction...
bool getShouldStoreDeltas() const
Return whether the terrain pager should store changes to the terrain every time step.
agx::Vector< TerrainPager::TileAttachments * > TileAttachmentPtrVector
Definition: TerrainPager.h:119
void setShouldSynchronizeOverlapCompaction(bool shouldSynchronizeCompaction)
Specify whether the terrain pager should synchronize compaction changes in tile overlaps even if shou...
bool add(agxTerrain::TerrainWheel *TerrainWheel, agx::Real requiredTileRadius, agx::Real preloadTileRadius)
Adds a TerrainWheel and its RigidBody to the terrain pager.
TerrainRaycastResult
Enum with different types of result from the raycast method.
Definition: TerrainPager.h:392
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.
void syncOverlapCallback(agxTerrain::Terrain *terrain)
Callback from Terrain::preCollide, after shovels, avalanching, etc have modified the terrain for this...
bool getEmbedCacheFilesInArhive() const
Returns if cached files should be embedded in AGX Archives used for serialization.
Definition: TerrainPager.h:587
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:107
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:439
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.
SoilSimulationInterface * getSoilSimulationInterface()
Returns the soil simulation interface used by the terrains tracked by this pager or nullptr if there ...
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.
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:574
void setDeformable(bool deformable)
Sets if deformation should be enabled for terrains managed by this pager.
TerrainWheel is a rigid tire model that can deform soft terrain, modeled by agxTerrain::Terrain objec...
Definition: TerrainWheel.h:76
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
Definition: Terrain.h:92
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
An event with two arguments.
Definition: Event.h:122
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:670
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:49
Templated vector class.
Definition: agx/Vector.h:53
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:59
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:32
double Real
Definition: Real.h:41
std::pair< Real, Real > RealPair
agx::Real TimeStamp
Definition: TimeStamp.h:26
Struct with data to describe a terrain tile.
Definition: TerrainCache.h:42