AGX Dynamics  2.37.3.2
agxTerrain/Terrain.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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>
39 
40 #include <agxSDK/TerrainInstance.h>
41 
42 #include <agx/HashSet.h>
43 #include <agx/HighAccuracyTimer.h>
44 #include <agx/BitState.h>
45 #include <agx/IndexLambdaKernel.h>
46 
47 #include <agxCollide/Plane.h>
48 #include <agxCollide/Geometry.h>
49 #include <agxCollide/HeightField.h>
50 
51 namespace agxTerrain
52 {
56  typedef std::pair<SoilParticlePtr, agx::Real> ParticleWeightPair;
63 
66 
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:
157  static Terrain* createFromHeightField(const agxCollide::HeightField* heightField, agx::Real maximumDepth);
158 
167  Terrain(size_t resolutionX, size_t resolutionY, agx::Real elementSize, agx::Real maximumDepth);
168 
179  Terrain(
180  size_t resolutionX, size_t resolutionY, agx::Real elementSize, const agx::RealVector& heights,
181  bool flipY, agx::Real maximumDepth);
182 
191  void setCompaction( agx::Real compaction, bool replaceCompactedSoil=true ) const;
192 
197  void setTransform(const agx::AffineMatrix4x4& transform);
198 
202  agx::AffineMatrix4x4 getTransform() const;
203 
207  agx::AffineMatrix4x4 getInverseTransform() const;
208 
214  void setEnable( bool enable );
215 
220  bool getEnable() const;
221 
226  void setPosition(const agx::Vec3& position);
227 
231  agx::Vec3 getPosition() const;
232 
237  void setRotation(const agx::Quat& rotation);
238 
242  agx::Quat getRotation() const;
243 
250  agx::Vec3 transformPointToTerrain(const agx::Vec3 point) const;
251 
258  agx::Vec3 transformPointToVoxelSpace( const agx::Vec3 point ) const;
259 
266  agx::Vec3 transformPointToWorld(const agx::Vec3 point) const;
267 
274  agx::Vec3 transformVectorToTerrain(const agx::Vec3 vector) const;
275 
282  agx::Vec3 transformVectorToWorld(const agx::Vec3 vector) const;
283 
288  const agxCollide::HeightField* getHeightField() const;
289 
294  virtual agxCollide::Geometry* getGeometry() const override;
295 
302  const agxCollide::HeightField* getSoilParticleBoundedHeightField() const;
303 
309  void setDeformable( bool deformable );
310 
316  inline bool getDeformable() const;
317 
321  void setNoMergeEdgeMargin(size_t noMergeEdgeMargin);
322 
326  void setNoMerge(bool noMerge);
327 
331  bool getNoMerge();
332 
336  bool addNoMergeZoneToGeometry( agxCollide::Geometry* geometry, agx::Real extensionDistance);
337 
341  bool removeNoMergeZoneToGeometry( agxCollide::Geometry* geometry);
342 
347  const ModifiedVerticesVector& getModifiedVertices() const;
348 
353  const ModifiedVerticesVector& getModifiedParticleVertices() const;
354 
358  void removeModifiedParticleVertice(agx::Vec2i terrainIndex);
359 
364  const ModifiedVerticesVector& getParticleVertices() const;
365 
369  agx::Vec2 getSize() const;
370 
374  size_t getResolutionX() const;
375 
379  size_t getResolutionY() const;
380 
386  agx::Real getElementSize() const;
387 
391  size_t getMemoryUsage();
392 
396  agx::Real getMinimumAllowedHeight() const;
397 
404  agx::Vec3 getSurfacePosition(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
405 
412  agx::Vec3 getSurfacePositionWorld(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
413 
420  agx::Vec2i getClosestGridPoint(const agx::Vec3& worldPosition, bool clampToBound=false) const;
421 
429  agx::Bool projectPointToSurface(const agx::Vec3& worldPosition, agx::Vec3& result) const;
430 
436  bool isBorderIndex(const agx::Vec2i& terrainIndex) const;
437 
443  bool isIndexWithinBounds(const agx::Vec2i& terrainIndex) const;
444 
450  bool isWorldPositionWithinBounds(const agx::Vec3& position) const;
451 
457  bool isLocalPositionWithinBounds(const agx::Vec3& position) const;
458 
464  void setHeight(const agx::Vec2i& terrainIndex, agx::Real height);
465 
472  agx::Real getHeight(const agx::Vec2i& terrainIndex, bool includeSurfaceParticles = false) const;
473 
477  agx::Real calculateVolume();
478 
482  agx::Real calculateTotalSolidMass() const;
483 
487  agx::Real calculateTotalFluidMass() const;
488 
492  agx::Real calculateTotalDynamicMass() const;
493 
497  agx::Real calculateTotalMass() const;
498 
505  bool add(Shovel* shovel);
506 
512  bool remove(Shovel* shovel);
513 
517  const TerrainToolCollectionRefVector& getToolCollections() const;
518 
523  TerrainToolCollection* getToolCollection( const Shovel* shovel ) const;
524 
528  ShovelRefVector getShovels() const;
529 
538  bool getPenetrationForce(const Shovel* shovel, agx::Vec3& force, agx::Vec3& torque) const;
539 
547  agx::Vec3 getSeparationContactForce( const Shovel* shovel ) const;
548 
555  agx::Vec3 getSeparationNormalForce( const Shovel* shovel ) const;
556 
563  agx::Vec3 getSeparationFrictionForce( const Shovel* shovel ) const;
564 
573  agx::Vec3 getDeformationContactForce(const Shovel* shovel) const;
574 
583  agx::Vec3 getExcavationModeContactForce(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
584 
594  agx::Vec3 getContactForce( const Shovel* shovel ) const;
595 
603 
610  agx::Real getLastDeadLoadFraction(const Shovel* shovel) const;
611 
615  agx::Real getInnerVolume(const Shovel* shovel) const;
616 
620  agx::Real getLastExcavatedVolume() const;
621 
627  bool isDigging(const Shovel* shovel) const;
628 
633  agx::Real getDynamicMass( const Shovel* shovel ) const;
634 
643  agx::Real getSoilAggregateMass(const Shovel* shovel, Shovel::ExcavationMode excavationMode) const;
644 
650  bool contains(const Shovel* shovel) const;
651 
657  bool isRigidBodyShovel(const agx::RigidBody* body) const;
658 
664  bool isGeometryShovel(const agxCollide::Geometry* geometry) const;
665 
669  bool hasOverlap(const agxCollide::Geometry* geometry) const;
670 
682 
683 
697  void setMaterial(agx::Material* material, Terrain::MaterialType type = Terrain::MaterialType::TERRAIN);
698 
699 
700  /*
701  Returns the associated material with the given terrainMaterial. Use this material when configuring contact
702  materials between the given terrainMaterial and other materials, for example the shovelMaterial.
703  \returns the associated material with the given terrainMaterial.
704  */
705  agx::Material* getAssociatedMaterial(TerrainMaterial* terrainMaterial) const;
706 
707 
716  bool setAssociatedMaterial(TerrainMaterial* terrainMaterial, agx::Material* material);
717 
718 
725  bool exchangeTerrainMaterial(TerrainMaterial* oldTerrainMaterial, TerrainMaterial* newTerrainMaterial);
726 
727  /*
728  Remove an added terrain material, its assigned domain will also be removed along with it.
729  \param terrainMaterialToRemove - the terrain material to remove. Note that this can not be the default terrain material, instead try to swithc it with either setTerrainMaterial or exchangeTerrainMaterial!
730  */
731  bool removeTerrainMaterial(TerrainMaterial* terrainMaterialToRemove);
732 
733  /*
734  Remove all traces of added terrain materials. Clears the underlying grid with voxel assignments as well as the list with added terrain materials.
735  \param clearAddedMaterials - true to also clear the list of added materials, false to only clear the assigned domains. Default: true.
736  */
737  void clearTerrainMaterials(bool clearAddedMaterials = true);
738 
739 
750  agx::ContactMaterial* getContactMaterial( Terrain::MaterialType type1, Terrain::MaterialType type2 );
751 
757  TerrainMaterial* getDefaultTerrainMaterial() const;
758 
763  TerrainMaterial* getTerrainMaterial() const;
764 
765 
772  TerrainMaterial* getTerrainMaterial(agx::Vec3 position) const;
773 
780  void setTerrainMaterial( TerrainMaterial* terrainMaterial, agx::Material* material = nullptr);
781 
787  bool addTerrainMaterial(TerrainMaterial* terrainMaterial);
788 
789 
797  int addTerrainMaterial(TerrainMaterial* terrainMaterial, agxCollide::Geometry* geometry);
798 
806  bool loadLibraryMaterial( const agx::String& materialName );
807 
815  static TerrainMaterial* getLibraryMaterial(const agx::String& materialName);
816 
817 
823  static agx::StringVector getAvailableLibraryMaterials();
824 
830  bool loadMaterialFile( const agx::String& filename );
831 
837  static TerrainMaterial* getMaterialFromFile(const agx::String& filename);
838 
849  bool setShovelAggregateContactMaterial( const Shovel* shovel,
850  agx::ContactMaterial* contactMaterial,
852 
862  agx::ContactMaterial* getShovelAggregateContactMaterial( const Shovel* shovel,
864 
873  bool setHeights( const agx::RealVector& heights, bool flipY = false, bool resetCompaction = true );
874 
879  void convertToDynamicMassInShape(agxCollide::Shape* shape);
880 
881 
886  void triggerForceAvalancheAll();
887 
895  agx::RealVector getResizedHeightField(size_t factorX, size_t factorY) const;
896 
903  SoilSimulationInterface* getSoilSimulationInterface() const;
904 
911  TerrainGridControl* getTerrainGridControl() const;
912 
916  agx::Real getTotalSoilParticleMass() const;
917 
921  SoilParticleStatistics getSoilParticleStatistics() const;
922 
926  const TerrainContactRefVector& getTerrainContacts() const;
927 
931  TerrainProperties* getProperties() const;
932 
938  agx::RealVector getMaterialWeightsInLine( const agx::Line& line ) const;
939 
944  agx::RealVector getMaterialWeightsInGridPoints( const agx::Vec3iVector& gridPoints ) const;
945 
955  bool getIntersectingGridElements( agxCollide::Geometry* geometry,
956  agx::Vec3iVector& gridPoints ) const;
957 
969  bool getIntersectingActiveGridElements( agxCollide::Geometry* geometry,
970  agx::Vec3iVector& gridPoints,
971  MassType massType ) const;
972 
982  float removeFluidMassInGeometry(agxCollide::Geometry* geometry, float fluidMass);
983 
990  float getParticleNominalRadius() const;
991 
995  size_t getNumSoilParticles() const;
996 
1001  size_t getNumCreatedParticles() const;
1002 
1006  agx::Vec3 getUpDirection() const;
1007 
1011  agx::Real getVoxelArea() const;
1012 
1016  agx::Real getVoxelVolume() const;
1017 
1021  static agx::Real getTerrainParticlePackingRatio();
1022 
1023  /*
1024  \return Whether the terrain consists of one homogeneous terrain material or not.
1025  */
1026  bool isOfHomogeneousTerrainMaterial() const;
1027 
1034  agx::Vec3 getAggregateTerrainContactForce( const Shovel* shovel, Shovel::ExcavationMode excavationMode ) const;
1035 
1042  agx::Vec3 getAggregateTerrainNormalForce( const Shovel* shovel, Shovel::ExcavationMode excavationMode ) const;
1043 
1050  agx::Vec3 getAggregateTerrainTangentialForce( const Shovel* shovel, Shovel::ExcavationMode excavationMode ) const;
1051 
1058  agx::Real getAggregateTerrainContactArea( const Shovel* shovel, Shovel::ExcavationMode excavationMode ) const;
1059 
1067 
1069 
1071 
1073  size_t getNumActiveVoxels() const;
1074 
1076  size_t getNumModifiedVoxels() const;
1077 
1085  bool verifyMassColumnIntegrity(const agx::Vec2i& terrainIndex) const;
1086 
1090  bool isVoxelIndexBelowMaximumDepth(const agx::Vec3i& index) const;
1091 
1098  agx::Line transformLineToTerrain(const agx::Line& line) const;
1099 
1106  agx::Line transformLineToVoxelSpace( const agx::Line& line ) const;
1107 
1114  agx::Line transformLineToWorld(const agx::Line& line) const;
1115 
1119  bool isVoxelIndexInsideGeometryBound(const agx::Vec3i& index) const;
1120 
1124  agx::AffineMatrix4x4 getVoxelSpaceTransform() const;
1125 
1129  bool isBorderIndexAndBordersLocked(const agx::Vec2i& terrainIndex) const;
1130 
1131  CompactionController* getCompactionController() const;
1132 
1133  TerrainDataAtlas* getTerrainDataAtlas() const;
1134 
1135 
1136  virtual void runBuildIslandsTask() override;
1137 
1138  void clearAllSoilParticles();
1139 
1147  float addFluidMassToColumn(const agx::Vec2i& terrainIndex, float fluidMass) const;
1148 
1156  float removeFluidMassFromColumn(const agx::Vec2i& terrainIndex, float fluidMass);
1157 
1165  float addFluidMass(const agx::Vec3i& voxelIndex, float fluidMass) const;
1166 
1174  float removeFluidMass(const agx::Vec3i& voxelIndex, float fluidMass);
1175 
1182  agx::Real calculateFluidOccupancy() const;
1183 
1190  agx::Real getTotalSoilParticleOccupancy() const;
1191 
1197  agx::Vec3 getLocalPositionFromVoxelIndex(const agx::Vec3i& voxelIndex) const;
1198 
1204  agx::Vec3 getWorldPositionFromVoxelIndex(const agx::Vec3i& voxelIndex) const;
1205 
1211  agx::Vec3i getVoxelIndexFromLocalPosition(const agx::Vec3& position) const;
1212 
1218  agx::Vec3i getVoxelIndexFromGridPosition( agx::Vec3 position ) const;
1219 
1225  agx::Vec2i getTerrainIndexFromGridPosition(agx::Vec3 position) const;
1226 
1232  agx::Vec3i getVoxelIndexFromWorldPosition(const agx::Vec3& position) const;
1233 
1239  agx::Vec3i getSurfaceVoxelIndexFromTerrainIndex(const agx::Vec2i& terrainIndex) const;
1240 
1247  void setForbiddenVoxelsForDynamicMass(const agx::Vec3iVector& voxelIndices);
1248 
1254  bool getForbiddenVoxelsForDynamicMass(agx::Vec3iVector& voxelIndices) const;
1255 
1262  bool isForbiddenVoxelsForDynamicMass(const agx::Vec3i& voxelIndex) const;
1263 
1269  bool voxelContainsShovel(const agx::Vec3i& voxelIndex) const;
1270 
1277  bool hasCreatedParticle( agx::Physics::ParticlePtr particle ) const;
1278 
1286  bool hasSameParticleMaterial(agx::Physics::ParticlePtr particle) const;
1287 
1293  TerrainMassOccupancyController* getMassOccupancyController() const;
1294 
1300  TerrainMaterialController* getMaterialController() const;
1301 
1302  void adjustFluidMassInGrid(agx::Vec3i binIndex, int binVoxels, float availableFluidMass, float remainingFluidMass);
1303 
1304  agx::Vec3iVector getVoxelsOccupiedByShovel(TerrainToolCollection *collection) const;
1305 
1306  bool isPointSubmerged( const agx::Vec3& point ) const;
1307 
1308  bool voxelHasParticles(const agx::Vec3i& voxelIndex) const;
1309 
1310  bool isForbiddenIndex(const agx::Vec3i& index);
1311 
1312  void addTerrainIndexToAvalanche(const agx::Vec2i& index);
1313 
1314  int getLowestAllowableVoxelIndexZ() const;
1315 
1316  agx::Vec3 constructVoxelFieldOffset() const;
1317 
1318  void setIsTerrainPagerTile(bool isPagerTile);
1319 
1320  bool getIsTerrainPagerTile() const;
1321 
1322  bool hasEnabledToolCollections() const;
1323 
1324  bool shouldUpdateTerrain() const;
1325 
1326  bool hasParticlesInTerrain() const;
1327 
1328  void debugRenderFluidMass();
1329 
1330  void clampHeights(agx::Real minValue);
1331 
1332  bool isSolidVoxel( const agx::Vec3i& index ) const;
1333 
1334  bool isActiveSolidVoxel( const agx::Vec3i& index ) const;
1335 
1337 
1338  protected:
1342  Terrain();
1343 
1347  virtual void addNotification() override;
1348 
1352  virtual void removeNotification() override;
1353 
1357  virtual void preCollide() override;
1358 
1362  virtual void pre() override;
1363 
1367  virtual void post() override;
1368 
1372  virtual void last() override;
1373 
1374  protected:
1375  typedef struct offsetIndexInfo
1376  {
1382  } offsetIndexInfo;
1383 
1384  using GoToStopCondition = std::function<bool(const agx::Vec3i& index)>;
1386 
1388  bool isSurfaceVoxel(const agx::Vec3i& index) const;
1389  bool isSolidBoundaryVoxel(const agx::Vec3i& index) const;
1390  bool isSubmergedVoxel(const agx::Vec3i& index) const;
1391  bool isFilledSolidVoxel(const agx::Vec3i& index) const;
1392  bool hasVoxelCompaction(const agx::Vec3i& index) const;
1393  bool isVoxelWithinTerrainIndexBounds(const agx::Vec3i& index) const;
1394  bool getSurfaceVoxel(const agx::Vec3i& voxelIndex, agx::Vec3i& result) const;
1395  bool findPathToSurfaceVoxel(const agx::Vec3i& startVoxel, std::deque<agx::Vec3i>& result);
1396  bool findSurfaceVoxel(const agx::Vec3i& index, agx::Vec3i& result) const;
1397  agx::Vec3i goToVoxelKernel(const agx::Vec3i& start, const GoToStopCondition& stopCondition) const;
1398  agx::Vec3i goToSurfaceVoxel(const agx::Vec3i& start) const;
1399  agx::Vec3i goToBoundaryVoxel(const agx::Vec3i& start) const;
1400  void updateSurfaceHeightFieldFromDirtyVoxels();
1401  void updateSurfaceHeightFieldFromVoxelIndices(const agx::Vec3iVector& updateIndices);
1402  void updateSurfaceHeightFieldFromVoxelIndices(const agxData::LocalVector<agx::Vec3i>& updateIndices);
1403  void updateSurfaceHeightFieldFromVoxelIndex(const agx::Vec3i& voxelIndex);
1404  void updateSurfaceHeightFieldFromHeightsTable( agx::HashTable<agx::Vec2i, std::pair<agx::Vec3i, agx::Real> >& table );
1405  void traceGridSurfaceVoxel(const agx::Vec2i& terrainIndex, agx::Vec3i& surfaceVoxel) const;
1406  agx::Real traceGridSurfaceHeightFromVoxelIndex(const agx::Vec3i& index);
1407  agx::Real calculateSurfaceHeightFromVoxelIndex(const agx::Vec3i& voxelIndex);
1408  void initializeSurfaceHeightFieldFromVoxelGrid();
1409  void initVoxelStructureFromHeightField( bool adjustForCompaction = false );
1410  void convertVoxelsInActiveZonesToDynamicMass();
1411  bool shouldPrimaryActiveZoneRemoveMass(TerrainToolCollection* toolCollection);
1412  bool shouldDeformerActiveZoneRemoveMass(TerrainToolCollection* toolCollection, DeformerCollection* deformerCollection);
1413  void convertRemovedOccupancyToDynamicMass();
1414  void createSoilParticles();
1415  agx::Vec3 getValidParticlePosition(const agx::Real radius, const agx::Vec3i& index, const agx::Vec3& pos);
1416  void addDirtyVoxelsToAvalanche();
1417  void removeSoilParticlesOutsideTerrainBounds();
1418  void updateTransformationFromParentBody();
1419  void storeModifiedTerrainIndex( const agx::Vec2i& terrainVertex );
1420  void addModifiedParticleTerrainIndex(const agx::Vec2i& terrainIndex);
1421  void updateIndicesForAvalanche(TerrainIndexSet& forbiddenVertices);
1422  agx::Real findNewHeightFromVoxelIndex(const agx::Vec3i& index);
1423 
1424  SoilParticlePtrVector calculateParticlesInContactWithSurfaceGraph(agxCollide::GeometryPtrVector& contactGeometries);
1425 
1426  bool _getIntersectingGridElements(
1427  BasicGrid* grid,
1428  agxCollide::Geometry* geometry,
1429  agx::Vec3iVector& gridPoints,
1430  bool isOccupied = true) const;
1431 
1432  void sortParticlesIntoVoxels();
1433  void sortParticlesIntoColumns();
1434 
1435  TerrainIndexSet findTerrainIndices(agxTerrain::SoilParticlePtrVector& particles);
1436 
1437  /*
1438  For every column we have particles in, shoot a ray into space and find possible collisions with the geometries that are connected in the particle-surface graph.
1439  If there is a collision, save the position for filtering later.
1440  */
1441  void raytraceColumns( agxCollide::GeometryPtrVector geometries,
1442  agx::HashTable <agx::Vec2i, agx::Vec4>& contactPositionTable,
1443  const TerrainIndexSet& columnTerrainIndices,
1444  bool parallelExecute);
1445 
1446  /*
1447  Filter away particles so the remaining particles all are below that columns contact position voxel.
1448  This prevents merging through geometries.
1449  */
1450  void filterParticlesAboveContactGeometryVoxels( agx::HashTable <agx::Vec2i, agx::Vec4>& contactPositionTable,
1452 
1453  void debugRenderContactGeometryVoxels();
1454 
1455  agx::HashTable<agx::Vec3i, agx::UInt64Vector> sortParticlesIntoBins(int binVoxels);
1456 
1457  void preSortSynchronizeParticleHeightField();
1458  void postSortSynchronizeParticleHeightField();
1459 
1460  // Sampling method
1461  void sampleParticleMassVelocity();
1462 
1463  void preUpdateTerrainContacts();
1464  void postUpdateCompaction();
1465  bool isDirty();
1466  void markVoxelToAvalanche(const agx::Vec3i& changedVoxel);
1467  void postSolveShovels();
1468  void updateOccupancyFromHeightChange(const agx::Vec2i& heightFieldIndex);
1469  void avalanche();
1470  void computeForbiddenBounds();
1471  void initializeFromHeightField();
1472  void sanitizeMaxDepthData(agx::Real maximumDepth);
1473 
1474  // Resize soil particles
1475  void resizeSoilParticles();
1476  void computeParticleGrowthTables(const agx::UInt64Vector& particleIndices, ParticleDeltaVector& particles);
1477  void growParticlesFromFluidMass(agx::Vec3i binIndex, int binVoxels, float availableFluidMass, ParticleDeltaVector& particles);
1478  void growSmallParticlesFromParticleMass(ParticleDeltaVector& particles, float particleRadiusUpperLimit);
1479  void growLargeParticlesFromParticleMass(ParticleDeltaVector& particles, float particleRadiusUpperLimit);
1480 
1481  // Merge soil particles functions
1482  void mergeSoilParticlesAndFluidMass();
1483  void addNeighbours(agxData::LocalVector<agx::Vec3i>& indexQueue, agx::Vec3i index);
1484  bool mergeFluidMass();
1485  void createParticleIslands( ParticleIslandVector& particleIslands );
1486  void mergeParticleIslands( const ParticleIslandVector& particleIslands, agxData::LocalVector<agx::Vec3i>& mergedParticleIndices );
1487  void resizeOrRemoveParticles( const agx::HashTable<SoilParticlePtr, std::array<agx::Real, 2>>& particlesToUpdate );
1488  agx::Real getMergeSpeedSquared() const;
1489 
1490  // Merge to cutting edge functions
1491  void mergeSoilToCuttingEdge();
1492  agx::Vec3iVector mergeCuttingEdgeColumn(agx::Vec3i voxel, size_t numVoxelsToSurfaceThreshold, agx::Plane cuttingPlane);
1493  float convertParticlesToSolidMass(agx::Vec3i voxel, ParticleWeightPairVector particles, float massToRemove, agx::Vec3iVector& changedIndices);
1494  void moveParticlesToSurface(agx::Vec3iVector changedVoxelIndices);
1495 
1496  // Step fluid mass
1497  void stepFluidMass();
1498  void calculateFluidMassTransport(agx::HashTable<agx::Vec3i, float>& fluidMassTable);
1499  void applyFluidMassChanges(agx::HashTable<agx::Vec3i, float>& fluidMassTable);
1500  void enforceIncompressibilityColumnwise();
1501  agx::Vec3f getVoxelVelocity(const agx::Vec3i& voxelIndex, agx::HashTable<agx::Vec3i, size_t>& shovelVoxels);
1502  void createFluidMassInVoxel(const agx::Vec3i& voxelIndex, agx::Real32 amount);
1503  void createFluidMassOnTerrainSurface(const agx::Vec2i& terrainIndex, int surfaceVoxelOffset, agx::Real32 amount);
1504 
1505  // Conversion functions for particle radius and general occupancy
1506  float convertParticleRadiusToOccupancy(float radius) const;
1507  float convertOccupancyToParticleRadius(float occupancy) const;
1508  float getDynamicMassCompaction(const agx::Vec3i& voxelIndex) const;
1509  bool particleIslandShouldMerge(const agx::Vector<std::pair<agx::Vec3i, offsetIndexInfo>>& island);
1510  bool voxelColumnHasParticles(const agx::Vec2i& vertex) const;
1511 
1512  // TerrainMaterial bookkeeping functions
1516  void removeEmptyTerrainMaterialVoxels();
1517 
1518 
1519 
1520  void calculateShovelVoxelIntersection();
1521 
1522  agx::HashVector<agx::Vec2i, agx::Int> getShovelHeightPerColumn();
1523 
1524  agx::Real convertFluidOccupancyToMass(agx::Real occupancy) const;
1525  agx::Real convertFluidMassToOccupancy(agx::Real mass) const;
1526 
1527  agx::Real generateRandomNumber();
1528  agx::Vec3 generateRandomSoilParticlePositionPerturbation(agx::Real maxDelta);
1529  agx::Quat generateRandomSoilParticleRotation();
1530 
1531  void debugRenderForbiddenBounds();
1532  void debugRenderShovelForbiddenBounds();
1533 
1534  AvalancheController* getAvalancheController() const;
1535 
1536  agx::Bool isIndexWithinBoundsIssueWarning( const agx::Vec2i& terrainIndex ) const;
1537 
1538  void renderVoxelAABB(agx::Vec3i index, agx::Vec4f color);
1539 
1540  void toolCollectionsPreCollide();
1541  agx::TaskGroupRef createTask_preCollide();
1542  void initBuildIslandTask();
1543  void updateInternalTransforms();
1544 
1545  const agx::AffineMatrix4x4& _getVoxelSpaceTransform() const;
1546  const agx::AffineMatrix4x4& _getInvVoxelSpaceTransform() const;
1547  const agx::AffineMatrix4x4& _getTransform() const;
1548  const agx::AffineMatrix4x4& _getInverseTransform() const;
1549 
1550  void moveEntityDataToCurrentThread();
1551 
1552  void sanityCheckHeightFieldAgainstMaxDepth();
1553 
1554  friend class AvalancheController;
1556  friend class TerrainMaterial;
1558  friend class TerrainGridControl;
1560  friend class TerrainMaterialController;
1561  friend class CompactionController;
1562  friend class TerrainPhysics;
1563  friend class TerrainCache;
1564 
1565  protected:
1566  virtual ~Terrain();
1567 
1568  Terrain(agxCollide::HeightField* heightField, agx::Real maximumDepth);
1569 
1570  private:
1573 
1574  private:
1575  agxCollide::HeightFieldRef m_surfaceHeightField;
1576  agxCollide::HeightFieldRef m_particleHeightField;
1577  agx::Real m_voxelSize;
1578  TerrainPropertiesRef m_properties;
1579  agx::UniformRealGenerator m_generator;
1580  float m_nominalSoilParticleRadius;
1581 
1582  agxCollide::GeometryRef m_surfaceGeometry;
1583  agx::AffineMatrix4x4 m_transform;
1584  agx::AffineMatrix4x4 m_invTransform;
1585  agx::AffineMatrix4x4 m_voxelSpaceTransform;
1586  agx::AffineMatrix4x4 m_invVoxelSpaceTransform;
1587  agx::Vec3 m_voxelFieldOffset;
1588 
1589  TerrainDataAtlasRef m_terrainDataAtlas;
1590  TerrainMassOccupancyControllerRef m_massOccupancyController;
1591  TerrainMaterialControllerRef m_materialController;
1592  CompactionControllerRef m_compactionController;
1593  TerrainContactGeneratorRef m_terrainContactGenerator;
1594  AvalancheControllerRef m_avalancheController;
1595  SoilSimulationInterfaceRef m_soilSimulationInterface;
1596  TerrainGridControlRef m_terrainGridInterface;
1597 
1598  agx::MaterialRef m_particleMaterial;
1599  agx::MaterialRef m_aggregateMaterial;
1600 
1601  agx::ContactMaterialRef m_terrainParticleContactMaterial;
1602  agx::ContactMaterialRef m_terrainAggregateContactMaterial;
1603  agx::ContactMaterialRef m_particleParticleContactMaterial;
1604 
1605  TerrainToolCollectionRefVector m_toolCollections;
1606  AvalancheController::TerrainIndexSet m_terrainIndicesToAvalanche;
1607 
1608  agx::HashTable<agxCollide::Geometry*, agx::Real> m_noMergeGeometryToDistance;
1609  size_t m_noMergeEdgeMargin;
1610  bool m_noMerge;
1611  BoundingAABBVector m_forbiddenBounds;
1612  agx::HashTable<agx::Vec3i, size_t> m_voxelIndexToShovelIndex;
1613  agx::HashTable<TerrainToolCollection*, agx::Vec3iVector> m_voxelsInPrimaryActiveZoneLastTimeStep;
1614  agx::Vec3iVector m_dirtyVoxelIndices;
1615  agx::Real32Vector m_removedOccupancy;
1616  agx::Real m_lastRemovedVolume;
1617  VoxelIndexSet m_voxelHasCreatedParticle;
1618  VoxelIndexSet m_forbiddenVoxelsForDynamicMass;
1619  TerrainIndexSet m_mergedTerrainVertices;
1620  ModifiedVerticesSetVector m_modifiedVoxelColumns;
1621  ModifiedVerticesSetVector m_modifiedTerrainVertices;
1622  ModifiedVerticesSetVector m_modifiedParticleVertices;
1623  ModifiedVerticesSetVector m_particleTerrainVertices;
1624 
1625 
1626  size_t m_numCreatedParticles;
1627  float m_averageSoilParticleRadius;
1628 
1629  // Sort particles into voxels
1630  VoxelIndexOffsetIndexInfoTable m_voxelParticleOffsetTable;
1631  ParticleWeightPairVector m_voxelParticleWeightPairVector;
1632 
1633  // Sort particles into columns
1634  agx::IndexVector m_particlesInContactWithSurface;
1635  agx::HashTable <agx::Vec2i, agx::Vec4> m_contactPositionTable;
1636 
1637  VoxelIndexOffsetIndexInfoTable m_columnParticleOffsetTable;
1638  ParticleWeightPairVector m_columnParticleWeightPairVector;
1639 
1640  agx::HashTable<agx::Vec2i, agx::Real> m_columnParticleHeight;
1641  agx::HighAccuracyTimer m_timer;
1642 
1643  agx::IndexLambdaKernelRef m_indexKernel;
1644 
1645  bool m_isTerrainPagerTile;
1646  bool m_enable;
1647 
1648  // !HACK! !BEWARE! Islands partitioning
1649  // The build islands task is not executed when using PPGS.
1650  // No island partitioning means no parallelization of solving the rigid body system.
1651  // However, we still need islands in Terrain to determine which particles are in contact with the ground.
1652  // Thus, we manually load and execute the task in Terrain.
1653  // The task is loaded and bound for each Terrain that is added but is only executed once by agxSDK::Simulation.
1654  // The task is executed using the runBuildIslandsTask() method.
1655  agx::TaskRef m_buildIslandsTask;
1656  agx::TaskGroupRef m_preCollideUpdateTask;
1657  agx::TaskRef m_sortParticlesToVoxelsTask;
1658  agx::TaskRef m_sortParticlesToColumnsTask;
1659  agx::TaskRef m_calculateShovelVoxelIntersection;
1660  agx::TaskRef m_convertSolidToDynamicMassTask;
1661 
1662  agx::ParallelTaskRef m_updateHeightfieldsTask;
1663  agx::Job m_updateHeightfieldsJobs[2];
1664  agx::ParallelTaskRef m_voxelCollisionTask;
1665  agx::ParallelTaskRef m_geometryFilterTask;
1666 
1667  bool m_enableFluidMerge; // Only used for testing. Should not be serialized.
1668 
1669  agx::Vector<agxCollide::ShapeRef> m_userInputFailureShapes;
1670  };
1671 
1672 
1673  inline TerrainDataAtlas* Terrain::getTerrainDataAtlas() const
1674  {
1675  return m_terrainDataAtlas;
1676  }
1677 
1679  {
1680  return m_materialController->getDefaultTerrainMaterial();
1681  }
1682 
1683 
1685  {
1686  return m_materialController->getDefaultTerrainMaterial();
1687  }
1688 
1689 
1691  {
1692  auto voxelIndex = getVoxelIndexFromWorldPosition(position);
1693  return m_materialController->getTerrainMaterial(voxelIndex);
1694  }
1695 
1696 
1697  inline size_t Terrain::getResolutionX() const
1698  {
1699  return m_surfaceHeightField->getResolutionX();
1700  }
1701 
1702 
1703  inline size_t Terrain::getResolutionY() const
1704  {
1705  return m_surfaceHeightField->getResolutionY();
1706  }
1707 
1708 
1710  {
1711  return m_voxelSize;
1712  }
1713 
1714 
1715 
1717  {
1718  return m_voxelSpaceTransform;
1719  }
1720 
1721 
1722 
1724  {
1725  return m_invVoxelSpaceTransform;
1726  }
1727 
1728 
1729 
1731  {
1732  return m_transform;
1733  }
1734 
1735 
1736 
1738  {
1739  return m_invTransform;
1740  }
1741 
1742 
1743  inline agx::Vec3i Terrain::getSurfaceVoxelIndexFromTerrainIndex(const agx::Vec2i& terrainIndex) const
1744  {
1745  auto solidGrid = static_cast<ColumnHeightGrid*>(m_terrainDataAtlas->getGridMassOccupancyData(TerrainDataAtlas::MassType::SOLID));
1746  int voxelHeight = solidGrid->getSurfaceHeightIndex(terrainIndex);
1747 
1748  return agx::Vec3i( terrainIndex.x(), terrainIndex.y(), voxelHeight);
1749  }
1750 
1751  inline bool Terrain::getIsTerrainPagerTile() const
1752  {
1753  return m_isTerrainPagerTile;
1754  }
1755 
1756  inline bool Terrain::getDeformable() const
1757  {
1758  return m_properties->getEnableDeformation();
1759  }
1760 
1761 }
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...
agx::Vec3 AGXTERRAIN_EXPORT getSeparationFrictionForce(const Terrain *terrain, const Shovel *shovel)
Given geometry contacts exists and the solver has solved them - calculates total contact friction for...
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:515
int getSurfaceHeightIndex(const agx::Vec2i terrainIndex)
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...
Class that handles and sanity checks the mass logic in the terrain.
agx::Vec3 AGXTERRAIN_EXPORT getSeparationNormalForce(const Terrain *terrain, const Shovel *shovel)
Given geometry contacts exists and the solver has solved them - calculates total contact normal force...
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...
Pure virtual interface class for the data storage grid classes we have.
Definition: Grid.h:71
const agx::AffineMatrix4x4 & _getInvVoxelSpaceTransform() const
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
const agx::AffineMatrix4x4 & _getVoxelSpaceTransform() const
bool Bool
Definition: Integer.h:48
Base class for a shape.
Definition: Shape.h:53
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:62
A class holding 4 dimensional vectors and providing basic arithmetic.
Definition: Vec4Template.h:34
Class for containing and managing the different agxTerrain::Terrain settings.
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes...
Definition: Geometry.h:59
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...
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
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
Class representing the mathematical concept of a plane, also called a half- space.
Definition: Plane.h:31
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:92
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
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.
agx::HashSet< agx::Vec2i > TerrainIndexSet
Vec3T< Int > Vec3i
Definition: Vec3.h:43
agx::Vec3 AGXTERRAIN_EXPORT getAggregateTerrainTangentialForce(const Terrain *terrain, const Shovel *shovel, Shovel::ExcavationMode excavationMode)
Internal method Get the aggregate <-> terrain tangential force with the terrain given an excavation m...
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
Class implementing the BasicGrid interface.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:375
TerrainMaterial * getDefaultTerrainMaterial() const
Returns the default terrain material which is used to derive material and contact material parameters...
TerrainMaterial * getTerrainMaterial() const
Returns the default 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:207
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:126
Local scope vector.
Definition: LocalVector.h:43
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
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agx::HashTable< agx::Vec3i, ParticleWeightPairVector > IndexToParticleWeightPairVector
bool getDeformable() const
Gets whether or not deformations are enabled for this terrain.
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:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97