AGX Dynamics  2.35.0.1
agxCollide/HeightField.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGXCOLLIDE_HEIGHTFIELD_H
18 #define AGXCOLLIDE_HEIGHTFIELD_H
19 
20 #include <agx/agxPhysics_export.h>
21 
22 #include <agx/Vec2.h>
23 #include <agx/agx_vector_types.h>
24 
25 #include <agxCollide/Mesh.h>
26 #include <agxStream/Serializable.h>
27 #include <external/GIMPACT/AabbTree.h>
28 
29 #include <agxData/LocalVector.h>
30 
31 namespace agxIO
32 {
33  class Image;
34 }
35 
36 namespace 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 
416  {
417  return m_localBound.max().z();
418  }
419 
420 
422  {
423  return m_heightFieldData->m_resolutionX;
424  }
425 
426 
428  {
429  return m_heightFieldData->m_resolutionY;
430  }
431 
432 
434  {
435  return m_collisionMeshData->m_vertices[x + y * m_heightFieldData->m_resolutionX][2];
436  }
437 
438 
440  {
441  return m_heightFieldData->m_size;
442  }
443 
444 
446  {
447  return m_heightFieldData->m_scale;
448  }
449 
450 
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 
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 
482  {
483  return m_heightFieldData->m_minAllowedHeight;
484  }
485 
486 
488  {
489  return m_heightFieldData->m_dynamic;
490  }
491 
492 
494  {
495  m_heightFieldData->m_dynamic = dynamic;
496  calculateMassProperties();
497  }
498 
499 
501  {
502  return m_heightFieldData;
503  }
504 
505 
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 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.
agx::Vec2 getScale() const
Returns the scaling factor for a single cell in the HeightField (in dimension x and y).
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.
void setDynamic(bool dynamic)
Sets if the HeightField is dynamic, which means if it should keep its MassProperties updated.
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.
size_t getTriangleIndex(size_t indexX, size_t indexY, uint_fast8_t upper) const
Computes the global triangle index within the HeightField.
void setMinAllowedHeight(agx::Real minAllowedHeight)
Sets the new minAllowedHeight, which is at the same time the lower bound for a dynamic HeightField (f...
agx::Vec2 projectPointToGrid(const agx::Vec3 &point) const
Projects a point in local HeightField coordinate system to the HeightField grid (which is moved to th...
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 ...
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))
agx::Real getHeight(size_t x, size_t y) const
Gets the height at a grid point.
const HeightFieldMeshData * getHeightFieldMeshData() const
agx::Real getMinAllowedHeight() const
Returns the minimum allowed height, which is at the same time the lower bound for a dynamic HeightFie...
virtual Shape * clone() const override
Create a clone.
bool getDynamic() const
Return if the HeightField is dynamic, which means if it should keep its MassProperties updated.
agx::Vec3 getVertexFromGrid(size_t xIndex, size_t yIndex) const
Returns the vertex at a grid position.
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.
agx::Real getMaxHeight() const
Returns the (global) maximum height of the HeightField.
void calculateVertexCoordinates(size_t vertexIndex, size_t &vertexIndexX, size_t &vertexIndexY) const
Calculates the vertex indices for x and y given a global vertex index.
HeightField * deepCopy() const
Creates a new HeightField which copies data from this one.
void calculateMassProperties()
Calculates "mass properties" such as volume, unscaled inertia tensor and center.
size_t getResolutionX() const
Returns the resolution (the number of height values) in dimension x.
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 * shallowCopy() const
Creates a new HeightField which shares data with this one.
agx::Vec2 getSize() const
Returns the size of the HeightField (in dimension x and y).
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.
size_t getResolutionY() const
Returns the resolution (the number of height values) in dimension y.
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))
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
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...
AGX_DECLARE_POINTER_TYPES(SpaceListener)
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.
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: agx/Vec3.h:36
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
Definition: Vec2.h:35
double Real
Definition: Real.h:42
Vec2T< UInt > Vec2u
Definition: Vec2.h:47
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
VectorPOD< Real > RealVector