iMSTK
Interactive Medical Simulation Toolkit
imstkSphereToCylinderCD.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 "imstkSphereToCylinderCD.h"
8 #include "imstkCollisionData.h"
9 #include "imstkCollisionUtils.h"
10 #include "imstkCylinder.h"
11 #include "imstkSphere.h"
12 
13 namespace imstk
14 {
15 SphereToCylinderCD::SphereToCylinderCD()
16 {
17  setRequiredInputType<Sphere>(0);
18  setRequiredInputType<Cylinder>(1);
19 }
20 
21 void
23  std::shared_ptr<Geometry> geomA,
24  std::shared_ptr<Geometry> geomB,
25  std::vector<CollisionElement>& elementsA,
26  std::vector<CollisionElement>& elementsB)
27 {
28  std::shared_ptr<Sphere> sphere = std::dynamic_pointer_cast<Sphere>(geomA);
29  std::shared_ptr<Cylinder> cylinder = std::dynamic_pointer_cast<Cylinder>(geomB);
30 
31  // Get geometry properties
32  const Vec3d spherePos = sphere->getPosition();
33  const double rSphere = sphere->getRadius();
34 
35  const Vec3d cylinderPos = cylinder->getPosition();
36  const Vec3d cylinderAxis = cylinder->getOrientation().toRotationMatrix().col(1);
37  const double rCylinder = cylinder->getRadius();
38  const double cylLength = cylinder->getLength();
39 
40  Vec3d sphereContactPt, cylinderContactPt;
41  Vec3d sphereContactNormal, cylinderContactNormal;
42  double depth;
43  if (CollisionUtils::testSphereToCylinder(
44  spherePos, rSphere,
45  cylinderPos, cylinderAxis, rCylinder, cylLength,
46  sphereContactPt, sphereContactNormal,
47  cylinderContactPt, cylinderContactNormal,
48  depth))
49  {
51  elemA.dir = sphereContactNormal; // Direction to resolve sphere
52  elemA.pt = sphereContactPt;
53  elemA.penetrationDepth = depth;
54 
56  elemB.dir = cylinderContactNormal; // Direction to resolve cylinder
57  elemB.pt = cylinderContactPt;
58  elemB.penetrationDepth = depth;
59 
60  elementsA.push_back(elemA);
61  elementsB.push_back(elemB);
62  }
63 }
64 } // namespace imstk
Cylinder geometry, default configuration is at origin with length running up the y axes...
Definition: imstkCylinder.h:19
Vec3d getPosition(DataType type=DataType::PostTransform)
Get the local or global position (post transformed)
Compound Geometry.
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 AB simultaneously.
Represents a sphere via its position & radius.
Definition: imstkSphere.h:19
Direclty gives a point-direction contact as its collision data.