iMSTK
Interactive Medical Simulation Toolkit
imstkCDObjectFactory.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 #include "imstkCDObjectFactory.h"
7 
8 #include "imstkBidirectionalPlaneToSphereCD.h"
9 #include "imstkCapsuleToCapsuleCD.h"
10 #include "imstkClosedSurfaceMeshToCapsuleCD.h"
11 #include "imstkClosedSurfaceMeshToMeshCD.h"
12 #include "imstkCompoundCD.h"
13 #include "imstkGeometry.h"
14 #include "imstkImplicitGeometryToPointSetCCD.h"
15 #include "imstkImplicitGeometryToPointSetCD.h"
16 #include "imstkLineMeshToCapsuleCD.h"
17 #include "imstkLineMeshToLineMeshCCD.h"
18 #include "imstkLineMeshToSphereCD.h"
19 #include "imstkPointSetToCapsuleCD.h"
20 #include "imstkPointSetToCylinderCD.h"
21 #include "imstkPointSetToOrientedBoxCD.h"
22 #include "imstkPointSetToPlaneCD.h"
23 #include "imstkPointSetToSphereCD.h"
24 #include "imstkSphereToCapsuleCD.h"
25 #include "imstkSphereToCylinderCD.h"
26 #include "imstkSphereToSphereCD.h"
27 #include "imstkSurfaceMeshToCapsuleCD.h"
28 #include "imstkSurfaceMeshToSphereCD.h"
29 #include "imstkSurfaceMeshToSurfaceMeshCD.h"
30 #include "imstkTetrahedralMesh.h"
31 #include "imstkTetraToLineMeshCD.h"
32 #include "imstkTetraToPointSetCD.h"
33 #include "imstkUnidirectionalPlaneToCapsuleCD.h"
34 #include "imstkUnidirectionalPlaneToSphereCD.h"
35 
36 namespace imstk
37 {
38 IMSTK_REGISTER_COLLISION_DETECTION(BidirectionalPlaneToSphereCD);
39 IMSTK_REGISTER_COLLISION_DETECTION(CapsuleToCapsuleCD);
40 IMSTK_REGISTER_COLLISION_DETECTION(ClosedSurfaceMeshToCapsuleCD);
41 IMSTK_REGISTER_COLLISION_DETECTION(ClosedSurfaceMeshToMeshCD);
42 IMSTK_REGISTER_COLLISION_DETECTION(ImplicitGeometryToPointSetCD);
43 IMSTK_REGISTER_COLLISION_DETECTION(ImplicitGeometryToPointSetCCD);
44 IMSTK_REGISTER_COLLISION_DETECTION(LineMeshToLineMeshCCD);
45 IMSTK_REGISTER_COLLISION_DETECTION(LineMeshToSphereCD);
46 IMSTK_REGISTER_COLLISION_DETECTION(LineMeshToCapsuleCD);
47 IMSTK_REGISTER_COLLISION_DETECTION(PointSetToCapsuleCD);
48 IMSTK_REGISTER_COLLISION_DETECTION(PointSetToCylinderCD);
49 IMSTK_REGISTER_COLLISION_DETECTION(PointSetToPlaneCD);
50 IMSTK_REGISTER_COLLISION_DETECTION(PointSetToSphereCD);
51 IMSTK_REGISTER_COLLISION_DETECTION(PointSetToOrientedBoxCD);
52 IMSTK_REGISTER_COLLISION_DETECTION(SphereToCapsuleCD);
53 IMSTK_REGISTER_COLLISION_DETECTION(SphereToCylinderCD);
54 IMSTK_REGISTER_COLLISION_DETECTION(SphereToSphereCD);
55 IMSTK_REGISTER_COLLISION_DETECTION(SurfaceMeshToSurfaceMeshCD);
56 IMSTK_REGISTER_COLLISION_DETECTION(SurfaceMeshToCapsuleCD);
57 IMSTK_REGISTER_COLLISION_DETECTION(SurfaceMeshToSphereCD);
58 IMSTK_REGISTER_COLLISION_DETECTION(TetraToPointSetCD);
59 IMSTK_REGISTER_COLLISION_DETECTION(TetraToLineMeshCD);
60 IMSTK_REGISTER_COLLISION_DETECTION(UnidirectionalPlaneToSphereCD);
61 IMSTK_REGISTER_COLLISION_DETECTION(UnidirectionalPlaneToCapsuleCD);
62 IMSTK_REGISTER_COLLISION_DETECTION(CompoundCD);
63 
64 // Map types so order does not matter
65 #define IMSTK_MAP_TYPES(geomA, geomB, cdType) \
66  { std::string(#geomA) + std::string(#geomB), std::string(#cdType) }, \
67  { std::string(#geomB) + std::string(#geomA), std::string(#cdType) } \
68 
69 
70 std::shared_ptr<CollisionDetectionAlgorithm>
71 CDObjectFactory::makeCollisionDetection(const std::string collisionTypeName)
72 {
73  if (collisionTypeName == "MeshToMeshBruteForceCD")
74  {
75  LOG(WARNING) << "MeshToMeshBruteForceCD deprecated. Using ClosedSurfaceMeshToMeshCD instead.";
76  return create("ClosedSurfaceMeshToMeshCD");
77  }
78  if (!contains(collisionTypeName))
79  {
80  LOG(FATAL) << "No collision detection type named: " << collisionTypeName;
81  return nullptr;
82  }
83  else
84  {
85  return create(collisionTypeName);
86  }
87 }
88 
89 std::string
91  const Geometry& obj1,
92  const Geometry& obj2)
93 {
94  std::string type1 = obj1.getTypeName();
95  std::string type2 = obj2.getTypeName();
96 
97  // Map defining collision detection for different geometry types, default options
98  // NOTE HS 20221221 changed some AnalyticalGeometry to Mesh CD from
99  // ImplicitGeometryToPointSet to PointSetTo<Geometry> due to issues with
100  // ImplicitGeoemtryToPointSetCD
101 
102  static std::unordered_map<std::string, std::string> cdTypeMap = {
103  IMSTK_MAP_TYPES(Plane, Capsule, UnidirectionalPlaneToCapsuleCD),
104  IMSTK_MAP_TYPES(Plane, Sphere, BidirectionalPlaneToSphereCD),
105  IMSTK_MAP_TYPES(Plane, LineMesh, PointSetToPlaneCD),
106  IMSTK_MAP_TYPES(Plane, PointSet, PointSetToPlaneCD),
107  IMSTK_MAP_TYPES(Plane, SurfaceMesh, PointSetToPlaneCD),
110  IMSTK_MAP_TYPES(Sphere, Sphere, SphereToSphereCD),
111  IMSTK_MAP_TYPES(Sphere, Capsule, SphereToCapsuleCD),
112  IMSTK_MAP_TYPES(Sphere, PointSet, PointSetToSphereCD),
113  IMSTK_MAP_TYPES(Sphere, LineMesh, LineMeshToSphereCD),
114  IMSTK_MAP_TYPES(Sphere, SurfaceMesh, SurfaceMeshToSphereCD),
117  IMSTK_MAP_TYPES(Capsule, Capsule, CapsuleToCapsuleCD),
118  IMSTK_MAP_TYPES(Capsule, PointSet, PointSetToCapsuleCD),
119  IMSTK_MAP_TYPES(Capsule, LineMesh, LineMeshToCapsuleCD),
120  IMSTK_MAP_TYPES(Capsule, SurfaceMesh, SurfaceMeshToCapsuleCD),
121  // IMSTK_MAP_TYPES(Capsule, SurfaceMesh, ClosedSurfaceMeshToCapsuleCD),
124  IMSTK_MAP_TYPES(Cylinder, Sphere, SphereToCylinderCD),
125  IMSTK_MAP_TYPES(Cylinder, PointSet, PointSetToCylinderCD),
130  IMSTK_MAP_TYPES(OrientedBox, PointSet, PointSetToOrientedBoxCD),
131  IMSTK_MAP_TYPES(OrientedBox, LineMesh, PointSetToOrientedBoxCD),
136  IMSTK_MAP_TYPES(LineMesh, LineMesh, LineMeshToLineMeshCCD),
151 
152  // CompoundGeometry, uses CompoundCD for all types,
153  // the check if CD actually exists is done at runtime
154  IMSTK_MAP_TYPES(CompoundGeometry, Plane, CompoundCD),
155  IMSTK_MAP_TYPES(CompoundGeometry, Sphere, CompoundCD),
156  IMSTK_MAP_TYPES(CompoundGeometry, Capsule, CompoundCD),
157  IMSTK_MAP_TYPES(CompoundGeometry, Cylinder, CompoundCD),
158  IMSTK_MAP_TYPES(CompoundGeometry, OrientedBox, CompoundCD),
159  IMSTK_MAP_TYPES(CompoundGeometry, PointSet, CompoundCD),
160  IMSTK_MAP_TYPES(CompoundGeometry, HexahedralMesh, CompoundCD),
161  IMSTK_MAP_TYPES(CompoundGeometry, LineMesh, CompoundCD),
162  IMSTK_MAP_TYPES(CompoundGeometry, SurfaceMesh, CompoundCD)
163  };
164 
165  if (cdTypeMap.find(type1 + type2) == cdTypeMap.end())
166  {
167  LOG(INFO) << "No valid collision detection type for : " << type1 + type2;
168  return std::string("");
169  }
170 
171  return cdTypeMap[type1 + type2];
172 }
173 } // namespace imstk
OrientedBox geometry, specified with extents (half lengths)
Sphere to Capsule collision detection.
PointSet to OrientedBox collision detection. Generates point-direction contact data. By default only generates contact data for the pointset.
Base class for all geometries represented by discrete points and elements The pointsets follow a pipe...
Definition: imstkPointSet.h:25
Hexahedral mesh class.
Capsule to Capsule collision detection.
Sphere to sphere collision detection Generates point-direction contact data. By default generates con...
Cylinder geometry, default configuration is at origin with length running up the y axes...
Definition: imstkCylinder.h:19
virtual const std::string getTypeName() const =0
Returns the string representing the type name of the geometry.
Represents and infinite plane, width can be used for visual purposes.
Definition: imstkPlane.h:19
Compound Geometry.
static std::shared_ptr< CollisionDetectionAlgorithm > makeCollisionDetection(const std::string collisionTypeName)
attempts to create a new CD algorithm
LineMesh to Sphere collision detection Generates point-edge and point-point CD data By default only g...
Represents a set of tetrahedrons & vertices via an array of Vec3d double vertices & Vec4i integer ind...
Base class for any geometrical representation.
Definition: imstkGeometry.h:22
Closed mesh to mesh collision with brute force strategy. It can handle closed SurfaceMesh vs PointSet...
PointSet to sphere collision detection Generates point-direction contact data. By default only genera...
Base class for all volume mesh types.
Definition: imstkLineMesh.h:18
Represents a set of triangles & vertices via an array of Vec3d double vertices & Vec3i integer indice...
Represents a sphere via its position & radius.
Definition: imstkSphere.h:19
ImplicitGeometry to PointSet collision detection. This generates PointDirection collision data via si...
static std::string getCDType(const Geometry &obj1, const Geometry &obj2)
Get the CD type from the types of objects colliding.
SurfaceMesh to Sphere collision detection Generates vertex-triangle, point-edge, and point-point CD d...
Plane to sphere collision detection Generates point-direction contact data.
Structured field of signed distances implemented with ImageData The SDF differs in that when you scal...
Plane to capsule collision detection Generates point-direction contact data. By default only generate...
PointSet to Cylinder collision detection. Generates point-direction contact data. By default only gen...
LineMesh to LineMesh continous collision detection. This CCD method can process self collision as wel...
Collision detection that supports a geometry consisting of multiple subgeometries. For the actual calcualation the information gets passed to the appropriate shape/subshape CD algorithm. Currently Does not support adding/removing a shape during runtime.
Sphere-Cylinder collision detection Generates point-direction contact data. By default generates cont...
Capsule geometry, default configuration is centered at origin with length running up and down the y a...
Definition: imstkCapsule.h:21
PointSet to unidirectional plane collision detection Generates point-direction contact data...
PointSet to Capsule collision detection. Generates point-direction contact data. By default only gene...
static std::shared_ptr< CollisionDetectionAlgorithm > create(const std::string &name, Args &&... args)
tries to construct the object give name, it will forward the given paramters
Definition: imstkFactory.h:35
LineMesh to Capsule collision detection Generates point-edge and point-point CD data By default only ...
SurfaceMesh to Capsule collision detection Generates vertex-triangle, point-edge, and point-point CD ...