AGXDynamics  2.35.0.2
Shape.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_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  }
383  {
384  return getType() == GROUP;
385  }
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  }
401  {
402  return m_geometry;
403  }
405  {
406  return m_geometry;
407  }
409  {
410  return m_entity.transform();
411  }
412 
414  {
415  return m_entity.boundingAABB();
416  }
417 
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 
456  {
457  return m_entity.modifiedCount();
458  }
459 
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 
500  {
501  return agx::Vec3(0, 0, 0);
502  }
503 
505  {
506  return agx::Vec3(0, 0, 0);
507  }
508 
509 }
510 #endif
agxPhysics_export.h
BoundingAABB.h
SPDMatrix3x3.h
agxCollide::Shape::incrementModifiedCount
agx::UInt32 incrementModifiedCount()
Increment the number of times the shape have been modified.
Definition: Shape.h:460
agx::Real
double Real
Definition: Real.h:42
agxData::transform
void AGXCORE_EXPORT transform(void *target, const Format *targetFormat, const void *source, const Format *sourceFormat, size_t numElements)
Transform data between two buffers.
agx::Vector< agx::ref_ptr< Shape > >
agxStream::out
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.
Definition: OutputArchive.h:412
agxTerrain
The agxTerrain namespace contains a 3D model for a dynamic deformable Terrain and related classes.
Definition: Geometry.h:59
agxStream::Serializable
This class is an abstract base class for all classes that can be stored and retrieved from an Archive...
Definition: Serializable.h:44
agxCollide::Shape::hasSupportFunction
virtual bool hasSupportFunction() const
Definition: Shape.h:494
agxCollide::Shape::HEIGHT_FIELD
@ HEIGHT_FIELD
Definition: Shape.h:67
agxCollide::Shape::CONVEX
@ CONVEX
Definition: Shape.h:68
agxData::getType
AGXCORE_EXPORT Type * getType(const agx::String &name)
DOXYGEN_END_INTERNAL_BLOCK
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
agxCollide::Shape::isGroup
bool isGroup() const
Definition: Shape.h:382
agxCollide::Shape::getSupportPoint
virtual agx::Vec3 getSupportPoint(const agx::Vec3 &supportDirection) const
Returns the support point on face of the shape.
Definition: Shape.h:499
agxCollide::RenderData
Class for storing rendering information for a Shape.
Definition: RenderData.h:38
agx::AffineMatrix4x4T< Real >
DOXYGEN_START_INTERNAL_BLOCK
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
agxAssert
#define agxAssert(expr)
Definition: debug.h:143
agxCollide::Shape::getModifiedCount
agx::UInt32 getModifiedCount() const
Definition: Shape.h:455
AGXSTREAM_DECLARE_ABSTRACT_SERIALIZABLE
#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
agxCollide::Shape::PLANE
@ PLANE
Definition: Shape.h:64
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
Vector containing 'raw' data.
Definition: Vector.h:245
agxCollide::Shape::getEntity
agx::Physics::Geometry::ShapePtr getEntity() const
Definition: Shape.h:396
agx
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
Definition: AddedMassInteraction.h:22
AGXPHYSICS_EXPORT
#define AGXPHYSICS_EXPORT
ShapeEntity.h
agxStream::in
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
AffineMatrix4x4.h
agxCollide::Shape::BOX
@ BOX
Definition: Shape.h:60
agxCollide::Shape::HOLLOW_CONE
@ HOLLOW_CONE
Definition: Shape.h:71
RenderData.h
AGX_DECLARE_VECTOR_TYPES
#define AGX_DECLARE_VECTOR_TYPES(type)
Definition: Vector.h:34
agxCollide::Shape::CYLINDER
@ CYLINDER
Definition: Shape.h:62
Vec3.h
agx::ref_ptr< Shape >
agx::UInt8
uint8_t UInt8
Definition: Integer.h:38
agxCollide::Shape
Base class for a shape.
Definition: Shape.h:53
agx::Vec3
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
agx::Physics::Geometry::ShapeGroupPtr
Pointer to a entity instance of type Physics.Geometry.ShapeGroup.
Definition: ShapeGroupEntity.h:173
agxCollide::ShapeRefVector
agx::Vector< agx::ref_ptr< Shape > > ShapeRefVector
Definition: agxcollide_vector_types.h:39
agxModel::getCenter
AGXMODEL_EXPORT agx::Vec3 getCenter(const agxCollide::Mesh::Triangle &triangle)
agx::Index
UInt32 Index
Definition: Integer.h:52
agxCollide::Shape::Type
Type
Define the type of a Shape.
Definition: Shape.h:58
agxCollide::Geometry
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agx::SPDMatrix3x3
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
Serializable.h
agxCollide::Space
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
agxCollide::Shape::GROUP
@ GROUP
Definition: Shape.h:59
agxCollide.h
agxCollide::Shape::asGroup
ShapeGroup * asGroup()
Auto-cast shape to group.
Definition: Shape.h:386
agxCollide::Shape::CONE
@ CONE
Definition: Shape.h:70
agxCollide::Shape::WIRE_SHAPE
@ WIRE_SHAPE
Definition: Shape.h:69
agx::Referenced
Base class providing referencing counted objects.
Definition: Referenced.h:113
agx::calculateBoundingRadius
Real calculateBoundingRadius(Real)
Definition: CreateTier.h:37
agx::Physics::Geometry::ShapePtr
Pointer to a entity instance of type Physics.Geometry.Shape.
Definition: ShapeEntity.h:195
agxCollide
This namespace consists of a set of classes for handling geometric intersection tests including boole...
Definition: ContactForceFilterParticipant.h:22
agxCollide::Shape::SPHERE
@ SPHERE
Definition: Shape.h:65
agxStream
This namespace contain classes for streaming classes into archives, ASCII, binary for storage (serial...
Definition: ElementaryConstraint.h:31
agxCollide::AGX_DECLARE_POINTER_TYPES
AGX_DECLARE_POINTER_TYPES(SpaceListener)
agxCollide::Shape::getGeometry
Geometry * getGeometry()
Definition: Shape.h:400
agx::Vec3T< Real >
agxCollide::Shape::LINE
@ LINE
Definition: Shape.h:63
agxCollide::Shape::getBoundingVolume
const BoundingAABB & getBoundingVolume() const
Definition: Shape.h:413
agxCollide::Shape::HOLLOW_CYLINDER
@ HOLLOW_CYLINDER
Definition: Shape.h:72
agx::UInt32
uint32_t UInt32
Definition: Integer.h:40
agxCollide::Shape::TRIMESH
@ TRIMESH
Definition: Shape.h:66
ShapeGroupEntity.h
agxCollide::Shape::CAPSULE
@ CAPSULE
Definition: Shape.h:61
agxStream::ElementTypes::Type
Type
Definition: Types.h:37
agxCollide::Shape::getCenter
virtual agx::Vec3 getCenter() const
Definition: Shape.h:504
AGX_FORCE_INLINE
#define AGX_FORCE_INLINE
Definition: macros.h:58
agxCollide::Shape::calculateBound
static BoundingAABB calculateBound(const agx::AffineMatrix4x4 &transform, const BoundingAABB &localBound)
Definition: Shape.h:418
agxData::getEntity
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.
agxCollide::Shape::getTransform
agx::AffineMatrix4x4 getTransform() const
Definition: Shape.h:408