Simbody  3.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Contact.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_CONTACT_H_
2 #define SimTK_SIMMATH_CONTACT_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2008-12 Stanford University and the Authors. *
13  * Authors: Peter Eastman, Michael Sherman *
14  * Contributors: *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "SimTKcommon.h"
29 
30 namespace SimTK {
31 
32 
37 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactSurfaceIndex);
38 
49 
56 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactTypeId);
57 
58 
59 class ContactImpl;
60 class UntrackedContactImpl;
61 class BrokenContactImpl;
62 class CircularPointContactImpl;
63 class EllipticalPointContactImpl;
64 class TriangleMeshContactImpl;
65 class PointContactImpl;
66 
67 
68 //==============================================================================
69 // CONTACT
70 //==============================================================================
82 public:
85  enum Condition {
91  Broken
92  };
96  static const char* nameOfCondition(Condition);
97 
99  Contact() : impl(0) {}
102  Contact(const Contact& source);
105  ~Contact() {clear();}
108  Contact& operator=(const Contact& source);
111  void clear();
113  bool isEmpty() const {return impl==0;}
114 
118  ContactId getContactId() const;
120  Condition getCondition() const;
123  ContactSurfaceIndex getSurface1() const;
126  ContactSurfaceIndex getSurface2() const;
130  const Transform& getTransform() const;
131 
134  Contact& setContactId(ContactId id);
136  Contact& setCondition(Condition condition);
138  Contact& setSurfaces(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2);
140  Contact& setTransform(const Transform& X_S1S2);
141 
144  ContactTypeId getTypeId() const;
145 
149  static ContactId createNewContactId();
150 
151  const ContactImpl& getImpl() const {assert(impl); return *impl;}
152  ContactImpl& updImpl() {assert(impl); return *impl;}
153 protected:
154  explicit Contact(ContactImpl* impl);
155 private:
156  ContactImpl* impl;
157 };
158 
159 inline std::ostream& operator<<(std::ostream& o, const Contact& c) {
160  o << "Contact id=" << c.getContactId()
161  << " (typeId=" << c.getTypeId() << "):\n";
162  o << " surf1,surf2=" << c.getSurface1() << ","
163  << c.getSurface2() << "\n";
164  o << " condition=" << Contact::nameOfCondition(c.getCondition()) << "\n";
165  return o;
166 }
167 
168 
169 
170 //==============================================================================
171 // UNTRACKED CONTACT
172 //==============================================================================
179 public:
188 
190  static bool isInstance(const Contact& contact);
192  static ContactTypeId classTypeId();
193 
194 private:
195  const UntrackedContactImpl& getImpl() const
196  { assert(isInstance(*this));
197  return reinterpret_cast<const UntrackedContactImpl&>
198  (Contact::getImpl()); }
199 };
200 
201 
202 
203 //==============================================================================
204 // BROKEN CONTACT
205 //==============================================================================
213 public:
221  const Transform& X_S1S2, Real separation);
222 
226  Real getSeparation() const;
227 
229  static bool isInstance(const Contact& contact);
231  static ContactTypeId classTypeId();
232 
233 private:
234  const BrokenContactImpl& getImpl() const
235  { assert(isInstance(*this));
236  return reinterpret_cast<const BrokenContactImpl&>(Contact::getImpl()); }
237 };
238 
239 
240 
241 //==============================================================================
242 // CIRCULAR POINT CONTACT
243 //==============================================================================
257 public:
273  (ContactSurfaceIndex surf1, Real radius1,
274  ContactSurfaceIndex surf2, Real radius2,
275  const Transform& X_S1S2, Real radius, Real depth,
276  const Vec3& origin_S1, const UnitVec3& normal_S1);
277 
279  Real getRadius1() const;
281  Real getRadius2() const;
284  Real getEffectiveRadius() const;
289  Real getDepth() const;
291  const Vec3& getOrigin() const;
295  const UnitVec3& getNormal() const;
296 
298  static bool isInstance(const Contact& contact);
299  static const CircularPointContact& getAs(const Contact& contact)
300  { assert(isInstance(contact));
301  return static_cast<const CircularPointContact&>(contact); }
302  static CircularPointContact& updAs(Contact& contact)
303  { assert(isInstance(contact));
304  return static_cast<CircularPointContact&>(contact); }
305 
307  static ContactTypeId classTypeId();
308 
309 private:
310  const CircularPointContactImpl& getImpl() const
311  { assert(isInstance(*this));
312  return reinterpret_cast<const CircularPointContactImpl&>
313  (Contact::getImpl()); }
314 };
315 
316 
317 
318 //==============================================================================
319 // ELLIPTICAL POINT CONTACT
320 //==============================================================================
352 public:
368  const Transform& X_S1S2,
369  const Transform& X_S1C, const Vec2& k, Real depth);
370 
373  const Vec2& getCurvatures() const;
380  const Transform& getContactFrame() const;
385  Real getDepth() const;
386 
388  static bool isInstance(const Contact& contact);
389  static const EllipticalPointContact& getAs(const Contact& contact)
390  { assert(isInstance(contact));
391  return static_cast<const EllipticalPointContact&>(contact); }
392  static EllipticalPointContact& updAs(Contact& contact)
393  { assert(isInstance(contact));
394  return static_cast<EllipticalPointContact&>(contact); }
395 
397  static ContactTypeId classTypeId();
398 
399 private:
400  const EllipticalPointContactImpl& getImpl() const
401  { assert(isInstance(*this));
402  return reinterpret_cast<const EllipticalPointContactImpl&>
403  (Contact::getImpl()); }
404 };
405 
406 
407 
408 
409 //==============================================================================
410 // TRIANGLE MESH CONTACT
411 //==============================================================================
416 public:
429  ContactSurfaceIndex surf2,
430  const Transform& X_S1S2,
431  const std::set<int>& faces1,
432  const std::set<int>& faces2);
433 
437  const std::set<int>& getSurface1Faces() const;
441  const std::set<int>& getSurface2Faces() const;
442 
444  static bool isInstance(const Contact& contact);
447  static const TriangleMeshContact& getAs(const Contact& contact)
448  { assert(isInstance(contact));
449  return static_cast<const TriangleMeshContact&>(contact); }
452  static TriangleMeshContact& updAs(Contact& contact)
453  { assert(isInstance(contact));
454  return static_cast<TriangleMeshContact&>(contact); }
455 
458  static ContactTypeId classTypeId();
459 
460 private:
461  const TriangleMeshContactImpl& getImpl() const
462  { assert(isInstance(*this));
463  return reinterpret_cast<const TriangleMeshContactImpl&>
464  (Contact::getImpl()); }
465 };
466 
467 
468 
469 
470 //==============================================================================
471 // POINT CONTACT
472 //==============================================================================
480 public:
498  Vec3& location, Vec3& normal, Real radius1, Real radius2, Real depth);
515  Vec3& location, Vec3& normal, Real radius, Real depth);
521  Vec3 getLocation() const;
526  Vec3 getNormal() const;
530  Real getRadiusOfCurvature1() const;
534  Real getRadiusOfCurvature2() const;
539  Real getEffectiveRadiusOfCurvature() const;
545  Real getDepth() const;
549  static bool isInstance(const Contact& contact);
553  static ContactTypeId classTypeId();
554 
555 private:
556  const PointContactImpl& getImpl() const
557  { assert(isInstance(*this));
558  return reinterpret_cast<const PointContactImpl&>(Contact::getImpl()); }
559 };
560 
561 } // namespace SimTK
562 
563 #endif // SimTK_SIMMATH_CONTACT_H_