AGX Dynamics  2.37.3.2
Contacts.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 #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 
107 #ifndef SWIG
108 
113  agx::Real& localForce( size_t idx );
114 #endif
115 
120  agx::Real localForce( size_t idx ) const;
121 
126  agx::Vec3 getForce() const;
127 
131  agx::Real getForceMagnitude() const;
132 
136  agx::Vec3 getNormalForce() const;
137 
141  agx::Real getNormalForceMagnitude() const;
142 
146  agx::Real getTangentialForceUMagnitude() const;
147 
151  agx::Real getTangentialForceVMagnitude() const;
152 
156  agx::Real getTangentialForceMagnitude() const;
157 
161  agx::Vec3 getTangentialForce() const;
162 
167  agx::UInt32& faceIndex(size_t ith);
168 
173  agx::UInt32 const& faceIndex(size_t ith) const;
174 
179  agx::UInt8& faceFeature(size_t ith);
180 
185  agx::UInt8 const& faceFeature(size_t ith) const;
186 
191  agx::Physics::Geometry::ShapePtr& shape(size_t ith);
192 
197  agx::Physics::Geometry::ShapePtr const& shape(size_t ith) const;
198 
207  agx::Vec3 getWitnessPoint(size_t ith) const;
208 
212  agx::Bool hasState( ContactPointState contactPointState ) const;
213 
220  void setIsHolonomic( agx::Bool isHolonomic );
221 
226  agx::Bool isHolonomic() const;
227  };
228 
229 
235  template<typename T1, typename T2>
236  void copyContactPoint(const T1& from, T2& to)
237  {
238  to.point() = from.point();
239  to.normal() = from.normal();
240  to.depth() = from.depth();
241  to.area() = from.area();
242 
243  to.velocity() = from.velocity();
244  to.enabled() = from.enabled();
245 
246  to.shape1() = from.shape1();
247  to.shape2() = from.shape2();
248 
249  to.faceIndex1() = from.faceIndex1();
250  to.faceIndex2() = from.faceIndex2();
251  to.faceFeature1() = from.faceFeature1();
252  to.faceFeature2() = from.faceFeature2();
253  to.state() = from.state();
254  }
255 
256 #ifndef SWIG
257  template void copyContactPoint(const ContactPoint&, LocalContactPoint&);
258  template void copyContactPoint(const LocalContactPoint&, ContactPoint&);
261 
262 
264 #endif
266 
271  {
272  public:
274 
275  public:
278  NO_CONTACT = 0,
279  IMPACT_STATE = 1,
280  CONTACT_STATE = 2,
281  SEPARATION_STATE = 3
282  };
283 
284  public:
288  GeometryContact();
289 
295 
299  void clear();
300 
308  agx::Vec3f calculateRelativeVelocity( size_t pointIndex = 0 ) const;
309 
317  static bool calculateSurfaceVelocity( const agxCollide::Geometry* geometry1, const agxCollide::Geometry* geometry2, LocalContactPoint& p );
318 
326  static bool calculateSurfaceVelocity( const agxCollide::Geometry* geometry1, const agxCollide::Geometry* geometry2, ContactPoint& p );
327 
332  agxCollide::Geometry* geometry( size_t ith );
333 
338  const agxCollide::Geometry* geometry( size_t ith ) const;
339 
345  agx::RigidBody* rigidBody( size_t ith );
346 
352  const agx::RigidBody* rigidBody( size_t ith ) const;
353 
355  ContactPointVector& points();
356 
358  const ContactPointVector& points() const;
359 
361  void* getCustomData();
362 
364  const void* getCustomData() const;
365 
370  void setCustomData( void* customData );
371 
378  agx::ContactMaterial* getMaterial();
379 
386  const agx::ContactMaterial* getMaterial() const;
387 
393  const agx::ContactMaterial* getMaterial( bool ) const;
394 
396  agx::UInt8 state() const;
397 
399  void removeContactPoint( ContactPoint& point );
400 
404  void setEnable(bool flag);
405 
414  int contains(agx::RigidBody * body) const;
415 
424  int contains(agxCollide::Geometry * geometry) const;
425 
429  bool isEnabled() const;
430 
437  agx::Bool hasImpactingPoints() const;
438 
439 
441 
442 
443 
448  void setGeometry( size_t ith, Geometry* geom );
449 
450  /***
451  When called, this contact should be removed before entering the solver
452  NOTE: Contacts are never removed, only marked as disabled
453  \param immediately - Should the contact be removed immediately
454  */
455  void markForRemoval( bool immediately );
456 
462  bool shouldBeCalled() const;
463 
467  void setMaterial( agx::ContactMaterial* material );
468 
469  void setHasSurfaceVelocity( bool flag );
470 
471  GeometryPair getGeometryPair();
472 
473  void setHasInternalMaterial( bool flag );
474  bool getHasInternalMaterial( ) const;
475 
476  void setState( agx::UInt8 state );
478  };
479 
480 
481 
482  inline int GeometryContact::contains(agx::RigidBody * body) const
483  {
484  if (this->rigidBody(0) == body) return 0;
485  if (this->rigidBody(1) == body) return 1;
486  return -1;
487  }
488 
489  inline int GeometryContact::contains(agxCollide::Geometry * geometry) const
490  {
491  if (this->geometry(0) == geometry) return 0;
492  if (this->geometry(1) == geometry) return 1;
493  return -1;
494  }
495 
496 
497 
498 #ifndef SWIG
499  //AGX_STATIC_ASSERT(sizeof(GeometryContact) == sizeof(agx::Physics::GeometryContactPtr));
500 #endif
501 
502  /* Implementation */
503  inline ContactPoint::ContactPoint() : agx::Physics::ContactPointPtr( agx::Physics::ContactPointModel::createInstance() )
504  {
505  }
506 
508  {
509  }
510 
511 
513  {
514  depth() = agx::Real( 0 );
515  enabled() = true;
516  point().set(0, 0, 0);
517  normal().set(0, 0, 0);
518  state() = agx::UInt8( 0 );
519  tangentU().set(0, 0, 0);
520  tangentV().set(0, 0, 0);
521  localForce().set( 0, 0, 0 );
522  velocity().set(0, 0, 0);
523  faceIndex1() = 0;
524  faceIndex2() = 0;
525  faceFeature1() = 0;
526  faceFeature2() = 0;
527  area() = agx::Real( 0 );
530  }
531 
532 
534  {
535  agxAssert(idx < 3);
536  return localForce()[idx];
537  }
538 
539 
541  {
542  agxAssert(idx < 3);
543  return localForce()[idx];
544  }
545 
546 
547 
549  {
550  return getNormalForce() + getTangentialForce();
551  }
552 
553 
555  {
556  agx::Real sqr =
560  return sqr > 0 ? std::sqrt( sqr ) : 0;
561  }
562 
563 
565  {
566  return agx::Vec3(normal()) * localForce()[ NORMAL_FORCE ];
567  }
568 
569 
571  {
572  return std::abs(localForce()[ NORMAL_FORCE ]);
573  }
574 
575 
577  {
578  const agx::Vec3 forceU = agx::Vec3(tangentU()) * localForce()[ TANGENTIAL_FORCE_U ];
579  const agx::Vec3 forceV = agx::Vec3(tangentV()) * localForce()[ TANGENTIAL_FORCE_V ];
580  return forceU + forceV;
581  }
582 
583 
585  {
586  return std::abs(localForce()[ TANGENTIAL_FORCE_U ]);
587  }
588 
589 
591  {
592  return std::abs(localForce()[ TANGENTIAL_FORCE_V ]);
593  }
594 
595 
597  {
599  return sqr > 0 ? std::sqrt( sqr ) : 0;
600  }
601 
603  {
604  return ( state() & contactPointState ) != 0;
605  }
606 
608  {
609  return !hasState( NONHOLONOMIC );
610  }
611 
613  {
614  if ( isHolonomic )
615  {
616  // remove non-holonomic bit
617  state() = (agx::UInt8)( state() & ~NONHOLONOMIC );
618  }
619  else
620  {
621  // add non-holonomic bit
622  state() |= ( agx::UInt8 )( NONHOLONOMIC );
623  }
624  }
625 
627  {
628 
629  }
630 
632  agx::Physics::GeometryContactPtr(contact)
633 
634  {
635  }
636 
638  {
639  agxAssert(ith < 2);
640  if(ith == 0)
641  return geometry1().model();
642  else
643  return geometry2().model();
644  }
645 
647  {
648  agxAssert(ith < 2);
649  if(ith == 0)
650  return geometry1().model();
651  else
652  return geometry2().model();
653  }
654 
655  AGX_FORCE_INLINE GeometryPair GeometryContact::getGeometryPair()
656  {
657  return GeometryPair(geometry(0), geometry(1));
658  }
659 
660  AGX_FORCE_INLINE void GeometryContact::setGeometry( size_t ith, Geometry* geom )
661  {
662  agxAssert(ith < 2 );
663  if(ith == 0)
664  geometry1() = geom->getEntity();
665  else
666  geometry2() = geom->getEntity();
667  }
668 
670  {
671  agx::Vec3 v1, v2;
672  if (pointIndex > points().size() - 1)
673  return agx::Vec3f();
674 
675  const agx::RigidBody* rb1 = geometry(0)->getRigidBody();
676  const agx::RigidBody* rb2 = geometry(1)->getRigidBody();
677 
678  if (rb1)
679  v1 = rb1->getVelocity() + (rb1->getAngularVelocity() ^ (points()[pointIndex].point() - rb1->getCmPosition()));
680  if (rb2)
681  v2 = rb2->getVelocity() + (rb2->getAngularVelocity() ^ (points()[pointIndex].point() - rb2->getCmPosition()));
682 
683  return static_cast<agx::Vec3f>(v1 - v2);
684  }
685 
686 
688  {
689  LocalContactPoint lcp;
690  lcp.point() = p.point();
691  bool hasSurfaceVelocity = calculateSurfaceVelocity(geometry1, geometry2, lcp);
692  p.velocity() = lcp.velocity();
693  return hasSurfaceVelocity;
694  }
695 
696 
698  {
699  agxAssert(ith < 2 );
700  return geometry(ith)->getRigidBody();
701  }
702 
704  {
705  agxAssert(ith < 2 );
706  return geometry(ith)->getRigidBody();
707  }
708 
709 
711  {
712  return reinterpret_cast<ContactPointVector&>(agx::Physics::GeometryContactPtr::points());
713  }
714 
715  AGX_FORCE_INLINE const ContactPointVector& GeometryContact::points() const
716  {
717  return reinterpret_cast<const ContactPointVector&>(agx::Physics::GeometryContactPtr::points());
718  }
719 
721  {
722  return customData();
723  }
724 
726  {
727  return customData();
728  }
729 
731  {
732  this->customData() = customData;
733  }
734 
735 
736  AGX_FORCE_INLINE void GeometryContact::markForRemoval( bool immediately )
737  {
738  this->immediately() = immediately;
739  this->enabled() = false;
740  }
741 
743  {
744  this->enabled() = flag;
745  }
746 
748  {
749  return this->enabled();
750  }
751 
752 
753  AGX_FORCE_INLINE bool GeometryContact::shouldBeCalled() const
754  {
755  return !immediately();
756  }
757 
759  {
760  if ( material() && material().isExplicit() )
761  return material().model();
762  return nullptr;
763  }
764 
766  {
767  if ( material() && material().isExplicit() )
768  return material().model();
769  return nullptr;
770  }
771 
773  {
774  return material() ? material().model() : nullptr;
775  }
776 
777  AGX_FORCE_INLINE void GeometryContact::setMaterial( agx::ContactMaterial* material )
778  {
779  agxAssert( material != nullptr );
780  if (material)
781  this->material() = material->getEntity();
782  }
783 
784  AGX_FORCE_INLINE void GeometryContact::setHasSurfaceVelocity( bool flag )
785  {
786  hasSurfaceVelocity() = flag;
787  }
788 
789 
790  AGX_FORCE_INLINE void GeometryContact::setHasInternalMaterial( bool flag )
791  {
792  hasInternalMaterial() = flag;
793  }
794 
795  AGX_FORCE_INLINE bool GeometryContact::getHasInternalMaterial( ) const
796  {
797  return hasInternalMaterial();
798  }
799 
801  {
802  if ( broadPhasePair() )
803  return broadPhasePair().state();
804 
806  }
807 
808  AGX_FORCE_INLINE void GeometryContact::setState( agx::UInt8 state )
809  {
811  broadPhasePair().state() = state;
812  }
813 
815  {
816  if (ith == 0) return faceIndex1(); else return faceIndex2();
817  }
818 
819 
821  {
822  if (ith == 0) return faceIndex1(); else return faceIndex2();
823  }
824 
825 
827  {
828  if (ith == 0) return faceFeature1(); else return faceFeature2();
829  }
830 
831 
833  {
834  if (ith == 0) return faceFeature1(); else return faceFeature2();
835  }
836 
837 
839  {
840  if (ith == 0) return shape1(); else return shape2();
841  }
842 
843 
845  {
846  if (ith == 0) return shape1(); else return shape2();
847  }
848 
849 
851  {
852  if (ith == 0)
853  return point() - agx::Vec3(normal()) * (depth() * agx::Real(0.5));
854  else
855  return point() + agx::Vec3(normal()) * (depth() * agx::Real(0.5));
856  }
857 }
858 
859 #ifdef _MSC_VER
860 # pragma warning(pop)
861 #endif
862 
This class store the combine material properties between two agx::Material&#39;s.
Definition: Material.h:515
AGXTERRAIN_EXPORT void clear()
agx::Real getTangentialForceVMagnitude() const
Definition: Contacts.h:590
agx::UInt8 & faceFeature(size_t ith)
Definition: Contacts.h:826
Vec3T< Real > Vec3
The object holding 3 dimensional vectors and providing basic arithmetic.
Definition: Vec3.h:36
Pointer to a entity instance of type Physics.ContactPoint.
AGXPHYSICS_EXPORT agx::Physics::GeometryPtr & geometry2()
AGXPHYSICS_EXPORT agx::Physics::GeometryPtr & geometry1()
Tangential force V, orthogonal to the normal force and U.
Definition: Contacts.h:62
agx::Physics::Geometry::ShapePtr & shape(size_t ith)
Definition: Contacts.h:838
bool Bool
Definition: Integer.h:48
agx::SymmetricPair< agx::Physics::GeometryPtr > GeometryPair
Definition: Geometry.h:66
This namespace consists of a set of classes for handling geometric intersection tests including boole...
AGXPHYSICS_EXPORT agx::Vec3f & normal()
agxData::Array< ContactPoint > ContactPointVector
Definition: Contacts.h:265
A contact between two geometries.
Definition: Contacts.h:270
agx::Bool isHolonomic() const
Definition: Contacts.h:607
uint8_t UInt8
Definition: Integer.h:38
agx::Real getForceMagnitude() const
Definition: Contacts.h:554
#define AGXPHYSICS_EXPORT
Pointer to a entity instance of type Physics.Geometry.Shape.
Definition: ShapeEntity.h:195
AGXPHYSICS_EXPORT agx::UInt8 & faceFeature2()
The face feature of shape 2 in contact (e.g. voronoi region on triangle for trimesh).
AGXPHYSICS_EXPORT agx::Vec3f & tangentV()
agx::Vec3 getVelocity() const
Velocity of center of mass frame origin, in world coordinate frame.
Definition: RigidBody.h:1485
template void copyContactPoint(const agx::Physics::ContactPointPtr &, LocalContactPoint &)
#define AGX_FORCE_INLINE
Definition: Object.h:59
AGXPHYSICS_EXPORT agx::Bool & enabled()
AGXPHYSICS_EXPORT agx::Vec3f & tangentU()
AGXPHYSICS_EXPORT agx::Physics::Geometry::ShapePtr & shape2()
Shape number 2 in contact.
AGXPHYSICS_EXPORT agx::Vec3f & velocity()
ContactState
Specify the state of the contact.
Definition: Contacts.h:277
AGXPHYSICS_EXPORT agx::Physics::ContactMaterialPtr & material()
agx::Real getTangentialForceUMagnitude() const
Definition: Contacts.h:584
Tangential force U, orthogonal to the normal force and V.
Definition: Contacts.h:61
Type-specific Array used for fast access into the data held by a Buffer.
Definition: agxData.h:30
AGXPHYSICS_EXPORT agx::ContactMaterial *& model()
agx::UInt32 & faceIndex(size_t ith)
Definition: Contacts.h:814
The geometry representation used by the collision detection engine.
Definition: Geometry.h:92
agx::Vec3 getCmPosition() const
Definition: RigidBody.h:1500
void set(T x, T y, T z)
Definition: Vec3Template.h:459
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
AGXPHYSICS_EXPORT agx::Physics::BroadPhasePairPtr & broadPhasePair()
agx::UInt8 state() const
Definition: Contacts.h:800
agx::Vec3 getAngularVelocity() const
Angular velocity in world coordinate frame.
Definition: RigidBody.h:1490
agx::Real getTangentialForceMagnitude() const
Definition: Contacts.h:596
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...
AGXPHYSICS_EXPORT agx::UInt8 & state()
Contact point state, such as impacting, resting etc.
GeometryContact()
Default constructor.
Definition: Contacts.h:626
agx::Vec3 getWitnessPoint(size_t ith) const
Get witness point for ith geometry.
Definition: Contacts.h:850
agx::Vec3 getNormalForce() const
Definition: Contacts.h:564
Vec3T< Real32 > Vec3f
Definition: Vec3.h:39
#define agxAssert(expr)
Definition: debug.h:143
agx::Vec3 getForce() const
Definition: Contacts.h:548
AGXPHYSICS_EXPORT agx::Vec3 & localForce()
void clear()
Clear all values in this contact.
Definition: Contacts.h:512
agx::Vec3f calculateRelativeVelocity(size_t pointIndex=0) const
Calculate the relative velocity in a contact point.
Definition: Contacts.h:669
The rigid body class, combining a geometric model and a frame of reference.
Definition: RigidBody.h:54
void setCustomData(void *customData)
Store custom data for this Contact.
Definition: Contacts.h:730
agx::Vec3 getTangentialForce() const
Definition: Contacts.h:576
AGXPHYSICS_EXPORT agx::Vec3 & point()
AGXPHYSICS_EXPORT agx::UInt8 & state()
A geometry contact hold contact information between two geometries.
AGXPHYSICS_EXPORT ContactPointPtr()
This contact point is treated as a non-holonomic constraint in the normal direction - i...
Definition: Contacts.h:73
AGXPHYSICS_EXPORT agx::Bool & immediately()
AGXPHYSICS_EXPORT agxCollide::Geometry *& model()
agx::ContactMaterial * getMaterial()
If the contact material for this geometry contact is explicit it can be accessed here.
Definition: Contacts.h:758
void setIsHolonomic(agx::Bool isHolonomic)
Set if contact point should be treated as a holonomic constraint ( regular contact constraint with de...
Definition: Contacts.h:612
AGXPHYSICS_EXPORT agx::Real & depth()
AGXPHYSICS_EXPORT agx::UInt8 & faceFeature1()
The face feature of shape 1 in contact (e.g. voronoi region on triangle for trimesh).
A contact point in a contact data.
Definition: Contacts.h:55
AGXPHYSICS_EXPORT agxData::Array< agx::Physics::ContactPointPtr > & points()
The list of contact points.
agx::RigidBody * rigidBody(size_t ith)
Returns access to rigid body data.
Definition: Contacts.h:697
AGXPHYSICS_EXPORT agx::Bool & enabled()
AGXPHYSICS_EXPORT agx::Bool & hasSurfaceVelocity()
agx::Bool hasState(ContactPointState contactPointState) const
Definition: Contacts.h:602
AGXPHYSICS_EXPORT agx::Physics::Geometry::ShapePtr & shape1()
Shape number 1 in contact.
agx::Real getNormalForceMagnitude() const
Definition: Contacts.h:570
double Real
Definition: Real.h:42
void setEnable(bool flag)
Set to false to disable the contact.
Definition: Contacts.h:742
AGXPHYSICS_EXPORT agx::Real & area()
agx::Physics::GeometryPtr getEntity() const
Definition: Geometry.h:861
AGXPHYSICS_EXPORT agx::UInt32 & faceIndex2()
The face index of shape 2 in contact (e.g. index of triangle for trimesh).
The agxSDK namespace contain classes to bridge the collision detection system and the dynamical simul...
Definition: Constraint.h:30
agxCollide::Geometry * geometry(size_t ith)
Definition: Contacts.h:637
ContactPointVector & points()
Definition: Contacts.h:710
ContactPoint()
Default constructor.
Definition: Contacts.h:503
uint32_t UInt32
Definition: Integer.h:40
AGXPHYSICS_EXPORT agx::UInt32 & faceIndex1()
The face index of shape 1 in contact (e.g. index of triangle for trimesh).
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition: Object.h:98
AGXPHYSICS_EXPORT void *& customData()
AGXPHYSICS_EXPORT agx::Bool & hasInternalMaterial()
agx::RigidBody * getRigidBody()
Definition: Geometry.h:865
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition: Object.h:97
AGX_STATIC_ASSERT(sizeof(ContactPoint)==sizeof(agx::Physics::ContactPointPtr))