AGXDynamics  2.31.1.0
AvalancheController.h
Go to the documentation of this file.
1 
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:
43  AvalancheController(Terrain* terrain);
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 
64  agx::Real getAvalancheDecayPrecent() const;
65 
66  void setAvalancheMaxHeightGrowth(agx::Real maxHeightGrowth);
67 
68  agx::Real getAvalancheMaxHeightGrowth() const;
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)
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 }
AGXTERRAIN_EXPORT void clear()
#define AGXTERRAIN_STORE_RESTORE_INTERFACE
A terrain model based a 3D grid model with overlapping height field that can be deformed by interacti...
Copyright 2007 - 2018.
Definition: Geometry.h:56
Vec2T< Int > Vec2i
Definition: Vec2.h:40
STL namespace.
int32_t Int
Definition: Integer.h:35
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:248
#define AGX_FORCE_INLINE
Definition: Object.h:63
Base class providing referencing counted objects.
Definition: Referenced.h:113
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
Namespace agx contains the dynamics/math part of the AGX Dynamics API.
Vec3T< Int > Vec3i
Definition: Vec3.h:43
const TerrainIndexSet & getModifiedVertices() const
#define agxAssert(expr)
Definition: debug.h:143
#define AGXTERRAIN_EXPORT
const TerrainIndexSet & getAvalanchedVertices() const
double Real
Definition: Real.h:42
Inheritance with partial specialization due to bug with ref_ptr containers.
LinearProbingHashTable< KeyT, DataT, HashT, AllocatorT > HashTable
Definition: HashTable.h:24
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:102
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:101