Simbody

Contact.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMBODY_CONTACT_H_
00002 #define SimTK_SIMBODY_CONTACT_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                              SimTK Simbody(tm)                             *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK biosimulation toolkit originating from           *
00008  * Simbios, the NIH National Center for Physics-Based Simulation of           *
00009  * Biological Structures at Stanford, funded under the NIH Roadmap for        *
00010  * Medical Research, grant U54 GM072970. See https://simtk.org.               *
00011  *                                                                            *
00012  * Portions copyright (c) 2008-11 Stanford University and the Authors.        *
00013  * Authors: Peter Eastman                                                     *
00014  * Contributors: Michael Sherman                                              *
00015  *                                                                            *
00016  * Permission is hereby granted, free of charge, to any person obtaining a    *
00017  * copy of this software and associated documentation files (the "Software"), *
00018  * to deal in the Software without restriction, including without limitation  *
00019  * the rights to use, copy, modify, merge, publish, distribute, sublicense,   *
00020  * and/or sell copies of the Software, and to permit persons to whom the      *
00021  * Software is furnished to do so, subject to the following conditions:       *
00022  *                                                                            *
00023  * The above copyright notice and this permission notice shall be included in *
00024  * all copies or substantial portions of the Software.                        *
00025  *                                                                            *
00026  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
00027  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   *
00028  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    *
00029  * THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,    *
00030  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR      *
00031  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE  *
00032  * USE OR OTHER DEALINGS IN THE SOFTWARE.                                     *
00033  * -------------------------------------------------------------------------- */
00034 
00035 #include "SimTKcommon.h"
00036 
00037 #include "simbody/internal/common.h"
00038 
00039 namespace SimTK {
00040 
00041 
00046 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactSurfaceIndex);
00047 
00057 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactId);
00058 
00065 SimTK_DEFINE_UNIQUE_INDEX_TYPE(ContactTypeId);
00066 
00067 
00068 class ContactImpl;
00069 class UntrackedContactImpl;
00070 class BrokenContactImpl;
00071 class CircularPointContactImpl;
00072 class EllipticalPointContactImpl;
00073 class TriangleMeshContactImpl;
00074 class PointContactImpl;
00075 
00076 
00077 //==============================================================================
00078 //                                CONTACT
00079 //==============================================================================
00088 class SimTK_SIMBODY_EXPORT Contact {
00089 public:
00092     enum Condition {
00093         Unknown,    
00094         Untracked,  
00095         Anticipated,
00096         NewContact, 
00097         Ongoing,    
00098         Broken      
00099     };
00103     static const char* nameOfCondition(Condition);
00104 
00106     Contact() : impl(0) {}
00109     Contact(const Contact& source);
00112     ~Contact() {clear();}
00115     Contact& operator=(const Contact& source);
00118     void clear();
00120     bool isEmpty() const {return impl==0;}
00121 
00125     ContactId getContactId() const;
00127     Condition getCondition() const;
00130     ContactSurfaceIndex getSurface1() const;
00133     ContactSurfaceIndex getSurface2() const;
00137     const Transform& getTransform() const;
00138 
00141     Contact& setContactId(ContactId id);
00143     Contact& setCondition(Condition condition);
00145     Contact& setSurfaces(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2);
00147     Contact& setTransform(const Transform& X_S1S2);
00148 
00151     ContactTypeId getTypeId() const;
00152 
00156     static ContactId createNewContactId();
00157 
00158     const ContactImpl& getImpl() const {assert(impl); return *impl;}
00159     ContactImpl&       updImpl()       {assert(impl); return *impl;}
00160 protected:
00161     explicit Contact(ContactImpl* impl);
00162 private:
00163     ContactImpl* impl;
00164 };
00165 
00166 inline std::ostream& operator<<(std::ostream& o, const Contact& c) {
00167     o << "Contact id=" << c.getContactId() 
00168                        << " (typeId=" << c.getTypeId() << "):\n";
00169     o << "  surf1,surf2=" << c.getSurface1() << ","
00170                           << c.getSurface2() << "\n";
00171     o << "  condition=" << Contact::nameOfCondition(c.getCondition()) << "\n";
00172     return o;
00173 }
00174 
00175 
00176 
00177 //==============================================================================
00178 //                            UNTRACKED CONTACT
00179 //==============================================================================
00185 class SimTK_SIMBODY_EXPORT UntrackedContact : public Contact {
00186 public:
00188     UntrackedContact() {}
00194     UntrackedContact(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2); 
00195 
00197     static bool isInstance(const Contact& contact);
00199     static ContactTypeId classTypeId();
00200 
00201 private:
00202     const UntrackedContactImpl& getImpl() const 
00203     {   assert(isInstance(*this)); 
00204         return reinterpret_cast<const UntrackedContactImpl&>
00205                     (Contact::getImpl()); }
00206 };
00207 
00208 
00209 
00210 //==============================================================================
00211 //                            BROKEN CONTACT
00212 //==============================================================================
00219 class SimTK_SIMBODY_EXPORT BrokenContact : public Contact {
00220 public:
00227     BrokenContact(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2,
00228                   const Transform& X_S1S2, Real separation); 
00229 
00233     Real getSeparation() const;
00234 
00236     static bool isInstance(const Contact& contact);
00238     static ContactTypeId classTypeId();
00239 
00240 private:
00241     const BrokenContactImpl& getImpl() const 
00242     {   assert(isInstance(*this)); 
00243         return reinterpret_cast<const BrokenContactImpl&>(Contact::getImpl()); }
00244 };
00245 
00246 
00247 
00248 //==============================================================================
00249 //                           CIRCULAR POINT CONTACT
00250 //==============================================================================
00263 class SimTK_SIMBODY_EXPORT CircularPointContact : public Contact {
00264 public:
00279     CircularPointContact
00280        (ContactSurfaceIndex surf1, Real radius1, 
00281         ContactSurfaceIndex surf2, Real radius2, 
00282         const Transform& X_S1S2, Real radius, Real depth, 
00283         const Vec3& origin_S1, const UnitVec3& normal_S1);
00284 
00286     Real getRadius1() const;
00288     Real getRadius2() const;
00291     Real getEffectiveRadius() const;
00296     Real getDepth() const;
00298     const Vec3& getOrigin() const;
00302     const UnitVec3& getNormal() const;
00303 
00305     static bool isInstance(const Contact& contact);
00306     static const CircularPointContact& getAs(const Contact& contact)
00307     {   assert(isInstance(contact)); 
00308         return static_cast<const CircularPointContact&>(contact); }
00309     static CircularPointContact& updAs(Contact& contact)
00310     {   assert(isInstance(contact)); 
00311         return static_cast<CircularPointContact&>(contact); }
00312 
00314     static ContactTypeId classTypeId();
00315 
00316 private:
00317     const CircularPointContactImpl& getImpl() const 
00318     {   assert(isInstance(*this)); 
00319         return reinterpret_cast<const CircularPointContactImpl&>
00320                     (Contact::getImpl()); }
00321 };
00322 
00323 
00324 
00325 //==============================================================================
00326 //                           ELLIPTICAL POINT CONTACT
00327 //==============================================================================
00358 class SimTK_SIMBODY_EXPORT EllipticalPointContact : public Contact {
00359 public:
00373     EllipticalPointContact
00374        (ContactSurfaceIndex surf1, ContactSurfaceIndex surf2,
00375         const Transform& X_S1S2, 
00376         const Transform& X_S1C, const Vec2& k, Real depth); 
00377 
00380     const Vec2& getCurvatures() const;
00387     const Transform& getContactFrame() const;
00392     Real getDepth() const;
00393 
00395     static bool isInstance(const Contact& contact);
00396     static const EllipticalPointContact& getAs(const Contact& contact)
00397     {   assert(isInstance(contact)); 
00398         return static_cast<const EllipticalPointContact&>(contact); }
00399     static EllipticalPointContact& updAs(Contact& contact)
00400     {   assert(isInstance(contact)); 
00401         return static_cast<EllipticalPointContact&>(contact); }
00402 
00404     static ContactTypeId classTypeId();
00405 
00406 private:
00407     const EllipticalPointContactImpl& getImpl() const 
00408     {   assert(isInstance(*this)); 
00409         return reinterpret_cast<const EllipticalPointContactImpl&>
00410                     (Contact::getImpl()); }
00411 };
00412 
00413 
00414 
00415 
00416 //==============================================================================
00417 //                           TRIANGLE MESH CONTACT
00418 //==============================================================================
00422 class SimTK_SIMBODY_EXPORT TriangleMeshContact : public Contact {
00423 public:
00435     TriangleMeshContact(ContactSurfaceIndex     surf1, 
00436                         ContactSurfaceIndex     surf2,
00437                         const Transform&        X_S1S2,
00438                         const std::set<int>&    faces1, 
00439                         const std::set<int>&    faces2);
00440 
00444     const std::set<int>& getSurface1Faces() const;
00448     const std::set<int>& getSurface2Faces() const;
00449 
00451     static bool isInstance(const Contact& contact);
00454     static const TriangleMeshContact& getAs(const Contact& contact)
00455     {   assert(isInstance(contact)); 
00456         return static_cast<const TriangleMeshContact&>(contact); }
00459     static TriangleMeshContact& updAs(Contact& contact)
00460     {   assert(isInstance(contact)); 
00461         return static_cast<TriangleMeshContact&>(contact); }
00462 
00465     static ContactTypeId classTypeId();
00466 
00467 private:
00468     const TriangleMeshContactImpl& getImpl() const 
00469     {   assert(isInstance(*this)); 
00470         return reinterpret_cast<const TriangleMeshContactImpl&>
00471                     (Contact::getImpl()); }
00472 };
00473 
00474 
00475 
00476 
00477 //==============================================================================
00478 //                               POINT CONTACT
00479 //==============================================================================
00486 class SimTK_SIMBODY_EXPORT PointContact : public Contact {
00487 public:
00504     PointContact(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2, 
00505                  Vec3& location, Vec3& normal, Real radius1, Real radius2, Real depth);
00521     PointContact(ContactSurfaceIndex surf1, ContactSurfaceIndex surf2,
00522                  Vec3& location, Vec3& normal, Real radius, Real depth);
00528     Vec3 getLocation() const;
00533     Vec3 getNormal() const;
00537     Real getRadiusOfCurvature1() const;
00541     Real getRadiusOfCurvature2() const;
00546     Real getEffectiveRadiusOfCurvature() const;
00552     Real getDepth() const;
00556     static bool isInstance(const Contact& contact);
00560     static ContactTypeId classTypeId();
00561 
00562 private:
00563     const PointContactImpl& getImpl() const 
00564     {   assert(isInstance(*this)); 
00565         return reinterpret_cast<const PointContactImpl&>(Contact::getImpl()); }
00566 };
00567 
00568 } // namespace SimTK
00569 
00570 #endif // SimTK_SIMBODY_CONTACT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines