AGX Dynamics  2.35.0.1
AvalancheController.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 #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;
28 
34  {
35  public:
37 
38  public:
44 
51 
52  void resetIndices();
53 
54  void update();
55 
56  void setForbiddenVertices(const TerrainIndexSet& forbiddenVertices);
57 
58  void setIndicesToAvalanche(const TerrainIndexSet& indicesToAvalanche);
59 
60  void addIndicesToAvalanche(const TerrainIndexSet& indicesToAvalanche);
61 
62  void setAvalancheDecayPrecent(agx::Real avalancheDecayPrecent);
63 
65 
66  void setAvalancheMaxHeightGrowth(agx::Real maxHeightGrowth);
67 
69 
70  const TerrainIndexSet& getModifiedVertices() const;
71 
72  const TerrainIndexSet& getAvalanchedVertices() const;
73 
74  public:
78 
79  protected:
80  virtual ~AvalancheController();
81 
82 
83  // Used to tracks surface during avalanching and cache information so
84  // nothing has to be read from the heightfield(s).
85  // Terrain (x,y) -> ( surfaceVoxel, height)
86  typedef agx::HashTable<agx::Vec2i, std::pair<agx::Vec3i, agx::Real> > SurfaceInfoTable;
87  SurfaceInfoTable m_surfaceInfoTable;
88 
89  void applyGrowthMap();
90 
91  agx::Real findReposeHeightDifference(const agx::Vec2i& v1, const agx::Vec2i& v2);
92 
93  void generateVertexNeighborList(const agx::Vec2i& surfaceVertex, agx::StackArray<agx::Vec2i, 8>& neighborList );
94 
95  agx::Real calculateOccupancyTransfer(const agx::Vec2i& surfaceVertex, agx::Real heightDifference);
96 
97  agx::Real calculateMaximumOccupancyTransfer();
98 
99  private:
100  class AvalancheIterationMap
101  {
102  public:
103  AvalancheIterationMap(agx::Int kmin, agx::Int kmax)
104  : m_kmin(kmin)
105  , m_kmax(kmax)
106 
107  {
108  agxAssert(kmin <= kmax);
109  if (kmin <= kmax) {
110  m_avalancheIterationVector.resize((kmax - kmin + 1), agx::HashVector<agx::Vec2i>());
111  }
112  }
113 
114  const agx::HashVector<agx::Vec2i>& operator[](agx::Int k)
115  {
116  resizeFromK(k);
117  return m_avalancheIterationVector[toIndex(k)];
118  }
119 
120  void push_back(agx::Int k, const agx::Vec2i& index)
121  {
122  resizeFromK(k);
123  size_t vectorIndex = toIndex(k);
124  if (!m_avalancheIterationVector[vectorIndex].contains(index))
125  {
126  m_avalancheIterationVector[vectorIndex].push_back(index,index);
127  }
128  }
129 
130  bool contains(agx::Int k, const agx::Vec2i& index)
131  {
132  size_t vectorIndex = toIndex(k);
133  if (vectorIndex > m_avalancheIterationVector.size() - 1)
134  return false;
135 
136  return m_avalancheIterationVector[vectorIndex].contains(index);
137  }
138 
139  size_t toIndex(agx::Int k)
140  {
141  return m_kmax - k;
142  }
143 
144  size_t toK(agx::Int i)
145  {
146  return m_kmax - i;
147  }
148 
149  void resizeFromK(agx::Int k)
150  {
151  size_t vectorIndex = toIndex(k);
152  if (vectorIndex > m_avalancheIterationVector.size() - 1)
153  {
154  m_avalancheIterationVector.resize(vectorIndex + 1);
155  m_kmin = k;
156  }
157  }
158 
159  agx::Int kmin()
160  {
161  return m_kmin;
162  }
163 
164  agx::Int kmax()
165  {
166  return m_kmax;
167  }
168 
169  void clear()
170  {
171  m_avalancheIterationVector.clear();
172  }
173 
174  size_t size()
175  {
176  return m_avalancheIterationVector.size();
177  }
178 
179  private:
180  agx::Vector<agx::HashVector<agx::Vec2i>> m_avalancheIterationVector;
181  agx::Int m_kmin;
182  agx::Int m_kmax;
183  };
184 
185  private:
186  void avalanche(const agx::Vec2i vertexXY, AvalancheIterationMap& avalancheMap);
187 
188  private:
189  Terrain* m_terrain;
190  TerrainIndexSet m_forbiddenTerrainVertices;
191  TerrainIndexSet m_avalanchedTerrainVertices;
192  TerrainIndexSet m_indicesToAvalanche;
193  TerrainIndexSet m_modifiedTerrainVertices;
195  agx::Real m_avalancheDecayPrecent;
196  agx::Real m_maxHeightGrowth;
197  };
198 
200  {
201  return m_modifiedTerrainVertices;
202  }
203 
205  {
206  return m_avalanchedTerrainVertices;
207  }
208 }
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGXTERRAIN_STORE_RESTORE_INTERFACE
#define AGXTERRAIN_EXPORT
void setAvalancheDecayPrecent(agx::Real avalancheDecayPrecent)
void setForbiddenVertices(const TerrainIndexSet &forbiddenVertices)
const TerrainIndexSet & getModifiedVertices() const
const TerrainIndexSet & getAvalanchedVertices() const
void setIndicesToAvalanche(const TerrainIndexSet &indicesToAvalanche)
agx::Real getAvalancheMaxHeightGrowth() const
AvalancheController(Terrain *terrain)
Construct given terrain instance.
void setAvalancheMaxHeightGrowth(agx::Real maxHeightGrowth)
agx::Real getAvalancheDecayPrecent() const
void addIndicesToAvalanche(const TerrainIndexSet &indicesToAvalanche)
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
This class is a combined container which has the find complexity of a HashTable, deterministic iterat...
Definition: HashVector.h:41
Inheritance with partial specialization due to bug with ref_ptr containers.
Base class providing referencing counted objects.
Definition: Referenced.h:114
Templated vector class.
Definition: agx/Vector.h:53
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
AGXTERRAIN_EXPORT void clear()
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:60
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Vec2T< Int > Vec2i
Definition: Vec2.h:40
int32_t Int
Definition: Integer.h:35
Vec3T< Int > Vec3i
Definition: agx/Vec3.h:43
double Real
Definition: Real.h:42
LinearProbingHashTable< KeyT, DataT, HashT, AllocatorT > HashTable
Definition: HashTable.h:24