Moby
UnilateralConstraint.h
1 /****************************************************************************
2  * Copyright 2011 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef _MOBY_EVENT_H
8 #define _MOBY_EVENT_H
9 
10 #include <iostream>
11 #include <list>
12 #include <Ravelin/Vector3d.h>
13 #include <Ravelin/SForced.h>
14 #include <Ravelin/DynamicBodyd.h>
15 #include <boost/shared_ptr.hpp>
16 #include <Moby/Constants.h>
17 #include <Moby/Types.h>
18 #include <Moby/ContactParameters.h>
19 
20 namespace osg { class Node; }
21 
22 namespace Moby {
23 
24 class CollisionGeometry;
25 
28 {
29  public:
30  enum UnilateralConstraintType { eNone, eContact, eLimit };
31  enum UnilateralConstraintClass { ePositive, eZero, eNegative };
32  enum Compliance { eRigid, eCompliant};
34  UnilateralConstraint(const UnilateralConstraint& e) { _contact_frame = boost::shared_ptr<Ravelin::Pose3d>(new Ravelin::Pose3d); *this = e; }
35  static void determine_connected_constraints(const std::vector<UnilateralConstraint>& constraints, const std::vector<JointPtr>& implicit_joints, std::list<std::pair<std::list<UnilateralConstraint*>, std::list<boost::shared_ptr<Ravelin::SingleBodyd> > > >& groups, std::list<std::vector<boost::shared_ptr<Ravelin::DynamicBodyd> > >& remaining_islands);
36  static void remove_inactive_groups(std::list<std::pair<std::list<UnilateralConstraint*>, std::list<boost::shared_ptr<Ravelin::SingleBodyd> > > >& groups);
37  UnilateralConstraint& operator=(const UnilateralConstraint& e);
38  double calc_contact_vel(const Ravelin::Vector3d& v) const;
39  double calc_constraint_vel() const;
40  double calc_constraint_tol() const;
41  UnilateralConstraintClass determine_constraint_class() const;
42  bool is_impacting() const { return determine_constraint_class() == eNegative; }
43  bool is_resting() const { return determine_constraint_class() == eZero; }
44  bool is_separating() const { return determine_constraint_class() == ePositive; }
45  void set_contact_parameters(const ContactParameters& cparams);
47  boost::shared_ptr<const Ravelin::Pose3d> get_pose() const { return GLOBAL; }
48  void compute_constraint_data(Ravelin::MatrixNd& M, Ravelin::VectorNd& q) const;
49  void compute_cross_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M) const;
50 
51  template <class OutputIterator>
52  OutputIterator get_super_bodies(OutputIterator begin) const;
53 
55  UnilateralConstraintType constraint_type;
56 
58  Compliance compliance;
59 
62 
63  // the contact stiffness (applicable only when contact is in compliant layer)
64  double contact_stiffness;
65 
66  // the contact damping (applicable only when contact is in compliant layer)
67  double contact_damping;
68 
69  // the limit stiffness (applicable only when limit is in compliant layer)
70  double limit_stiffness;
71 
72  // the limit damping (applicable only when limit is in compliant layer)
73  double limit_damping;
74 
77 
79  double limit_epsilon;
80 
82  unsigned limit_dof;
83 
86 
88  double limit_impulse;
89 
92 
94  Ravelin::Vector3d contact_normal;
95 
97  Ravelin::Vector3d contact_tan1;
98 
100  Ravelin::Vector3d contact_tan2;
101 
103 
108  Ravelin::SMomentumd contact_impulse;
109 
112 
115 
118 
121 
124 
127 
129  unsigned contact_NK;
130 
131  osg::Node* to_visualization_data() const;
132 
134  double tol;
135 
136  void write_vrml(const std::string& filename, double sphere_radius = 0.1, double normal_length = 1.0) const;
137 
138  private:
139  // structure for comparing pairs of doubles
140  struct DblComp
141  {
142  bool operator()(const std::pair<double, double>& a, const std::pair<double, double>& b) const
143  {
144  return (a.first < b.first - NEAR_ZERO && a.second < b.second - NEAR_ZERO);
145  }
146  };
147 
148  // static variables
149  boost::shared_ptr<Ravelin::Pose3d> _contact_frame;
150  static Ravelin::MatrixNd JJ, J, Jx, Jy, J1, J2, dJ1, dJ2, workM1, workM2;
151  static Ravelin::VectorNd v, workv, workv2;
152 
153  void compute_cross_contact_contact_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M) const;
154  void compute_cross_contact_contact_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M, boost::shared_ptr<Ravelin::DynamicBodyd> su) const;
155  void compute_cross_contact_contact_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M, boost::shared_ptr<Ravelin::DynamicBodyd> su, const Ravelin::MatrixNd& J) const;
156  void compute_cross_contact_limit_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M) const;
157  void compute_cross_limit_contact_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M) const;
158  void compute_cross_limit_limit_constraint_data(const UnilateralConstraint& e, Ravelin::MatrixNd& M) const;
159  static bool is_linked(const UnilateralConstraint& e1, const UnilateralConstraint& e2);
160  unsigned get_super_bodies(boost::shared_ptr<Ravelin::DynamicBodyd>& sb1, boost::shared_ptr<Ravelin::DynamicBodyd>& sb2) const;
161 
162  template <class BidirectionalIterator>
163  static void insertion_sort(BidirectionalIterator begin, BidirectionalIterator end);
164 }; // end class
165 
166 std::ostream& operator<<(std::ostream& out, const UnilateralConstraint& e);
167 
168 #include "UnilateralConstraint.inl"
169 
170 } // end namespace Moby
171 
172 #endif
173 
bool limit_upper
Whether the upper/lower limit is reached (for limit constraints)
Definition: UnilateralConstraint.h:85
void compute_constraint_data(Ravelin::MatrixNd &M, Ravelin::VectorNd &q) const
Computes the constraint data.
Definition: UnilateralConstraint.cpp:109
Ravelin::Vector3d contact_tan1
The first tangent direction to the contact normal.
Definition: UnilateralConstraint.h:97
double contact_epsilon
The coefficient of restitution (for contact constraints)
Definition: UnilateralConstraint.h:126
osg::Node * to_visualization_data() const
Makes a contact visualizable.
Definition: UnilateralConstraint.cpp:883
double contact_penalty_Kv
Penalty Method Interpenetration Speed.
Definition: UnilateralConstraint.h:123
Ravelin::Vector3d contact_normal
The vector pointing outward from the contact on the first body, in world coordinates (for contact con...
Definition: UnilateralConstraint.h:94
Ravelin::Vector3d contact_tan2
The second tangent direction to the contact normal.
Definition: UnilateralConstraint.h:100
UnilateralConstraint()
Creates an empty constraint.
Definition: UnilateralConstraint.cpp:49
double contact_penalty_Kp
Penalty Method Depth Penalty.
Definition: UnilateralConstraint.h:120
CollisionGeometryPtr contact_geom1
The collision geometries involved (for contact constraints)
Definition: UnilateralConstraint.h:111
double limit_impulse
Limit impulse magnitude (for limit constraints)
Definition: UnilateralConstraint.h:88
Class for storing contact modeling parameters between two bodies.
Definition: ContactParameters.h:17
void determine_contact_tangents()
Determines the set of contact tangents.
Definition: UnilateralConstraint.cpp:1396
void compute_cross_constraint_data(const UnilateralConstraint &e, Ravelin::MatrixNd &M) const
Updates the constraint data.
Definition: UnilateralConstraint.cpp:296
boost::shared_ptr< CollisionGeometry > CollisionGeometryPtr
Collision geometry smart pointer.
Definition: Types.h:77
static void remove_inactive_groups(std::list< std::pair< std::list< UnilateralConstraint * >, std::list< boost::shared_ptr< Ravelin::SingleBodyd > > > > &groups)
Removes groups of contacts that contain no active contacts.
Definition: UnilateralConstraint.cpp:1206
double calc_constraint_tol() const
Computes the constraint tolerance.
Definition: UnilateralConstraint.cpp:1462
double limit_epsilon
The coefficient of restitution for this limit.
Definition: UnilateralConstraint.h:79
double tol
Tolerance for the constraint (users never need to modify this)
Definition: UnilateralConstraint.h:134
boost::shared_ptr< Joint > JointPtr
Reduced-coordinate articulated body joint smart pointer.
Definition: Types.h:74
void write_vrml(const std::string &filename, double sphere_radius=0.1, double normal_length=1.0) const
Writes an constraint to the specified filename in VRML format for visualization.
Definition: UnilateralConstraint.cpp:1240
Ravelin::Vector3d Point3d
Typedef to distinguish between a 3D vector and a point.
Definition: Types.h:47
double contact_mu_coulomb
The coefficient of Coulomb friction (for contact constraints)
Definition: UnilateralConstraint.h:114
void set_contact_parameters(const ContactParameters &cparams)
Sets the contact parameters for this constraint.
Definition: UnilateralConstraint.cpp:638
static void determine_connected_constraints(const std::vector< UnilateralConstraint > &constraints, const std::vector< JointPtr > &implicit_joints, std::list< std::pair< std::list< UnilateralConstraint * >, std::list< boost::shared_ptr< Ravelin::SingleBodyd > > > > &groups, std::list< std::vector< boost::shared_ptr< Ravelin::DynamicBodyd > > > &remaining_islands)
Given a vector of constraints, determines all of the sets of connected constraints.
Definition: UnilateralConstraint.cpp:949
double contact_mu_viscous
The coefficient of viscous friction (for contact constraints)
Definition: UnilateralConstraint.h:117
double signed_violation
Signed violation for this constraint.
Definition: UnilateralConstraint.h:76
unsigned limit_dof
The DOF at which the limit is reached (for limit constraints)
Definition: UnilateralConstraint.h:82
Container class for describing a unilateral constraint in the simulation.
Definition: UnilateralConstraint.h:27
unsigned contact_NK
The number of friction directions >= 4 (for contact constraints)
Definition: UnilateralConstraint.h:129
Ravelin::SMomentumd contact_impulse
Impulse that has been applied (for contact constraints)
Definition: UnilateralConstraint.h:108
UnilateralConstraintClass determine_constraint_class() const
Determines the type of constraint.
Definition: UnilateralConstraint.cpp:1442
Point3d contact_point
The point contact (for contact constraints)
Definition: UnilateralConstraint.h:91
UnilateralConstraintType constraint_type
The type of constraint.
Definition: UnilateralConstraint.h:55
JointPtr limit_joint
The joint at which the limit is reached (for limit constraints)
Definition: UnilateralConstraint.h:61
Compliance compliance
Compliance of the constraint.
Definition: UnilateralConstraint.h:58
double calc_constraint_vel() const
Computes the velocity of this constraint.
Definition: UnilateralConstraint.cpp:704