FemRepresentation.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_FEMREPRESENTATION_H
17 #define SURGSIM_PHYSICS_FEMREPRESENTATION_H
18 
19 #include <memory>
20 
22 #include "SurgSim/Math/Vector.h"
24 
25 namespace SurgSim
26 {
27 
28 namespace Physics
29 {
30 
31 class FemElement;
32 class FemPlyReaderDelegate;
33 
39 {
40 public:
43  explicit FemRepresentation(const std::string& name);
44 
46  virtual ~FemRepresentation();
47 
50  void setFilename(const std::string& filename);
51 
54  const std::string& getFilename() const;
55 
58  bool loadFile();
59 
62  void addFemElement(const std::shared_ptr<FemElement> element);
63 
66  size_t getNumFemElements() const;
67 
73  std::shared_ptr<FemElement> getFemElement(size_t femElementId);
74 
77  double getTotalMass() const;
78 
81  double getRayleighDampingStiffness() const;
82 
85  double getRayleighDampingMass() const;
86 
89  void setRayleighDampingStiffness(double stiffnessCoef);
90 
93  void setRayleighDampingMass(double massCoef);
94 
99 
102  virtual void beforeUpdate(double dt) override;
103 
108  virtual void afterUpdate(double dt) override;
109 
114  virtual SurgSim::Math::Vector& computeF(const SurgSim::Math::OdeState& state) override;
115 
120  virtual const SurgSim::Math::Matrix& computeM(const SurgSim::Math::OdeState& state) override;
121 
126  virtual const SurgSim::Math::Matrix& computeD(const SurgSim::Math::OdeState& state) override;
127 
132  virtual const SurgSim::Math::Matrix& computeK(const SurgSim::Math::OdeState& state) override;
133 
143  virtual void computeFMDK(const SurgSim::Math::OdeState& state, SurgSim::Math::Vector** f,
145 
146 protected:
159  bool useGlobalMassMatrix = false, bool useGlobalStiffnessMatrix = false, double scale = 1.0);
160 
165  void addFemElementsForce(SurgSim::Math::Vector* f, const SurgSim::Math::OdeState& state, double scale = 1.0);
166 
172  void addGravityForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState& state, double scale = 1.0);
173 
174  virtual bool doInitialize() override;
175 
177  std::vector<double> m_massPerNode; //< Useful in setting up the gravity force F=mg
178 
180  std::string m_filename;
181 
182 private:
185  virtual std::shared_ptr<FemPlyReaderDelegate> getDelegate() = 0;
186 
188  std::vector<std::shared_ptr<FemElement>> m_femElements;
189 
193  struct {
197 };
198 
199 } // namespace Physics
200 } // namespace SurgSim
201 
202 #endif // SURGSIM_PHYSICS_FEMREPRESENTATION_H
virtual const SurgSim::Math::Matrix & computeK(const SurgSim::Math::OdeState &state) override
Evaluation of K = -df/dx (x,v) for a given state.
Definition: FemRepresentation.cpp:284
const std::string & getFilename() const
Gets the name of the file to be loaded.
Definition: FemRepresentation.cpp:52
Definition: DriveElementFromInputBehavior.cpp:27
double getTotalMass() const
Gets the total mass of the fem.
Definition: FemRepresentation.cpp:153
virtual bool doInitialize() override
Interface to be implemented by derived classes.
Definition: FemRepresentation.cpp:95
A generic (size_t index, Vector coordinate) pair.
Definition: IndexedLocalCoordinate.h:29
Finite Element Model (a.k.a.
Definition: FemRepresentation.h:38
virtual void afterUpdate(double dt) override
Postprocessing done after the update call.
Definition: FemRepresentation.cpp:193
std::string m_filename
Filename for loading the fem representation.
Definition: FemRepresentation.h:180
virtual std::shared_ptr< FemPlyReaderDelegate > getDelegate()=0
To be implemented by derived classes.
FemRepresentation(const std::string &name)
Constructor.
Definition: FemRepresentation.cpp:34
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
Base class for all deformable representations MassSprings, Finite Element Models,...
Definition: DeformableRepresentation.h:46
void setFilename(const std::string &filename)
Sets the name of the file to be loaded.
Definition: FemRepresentation.cpp:47
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: FemRepresentation.cpp:231
struct SurgSim::Physics::FemRepresentation::@1 m_rayleighDamping
Rayleigh damping parameters (massCoefficient and stiffnessCoefficient) D = massCoefficient.M + stiffnessCoefficient.K Matrices: D = damping, M = mass, K = stiffness.
void addFemElementsForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, double scale=1.0)
Adds the FemElements forces to f (given a state)
Definition: FemRepresentation.cpp:399
double massCoefficient
Definition: FemRepresentation.h:194
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
void setRayleighDampingStiffness(double stiffnessCoef)
Sets the Rayleigh stiffness parameter.
Definition: FemRepresentation.cpp:173
double getRayleighDampingMass() const
Gets the Rayleigh mass parameter.
Definition: FemRepresentation.cpp:168
double stiffnessCoefficient
Definition: FemRepresentation.h:195
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:67
virtual const SurgSim::Math::Matrix & computeD(const SurgSim::Math::OdeState &state) override
Evaluation of D = -df/dv (x,v) for a given state.
Definition: FemRepresentation.cpp:245
bool isValidCoordinate(const SurgSim::DataStructures::IndexedLocalCoordinate &coordinate) const
Determines whether the associated coordinate is valid.
Definition: FemRepresentation.cpp:147
virtual SurgSim::Math::Vector & computeF(const SurgSim::Math::OdeState &state) override
Evaluation of the RHS function f(x,v) for a given state.
Definition: FemRepresentation.cpp:215
double getRayleighDampingStiffness() const
Gets the Rayleigh stiffness parameter.
Definition: FemRepresentation.cpp:163
size_t getNumFemElements() const
Gets the number of FemElement.
Definition: FemRepresentation.cpp:136
std::vector< std::shared_ptr< FemElement > > m_femElements
FemElements.
Definition: FemRepresentation.h:188
void setRayleighDampingMass(double massCoef)
Sets the Rayleigh mass parameter.
Definition: FemRepresentation.cpp:178
Definitions of small fixed-size vector types.
std::vector< double > m_massPerNode
Useful information per node.
Definition: FemRepresentation.h:177
virtual void beforeUpdate(double dt) override
Preprocessing done before the update call.
Definition: FemRepresentation.cpp:183
virtual ~FemRepresentation()
Destructor.
Definition: FemRepresentation.cpp:43
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: FemRepresentation.cpp:301
void addFemElement(const std::shared_ptr< FemElement > element)
Adds a FemElement.
Definition: FemRepresentation.cpp:131
bool loadFile()
Loads the file.
Definition: FemRepresentation.cpp:57
void addRayleighDampingForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, bool useGlobalMassMatrix=false, bool useGlobalStiffnessMatrix=false, double scale=1.0)
Adds the Rayleigh damping forces.
Definition: FemRepresentation.cpp:353
std::shared_ptr< FemElement > getFemElement(size_t femElementId)
Retrieves a given FemElement from its id.
Definition: FemRepresentation.cpp:141
void addGravityForce(SurgSim::Math::Vector *f, const SurgSim::Math::OdeState &state, double scale=1.0)
Adds the gravity force to f (given a state)
Definition: FemRepresentation.cpp:409