iMSTK
Interactive Medical Simulation Toolkit
imstkLevelSetModel.h
1 /*
2 ** This file is part of the Interactive Medical Simulation Toolkit (iMSTK)
3 ** iMSTK is distributed under the Apache License, Version 2.0.
4 ** See accompanying NOTICE for details.
5 */
6 
7 #pragma once
8 
9 #include "imstkDynamicalModel.h"
10 #include "imstkImplicitFunctionFiniteDifferenceFunctor.h"
11 
12 #include <unordered_map>
13 #include <tuple>
14 
15 namespace imstk
16 {
17 class ImageData;
18 
20 {
21  double m_dt = 0.001;
22  bool m_sparseUpdate = false;
23  bool m_useCurvature = false;
24  double m_k = 0.05; // Curvature term
25  double m_constantVelocity = 0.0; // Constant velocity
26  int m_substeps = 10; // How many steps to do every iteration
27 };
28 
36 {
37 public:
38  LevelSetModel();
39  ~LevelSetModel() override = default;
40 
44  void setTimeStep(const double timeStep) override { m_config->m_dt = timeStep; }
45  double getTimeStep() const override { return m_config->m_dt; }
47 
48  std::shared_ptr<LevelSetModelConfig> getConfig() const { return m_config; }
49 
53  bool initialize() override;
54 
58  void configure(std::shared_ptr<LevelSetModelConfig> config);
59 
60  virtual void evolve();
61 
66  void addImpulse(const Vec3i& coord, double f);
70  void setImpulse(const Vec3i& coord, double f);
71 
72  std::shared_ptr<TaskNode> getQuantityEvolveNode(size_t i) const { return m_evolveQuantitiesNodes[i]; }
73  std::shared_ptr<TaskNode> getGenerateVelocitiesBeginNode() const { return m_generateVelocitiesBegin; }
74  std::shared_ptr<TaskNode> getGenerateVelocitiesEndNode() const { return m_generateVelocitiesEnd; }
75 
76  std::unordered_map<size_t, std::tuple<Vec3i, double>>& getNodesToUpdate() { return m_nodesToUpdate; }
77 
78  void resetToInitialState() override;
79 
80 protected:
84  void initGraphEdges(std::shared_ptr<TaskNode> source, std::shared_ptr<TaskNode> sink) override;
85 
86  std::shared_ptr<ImplicitGeometry> m_mesh = nullptr;
87 
88  std::vector<std::shared_ptr<TaskNode>> m_evolveQuantitiesNodes;
89 
90  std::shared_ptr<TaskNode> m_generateVelocitiesBegin;
91  std::shared_ptr<TaskNode> m_generateVelocitiesEnd;
92 
93  std::shared_ptr<LevelSetModelConfig> m_config;
94 
95  std::unordered_map<size_t, std::tuple<Vec3i, double>> m_nodesToUpdate;
96  std::vector<std::tuple<size_t, Vec3i, double, Vec2d, double>> m_nodeUpdatePool;
97  size_t noteUpdatePoolSize;
98  size_t m_maxVelocitiesParallel = 100; // In sparse mode, if surpass this value, switch to parallel
99 
100  std::shared_ptr<ImageData> m_gradientMagnitudes = nullptr;
101  std::shared_ptr<ImageData> m_velocities = nullptr;
102  std::shared_ptr<ImageData> m_curvatures = nullptr;
103 
104  // I'm unable to use the more generic double/floating pt based version
105  // suspect floating point error
106  StructuredForwardGradient m_forwardGrad;
107  StructuredBackwardGradient m_backwardGrad;
108 
109  ImplicitStructuredCurvature m_curvature;
110 };
111 } // namespace imstk
void setTimeStep(const double timeStep) override
Get/Set the time step size.
bool m_sparseUpdate
Only updates nodes that recieve force.
Compound Geometry.
Abstract class for mathematical model of the physics governing the dynamic object.
This class implements a generic level set model, it requires both a forward and backward finite diffe...
double m_dt
Time step size.
double getTimeStep() const override
Returns the time step size.
Gradient given by backward finite differences.
Gradient given by forward finite differences.