AGX Dynamics  2.35.0.1
Contacts.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 #pragma once
18 
19 #ifdef _MSC_VER
20 # pragma warning(push)
21 # pragma warning( disable : 4275 ) // warning C4275: non dll-interface class
22 #endif
23 
24 #include <agx/agxPhysics_export.h>
25 
26 #include <agxCollide/agxCollide.h>
27 #include <agx/agx.h>
28 #include <agx/Referenced.h>
29 #include <agx/RigidBody.h>
30 #include <agx/Vec3.h>
32 
33 #include <agx/Material.h>
34 #include <agx/Vector.h>
40 
41 
42 namespace agxSDK
43 {
44  class MaterialManager;
45 }
46 
47 namespace agxCollide
48 {
49  class Geometry;
50 
51 
56  {
57  public:
59  {
62  TANGENTIAL_FORCE_V
63  };
64 
66  {
67  IMPACTING = 1 << 0,
73  NONHOLONOMIC = 1 << 1
79  };
80 
81  public:
85  ContactPoint();
86 
93 
100  ContactPoint(agx::Vec3 thePoint, agx::Vec3f theNormal, agx::Real theDepth);
101 
103  void clear();
104 
106 
112  agx::Real& localForce( size_t idx );
113 
119  agx::Real localForce( size_t idx ) const;
120 
125  agx::Vec3 getForce() const;
126 
130  agx::Real getForceMagnitude() const;
131 
135  agx::Vec3 getNormalForce() const;
136 
140  agx::Real getNormalForceMagnitude() const;
141 
145  agx::Real getTangentialForceUMagnitude() const;
146 
150  agx::Real getTangentialForceVMagnitude() const;
151 
155  agx::Real getTangentialForceMagnitude() const;
156 
160  agx::Vec3 getTangentialForce() const;
161 
166  agx::UInt32& faceIndex(size_t ith);
167 
172  agx::UInt32 const& faceIndex(size_t ith) const;
173 
178  agx::UInt8& faceFeature(size_t ith);
179 
184  agx::UInt8 const& faceFeature(size_t ith) const;
185 
190  agx::Physics::Geometry::ShapePtr& shape(size_t ith);
191 
196  agx::Physics::Geometry::ShapePtr const& shape(size_t ith) const;
197 
206  agx::Vec3 getWitnessPoint(size_t ith) const;
207 
211  agx::Bool hasState( ContactPointState contactPointState ) const;
212 
219  void setIsHolonomic( agx::Bool isHolonomic );
220 
225  agx::Bool isHolonomic() const;
226  };
227 
228 
234  template<typename T1, typename T2>
235  void copyContactPoint(const T1& from, T2& to)
236  {
237  to.point() = from.point();
238  to.normal() = from.normal();
239  to.depth() = from.depth();
240  to.area() = from.area();
241 
242  to.velocity() = from.velocity();
243  to.enabled() = from.enabled();
244 
245  to.shape1() = from.shape1();
246  to.shape2() = from.shape2();
247 
248  to.faceIndex1() = from.faceIndex1();
249  to.faceIndex2() = from.faceIndex2();
250  to.faceFeature1() = from.faceFeature1();
251  to.faceFeature2() = from.faceFeature2();
252  to.state() = from.state();
253  }
254 
255 #ifndef SWIG
260 
261 
263 #endif
265 
270  {
271  public:
273 
274  public:
277  NO_CONTACT = 0,
278  IMPACT_STATE = 1,
279  CONTACT_STATE = 2,
280  SEPARATION_STATE = 3
281  };
282 
283  public:
287  GeometryContact();
288 
294 
298  void clear();
299 
307  agx::Vec3f calculateRelativeVelocity( size_t pointIndex = 0 ) const;
308 
316  static bool calculateSurfaceVelocity( const agxCollide::Geometry* geometry1, const agxCollide::Geometry* geometry2, LocalContactPoint& p );
317 
325  static bool calculateSurfaceVelocity( const agxCollide::Geometry* geometry1, const agxCollide::Geometry* geometry2, ContactPoint& p );
326 
331  agxCollide::Geometry* geometry( size_t ith );
332 
337  const agxCollide::Geometry* geometry( size_t ith ) const;
338 
344  agx::RigidBody* rigidBody( size_t ith );
345 
351  const agx::RigidBody* rigidBody( size_t ith ) const;
352 
354  ContactPointVector& points();
355 
357  const ContactPointVector& points() const;
358 
360  void* getCustomData();
361 
363  const void* getCustomData() const;
364 
369  void setCustomData( void* customData );
370 
377  agx::ContactMaterial* getMaterial();
378 
385  const agx::ContactMaterial* getMaterial() const;
386 
392  const agx::ContactMaterial* getMaterial( bool ) const;
393 
395  agx::UInt8 state() const;
396 
399 
403  void setEnable(bool flag);
404 
413  int contains(agx::RigidBody * body) const;
414 
423  int contains(agxCollide::Geometry * geometry) const;
424 
428  bool isEnabled() const;
429 
437 
438 
440 
441 
442 
447  void setGeometry( size_t ith, Geometry* geom );
448 
449  /***
450  When called, this contact should be removed before entering the solver
451  NOTE: Contacts are never removed, only marked as disabled
452  \param immediately - Should the contact be removed immediately
453  */
454  void markForRemoval( bool immediately );
455 
461  bool shouldBeCalled() const;
462 
466  void setMaterial( agx::ContactMaterial* material );
467 
468  void setHasSurfaceVelocity( bool flag );
469 
470  GeometryPair getGeometryPair();
471 
472  void setHasInternalMaterial( bool flag );
473  bool getHasInternalMaterial( ) const;
474 
475  void setState( agx::UInt8 state );
477  };
478 
479 
480 
481  inline int GeometryContact::contains(agx::RigidBody * body) const
482  {
483  if (this->rigidBody(0) == body) return 0;
484  if (this->rigidBody(1) == body) return 1;
485  return -1;
486  }
487 
488  inline int GeometryContact::contains(agxCollide::Geometry * geometry) const
489  {
490  if (this->geometry(0) == geometry) return 0;
491  if (this->geometry(1) == geometry) return 1;
492  return -1;
493  }
494 
495 
496 
497 #ifndef SWIG
498  //AGX_STATIC_ASSERT(sizeof(GeometryContact) == sizeof(agx::Physics::GeometryContactPtr));
499 #endif
500 
501  /* Implementation */
502  inline ContactPoint::ContactPoint() : agx::Physics::ContactPointPtr( agx::Physics::ContactPointModel::createInstance() )
503  {
504  }
505 
506  inline ContactPoint::ContactPoint(agx::Physics::ContactPointPtr contact) : agx::Physics::ContactPointPtr(contact)
507  {
508  }
509 
510 
512  {
513  depth() = agx::Real( 0 );
514  enabled() = true;
515  point().set(0, 0, 0);
516  normal().set(0, 0, 0);
517  state() = agx::UInt8( 0 );
518  tangentU().set(0, 0, 0);
519  tangentV().set(0, 0, 0);
520  localForce().set( 0, 0, 0 );
521  velocity().set(0, 0, 0);
522  faceIndex1() = 0;
523  faceIndex2() = 0;
524  faceFeature1() = 0;
525  faceFeature2() = 0;
526  area() = agx::Real( 0 );
529  }
530 
531 
533  {
534  agxAssert(idx < 3);
535  return localForce()[idx];
536  }
537 
538 
540  {
541  agxAssert(idx < 3);
542  return localForce()[idx];
543  }
544 
545 
546 
548  {
549  return getNormalForce() + getTangentialForce();
550  }
551 
552 
554  {
555  agx::Real sqr =
559  return sqr > 0 ? std::sqrt( sqr ) : 0;
560  }
561 
562 
564  {
565  return agx::Vec3(normal()) * localForce()[ NORMAL_FORCE ];
566  }
567 
568 
570  {
571  return std::abs(localForce()[ NORMAL_FORCE ]);
572  }
573 
574 
576  {
577  const agx::Vec3 forceU = agx::Vec3(tangentU()) * localForce()[ TANGENTIAL_FORCE_U ];
578  const agx::Vec3 forceV = agx::Vec3(tangentV()) * localForce()[ TANGENTIAL_FORCE_V ];
579  return forceU + forceV;
580  }
581 
582 
584  {
585  return std::abs(localForce()[ TANGENTIAL_FORCE_U ]);
586  }
587 
588 
590  {
591  return std::abs(localForce()[ TANGENTIAL_FORCE_V ]);
592  }
593 
594 
596  {
598  return sqr > 0 ? std::sqrt( sqr ) : 0;
599  }
600 
602  {
603  return ( state() & contactPointState ) != 0;
604  }
605 
607  {
608  return !hasState( NONHOLONOMIC );
609  }
610 
612  {
613  if ( isHolonomic )
614  {
615  // remove non-holonomic bit
616  state() = (agx::UInt8)( state() & ~NONHOLONOMIC );
617  }
618  else
619  {
620  // add non-holonomic bit
621  state() |= ( agx::UInt8 )( NONHOLONOMIC );
622  }
623  }
624 
626  {
627 
628  }
629 
631  agx::Physics::GeometryContactPtr(contact)
632 
633  {
634  }
635 
637  {
638  agxAssert(ith < 2);
639  if(ith == 0)
640  return geometry1().model();
641  else
642  return geometry2().model();
643  }
644 
646  {
647  agxAssert(ith < 2);
648  if(ith == 0)
649  return geometry1().model();
650  else
651  return geometry2().model();
652  }
653 
654  AGX_FORCE_INLINE GeometryPair GeometryContact::getGeometryPair()
655  {
656  return GeometryPair(geometry(0), geometry(1));
657  }
658 
659  AGX_FORCE_INLINE void GeometryContact::setGeometry( size_t ith, Geometry* geom )
660  {
661  agxAssert(ith < 2 );
662  if(ith == 0)
663  geometry1() = geom->getEntity();
664  else
665  geometry2() = geom->getEntity();
666  }
667 
669  {
670  agx::Vec3 v1, v2;
671  if (pointIndex > points().size() - 1)
672  return agx::Vec3f();
673 
674  const agx::RigidBody* rb1 = geometry(0)->getRigidBody();
675  const agx::RigidBody* rb2 = geometry(1)->getRigidBody();
676 
677  if (rb1)
678  v1 = rb1->getVelocity() + (rb1->getAngularVelocity() ^ (points()[pointIndex].point() - rb1->getCmPosition()));
679  if (rb2)
680  v2 = rb2->getVelocity() + (rb2->getAngularVelocity() ^ (points()[pointIndex].point() - rb2->getCmPosition()));
681 
682  return static_cast<agx::Vec3f>(v1 - v2);
683  }
684 
685 
687  {
688  LocalContactPoint lcp;
689  lcp.point() = p.point();
691  p.velocity() = lcp.velocity();
692  return hasSurfaceVelocity;
693  }
694 
695 
697  {
698  agxAssert(ith < 2 );
699  return geometry(ith)->getRigidBody();
700  }
701 
703  {
704  agxAssert(ith < 2 );
705  return geometry(ith)->getRigidBody();
706  }
707 
708 
710  {
712  }
713 
715  {
716  return reinterpret_cast<const ContactPointVector&>(agx::Physics::GeometryContactPtr::points());
717  }
718 
720  {
721  return customData();
722  }
723 
725  {
726  return customData();
727  }
728 
729  inline void GeometryContact::setCustomData( void* customData )
730  {
731  this->customData() = customData;
732  }
733 
734 
735  AGX_FORCE_INLINE void GeometryContact::markForRemoval( bool immediately )
736  {
737  this->immediately() = immediately;
738  this->enabled() = false;
739  }
740 
742  {
743  this->enabled() = flag;
744  }
745 
747  {
748  return this->enabled();
749  }
750 
751 
752  AGX_FORCE_INLINE bool GeometryContact::shouldBeCalled() const
753  {
754  return !immediately();
755  }
756 
758  {
759  if ( material() && material().isExplicit() )
760  return material().model();
761  return nullptr;
762  }
763 
765  {
766  if ( material() && material().isExplicit() )
767  return material().model();
768  return nullptr;
769  }
770 
772  {
773  return material() ? material().model() : nullptr;
774  }
775 
776  AGX_FORCE_INLINE void GeometryContact::setMaterial( agx::ContactMaterial* material )
777  {
778  agxAssert( material != nullptr );
779  if (material)
780  this->material() = material->getEntity();
781  }
782 
783  AGX_FORCE_INLINE void GeometryContact::setHasSurfaceVelocity( bool flag )
784  {
785  hasSurfaceVelocity() = flag;
786  }
787 
788 
789  AGX_FORCE_INLINE void GeometryContact::setHasInternalMaterial( bool flag )
790  {
791  hasInternalMaterial() = flag;
792  }
793 
794  AGX_FORCE_INLINE bool GeometryContact::getHasInternalMaterial( ) const
795  {
796  return hasInternalMaterial();
797  }
798 
800  {
801  if ( broadPhasePair() )
802  return broadPhasePair().state();
803 
805  }
806 
807  AGX_FORCE_INLINE void GeometryContact::setState( agx::UInt8 state )
808  {
810  broadPhasePair().state() = state;
811  }
812 
814  {
815  if (ith == 0) return faceIndex1(); else return faceIndex2();
816  }
817 
818 
820  {
821  if (ith == 0) return faceIndex1(); else return faceIndex2();
822  }
823 
824 
826  {
827  if (ith == 0) return faceFeature1(); else return faceFeature2();
828  }
829 
830 
832  {
833  if (ith == 0) return faceFeature1(); else return faceFeature2();
834  }
835 
836 
838  {
839  if (ith == 0) return shape1(); else return shape2();
840  }
841 
842 
844  {
845  if (ith == 0) return shape1(); else return shape2();
846  }
847 
848 
850  {
851  if (ith == 0)
852  return point() - agx::Vec3(normal()) * (depth() * agx::Real(0.5));
853  else
854  return point() + agx::Vec3(normal()) * (depth() * agx::Real(0.5));
855  }
856 }
857 
858 #ifdef _MSC_VER
859 # pragma warning(pop)
860 #endif
861 
#define AGXPHYSICS_EXPORT
A contact point in a contact data.
Definition: Contacts.h:56
agx::Real getForceMagnitude() const
Definition: Contacts.h:553
void clear()
Clear all values in this contact.
Definition: Contacts.h:511
agx::Real getNormalForceMagnitude() const
Definition: Contacts.h:569
agx::Real getTangentialForceVMagnitude() const
Definition: Contacts.h:589
agx::Bool isHolonomic() const
Definition: Contacts.h:606
ContactPoint(agx::Vec3 thePoint, agx::Vec3f theNormal, agx::Real theDepth)
Construct given point, normal and depth.
@ TANGENTIAL_FORCE_V
Tangential force V, orthogonal to the normal force and U.
Definition: Contacts.h:62
@ TANGENTIAL_FORCE_U
Tangential force U, orthogonal to the normal force and V.
Definition: Contacts.h:61
@ NORMAL_FORCE
Normal force.
Definition: Contacts.h:60
void setIsHolonomic(agx::Bool isHolonomic)
Set if contact point should be treated as a holonomic constraint ( regular contact constraint with de...
Definition: Contacts.h:611
agx::Real getTangentialForceUMagnitude() const
Definition: Contacts.h:583
agx::UInt32 & faceIndex(size_t ith)
Definition: Contacts.h:813
agx::Physics::Geometry::ShapePtr & shape(size_t ith)
Definition: Contacts.h:837
agx::Bool hasState(ContactPointState contactPointState) const
Definition: Contacts.h:601
agx::Vec3 getWitnessPoint(size_t ith) const
Get witness point for ith geometry.
Definition: Contacts.h:849
agx::Vec3 getTangentialForce() const
Definition: Contacts.h:575
agx::Vec3 getNormalForce() const
Definition: Contacts.h:563
@ NONHOLONOMIC
This contact point is treated as a non-holonomic constraint in the normal direction - i....
Definition: Contacts.h:73
agx::Vec3 getForce() const
Definition: Contacts.h:547
agx::Real getTangentialForceMagnitude() const
Definition: Contacts.h:595
agx::UInt8 & faceFeature(size_t ith)
Definition: Contacts.h:825
ContactPoint()
Default constructor.
Definition: Contacts.h:502
A contact between two geometries.
Definition: Contacts.h:270
void setCustomData(void *customData)
Store custom data for this Contact.
Definition: Contacts.h:729
agxCollide::Geometry * geometry(size_t ith)
Definition: Contacts.h:636
void setEnable(bool flag)
Set to false to disable the contact.
Definition: Contacts.h:741
agx::UInt8 state() const
Definition: Contacts.h:799
agx::RigidBody * rigidBody(size_t ith)
Returns access to rigid body data.
Definition: Contacts.h:696
agx::Vec3f calculateRelativeVelocity(size_t pointIndex=0) const
Calculate the relative velocity in a contact point.
Definition: Contacts.h:668
void removeContactPoint(ContactPoint &point)
remove the specified ContactPoint from the set of contact points.
void clear()
Clear contact points.
agx::ContactMaterial * getMaterial()
If the contact material for this geometry contact is explicit it can be accessed here.
Definition: Contacts.h:757
int contains(agx::RigidBody *body) const
Will return -1 if body does not exist in this GeometryContact, 0 if it is the first (index 0) and 1 i...
Definition: Contacts.h:481
ContactState
Specify the state of the contact.
Definition: Contacts.h:276
@ NO_CONTACT
! No contact yet
Definition: Contacts.h:277
ContactPointVector & points()
Definition: Contacts.h:709
static bool calculateSurfaceVelocity(const agxCollide::Geometry *geometry1, const agxCollide::Geometry *geometry2, LocalContactPoint &p)
Calculate the relative velocity in a contact point between geometry1 and geometry2 Put result in p.
agx::Bool hasImpactingPoints() const
GeometryContact()
Default constructor.
Definition: Contacts.h:625
The geometry representation used by the collision detection engine.
Definition: Geometry.h:93
agx::RigidBody * getRigidBody()
Definition: Geometry.h:850
Type-specific Array used for fast access into the data held by a Buffer.
Definition: Array.h:107
This class store the combine material properties between two agx::Material's.
Definition: Material.h:528
AGXPHYSICS_EXPORT agx::UInt8 & state()
AGXPHYSICS_EXPORT agx::ContactMaterial *& model()
Pointer to a entity instance of type Physics.ContactPoint.
AGXPHYSICS_EXPORT agx::Vec3f & normal()
AGXPHYSICS_EXPORT agx::UInt32 & faceIndex1()
The face index of shape 1 in contact (e.g. index of triangle for trimesh).
AGXPHYSICS_EXPORT agx::Vec3f & velocity()
AGXPHYSICS_EXPORT agx::Vec3f & tangentU()
AGXPHYSICS_EXPORT agx::Physics::Geometry::ShapePtr & shape2()
Shape number 2 in contact.
AGXPHYSICS_EXPORT agx::UInt32 & faceIndex2()
The face index of shape 2 in contact (e.g. index of triangle for trimesh).
AGXPHYSICS_EXPORT agx::Physics::Geometry::ShapePtr & shape1()
Shape number 1 in contact.
AGXPHYSICS_EXPORT agx::Real & depth()
AGXPHYSICS_EXPORT agx::Vec3f & tangentV()
AGXPHYSICS_EXPORT agx::UInt8 & state()
Contact point state, such as impacting, resting etc.
AGXPHYSICS_EXPORT agx::UInt8 & faceFeature1()
The face feature of shape 1 in contact (e.g. voronoi region on triangle for trimesh).
AGXPHYSICS_EXPORT agx::UInt8 & faceFeature2()
The face feature of shape 2 in contact (e.g. voronoi region on triangle for trimesh).
AGXPHYSICS_EXPORT agx::Vec3 & localForce()
AGXPHYSICS_EXPORT agx::Vec3 & point()
AGXPHYSICS_EXPORT agx::Real & area()
AGXPHYSICS_EXPORT agx::Bool & enabled()
AGXPHYSICS_EXPORT agx::Physics::ContactMaterialPtr & material()
A geometry contact hold contact information between two geometries.
AGXPHYSICS_EXPORT agx::Physics::GeometryPtr & geometry1()
AGXPHYSICS_EXPORT void *& customData()
AGXPHYSICS_EXPORT agx::Bool & hasSurfaceVelocity()
AGXPHYSICS_EXPORT agx::Physics::GeometryPtr & geometry2()
AGXPHYSICS_EXPORT agx::Bool & enabled()
AGXPHYSICS_EXPORT agxData::Array< agx::Physics::ContactPointPtr > & points()
The list of contact points.
AGXPHYSICS_EXPORT agx::Physics::BroadPhasePairPtr & broadPhasePair()
AGXPHYSICS_EXPORT agx::Bool & immediately()
AGXPHYSICS_EXPORT agx::Bool & hasInternalMaterial()
AGXPHYSICS_EXPORT agxCollide::Geometry *& model()
Pointer to a entity instance of type Physics.Geometry.Shape.
Definition: ShapeEntity.h:196
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:55
agx::Vec3 getAngularVelocity() const
Angular velocity in world coordinate frame.
Definition: RigidBody.h:1514
agx::Vec3 getVelocity() const
Velocity of center of mass frame origin, in world coordinate frame.
Definition: RigidBody.h:1509
agx::Vec3 getCmPosition() const
Definition: RigidBody.h:1524
void set(T x, T y, T z)
Definition: Vec3Template.h:465
#define agxAssert(expr)
Definition: debug.h:143
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: macros.h:96
#define AGX_FORCE_INLINE
Definition: macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
void copyContactPoint(const T1 &from, T2 &to)
Copy between the different kinds of contact points.
Definition: Contacts.h:235
AGX_STATIC_ASSERT(sizeof(ContactPoint)==sizeof(agx::Physics::ContactPointPtr))
agxData::Array< ContactPoint > ContactPointVector
Definition: Contacts.h:264
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:31
AGXTERRAIN_EXPORT void clear()
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
uint32_t UInt32
Definition: Integer.h:40
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: agx/Vec3.h:36
bool Bool
Definition: Integer.h:48
double Real
Definition: Real.h:42
uint8_t UInt8
Definition: Integer.h:38
Vec3T< Real32 > Vec3f
Definition: agx/Vec3.h:39