AGX Dynamics  2.37.2.0
agxCollide/HeightField.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. 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 <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 
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 */
Mesh is a common base class for triangle meshes, such as Mesh or HeightField.
Definition: Mesh.h:69
Vec3T< T > min(const Vec3T< T > &lhs, const Vec3T< T > &rhs)
Definition: Vec3Template.h:861
The agxIO namespace contains classes for reading, writing and finding files.
Definition: Material.h:41
Base class for a shape.
Definition: Shape.h:53
This namespace consists of a set of classes for handling geometric intersection tests including boole...
#define AGXPHYSICS_EXPORT
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
Class for data sharing only to be used by HeightField.
Definition: MeshData.h:141
#define AGX_FORCE_INLINE
Definition: Object.h:59
Vec2T< Real > Vec2
The object holding 2 dimensional vectors and providing basic arithmetic.
Definition: Vec2.h:35
Class for more intuitive access to the Mesh&#39;s mesh data.
Definition: Mesh.h:78
#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
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)
double Real
Definition: Real.h:42
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
Class to represent Images.
Definition: Image.h:32
A HeightField is a collision shape that can be created from a grid structure, like an image...
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
HeightFieldMeshDataRef m_heightFieldData