AGX Dynamics  2.37.2.0
Shape.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_SHAPE_H
18 #define AGXCOLLIDE_SHAPE_H
19 
20 
21 #include <agx/agxPhysics_export.h>
22 
23 #include <agx/Vec3.h>
24 #include <agx/SPDMatrix3x3.h>
25 #include <agx/AffineMatrix4x4.h>
26 
27 #include <agxCollide/agxCollide.h>
29 #include <agxStream/Serializable.h>
30 
31 #include <agxCollide/RenderData.h>
32 
35 
36 namespace agxTerrain
37 {
38  class Terrain;
39 }
40 
41 namespace agxCollide
42 {
43  class Geometry;
44  class Space;
45 
49 
54  {
55  public:
56 
58  enum Type {
60  BOX,
73  NUM_TYPES
74  };
75 
77  static const char* getTypeName(agx::UInt8 type);
78 
79  public:
80 
84  virtual Shape *clone() const;
85 
89  const char* getTypeName() const;
90 
94  agx::UInt8 getType() const;
95 
99  bool isGroup() const;
100 
104  ShapeGroup* asGroup();
105 
109  const ShapeGroup* asGroup() const;
110 
114  const BoundingAABB& getBoundingVolume() const;
115 
120  virtual bool hasSupportFunction() const;
121 
126  virtual agx::Vec3 getSupportPoint( const agx::Vec3& supportDirection) const;
127 
132  virtual agx::Vec3 getCenter() const;
133 
137  virtual agx::Real getVolume() const = 0;
138 
143  virtual agx::SPDMatrix3x3 calculateInertia(agx::Real mass) const = 0;
144 
147  virtual void propagateTransform(const agx::AffineMatrix4x4& transform);
148 
153  virtual const BoundingAABB& updateBoundingVolume() = 0;
154 
159  virtual BoundingAABB calculateLocalBound() const = 0;
160 
166  virtual agx::Real calculateBoundingRadius( BoundingAABB& localAABB ) const;
167 
169 
173  Geometry* getGeometry();
174 
178  const Geometry* getGeometry() const;
179 
181  agx::AffineMatrix4x4 getTransform() const;
182 
184  agx::AffineMatrix4x4 getLocalTransform() const;
185 
186 
190  static BoundingAABB calculateBound(const agx::AffineMatrix4x4& transform, const BoundingAABB& localBound);
191 
195  agx::UInt32 getModifiedCount() const;
196 
198 
199 
201  void store( agxStream::OutputArchive& out ) const override;
202  void restore( agxStream::InputArchive& in ) override;
204 
205 
211  virtual void setRenderData( agxCollide::RenderData *renderData );
212 
218  const agxCollide::RenderData* getRenderData( ) const;
219  agxCollide::RenderData* getRenderData();
220 
221 
222  protected:
223 
229  Shape(Type type, agx::Physics::Geometry::ShapePtr entity = agx::Physics::Geometry::ShapeModel::createInstance());
230 
235  agx::UInt32 incrementModifiedCount();
236 
238  virtual ~Shape();
239 
241 
242 
243  Shape& operator=(const Shape&) = delete;
244 
246  void synchronize();
248 
249  private:
250  friend class ShapeGroup;
251  friend class Geometry;
252  friend class Space;
253  friend class agxTerrain::Terrain;
254 
255  Shape();
256  virtual void setGeometry(Geometry* geometry);
257  virtual void transfer(Space* space);
258 
259  virtual void updateGeometryPtr();
260 
261 
262  protected:
263 
264  void syncronizeShapeIndex();
265 
266  agx::Physics::Geometry::ShapeRef m_entity;
267  Geometry* m_geometry;
268  agxCollide::RenderDataRef m_renderData;
269  agx::Index m_shapeIndex;
270 
271  ShapeGroup* m_parent;
272  };
273 
274 
276 
277 
278  AGX_DECLARE_POINTER_TYPES(ShapeGroup);
279  class AGXPHYSICS_EXPORT ShapeGroup : public Shape
280  {
281  public:
282  ShapeGroup();
283  virtual Shape *clone() const override;
284 
285  void addChild(Shape* shape, const agx::AffineMatrix4x4& transform = agx::AffineMatrix4x4());
286  void removeChild(Shape* shape);
287  void removeChild(size_t index);
288 
293  virtual void setRenderData( agxCollide::RenderData *renderData ) override;
294 
295 
297 
298  size_t getNumChildren() const;
299  Shape* getChild(size_t index);
300  const Shape* getChild(size_t index) const;
301  const agx::AffineMatrix4x4& getChildTransform(size_t index) const;
302 
308  bool setChildTransform(size_t childIndex, const agx::AffineMatrix4x4& transform);
309 
310 
311  const ShapeRefVector& getChildren() const;
312  const agx::AffineMatrix4x4Vector& getChildTransforms() const;
313 
314  bool replaceChild(size_t childIndex, Shape* newChild, const agx::AffineMatrix4x4& transform = agx::AffineMatrix4x4());
315 
316  /* Common shape methods */
317 
319  virtual agx::SPDMatrix3x3 calculateInertia(agx::Real mass) const override;
320 
322  virtual agx::Real getVolume() const override;
323 
325  virtual agx::Vec3 getCenter() const override;
326 
327  // Computes bounding volume in world coordinate system.
328  virtual const BoundingAABB& updateBoundingVolume() override;
329 
330  // Computes bounding volume in local coordinate system.
331  virtual BoundingAABB calculateLocalBound() const override;
332 
333  virtual void propagateTransform(const agx::AffineMatrix4x4& transform) override;
334 
335  AGXSTREAM_DECLARE_SERIALIZABLE(agxCollide::ShapeGroup);
336 
337  protected:
338  virtual ~ShapeGroup();
339 
340  private:
341  friend class Geometry;
342 
343  // void childUpdated(Shape *child);
344  virtual void updateGeometryPtr() override;
345  virtual void setGeometry(Geometry* geometry) override;
346  virtual void transfer(Space* space) override;
347 
348  private:
349  ShapeRefVector m_children;
350  };
351 
352  class AGXPHYSICS_EXPORT ShapeIterator
353  {
354  public:
355  ShapeIterator(Geometry* geometry);
356 
357  Shape* getShape();
358  const Shape* getShape() const;
359  agx::AffineMatrix4x4 getLocalTransform() const;
360 
361  bool isValid() const;
362  void next();
363 
364  private:
365  void push(Shape* node, const agx::AffineMatrix4x4& transform);
366 
367  private:
368  agx::Vector<Shape*> m_shapes;
370  size_t m_index;
371  };
372 
374 
375 
376  /* Implementation */
377 
379  {
380  return m_entity.type();
381  }
382  AGX_FORCE_INLINE bool Shape::isGroup() const
383  {
384  return getType() == GROUP;
385  }
386  AGX_FORCE_INLINE ShapeGroup* Shape::asGroup()
387  {
388  agxAssert(this->isGroup());
389  return static_cast<ShapeGroup*>(this);
390  }
391  AGX_FORCE_INLINE const ShapeGroup* Shape::asGroup() const
392  {
393  return const_cast<Shape*>(this)->asGroup();
394  }
395 
397  {
398  return m_entity;
399  }
400  AGX_FORCE_INLINE Geometry* Shape::getGeometry()
401  {
402  return m_geometry;
403  }
404  AGX_FORCE_INLINE const Geometry* Shape::getGeometry() const
405  {
406  return m_geometry;
407  }
408  AGX_FORCE_INLINE agx::AffineMatrix4x4 Shape::getTransform() const
409  {
410  return m_entity.transform();
411  }
412 
413  AGX_FORCE_INLINE const BoundingAABB& Shape::getBoundingVolume() const
414  {
415  return m_entity.boundingAABB();
416  }
417 
418  AGX_FORCE_INLINE BoundingAABB Shape::calculateBound(const agx::AffineMatrix4x4& transform, const BoundingAABB& localBound)
419  {
420  return BoundingAABB(localBound, transform);
421  }
422 
423  AGX_FORCE_INLINE size_t ShapeGroup::getNumChildren() const
424  {
425  return m_children.size();
426  }
427  AGX_FORCE_INLINE Shape* ShapeGroup::getChild(size_t index)
428  {
429  return m_children[index];
430  }
431  AGX_FORCE_INLINE const Shape* ShapeGroup::getChild(size_t index) const
432  {
433  return m_children[index];
434  }
435  AGX_FORCE_INLINE const agx::AffineMatrix4x4& ShapeGroup::getChildTransform(size_t index) const
436  {
437  return getEntity().childTransforms()[index];
438  }
439 
441  {
442  return m_entity;
443  }
444 
445  AGX_FORCE_INLINE const ShapeRefVector& ShapeGroup::getChildren() const
446  {
447  return m_children;
448  }
449  AGX_FORCE_INLINE const agx::AffineMatrix4x4Vector& ShapeGroup::getChildTransforms() const
450  {
451  return reinterpret_cast<agx::AffineMatrix4x4Vector&>(getEntity().childTransforms());
452  }
453 
455  AGX_FORCE_INLINE agx::UInt32 Shape::getModifiedCount() const
456  {
457  return m_entity.modifiedCount();
458  }
459 
460  AGX_FORCE_INLINE agx::UInt32 Shape::incrementModifiedCount()
461  {
462  return ++m_entity.modifiedCount();
463  }
464 
465 
466  AGX_FORCE_INLINE Shape* ShapeIterator::getShape()
467  {
468  agxAssert(this->isValid());
469  return m_shapes[m_index];
470  }
471 
472  AGX_FORCE_INLINE const Shape* ShapeIterator::getShape() const
473  {
474  return const_cast<ShapeIterator*>(this)->getShape();
475  }
476 
477  inline agx::AffineMatrix4x4 ShapeIterator::getLocalTransform() const
478  {
479  agxAssert(this->isValid());
480  return m_transforms[m_index];
481  }
482 
483  AGX_FORCE_INLINE bool ShapeIterator::isValid() const
484  {
485  return m_index < m_shapes.size();
486  }
487 
488  AGX_FORCE_INLINE void ShapeIterator::next()
489  {
490  agxAssert(this->isValid());
491  m_index++;
492  }
493 
494  inline bool Shape::hasSupportFunction() const
495  {
496  return false;
497  }
498 
499  inline agx::Vec3 Shape::getSupportPoint( const agx::Vec3& ) const
500  {
501  return agx::Vec3(0, 0, 0);
502  }
503 
505  {
506  return agx::Vec3(0, 0, 0);
507  }
508 
509 }
510 #endif
InputRef< T > in(const char *name, T &obj)
Create an object with a name and a reference to the object that should be restored (usually a pointer...
Definition: InputArchive.h:400
AGXCORE_EXPORT Type * getType(const agx::String &name)
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
OutputRef< Serializable > out(const char *name, const Serializable *obj)
Return an object that contain the name and the object that should be serialized to an archive...
Base class for a shape.
Definition: Shape.h:53
This namespace consists of a set of classes for handling geometric intersection tests including boole...
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes...
Definition: Geometry.h:59
uint8_t UInt8
Definition: Integer.h:38
Class for storing rendering information for a Shape.
Definition: RenderData.h:38
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
#define AGXPHYSICS_EXPORT
Pointer to a entity instance of type Physics.Geometry.Shape.
Definition: ShapeEntity.h:195
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGX_FORCE_INLINE
Definition: Object.h:59
Base class providing referencing counted objects.
Definition: Referenced.h:119
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
AGXMODEL_EXPORT agx::Vec3 getCenter(const agxCollide::Mesh::Triangle &triangle)
Real calculateBoundingRadius(Real)
Definition: CreateTier.h:37
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
UInt32 Index
Definition: Integer.h:52
#define agxAssert(expr)
Definition: debug.h:143
#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
Type
Define the type of a Shape.
Definition: Shape.h:58
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
#define AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE(T)
Use this in a pure abstract Serializable class to add the required methods Important: Use full namesp...
Definition: Serializable.h:221
double Real
Definition: Real.h:42
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.
Pointer to a entity instance of type Physics.Geometry.ShapeGroup.