iMSTK
Interactive Medical Simulation Toolkit
imstkTetraToLineMeshCD.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 "imstkTetraToLineMeshCD.h"
8 #include "imstkCollisionData.h"
9 #include "imstkCollisionUtils.h"
10 #include "imstkLineMesh.h"
11 #include "imstkParallelUtils.h"
12 #include "imstkTetrahedralMesh.h"
13 
14 namespace imstk
15 {
16 TetraToLineMeshCD::TetraToLineMeshCD()
17 {
18  setRequiredInputType<TetrahedralMesh>(0);
19  setRequiredInputType<LineMesh>(1);
20 }
21 
22 void
24  std::shared_ptr<Geometry> geomA,
25  std::shared_ptr<Geometry> geomB,
26  std::vector<CollisionElement>& elementsA,
27  std::vector<CollisionElement>& elementsB)
28 {
29  std::shared_ptr<TetrahedralMesh> tetMesh = std::dynamic_pointer_cast<TetrahedralMesh>(geomA);
30  std::shared_ptr<LineMesh> lineMesh = std::dynamic_pointer_cast<LineMesh>(geomB);
31 
32  std::shared_ptr<VecDataArray<int, 4>> tetsPtr = tetMesh->getCells();
33  std::shared_ptr<VecDataArray<double, 3>> tetVerticesPtr = tetMesh->getVertexPositions();
34  const VecDataArray<int, 4>& tets = *tetsPtr;
35  const VecDataArray<double, 3>& tetVerts = *tetVerticesPtr;
36 
37  std::shared_ptr<VecDataArray<int, 2>> linesPtr = lineMesh->getCells();
38  std::shared_ptr<VecDataArray<double, 3>> verticesPtr = lineMesh->getVertexPositions();
39  const VecDataArray<int, 2>& lines = *linesPtr;
40  const VecDataArray<double, 3>& lineVerts = *verticesPtr;
41 
42  // Brute force
44  ParallelUtils::parallelFor(lines.size(), [&](int i)
45  {
46  const Vec3d& x0 = lineVerts[lines[i][0]];
47  const Vec3d& x1 = lineVerts[lines[i][1]];
48  for (int j = 0; j < tets.size(); j++)
49  {
50  std::array<Vec3d, 4> tet;
51  tet[0] = tetVerts[tets[j][0]];
52  tet[1] = tetVerts[tets[j][1]];
53  tet[2] = tetVerts[tets[j][2]];
54  tet[3] = tetVerts[tets[j][3]];
55  if (CollisionUtils::testTetToSegment(tet, x0, x1))
56  {
57  CellIndexElement elemA;
58  elemA.ids[0] = j;
59  elemA.idCount = 1;
60  elemA.cellType = IMSTK_TETRAHEDRON;
61 
62  CellIndexElement elemB;
63  elemB.ids[0] = i;
64  elemB.idCount = 1;
65  elemB.cellType = IMSTK_EDGE;
66 
67  lock.lock();
68  elementsA.push_back(elemA);
69  elementsB.push_back(elemB);
70  lock.unlock();
71  }
72  }
73  });
74 }
75 } // namespace imstk
The SpinLock class.
Definition: imstkSpinLock.h:20
void unlock()
End a thread-safe region.
Definition: imstkSpinLock.h:53
void computeCollisionDataAB(std::shared_ptr< Geometry > geomA, std::shared_ptr< Geometry > geomB, std::vector< CollisionElement > &elementsA, std::vector< CollisionElement > &elementsB) override
Compute collision data for both sides simultaneously.
Compound Geometry.
Represents a set of tetrahedrons & vertices via an array of Vec3d double vertices & Vec4i integer ind...
void lock()
Start a thread-safe region, where only one thread can execute at a time until a call to the unlock fu...
Definition: imstkSpinLock.h:45
Base class for all volume mesh types.
Definition: imstkLineMesh.h:18
Represents a cell by a single cell id OR by N vertex ids. Which case can be determined by the idCount...