AGXDynamics  2.35.0.2
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:
54  static agxCollide::HeightField* createFromImage( agxIO::Image* image,
55  agx::Real sizeX, agx::Real sizeY,
56  agx::Real low, agx::Real high,
57  agx::Real bottomMargin = agx::Real(1));
58 
68  static agxCollide::HeightField* createFromFile( const agx::String& filename,
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 
187  Mesh::Triangle getTriangleFromPoint(const agx::Vec3& point) const;
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 
256  bool calculateVertexIndicesTouchedByAABB(
257  const BoundingAABB& volume,
258  const agx::AffineMatrix4x4& meshToWorld,
259  const agx::AffineMatrix4x4& volumeToWorld,
260  agx::Vec2i& gridMin,
261  agx::Vec2i& gridMax) const;
262 
270  virtual void calculatePossibleTriangleOverlapsAlongLineSegment(
271  const agx::Vec3& segmentStartLocal,
272  const agx::Vec3& segmentEndLocal,
273  agx::UInt32Vector& triangleIndices) const override;
274 
275 
283  virtual void calculatePossibleTriangleOverlapsAlongLineSegment(
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 
314  HeightField* shallowCopy() const;
315 
323  HeightField* deepCopy() const;
324 
331  void calculateMassProperties();
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:
403  };
404 
405 
406 
407  /* IMPLEMENTATIONS */
408 
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)
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;
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 */
agxPhysics_export.h
agxCollide::Mesh::getVertex
agx::Vec3 getVertex(size_t globalVertexIndex) const
Definition: Mesh.h:755
agxCollide::HeightField::setDynamic
void setDynamic(bool dynamic)
Sets if the HeightField is dynamic, which means if it should keep its MassProperties updated.
Definition: agxCollide/HeightField.h:493
agx::end
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
Definition: LinearProbingHashSet.h:612
agxCollide::Mesh
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:69
agxCollide::HeightField::calculateVertexCoordinates
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.
Definition: agxCollide/HeightField.h:451
agx::Real
double Real
Definition: Real.h:42
agxCollide::Mesh::m_localBound
BoundingAABB m_localBound
Definition: Mesh.h:635
agxCollide::HeightField::getHeightFieldMeshData
const HeightFieldMeshData * getHeightFieldMeshData() const
Definition: agxCollide/HeightField.h:500
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
agxCollide::HeightField::getSize
agx::Vec2 getSize() const
Returns the size of the HeightField (in dimension x and y).
Definition: agxCollide/HeightField.h:439
agx::AffineMatrix4x4T< Real >
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxCollide::HeightField
A HeightField is a collision shape that can be created from a grid structure, like an image.
Definition: agxCollide/HeightField.h:51
agxCollide::HeightField::setMinAllowedHeight
void setMinAllowedHeight(agx::Real minAllowedHeight)
Sets the new minAllowedHeight, which is at the same time the lower bound for a dynamic HeightField (f...
Definition: agxCollide/HeightField.h:472
agxCollide::BoundingAABB
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
AGXSTREAM_DECLARE_SERIALIZABLE
#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
agx::VectorPOD< Real >
agxCollide::HeightField::calculateMassProperties
void calculateMassProperties()
Calculates "mass properties" such as volume, unscaled inertia tensor and center.
agxCollide::HeightFieldMeshData
Class for data sharing only to be used by HeightField.
Definition: MeshData.h:141
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
agxCollide::HeightField::getTriangleIndex
size_t getTriangleIndex(size_t indexX, size_t indexY, uint_fast8_t upper) const
Computes the global triangle index within the HeightField.
Definition: agxCollide/HeightField.h:459
agxCollide::HeightField::m_heightFieldData
HeightFieldMeshDataRef m_heightFieldData
Definition: agxCollide/HeightField.h:402
agxCollide::HeightField::getVertexFromGrid
agx::Vec3 getVertexFromGrid(size_t xIndex, size_t yIndex) const
Returns the vertex at a grid position.
Definition: agxCollide/HeightField.h:466
Vec2.h
agx::ref_ptr< HeightFieldMeshData >
agxCollide::HeightField::getHeight
agx::Real getHeight(size_t x, size_t y) const
Gets the height at a grid point.
Definition: agxCollide/HeightField.h:433
agxCollide::Shape
Base class for a shape.
Definition: Shape.h:53
agxCollide::HeightField::getMinAllowedHeight
agx::Real getMinAllowedHeight() const
Returns the minimum allowed height, which is at the same time the lower bound for a dynamic HeightFie...
Definition: agxCollide/HeightField.h:481
agxCollide::Mesh::Triangle
Class for more intuitive access to the Mesh's mesh data.
Definition: Mesh.h:78
agxCollide::HeightField::getMinHeight
agx::Real getMinHeight() const
Returns the (global) minimum height of the HeightField.
Definition: agxCollide/HeightField.h:409
agx::min
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:880
agx::BoundT::max
const T & max() const
Definition: Bound.h:255
agxCollide::HeightField::getResolutionY
size_t getResolutionY() const
Returns the resolution (the number of height values) in dimension y.
Definition: agxCollide/HeightField.h:427
agxCollide::HeightField::getDynamic
bool getDynamic() const
Return if the HeightField is dynamic, which means if it should keep its MassProperties updated.
Definition: agxCollide/HeightField.h:487
agx::Vec2T
Definition: Vec2Template.h:30
agx::BoundT::min
const T & min() const
Definition: Bound.h:252
agxIO::Image
Class to represent Images.
Definition: Image.h:32
Serializable.h
agxCollide::HeightField::getScale
agx::Vec2 getScale() const
Returns the scaling factor for a single cell in the HeightField (in dimension x and y).
Definition: agxCollide/HeightField.h:445
Mesh.h
agxCollide
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Definition: ContactForceFilterParticipant.h:22
agxCollide::HeightField::getResolutionX
size_t getResolutionX() const
Returns the resolution (the number of height values) in dimension x.
Definition: agxCollide/HeightField.h:421
agxCollide::Mesh::m_collisionMeshData
CollisionMeshDataRef m_collisionMeshData
Definition: Mesh.h:633
agxCollide::AGX_DECLARE_POINTER_TYPES
AGX_DECLARE_POINTER_TYPES(SpaceListener)
agx::AgXString< std::string >
agxCollide::HeightField::projectPointToGrid
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...
Definition: agxCollide/HeightField.h:506
agxCollide::HeightField::getMaxHeight
agx::Real getMaxHeight() const
Returns the (global) maximum height of the HeightField.
Definition: agxCollide/HeightField.h:415
agx::Vec3T< Real >
agx::Vec2
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
Definition: Vec2.h:35
LocalVector.h
agx::Vec3T::z
T & z()
Definition: Vec3Template.h:511
agxIO
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:41
agx_vector_types.h
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agxData::LocalVector< agx::UInt32 >