AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Shape.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 
220 
221  protected:
222 
228  Shape(Type type, agx::Physics::Geometry::ShapePtr entity = agx::Physics::Geometry::ShapeModel::createInstance());
229 
234  agx::UInt32 incrementModifiedCount();
235 
237  virtual ~Shape();
238 
240 
241 
242  Shape& operator=(const Shape&) = delete;
243 
245  void synchronize();
247 
248  private:
249  friend class ShapeGroup;
250  friend class Geometry;
251  friend class Space;
252  friend class agxTerrain::Terrain;
253 
254  Shape();
255  virtual void setGeometry(Geometry* geometry);
256  virtual void transfer(Space* space);
257 
258  virtual void updateGeometryPtr();
259 
260 
261  protected:
262 
263  void syncronizeShapeIndex();
264 
265  agx::Physics::Geometry::ShapeRef m_entity;
266  Geometry* m_geometry;
267  agxCollide::RenderDataRef m_renderData;
268  agx::Index m_shapeIndex;
269 
270  ShapeGroup* m_parent;
271  };
272 
273 
275 
276 
277  AGX_DECLARE_POINTER_TYPES(ShapeGroup);
278  class AGXPHYSICS_EXPORT ShapeGroup : public Shape
279  {
280  public:
281  ShapeGroup();
282  virtual Shape *clone() const override;
283 
284  void addChild(Shape* shape, const agx::AffineMatrix4x4& transform = agx::AffineMatrix4x4());
285  void removeChild(Shape* shape);
286  void removeChild(size_t index);
287 
292  virtual void setRenderData( agxCollide::RenderData *renderData ) override;
293 
294 
296 
297  size_t getNumChildren() const;
298  Shape* getChild(size_t index);
299  const Shape* getChild(size_t index) const;
300  const agx::AffineMatrix4x4& getChildTransform(size_t index) const;
301 
307  bool setChildTransform(size_t childIndex, const agx::AffineMatrix4x4& transform);
308 
309 
310  const ShapeRefVector& getChildren() const;
311  const agx::AffineMatrix4x4Vector& getChildTransforms() const;
312 
313  bool replaceChild(size_t childIndex, Shape* newChild, const agx::AffineMatrix4x4& transform = agx::AffineMatrix4x4());
314 
315  /* Common shape methods */
316 
318  virtual agx::SPDMatrix3x3 calculateInertia(agx::Real mass) const override;
319 
321  virtual agx::Real getVolume() const override;
322 
324  virtual agx::Vec3 getCenter() const override;
325 
326  // Computes bounding volume in world coordinate system.
327  virtual const BoundingAABB& updateBoundingVolume() override;
328 
329  // Computes bounding volume in local coordinate system.
330  virtual BoundingAABB calculateLocalBound() const override;
331 
332  virtual void propagateTransform(const agx::AffineMatrix4x4& transform) override;
333 
334  AGXSTREAM_DECLARE_SERIALIZABLE(agxCollide::ShapeGroup);
335 
336  protected:
337  virtual ~ShapeGroup();
338 
339  private:
340  friend class Geometry;
341 
342  // void childUpdated(Shape *child);
343  virtual void updateGeometryPtr() override;
344  virtual void setGeometry(Geometry* geometry) override;
345  virtual void transfer(Space* space) override;
346 
347  private:
348  ShapeRefVector m_children;
349  };
350 
351  class AGXPHYSICS_EXPORT ShapeIterator
352  {
353  public:
354  ShapeIterator(Geometry* geometry);
355 
356  Shape* getShape();
357  const Shape* getShape() const;
358  agx::AffineMatrix4x4 getLocalTransform() const;
359 
360  bool isValid() const;
361  void next();
362 
363  private:
364  void push(Shape* node, const agx::AffineMatrix4x4& transform);
365 
366  private:
367  agx::Vector<Shape*> m_shapes;
369  size_t m_index;
370  };
371 
373 
374 
375  /* Implementation */
376 
378  {
379  return m_entity.type();
380  }
382  {
383  return getType() == GROUP;
384  }
386  {
387  agxAssert(this->isGroup());
388  return static_cast<ShapeGroup*>(this);
389  }
390  AGX_FORCE_INLINE const ShapeGroup* Shape::asGroup() const
391  {
392  return const_cast<Shape*>(this)->asGroup();
393  }
394 
396  {
397  return m_entity;
398  }
400  {
401  return m_geometry;
402  }
404  {
405  return m_geometry;
406  }
408  {
409  return m_entity.transform();
410  }
411 
413  {
414  return m_entity.boundingAABB();
415  }
416 
418  {
419  return BoundingAABB(localBound, transform);
420  }
421 
422  AGX_FORCE_INLINE size_t ShapeGroup::getNumChildren() const
423  {
424  return m_children.size();
425  }
426  AGX_FORCE_INLINE Shape* ShapeGroup::getChild(size_t index)
427  {
428  return m_children[index];
429  }
430  AGX_FORCE_INLINE const Shape* ShapeGroup::getChild(size_t index) const
431  {
432  return m_children[index];
433  }
434  AGX_FORCE_INLINE const agx::AffineMatrix4x4& ShapeGroup::getChildTransform(size_t index) const
435  {
436  return getEntity().childTransforms()[index];
437  }
438 
440  {
441  return m_entity;
442  }
443 
444  AGX_FORCE_INLINE const ShapeRefVector& ShapeGroup::getChildren() const
445  {
446  return m_children;
447  }
448  AGX_FORCE_INLINE const agx::AffineMatrix4x4Vector& ShapeGroup::getChildTransforms() const
449  {
450  return reinterpret_cast<agx::AffineMatrix4x4Vector&>(getEntity().childTransforms());
451  }
452 
455  {
456  return m_entity.modifiedCount();
457  }
458 
460  {
461  return ++m_entity.modifiedCount();
462  }
463 
464 
465  AGX_FORCE_INLINE Shape* ShapeIterator::getShape()
466  {
467  agxAssert(this->isValid());
468  return m_shapes[m_index];
469  }
470 
471  AGX_FORCE_INLINE const Shape* ShapeIterator::getShape() const
472  {
473  return const_cast<ShapeIterator*>(this)->getShape();
474  }
475 
476  inline agx::AffineMatrix4x4 ShapeIterator::getLocalTransform() const
477  {
478  agxAssert(this->isValid());
479  return m_transforms[m_index];
480  }
481 
482  AGX_FORCE_INLINE bool ShapeIterator::isValid() const
483  {
484  return m_index < m_shapes.size();
485  }
486 
487  AGX_FORCE_INLINE void ShapeIterator::next()
488  {
489  agxAssert(this->isValid());
490  m_index++;
491  }
492 
493  inline bool Shape::hasSupportFunction() const
494  {
495  return false;
496  }
497 
499  {
500  return agx::Vec3(0, 0, 0);
501  }
502 
504  {
505  return agx::Vec3(0, 0, 0);
506  }
507 
508 }
509 #endif
agx::AffineMatrix4x4 getTransform() const
Definition: Shape.h:407
ShapeGroup * asGroup()
Auto-cast shape to group.
Definition: Shape.h:385
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)
agx::Vector< agx::ref_ptr< Shape > > ShapeRefVector
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
virtual agx::Vec3 getCenter() const
Definition: Shape.h:503
bool isGroup() const
Definition: Shape.h:381
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
agx::UInt32 incrementModifiedCount()
Increment the number of times the shape have been modified.
Definition: Shape.h:459
uint8_t UInt8
Definition: Integer.h:38
Class for storing rendering information for a Shape.
Definition: RenderData.h:39
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
virtual bool hasSupportFunction() const
Definition: Shape.h:493
#define AGX_FORCE_INLINE
Definition: Object.h:62
agx::Physics::Geometry::ShapePtr getEntity() const
Definition: Shape.h:395
Base class providing referencing counted objects.
Definition: Referenced.h:113
The geometry representation used by the collision detection engine.
Definition: Geometry.h:88
AGXMODEL_EXPORT agx::Vec3 getCenter(const agxCollide::Mesh::Triangle &triangle)
Geometry * getGeometry()
Definition: Shape.h:399
Real calculateBoundingRadius(Real)
Definition: CreateTier.h:37
Vector containing &#39;raw&#39; data.
Definition: Vector.h:245
static BoundingAABB calculateBound(const agx::AffineMatrix4x4 &transform, const BoundingAABB &localBound)
Definition: Shape.h:417
This class contains all Geometries and performs Broad Phase and Narrow Phase collision detection to c...
Definition: Space.h:81
Specialized type of matrices for holding symmetric positive definite matrices.
Definition: SPDMatrix3x3.h:48
agx::UInt32 getModifiedCount() const
Definition: Shape.h:454
UInt32 Index
Definition: Integer.h:52
AGX_DECLARE_VECTOR_TYPES(Geometry)
const BoundingAABB & getBoundingVolume() const
Definition: Shape.h:412
#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:206
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:220
double Real
Definition: Real.h:42
Axis aligned bounding box implementation.
Definition: BoundingAABB.h:37
uint32_t UInt32
Definition: Integer.h:40
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:101
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:100
AGX_DECLARE_POINTER_TYPES(SpaceListener)
AGXCORE_EXPORT EntityModel * getEntity(const agx::Path &path)
Get a entity with a specified path.
virtual agx::Vec3 getSupportPoint(const agx::Vec3 &supportDirection) const
Returns the support point on face of the shape.
Definition: Shape.h:498
GLenum type
Definition: gl.h:5539
Pointer to a entity instance of type Physics.Geometry.ShapeGroup.