MassSpringRepresentation.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_PHYSICS_MASSSPRINGREPRESENTATION_H
17 #define SURGSIM_PHYSICS_MASSSPRINGREPRESENTATION_H
18 
19 #include <memory>
20 
22 #include "SurgSim/Physics/Mass.h"
23 #include "SurgSim/Physics/Spring.h"
24 
25 #include "SurgSim/Math/Vector.h"
26 #include "SurgSim/Math/Matrix.h"
27 
28 namespace SurgSim
29 {
30 
31 namespace Physics
32 {
33 
39 {
40 public:
43  explicit MassSpringRepresentation(const std::string& name);
44 
46  virtual ~MassSpringRepresentation();
47 
52  void addMass(const std::shared_ptr<Mass> mass);
53 
56  void addSpring(const std::shared_ptr<Spring> spring);
57 
60  size_t getNumMasses() const;
61 
64  size_t getNumSprings() const;
65 
71  std::shared_ptr<Mass> getMass(size_t nodeId);
72 
78  std::shared_ptr<Spring> getSpring(size_t springId);
79 
82  double getTotalMass() const;
83 
86  double getRayleighDampingStiffness() const;
87 
90  double getRayleighDampingMass() const;
91 
94  void setRayleighDampingStiffness(double stiffnessCoef);
95 
98  void setRayleighDampingMass(double massCoef);
99 
102  virtual RepresentationType getType() const override;
103 
104  virtual void addExternalGeneralizedForce(std::shared_ptr<Localization> localization,
105  SurgSim::Math::Vector& generalizedForce,
107  const SurgSim::Math::Matrix& D = SurgSim::Math::Matrix()) override;
108 
111  virtual void beforeUpdate(double dt) override;
112 
117  virtual SurgSim::Math::Vector& computeF(const SurgSim::Math::OdeState& state) override;
118 
123  virtual const SurgSim::Math::Matrix& computeM(const SurgSim::Math::OdeState& state) override;
124 
129  virtual const SurgSim::Math::Matrix& computeD(const SurgSim::Math::OdeState& state) override;
130 
135  virtual const SurgSim::Math::Matrix& computeK(const SurgSim::Math::OdeState& state) override;
136 
146  virtual void computeFMDK(const SurgSim::Math::OdeState& state, SurgSim::Math::Vector** f,
148 
149 protected:
161  bool useGlobalStiffnessMatrix = false, bool useGlobalMassMatrix = false, double scale = 1.0);
162 
167  void addSpringsForce(SurgSim::Math::Vector* f, const SurgSim::Math::OdeState& state, double scale = 1.0);
168 
174  void addGravityForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState& state, double scale = 1.0);
175 
179  void transformState(std::shared_ptr<SurgSim::Math::OdeState> state,
180  const SurgSim::Math::RigidTransform3d& transform);
181 
182 private:
184  std::vector<std::shared_ptr<Mass>> m_masses;
185 
187  std::vector<std::shared_ptr<Spring>> m_springs;
188 
192  struct {
196 };
197 
198 } // namespace Physics
199 
200 } // namespace SurgSim
201 
202 #endif // SURGSIM_PHYSICS_MASSSPRINGREPRESENTATION_H
Definition: DriveElementFromInputBehavior.cpp:27
RepresentationType
Definition: Representation.h:42
struct SurgSim::Physics::MassSpringRepresentation::@2 m_rayleighDamping
Rayleigh damping parameters (massCoefficient and stiffnessCoefficient) D = massCoefficient.M + stiffnessCoefficient.K Matrices: D = damping, M = mass, K = stiffness.
virtual ~MassSpringRepresentation()
Destructor.
Definition: MassSpringRepresentation.cpp:43
size_t getNumSprings() const
Gets the number of springs.
Definition: MassSpringRepresentation.cpp:62
void addRayleighDampingForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, bool useGlobalStiffnessMatrix=false, bool useGlobalMassMatrix=false, double scale=1.0)
Add the Rayleigh damping forces.
Definition: MassSpringRepresentation.cpp:366
virtual void addExternalGeneralizedForce(std::shared_ptr< Localization > localization, SurgSim::Math::Vector &generalizedForce, const SurgSim::Math::Matrix &K=SurgSim::Math::Matrix(), const SurgSim::Math::Matrix &D=SurgSim::Math::Matrix()) override
Add an external generalized force applied on a specific localization.
Definition: MassSpringRepresentation.cpp:114
std::shared_ptr< Spring > getSpring(size_t springId)
Retrieves a given spring from its id.
Definition: MassSpringRepresentation.cpp:73
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
double stiffnessCoefficient
Definition: MassSpringRepresentation.h:194
Base class for all deformable representations MassSprings, Finite Element Models,...
Definition: DeformableRepresentation.h:46
Base class for all deformable representations (abstract class)
virtual const SurgSim::Math::Matrix & computeM(const SurgSim::Math::OdeState &state) override
Evaluation of the LHS matrix M(x,v) for a given state.
Definition: MassSpringRepresentation.cpp:177
double getTotalMass() const
Gets the total mass of the mass spring.
Definition: MassSpringRepresentation.cpp:79
OdeState defines the state y of an ode of 2nd order of the form M(x,v).a = F(x, v) with boundary cond...
Definition: OdeState.h:34
virtual RepresentationType getType() const override
Query the representation type.
Definition: MassSpringRepresentation.cpp:109
double getRayleighDampingMass() const
Gets the Rayleigh mass parameter.
Definition: MassSpringRepresentation.cpp:94
void transformState(std::shared_ptr< SurgSim::Math::OdeState > state, const SurgSim::Math::RigidTransform3d &transform)
Transform a state using a given transformation.
Definition: MassSpringRepresentation.cpp:460
std::shared_ptr< Mass > getMass(size_t nodeId)
Retrieves the mass of a given node.
Definition: MassSpringRepresentation.cpp:67
void setRayleighDampingMass(double massCoef)
Sets the Rayleigh mass parameter.
Definition: MassSpringRepresentation.cpp:104
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:67
double getRayleighDampingStiffness() const
Gets the Rayleigh stiffness parameter.
Definition: MassSpringRepresentation.cpp:89
MassSpring model is a deformable model (a set of masses connected by springs).
Definition: MassSpringRepresentation.h:38
virtual SurgSim::Math::Vector & computeF(const SurgSim::Math::OdeState &state) override
Evaluation of the RHS function f(x,v) for a given state.
Definition: MassSpringRepresentation.cpp:153
virtual void computeFMDK(const SurgSim::Math::OdeState &state, SurgSim::Math::Vector **f, SurgSim::Math::Matrix **M, SurgSim::Math::Matrix **D, SurgSim::Math::Matrix **K) override
Evaluation of f(x,v), M(x,v), D = -df/dv(x,v), K = -df/dx(x,v) When all the terms are needed...
Definition: MassSpringRepresentation.cpp:288
void addSpringsForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, double scale=1.0)
Add the springs force to f (given a state)
Definition: MassSpringRepresentation.cpp:416
MassSpringRepresentation(const std::string &name)
Constructor.
Definition: MassSpringRepresentation.cpp:32
Definitions of small fixed-size square matrix types.
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
std::vector< std::shared_ptr< Spring > > m_springs
Springs.
Definition: MassSpringRepresentation.h:187
virtual const SurgSim::Math::Matrix & computeK(const SurgSim::Math::OdeState &state) override
Evaluation of K = -df/dx (x,v) for a given state.
Definition: MassSpringRepresentation.cpp:259
virtual const SurgSim::Math::Matrix & computeD(const SurgSim::Math::OdeState &state) override
Evaluation of D = -df/dv (x,v) for a given state.
Definition: MassSpringRepresentation.cpp:204
void addMass(const std::shared_ptr< Mass > mass)
Adds a mass.
Definition: MassSpringRepresentation.cpp:47
Definitions of small fixed-size vector types.
double massCoefficient
Definition: MassSpringRepresentation.h:193
std::vector< std::shared_ptr< Mass > > m_masses
Masses.
Definition: MassSpringRepresentation.h:184
void addSpring(const std::shared_ptr< Spring > spring)
Adds a spring.
Definition: MassSpringRepresentation.cpp:52
void setRayleighDampingStiffness(double stiffnessCoef)
Sets the Rayleigh stiffness parameter.
Definition: MassSpringRepresentation.cpp:99
void addGravityForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, double scale=1.0)
Add the gravity force to f (given a state)
Definition: MassSpringRepresentation.cpp:424
size_t getNumMasses() const
Gets the number of masses.
Definition: MassSpringRepresentation.cpp:57
virtual void beforeUpdate(double dt) override
Preprocessing done before the update call.
Definition: MassSpringRepresentation.cpp:135