AGX Dynamics 2.42.2.1
Loading...
Searching...
No Matches
agxCollide/HeightField.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#ifndef AGXCOLLIDE_HEIGHTFIELD_H
18#define AGXCOLLIDE_HEIGHTFIELD_H
19
21
22#include <agx/Vec2.h>
24
25#include <agxCollide/Mesh.h>
27#include <GIMPACT/AabbTree.h>
28
29#include <agxData/LocalVector.h>
31
32namespace agxIO
33{
34 class Image;
35}
36
37namespace agxCollide
38{
40
53 {
54 public:
56 agx::Real sizeX, agx::Real sizeY,
57 agx::Real low, agx::Real high,
58 agx::Real bottomMargin = agx::Real(1));
59
70 agx::Real sizeX, agx::Real sizeY,
71 agx::Real low, agx::Real high,
72 agx::Real bottomMargin = agx::Real(1));
73
74 public:
75
87 HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX,
88 agx::Real sizeY, agx::Real bottomMargin = agx::Real(1));
89
104 HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX,
105 agx::Real sizeY, const agx::RealVector& heights, bool flipY = false, agx::Real bottomMargin = agx::Real(1));
106
113 void setHeights(const agx::RealVector& heights, bool flipY = false);
114
123 void setHeightsFromList(const agx::Vec2uVector& indices, const agx::RealVector& heights);
124
146 bool setHeights(const agx::RealVector& heights,
147 size_t minVertexX, size_t maxVertexX,
148 size_t minVertexY, size_t maxVertexY);
149
168 void setHeight(size_t xIndex, size_t yIndex, agx::Real height);
169
176 agx::Real getHeight(size_t x, size_t y) const;
177
179 size_t getResolutionX() const;
180
182 size_t getResolutionY() const;
183
189
198 agx::Vec2 projectPointToGrid(const agx::Vec3& point) const;
199
201 agx::Real getMinHeight() const;
202
204 agx::Real getMaxHeight() const;
205
207 agx::Vec2 getSize() const;
208
210 agx::Vec2 getScale() const;
211
213 agx::Vec3 getVertexFromGrid(size_t xIndex, size_t yIndex) const;
214
225 void setMinAllowedHeight(agx::Real minAllowedHeight);
226
231 agx::Real getMinAllowedHeight() const;
232
237 void setDynamic(bool dynamic);
238
243 bool getDynamic() const;
244
256 const BoundingAABB& volume,
257 const agx::AffineMatrix4x4& meshToWorld,
258 const agx::AffineMatrix4x4& volumeToWorld,
259 agx::Vec2i& gridMin,
260 agx::Vec2i& gridMax) const;
261
270 const agx::Vec3& segmentStartLocal,
271 const agx::Vec3& segmentEndLocal,
272 agx::UInt32Vector& triangleIndices) const override;
273
274
283 const agx::Vec3& segmentStartLocal,
284 const agx::Vec3& segmentEndLocal,
285 agxData::LocalVector<agx::UInt32>& triangleIndices) const override;
286
293 void calculateVertexCoordinates(
294 size_t vertexIndex,
295 size_t& vertexIndexX, size_t& vertexIndexY) const;
296
303 size_t getTriangleIndex(
304 size_t indexX, size_t indexY,
305 uint_fast8_t upper) const;
306
314
323
331
335 const HeightFieldMeshData* getHeightFieldMeshData() const;
336
337 virtual Shape* clone() const override;
338
340
342
345
346 protected:
347
349
350 // Update the bounding volume hierarchy.
351 virtual void updateBvhTree() override;
352
359 void updateTriangle(size_t triangleIndex, bool shouldUpdateBvhNode = true);
360
361 void addQuadrantVolume(int xIndex, int yIndex, agx::Real factor);
362
363 void addTriangleVolume(const agx::Vec3& v0, const agx::Vec3& v1,
364 const agx::Vec3& v2, agx::Real factor);
365
366
367 // Builds a partial tree of the bounding volume hierarchy.
368 void buildSubTree(
369 agx::Vec2u start,
370 agx::Vec2u end,
371 int& nextNodeIndex,
372 int parentIndex );
373
380 void finalize(bool updateMassProperties, const agx::RealVector& heights, bool flipY = false);
381
388 void setHeights(bool shouldUpdateMassProperties, const agx::RealVector& heights, bool flipY = false);
389
390 // Hiding default constructor.
391 HeightField();
392
393 // hiding assignment operator
394 HeightField& operator=(const HeightField&);
395
397
398 // hiding destructor
399 virtual ~HeightField();
400
401
402 protected:
403 HeightFieldMeshDataRef m_heightFieldData;
404 };
405
406
407
408 /* IMPLEMENTATIONS */
409
410 AGX_FORCE_INLINE agx::Real HeightField::getMinHeight() const
411 {
412 return m_localBound.min().z() + m_collisionMeshData->m_bottomMargin;
413 }
414
415
416 AGX_FORCE_INLINE agx::Real HeightField::getMaxHeight() const
417 {
418 return m_localBound.max().z();
419 }
420
421
422 AGX_FORCE_INLINE size_t HeightField::getResolutionX() const
423 {
424 return m_heightFieldData->m_resolutionX;
425 }
426
427
428 AGX_FORCE_INLINE size_t HeightField::getResolutionY() const
429 {
430 return m_heightFieldData->m_resolutionY;
431 }
432
433
434 AGX_FORCE_INLINE agx::Real HeightField::getHeight(size_t x, size_t y) const
435 {
436 return m_collisionMeshData->m_vertices[x + y * m_heightFieldData->m_resolutionX][2];
437 }
438
439
440 AGX_FORCE_INLINE agx::Vec2 HeightField::getSize() const
441 {
442 return m_heightFieldData->m_size;
443 }
444
445
446 AGX_FORCE_INLINE agx::Vec2 HeightField::getScale() const
447 {
448 return m_heightFieldData->m_scale;
449 }
450
451
452 AGX_FORCE_INLINE void HeightField::calculateVertexCoordinates( size_t vertexIndex,
453 size_t& vertexIndexX, size_t& vertexIndexY) const
454 {
455 vertexIndexX = vertexIndex % m_heightFieldData->m_resolutionX;
456 vertexIndexY = vertexIndex / m_heightFieldData->m_resolutionX;
457 }
458
459
460 AGX_FORCE_INLINE size_t HeightField::getTriangleIndex(size_t indexX, size_t indexY, uint_fast8_t upper) const
461 {
462 return 2 * (indexX + indexY * (m_heightFieldData->m_resolutionX - 1)) + upper;
463 }
464
465
466
467 AGX_FORCE_INLINE agx::Vec3 HeightField::getVertexFromGrid(size_t xIndex, size_t yIndex) const
468 {
469 return getVertex(yIndex * m_heightFieldData->m_resolutionX + xIndex);
470 }
471
472
473 AGX_FORCE_INLINE void HeightField::setMinAllowedHeight( agx::Real minAllowedHeight )
474 {
475 m_heightFieldData->m_minAllowedHeight = minAllowedHeight;
476 if (m_heightFieldData->m_dynamic)
477 calculateMassProperties();
478 }
479
480
481 AGX_FORCE_INLINE agx::Real HeightField::getMinAllowedHeight() const
482 {
483 return m_heightFieldData->m_minAllowedHeight;
484 }
485
486
487 AGX_FORCE_INLINE bool HeightField::getDynamic() const
488 {
489 return m_heightFieldData->m_dynamic;
490 }
491
492
493 AGX_FORCE_INLINE void HeightField::setDynamic( bool dynamic )
494 {
495 m_heightFieldData->m_dynamic = dynamic;
496 calculateMassProperties();
497 }
498
499
500 AGX_FORCE_INLINE const HeightFieldMeshData* HeightField::getHeightFieldMeshData() const
501 {
502 return m_heightFieldData;
503 }
504
505
506 AGX_FORCE_INLINE agx::Vec2 HeightField::projectPointToGrid(const agx::Vec3& point) const
507 {
508 const agx::Vec2 projectedPoint = agx::Vec2(point[0], point[1]) + m_heightFieldData->m_size * agx::Real(0.5);
509 const agx::Vec2 pointInGrid(projectedPoint[0] * m_heightFieldData->m_invScale[0], projectedPoint[1] * m_heightFieldData->m_invScale[1]);
510 return pointInGrid;
511 }
512
513 inline agx::Physics::Geometry::HeightFieldPtr HeightField::getEntity() const
514 {
515 return m_entity;
516 }
517}
518
519
520#endif /* AGXCOLLIDE_HEIGHTFIELD_H */
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGXSTREAM_DECLARE_SERIALIZABLE(T)
Use this in a Serializable class to add the required methods Important: Use full namespace in the dec...
Definition: Serializable.h:207
#define AGXPHYSICS_EXPORT
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:38
Class for data sharing only to be used by HeightField.
Definition: MeshData.h:142
A HeightField is a collision shape that can be created from a grid structure, like an image.
bool calculateVertexIndicesTouchedByAABB(const BoundingAABB &volume, const agx::AffineMatrix4x4 &meshToWorld, const agx::AffineMatrix4x4 &volumeToWorld, agx::Vec2i &gridMin, agx::Vec2i &gridMax) const
Calculates the minimum and maximum vertex indices of the square that a bounding box resides in.
bool setHeights(const agx::RealVector &heights, size_t minVertexX, size_t maxVertexX, size_t minVertexY, size_t maxVertexY)
Set height values of all grid nodes within a rectangle.
HeightField * shallowCopy() const
Creates a new HeightField which shares data with this one.
void setHeightsFromList(const agx::Vec2uVector &indices, const agx::RealVector &heights)
Set height values given a list of terrain indices and corresponding heights.
virtual void calculatePossibleTriangleOverlapsAlongLineSegment(const agx::Vec3 &segmentStartLocal, const agx::Vec3 &segmentEndLocal, agxData::LocalVector< agx::UInt32 > &triangleIndices) const override
Calculates an array of triangle indices of the triangles whose bounding volumes get intersected by a ...
HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX, agx::Real sizeY, agx::Real bottomMargin=agx::Real(1))
Constructor for the HeightField class.
virtual void calculatePossibleTriangleOverlapsAlongLineSegment(const agx::Vec3 &segmentStartLocal, const agx::Vec3 &segmentEndLocal, agx::UInt32Vector &triangleIndices) const override
Calculates an array of triangle indices of the triangles whose bounding volumes get intersected by a ...
void setHeights(const agx::RealVector &heights, bool flipY=false)
Set height values of all sampled points.
virtual Shape * clone() const override
Create a clone.
static agxCollide::HeightField * createFromFile(const agx::String &filename, agx::Real sizeX, agx::Real sizeY, agx::Real low, agx::Real high, agx::Real bottomMargin=agx::Real(1))
static agxCollide::HeightField * createFromImage(agxIO::Image *image, agx::Real sizeX, agx::Real sizeY, agx::Real low, agx::Real high, agx::Real bottomMargin=agx::Real(1))
void calculateMassProperties()
Calculates "mass properties" such as volume, unscaled inertia tensor and center.
Mesh::Triangle getTriangleFromPoint(const agx::Vec3 &point) const
Returns a triangle given the 2D-projection of a point on the xy-plane in the grid.
void setHeight(size_t xIndex, size_t yIndex, agx::Real height)
Sets the height at a specific grid node.
HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX, agx::Real sizeY, const agx::RealVector &heights, bool flipY=false, agx::Real bottomMargin=agx::Real(1))
Constructor for the HeightField class.
HeightField * deepCopy() const
Creates a new HeightField which copies data from this one.
Class for more intuitive access to the Mesh's mesh data.
Definition: Mesh.h:79
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:70
Base class for a shape.
Definition: Shape.h:58
Local scope vector.
Definition: LocalVector.h:44
Class to represent Images.
Definition: Image.h:33
Pointer to a entity instance of type Physics.Geometry.HeightField.
#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
This namespace consists of a set of classes for handling geometric intersection tests including boole...
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:42
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
Definition: Vec2.h:35
double Real
Definition: Real.h:41