iMSTK
Interactive Medical Simulation Toolkit
imstkCollisionHandling.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 "imstkCollisionHandling.h"
8 #include "imstkCollidingObject.h"
9 
10 namespace imstk
11 {
12 void
13 CollisionHandling::updateCollisionData(std::shared_ptr<const CollisionData> data)
14 {
15  // Get the geometry and elements
16  const std::vector<CollisionElement>* a = &data->elementsA;
17  const std::vector<CollisionElement>* b = &data->elementsB;
18 
19  std::shared_ptr<Geometry> handleGeomA = getHandlingGeometryA();
20  std::shared_ptr<Geometry> handleGeomB = getHandlingGeometryB();
21 
22  bool flipSides = false;
23  // If the geometry of the CD exists on the input object B then flip
24  if (data->geomA != nullptr
25  && m_inputObjectB != nullptr
26  && handleGeomB != nullptr
27  && data->geomA == handleGeomB)
28  {
29  flipSides = true;
30  }
31  if (data->geomB != nullptr
32  && m_inputObjectA != nullptr
33  && handleGeomA != nullptr
34  && data->geomB == handleGeomA)
35  {
36  flipSides = true;
37  }
38 
39  if (flipSides)
40  {
41  std::swap(a, b);
42  }
43  handle(*a, *b);
44 }
45 
46 std::shared_ptr<Geometry>
48 {
49  return (m_inputObjectA == nullptr) ? nullptr : m_inputObjectA->getCollidingGeometry();
50 }
51 
52 std::shared_ptr<Geometry>
53 CollisionHandling::getHandlingGeometryB()
54 {
55  return (m_inputObjectB == nullptr) ? nullptr : m_inputObjectB->getCollidingGeometry();
56 }
57 
58 void
59 CollisionHandling::setInputCollisionData(std::shared_ptr<CollisionData> collisionData)
60 {
61  m_colData = collisionData;
62  m_colVectorData = nullptr;
63  m_updateFunction = [this]() {
64  if (m_colData == nullptr)
65  {
66  return;
67  }
68  m_clearData = true;
69  m_processConstraints = true;
70  updateCollisionData(m_colData);
71  };
72 }
73 
74 void
75 CollisionHandling::setInputCollisionData(std::shared_ptr<std::vector<std::shared_ptr<CollisionData>>> collisionVectorData)
76 {
77  m_colVectorData = collisionVectorData;
78  m_colData = nullptr;
79 
80  m_updateFunction = [this]() {
81  m_clearData = true;
82  m_processConstraints = false;
83  for (size_t i = 0; i < m_colVectorData->size(); ++i)
84  {
85  // CCD accesses this ...
86  m_colData = m_colVectorData->at(i);
87  m_processConstraints = i == m_colVectorData->size() - 1;
88  updateCollisionData(m_colData);
89  m_clearData = false;
90  }
91  m_colData = nullptr;
92  };
93 }
94 } // namespace imstk
std::shared_ptr< const CollisionData > m_colData
Collision data.
std::shared_ptr< std::vector< std::shared_ptr< CollisionData > > > m_colVectorData
Expansion to allow collision detection to return multiple types of collision data.
void setInputCollisionData(std::shared_ptr< CollisionData > collisionData)
Set/Get the input collision data used for handling.
Compound Geometry.
virtual std::shared_ptr< Geometry > getHandlingGeometryA()
Get the geometry used for handling defaults to the collision geometry.
virtual void handle(const std::vector< CollisionElement > &elementsA, const std::vector< CollisionElement > &elementsB)=0
Handle the input collision data. Elements will be flipped (if needed) such that elementsA corresponds...