opensurgsim
MassSpringRepresentation.h
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 
21 #include "SurgSim/DataStructures/IndexedLocalCoordinate.h"
22 #include "SurgSim/Math/Matrix.h"
23 #include "SurgSim/Math/Vector.h"
25 
26 namespace SurgSim
27 {
28 namespace Framework
29 {
30 class Asset;
31 }
32 
33 namespace Physics
34 {
35 class Mass;
36 class MassSpringModel;
37 class Spring;
38 
39 SURGSIM_STATIC_REGISTRATION(MassSpringRepresentation);
40 
46 {
47 public:
50  explicit MassSpringRepresentation(const std::string& name);
51 
53  virtual ~MassSpringRepresentation();
54 
56 
61  void addMass(const std::shared_ptr<Mass> mass);
62 
65  void addSpring(const std::shared_ptr<Spring> spring);
66 
69  size_t getNumMasses() const;
70 
73  size_t getNumSprings() const;
74 
76  size_t getNumElements() const;
77 
83  std::shared_ptr<Mass> getMass(size_t nodeId);
84 
90  std::shared_ptr<Spring> getSpring(size_t springId);
91 
94  double getTotalMass() const;
95 
98  double getRayleighDampingStiffness() const;
99 
102  double getRayleighDampingMass() const;
103 
106  void setRayleighDampingStiffness(double stiffnessCoef);
107 
110  void setRayleighDampingMass(double massCoef);
111 
112  void addExternalGeneralizedForce(std::shared_ptr<Localization> localization,
113  const SurgSim::Math::Vector& generalizedForce,
115  const SurgSim::Math::Matrix& D = SurgSim::Math::Matrix()) override;
116 
119  void beforeUpdate(double dt) override;
120 
121  std::shared_ptr<Localization> createLocalization(const SurgSim::DataStructures::Location& location) override;
122 
125  void loadMassSpringModel(const std::string & filename);
126 
130  void setMassSpringModel(std::shared_ptr<Framework::Asset> mesh);
131 
133  std::shared_ptr<MassSpringModel> getMassSpringModel() const;
134 
138  bool isValidCoordinate(const SurgSim::Math::Vector& naturalCoordinate) const;
139 
143  bool isValidCoordinate(const SurgSim::DataStructures::IndexedLocalCoordinate& localCoordinate) const;
144 
149  Math::Vector3d computeCartesianCoordinate(const Math::OdeState& state,
150  const DataStructures::IndexedLocalCoordinate& localCoordinate) const;
151 
155  const std::vector<size_t>& getNodeIds(size_t index) const;
156 
161  bool saveMassSpringModel(const std::string& fileName, double physicsLength = 0.0) const;
162 
163 protected:
174  void addRayleighDampingForce(SurgSim::Math::Vector* f, const SurgSim::Math::OdeState& state,
175  bool useGlobalStiffnessMatrix = false, bool useGlobalMassMatrix = false,
176  double scale = 1.0);
177 
182  void addSpringsForce(SurgSim::Math::Vector* f, const SurgSim::Math::OdeState& state, double scale = 1.0);
183 
189  void addGravityForce(SurgSim::Math::Vector* f, const SurgSim::Math::OdeState& state, double scale = 1.0);
190 
194  void transformState(std::shared_ptr<SurgSim::Math::OdeState> state,
195  const SurgSim::Math::RigidTransform3d& transform);
196 
197  bool doInitialize() override;
198 
199  void computeF(const SurgSim::Math::OdeState& state) override;
200 
201  void computeM(const SurgSim::Math::OdeState& state) override;
202 
203  void computeD(const SurgSim::Math::OdeState& state) override;
204 
205  void computeK(const SurgSim::Math::OdeState& state) override;
206 
207  void computeFMDK(const SurgSim::Math::OdeState& state) override;
208 
209 private:
211  std::vector<std::shared_ptr<Mass>> m_masses;
212 
214  std::vector<std::shared_ptr<Spring>> m_springs;
215 
219  struct
220  {
221  double massCoefficient;
222  double stiffnessCoefficient;
223  } m_rayleighDamping;
224 
226  std::shared_ptr<MassSpringModel> m_mesh;
227 };
228 
229 } // namespace Physics
230 
231 } // namespace SurgSim
232 
233 #endif // SURGSIM_PHYSICS_MASSSPRINGREPRESENTATION_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
A generic (size_t index, Vector coordinate) pair.
Definition: IndexedLocalCoordinate.h:29
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
A Location defines a local position w.r.t.
Definition: Location.h:39
Base class for all deformable representations MassSprings, Finite Element Models,...
Definition: DeformableRepresentation.h:50
Base class for all deformable representations (abstract class)
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
The state of an ode of 2nd order of the form with boundary conditions.
Definition: OdeState.h:38
MassSpring model is a deformable model (a set of masses connected by springs).
Definition: MassSpringRepresentation.h:45
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
Definitions of small fixed-size square matrix types.
Definitions of small fixed-size vector types.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65