AGX Dynamics 2.42.2.1
Loading...
Searching...
No Matches
TerrainDataAtlas.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
9having been advised so by Algoryx Simulation AB for a time limited evaluation,
10or having purchased a valid 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 <agxTerrain/Grid.h>
21#include <agx/StackArray.h>
23
24namespace agxTerrain
25{
27
28 typedef agx::HashTable<agx::Vec3i, agx::Vec3f> VoxelToVelocityTable;
29 typedef agx::HashTable<agx::Vec3i, agx::UInt32> VoxelToTerrainMaterialTable;
30 typedef agx::Vector<TerrainMaterialRef> TerrainMaterialRefVector;
31
32 struct MaterialPair {
33 TerrainMaterialRef terrainMaterial;
34 agx::MaterialRef material;
35
37 : terrainMaterial( nullptr )
38 , material( nullptr ) {}
39
40 MaterialPair(TerrainMaterial* terrainMaterial, agx::Material* material)
41 : terrainMaterial(terrainMaterial)
42 , material(material)
43 {}
44 };
45 typedef agx::Vector<agxTerrain::MaterialPair> MaterialPairVector;
46
47 AGX_DECLARE_POINTER_TYPES(TerrainDataAtlas);
48
49 /*
50 Atlas for holding information about terrain mass and material in the grid system.
51 Is active in the same index space as the regular grid.
52 */
53 class AGXTERRAIN_EXPORT TerrainDataAtlas : public agx::Referenced
54 {
55 static constexpr float MAX_MASS = 1.0;
56
57 public:
58 enum MassType
59 {
60 SOLID,
62 FLUID
63 };
64 struct MassTypeHash
65 {
66 AGX_FORCE_INLINE agx::UInt32 operator()(TerrainDataAtlas::MassType key) const
67 {
68 return agx::hash((agx::UInt32)key);
69 }
70 };
71
72 public:
80 TerrainDataAtlas(agx::Real voxelSize, size_t resolutionX, size_t resolutionY, agx::Real lowestminimumHeight);
81
82
89 float getOccupancy(const Grid::GridCoord& coord) const;
90
91
92 /*
93 Returns the mass [kg] in the voxel at the given coordinates.
94 This is calculated as:
95 mass = base_density * compaction * voxelVolume * occupancy == density * filledVolume
96 */
97 agx::Real getMass(const Grid::GridCoord& coord) const;
98
99
111 float addOccupancy(const agx::Vec2i& ti, float occupancy);
112
113
126 float addOccupancy(const Grid::GridCoord& coord, float occupancy);
127
128
139 float removeOccupancy(const agx::Vec2i& ti, float occupancy, agx::Real& removedMass);
140
141
153 float removeOccupancy(const Grid::GridCoord& coord, float occupancy, agx::Real& removedMass);
154
155
171 bool addMass(const agx::Vec2i& ti, agx::Real mass, float compaction);
172
173
189 bool addMass(const Grid::GridCoord& coord, agx::Real mass, float compaction);
190
192
199 float getMassOccupancy(const Grid::GridCoord& coord, MassType massType) const;
200
208 void setMassOccupancy(const Grid::GridCoord& coord, MassType massType, float value);
209
218 float addMassOccupancy(const Grid::GridCoord& coord, MassType massType, float value);
219
226 void setCompaction(const Grid::GridCoord& coord, float value);
227
233 float getCompaction(const Grid::GridCoord& coord) const;
234
242 float getCompactedSolidMassOccupancy(const Grid::GridCoord& coord) const;
243
249 float getMaximumAllowedSolidMassOccupancyInVoxel(const Grid::GridCoord& coord) const;
250
256 bool isActiveVoxel(const Grid::GridCoord& coord, MassType massType) const;
257
263 agx::Vec3iVector getIndicesWithMassOccupancy(MassType massType) const;
264
265
266 float getTotalMassOccupancyInVoxel(const Grid::GridCoord& coord) const;
267
268 void setVelocity(const Grid::GridCoord& coord, const agx::Vec3f& value);
269 agx::Vec3f getVelocity(const Grid::GridCoord& coord) const;
270 void clearVelocityGrid();
271
272 bool addNewTerrainMaterial(TerrainMaterial* terrainMaterial);
273 void setDefaultTerrainMaterial(TerrainMaterial* defaultTerrainMaterial, agx::Material* defaultMaterial = nullptr);
274 TerrainMaterial* getDefaulTerrainMaterial() const;
275 int setTerrainMaterialInVoxels(const Grid::GridCoordVector& coords, TerrainMaterial* terrainMaterial);
276 bool setTerrainMaterialInVoxel(const Grid::GridCoord& coord, TerrainMaterial* terrainMaterial);
277 bool setTerrainMaterialInVoxel(const Grid::GridCoord& coord, const agx::UInt32 terrainMaterialIndex);
278
279 /*
280 \return the index in the MaterialPair vector if it exists, otherwise agx::InvalidIndex.
281 */
282 agx::UInt32 getTerrainMaterialPairIndex(TerrainMaterial* terrainMaterial) const;
283
284 /*
285 \return whether the given index corresponds to any added terrain materials
286 */
287 bool terrainMaterialIndexIsValid(const agx::UInt32& terrainMaterialIndex) const;
288
289 TerrainMaterial* getTerrainMaterialInVoxel(const Grid::GridCoord& coord) const;
290 TerrainMaterial* getTerrainMaterialByIndex(const agx::UInt32 terrainMaterialIndex) const;
291 agx::Vector<TerrainMaterial*> getTerrainMaterials();
292
293 void clearTerrainMaterials();
294
298 void setTerrainMaterialIndexInVoxelNoChecks(const Grid::GridCoord& coord, const agx::UInt32 terrainMaterialIndex);
299
300
301 bool exchangeTerrainMaterial(TerrainMaterial* oldTerrainMaterial, TerrainMaterial* newTerrainMaterial);
302 bool removeTerrainMaterial(TerrainMaterial* terrainMaterialToRemove);
303 bool associateMaterialToTerrainMaterial(agx::Material* material, TerrainMaterial* terrainMaterial);
304
305 void setDefaultMaterial(agx::Material* defaultMaterial);
306 agx::Material* getDefaultMaterial() const;
307 agx::Material* getMaterial(TerrainMaterial* terrainMaterial) const;
308 agx::Material* getMaterialInVoxel(const Grid::GridCoord& coord) const;
309
310 /*
311 \returns The corresponding index (>= 0) in m_terrainMaterialPairs.
312 */
313 agx::UInt32 getTerrainMaterialIndex(const Grid::GridCoord& coord) const;
314 void clearTerrainMaterialGrid();
315 bool removeSpecificTerrainMaterialFromVoxel(const Grid::GridCoord& coord);
316
317 MaterialPairVector getTerrainMaterialPairs();
318 void setTerrainMaterialPairs(MaterialPairVector materialPairs);
319
320 Grid* getSolidGridMassOccupancyData() const;
321
322 BasicGrid* getGridMassOccupancyData(MassType massType) const;
323 BasicGrid* getCompactionGridData() const;
324
325 size_t getTotalGridMemoryUsage() const;
326
327 size_t getTotalActiveVoxels() const;
328
329 void clearMassOccupancyGrid(MassType massType);
330
331 agx::Real getMassOccupancyInTerrain(MassType massType) const;
332
333 VoxelToVelocityTable& getVelocityTable();
334 VoxelToTerrainMaterialTable& getTerrainMaterialTable();
335
336 bool getEnableMinimumHeights() const;
337
338 void setEnableMinimumHeights(bool enable);
339
340 agx::RealVector getMinimumHeights() const;
341
342 /*
343 \param minimumHeights - row major array for setting minimum heights in the terrain.
344 */
345 void setMinimumHeights(const agx::RealVector& minimumHeights);
346
347 public:
349
350 protected:
354 virtual ~TerrainDataAtlas();
355
356 agx::Real getBaseDensity(const Grid::GridCoord& coord) const;
357 float getBaseDensityFloat(const Grid::GridCoord& coord) const;
358
359 void fillGridVector();
360
361 protected:
362 agx::Real m_voxelSize;
363 agx::Real m_voxelVolume;
364 BasicGridRef m_solidMassOccupancyGrid;
365 BasicGridRef m_fluidMassOccupancyGrid;
366 BasicGridRef m_particleMassOccupancyGrid;
367 BasicGridRef m_compactionGrid;
368
369 VoxelToVelocityTable m_voxelToVelocityTable;
370
371 // \todo Move this to a separate grid!
372 agx::Vector<MaterialPair> m_terrainMaterialPairs;
373 VoxelToTerrainMaterialTable m_voxelToTerrainMaterialTable;
374
376
377 Grid* m_rawSolidMassOccupancyGrid; // alias for m_solidMassGrid
378 };
380}
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXTERRAIN_STORE_RESTORE_INTERFACE
#define AGXTERRAIN_EXPORT
Main material class which acts as a holder of a Surface Material and a Bulk material.
Definition: Material.h:378
Base class providing referencing counted objects.
Definition: Referenced.h:120
Templated stack array class.
Vector containing 'raw' data.
Definition: agx/Vector.h:246
Templated vector class.
Definition: agx/Vector.h:53
#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
agx::SymmetricPair< const agx::Material * > MaterialPair
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:59
agx::ref_ptr< BasicGrid > BasicGridRef
Definition: Grid.h:66
agx::ref_ptr< TerrainMaterial > TerrainMaterialRef
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
UInt32 hash(const T &key)
Definition: HashFunction.h:165
uint32_t UInt32
Definition: Integer.h:32
double Real
Definition: Real.h:41