AGX Dynamics  2.35.0.1
agxModel/Terrain.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 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 #ifndef AGXMODEL_TERRAIN_H
18 #define AGXMODEL_TERRAIN_H
19 
20 #include <agxModel/export.h>
21 
22 
24 #include <agxCollide/Geometry.h>
25 #include <agxCollide/Contacts.h>
26 #include <agx/Material.h>
27 
28 #include <agx/FrictionModel.h>
29 
30 #include <agxCollide/agxCollide.h>
31 #include <agxSDK/Simulation.h>
32 #include <agx/Range.h>
33 
36 #include <agxModel/TerrainUtils.h>
38 
39 
40 namespace agxCollide
41 {
42  class HeightField;
43 }
44 
45 
46 namespace agxModel
47 {
84  {
85  public:
86 
91 
96  Terrain( agxCollide::Geometry* terrainGeometry );
97 
102 
107 
112 
117 
122 
131 
132 
133 #if TERRAIN_USE_PARTICLE_SYSTEM
134  agx::ParticleSystem* getParticleSystem() const;
135 #endif
139  virtual void setMaterial(agx::Material* material);
140 
145 
149  void setEnableDynamicHeightField( bool enable );
150 
155 
159  bool isGeometryShovel(const agxCollide::Geometry* otherGeometry) const;
160 
164  bool isGeometryShovelMovingForward(const agxCollide::Geometry* otherGeometry, agx::Vec3& shovelForwardW) const;
165 
169  void setEnableLockedBorders( bool enable, bool triggerAvalancheMassLoss = true );
170 
179 
184 
194 
199 
204  void avalancheAll( size_t iterations );
205 
213  void setWetnessScale( agx::Real wetnessScaler );
214 
219 
227 
232 
236  void setAvalancheStepLifeTime( size_t numSteps );
237 
241  size_t getAvalancheStepLifeTime() const;
242 
246  void setAvalancheVertexDistanceExtent( size_t numVertices );
247 
252 
260 
265 
269  agx::Vec3 pointRelativeVelocityLocal(const agxCollide::Geometry* shovelGeometry, const agx::Vec3 pointWorld) const;
270 
274  agx::Real calculateShovelContactDepth(const agxCollide::Geometry* shovelGeometry, const agx::Vec3 normalLocal, const agx::Vec3 pointWorld) const;
275 
280 
288 
293 
301 
307 
312 #if TERRAIN_USE_PARTICLE_SYSTEM
313  const agx::VectorPOD<agxData::EntityPtr>& getRemovedParticles() const;
314 #else
316 #endif
318 
323 
331 
333 
338  virtual void pre(const agx::TimeStamp&) override;
339 
344  virtual void post(const agx::TimeStamp&) override;
345 
349  virtual void addNotification() override;
350 
354  virtual void removeNotification() override;
355 
356 #if defined(USE_SOIL_PARTICLE_SIMULATION) && USE_SOIL_PARTICLE_SIMULATION == 1
360  SoilParticleSimulation* getParticleSimulation();
361 #endif
363 
364  protected:
365 
367  Terrain( const Terrain& original );
368  virtual ~Terrain();
369 
370  private:
371 
372  struct ShovelPlane
373  {
374  public:
375  size_t hfXorigin;
376  size_t hfYorigin;
377  agx::Real hfZorigin;
378  agx::Vec3 normal;
379 
380  agx::Real getHeight(const agx::Real hfX, const agx::Real hfY) const
381  {
382  agx::Real dx = hfX - agx::Real(hfXorigin);
383  agx::Real dy = hfY - agx::Real(hfYorigin);
384 
385  if (agx::equalsZero(dx) && agx::equalsZero(dy))
386  return hfZorigin;
387 
388  agx::Vec3 inHfPlaneVector = agx::Vec3(dx, dy, 0);
389  agx::Vec3 inHfPlaneOrthoVector = inHfPlaneVector - (inHfPlaneVector*normal) * normal;
390 
391  const size_t variable = agx::equalsZero(dx) ? 1 : 0;
392 
393  agx::Real percentOfActual = inHfPlaneOrthoVector[variable]/inHfPlaneOrthoVector[variable];
394 
395  agx::Real actualHeight = inHfPlaneOrthoVector.z() / percentOfActual;
396 
397  return actualHeight;
398  }
399 
400  agx::Bool isAbovePlane(agx::Vec3 hfPos, const agx::Real particleRadius) const
401  {
402  agx::Real planePosAtCoordinates = getHeight(hfPos.x(), hfPos.y());
403  return hfPos.z() > (planePosAtCoordinates + particleRadius);
404  }
405 
406  };
407 
408  void prepareForContactModification();
409 
413  SoilParticle* createParticle( agx::Real radius );
414  SoilParticle* getParticle( const size_t& x, const size_t& y, agxSDK::Simulation* simulation, bool forDynamicHeightField );
415 
416  void createTerrain();
417 
418  agxCollide::HeightField* _getHeightField();
419 
420  void replaceDynamicHeightFieldParticles();
421 
422  agx::Real calculateTotalVolume( agx::Vector< size_t >& allChangedVertices );
423 
424  void moveVolumeFromParticleMovement( agx::Vector<size_t>& grownIndices, agx::RigidBodyRefVector& bodies, const size_t numGeometriesColliding, agx::Real totalVolume, const agx::RigidBody* deformerBody );
425 
426  void simulateDynamicHeightField( const agxCollide::Geometry* geometry, agx::Vector< size_t >& localShrinkedVertices);
427 
428  void fakeSimulatedDynamicHeightField( const agxCollide::Geometry* geometry, agx::Vector< size_t >& localShrinkedVertices );
429 
430  void createProjectedGeometryContact(
431  const agxCollide::GeometryContact* geometryContact, agx::ContactMaterial* contactMaterialGravityDirection,
432  const agx::Vec3 gravityDirection, const bool inGravityDirection, const bool useGravityDepth);
433 
434  void addParticleParticleGeometryContact( size_t index1, size_t index2, agx::RigidBodyRefVector& bodies );
435 
439 #if TERRAIN_USE_PARTICLE_SYSTEM
440  bool validateParticleToMerge(agx::Physics::ParticlePtr particle);
441 #else
442  bool validateParticleToMerge(SoilParticle* particle) const;
443 #endif
444  bool handleParticleContactMaterial(SoilParticle* sp, agxCollide::GeometryContact* geometryContact, agxCollide::Geometry* otherGeometry);
445 
446 
447  void splitDeformingGeometryContact(DeformingGeometryContactProperties& dgp, agxCollide::GeometryContact* geometryContact, ConstContactMaterialVector& contactMaterials, agx::ContactMaterial* materialForFrictionOnly);
448 
449  void createGeometryContactGivenContactPoint(const agxCollide::GeometryContact* parentGeometryContact, ContactPointData& cpd, const agxCollide::ContactPoint* cp,const agx::ContactMaterial* contactMaterial);
450 
451  void createOldShovelDefinition(DeformingGeometryContactProperties& dgp, agxCollide::GeometryContact* geometryContact, ConstContactMaterialVector& contactMaterials, agx::ContactMaterial* contactMaterial);
452 
453  void resetNoGrowIndices();
454 
455  void updateAvalanceIndices(size_t minX, size_t maxX, size_t minY, size_t maxY);
456 
457  //Does the vertex have a body compressing int (or hovering above eps away?)
458  bool isNoGrowIndex( size_t index, agx::Real eps = agx::Real(0.1));
459 
460  agx::Real findReposeHeight( size_t indexHF, size_t x, size_t y, agx::Real compression, agx::Real hfScale ) const;
461 
467  void spreadAvalanche( size_t indexHF, const size_t vertexDistance );
468 
469  void avalanche();
470 
471  agx::ContactMaterial* createContactMaterial(const agx::ContactMaterial* originalCM, const agx::Real& newYoungs);
472 
476  agx::ContactMaterial* createMeanMaterial( ConstContactMaterialVector& contactMaterials, const agx::Real totalYoungsModulus );
477 
481  bool isInsideNonMergeArea( agx::Vec2i* coordinates );
482 
486  bool isInsideNonMergeArea( agx::Vec2i& coordinates );
487 
498  bool getDeltaHeights( agx::Vec3 pos, const agx::Real volume, agx::Real& dh0, agx::Real& dh1, agx::Real& dh2, ImpactVolumeData& ivd );
499 
503 #if TERRAIN_USE_PARTICLE_SYSTEM
504  bool mergeParticle( agx::Physics::ParticlePtr particle );
505 #else
506  bool mergeParticle( SoilParticle* particle );
507 #endif
508 
509  void addGeometryContactsForAddedParticles( SizeTPairVector& bodyIndexContactPairs,
510  agx::RigidBodyRefVector& bodies, RealSizeTPairVector& localMatrixHeights,
511  size_t localX, size_t localY, const size_t sizeX, const size_t sizeY
512  );
513 
518  size_t generateVolumeOfParticlesWithContacts( agx::HashSet<size_t>& insertedFccIndices, agx::Vector< size_t >& changedVertices, agxSDK::Simulation* simulation, agx::RigidBodyRefVector& bodies, SizeTPairVector& bodyIndexContactPairs, const agx::RigidBody* deformBody );
519 
523  void mergeParticles();
524 
528  void collectContacts();
529 
533  void clearContacts();
534 
538  void updateContacts( const agxCollide::GeometryContactPtrVector& contacts, bool removeContacts = false);
539 
543  agx::Real scaleGrowth( agx::Vector<size_t>& changedIndices, const bool justFindScaler, agx::Real scaler = agx::Real(1));
544 
552  VolumeOverlap changeHeights( agx::Vector< size_t >& changedVertices, const bool shrink = true, const bool volumeCalculation = false, const agx::Real compressionPercent = agx::Real(0), const bool onlyCompression = true);
553 
557  agx::Real calculateVolumeForTriangle( agx::Vec3 xPositions, agx::Vec3 yPositions, agx::Vec3 zPositions );
558 
563  agx::Real calculateVolumeDiffAroundOneVertex( size_t vectorIndex, agx::Real compression = 0 );
564 
568  bool shrinkShovelVolume( agx::Vector< size_t >& localChangedVertices, const bool shovelIsDeformer, const agxCollide::Geometry* shovel, const agx::Real shovelThickness, const agx::Vec3 shovelForwardW );
569 
573  agx::RigidBody* createVolumeReplacingParticle( agx::HashSet<size_t>& insertedFccIndices, agxSDK::Simulation* simulation, const agx::Real minHeightLevel, const agx::Real heightFieldHeight, const size_t particleX, const size_t particleY, const size_t particleZ, ParticleCoordinateSystem::DirectionFCC fccPos );
574 
575  bool accumulateVertexY( agx::Vec3& meanPos, agx::Vec3Vector& vertices, agx::Vector<agx::RangeReal>& localChangedHeights, const size_t minX, const size_t minY, const size_t sizeX, const size_t sizeY, const size_t localX, const size_t localY);
576  size_t accumulateVerticesX( agx::Vec3& meanPos, agx::Vec3Vector& vertices, agx::Vector<agx::RangeReal>& localChangedHeights, const size_t minX, const size_t minY, const size_t sizeX, const size_t sizeY, const size_t localX, const size_t localY, const size_t maxNum);
577  void createConvexBody(agx::Vector< size_t >& localChangedVertices, const agx::Real shovelThickness);
578 
583  size_t createParticles( agx::Vector< size_t >& localChangedVertices, const agx::Real volume, const agxCollide::Geometry* shovel, const agx::Real shovelThickness, const ShovelPlane& plane );
584 
585  class GeometryContactInfo;
589  void collectDeformationFromGeometryContact( agxModel::GeometryContactInfo* geometryContactInfo, agx::Vector< size_t >& allDeformedVertices);
590 
594  agx::Real handleContact(agxModel::GeometryContactInfo* geometryContactInfo, agx::Vector< size_t >& localChangedVertices, const bool isShovel, const bool deformsDynamicHeightField );
595 
600  bool isVisited( const size_t vertexIndex ) const;
601 
605  void setVisited( const size_t vertexIndex );
606 
610  void enqueueNeighbors( std::queue<size_t>& queue, int x, int y );
611 
612  void updateTempCompression(const size_t vertexIndex, agx::Real deltaCompression);
613  void setTempCompression(const size_t vertexIndex ,agx::Real compression);
614  agx::Real getTempCompression(const size_t vertexIndex) const;
615 
616  void notifyModifiedIndex(const size_t x, const size_t y, const size_t currentIndex, agx::Real& depth);
617 
622  agx::Real findVertexDisplacements( agxCollide::Geometry* collidingGeometry, ImpactVolumeData& ivd, agx::Vector< size_t >& localChangedVertices, const bool isShovel, const bool deformsDynamicHeightField );
623 
627  bool raycastVertex( agxCollide::Geometry* geom , agx::Real& depth, agx::Vec3 hfLocalPosition, const bool isShovelWithoutDeformer, agx::Real& maxThickness ) const;
628 
632  const agxCollide::Geometry* getOtherGeometry( const agxCollide::LocalGeometryContact* geometryContact ) const;
633  agxCollide::Geometry* getOtherGeometry( agxCollide::LocalGeometryContact* geometryContact );
634 
635  const agxCollide::Geometry* getOtherGeometry( const agxModel::GeometryContactInfo* geometryContactInfo ) const;
636  agxCollide::Geometry* getOtherGeometry( agxModel::GeometryContactInfo* geometryContactInfo );
637 
641  void setModifiedIndex( size_t x, size_t y );
642 
643  agxCollide::GeometryRef m_geometry;
644 
645  // Vectors with temporary values (could maybe be moved to TerrainDataInterface, and maybe replaced with hash tables for large height fields)
646  agx::RealVector m_tempCompression;
647  agx::RealVector m_noGrowIndices;
648  agx::BoolVector m_visitedIndices;
649 
650  GeometryContactInfoRefVector m_geometryContactsWithImpactVolumeData;
651 
655  bool m_useDynamicHeightField;
656  agx::Real m_cutOffVelocity;
657  agx::Real m_cutOffAngularVelocity;
658  agx::Real m_verticalYoungsModulusAreaScaler;
659  agx::Real m_horizontalYoungsModulusAreaScaler;
660 
664  agx::Real m_wetnessScale;
665  agx::Real m_avalancheDecayPercent;
666  size_t m_avalancheStepLifeTime;
667  size_t m_avalanceVertexDistanceExtent;
668 
672  agx::Real m_shovelMinAngleToHorizontalPlane;
673 
674  ParticleAttributes m_particleAttribute;
675  ParticleCoordinateSystem m_particleCoordinateSystem;
676  HeightControllerRef m_heightController;
677 #if defined(USE_SOIL_PARTICLE_SIMULATION) && USE_SOIL_PARTICLE_SIMULATION == 1
678  agxSDK::SimulationRef m_particleSimulation;
679 #endif
680 
681  DynamicHeightFieldSimulationRef m_dynamicHeightFieldSimulation;
682  ContactGeneratorRef m_shovelContactGenerator;
683 
684  agx::Vector<agx::ContactMaterialRef> m_temporaryContactMaterials;
685  agx::Vector< agx::ContactMaterialRef > m_originalContactMaterialStorage;
686  agx::Vector< agx::FrictionModelRef > m_shovelFrictionMaterialPool;
687 
688  //reset each timestep
689  agx::Vec2iVector m_modifiedIndices;
690 #if TERRAIN_USE_PARTICLE_SYSTEM
691  agx::ParticleSystemRef m_particleSystem;
692  agx::List<agx::Physics::ParticlePtr> m_particlesConsideredToBeRemoved;
693  SoilParticleObsVector m_createdParticles;
694  agx::VectorPOD<agxData::EntityPtr> m_removedParticles; //This vector contain the last ref ptr to these bodies
695 #else
696  agx::List<SoilParticle*> m_particlesConsideredToBeRemoved;
697  SoilParticleObsVector m_createdParticles;
698  SoilParticleRefVector m_removedParticles; //This vector contain the last ref ptr to these bodies
699 #endif
700  // Access to data for the terrain
702 
703  agx::ref_ptr<TerrainDebugRenderer> m_terrainDebugRenderer;
704 
705  agx::Vector<int> m_xDiff;
706  agx::Vector<int> m_yDiff;
707 
708  agx::Vector< agx::Vec2i > m_minNonMergeCoordinates;
709  agx::Vector< agx::Vec2i > m_maxNonMergeCoordinates;
710 
711  agx::Real m_cachedDigVolume;
712  size_t m_particlesLost;
713  };
714 
716 
717 }
718 
719 #endif
#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 AGXMODEL_EXPORT
A contact point in a contact data.
Definition: Contacts.h:56
A contact between two geometries.
Definition: Contacts.h:270
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
A HeightField is a collision shape that can be created from a grid structure, like an image.
This class implements an interface for how the terrain accesses the data it needs to access data item...
agxCollide::Geometry * getGeometry() const
agx::Real getDynamicHeightFieldParticleRadius() const
const ParticleAttributes & getParticleAttributes() const
bool getEnableDynamicHeightField() const
agx::Real getDynamicHeightFieldCutOffAngularVelocity() const
bool isGeometryShovel(const agxCollide::Geometry *otherGeometry) const
agx::Real getWetnessScale() const
void setEnableLockedBorders(bool enable, bool triggerAvalancheMassLoss=true)
Fixate the height of the height field borders.
agx::Vec3 transformPointToWorld(const agx::Vec3 point) const
virtual void post(const agx::TimeStamp &) override
Finds terrain vertices to shrink, and thereby introduce particles at the correct positions.
void avalancheAll(size_t iterations)
Goes through all vertices 'iterations' times and triggers avalanches.
agx::Real getMaximumParticleRadius() const
void setVerticalYoungsModulusAreaScaler(agx::Real scaler)
Set the vertical Young's modulus area scaler.
Terrain(agxCollide::HeightField *heightField)
Constructor.
const agxCollide::HeightField * getHeightField() const
ParticleAttributes & getParticleAttributes()
void set(agxModel::TerrainDataInterface *dtdc)
Set TerrainDataInterface.
agx::Vec3 transformVectorToWorld(const agx::Vec3 vector) const
const DynamicHeightFieldSimulation * getDynamicHeightFieldSimulation() const
void setWetnessScale(agx::Real wetnessScaler)
Set the wetness scale.
void setDynamicHeightFieldCutOffVelocity(agx::Real cutOffVelocity)
Set the cut off (threshold) velocity for an activation of the dynamic height field functionality at a...
void setAvalancheDecayPercent(agx::Real scaler)
Set the avalanche decay percent.
virtual void pre(const agx::TimeStamp &) override
Function below must be public. But should not be called from external functions.
const SoilParticleRefVector & getRemovedParticles() const
void setEnableDynamicHeightField(bool enable)
Use a particle simulation to deform the terrain (replaces pure compression).
void setShovelMinimumDigAngleToHorizontalPlane(agx::Real angle)
Set the angle between the Shovel forward vector and the height field "up" direction (gravitational di...
Terrain(agxCollide::Geometry *terrainGeometry)
Constructor.
virtual ~Terrain()
agx::Vec3 pointRelativeVelocityLocal(const agxCollide::Geometry *shovelGeometry, const agx::Vec3 pointWorld) const
Returns the relative velocity between the terrain and a point on a geometry.
void setHorizontalYoungsModulusAreaScaler(agx::Real scaler)
Set the horizontal Young's modulus area scaler.
agx::Real getShovelMinimumDigAngleToHorizontalPlane() const
virtual void removeNotification() override
Removes terrain geometry from space, but not simulation.
Terrain(const Terrain &original)
agxModel::TerrainDataInterface * getDataInterface() const
agx::Vec3 transformVectorToTerrain(const agx::Vec3 vector) const
agx::Real getVerticalYoungsModulusAreaScaler() const
bool isGeometryShovelMovingForward(const agxCollide::Geometry *otherGeometry, agx::Vec3 &shovelForwardW) const
agx::Real calculateShovelContactDepth(const agxCollide::Geometry *shovelGeometry, const agx::Vec3 normalLocal, const agx::Vec3 pointWorld) const
Finds the relative velocity between a point on the shovel and the Terrain, along the normalLocal dire...
agx::Real getHorizontalYoungsModulusAreaScaler() const
virtual void addNotification() override
Adds terrain geometry to simulation.
size_t getAvalancheVertexDistanceExtent() const
void setDynamicHeightFieldCutOffAngularVelocity(agx::Real cutOffAngularVelocity)
Set the cut off (threshold) angular velocity for an activation of the dynamic height field functional...
agx::Real getAvalancheDecayPercent() const
agx::Vec3 transformPointToTerrain(const agx::Vec3 point) const
Transform positions/vectors to/from terrain/world.
size_t getAvalancheStepLifeTime() const
agx::Real getDynamicHeightFieldCutOffVelocity() const
const agxModel::HeightController * getHeightController() const
void setAvalancheVertexDistanceExtent(size_t numVertices)
Set how many vertices away from a geometry with deform property an avalanche could be triggered.
void setAvalancheStepLifeTime(size_t numSteps)
Set the number of time steps the avalanche will continue at a vertex after the last time it was defor...
const agx::Vec2iVector & getModifiedIndices() const
virtual void setMaterial(agx::Material *material)
Set a material to the terrain geometry, which will be used for non material-mapped grid points.
const SoilParticleObsVector & getCreatedParticles() const
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:127
Derive from this class to implement a listener for simulation step events.
This class store the combine material properties between two agx::Material's.
Definition: Material.h:528
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:666
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:388
A basic particle system that contains all the storages and buffers required for basic operation and a...
Pointer to a entity instance of type Physics.Particle.
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
Vector containing 'raw' data.
Definition: agx/Vector.h:246
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:37
agx::Vector< SoilParticleRef > SoilParticleRefVector
agx::Vector< const agx::ContactMaterial * > ConstContactMaterialVector
agx::Vector< std::pair< agx::Real, size_t > > RealSizeTPairVector
agx::ref_ptr< HeightController > HeightControllerRef
Definition: TerrainUtils.h:106
agx::ref_ptr< Terrain > TerrainRef
agx::ref_ptr< DynamicHeightFieldSimulation > DynamicHeightFieldSimulationRef
Definition: TerrainUtils.h:306
agx::Vector< GeometryContactInfoRef > GeometryContactInfoRefVector
Definition: TerrainUtils.h:364
agx::Vector< std::pair< size_t, size_t > > SizeTPairVector
agx::Vector< SoilParticleObs > SoilParticleObsVector
agx::ref_ptr< ContactGenerator > ContactGeneratorRef
Definition: TerrainUtils.h:229
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: agx/Vec3.h:36
bool Bool
Definition: Integer.h:48
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
double Real
Definition: Real.h:42
agx::Real TimeStamp
Definition: TimeStamp.h:26