iMSTK
Interactive Medical Simulation Toolkit
imstkCompoundCD.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 "imstkCompoundCD.h"
8 
9 #include "imstkCollisionData.h"
10 #include "imstkCollisionUtils.h"
11 #include "imstkCompoundGeometry.h"
12 #include "imstkCDObjectFactory.h"
13 #include "imstkLogger.h"
14 #include "imstkParallelFor.h"
15 
16 namespace imstk
17 {
18 CompoundCD::CompoundCD()
19 {
20  setRequiredInputType<CompoundGeometry>(0);
21  setRequiredInputType<Geometry>(1);
22 
23  // Will be populated in the first run
24  m_collisionDataVector->clear();
25  setGenerateCD(true, true);
26 }
27 
28 void
30 {
31  ParallelUtils::parallelFor(m_cdAlgorithms.size(), [this](const int idx) { m_cdAlgorithms[idx]->update(); }, true);
32 }
33 
34 bool
36 {
37  // TODO check validation ...
39 
40  CHECK(valid) << "Invalid or missing inputs in CompoundCD";
41 
42  int otherIndex = 1;
43  auto compound = std::dynamic_pointer_cast<CompoundGeometry>(getInput(0));
44  if (compound == nullptr)
45  {
46  compound = std::dynamic_pointer_cast<CompoundGeometry>(getInput(1));
47  otherIndex = 0;
48  }
49  auto other = getInput(otherIndex);
50 
51  // disregard additions of geometry during runtime
52  if (m_cdAlgorithms.size() > 0)
53  {
54  return valid;
55  }
56 
57  for (size_t i = 0; i < compound->count(); ++i)
58  {
59  auto geom = compound->get(i);
60  auto type = CDObjectFactory::getCDType(*geom, *other);
61 
62  CHECK(type != getTypeName()) << "Can't stack a vector data algorithm inside of CompoundCD";
63 
64  if (type.empty())
65  {
66  LOG(WARNING) << "CompoundCD could not find a CD Algorithm for " << geom->getTypeName() << " and " <<
67  other->getTypeName() << " skipping.";
68  }
69  else
70  {
71  auto algorithm = CDObjectFactory::makeCollisionDetection(type);
72  algorithm->setInput(geom, 0);
73  algorithm->setInput(other, 1);
74  m_collisionDataVector->push_back(algorithm->getCollisionData());
75  m_cdAlgorithms.push_back(algorithm);
76  }
77  }
78 
79  return valid;
80 }
81 } // namespace imstk
bool areInputsValid() override
Check inputs are correct (always works reversibly)
void setGenerateCD(const bool generateA, const bool generateB)
If generateA is false, CD data will not be generated for input0,A Similarly, if generateB is false...
std::shared_ptr< Geometry > getInput(size_t port=0) const
Returns input geometry given port, returns nullptr if doesn&#39;t exist.
virtual bool areInputsValid() override
Check inputs are correct (always works reversibly)
Compound Geometry.
virtual const std::string getTypeName() const =0
Returns collision detection type string name.
static std::shared_ptr< CollisionDetectionAlgorithm > makeCollisionDetection(const std::string collisionTypeName)
attempts to create a new CD algorithm
void requestUpdate() override
Compute the collision data.
static std::string getCDType(const Geometry &obj1, const Geometry &obj2)
Get the CD type from the types of objects colliding.