AGX Dynamics  2.35.0.1
TerrainUtils.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_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);
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:
113 
114  protected:
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 
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 
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 
269  };
270 
271 
274  {
275  public:
277 
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:
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 
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:
449  enum Type
450  {
451  TOTAL = 0,
453  MOVED
454  };
455 
456  };
457 
458 }
459 
460 #endif
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#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 between two geometries.
Definition: Contacts.h:270
agxCollide::Geometry * geometry(size_t ith)
Definition: Contacts.h:636
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.
bool createGeometryContact(agxCollide::Geometry *g1, agxCollide::Geometry *g2, agx::Vec3Vector points, agx::Vec3Vector normals, agx::RealVector depths, agx::Real pointArea, agx::ContactMaterial *cm=nullptr)
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 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
const agx::Material * particleMat
Definition: TerrainUtils.h:402
ImpactVolumeData * ivd
Definition: TerrainUtils.h:397
const agx::Material * terrainMat
Definition: TerrainUtils.h:401
ContactPointData(ImpactVolumeData *vd, Terrain *terrain)
agx::observer_ptr< agxCollide::Geometry > collidedGeometry
Definition: TerrainUtils.h:419
void extractGeometryProperties(Terrain *terrain)
DeformingGeometryContactProperties(agxCollide::Geometry *otherGeometry)
ContactGenerator * getContactGenerator()
Definition: TerrainUtils.h:282
ParticleCoordinateSystem m_dynamicHeightFieldParticleCoordinateSystem
Definition: TerrainUtils.h:300
void setParticleRadius(agx::Real radius)
Definition: TerrainUtils.h:286
ParticleCoordinateSystem & getParticleCoordinateSystem()
Definition: TerrainUtils.h:278
const agxCollide::Geometry * getGeometry1() const
Definition: TerrainUtils.h:330
agxCollide::GeometryContact * getGeometryContact()
Definition: TerrainUtils.h:343
const agxCollide::Geometry * getGeometry2() const
Definition: TerrainUtils.h:332
agxCollide::Geometry * getGeometry2()
Definition: TerrainUtils.h:333
const ImpactVolumeDataPtrVector & getImpactVolumeDataVector()
Definition: TerrainUtils.h:346
void add(ImpactVolumeData *ivd)
Definition: TerrainUtils.h:326
GeometryContactInfo(agxCollide::GeometryContact *gc, size_t index)
Definition: TerrainUtils.h:316
agxCollide::Geometry * getGeometry1()
Definition: TerrainUtils.h:331
size_t getLocalX(size_t globalX)
Definition: TerrainUtils.h:71
HeightController(agxCollide::HeightField *heightField)
Definition: TerrainUtils.h:51
void setHeight(size_t x, size_t y, agx::Real height)
Definition: TerrainUtils.h:379
size_t getLocalY(size_t globalY)
Definition: TerrainUtils.h:74
bool getLocalHeightUpdate() const
Definition: TerrainUtils.h:84
void setEnableLockedBorders(bool lock, bool avalancheMassLoss=true)
Definition: TerrainUtils.h:57
agx::Real getHeight(size_t x, size_t y) const
Definition: TerrainUtils.h:366
size_t getLocalVectorIndex(size_t globalX, size_t globalY)
Definition: TerrainUtils.h:77
bool getEnabledLockedBorders() const
Definition: TerrainUtils.h:64
const agxCollide::HeightField * getHeightField() const
Definition: TerrainUtils.h:87
bool getEnabledAvalancheMassLoss() const
Definition: TerrainUtils.h:61
void startLocalHeightUpdate(size_t minX=0, size_t maxX=0, size_t minY=0, size_t maxY=0)
void collectCloseParticleLowerXYCoordinatesWithHeight(const agxCollide::HeightField *hf, const size_t &hfX, const size_t &hfY, Vec2iHeightDataPairVector &particleCoordinatesWithHeight, Terrain *terrain)
Given one vertex in the height field (that has changed height for example) we get all surrounding par...
agx::Vec2 getHeightFieldPosition(const size_t pcsX, const size_t pcsY, const size_t dX=0, const size_t dY=0)
Get 2D position on the height field in length units.
void updateResolution(const agx::Real &particleRadius, const size_t &heightFieldResolutionX, const size_t &heightFieldResolutionY, const agx::Real &scale)
If the particle radius is changed, we must update our resolution for the particle coordinate system.
agx::Real findParticleCoordinateHeight(const size_t pcsX, const size_t pcsY, Terrain *terrain)
Calculate the height of a particle position.
agx::Real scaleDepth(const agx::Real &depth, const size_t pcsX, const size_t pcsY, const agx::Vec2 &hf2Dpos)
Given the changed height (depth) of a height field vertex, we get the height change at the particle c...
agx::Vec3 getHeightFieldPositionFromFCCIndex(size_t fccIndex, const agx::Real minZ, size_t &pcsX, size_t &pcsY)
Translates a position in the imaginary FCC 3 dimensional grid to a world position Returns positions i...
size_t indexForFCCLattice(size_t pcsX, size_t pcsY, size_t pcsZ, DirectionFCC dir)
hfX, hfY are index in height field.
agx::Real findFccHeight(agx::Real heightAtVertexPos, agx::Real dX, agx::Real dY, DirectionFCC fccLocalPos)
Calculate height of height field fcc position, given x and y derivatives and the height of the middle...
void collectCloseParticleCoordinates(const size_t &hfX, const size_t &hfY, agx::Vec2iVector &particleCoordinates)
Given one vertex in the height field (that has changed height for example) we get all surrounding par...
void getParentHeightFieldVertex(const size_t &pcsX, const size_t &pcsY, size_t &hfX, size_t &hfY)
To find what hf vertex position For example needed when we need to know which material a particle sho...
TerrainDebugRenderer(agxModel::Terrain *terrain)
agx::observer_ptr< agxRender::HeightFieldProxy > m_proxy
Definition: TerrainUtils.h:118
void render(agxRender::RenderManager *mgr)
Pure virtual method that should implemented to render a derived class during debug rendering.
void addBody(agx::RigidBody *body, const agx::Vec3 &localFrictionDir)
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
Calculates friction plane given normal.
RigidBodyObserverVec3PairVector m_bodiesAndDir
Definition: TerrainUtils.h:263
TerrainIterativeProjectedConeFrictionModel(agx::FrictionModel::SolveType solveType=agx::FrictionModel::SPLIT)
Default constructor.
Class for managing the rendering of geometries, shapes, rigid bodies, constraints etc.
Inherit from this class and implement the render() method to make it it self during debug rendering.
Definition: Renderable.h:39
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.
@ POST_STEP
Triggered just AFTER a Dynamics stepForward step is taken.
@ PRE_STEP
Triggered just BEFORE a Dynamics stepForward step is taken.
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:45
This class store the combine material properties between two agx::Material's.
Definition: Material.h:528
@ MAINTAIN_BUFFER
Buffer is maintained (normal stl behavior).
Definition: Container.h:46
size_t size() const
Definition: Container.h:134
@ SPLIT
First Normal equation is calculated in the DIRECT solver, then in a second pass the normal and the fr...
Definition: FrictionModel.h:60
Iterative Projected Cone Friction (IPC friction) model.
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:388
Base class providing referencing counted objects.
Definition: Referenced.h:114
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
Templated vector class.
Definition: agx/Vector.h:53
void clear(ClearPolicy policy=SHRINK_BUFFER_AVERAGED)
Remove all elements, optionally with maintained buffer allocation.
Definition: agx/Vector.h:491
void push_back(const T2 &value)
Definition: agx/Vector.h:715
#define agxAssert(expr)
Definition: debug.h:143
#define AGX_FORCE_INLINE
Definition: macros.h:58
Contain classes for higher level modeling primitives, such as Tree, Terrain etc.
Definition: FlowUnit.h:37
agx::ref_ptr< HeightController > HeightControllerRef
Definition: TerrainUtils.h:106
agx::ref_ptr< GeometryContactInfo > GeometryContactInfoRef
Definition: TerrainUtils.h:363
agx::ref_ptr< DynamicHeightFieldSimulation > DynamicHeightFieldSimulationRef
Definition: TerrainUtils.h:306
agx::Vector< GeometryContactInfoRef > GeometryContactInfoRefVector
Definition: TerrainUtils.h:364
agx::Vector< std::pair< agx::RigidBodyObserver, agx::Vec3 > > RigidBodyObserverVec3PairVector
Definition: TerrainUtils.h:233
agx::ref_ptr< ContactGenerator > ContactGeneratorRef
Definition: TerrainUtils.h:229
uint32_t UInt32
Definition: Integer.h:40
bool Bool
Definition: Integer.h:48
AGXCORE_EXPORT const Real Infinity
double Real
Definition: Real.h:42
Vec3T< T > max(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:874
agx::Real TimeStamp
Definition: TimeStamp.h:26