AGX Dynamics  2.37.2.0
AvalancheController.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 <agxTerrain/export.h>
20 #include <agx/Referenced.h>
21 #include <agx/agx_vector_types.h>
22 #include <agx/StackArray.h>
23 #include <agx/HashVector.h>
24 
25 namespace agxTerrain
26 {
27  class Terrain;
29 
30 
33  AGX_DECLARE_POINTER_TYPES(AvalancheController);
34  class AGXTERRAIN_EXPORT AvalancheController : public agx::Referenced
35  {
36  public:
38 
39  public:
44  AvalancheController(Terrain* terrain);
45 
51  AvalancheController();
52 
53  void resetIndices();
54 
55  void update();
56 
57  void setForbiddenVertices(const TerrainIndexSet& forbiddenVertices);
58 
59  void setIndicesToAvalanche(const TerrainIndexSet& indicesToAvalanche);
60 
61  void addIndicesToAvalanche(const TerrainIndexSet& indicesToAvalanche);
62 
63  void setAvalancheDecayPercent(agx::Real avalancheDecayPrecent);
64 
65  agx::Real getAvalancheDecayPercent() const;
66 
67  void setAvalancheMaxHeightGrowth(agx::Real maxHeightGrowth);
68 
69  agx::Real getAvalancheMaxHeightGrowth() const;
70 
71  const TerrainIndexSet& getModifiedVertices() const;
72 
73  const TerrainIndexSet& getAvalanchedVertices() const;
74 
75  const agx::Vec3iVector getTouchedIndices() const;
76 
77  public:
78 
80 
81 
82  protected:
83  virtual ~AvalancheController();
84 
85 
86  // Used to tracks surface during avalanching and cache information so
87  // nothing has to be read from the heightfield(s).
88  // Terrain (x,y) -> ( surfaceVoxel, height)
90  SurfaceInfoTable m_surfaceInfoTable;
91 
92  void applyGrowthMap();
93 
94  agx::Real findReposeHeightDifference(const agx::Vec2i& v1, const agx::Vec2i& v2);
95 
96  void generateVertexNeighborList(const agx::Vec2i& surfaceVertex, agx::StackArray<agx::Vec2i, 8>& neighborList );
97 
98  agx::Real calculateOccupancyTransfer(const agx::Vec2i& surfaceVertex, agx::Real heightDifference);
99 
100  agx::Real calculateMaximumOccupancyTransfer(const agx::Vec2i& surfaceVertex);
101 
102  private:
103  class AvalancheIterationMap
104  {
105  public:
106  AvalancheIterationMap(agx::Int kmin, agx::Int kmax)
107  : m_kmin(kmin)
108  , m_kmax(kmax)
109 
110  {
111  agxAssert(kmin <= kmax);
112  if (kmin <= kmax) {
113  m_avalancheIterationVector.resize((kmax - kmin + 1), agx::HashVector<agx::Vec2i>());
114  }
115  }
116 
117  const agx::HashVector<agx::Vec2i>& operator[](agx::Int k)
118  {
119  resizeFromK(k);
120  return m_avalancheIterationVector[toIndex(k)];
121  }
122 
123  void push_back(agx::Int k, const agx::Vec2i& index)
124  {
125  resizeFromK(k);
126  size_t vectorIndex = toIndex(k);
127  if (!m_avalancheIterationVector[vectorIndex].contains(index))
128  {
129  m_avalancheIterationVector[vectorIndex].push_back(index,index);
130  }
131  }
132 
133  bool contains(agx::Int k, const agx::Vec2i& index)
134  {
135  size_t vectorIndex = toIndex(k);
136  if (vectorIndex > m_avalancheIterationVector.size() - 1)
137  return false;
138 
139  return m_avalancheIterationVector[vectorIndex].contains(index);
140  }
141 
142  size_t toIndex(agx::Int k)
143  {
144  return m_kmax - k;
145  }
146 
147  size_t toK(agx::Int i)
148  {
149  return m_kmax - i;
150  }
151 
152  void resizeFromK(agx::Int k)
153  {
154  size_t vectorIndex = toIndex(k);
155  if (vectorIndex > m_avalancheIterationVector.size() - 1)
156  {
157  m_avalancheIterationVector.resize(vectorIndex + 1);
158  m_kmin = k;
159  }
160  }
161 
162  agx::Int kmin()
163  {
164  return m_kmin;
165  }
166 
167  agx::Int kmax()
168  {
169  return m_kmax;
170  }
171 
172  void clear()
173  {
174  m_avalancheIterationVector.clear();
175  }
176 
177  size_t size()
178  {
179  return m_avalancheIterationVector.size();
180  }
181 
182  private:
183  agx::Vector<agx::HashVector<agx::Vec2i>> m_avalancheIterationVector;
184  agx::Int m_kmin;
185  agx::Int m_kmax;
186  };
187 
188  private:
189  void avalanche(const agx::Vec2i vertexXY, AvalancheIterationMap& avalancheMap);
190 
191  private:
192  Terrain* m_terrain;
193  TerrainIndexSet m_forbiddenTerrainVertices;
194  TerrainIndexSet m_avalanchedTerrainVertices;
195  TerrainIndexSet m_indicesToAvalanche;
196  TerrainIndexSet m_modifiedTerrainVertices;
197  agx::Vec3iVector m_touchedTerrainIndices;
199  agx::Real m_avalancheDecayPercent;
200  agx::Real m_maxHeightGrowth;
201  };
202 
203  AGX_FORCE_INLINE const AvalancheController::TerrainIndexSet& AvalancheController::getModifiedVertices() const
204  {
205  return m_modifiedTerrainVertices;
206  }
207 
208  AGX_FORCE_INLINE const AvalancheController::TerrainIndexSet& AvalancheController::getAvalanchedVertices() const
209  {
210  return m_avalanchedTerrainVertices;
211  }
212 
213  AGX_FORCE_INLINE const agx::Vec3iVector AvalancheController::getTouchedIndices() const
214  {
215  return m_touchedTerrainIndices;
216  }
217 
219 }
AGXTERRAIN_EXPORT void clear()
#define AGXTERRAIN_STORE_RESTORE_INTERFACE
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes...
Definition: Geometry.h:59
Templated stack array class.
Definition: StackArray.h:33
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGX_FORCE_INLINE
Definition: Object.h:59
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:40
Templated vector class.
Definition: Vector.h:52
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
agx::HashSet< agx::Vec2i > TerrainIndexSet
#define agxAssert(expr)
Definition: debug.h:143
#define AGXTERRAIN_EXPORT
double Real
Definition: Real.h:42
Inheritance with partial specialization due to bug with ref_ptr containers.
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97