iMSTK
Interactive Medical Simulation Toolkit
imstkGeometryAlgorithm.cpp
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 #include "imstkGeometryAlgorithm.h"
8 #include "imstkGeometry.h"
9 #include "imstkLogger.h"
10 
11 namespace imstk
12 {
13 void
14 GeometryAlgorithm::setInput(std::shared_ptr<Geometry> inputGeometry, size_t port)
15 {
16  if (m_inputs.count(port) == 0)
17  {
18  LOG(WARNING) << "Tried to set input " << port << " on filter with " << m_NumInputPorts << " ports";
19  }
20 
21  m_inputs[port] = inputGeometry;
22 }
23 
24 void
25 GeometryAlgorithm::setOutput(std::shared_ptr<Geometry> outputGeometry, const size_t port)
26 {
27  if (m_outputs.count(port) == 0)
28  {
29  LOG(WARNING) << "Tried to set output " << port << " on filter with " << m_NumOutputPorts << " ports";
30  }
31  m_outputs[port] = outputGeometry;
32 }
33 
34 void
36 {
37  this->m_NumInputPorts = numPorts;
38  // Add entries in the map for it
39  for (size_t i = 0; i < numPorts; i++)
40  {
41  if (m_inputs.count(i) == 0)
42  {
43  m_inputs[i] = nullptr;
44  }
45  }
46 }
47 
48 void
50 {
51  this->m_NumOutputPorts = numPorts;
52  // Add entries in the map for it
53  for (size_t i = 0; i < numPorts; i++)
54  {
55  if (m_outputs.count(i) == 0)
56  {
57  m_outputs[i] = nullptr;
58  }
59  }
60 }
61 
62 bool
64 {
65  // Check input types
66  for (const auto& port : m_inputs)
67  {
68  const size_t portId = port.first;
69  Geometry* input = port.second.get();
70  auto found = m_requiredTypeChecks.find(portId);
71  if (found != m_requiredTypeChecks.cend())
72  {
73  // Require Input: can't be null has to succeed type check
74  if (port.second == nullptr)
75  {
76  LOG(WARNING) << "GeometryAlgorithm input " << portId << " missing!";
77  return false;
78  }
79  else if (!found->second(input))
80  {
81  LOG(WARNING) << "GeometryAlgorithm received invalid geometry type \"" <<
82  m_inputs.at(portId)->getTypeName() << "\" in port " << portId;
83  return false;
84  }
85  continue;
86  }
87 
88  found = m_optionalTypeChecks.find(portId);
89  if (found != m_optionalTypeChecks.cend())
90  {
91  // Require Input: may be null, if not has succeed type check
92  if (input != nullptr && !found->second(input))
93  {
94  LOG(WARNING) << "GeometryAlgorithm received invalid geometry type \"" <<
95  m_inputs.at(portId)->getTypeName() << "\" in port " << portId;
96  return false;
97  }
98  }
99  }
100  return true;
101 }
102 } // namespace imstk
Compound Geometry.
void setNumOutputPorts(const size_t numPorts)
Get/Set the amount of output ports.
Base class for any geometrical representation.
Definition: imstkGeometry.h:22
void setInput(std::shared_ptr< Geometry > inputGeometry, size_t port=0)
Set the input at the port.
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 setNumInputPorts(const size_t numPorts)
Get/Set the amount of input ports.