AGX Dynamics  2.37.3.2
agxModel/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 #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 
90  Terrain( agxCollide::HeightField* heightField );
91 
96  Terrain( agxCollide::Geometry* terrainGeometry );
97 
101  agxCollide::Geometry* getGeometry() const;
102 
106  agxModel::TerrainDataInterface* getDataInterface() const;
107 
111  agx::Real getMaximumParticleRadius() const;
112 
116  void setDynamicHeightFieldCutOffVelocity( agx::Real cutOffVelocity );
117 
121  agx::Real getDynamicHeightFieldCutOffVelocity() const;
122 
126  void setDynamicHeightFieldCutOffAngularVelocity( agx::Real cutOffAngularVelocity );
130  agx::Real getDynamicHeightFieldCutOffAngularVelocity() const;
131 
132 
133 #if TERRAIN_USE_PARTICLE_SYSTEM
134  agx::ParticleSystem* getParticleSystem() const;
135 #endif
136 
139  virtual void setMaterial(agx::Material* material);
140 
144  void set( agxModel::TerrainDataInterface* dtdc );
145 
149  void setEnableDynamicHeightField( bool enable );
150 
154  bool getEnableDynamicHeightField() const;
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 
178  void setVerticalYoungsModulusAreaScaler( agx::Real scaler );
179 
183  agx::Real getVerticalYoungsModulusAreaScaler() const;
184 
193  void setHorizontalYoungsModulusAreaScaler( agx::Real scaler );
194 
198  agx::Real getHorizontalYoungsModulusAreaScaler() const;
199 
204  void avalancheAll( size_t iterations );
205 
213  void setWetnessScale( agx::Real wetnessScaler );
214 
218  agx::Real getWetnessScale() const;
219 
226  void setAvalancheDecayPercent( agx::Real scaler );
227 
231  agx::Real getAvalancheDecayPercent() const;
232 
236  void setAvalancheStepLifeTime( size_t numSteps );
237 
241  size_t getAvalancheStepLifeTime() const;
242 
246  void setAvalancheVertexDistanceExtent( size_t numVertices );
247 
251  size_t getAvalancheVertexDistanceExtent() const;
252 
259  void setShovelMinimumDigAngleToHorizontalPlane( agx::Real angle );
260 
264  agx::Real getShovelMinimumDigAngleToHorizontalPlane() const;
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 
279  const agxModel::HeightController* getHeightController() const;
280 
286  const ParticleAttributes& getParticleAttributes() const;
287  ParticleAttributes& getParticleAttributes();
288 
292  agx::Real getDynamicHeightFieldParticleRadius() const;
293 
300  const agx::Vec2iVector& getModifiedIndices() const;
301 
306  const SoilParticleObsVector& getCreatedParticles() const;
307 
312 #if TERRAIN_USE_PARTICLE_SYSTEM
313  const agx::VectorPOD<agxData::EntityPtr>& getRemovedParticles() const;
314 #else
315  const SoilParticleRefVector& getRemovedParticles() const;
316 #endif
317  const agxCollide::HeightField* getHeightField( ) const;
318 
322  const DynamicHeightFieldSimulation* getDynamicHeightFieldSimulation() const;
323 
327  agx::Vec3 transformPointToTerrain( const agx::Vec3 point ) const;
328  agx::Vec3 transformPointToWorld( const agx::Vec3 point ) const;
329  agx::Vec3 transformVectorToTerrain( const agx::Vec3 vector ) const;
330  agx::Vec3 transformVectorToWorld( const agx::Vec3 vector ) const;
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
357 
360  SoilParticleSimulation* getParticleSimulation();
361 #endif
363 
364  protected:
365 
366  Terrain();
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
This class store the combine material properties between two agx::Material&#39;s.
Definition: Material.h:515
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: HashSet.h:669
When converting Terrain to particles, we create SoilParticles.
Derive from this class to implement a listener for simulation step events.
bool Bool
Definition: Integer.h:48
This namespace consists of a set of classes for handling geometric intersection tests including boole...
A contact between two geometries.
Definition: Contacts.h:270
agx::Real TimeStamp
Definition: TimeStamp.h:26
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
A basic particle system that contains all the storages and buffers required for basic operation and a...
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
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
This class implements an interface for how the terrain accesses the data it needs to access data item...
#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
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
A contact point in a contact data.
Definition: Contacts.h:55
agx::ref_ptr< Terrain > TerrainRef
#define AGXMODEL_EXPORT
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
double Real
Definition: Real.h:42
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
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...