iMSTK
Interactive Medical Simulation Toolkit
imstkGeometryAlgorithm.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 "imstkLogger.h"
10 #include "imstkMacros.h"
11 
12 #include <functional>
13 #include <memory>
14 #include <unordered_map>
15 
16 namespace imstk
17 {
18 class Geometry;
19 
22 template<class Base, class Target>
23 std::function<bool(Base*)>
25 {
26  return [](Base* p) {
27  return (dynamic_cast<Target*>(p) != nullptr);
28  };
29 }
30 
40 {
41 protected:
42  GeometryAlgorithm() = default;
43 
44 public:
45  virtual ~GeometryAlgorithm() = default;
46 
50  std::shared_ptr<Geometry> getInput(size_t port = 0) const
51  {
52  return (m_inputs.count(port) == 0) ? nullptr : m_inputs.at(port);
53  }
54 
58  std::shared_ptr<Geometry> getOutput(size_t port = 0) const
59  {
60  return m_outputs.count(port) == 0 ? nullptr : m_outputs.at(port);
61  }
62 
66  void setInput(std::shared_ptr<Geometry> inputGeometry, size_t port = 0);
67 
71  void update()
72  {
73  if (!areInputsValid())
74  {
75  LOG(WARNING) << "GeometryAlgorithm failed to run, inputs not satisfied";
76  return;
77  }
78  //if (m_modified)
79  //{
80  requestUpdate();
81  //}
82  //m_modified = false;
83  }
84 
85 protected:
89  void setOutput(std::shared_ptr<Geometry> inputGeometry, const size_t port = 0);
90 
94  void setNumInputPorts(const size_t numPorts);
95  imstkGetMacro(NumInputPorts, size_t);
97 
101  void setNumOutputPorts(const size_t numPorts);
102  imstkGetMacro(NumOutputPorts, size_t);
104 
109  template<typename T>
110  void setRequiredInputType(const size_t port)
111  {
112  CHECK(m_optionalTypeChecks.find(port) == m_optionalTypeChecks.end())
113  << "There is already an optional type for this port " << port << ", can't assign another one.";
114  m_requiredTypeChecks[port] = makeTypeCheck<Geometry, T>();
115  }
116 
121  template<typename T>
122  void setOptionalInputType(const size_t port)
123  {
124  CHECK(m_requiredTypeChecks.find(port) == m_requiredTypeChecks.end())
125  << "There is already a required type for port " << port << " , can't assign another one.";
126  m_optionalTypeChecks[port] = makeTypeCheck<Geometry, T>();
127  }
128 
132  virtual void requestUpdate() = 0;
133 
138  virtual bool areInputsValid();
139 
140  using GeometryCheck = std::function<bool (Geometry*)>;
141  using TypeCheckContainer = std::unordered_map<size_t, GeometryCheck>;
142 
143  TypeCheckContainer m_requiredTypeChecks;
144  TypeCheckContainer m_optionalTypeChecks;
145 
146 private:
147 
148  std::unordered_map<size_t, std::shared_ptr<Geometry>> m_inputs;
149  std::unordered_map<size_t, std::shared_ptr<Geometry>> m_outputs;
150 
151  //bool m_modified = true;
152  size_t m_NumInputPorts = 1;
153  size_t m_NumOutputPorts = 1;
154 };
155 } // namespace imstk
virtual void requestUpdate()=0
Users can implement this for the logic to be run.
std::shared_ptr< Geometry > getInput(size_t port=0) const
Returns input geometry given port, returns nullptr if doesn&#39;t exist.
Compound Geometry.
void setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
std::shared_ptr< Geometry > getOutput(size_t port=0) const
Returns output geometry given port, returns nullptr if doesn&#39;t exist.
void update()
Do the actual algorithm.
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
std::function< bool(Base *)> makeTypeCheck()
void setOptionalInputType(const size_t port)
Declares the type for the port with the given number, the data for this port is optional and may be o...
Abstract base class for geometry algorithms. GeometryAlgorithms take N input geometries and produce N...
virtual bool areInputsValid()
Check inputs are correct.
void setOutput(std::shared_ptr< Geometry > inputGeometry, const size_t port=0)
Set the output at the port.
void setRequiredInputType(const size_t port)
Declares the type for the port with the given number, also defines that the give port is required for...
void setNumInputPorts(const size_t numPorts)
Get/Set the amount of input ports.