AGX Dynamics 2.42.2.1
Loading...
Searching...
No Matches
AvalancheController.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB.You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#pragma once
18
19#include <agxTerrain/export.h>
20#include <agx/Referenced.h>
22#include <agx/StackArray.h>
23#include <agx/HashVector.h>
24
25namespace 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 setAvalancheDecayFraction(agx::Real avalancheDecayFraction);
64
65 agx::Real getAvalancheDecayFraction() 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 modify new surface heights.
87 */
88 typedef agx::HashTable<agx::Vec2i, agx::Real> SurfaceHeightTable;
89 SurfaceHeightTable m_surfaceHeights;
90
91 void applyGrowthMap();
92
93 agx::Real findReposeHeightDifference(const agx::Vec2i& v1, const agx::Vec2i& v2);
94
95 void generateVertexNeighborList(const agx::Vec2i& surfaceVertex, agx::StackArray<agx::Vec2i, 8>& neighborList );
96
97 agx::Real calculateOccupancyTransfer(const agx::Vec2i& surfaceVertex, agx::Real heightDifference);
98
99 agx::Real calculateMaximumOccupancyTransfer(const agx::Vec2i& surfaceVertex);
100
101 private:
102 class AvalancheIterationMap
103 {
104 public:
105 AvalancheIterationMap(agx::Int kmin, agx::Int kmax)
106 : m_kmin(kmin)
107 , m_kmax(kmax)
108
109 {
110 agxAssert(kmin <= kmax);
111 if (kmin <= kmax) {
112 m_avalancheIterationVector.resize((kmax - kmin + 1), agx::HashVector<agx::Vec2i>());
113 }
114 }
115
116 const agx::HashVector<agx::Vec2i>& operator[](agx::Int k)
117 {
118 resizeFromK(k);
119 return m_avalancheIterationVector[toIndex(k)];
120 }
121
122 void push_back(agx::Int k, const agx::Vec2i& index)
123 {
124 resizeFromK(k);
125 size_t vectorIndex = toIndex(k);
126 if (!m_avalancheIterationVector[vectorIndex].contains(index))
127 {
128 m_avalancheIterationVector[vectorIndex].push_back(index,index);
129 }
130 }
131
132 bool contains(agx::Int k, const agx::Vec2i& index)
133 {
134 size_t vectorIndex = toIndex(k);
135 if (vectorIndex > m_avalancheIterationVector.size() - 1)
136 return false;
137
138 return m_avalancheIterationVector[vectorIndex].contains(index);
139 }
140
141 size_t toIndex(agx::Int k)
142 {
143 return m_kmax - k;
144 }
145
146 size_t toK(agx::Int i)
147 {
148 return m_kmax - i;
149 }
150
151 void resizeFromK(agx::Int k)
152 {
153 size_t vectorIndex = toIndex(k);
154 if (vectorIndex > m_avalancheIterationVector.size() - 1)
155 {
156 m_avalancheIterationVector.resize(vectorIndex + 1);
157 m_kmin = k;
158 }
159 }
160
161 agx::Int kmin()
162 {
163 return m_kmin;
164 }
165
166 agx::Int kmax()
167 {
168 return m_kmax;
169 }
170
171 void clear()
172 {
173 m_avalancheIterationVector.clear();
174 }
175
176 size_t size()
177 {
178 return m_avalancheIterationVector.size();
179 }
180
181 private:
182 agx::Vector<agx::HashVector<agx::Vec2i>> m_avalancheIterationVector;
183 agx::Int m_kmin;
184 agx::Int m_kmax;
185 };
186
187 private:
188 Terrain* m_terrain;
189 TerrainIndexSet m_forbiddenTerrainVertices;
190 TerrainIndexSet m_avalanchedTerrainVertices;
191 TerrainIndexSet m_indicesToAvalanche;
192 TerrainIndexSet m_modifiedTerrainVertices;
193 agx::Vec3iVector m_touchedTerrainIndices;
195 agx::Real m_avalancheDecayFraction;
196 agx::Real m_maxHeightGrowth;
197 agx::Vec2uVector m_hfUpdateIndices;
198 agx::RealVector m_hfUpdateHeights;
199 };
200
201 AGX_FORCE_INLINE const AvalancheController::TerrainIndexSet& AvalancheController::getModifiedVertices() const
202 {
203 return m_modifiedTerrainVertices;
204 }
205
206 AGX_FORCE_INLINE const AvalancheController::TerrainIndexSet& AvalancheController::getAvalanchedVertices() const
207 {
208 return m_avalanchedTerrainVertices;
209 }
210
211 AGX_FORCE_INLINE const agx::Vec3iVector AvalancheController::getTouchedIndices() const
212 {
213 return m_touchedTerrainIndices;
214 }
215
217}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXTERRAIN_STORE_RESTORE_INTERFACE
#define AGXTERRAIN_EXPORT
Inheritance with partial specialization due to bug with ref_ptr containers.
Definition: agx/HashSet.h:670
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.
Templated stack array class.
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:89
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:88
#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:59
agx::HashSet< agx::Vec2i > TerrainIndexSet
Definition: Terrain.h:69
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real
Definition: Real.h:41
int64_t Int
Definition: Integer.h:28