AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
agxCollide/HeightField.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. 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>
30
31namespace agxIO
32{
33 class Image;
34}
35
36namespace agxCollide
37{
39
52 {
53 public:
55 agx::Real sizeX, agx::Real sizeY,
56 agx::Real low, agx::Real high,
57 agx::Real bottomMargin = agx::Real(1));
58
69 agx::Real sizeX, agx::Real sizeY,
70 agx::Real low, agx::Real high,
71 agx::Real bottomMargin = agx::Real(1));
72
73 public:
74
86 HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX,
87 agx::Real sizeY, agx::Real bottomMargin = agx::Real(1));
88
103 HeightField(size_t resolutionX, size_t resolutionY, agx::Real sizeX,
104 agx::Real sizeY, const agx::RealVector& heights, bool flipY = false, agx::Real bottomMargin = agx::Real(1));
105
112 void setHeights(const agx::RealVector& heights, bool flipY = false);
113
122 void setHeightsFromList(const agx::Vec2uVector& indices, const agx::RealVector& heights);
123
145 bool setHeights(const agx::RealVector& heights,
146 size_t minVertexX, size_t maxVertexX,
147 size_t minVertexY, size_t maxVertexY);
148
167 void setHeight(size_t xIndex, size_t yIndex, agx::Real height);
168
175 agx::Real getHeight(size_t x, size_t y) const;
176
178 size_t getResolutionX() const;
179
181 size_t getResolutionY() const;
182
188
197 agx::Vec2 projectPointToGrid(const agx::Vec3& point) const;
198
200 agx::Real getMinHeight() const;
201
203 agx::Real getMaxHeight() const;
204
206 agx::Vec2 getSize() const;
207
209 agx::Vec2 getScale() const;
210
212 agx::Vec3 getVertexFromGrid(size_t xIndex, size_t yIndex) const;
213
226 void setMinAllowedHeight(agx::Real minAllowedHeight);
227
232 agx::Real getMinAllowedHeight() const;
233
238 void setDynamic(bool dynamic);
239
244 bool getDynamic() const;
245
257 const BoundingAABB& volume,
258 const agx::AffineMatrix4x4& meshToWorld,
259 const agx::AffineMatrix4x4& volumeToWorld,
260 agx::Vec2i& gridMin,
261 agx::Vec2i& gridMax) const;
262
271 const agx::Vec3& segmentStartLocal,
272 const agx::Vec3& segmentEndLocal,
273 agx::UInt32Vector& triangleIndices) const override;
274
275
284 const agx::Vec3& segmentStartLocal,
285 const agx::Vec3& segmentEndLocal,
286 agxData::LocalVector<agx::UInt32>& triangleIndices) const override;
287
294 void calculateVertexCoordinates(
295 size_t vertexIndex,
296 size_t& vertexIndexX, size_t& vertexIndexY) const;
297
304 size_t getTriangleIndex(
305 size_t indexX, size_t indexY,
306 uint_fast8_t upper) const;
307
315
324
332
336 const HeightFieldMeshData* getHeightFieldMeshData() const;
337
338 virtual Shape* clone() const override;
339
341
344
345 protected:
346
348
349 // Update the bounding volume hierarchy.
350 virtual void updateBvhTree() override;
351
358 void updateTriangle(size_t triangleIndex, bool shouldUpdateBvhNode = true);
359
360 void addQuadrantVolume(int xIndex, int yIndex, agx::Real factor);
361
362 void addTriangleVolume(const agx::Vec3& v0, const agx::Vec3& v1,
363 const agx::Vec3& v2, agx::Real factor);
364
365
366 // Builds a partial tree of the bounding volume hierarchy.
367 void buildSubTree(
368 agx::Vec2u start,
369 agx::Vec2u end,
370 int& nextNodeIndex,
371 int parentIndex );
372
379 void finalize(bool updateMassProperties, const agx::RealVector& heights, bool flipY = false);
380
387 void setHeights(bool shouldUpdateMassProperties, const agx::RealVector& heights, bool flipY = false);
388
389 // Hiding default constructor.
390 HeightField();
391
392 // hiding assignment operator
393 HeightField& operator=(const HeightField&);
394
396
397 // hiding destructor
398 virtual ~HeightField();
399
400
401 protected:
402 HeightFieldMeshDataRef m_heightFieldData;
403 };
404
405
406
407 /* IMPLEMENTATIONS */
408
409 AGX_FORCE_INLINE agx::Real HeightField::getMinHeight() const
410 {
411 return m_localBound.min().z() + m_collisionMeshData->m_bottomMargin;
412 }
413
414
415 AGX_FORCE_INLINE agx::Real HeightField::getMaxHeight() const
416 {
417 return m_localBound.max().z();
418 }
419
420
421 AGX_FORCE_INLINE size_t HeightField::getResolutionX() const
422 {
423 return m_heightFieldData->m_resolutionX;
424 }
425
426
427 AGX_FORCE_INLINE size_t HeightField::getResolutionY() const
428 {
429 return m_heightFieldData->m_resolutionY;
430 }
431
432
433 AGX_FORCE_INLINE agx::Real HeightField::getHeight(size_t x, size_t y) const
434 {
435 return m_collisionMeshData->m_vertices[x + y * m_heightFieldData->m_resolutionX][2];
436 }
437
438
439 AGX_FORCE_INLINE agx::Vec2 HeightField::getSize() const
440 {
441 return m_heightFieldData->m_size;
442 }
443
444
445 AGX_FORCE_INLINE agx::Vec2 HeightField::getScale() const
446 {
447 return m_heightFieldData->m_scale;
448 }
449
450
451 AGX_FORCE_INLINE void HeightField::calculateVertexCoordinates( size_t vertexIndex,
452 size_t& vertexIndexX, size_t& vertexIndexY) const
453 {
454 vertexIndexX = vertexIndex % m_heightFieldData->m_resolutionX;
455 vertexIndexY = vertexIndex / m_heightFieldData->m_resolutionX;
456 }
457
458
459 AGX_FORCE_INLINE size_t HeightField::getTriangleIndex(size_t indexX, size_t indexY, uint_fast8_t upper) const
460 {
461 return 2 * (indexX + indexY * (m_heightFieldData->m_resolutionX - 1)) + upper;
462 }
463
464
465
466 AGX_FORCE_INLINE agx::Vec3 HeightField::getVertexFromGrid(size_t xIndex, size_t yIndex) const
467 {
468 return getVertex(yIndex * m_heightFieldData->m_resolutionX + xIndex);
469 }
470
471
472 AGX_FORCE_INLINE void HeightField::setMinAllowedHeight( agx::Real minAllowedHeight )
473 {
474 // Get the lowest value z from bounding box.
475 m_heightFieldData->m_minAllowedHeight = std::min( minAllowedHeight, m_localBound.min().z() + m_collisionMeshData->m_bottomMargin );
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}
514
515
516#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...
#define AGXPHYSICS_EXPORT
Axis aligned bounding box implementation.
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:54
Only use as member allocated variable.
Definition Value.h:285
Class to represent Images.
Definition Image.h:33
#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
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:42