Constraint.h

Go to the documentation of this file.
00001 #ifndef SimTK_SIMBODY_CONSTRAINT_H_
00002 #define SimTK_SIMBODY_CONSTRAINT_H_
00003 
00004 /* -------------------------------------------------------------------------- *
00005  *                      SimTK Core: SimTK Simbody(tm)                         *
00006  * -------------------------------------------------------------------------- *
00007  * This is part of the SimTK Core 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) 2007-8 Stanford University and the Authors.         *
00013  * Authors: Michael Sherman                                                   *
00014  * Contributors:                                                              *
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 
00045 #include "SimTKmath.h"
00046 #include "simbody/internal/common.h"
00047 
00048 #include <cassert>
00049 
00050 namespace SimTK {
00051 
00052 class SimbodyMatterSubsystem;
00053 class SimbodyMatterSubtree;
00054 class MobilizedBody;
00055 class Constraint;
00056 class ConstraintImpl;
00057 
00058 // We only want the template instantiation to occur once. This symbol is defined in the SimTK core
00059 // compilation unit that defines the Constraint class but should not be defined any other time.
00060 #ifndef SimTK_SIMBODY_DEFINING_CONSTRAINT
00061     extern template class PIMPLHandle<Constraint, ConstraintImpl, true>;
00062 #endif
00063 
00065     // CONSTRAINT BASE CLASS //
00067 
00073 class SimTK_SIMBODY_EXPORT Constraint : public PIMPLHandle<Constraint, ConstraintImpl, true> {
00074 public:
00075     Constraint() { }
00076     explicit Constraint(ConstraintImpl* r) : HandleBase(r) { }
00077 
00078     void disable(State&) const;
00079     void enable(State&) const;
00080     bool isDisabled(const State&) const;
00081     bool isDisabledByDefault() const;
00082 
00086     void setDisabledByDefault(bool shouldBeDisabled);
00087 
00088     // Implicit conversion to ConstraintIndex when needed.
00089     operator ConstraintIndex() const {return getConstraintIndex();}
00090 
00091     // These will fail unless this Constraint is owned by a MatterSubsystem.
00092     ConstraintIndex               getConstraintIndex()      const;
00093     const SimbodyMatterSubsystem& getMatterSubsystem()      const;
00094     SimbodyMatterSubsystem&       updMatterSubsystem();
00095 
00096     bool isInSubsystem() const;
00097     bool isInSameSubsystem(const MobilizedBody&) const;
00098 
00099         // TOPOLOGY STAGE (i.e., post-construction) //
00100 
00106     int getNumConstrainedBodies() const;
00107 
00110     const MobilizedBody& getMobilizedBodyFromConstrainedBody(ConstrainedBodyIndex) const;
00111 
00116     const MobilizedBody& getAncestorMobilizedBody() const;
00117 
00122     int getNumConstrainedMobilizers() const;
00123 
00126     const MobilizedBody& getMobilizedBodyFromConstrainedMobilizer(ConstrainedMobilizerIndex) const;
00127 
00128     const SimbodyMatterSubtree& getSubtree() const;
00129 
00130         // MODEL STAGE //
00131 
00135     int getNumConstrainedQ(const State&, ConstrainedMobilizerIndex) const;
00139     int getNumConstrainedU(const State&, ConstrainedMobilizerIndex) const;
00140 
00147     ConstrainedUIndex getConstrainedUIndex
00148         (const State&, ConstrainedMobilizerIndex, MobilizerUIndex which) const;
00155     ConstrainedQIndex getConstrainedQIndex
00156         (const State&, ConstrainedMobilizerIndex, MobilizerQIndex which) const;
00157 
00160     int getNumConstrainedQ(const State&) const;
00161 
00166     int getNumConstrainedU(const State&) const;
00167     
00170     void getNumConstraintEquationsInUse(const State&, int& mp, int& mv, int& ma) const;
00171 
00172         // INSTANCE STAGE //
00173     // nothing in base class currently
00174 
00175         // POSITION STAGE //
00178     Vector getPositionErrorsAsVector(const State&) const;   // mp of these
00179     Vector calcPositionErrorFromQ(const State&, const Vector& q) const;
00180 
00181     // Matrix P = partial(perr_dot)/partial(u). (just the holonomic constraints)
00182     Matrix calcPositionConstraintMatrixP(const State&) const; // mp X nu
00183     Matrix calcPositionConstraintMatrixPt(const State&) const; // nu X mp
00184 
00185     // Matrix PQInv = partial(perr)/partial(q) = P*Q^-1
00186     Matrix calcPositionConstraintMatrixPQInverse(const State&) const; // mp X nq
00187 
00188     // This operator calculates this constraint's body and mobility forces
00189     // given the complete set of multipliers lambda. We expect that lambda
00190     // has been packed to include multipliers associated with the
00191     // second derivatives of the position
00192     // (holonomic) constraints, the first derivatives of the velocity
00193     // (nonholonomic) constraints, and the acceleration only constraints, in
00194     // that order.
00195     // The state must be realized already to Stage::Position. Returned body
00196     // forces correspond only to the *constrained bodies* and the mobility
00197     // forces correspond only to the *constrained mobilities*; they must 
00198     // be unpacked by the caller into the actual system mobilized bodies and 
00199     // actual system mobilities.
00200     // Note that the body forces are in the ancestor body frame A, not necessarily
00201     // the Ground frame G.
00202     void calcConstraintForcesFromMultipliers(const State&,
00203         const Vector&        lambda,                // mp+mv+ma of these
00204         Vector_<SpatialVec>& bodyForcesInA,         // numConstrainedBodies
00205         Vector&              mobilityForces) const; // numConstrainedU
00206 
00207         // VELOCITY STAGE //
00210     Vector getVelocityErrorsAsVector(const State&) const;   // mp+mv of these
00211     Vector calcVelocityErrorFromU(const State&,     // mp+mv of these
00212                                   const Vector& u) const;   // numParticipatingU u's
00213 
00214     // Matrix V = partial(verr)/partial(u) for just the non-holonomic constraints.
00215     Matrix calcVelocityConstraintMatrixV(const State&) const;  // mv X nu
00216     Matrix calcVelocityConstraintMatrixVt(const State&) const; // nu X mv
00217 
00218         // DYNAMICS STAGE //
00219     // nothing in base class currently
00220 
00221         // ACCELERATION STAGE //
00224     Vector getAccelerationErrorsAsVector(const State&) const;   // mp+mv+ma of these
00225     Vector calcAccelerationErrorFromUDot(const State&,  // mp+mv+ma of these
00226                                          const Vector& udot) const; // numParticipatingU udot's
00227 
00230     Vector getMultipliersAsVector(const State&) const;          // mp+mv+ma of these   
00231 
00232     // Matrix A = partial(aerr)/partial(udot) for just the acceleration-only constraints.
00233     Matrix calcAccelerationConstraintMatrixA(const State&) const;  // ma X nu
00234     Matrix calcAccelerationConstraintMatrixAt(const State&) const; // nu X ma
00235 
00236     // These are the built-in Constraint types. Types on the same line are
00237     // synonymous.
00238     class Rod;  typedef Rod  ConstantDistance;
00239     class Ball; typedef Ball CoincidentPoints;
00240     class Weld; typedef Weld CoincidentFrames;
00241     class PointInPlane;  // translations perpendicular to plane normal only
00242     class PointOnLine;   // translations along a line only
00243     class ConstantAngle; // prevent rotation about common normal of two vectors
00244     class ConstantOrientation; // allows any translation but no rotation
00245     class NoSlip1D; // same velocity at a point along a direction
00246     class ConstantSpeed; // prescribe generalized speed value
00247     class Custom;
00248     class CoordinateCoupler;
00249     class SpeedCoupler;
00250     class PrescribedMotion;
00251 
00252     class RodImpl;
00253     class BallImpl;
00254     class WeldImpl;
00255     class PointInPlaneImpl;
00256     class PointOnLineImpl;
00257     class ConstantAngleImpl;
00258     class ConstantOrientationImpl;
00259     class NoSlip1DImpl;
00260     class ConstantSpeedImpl;
00261     class CustomImpl;
00262     class CoordinateCouplerImpl;
00263     class SpeedCouplerImpl;
00264     class PrescribedMotionImpl;
00265 };
00266 
00268     // ROD (CONSTANT DISTANCE) CONSTRAINT //
00270 
00284  class SimTK_SIMBODY_EXPORT Constraint::Rod : public Constraint {
00285 public:
00286     // no default constructor
00287     Rod(MobilizedBody& body1, MobilizedBody& body2,
00288         Real defaultLength=1);
00289     Rod(MobilizedBody& body1, const Vec3& defaultPoint1,
00290         MobilizedBody& body2, const Vec3& defaultPoint2,
00291         Real defaultLength=1);
00292 
00293     // Defaults for Instance variables.
00294     Rod& setDefaultPointOnBody1(const Vec3&);
00295     Rod& setDefaultPointOnBody2(const Vec3&);
00296     Rod& setDefaultRodLength(Real);
00297 
00298     // Stage::Topology
00299     MobilizedBodyIndex getBody1MobilizedBodyIndex() const;
00300     MobilizedBodyIndex getBody2MobilizedBodyIndex() const;
00301     const Vec3& getDefaultPointOnBody1() const;
00302     const Vec3& getDefaultPointOnBody2() const;
00303     Real getDefaultRodLength() const;
00304 
00305     // Stage::Instance
00306     const Vec3& getPointOnBody1(const State&) const;
00307     const Vec3& getPointOnBody2(const State&) const;
00308     Real        getRodLength   (const State&) const;
00309 
00310     // Stage::Position, Velocity, Acceleration
00311     Real getPositionError(const State&) const;
00312     Real getVelocityError(const State&) const;
00313 
00314     // Stage::Acceleration
00315     Real getAccelerationError(const State&) const;
00316     Real getMultiplier(const State&) const;
00317     Real getRodTension(const State&) const; // negative means compression
00318     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(Rod, RodImpl, Constraint);
00319 };
00320 
00322     // POINT IN PLANE CONSTRAINT //
00324 
00335 class SimTK_SIMBODY_EXPORT Constraint::PointInPlane : public Constraint  {
00336 public:
00337     // no default constructor
00338     PointInPlane(MobilizedBody& planeBody_B, const UnitVec3& defaultPlaneNormal_B, Real defaultHeight,
00339                  MobilizedBody& followerBody_F, const Vec3& defaultFollowerPoint_F);
00340 
00341     // These affect only generated decorative geometry for visualization;
00342     // the plane is really infinite in extent with zero depth and the
00343     // point is really of zero radius.
00344     PointInPlane& setPlaneDisplayHalfWidth(Real);
00345     PointInPlane& setPointDisplayRadius(Real);
00346     Real getPlaneDisplayHalfWidth() const;
00347     Real getPointDisplayRadius() const;
00348 
00349     // Defaults for Instance variables.
00350     PointInPlane& setDefaultPlaneNormal(const UnitVec3&);
00351     PointInPlane& setDefaultPlaneHeight(Real);
00352     PointInPlane& setDefaultFollowerPoint(const Vec3&);
00353 
00354     // Stage::Topology
00355     MobilizedBodyIndex getPlaneMobilizedBodyIndex() const;
00356     MobilizedBodyIndex getFollowerMobilizedBodyIndex() const;
00357 
00358     const UnitVec3& getDefaultPlaneNormal() const;
00359     Real            getDefaultPlaneHeight() const;
00360     const Vec3&     getDefaultFollowerPoint() const;
00361 
00362     // Stage::Instance
00363     const UnitVec3& getPlaneNormal(const State&) const;
00364     Real            getPlaneHeight(const State&) const;
00365     const Vec3&     getFollowerPoint(const State&) const;
00366 
00367     // Stage::Position, Velocity
00368     Real getPositionError(const State&) const;
00369     Real getVelocityError(const State&) const;
00370 
00371     // Stage::Acceleration
00372     Real getAccelerationError(const State&) const;
00373     Real getMultiplier(const State&) const;
00374     Real getForceOnFollowerPoint(const State&) const; // in normal direction
00375     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(PointInPlane, PointInPlaneImpl, Constraint);
00376 };
00377 
00379     // POINT ON LINE CONSTRAINT //
00381 
00392 class SimTK_SIMBODY_EXPORT Constraint::PointOnLine : public Constraint  {
00393 public:
00394     // no default constructor
00395     PointOnLine(MobilizedBody& lineBody_B, const UnitVec3& defaultLineDirection_B, const Vec3& defaultPointOnLine_B,
00396                 MobilizedBody& followerBody_F, const Vec3& defaultFollowerPoint_F);
00397 
00398     // These affect only generated decorative geometry for visualization;
00399     // the line is really infinite in extent and the
00400     // point is really of zero radius.
00401     PointOnLine& setLineDisplayHalfLength(Real);
00402     PointOnLine& setPointDisplayRadius(Real);
00403     Real getLineDisplayHalfLength() const;
00404     Real getPointDisplayRadius() const;
00405 
00406     // Defaults for Instance variables.
00407     PointOnLine& setDefaultLineDirection(const UnitVec3&);
00408     PointOnLine& setDefaultPointOnLine(const Vec3&);
00409     PointOnLine& setDefaultFollowerPoint(const Vec3&);
00410 
00411     // Stage::Topology
00412     MobilizedBodyIndex getLineMobilizedBodyIndex() const;
00413     MobilizedBodyIndex getFollowerMobilizedBodyIndex() const;
00414 
00415     const UnitVec3& getDefaultLineDirection() const;
00416     const Vec3&     getDefaultPointOnLine() const;
00417     const Vec3&     getDefaultFollowerPoint() const;
00418 
00419     // Stage::Instance
00420     const UnitVec3& getLineDirection(const State&) const;
00421     const Vec3&     getPointOnLine(const State&) const;
00422     const Vec3&     getFollowerPoint(const State&) const;
00423 
00424     // Stage::Position, Velocity
00425     Vec2 getPositionErrors(const State&) const;
00426     Vec2 getVelocityErrors(const State&) const;
00427 
00428     // Stage::Acceleration
00429     Vec2 getAccelerationErrors(const State&) const;
00430     Vec2 getMultipliers(const State&) const;
00431     const Vec2& getForceOnFollowerPoint(const State&) const; // in normal direction
00432     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(PointOnLine, PointOnLineImpl, Constraint);
00433 };
00434 
00436     // CONSTANT ANGLE CONSTRAINT //
00438 
00452 class SimTK_SIMBODY_EXPORT Constraint::ConstantAngle : public Constraint {
00453 public:
00454     // no default constructor
00455     ConstantAngle(MobilizedBody& baseBody_B,     const UnitVec3& defaultAxis_B,
00456                   MobilizedBody& followerBody_F, const UnitVec3& defaultAxis_F, 
00457                   Real angle = Pi/2);
00458 
00459     // These affect only generated decorative geometry for visualization.
00460     ConstantAngle& setAxisDisplayLength(Real);
00461     ConstantAngle& setAxisDisplayWidth(Real);
00462     Real getAxisDisplayLength() const;
00463     Real getAxisDisplayWidth() const;
00464 
00465     // Defaults for Instance variables.
00466     ConstantAngle& setDefaultBaseAxis(const UnitVec3&);
00467     ConstantAngle& setDefaultFollowerAxis(const UnitVec3&);
00468     ConstantAngle& setDefaultAngle(Real);
00469 
00470     // Stage::Topology
00471     MobilizedBodyIndex getBaseMobilizedBodyIndex() const;
00472     MobilizedBodyIndex getFollowerMobilizedBodyIndex() const;
00473 
00474     const UnitVec3& getDefaultBaseAxis() const;
00475     const UnitVec3& getDefaultFollowerAxis() const;
00476     Real getDefaultAngle() const;
00477 
00478     // Stage::Instance
00479     const UnitVec3& getBaseAxis(const State&) const;
00480     const UnitVec3& getFollowerAxis(const State&) const;
00481     Real getAngle(const State&) const;
00482 
00483     // Stage::Position, Velocity
00484     Real getPositionError(const State&) const;
00485     Real getVelocityError(const State&) const;
00486 
00487     // Stage::Acceleration
00488     Real getAccelerationError(const State&) const;
00489     Real getMultiplier(const State&) const;
00490     Real getTorqueOnFollowerBody(const State&) const; // about f X b
00491     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(ConstantAngle, ConstantAngleImpl, Constraint);
00492 };
00493 
00495     // BALL (COINCIDENT POINTS) CONSTRAINT //
00497 
00511 class SimTK_SIMBODY_EXPORT Constraint::Ball : public Constraint {
00512 public:
00513     // no default constructor
00514     Ball(MobilizedBody& body1, MobilizedBody& body2);
00515     Ball(MobilizedBody& body1, const Vec3& defaultPoint1,
00516          MobilizedBody& body2, const Vec3& defaultPoint2);
00517 
00518     // Defaults for Instance variables.
00519     Ball& setDefaultPointOnBody1(const Vec3&);
00520     Ball& setDefaultPointOnBody2(const Vec3&);
00521 
00522     // This is used only for visualization.
00523     Ball& setDefaultRadius(Real r);
00524     Real getDefaultRadius() const;
00525 
00526     // Stage::Topology
00527     MobilizedBodyIndex getBody1MobilizedBodyIndex() const;
00528     MobilizedBodyIndex getBody2MobilizedBodyIndex() const;
00529     const Vec3& getDefaultPointOnBody1() const;
00530     const Vec3& getDefaultPointOnBody2() const;
00531 
00532     // Stage::Instance
00533     const Vec3& getPointOnBody1(const State&) const;
00534     const Vec3& getPointOnBody2(const State&) const;
00535 
00536     // Stage::Position, Velocity, Acceleration
00537     Vec3 getPositionErrors(const State&) const;
00538     Vec3 getVelocityErrors(const State&) const;
00539 
00540     // Stage::Acceleration
00541     Vec3 getAccelerationErrors(const State&) const;
00542     Vec3 getMultipliers(const State&) const;
00543 
00544     // Forces are reported expressed in the body frame of the indicated body.
00545     const Vec3& getBallReactionForceOnBody1(const State&) const;
00546     const Vec3& getBallReactionForceOnBody2(const State&) const;
00547     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(Ball, BallImpl, Constraint);
00548 };
00549 
00551     // CONSTANT ORIENTATION CONSTRAINT //
00553 
00570 class SimTK_SIMBODY_EXPORT Constraint::ConstantOrientation : public Constraint
00571 {
00572 public:
00573     // no default constructor
00574     ConstantOrientation(MobilizedBody& baseBody_B,     const Rotation& defaultRB,
00575                         MobilizedBody& followerBody_F, const Rotation& defaultRF); 
00576 
00577     //TODO: default visualization geometry?
00578 
00579     // Defaults for Instance variables.
00580     ConstantOrientation& setDefaultBaseRotation(const Rotation&);
00581     ConstantOrientation& setDefaultFollowerRotation(const Rotation&);
00582 
00583     // Stage::Topology
00584     MobilizedBodyIndex getBaseMobilizedBodyIndex() const;
00585     MobilizedBodyIndex getFollowerMobilizedBodyIndex() const;
00586 
00587     const Rotation& getDefaultBaseRotation() const;
00588     const Rotation& getDefaultFollowerRotation() const;
00589 
00590     // Stage::Instance
00591     const Rotation& getBaseRotation(const State&) const;
00592     const Rotation& getFollowerRotation(const State&) const;
00593 
00594     // Stage::Position, Velocity
00595     Vec3 getPositionErrors(const State&) const;
00596     Vec3 getVelocityErrors(const State&) const;
00597 
00598     // Stage::Acceleration
00599     Vec3 getAccelerationErrors(const State&) const;
00600     Vec3 getMultipliers(const State&) const;
00601     Vec3 getTorqueOnFollowerBody(const State&) const;
00602     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(ConstantOrientation, ConstantOrientationImpl, Constraint);
00603 };
00604 
00606     // WELD (COINCIDENT FRAMES) CONSTRAINT //
00608 
00632 class SimTK_SIMBODY_EXPORT Constraint::Weld : public Constraint {
00633 public:
00634         // no default constructor
00635 
00638     Weld(MobilizedBody& body1, MobilizedBody& body2);
00639 
00644     Weld(MobilizedBody& body1, const Transform& frame1,
00645          MobilizedBody& body2, const Transform& frame2);
00646 
00647         // Control over generated decorative geometry.
00648 
00652     Weld& setAxisDisplayLength(Real r);
00653 
00657     Real getAxisDisplayLength() const;
00658 
00659         // Defaults for Instance variables.
00660 
00664     Weld& setDefaultFrameOnBody1(const Transform&);
00665 
00667     const Transform& getDefaultFrameOnBody1() const;
00668 
00672     Weld& setDefaultFrameOnBody2(const Transform&);
00673 
00675     const Transform& getDefaultFrameOnBody2() const;
00676 
00677 
00678         // Stage::Topology
00679 
00681     MobilizedBodyIndex getBody1MobilizedBodyIndex() const;
00682 
00684     MobilizedBodyIndex getBody2MobilizedBodyIndex() const;
00685 
00686 
00687         // Stage::Instance
00688     const Transform& getFrameOnBody1(const State&) const;
00689     const Transform& getFrameOnBody2(const State&) const;
00690 
00691         // Stage::Position, Velocity, Acceleration
00692     Vec6 getPositionErrors(const State&) const;
00693     Vec6 getVelocityErrors(const State&) const;
00694 
00695         // Stage::Acceleration
00696     Vec6 getAccelerationErrors(const State&) const;
00697     Vec6 getMultipliers(const State&) const;
00698 
00699         // Forces are reported expressed in the body frame of the indicated body.
00700     const SpatialVec& getWeldReactionOnBody1(const State&) const;
00701     const SpatialVec& getWeldReactionOnBody2(const State&) const;
00702     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(Weld, WeldImpl, Constraint);
00703 };
00704 
00706     // NO SLIP 1D CONSTRAINT //
00708 
00720 class SimTK_SIMBODY_EXPORT Constraint::NoSlip1D : public Constraint {
00721 public:
00722     // no default constructor
00723     NoSlip1D(MobilizedBody& caseBodyC, const Vec3& P_C, const UnitVec3& n_C,
00724              MobilizedBody& movingBody0, MobilizedBody& movingBody1);
00725 
00726     // These affect only generated decorative geometry for visualization;
00727     // the plane is really infinite in extent with zero depth and the
00728     // point is really of zero radius.
00729     NoSlip1D& setDirectionDisplayLength(Real);
00730     NoSlip1D& setPointDisplayRadius(Real);
00731     Real getDirectionDisplayLength() const;
00732     Real getPointDisplayRadius() const;
00733 
00734     // Defaults for Instance variables.
00735     NoSlip1D& setDefaultDirection(const UnitVec3&);
00736     NoSlip1D& setDefaultContactPoint(const Vec3&);
00737 
00738     // Stage::Topology
00739     MobilizedBodyIndex getCaseMobilizedBodyIndex() const;
00740     MobilizedBodyIndex getMovingBodyMobilizedBodyIndex(int which) const;
00741 
00742     const UnitVec3& getDefaultDirection() const;
00743     const Vec3&     getDefaultContactPoint() const;
00744 
00745     // Stage::Instance
00746     const UnitVec3& getDirection(const State&) const;
00747     const Vec3&     getContactPoint(const State&) const;
00748 
00749     // Stage::Position, Velocity
00750         // no position error
00751     Real getVelocityError(const State&) const;
00752 
00753     // Stage::Acceleration
00754     Real getAccelerationError(const State&) const;
00755     Real getMultiplier(const State&) const;
00756     Real getForceAtContactPoint(const State&) const; // in normal direction, no body 2
00757     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(NoSlip1D, NoSlip1DImpl, Constraint);
00758 };
00759 
00761     // CONSTANT SPEED //
00763 
00770 class SimTK_SIMBODY_EXPORT Constraint::ConstantSpeed : public Constraint
00771 {
00772 public:
00773     // no default constructor
00774    ConstantSpeed(MobilizedBody& mobilizer, MobilizerUIndex, Real speed);
00775    ConstantSpeed(MobilizedBody& mobilizer, Real speed); // only if 1 dof mobilizer
00776 
00777     // Stage::Topology
00778     MobilizedBodyIndex getMobilizedBodyIndex() const;
00779     MobilizerUIndex    getWhichU() const;
00780     Real               getDefaultSpeed() const;
00781 
00782     // Stage::Position, Velocity
00783         // no position error
00784     Real getVelocityError(const State&) const;
00785 
00786     // Stage::Acceleration
00787     Real getAccelerationError(const State&) const;
00788     Real getMultiplier(const State&) const;
00789     Real getGeneralizedForce(const State&) const;
00790     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(ConstantSpeed, ConstantSpeedImpl, Constraint);
00791 };
00792 
00824 class SimTK_SIMBODY_EXPORT Constraint::Custom : public Constraint 
00825 {
00826 public:
00827     class Implementation;
00828     class ImplementationImpl;
00829 
00830     /* Create a Custom Constraint.
00831      * 
00832      * @param implementation the object which implements the custom constraint.  The Constraint::Custom takes over
00833      *                       ownership of the implementation object, and deletes it when the Constraint itself
00834      *                       is deleted.
00835      */
00836     explicit Custom(Implementation* implementation);
00837     SimTK_INSERT_DERIVED_HANDLE_DECLARATIONS(Custom, CustomImpl, Constraint);
00838 protected:
00839     const Implementation& getImplementation() const;
00840     Implementation&       updImplementation();
00841 };
00842 
00843 // We only want the template instantiation to occur once. This symbol is defined in the SimTK core
00844 // compilation unit that defines the Constraint class but should not be defined any other time.
00845 #ifndef SimTK_SIMBODY_DEFINING_CONSTRAINT
00846     extern template class PIMPLHandle<Constraint::Custom::Implementation, 
00847                                       Constraint::Custom::ImplementationImpl>;
00848 #endif
00849 
00850 class SimTK_SIMBODY_EXPORT Constraint::Custom::Implementation 
00851   : public PIMPLHandle<Implementation,ImplementationImpl> 
00852 {
00853 public:
00854     // No default constructor because you have to supply at least the SimbodyMatterSubsystem
00855     // to which this Constraint belongs.
00856 
00858     virtual ~Implementation() { }
00859 
00864     virtual Implementation* clone() const = 0;
00865 
00866 
00870     Implementation(SimbodyMatterSubsystem&, int mp, int mv, int ma);
00871 
00876     Implementation(SimbodyMatterSubsystem&);
00877 
00878     const SimbodyMatterSubsystem& getMatterSubsystem() const;
00879 
00880         // Topological information//
00881 
00888     void invalidateTopologyCache() const;
00889 
00894     Implementation& setDefaultNumConstraintEquations(int mp, int mv, int ma);
00895 
00900     Implementation& setDisabledByDefault(bool shouldBeDisabled);
00901 
00907     ConstrainedBodyIndex addConstrainedBody(const MobilizedBody&);
00908 
00914     ConstrainedMobilizerIndex addConstrainedMobilizer(const MobilizedBody&);
00915 
00916     MobilizedBodyIndex getMobilizedBodyIndexOfConstrainedBody(ConstrainedBodyIndex) const;
00917     MobilizedBodyIndex getMobilizedBodyIndexOfConstrainedMobilizer(ConstrainedMobilizerIndex) const;
00918 
00919         // Model stage information //
00920 
00921         // Methods for use with ConstrainedMobilizers.
00922 
00926     Real getOneQ(const State&, ConstrainedMobilizerIndex, MobilizerQIndex) const;
00932     Real getOneU(const State&, ConstrainedMobilizerIndex, MobilizerUIndex) const;
00933 
00938     Real getOneQDot   (const State&, ConstrainedMobilizerIndex, MobilizerQIndex, bool realizingVelocity=false) const;
00943     Real getOneQDotDot(const State&, ConstrainedMobilizerIndex, MobilizerQIndex, bool realizingAcceleration=false) const;
00948     Real getOneUDot(const State&, ConstrainedMobilizerIndex, MobilizerUIndex, bool realizingAcceleration=false) const;
00949 
00955     void addInOneMobilityForce(const State&, ConstrainedMobilizerIndex, MobilizerUIndex whichU,
00956                                Real force, Vector& mobilityForces) const;
00957 
00958         // Methods for use with ConstrainedBodies
00959 
00964     const Transform&  getBodyTransform(const State& s, ConstrainedBodyIndex B, bool realizingPosition=false)     const; // X_AB
00969     const SpatialVec& getBodyVelocity(const State& s, ConstrainedBodyIndex B, bool realizingVelocity=false)     const; // V_AB
00974     const SpatialVec& getBodyAcceleration(const State& s, ConstrainedBodyIndex B, bool realizingAcceleration=false) const; // A_AB
00975 
00976         // Extract just the rotational quantities from the spatial quantities above.
00977 
00982     const Rotation& getBodyRotation           (const State& s, ConstrainedBodyIndex B, bool realizingPosition=false) const
00983        {return getBodyTransform(s,B,realizingPosition).R();}   // R_AB
00988     const Vec3& getBodyAngularVelocity    (const State& s, ConstrainedBodyIndex B, bool realizingVelocity=false) const
00989        {return getBodyVelocity(s,B,realizingVelocity)[0];}     // w_AB
00994     const Vec3& getBodyAngularAcceleration(const State& s, ConstrainedBodyIndex B, bool realizingAcceleration=false) const
00995        {return getBodyAcceleration(s,B,realizingAcceleration)[0];} // b_AB
00996 
00997         // Extract just the translational (linear) quantities from the spatial quantities above.
00998 
01003     const Vec3& getBodyOriginLocation    (const State& s, ConstrainedBodyIndex B, bool realizingPosition=false) const
01004        {return getBodyTransform(s,B,realizingPosition).T();}   // p_AB
01009     const Vec3& getBodyOriginVelocity    (const State& s, ConstrainedBodyIndex B, bool realizingVelocity=false) const 
01010        {return getBodyVelocity(s,B,realizingVelocity)[1];}     // v_AB
01016     const Vec3& getBodyOriginAcceleration(const State& s, ConstrainedBodyIndex B, bool realizingAcceleration=false) const 
01017        {return getBodyAcceleration(s,B,realizingAcceleration)[1];} // a_AB
01018 
01019         // Calculate location, velocity, and acceleration for a given station.
01020 
01027     Vec3 findStationLocation(const State& s, ConstrainedBodyIndex B, const Vec3& p_BS, bool realizingPosition=false) const {
01028         return getBodyTransform(s,B,realizingPosition) * p_BS; // re-measure and re-express
01029     }
01036     Vec3 findStationVelocity(const State& s, ConstrainedBodyIndex B, const Vec3& p_BS, bool realizingVelocity=false) const {
01037         const Vec3        p_AS = getBodyRotation(s,B) * p_BS; // rexpressed but not shifted
01038         const SpatialVec& V_AB = getBodyVelocity(s,B,realizingVelocity);
01039         return V_AB[1] + (V_AB[0] % p_AS);
01040     }
01047     Vec3 findStationAcceleration(const State& s, ConstrainedBodyIndex B, const Vec3& p_BS, bool realizingAcceleration=false) const {
01048         const Vec3        p_AS = getBodyRotation(s,B) * p_BS; // rexpressed but not shifted
01049         const Vec3&       w_AB = getBodyAngularVelocity(s,B);
01050         const SpatialVec& A_AB = getBodyAcceleration(s,B,realizingAcceleration);
01051         return A_AB[1] + (A_AB[0] % p_AS) + w_AB % (w_AB % p_AS); // careful: cross product is not associative
01052     }
01053 
01054         // Utilities for applying constraint forces to ConstrainedBodies.
01055 
01059     void addInStationForce(const State& s, ConstrainedBodyIndex B, const Vec3& p_BS, 
01060                            const Vec3& forceInA, Vector_<SpatialVec>& bodyForcesInA) const;
01061 
01064     void addInBodyTorque(const State& s, ConstrainedBodyIndex B,
01065                          const Vec3& torqueInA, Vector_<SpatialVec>& bodyForcesInA) const;
01066 
01067 
01068 protected:
01077 
01079 
01080 
01081 
01082 
01083 
01084 
01085 
01086 
01087     virtual void realizeTopology(State&) const { }
01088 
01097     virtual void realizeModel(State&) const { }
01098 
01102     virtual void realizeInstance(const State&) const { }
01103 
01108     virtual void realizeTime(const State&) const { }
01109 
01116     virtual void realizePosition(const State&) const { }
01117 
01124     virtual void realizeVelocity(const State&) const { }
01125 
01130     virtual void realizeDynamics(const State&) const { }
01131 
01138     virtual void realizeAcceleration(const State&) const { }
01139 
01144     virtual void realizeReport(const State&) const { }
01146 
01153 
01154 
01155 
01156 
01157 
01158     virtual void realizePositionErrors(const State&, int mp,  Real* perr) const;
01159 
01164     virtual void realizePositionDotErrors(const State&, int mp,  Real* pverr) const;
01165 
01170     virtual void realizePositionDotDotErrors(const State&, int mp,  Real* paerr) const;
01171 
01184     virtual void applyPositionConstraintForces
01185        (const State&, int mp, const Real* multipliers,
01186         Vector_<SpatialVec>& bodyForces,
01187         Vector&              mobilityForces) const;
01189 
01192 
01193 
01194 
01195 
01196 
01197     virtual void realizeVelocityErrors(const State&, int mv,  Real* verr) const;
01198 
01203     virtual void realizeVelocityDotErrors(const State&, int mv,  Real* vaerr) const;
01204 
01218     virtual void applyVelocityConstraintForces
01219        (const State&, int mv, const Real* multipliers,
01220         Vector_<SpatialVec>& bodyForces,
01221         Vector&              mobilityForces) const;
01223 
01226 
01227 
01228 
01229 
01230 
01231     virtual void realizeAccelerationErrors(const State&, int ma,  Real* aerr) const;
01245     virtual void applyAccelerationConstraintForces
01246        (const State&, int ma, const Real* multipliers,
01247         Vector_<SpatialVec>& bodyForces,
01248         Vector&              mobilityForces) const;
01250 
01257     virtual void calcDecorativeGeometryAndAppend
01258        (const State& s, Stage stage, std::vector<DecorativeGeometry>& geom) const
01259     {
01260     }
01261 
01262     friend class Constraint::CustomImpl;
01263 };
01264 
01272 class SimTK_SIMBODY_EXPORT Constraint::CoordinateCoupler : public Constraint::Custom {
01273 public:
01286     CoordinateCoupler(SimbodyMatterSubsystem& matter, const Function<1>* function, const std::vector<MobilizedBodyIndex>& coordBody, const std::vector<MobilizerQIndex>& coordIndex);
01287 };
01288 
01300 class SimTK_SIMBODY_EXPORT Constraint::SpeedCoupler : public Constraint::Custom {
01301 public:
01314     SpeedCoupler(SimbodyMatterSubsystem& matter, const Function<1>* function, const std::vector<MobilizedBodyIndex>& speedBody, const std::vector<MobilizerUIndex>& speedIndex);
01331     SpeedCoupler(SimbodyMatterSubsystem& matter, const Function<1>* function, const std::vector<MobilizedBodyIndex>& speedBody, const std::vector<MobilizerUIndex>& speedIndex,
01332             const std::vector<MobilizedBodyIndex>& coordBody, const std::vector<MobilizerQIndex>& coordIndex);
01333 };
01334 
01341 class SimTK_SIMBODY_EXPORT Constraint::PrescribedMotion : public Constraint::Custom {
01342 public:
01353     PrescribedMotion(SimbodyMatterSubsystem& matter, const Function<1>* function, MobilizedBodyIndex coordBody, MobilizerQIndex coordIndex);
01354 };
01355 
01356 } // namespace SimTK
01357 
01358 #endif // SimTK_SIMBODY_CONSTRAINT_H_
01359 
01360 
01361 

Generated on Fri Sep 26 07:44:09 2008 for SimTKcore by  doxygen 1.5.6