AGXDynamics  2.31.1.0
agxTerrain/Terrain.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 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 #include <agxTerrain/export.h>
24 #include <agxTerrain/Grid.h>
37 
38 #include <agxSDK/TerrainInstance.h>
39 
40 #include <agx/HashSet.h>
41 #include <agx/HighAccuracyTimer.h>
42 #include <agx/BitState.h>
43 #include <agx/IndexLambdaKernel.h>
44 
45 #include <agxCollide/Plane.h>
46 #include <agxCollide/Geometry.h>
47 #include <agxCollide/HeightField.h>
48 
49 namespace agxTerrain
50 {
54  typedef std::pair<SoilParticlePtr, agx::Real> ParticleWeightPair;
61 
64 
77 
85  {
86  public:
87  enum class MaterialType
88  {
89  TERRAIN,
90  PARTICLE,
91  AGGREGATE
92  };
93 
94  enum class MassType
95  {
96  SOLID,
97  PARTICLE,
98  FLUID
99  };
100 
101  public:
105  static agx::Bool getEnableKinematicForParticles( const agx::RigidBody* rb );
106 
112  static void setEnableKinematicForParticles( agx::RigidBody* rb, agx::Bool enable );
113 
120  static Terrain* find( const agxSDK::Simulation* simulation, const agx::Name& name );
121 
128  static Terrain* find( const agxSDK::Simulation* simulation, const agx::Uuid& uuid );
129 
136  static TerrainPtrVector findAll( const agxSDK::Simulation* simulation, const agx::Name& name );
137 
143  static TerrainPtrVector findAll( const agxSDK::Simulation* simulation );
144 
145  public:
155  static Terrain* createFromHeightField(const agxCollide::HeightField* heightField, agx::Real maximumDepth);
156 
165  Terrain(size_t resolutionX, size_t resolutionY, agx::Real elementSize, agx::Real maximumDepth);
166 
177  Terrain(
178  size_t resolutionX, size_t resolutionY, agx::Real elementSize, const agx::RealVector& heights,
179  bool flipY, agx::Real maximumDepth);
180 
189  void setCompaction( agx::Real compaction, bool replaceCompactedSoil=true ) const;
190 
195  void setTransform(const agx::AffineMatrix4x4& transform);
196 
200  agx::AffineMatrix4x4 getTransform() const;
201 
205  agx::AffineMatrix4x4 getInverseTransform() const;
206 
212  void setEnable( bool enable );
213 
218  bool getEnable() const;
219 
224  void setPosition(const agx::Vec3& position);
225 
229  agx::Vec3 getPosition() const;
230 
235  void setRotation(const agx::Quat& rotation);
236 
240  agx::Quat getRotation() const;
241 
248  agx::Vec3 transformPointToTerrain(const agx::Vec3 point) const;
249 
256  agx::Vec3 transformPointToWorld(const agx::Vec3 point) const;
257 
264  agx::Vec3 transformVectorToTerrain(const agx::Vec3 vector) const;
265 
272  agx::Vec3 transformVectorToWorld(const agx::Vec3 vector) const;
273 
278  const agxCollide::HeightField* getHeightField() const;
279 
284  virtual agxCollide::Geometry* getGeometry() const override;
285 
292  const agxCollide::HeightField* getSoilParticleBoundedHeightField() const;
293 
297  void setNoMergeEdgeMargin(size_t noMergeEdgeMargin);
298 
302  void setNoMerge(bool noMerge);
303 
307  bool getNoMerge();
308 
312  bool addNoMergeZoneToGeometry( agxCollide::Geometry* geometry, agx::Real extensionDistance);
313 
317  bool removeNoMergeZoneToGeometry( agxCollide::Geometry* geometry);
318 
327  const ModifiedVerticesVector& getModifiedVoxelColumns() const;
328 
333  const ModifiedVerticesVector& getModifiedVertices() const;
334 
339  const ModifiedVerticesVector& getModifiedParticleVertices() const;
340 
344  void removeModifiedParticleVertice(agx::Vec2i terrainIndex);
345 
350  const ModifiedVerticesVector& getParticleVertices() const;
351 
355  agx::Vec2 getSize() const;
356 
360  size_t getResolutionX() const;
361 
365  size_t getResolutionY() const;
366 
372  agx::Real getElementSize() const;
373 
377  size_t getMemoryUsage();
378 
382  agx::Real getMinimumAllowedHeight() const;
383 
390  agx::Vec3 getSurfacePosition(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
391 
398  agx::Vec3 getSurfacePositionWorld(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
399 
406  agx::Vec2i getClosestGridPoint(const agx::Vec3& worldPosition, bool clampToBound=false) const;
407 
415  agx::Bool projectPointToSurface(const agx::Vec3& worldPosition, agx::Vec3& result) const;
416 
422  bool isBorderIndex(const agx::Vec2i& terrainIndex) const;
423 
429  bool isIndexWithinBounds(const agx::Vec2i& terrainIndex) const;
430 
436  bool isWorldPositionWithinBounds(const agx::Vec3& position) const;
437 
443  bool isLocalPositionWithinBounds(const agx::Vec3& position) const;
444 
450  void setHeight(const agx::Vec2i& terrainIndex, agx::Real height);
451 
458  agx::Real getHeight(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
459 
463  agx::Real calculateVolume();
464 
468  agx::Real calculateTotalSolidMass() const;
469 
473  agx::Real calculateTotalFluidMass() const;
474 
478  agx::Real calculateTotalDynamicMass() const;
479 
483  agx::Real calculateTotalMass() const;
484 
491  bool add(Shovel* shovel);
492 
498  bool remove(Shovel* shovel);
499 
503  const TerrainToolCollectionRefVector& getToolCollections() const;
504 
509  TerrainToolCollection* getToolCollection( const Shovel* shovel ) const;
510 
514  ShovelRefVector getShovels() const;
515 
524  bool getPenetrationForce(const Shovel* shovel, agx::Vec3& force, agx::Vec3& torque) const;
525 
533  agx::Vec3 getSeparationContactForce( const Shovel* shovel ) const;
534 
543  agx::Vec3 getDeformationContactForce(const Shovel* shovel) const;
544 
553  agx::Vec3 getExcavationModeContactForce(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
554 
564  agx::Vec3 getContactForce( const Shovel* shovel ) const;
565 
573 
580  agx::Real getLastDeadLoadFraction(const Shovel* shovel) const;
581 
585  agx::Real getInnerVolume(const Shovel* shovel) const;
586 
590  agx::Real getLastExcavatedVolume() const;
591 
597  bool isDigging(const Shovel* shovel) const;
598 
603  agx::Real getDynamicMass( const Shovel* shovel ) const;
604 
613  agx::Real getSoilAggregateMass(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
614 
620  bool contains(const Shovel* shovel) const;
621 
627  bool isRigidBodyShovel(const agx::RigidBody* body) const;
628 
634  bool isGeometryShovel(const agxCollide::Geometry* geometry) const;
635 
639  bool hasOverlap(const agxCollide::Geometry* geometry) const;
640 
652 
660  void setMaterial( agx::Material* material );
661 
672  agx::ContactMaterial* getContactMaterial( Terrain::MaterialType type1, Terrain::MaterialType type2 );
673 
679  TerrainMaterial* getTerrainMaterial() const;
680 
685  void setTerrainMaterial( TerrainMaterial* terrainMaterial );
686 
694  bool loadLibraryMaterial( const agx::String materialName );
695 
701  agx::StringVector getAvailableLibraryMaterials() const;
702 
708  bool loadMaterialFile( const agx::String& filename );
709 
720  bool setShovelAggregateContactMaterial( const Shovel* shovel,
721  agx::ContactMaterial* contactMaterial,
723 
733  agx::ContactMaterial* getShovelAggregateContactMaterial( const Shovel* shovel,
735 
744  bool setHeights( const agx::RealVector& heights, bool flipY = false, bool resetCompaction = true );
745 
750  void triggerForceAvalancheAll();
751 
759  agx::RealVector getResizedHeightField(size_t factorX, size_t factorY) const;
760 
767  SoilSimulationInterface* getSoilSimulationInterface() const;
768 
775  TerrainGridControl* getTerrainGridControl() const;
776 
780  agx::Real getTotalSoilParticleMass() const;
781 
785  SoilParticleStatistics getSoilParticleStatistics() const;
786 
790  const TerrainContactRefVector& getTerrainContacts() const;
791 
795  TerrainProperties* getProperties() const;
796 
806  bool getIntersectingGridElements( agxCollide::Geometry* geometry,
807  agx::Vec3iVector& gridPoints ) const;
808 
820  bool getIntersectingActiveGridElements( agxCollide::Geometry* geometry,
821  agx::Vec3iVector& gridPoints,
822  MassType massType ) const;
823 
833  float removeFluidMassInGeometry(agxCollide::Geometry* geometry, float fluidMass);
834 
841  float getParticleNominalRadius();
842 
846  size_t getNumSoilParticles() const;
847 
852  size_t getNumCreatedParticles() const;
853 
857  agx::Vec3 getUpDirection() const;
858 
862  agx::Real getVoxelArea() const;
863 
867  agx::Real getVoxelVolume() const;
868 
870 
872 
874  size_t getNumActiveVoxels();
875 
877  size_t getNumModifiedVoxels();
878 
886  bool verifyMassColumnIntegrity(const agx::Vec2i& terrainIndex);
887 
891  bool isVoxelIndexBelowMaximumDepth(const agx::Vec3i& index) const;
892 
899  agx::Line transformLineToTerrain(const agx::Line& line) const;
900 
907  agx::Line transformLineToWorld(const agx::Line& line) const;
908 
912  bool isVoxelIndexInsideGeometryBound(const agx::Vec3i& index) const;
913 
917  bool isBorderIndexAndBordersLocked(const agx::Vec2i& terrainIndex) const;
918 
919  CompactionController* getCompactionController() const;
920 
921  TerrainDataAtlas* getTerrainDataAtlas() const;
922 
923  virtual void runBuildIslandsTask() override;
924 
925  void clearAllSoilParticles();
926 
934  float addFluidMassToColumn(const agx::Vec2i& terrainIndex, float fluidMass) const;
935 
943  float removeFluidMassFromColumn(const agx::Vec2i& terrainIndex, float fluidMass);
944 
952  float addFluidMass(const agx::Vec3i& voxelIndex, float fluidMass) const;
953 
961  float removeFluidMass(const agx::Vec3i& voxelIndex, float fluidMass);
962 
969  agx::Real calculateFluidOccupancy() const;
970 
977  agx::Real getTotalSoilParticleOccupancy() const;
978 
984  agx::Vec3 getLocalPositionFromVoxelIndex(const agx::Vec3i& voxelIndex) const;
985 
991  agx::Vec3 getWorldPositionFromVoxelIndex(const agx::Vec3i& voxelIndex) const;
992 
998  agx::Vec3i getVoxelIndexFromLocalPosition(agx::Vec3 position) const;
999 
1005  agx::Vec3i getVoxelIndexFromWorldPosition(const agx::Vec3& position) const;
1006 
1012  agx::Vec3i getSurfaceVoxelIndexFromTerrainIndex(const agx::Vec2i& terrainIndex) const;
1013 
1019  agx::Vec3 getSurfacePositionFromVoxelXY(const agx::Vec2i& voxelXY) const;
1020 
1026  agx::Vec3 getSurfacePositionWorldFromVoxelXY(const agx::Vec2i& voxelXY) const;
1027 
1033  agx::Vec2i getTerrainIndexFromVoxelIndexXY(const agx::Vec2i& voxelXY) const;
1034 
1040  agx::Vec2i getVoxelIndexXYFromTerrainIndex(const agx::Vec2i& terrainIndex) const;
1041 
1047  agx::Vec3i getSurfaceVoxelIndexFromVoxelXY(const agx::Vec2i& voxelXY) const;
1048 
1054  bool voxelContainsShovel(const agx::Vec3i& voxelIndex) const;
1055 
1062  bool hasCreatedParticle( agx::Physics::ParticlePtr particle ) const;
1063 
1071  bool hasSameParticleMaterial(agx::Physics::ParticlePtr particle) const;
1072 
1078  TerrainMassController* getMassController() const;
1079 
1087  agx::Vec3 getAggregateTerrainContactForce(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
1088 
1096  agx::Vec3 getAggregateTerrainNormalForce(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
1097 
1106 
1107  void adjustFluidMassInGrid(agx::Vec3i binIndex, int binVoxels, float availableFluidMass, float remainingFluidMass);
1108 
1109  agx::Vec3iVector getVoxelsOccupiedByShovel(TerrainToolCollection *collection) const;
1110 
1111  bool voxelHasParticles(const agx::Vec3i& voxelIndex) const;
1112 
1113  bool isForbiddenIndex(const agx::Vec3i& index);
1114 
1115  void addTerrainIndexToAvalanche(const agx::Vec2i& index);
1116 
1117  int getLowestAllowableVoxelIndexZ() const;
1118 
1119  agx::Vec3 constructHeightFieldOffset() const;
1120 
1121  void setIsTerrainPagerTile(bool isPagerTile);
1122 
1123  bool getIsTerrainPagerTile() const;
1124 
1125  bool hasEnabledToolCollections() const;
1126 
1127  bool shouldUpdateTerrain() const;
1128 
1129  bool hasParticlesInTerrain() const;
1130 
1131  void debugRenderFluidMass();
1132 
1134 
1135  protected:
1139  Terrain();
1140 
1144  virtual void addNotification() override;
1145 
1149  virtual void removeNotification() override;
1150 
1154  virtual void preCollide() override;
1155 
1159  virtual void pre() override;
1160 
1164  virtual void post() override;
1165 
1169  virtual void last() override;
1170 
1171  protected:
1172  typedef struct offsetIndexInfo
1173  {
1179  } offsetIndexInfo;
1180 
1181  using GoToStopCondition = std::function<bool(const agx::Vec3i& index)>;
1183 
1185  bool isSurfaceVoxel(const agx::Vec3i& index) const;
1186  bool isSolidBoundaryVoxel(const agx::Vec3i& index) const;
1187  bool isSubmergedVoxel(const agx::Vec3i& index) const;
1188  bool isSolidVoxel(const agx::Vec3i& index) const;
1189  bool isActiveSolidVoxel(const agx::Vec3i& index) const;
1190  bool hasVoxelCompaction(const agx::Vec3i& index) const;
1191  bool isVoxelWithinTerrainIndexBounds(const agx::Vec3i& index) const;
1192  bool getSurfaceVoxel(const agx::Vec3i& voxelIndex, agx::Vec3i& result) const;
1193  bool findPathToSurfaceVoxel(const agx::Vec3i& startVoxel, std::deque<agx::Vec3i>& result);
1194  bool findSurfaceVoxel(const agx::Vec3i& index, agx::Vec3i& result) const;
1195  agx::Vec3i goToVoxelKernel(const agx::Vec3i& start, const GoToStopCondition& stopCondition) const;
1196  agx::Vec3i goToSurfaceVoxel(const agx::Vec3i& start) const;
1197  agx::Vec3i goToBoundaryVoxel(const agx::Vec3i& start) const;
1198  void updateSurfaceHeightFieldFromDirtyVoxels();
1199  void updateSurfaceHeightFieldFromVoxelIndices(const agx::Vec3iVector& updateIndices);
1200  void updateSurfaceHeightFieldFromVoxelIndices(const agxData::LocalVector<agx::Vec3i>& updateIndices);
1201  void updateSurfaceHeightFieldFromVoxelIndex(const agx::Vec3i& voxelIndex);
1202  void updateSurfaceHeightFieldFromHeightsTable( agx::HashTable<agx::Vec2i, std::pair<agx::Vec3i, agx::Real> >& table );
1203  bool traceGridSurfaceVoxel(const agx::Vec2i& voxelIndexXY, agx::Vec3i& surfaceVoxel) const;
1204  agx::Real traceGridSurfaceHeightFromVoxelIndex(const agx::Vec3i& index, bool globalSearch = false);
1205  agx::Real calculateSurfaceHeightFromVoxelIndex(const agx::Vec3i& voxelIndex);
1206  void initializeSurfaceHeightFieldFromVoxelGrid();
1207  void initVoxelStructureFromHeightField( bool adjustForCompaction = false );
1208  void convertVoxelsInActiveZonesToDynamicMass();
1209  bool shouldPrimaryActiveZoneRemoveMass(TerrainToolCollection* toolCollection);
1210  bool shouldDeformerActiveZoneRemoveMass(TerrainToolCollection* toolCollection, DeformerCollection* deformerCollection);
1211  void convertRemovedOccupancyToDynamicMass();
1212  void createSoilParticles();
1213  agx::Vec3 getValidParticlePosition(const agx::Real radius, const agx::Vec3i& index, const agx::Vec3& pos);
1214  void addDirtyVoxelsToAvalanche();
1215  void removeSoilParticlesOutsideTerrainBounds();
1216  void updateTransformationFromParentBody();
1217  void storeModifiedTerrainIndex( const agx::Vec2i& terrainVertex );
1218  void addModifiedParticleTerrainIndex(const agx::Vec2i& terrainIndex);
1219  void updateIndicesForAvalanche(TerrainIndexSet& forbiddenVertices);
1220  agx::Real findNewHeightFromVoxelIndex(const agx::Vec3i& index);
1221 
1222  SoilParticlePtrVector calculateParticlesInContactWithSurfaceGraph();
1223 
1224  bool _getIntersectingGridElements(
1225  Grid* grid,
1226  agxCollide::Geometry* geometry,
1227  agx::Vec3iVector& gridPoints,
1228  bool isOccupied = true) const;
1229 
1230  void sortParticlesIntoVoxels();
1231  void sortParticlesIntoColumns();
1232  agx::HashTable<agx::Vec3i, agx::UInt64Vector> sortParticlesIntoBins(int binVoxels);
1233 
1234  void preSortSynchronizeParticleHeightField();
1235  void postSortSynchronizeParticleHeightField();
1236 
1237  // Sampling method
1238  void sampleParticleMassVelocity();
1239 
1240  void preUpdateTerrainContacts();
1241  void postUpdateCompaction();
1242  bool isDirty();
1243  void markVoxelToAvalanche(const agx::Vec3i& changedVoxel);
1244  void postSolveShovels();
1245  void updateOccupancyFromHeightChange(const agx::Vec2i& heightFieldIndex);
1246  void avalanche();
1247  void computeForbiddenBounds();
1248  void initializeFromHeightField();
1249 
1250  // Resize soil particles
1251  void resizeSoilParticles();
1252  void computeParticleGrowthTables(const agx::UInt64Vector& particleIndices, ParticleDeltaVector& particles);
1253  void growParticlesFromFluidMass(agx::Vec3i binIndex, int binVoxels, float availableFluidMass, ParticleDeltaVector& particles);
1254  void growSmallParticlesFromParticleMass(ParticleDeltaVector& particles, float particleRadiusUpperLimit);
1255  void growLargeParticlesFromParticleMass(ParticleDeltaVector& particles, float particleRadiusUpperLimit);
1256 
1257  // Merge soil particles functions
1258  void mergeSoilParticlesAndFluidMass();
1259  void addNeighbours(agxData::LocalVector<agx::Vec3i>& indexQueue, agx::Vec3i index);
1260  bool mergeFluidMass();
1261  void createParticleIslands( ParticleIslandVector& particleIslands );
1262  void mergeParticleIslands( const ParticleIslandVector& particleIslands, agxData::LocalVector<agx::Vec3i>& mergedParticleIndices );
1263  void resizeOrRemoveParticles( const agx::HashTable<SoilParticlePtr, std::array<agx::Real, 2>>& particlesToUpdate );
1264  agx::Real getMergeSpeedSquared() const;
1265 
1266  // Merge to cutting edge functions
1267  void mergeSoilToCuttingEdge();
1268  agx::Vec3iVector mergeCuttingEdgeColumn(agx::Vec3i voxel, size_t numVoxelsToSurfaceThreshold, agx::Plane cuttingPlane);
1269  float convertParticlesToSolidMass(agx::Vec3i voxel, ParticleWeightPairVector particles, float massToRemove, agx::Vec3iVector& changedIndices);
1270  void moveParticlesToSurface(agx::Vec3iVector changedVoxelIndices);
1271 
1272  // Step fluid mass
1273  void stepFluidMass();
1274  void calculateFluidMassTransport(agx::HashTable<agx::Vec3i, float>& fluidMassTable);
1275  void applyFluidMassChanges(agx::HashTable<agx::Vec3i, float>& fluidMassTable);
1276  void enforceIncompressibilityColumnwise();
1277  agx::Vec3f getVoxelVelocity(const agx::Vec3i& voxelIndex, agx::HashTable<agx::Vec3i, size_t>& shovelVoxels);
1278  void createFluidMassInVoxel(const agx::Vec3i& voxelIndex, agx::Real32 amount);
1279  void createFluidMassOnTerrainSurface(const agx::Vec2i& terrainIndex, int surfaceVoxelOffset, agx::Real32 amount);
1280 
1281  // Conversion functions for particle radius and general occupancy
1282  float convertParticleRadiusToOccupancy(float radius) const;
1283  float convertOccupancyToParticleRadius(float occupancy) const;
1284  float getDynamicMassCompaction() const;
1285  bool soilParticleShouldMerge(SoilParticlePtr& ptr) const;
1286  bool particleIslandShouldMerge(const agx::Vector<std::pair<agx::Vec3i, offsetIndexInfo>>& island);
1287  bool voxelColumnHasParticles(const agx::Vec2i& vertex) const;
1288 
1289  void calculateShovelVoxelIntersection();
1290 
1291  agx::HashVector<agx::Vec2i, agx::Int> getShovelHeightPerColumn();
1292 
1293  agx::Real convertOccupancyToMass(agx::Real occupnacy) const;
1294  agx::Real convertMassToOccupancy(agx::Real mass) const;
1295 
1296  agx::Real generateRandomNumber();
1297  agx::Vec3 generateRandomSoilParticlePositionPerturbation(agx::Real maxDelta);
1298  agx::Quat generateRandomSoilParticleRotation();
1299 
1300  void debugRenderForbiddenBounds();
1301  void debugRenderShovelForbiddenBounds();
1302 
1303  AvalancheController* getAvalancheController() const;
1304 
1305  agx::Bool isIndexWithinBoundsIssueWarning( const agx::Vec2i& terrainIndex ) const;
1306 
1307  void renderVoxelAABB(agx::Vec3i index, agx::Vec4f color);
1308 
1309  void toolCollectionsPreCollide();
1310  agx::TaskGroupRef createTask_preCollide();
1311  void initBuildIslandTask();
1312 
1313  const agx::AffineMatrix4x4& _getTransform() const;
1314  const agx::AffineMatrix4x4& _getInverseTransform() const;
1315 
1316  void moveEntityDataToCurrentThread();
1317 
1318  friend class AvalancheController;
1320  friend class TerrainMaterial;
1322  friend class TerrainGridControl;
1324  friend class CompactionController;
1325  friend class TerrainPhysics;
1326  friend class TerrainCache;
1327 
1328  protected:
1329  virtual ~Terrain();
1330 
1331  Terrain(agxCollide::HeightField* heightField, agx::Real maximumDepth);
1332 
1333  private:
1336 
1337  private:
1338  agxCollide::HeightFieldRef m_surfaceHeightField;
1339  agxCollide::HeightFieldRef m_particleHeightField;
1340  agx::Real m_voxelSize;
1341  TerrainPropertiesRef m_properties;
1342  agx::UniformRealGenerator m_generator;
1343  float m_nominalSoilParticleRadius;
1344 
1345  agxCollide::GeometryRef m_surfaceGeometry;
1346  agx::AffineMatrix4x4 m_transform;
1347  agx::AffineMatrix4x4 m_invTransform;
1348  agx::Vec3 m_heightFieldOffset;
1349  agx::Vec2i m_voxelToHeightFieldIndexSpaceOffset;
1350 
1351  TerrainDataAtlasRef m_terrainDataAtlas;
1352  TerrainMassControllerRef m_massController;
1353  CompactionControllerRef m_compactionController;
1354  TerrainContactGeneratorRef m_terrainContactGenerator;
1355  AvalancheControllerRef m_avalancheController;
1356  SoilSimulationInterfaceRef m_soilSimulationInterface;
1357  TerrainGridControlRef m_terrainGridInterface;
1358 
1359  TerrainMaterialRef m_terrainMaterial;
1360  agx::MaterialRef m_material;
1361  agx::MaterialRef m_particleMaterial;
1362  agx::MaterialRef m_aggregateMaterial;
1363 
1364  agx::ContactMaterialRef m_terrainParticleContactMaterial;
1365  agx::ContactMaterialRef m_terrainAggregateContactMaterial;
1366  agx::ContactMaterialRef m_particleParticleContactMaterial;
1367 
1368  TerrainToolCollectionRefVector m_toolCollections;
1369  AvalancheController::TerrainIndexSet m_terrainIndicesToAvalanche;
1370 
1371  agx::HashTable<agxCollide::Geometry*, agx::Real> m_noMergeGeometryToDistance;
1372  size_t m_noMergeEdgeMargin;
1373  bool m_noMerge;
1374  BoundingAABBVector m_forbiddenBounds;
1375  agx::HashTable<agx::Vec3i, size_t> m_voxelIndexToShovelIndex;
1376  agx::HashTable<TerrainToolCollection*, agx::Vec3iVector> m_voxelsInPrimaryActiveZoneLastTimeStep;
1377  agx::Vec3iVector m_dirtyVoxelIndices;
1378  agx::Real32Vector m_removedOccupancy;
1379  agx::Real m_lastRemovedVolume;
1380  VoxelIndexSet m_voxelHasCreatedParticle;
1381  TerrainIndexSet m_mergedVoxelXYVertices;
1382  ModifiedVerticesSetVector m_modifiedVoxelColumns;
1383  ModifiedVerticesSetVector m_modifiedTerrainVertices;
1384  ModifiedVerticesSetVector m_modifiedParticleVertices;
1385  ModifiedVerticesSetVector m_particleTerrainVertices;
1386 
1387  size_t m_numCreatedParticles;
1388  float m_averageSoilParticleRadius;
1389 
1390  // Sort particles into voxels
1391  VoxelIndexOffsetIndexInfoTable m_voxelParticleOffsetTable;
1392  ParticleWeightPairVector m_voxelParticleWeightPairVector;
1393 
1394  // Sort particles into columns
1395  VoxelIndexOffsetIndexInfoTable m_columnParticleOffsetTable;
1396  ParticleWeightPairVector m_columnParticleWeightPairVector;
1397 
1398  agx::HashTable<agx::Vec2i, agx::Real> m_columnParticleHeight;
1399  agx::HighAccuracyTimer m_timer;
1400 
1401  agx::IndexLambdaKernelRef m_indexKernel;
1402 
1403  bool m_isTerrainPagerTile;
1404  bool m_enable;
1405 
1406  // !HACK! !BEWARE! Islands partitioning
1407  // The build islands task is not executed when using PPGS.
1408  // No island partitioning means no parallelization of solving the rigid body system.
1409  // However, we still need islands in Terrain to determine which particles are in contact with the ground.
1410  // Thus, we manually load and execute the task in Terrain.
1411  // The task is loaded and bound for each Terrain that is added but is only executed once by agxSDK::Simulation.
1412  // The task is executed using the runBuildIslandsTask() method.
1413  agx::TaskRef m_buildIslandsTask;
1414  agx::TaskGroupRef m_preCollideUpdateTask;
1415  agx::TaskRef m_sortParticlesToVoxelsTask;
1416  agx::TaskRef m_sortParticlesToColumnsTask;
1417  agx::TaskRef m_calculateShovelVoxelIntersection;
1418  agx::TaskRef m_convertSolidToDynamicMassTask;
1419 
1420  agx::ParallelTaskRef m_updateHeightfieldsTask;
1421  agx::Job m_updateHeightfieldsJobs[2];
1422  agx::ParallelTaskRef m_voxelCollisionTask;
1423 
1424  bool m_enableFluidMerge; // Only used for testing. Should not be serialized.
1425  };
1426 
1427 
1428  AGX_FORCE_INLINE agx::Vec3i Terrain::getVoxelIndexFromLocalPosition(agx::Vec3 position) const
1429  {
1430  position *= (1.0 / m_voxelSize);
1431  // Note: ( position + 0.5 * m_voxelSize ) / m_voxelSize
1432  return agx::Vec3i((agx::Int)std::floor(position.x() + 0.5),
1433  (agx::Int)std::floor(position.y() + 0.5),
1434  (agx::Int)std::floor(position.z() + 0.5));
1435  }
1436 
1437 
1438  inline TerrainDataAtlas* Terrain::getTerrainDataAtlas() const
1439  {
1440  return m_terrainDataAtlas;
1441  }
1442 
1443 
1445  {
1446  return m_terrainMaterial;
1447  }
1448 
1449 
1450  inline size_t Terrain::getResolutionX() const
1451  {
1452  return m_surfaceHeightField->getResolutionX();
1453  }
1454 
1455 
1456  inline size_t Terrain::getResolutionY() const
1457  {
1458  return m_surfaceHeightField->getResolutionY();
1459  }
1460 
1461 
1463  {
1464  return m_voxelSize;
1465  }
1466 
1467 
1468  inline agx::Vec2i Terrain::getTerrainIndexFromVoxelIndexXY(const agx::Vec2i& voxelXY) const
1469  {
1470  return voxelXY + m_voxelToHeightFieldIndexSpaceOffset;
1471  }
1472 
1473 
1474  inline agx::Vec2i Terrain::getVoxelIndexXYFromTerrainIndex(const agx::Vec2i& terrainIndex) const
1475  {
1476  return terrainIndex - m_voxelToHeightFieldIndexSpaceOffset;
1477  }
1478 
1479 
1481  {
1482  return m_transform;
1483  }
1484 
1485 
1486 
1488  {
1489  return m_invTransform;
1490  }
1491 
1492 
1493  inline agx::Vec3i Terrain::getSurfaceVoxelIndexFromTerrainIndex(const agx::Vec2i& terrainIndex) const
1494  {
1495  return getVoxelIndexFromLocalPosition( getSurfacePosition( terrainIndex ) );
1496  }
1497 
1498 
1499  inline agx::Vec3 Terrain::getSurfacePositionFromVoxelXY( const agx::Vec2i& voxelXY ) const
1500  {
1501  return getSurfacePosition( getTerrainIndexFromVoxelIndexXY( voxelXY ) );
1502  }
1503 
1504 
1505  inline agx::Vec3 Terrain::getSurfacePositionWorldFromVoxelXY( const agx::Vec2i& voxelXY ) const
1506  {
1507  return getSurfacePositionWorld( getTerrainIndexFromVoxelIndexXY( voxelXY ) );
1508  }
1509 
1510 
1511  inline bool Terrain::getIsTerrainPagerTile() const
1512  {
1513  return m_isTerrainPagerTile;
1514  }
1515 
1516 
1517 
1518 }
agxCollide::GeometryContactPtrVector AGXTERRAIN_EXPORT getShovelAggregateContacts(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Internal method Get the shovel <-> aggregate contacts with the terrain given an excavation mode and a...
agxTerrain::Terrain instance in a simulation, receiving step and contact callbacks in a controllable ...
This class store the combine material properties between two agx::Material&#39;s.
Definition: Material.h:503
float Real32
Definition: Real.h:44
agx::Vec3 AGXTERRAIN_EXPORT getSeparationContactForce(const Terrain *terrain, const Shovel *shovel)
Given geometry contacts exists and the solver has solved them - calculates total contact force betwee...
Interface class for accessing and manipulating the underlying 3D grid data in the terrain using terra...
agxCollide::GeometryContactPtrVector AGXTERRAIN_EXPORT getAggregateTerrainContacts(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Internal method Get the aggregate normal and shear contact force with the terrain given an excavation...
agx::Vec3 AGXTERRAIN_EXPORT getExcavationModeContactForce(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Calculates total contact force between the soil aggregate and the shovel associated with the specifie...
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
bool Bool
Definition: Integer.h:48
Pointer to a entity instance of type Physics.GranularBody.
An abstract job/workblock representation, which allows work threads to execute arbitrary tasks...
Definition: Job.h:59
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
Class for containing and managing the different agxTerrain::Terrain settings.
Copyright 2007 - 2018.
Definition: Geometry.h:56
agx::Physics::GranularBodyPtrVector SoilParticlePtrVector
The HighAccurayTimer class can replace the regular Timer class in high level applications where the n...
agx::Vector< agx::Vec2i > ModifiedVerticesVector
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
Collection of terrain tool (currently Shovel) related objects which are terrain instance specific...
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
ExcavationMode
Enum for describing the different excavation modes of a shovel: PRIMARY - The primary mode of excavat...
Definition: Shovel.h:86
Representation of a name string.
Definition: Name.h:32
#define AGX_FORCE_INLINE
Definition: Object.h:63
Class representing the mathematical concept of a plane, also called a half- space.
Definition: Plane.h:31
Pure virtual interface class for the data storage grid classes we have.
Definition: Grid.h:55
std::function< bool(const agx::Vec3i &index)> GoToStopCondition
agx::Real getElementSize() const
Returns the element size of a cell in agxTerrain, which is incidentally the distance between the cent...
This class is used to describe the general material properties of the Terrain.
agx::Physics::GranularBodyPtr SoilParticlePtr
agx::Vec3 AGXTERRAIN_EXPORT getAggregateTerrainNormalForce(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Internal method Get the aggregate normal force with the terrain given an excavation mode and a shovel...
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
Class that handles and sanity checks the mass logic in the terrain.
agx::Vec3 AGXTERRAIN_EXPORT getAggregateTerrainContactForce(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Internal method Get the aggregate contact force with the terrain given an excavation mode and a shove...
uint32_t UInt
Definition: Integer.h:34
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
std::pair< SoilParticlePtr, agx::Real > ParticleWeightPair
The TerrainCache is used internally by the TerrainPager to provide requested terrain tiles...
Definition: TerrainCache.h:53
agx::Vector< ParticleWeightPair > ParticleWeightPairVector
Controller that handles compaction in the terrain from generated terrain contacts in TerrainContactGe...
Utility class where most of the physics calculation used in agxTerrain is gathered.
Vec3T< Int > Vec3i
Definition: Vec3.h:43
agx::Vector< std::pair< SoilParticlePtr, float > > ParticleDeltaVector
agx::Vec3 AGXTERRAIN_EXPORT getDeformationContactForce(const Terrain *terrain, const Shovel *shovel)
Given geometry contacts exists and the solver has solved them - calculates total contact force betwee...
size_t getResolutionY() const
Returns the element resolution of the terrain in the local Y-axis.
const agx::AffineMatrix4x4 & _getInverseTransform() const
#define AGXTERRAIN_EXPORT
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:52
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:384
TerrainMaterial * getTerrainMaterial() const
Returns the terrain material which is used to derive material and contact material parameters...
A UUID, or Universally unique identifier, is intended to uniquely identify information in a distribut...
Definition: Uuid.h:41
Interface class for accessing data and functions for the internal soil particle simulation used in ag...
#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:206
agx::HashSet< agx::Vec3i > VoxelIndexSet
agx::Vec3 AGXTERRAIN_EXPORT getContactForce(const Terrain *terrain, const Shovel *shovel)
Given geometry contacts exists and the solver has solved them - calculates total shovel contact force...
static agx::Vec3 transformVectorToWorld(const agx::Vec3 &v, const agxCollide::Geometry *g)
Transforms a vector given in shape coordinates to world coordinates given geometry g...
Definition: WireUtils.h:57
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:121
Local scope vector.
Definition: LocalVector.h:43
AGX_DECLARE_VECTOR_TYPES(ActiveZone)
const agx::AffineMatrix4x4 & _getTransform() const
size_t getResolutionX() const
Returns the element resolution of the terrain in the local X-axis.
double Real
Definition: Real.h:42
agx::HashTable< agx::Vec3i, ParticleWeightPairVector > IndexToParticleWeightPairVector
static agx::Vec3 transformPointToWorld(const agx::Vec3 &p, const agxCollide::Geometry *g)
Transforms a point given in shape coordinates to world given geometry g.
Definition: WireUtils.h:81
Inheritance with partial specialization due to bug with ref_ptr containers.
agx::SetVector< agx::Vec2i > ModifiedVerticesSetVector
Pointer to a entity instance of type Physics.Particle.
A HeightField is a collision shape that can be created from a grid structure, like an image...
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:102
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:101
GLenum type
Definition: gl.h:5539