AGX Dynamics  2.37.2.0
TerrainUtils.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_UTILS_H
18 #define AGXMODEL_TERRAIN_UTILS_H
19 
20 #include <agxModel/export.h>
21 
22 
24 #include <agxCollide/Geometry.h>
25 #include <agxCollide/HeightField.h>
26 #include <agxCollide/Contacts.h>
27 #include <agx/Material.h>
28 
29 #include <agx/FrictionModel.h>
30 
31 #include <agxSDK/Simulation.h>
32 
33 
37 
38 namespace agxModel
39 {
40 
41  class ImpactVolumeData;
42 
49  {
50  public:
52  : m_localHeightUpdate(false), m_lockBorders(false), m_avalancheMassLoss(true), m_heightField(heightField)
53  {}
55  : m_localHeightUpdate(false), m_lockBorders(false), m_avalancheMassLoss(true), m_heightField(nullptr)
56  {}
57  void setEnableLockedBorders( bool lock, bool avalancheMassLoss = true ) {
58  m_lockBorders = lock;
59  m_avalancheMassLoss = avalancheMassLoss;
60  }
62  return m_avalancheMassLoss;
63  }
64  bool getEnabledLockedBorders() const {
65  return m_lockBorders;
66  }
68  m_heightField->calculateMassProperties();
69  }
70  void setHeight( size_t x, size_t y, agx::Real height );
71  AGX_FORCE_INLINE size_t getLocalX( size_t globalX ) {
72  return std::max(globalX, m_minX) - m_minX;
73  }
74  AGX_FORCE_INLINE size_t getLocalY( size_t globalY ) {
75  return std::max(globalY, m_minY) - m_minY;
76  }
77  AGX_FORCE_INLINE size_t getLocalVectorIndex( size_t globalX, size_t globalY ) {
78  return (m_maxX - m_minX + 1) * getLocalY(globalY) + getLocalX(globalX);
79  }
80  agx::Real getHeight( size_t x, size_t y ) const;
81  void startLocalHeightUpdate( size_t minX = 0, size_t maxX = 0, size_t minY = 0, size_t maxY = 0);
82  void endLocalHeightUpdate();
83 
85  return m_localHeightUpdate;
86  }
88  return m_heightField;
89  }
90 
92 
93  protected:
94 
95  private:
96  bool m_localHeightUpdate;
97  bool m_lockBorders;
98  bool m_avalancheMassLoss;
99  agxCollide::HeightFieldRef m_heightField;
100  agx::RealVector m_tempHeights;
101  size_t m_minX;
102  size_t m_maxX;
103  size_t m_minY;
104  size_t m_maxY;
105  };
107 
109  {
110  public:
112  void render( agxRender::RenderManager* mgr );
113 
114  protected:
115  virtual ~TerrainDebugRenderer();
116 
120  };
121 
127  {
128  public:
129 
131  : m_particleResolutionX(1), m_particleResolutionY(1), m_scale(1), m_heightFieldToParticleCoordinateMultiplier(1)
132  {}
133 
137  void updateResolution( const agx::Real& particleRadius, const size_t& heightFieldResolutionX, const size_t& heightFieldResolutionY, const agx::Real& scale );
138 
139  enum DirectionFCC { VERTEX_POS, POS_X_NEG_Z, POS_Y_NEG_Z, NEG_X_NEG_Z, NEG_Y_NEG_Z, POS_X_POS_Y, NEG_X_POS_Y, NEG_X_NEG_Y, POS_X_NEG_Y, POS_X_POS_Z, POS_Y_POS_Z, NEG_X_POS_Z, NEG_Y_POS_Z };
140  //middle vertex,--------- the four below-------ccw--------------,-----the four on same height as vertex----ccw---,----the four above------ccw----------------------
141 
145  agx::Real findFccHeight( agx::Real heightAtVertexPos, agx::Real dX, agx::Real dY, DirectionFCC fccLocalPos );
146 
150  agx::Real findParticleCoordinateHeight(const size_t pcsX, const size_t pcsY, Terrain* terrain );
151 
152 
157  agx::Vec3 getHeightFieldPositionFromFCCIndex( size_t fccIndex, const agx::Real minZ, size_t& pcsX, size_t& pcsY );
158 
163  void getParentHeightFieldVertex( const size_t& pcsX, const size_t& pcsY, size_t& hfX, size_t& hfY );
164 
169  void collectCloseParticleCoordinates( const size_t& hfX, const size_t& hfY, agx::Vec2iVector& particleCoordinates );
170 
176  void collectCloseParticleLowerXYCoordinatesWithHeight( const agxCollide::HeightField* hf, const size_t& hfX, const size_t& hfY,
177  Vec2iHeightDataPairVector& particleCoordinatesWithHeight, Terrain* terrain);
178 
183  size_t indexForFCCLattice( size_t pcsX, size_t pcsY, size_t pcsZ, DirectionFCC dir );
184 
188  agx::Real scaleDepth( const agx::Real& depth, const size_t pcsX, const size_t pcsY, const agx::Vec2& hf2Dpos );
189 
194  agx::Vec2 getHeightFieldPosition( const size_t pcsX, const size_t pcsY, const size_t dX = 0, const size_t dY = 0 );
195 
196  private:
197  size_t m_particleResolutionX;
198  size_t m_particleResolutionY;
199 
200  agx::Real m_scale;
201 
202  agx::Real m_heightFieldToParticleCoordinateMultiplier;
203  };
204 
207  {
208  public:
211  }
212 
213  void post(const agx::TimeStamp& ) {
214  m_localContacts.clear(agx::Container::MAINTAIN_BUFFER);
215  }
216 
217  void pre( const agx::TimeStamp& /*t*/ ) {
218  this->getSimulation()->getSpace()->addGeometryContacts(m_localContacts);
219  }
220 
221  bool createGeometryContact(agxCollide::Geometry* g1, agxCollide::Geometry* g2, agx::Vec3Vector points, agx::Vec3Vector normals, agx::RealVector depths, agx::Real pointArea, agx::ContactMaterial* cm = nullptr);
222 
223 
224  private:
226 
227  };
228 
230 
232 
234 
237  {
238  public:
248 
259  virtual void calculateTangentPlane(const agxCollide::Geometry* geometry1, const agxCollide::Geometry* geometry2, const agx::Vec3& point, const agx::Vec3& normal, const agx::Real depth, agx::Vec3& ret_u, agx::Vec3& ret_v) const override;
260 
261  void addBody(agx::RigidBody* body, const agx::Vec3& localFrictionDir);
262 
264 
265  protected:
266 
267  RigidBodyObserverVec3PairVector m_bodiesAndDir;
269  };
270 
271 
274  {
275  public:
277 
279  return m_dynamicHeightFieldParticleCoordinateSystem;
280  }
281 
283  return m_contactGenerator;
284  }
285 
286  void setParticleRadius( agx::Real radius ) {
287  m_particleRadius = radius;
288  }
289 
291  return m_particleRadius;
292  }
293 
295  return m_uniqueParticleId;
296  }
297 
298  protected:
301  ContactGeneratorRef m_contactGenerator;
304  };
305 
307 
308 
314  {
315  public:
317  : m_geometryContact(gc), m_index(index) {
318  m_geometry1 = gc->geometry(0);
319  m_geometry2 = gc->geometry(1);
320  }
321 
322  bool isValid() const {
323  return m_geometry1->getEnable() && m_geometry2->getEnable() && (m_geometry1->getRigidBody() == nullptr || m_geometry1->getRigidBody()->getEnable() ) && (m_geometry2->getRigidBody() == nullptr || m_geometry2->getRigidBody()->getEnable());
324  }
325 
326  void add( ImpactVolumeData* ivd ) {
327  m_impactVolumeDataVector.push_back(ivd);
328  }
329 
330  const agxCollide::Geometry* getGeometry1() const { return m_geometry1; }
331  agxCollide::Geometry* getGeometry1() { return m_geometry1; }
332  const agxCollide::Geometry* getGeometry2() const { return m_geometry2; }
333  agxCollide::Geometry* getGeometry2() { return m_geometry2; }
334 
335  size_t getIndex() {
336  return m_index;
337  }
338 
339  void clear() {
340  m_impactVolumeDataVector.clear();
341  }
342 
344  return m_geometryContact;
345  }
347  return m_impactVolumeDataVector;
348  }
349  protected:
350  virtual ~GeometryContactInfo( ) {
351  clear();
352  }
353 
354  private:
355  ImpactVolumeDataPtrVector m_impactVolumeDataVector;
356  agxCollide::GeometryContact* m_geometryContact;
357  agxCollide::GeometryRef m_geometry1;
358  agxCollide::GeometryRef m_geometry2;
359  size_t m_index;
360 
361  };
362 
365 
367  {
368  if ( m_heightField == nullptr )
369  return agx::Real(0);
370  size_t vectorIndex = m_heightField->getResolutionX() * y + x;
371  agxAssert( !getLocalHeightUpdate() || m_tempHeights.size() > vectorIndex);
372 
373  if ( getLocalHeightUpdate() && m_tempHeights[vectorIndex] != -agx::Infinity )
374  return m_tempHeights[vectorIndex];
375 
376  return m_heightField->getHeight(x, y);
377  }
378 
379  AGX_FORCE_INLINE void HeightController::setHeight( size_t x, size_t y, agx::Real height )
380  {
381  if ( m_heightField == nullptr )
382  return;
383 
384  if ( m_lockBorders && ( x == 0 || y == 0 || x + 1 >= m_heightField->getResolutionX() || y + 1 >= m_heightField->getResolutionY() ) )
385  return;
386  if ( getLocalHeightUpdate() )
387  m_tempHeights[getLocalVectorIndex(x, y)] = height;
388  else
389  m_heightField->setHeight(x, y, height);
390  }
391 
393  {
394  public:
396 
398  size_t idx;
399  size_t x;
400  size_t y;
403 
404  private:
405  void initialize(Terrain* terrain);
406 
407 
408  };
409 
411  {
412  public:
413 
415 
416  void extractGeometryProperties(Terrain* terrain);
417 
418  //Values found before analyzing contact points
431 
432  // values updated during contact point analysis
442 
443  };
444 
445  class VolumeOverlap : public agx::Vec3
446  {
447  public:
448  VolumeOverlap();
449  enum Type
450  {
451  TOTAL = 0,
453  MOVED
454  };
455 
456  };
457 
458 }
459 
460 #endif
agx::Real getHeight(size_t x, size_t y) const
Definition: TerrainUtils.h:366
This class store the combine material properties between two agx::Material&#39;s.
Definition: Material.h:515
AGXTERRAIN_EXPORT void clear()
void setHeight(size_t x, size_t y, agx::Real height)
Definition: TerrainUtils.h:379
agx::ref_ptr< GeometryContactInfo > GeometryContactInfoRef
Definition: TerrainUtils.h:363
Derive from this class to implement a listener for simulation step events.
ParticleCoordinateSystem m_dynamicHeightFieldParticleCoordinateSystem
Definition: TerrainUtils.h:300
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:855
void pre(const agx::TimeStamp &)
Called before a step is taken in the simulation Implement this method in the derived class to get cal...
Definition: TerrainUtils.h:217
void setParticleRadius(agx::Real radius)
Definition: TerrainUtils.h:286
bool Bool
Definition: Integer.h:48
bool getEnabledLockedBorders() const
Definition: TerrainUtils.h:64
Triggered just AFTER a Dynamics stepForward step is taken.
Triggered just BEFORE a Dynamics stepForward step is taken.
HeightController(agxCollide::HeightField *heightField)
Definition: TerrainUtils.h:51
GeometryContactInfo(agxCollide::GeometryContact *gc, size_t index)
Definition: TerrainUtils.h:316
A contact between two geometries.
Definition: Contacts.h:270
agxCollide::GeometryContact * getGeometryContact()
Definition: TerrainUtils.h:343
size_t getLocalX(size_t globalX)
Definition: TerrainUtils.h:71
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
const agxCollide::Geometry * getGeometry1() const
Definition: TerrainUtils.h:330
agx::observer_ptr< agxCollide::Geometry > collidedGeometry
Definition: TerrainUtils.h:419
const agx::Material * terrainMat
Definition: TerrainUtils.h:401
#define AGX_FORCE_INLINE
Definition: Object.h:59
agx::Real TimeStamp
Definition: TimeStamp.h:26
agx::Vector< GeometryContactInfoRef > GeometryContactInfoRefVector
Definition: TerrainUtils.h:364
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc...
agx::ref_ptr< HeightController > HeightControllerRef
Definition: TerrainUtils.h:106
const agxCollide::HeightField * getHeightField() const
Definition: TerrainUtils.h:87
Base class providing referencing counted objects.
Definition: Referenced.h:119
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:36
agxCollide::Geometry * getGeometry1()
Definition: TerrainUtils.h:331
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
Templated vector class.
Definition: Vector.h:52
ParticleCoordinateSystem & getParticleCoordinateSystem()
Definition: TerrainUtils.h:278
ImpactVolumeData * ivd
Definition: TerrainUtils.h:397
const agx::Material * particleMat
Definition: TerrainUtils.h:402
const ImpactVolumeDataPtrVector & getImpactVolumeDataVector()
Definition: TerrainUtils.h:346
void post(const agx::TimeStamp &)
Called after a step is taken in the simulation Implement this method in the derived class to get call...
Definition: TerrainUtils.h:213
AGXCORE_EXPORT const Real Infinity
size_t getLocalVectorIndex(size_t globalX, size_t globalY)
Definition: TerrainUtils.h:77
#define agxAssert(expr)
Definition: debug.h:143
Inherit from this class and implement the render() method to make it it self during debug rendering...
Definition: Renderable.h:38
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
void setEnableLockedBorders(bool lock, bool avalancheMassLoss=true)
Definition: TerrainUtils.h:57
agx::Vector< std::pair< agx::RigidBodyObserver, agx::Vec3 > > RigidBodyObserverVec3PairVector
Definition: TerrainUtils.h:233
First Normal equation is calculated in the DIRECT solver, then in a second pass the normal and the fr...
Definition: FrictionModel.h:60
#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
Iterative Projected Cone Friction (IPC friction) model.
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
Simulation is a class that bridges the collision space agxCollide::Space and the dynamic simulation s...
Definition: Simulation.h:126
agxCollide::Geometry * getGeometry2()
Definition: TerrainUtils.h:333
bool getLocalHeightUpdate() const
Definition: TerrainUtils.h:84
const agxCollide::Geometry * getGeometry2() const
Definition: TerrainUtils.h:332
#define AGXMODEL_EXPORT
agx::ref_ptr< ContactGenerator > ContactGeneratorRef
Definition: TerrainUtils.h:229
double Real
Definition: Real.h:42
ContactGenerator * getContactGenerator()
Definition: TerrainUtils.h:282
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
agx::ref_ptr< DynamicHeightFieldSimulation > DynamicHeightFieldSimulationRef
Definition: TerrainUtils.h:306
void add(ImpactVolumeData *ivd)
Definition: TerrainUtils.h:326
agxCollide::Geometry * geometry(size_t ith)
Definition: Contacts.h:637
RigidBodyObserverVec3PairVector m_bodiesAndDir
Definition: TerrainUtils.h:263
size_t getLocalY(size_t globalY)
Definition: TerrainUtils.h:74
A HeightField is a collision shape that can be created from a grid structure, like an image...
uint32_t UInt32
Definition: Integer.h:40
bool getEnabledAvalancheMassLoss() const
Definition: TerrainUtils.h:61
agx::observer_ptr< agxRender::HeightFieldProxy > m_proxy
Definition: TerrainUtils.h:118