iMSTK
Interactive Medical Simulation Toolkit
imstkProgrammableClient.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 "imstkDeviceClient.h"
10 #include "imstkPbdObject.h"
11 #include "imstkPbdObjectGrasping.h"
12 #include "imstkSurfaceMesh.h"
13 
14 #include <unordered_map>
15 
16 namespace imstk
17 {
25 {
26 public:
30  ProgrammableClient(const std::string& name = "") : DeviceClient(name, "localhost") {}
31 
35  ~ProgrammableClient() override;
36 
40  enum DeformationType { Compression, Tension, SimpleShear, PureShear };
41 protected:
42  void run() = delete;
43  void cleanUp() = delete;
44 
48  enum class CommandState { WAITING=0, ACTIVE, COMPLETE };
49 
54  struct Command
55  {
56  virtual ~Command() = default;
57  CommandState state = CommandState::WAITING;
58  double startTime = 0.0;
59  double duration = 0.0;
60  virtual void activate(ProgrammableClient&) { state = CommandState::ACTIVE; }
61  virtual void updateDevice(ProgrammableClient&) = 0;
62  virtual void complete(ProgrammableClient&) { state = CommandState::COMPLETE; }
63  };
64  friend Command;
65 
70  {
71  Vec3d startPosition = Vec3d::Zero();
72  Vec3d stopPosition = Vec3d::Zero();
73  void activate(ProgrammableClient&) override;
74  void updateDevice(ProgrammableClient&) override;
75  void complete(ProgrammableClient&) override;
76  };
77 
82  {
83  Vec3d startPosition = Vec3d::Zero();
84  Vec3d centerPosition = Vec3d::Zero();
85  double radius = 0.0;
86  double angle = 0;
87  double angleStep = 0;
88  void activate(ProgrammableClient&) override;
89  void updateDevice(ProgrammableClient&) override;
90  void complete(ProgrammableClient&) override;
91  };
92 
97  {
98  std::shared_ptr<PbdObjectGrasping> objectGrasping;
99  std::shared_ptr<PbdObject> tool;
100  void activate(ProgrammableClient&) override;
101  void updateDevice(ProgrammableClient&) override { return; }
102  void complete(ProgrammableClient&) override;
103  };
104 
109  {
110  std::shared_ptr<PbdObject> object;
111  Vec3d translation = Vec3d::Zero();
112  std::vector<bool> pin{ true, true, true };
113  std::vector<int> vertexIds;
114  std::vector<Vec3d> currPos;
115  void activate(ProgrammableClient&) override;
116  void updateDevice(ProgrammableClient&) override;
117  void complete(ProgrammableClient&) override;
118  };
119 
124  {
125  std::shared_ptr<PbdObject> object;
126  Mat3d defGrad = Mat3d::Identity();
127  double strain = 0.0;
128  double strainRate = 0.0;
129  double poissons = 0.49;
130  std::vector<bool> pin{ true, true, true };
131  std::vector<int> vertexIds;
132 
133  DeformationType type;
134 
135  void activate(ProgrammableClient&) override;
136  void updateDevice(ProgrammableClient&) override;
137  void complete(ProgrammableClient&) override;
138  };
139 
144  {
145  void activate(ProgrammableClient& pc) override { Command::activate(pc); }
146  void updateDevice(ProgrammableClient&) override { return; }
147  void complete(ProgrammableClient& pc) override { Command::complete(pc); }
148  };
149 
154  {
155  std::shared_ptr<PbdObject> object;
156  std::vector<int> vertexIds;
157  std::vector<Vec3d> holdPosition;
158  void activate(ProgrammableClient& pc) override;
159  void updateDevice(ProgrammableClient&) override;
160  void complete(ProgrammableClient& pc) override;
161  };
162 
163  double m_dt = 0.0;
164  double m_currentTime = 0.0;
165  bool m_complete = false;
166  std::vector<Command*> m_commands;
167 
168 public:
169 
173  void setDeltaTime(double dt) { m_dt = dt; }
174 
178  void update() override;
179 
187  bool addLinearMovement(Vec3d startPos, Vec3d stopPos, double startTime, double duration);
188 
196  bool addCircularMovement(Vec3d startPos, Vec3d centerPos, double startTime, double duration);
197 
205  bool addGrasping(std::shared_ptr<PbdObject> tool, std::shared_ptr<PbdObjectGrasping> objectGrasping, double startTime, double duration);
206 
216  bool addLinearVertexMovement(std::shared_ptr<PbdObject> object, std::vector<int> vertexIds, Vec3d translation, std::vector<bool> pin, double startTime, double duration);
217 
229  bool addDeformation(std::shared_ptr<PbdObject> object, std::vector<int> initPos, double strain, DeformationType defType, double poisson, std::vector<bool> pin, double startTime, double duration);
230 
231  // TODO remove once addDeformation has been updated
232  std::vector<int> findVertex(std::shared_ptr<PointSet> mesh, std::vector<Vec3d> initPos);
233 
239  bool addWaitCommand(double startTime, double duration);
240 
248  bool addHoldCommand(std::shared_ptr<PbdObject> object, double startTime, double duration, std::vector<int> vertexIds);
249 
253  double getTotalDuration();
254 
258  bool isFinished() { return this->m_complete; }
259 
260  void setOrientation(Quatd temp) { m_orientation = temp; }
261 };
262 } // namespace imstk
The device client&#39;s represents the device and provides an interface to acquire data from a device...
~ProgrammableClient() override
Destructor.
Command for holding a subset of vertices at a specific position.
bool addGrasping(std::shared_ptr< PbdObject > tool, std::shared_ptr< PbdObjectGrasping > objectGrasping, double startTime, double duration)
Add a grasp command to run.
Command for linear movement of a subset of vertices from an object.
bool addHoldCommand(std::shared_ptr< PbdObject > object, double startTime, double duration, std::vector< int > vertexIds)
Add a hold command to run.
double getTotalDuration()
Returns to total duration of all commands.
bool addWaitCommand(double startTime, double duration)
Add a wait command to run.
Compound Geometry.
bool addCircularMovement(Vec3d startPos, Vec3d centerPos, double startTime, double duration)
Add a circular movement command to run.
bool isFinished()
Returns true if all commands have finished executing. Returns false otherwise.
bool addLinearMovement(Vec3d startPos, Vec3d stopPos, double startTime, double duration)
Add a linear movement command to run.
DeformationType
Enum for type of deformation.
bool addDeformation(std::shared_ptr< PbdObject > object, std::vector< int > initPos, double strain, DeformationType defType, double poisson, std::vector< bool > pin, double startTime, double duration)
Add a defomation command to run.
Quatd m_orientation
Orientation of the end effector.
CommandState
Enum for current state of command.
Command for linear movement of an analytical geometry.
Command for waiting. Used to let system continue running with no active commands. ...
void setDeltaTime(double dt)
Set the dt of the system. Should be the same as the simulation dt.
ProgrammableClient(const std::string &name="")
Constructor.
bool addLinearVertexMovement(std::shared_ptr< PbdObject > object, std::vector< int > vertexIds, Vec3d translation, std::vector< bool > pin, double startTime, double duration)
Add a linear vertex movement command to run.
void update() override
Update all commands.
Command for grasping an object.
General struct for command. Command will call activate when start time is reached, call updateDevice while active, and call complete when duration is over.
Command for applying deformationt to a subset of vertices from an object.
Command for circular movement of an analytical geometry.
Allows setting the pose of the device from external caller without a real device connected.