opensurgsim
DeformableRepresentation.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 
18 
19 #ifndef SURGSIM_PHYSICS_DEFORMABLEREPRESENTATION_H
20 #define SURGSIM_PHYSICS_DEFORMABLEREPRESENTATION_H
21 
22 #include <memory>
23 
24 #include "SurgSim/Math/LinearSparseSolveAndInverse.h"
25 #include "SurgSim/Math/Matrix.h"
26 #include "SurgSim/Math/OdeEquation.h"
27 #include "SurgSim/Math/OdeSolver.h"
28 #include "SurgSim/Math/OdeState.h"
29 #include "SurgSim/Math/Vector.h"
30 #include "SurgSim/Physics/Representation.h"
31 
32 namespace SurgSim
33 {
34 
35 namespace Physics
36 {
37 
38 class Localization;
39 
51  public Representation,
53 {
54 public:
57  explicit DeformableRepresentation(const std::string& name);
58 
60  virtual ~DeformableRepresentation();
61 
62  void resetState() override;
63 
66  virtual void setInitialState(std::shared_ptr<SurgSim::Math::OdeState> initialState);
67 
70  virtual const std::shared_ptr<SurgSim::Math::OdeState> getCurrentState() const;
71 
74  virtual const std::shared_ptr<SurgSim::Math::OdeState> getPreviousState() const;
75 
78  virtual const std::shared_ptr<SurgSim::Math::OdeState> getFinalState() const;
79 
83  virtual void interpolatePreviousState(double t);
84 
87  size_t getNumDofPerNode() const;
88 
92  void setIntegrationScheme(SurgSim::Math::IntegrationScheme integrationScheme);
93 
97  SurgSim::Math::IntegrationScheme getIntegrationScheme() const;
98 
101  std::shared_ptr<SurgSim::Math::OdeSolver> getOdeSolver() const;
102 
106  void setLinearSolver(SurgSim::Math::LinearSolver linearSolver);
107 
111  SurgSim::Math::LinearSolver getLinearSolver() const;
112 
118  virtual void addExternalGeneralizedForce(std::shared_ptr<Localization> localization,
119  const SurgSim::Math::Vector& generalizedForce,
122 
125 
128 
131 
132  Math::Matrix applyCompliance(const Math::OdeState& state, const Math::Matrix& b) override;
133 
135  virtual const SurgSim::Math::Matrix& getComplianceMatrix() const;
136 
137  void update(double dt) override;
138 
139  void afterUpdate(double dt) override;
140 
141  void applyCorrection(double dt, const Eigen::VectorBlock<SurgSim::Math::Vector>& deltaVelocity) override;
142 
144  void deactivateAndReset();
145 
150  void setCollisionRepresentation(std::shared_ptr<SurgSim::Collision::Representation> representation) override;
151 
152  void setLocalPose(const SurgSim::Math::RigidTransform3d& pose) override;
153 
154 
155 
156 protected:
157  bool doInitialize() override;
158  bool doWakeUp() override;
159 
163  virtual void transformState(std::shared_ptr<SurgSim::Math::OdeState> state,
164  const SurgSim::Math::RigidTransform3d& transform) = 0;
165 
167  std::shared_ptr<SurgSim::Math::OdeState> m_previousState;
168 
171  std::shared_ptr<SurgSim::Math::OdeState> m_currentState;
172 
174  std::shared_ptr<SurgSim::Math::OdeState> m_newState;
175 
178  std::shared_ptr<SurgSim::Math::OdeState> m_finalState;
179 
183  SurgSim::Math::Vector m_externalGeneralizedForce;
184  SurgSim::Math::SparseMatrix m_externalGeneralizedStiffness;
185  SurgSim::Math::SparseMatrix m_externalGeneralizedDamping;
186  bool m_previousHasExternalGeneralizedForce;
187  SurgSim::Math::SparseMatrix m_previousExternalGeneralizedStiffness;
188  SurgSim::Math::SparseMatrix m_previousExternalGeneralizedDamping;
190 
194 
196  SurgSim::Math::IntegrationScheme m_integrationScheme;
197 
199  SurgSim::Math::LinearSolver m_linearSolver;
200 
202  std::shared_ptr<SurgSim::Math::OdeSolver> m_odeSolver;
203 
204 private:
207 
210 };
211 
212 }; // namespace Physics
213 
214 }; // namespace SurgSim
215 
216 #endif // SURGSIM_PHYSICS_DEFORMABLEREPRESENTATION_H
217 
218 
219 
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
virtual void transformState(std::shared_ptr< SurgSim::Math::OdeState > state, const SurgSim::Math::RigidTransform3d &transform)=0
Transform a state using a given transformation.
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
The Representation class defines the base class for all physics objects.
Definition: Representation.h:53
SurgSim::Math::IntegrationScheme getIntegrationScheme() const
Gets the numerical integration scheme.
Definition: DeformableRepresentation.cpp:138
Eigen::SparseMatrix< double > SparseMatrix
A sparse matrix.
Definition: SparseMatrix.h:32
Base class for all deformable representations MassSprings, Finite Element Models,...
Definition: DeformableRepresentation.h:50
bool doWakeUp() override
Interface to be implemented by derived classes.
Definition: DeformableRepresentation.cpp:390
std::shared_ptr< SurgSim::Math::OdeState > m_finalState
Last valid state (a.k.a final state)
Definition: DeformableRepresentation.h:178
std::shared_ptr< SurgSim::Math::OdeState > m_currentState
The currently calculated state inside the physics loop, after the whole calculation is done this will...
Definition: DeformableRepresentation.h:171
Ode equation of 2nd order of the form with for initial conditions and a set of boundary conditions...
Definition: OdeEquation.h:54
The state of an ode of 2nd order of the form with boundary conditions.
Definition: OdeState.h:38
const SurgSim::Math::SparseMatrix & getExternalGeneralizedStiffness() const
Definition: DeformableRepresentation.cpp:165
virtual const SurgSim::Math::Matrix & getComplianceMatrix() const
Gets the compliance matrix associated with motion.
Definition: DeformableRepresentation.cpp:192
const SurgSim::Math::Vector & getExternalGeneralizedForce() const
Definition: DeformableRepresentation.cpp:160
void setIntegrationScheme(SurgSim::Math::IntegrationScheme integrationScheme)
Sets the numerical integration scheme.
Definition: DeformableRepresentation.cpp:131
void applyCorrection(double dt, const Eigen::VectorBlock< SurgSim::Math::Vector > &deltaVelocity) override
Update the Representation&#39;s current position and velocity using a time interval, dt, and change in velocity, deltaVelocity.
Definition: DeformableRepresentation.cpp:255
std::shared_ptr< SurgSim::Math::OdeState > m_previousState
The previous state inside the calculation loop, this has no meaning outside of the loop...
Definition: DeformableRepresentation.h:167
std::shared_ptr< SurgSim::Math::OdeSolver > m_odeSolver
Ode solver (its type depends on the numerical integration scheme)
Definition: DeformableRepresentation.h:202
virtual void addExternalGeneralizedForce(std::shared_ptr< Localization > localization, const SurgSim::Math::Vector &generalizedForce, const SurgSim::Math::Matrix &K=SurgSim::Math::Matrix(), const SurgSim::Math::Matrix &D=SurgSim::Math::Matrix())=0
Add an external generalized force applied on a specific localization.
void update(double dt) override
Update the representation state to the current time step.
Definition: DeformableRepresentation.cpp:198
virtual ~DeformableRepresentation()
Destructor.
Definition: DeformableRepresentation.cpp:63
virtual void interpolatePreviousState(double t)
Declare a new previous state by interpolating between the old previous state and the current state us...
Definition: DeformableRepresentation.cpp:406
void deactivateAndReset()
Deactivate and call resetState.
Definition: DeformableRepresentation.cpp:277
size_t m_numDofPerNode
Number of degrees of freedom per node (varies per deformable model)
Definition: DeformableRepresentation.h:193
DeformableRepresentation(const std::string &name)
Constructor.
Definition: DeformableRepresentation.cpp:47
std::shared_ptr< SurgSim::Math::OdeSolver > getOdeSolver() const
Definition: DeformableRepresentation.cpp:143
Eigen::Matrix< double, Eigen::Dynamic, 1 > Vector
A dynamic size column vector.
Definition: Vector.h:68
size_t getNumDofPerNode() const
Gets the number of degrees of freedom per node.
Definition: DeformableRepresentation.cpp:126
Math::Matrix applyCompliance(const Math::OdeState &state, const Math::Matrix &b) override
Calculate the product where is the compliance matrix with boundary conditions applied.
Definition: DeformableRepresentation.cpp:175
Definitions of small fixed-size square matrix types.
bool doInitialize() override
Interface to be implemented by derived classes.
Definition: DeformableRepresentation.cpp:314
Definitions of small fixed-size vector types.
void setLocalPose(const SurgSim::Math::RigidTransform3d &pose) override
Set the pose of the representation with respect to the Scene Element.
Definition: DeformableRepresentation.cpp:78
virtual const std::shared_ptr< SurgSim::Math::OdeState > getPreviousState() const
Return the previous state of the deformable representation.
Definition: DeformableRepresentation.cpp:116
SurgSim::Math::IntegrationScheme m_integrationScheme
Numerical Integration scheme (dynamic explicit/implicit solver)
Definition: DeformableRepresentation.h:196
void setLinearSolver(SurgSim::Math::LinearSolver linearSolver)
Sets the linear algebraic solver.
Definition: DeformableRepresentation.cpp:148
virtual const std::shared_ptr< SurgSim::Math::OdeState > getCurrentState() const
Return the current state of the deformable representation.
Definition: DeformableRepresentation.cpp:111
void resetState() override
Reset the representation to its initial/default state.
Definition: DeformableRepresentation.cpp:67
const SurgSim::Math::SparseMatrix & getExternalGeneralizedDamping() const
Definition: DeformableRepresentation.cpp:170
bool m_hasExternalGeneralizedForce
External generalized force, stiffness and damping applied on the deformable representation.
Definition: DeformableRepresentation.h:182
SurgSim::Math::LinearSolver getLinearSolver() const
Gets the linear algebraic solver.
Definition: DeformableRepresentation.cpp:155
virtual const std::shared_ptr< SurgSim::Math::OdeState > getFinalState() const
Return the final state of the deformable representation.
Definition: DeformableRepresentation.cpp:121
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > Matrix
A dynamic size matrix.
Definition: Matrix.h:65
void afterUpdate(double dt) override
Postprocessing done after the update call This needs to be called from the outside usually from a Com...
Definition: DeformableRepresentation.cpp:229
void setCollisionRepresentation(std::shared_ptr< SurgSim::Collision::Representation > representation) override
Set the collision representation for this physics representation, when the collision object is involv...
Definition: DeformableRepresentation.cpp:288
SurgSim::Math::LinearSolver m_linearSolver
Linear algebraic solver used.
Definition: DeformableRepresentation.h:199
std::shared_ptr< SurgSim::Math::OdeState > m_newState
New state is a temporary variable to store the newly computed state.
Definition: DeformableRepresentation.h:174
virtual void setInitialState(std::shared_ptr< SurgSim::Math::OdeState > initialState)
Initialize the state variables to initialState.
Definition: DeformableRepresentation.cpp:86