1 #ifndef SimTK_SIMBODY_GENERAL_CONTACT_SUBSYSTEM_H_
2 #define SimTK_SIMBODY_GENERAL_CONTACT_SUBSYSTEM_H_
3 
4 /* -------------------------------------------------------------------------- *
5  *                               Simbody(tm)                                  *
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                                                     *
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 "SimTKmath.h"
28 #include "simbody/internal/common.h"
29 
30 namespace SimTK {
31 
32 class MultibodySystem;
33 class MobilizedBody;
34 class ContactGeometry;
35 
36 /**
37  * This class performs collision detection for use in contact modeling.  It manages sets
38  * of bodies that can potentially interact with each other.  At each time step, it identifies
39  * all the contacts between them.  It does not directly affect the behavior of the system
40  * in any way.  Instead, it simply provides a service that can be used by other classes
41  * to implement forces, events, constraints, etc. that are based on contacts between bodies.
42  *
43  * To use this class, first create a "contact set" by calling createContactSet().  A contact
44  * set is a group of bodies which can interact with each other.  If you create multiple contact
45  * sets, the bodies within each one will be checked for contacts with each other, but bodies in
46  * different sets will not interact.
47  *
48  * Next, add bodies to the contact set.  Each one is represented by a ContactGeometry object
49  * that describes its shape, the MoblizedBody it is attached to, and a Transform giving the location
50  * of the geometry in the MobilizedBody's reference frame.
51  *
52  * Finally, call getContacts() to get a list of all contacts which exist between bodies in a
53  * contact set.  Each Contact specifies two bodies that overlap, along with a description of the
54  * contact point, such as its location and normal vector.
55  */
56 
57 class SimTK_SIMBODY_EXPORT GeneralContactSubsystem : public Subsystem {
58 public:
59     GeneralContactSubsystem();
60     explicit GeneralContactSubsystem(MultibodySystem&);
61     /**
62      * Create a new contact set.  The return value is an index which can be used to refer to the
63      * contact set when calling other methods.
64      */
65     ContactSetIndex createContactSet();
66     /**
67      * Get the total number of contact sets that have been created.
68      */
69     int getNumContactSets() const;
70     /**
71      * Add a body to a contact set.
72      *
73      * @param index     the index of the contact set the body should be added to
74      * @param body      the MobilizedBody whose reference frame the body is attached to
75      * @param geom      a ContactGeometry describing the shape of the body
76      * @param transform the location and orientation of the ContactGeometry in the MobilizedBody's reference frame
77      */
78     void addBody(ContactSetIndex index, const MobilizedBody& body, const ContactGeometry& geom, Transform transform);
79     /**
80      * Get the number of bodies in a contact set.
81      */
82     int getNumBodies(ContactSetIndex set) const;
83     /**
84      * Get the MobilizedBody in whose reference frame a body is defined.
85      *
86      * @param set    the contact set the body belongs to
87      * @param index  the index of the body within the contact set
88      */
89     const MobilizedBody& getBody(ContactSetIndex set, ContactSurfaceIndex index) const;
90     /**
91      * Get the ContactGeometry which defines the shape of a body.
92      *
93      * @param set    the contact set the body belongs to
94      * @param index  the index of the body within the contact set
95      */
96     const ContactGeometry& getBodyGeometry(ContactSetIndex set, ContactSurfaceIndex index) const;
97     /**
98      * Get a mutable reference to the ContactGeometry which defines the shape of a body.
99      *
100      * @param set    the contact set the body belongs to
101      * @param index  the index of the body within the contact set
102      */
103     ContactGeometry& updBodyGeometry(ContactSetIndex set, ContactSurfaceIndex index);
104     /**
105      * Get the location and orientation of a body.
106      *
107      * @param set    the contact set the body belongs to
108      * @param index  the index of the body within the contact set
109      */
110     const Transform& getBodyTransform(ContactSetIndex set, ContactSurfaceIndex index) const;
111     /**
112      * Get a mutable reference to the location and orientation of a body.
113      *
114      * @param set    the contact set the body belongs to
115      * @param index  the index of the body within the contact set
116      */
117     Transform& updBodyTransform(ContactSetIndex set, ContactSurfaceIndex index);
118     /**
119      * Get a list of all contacts between bodies in a contact set.  Contacts are calculated at
120      * Dynamics stage, so the state must have been realized to at least Dynamics stage.  This
121      * subsystem is guaranteed to be realized before all ForceSubsystems, however, so a Force
122      * may still invoke it to calculate forces based on contacts.
123      */
124     const Array_<Contact>& getContacts(const State& state, ContactSetIndex set) const;
125     SimTK_PIMPL_DOWNCAST(GeneralContactSubsystem, Subsystem);
126 private:
127     class GeneralContactSubsystemImpl& updImpl();
128     const GeneralContactSubsystemImpl& getImpl() const;
129 };
130 
131 } // namespace SimTK
132 
133 #endif // SimTK_SIMBODY_GENERAL_CONTACT_SUBSYSTEM_H_
134