iMSTK
Interactive Medical Simulation Toolkit
imstkSphereToSphereCD.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 "imstkSphereToSphereCD.h"
8 #include "imstkCollisionData.h"
9 #include "imstkCollisionUtils.h"
10 #include "imstkSphere.h"
11 
12 namespace imstk
13 {
14 SphereToSphereCD::SphereToSphereCD() : CollisionDetectionAlgorithm()
15 {
16  setRequiredInputType<Sphere>(0);
17  setRequiredInputType<Sphere>(1);
18 }
19 
20 void
22  std::shared_ptr<Geometry> geomA,
23  std::shared_ptr<Geometry> geomB,
24  std::vector<CollisionElement>& elementsA,
25  std::vector<CollisionElement>& elementsB)
26 {
27  std::shared_ptr<Sphere> sphereA = std::dynamic_pointer_cast<Sphere>(geomA);
28  std::shared_ptr<Sphere> sphereB = std::dynamic_pointer_cast<Sphere>(geomB);
29 
30  // Get geometry properties
31  const Vec3d sphereAPos = sphereA->getPosition();
32  const double rA = sphereA->getRadius();
33  const Vec3d sphereBPos = sphereB->getPosition();
34  const double rB = sphereB->getRadius();
35 
36  Vec3d sphereAContactPt, sphereBContactPt;
37  Vec3d sphereAContactNormal, sphereBContactNormal;
38  double depth;
39  if (CollisionUtils::testSphereToSphere(
40  sphereAPos, rA, sphereBPos, rB,
41  sphereAContactPt, sphereAContactNormal,
42  sphereBContactPt, sphereBContactNormal,
43  depth))
44  {
46  elemA.dir = sphereAContactNormal; // Direction to resolve sphereA
47  elemA.pt = sphereAContactPt; // Contact point on sphereA
48  elemA.penetrationDepth = depth;
49 
51  elemB.dir = sphereBContactNormal; // Direction to resolve sphereB
52  elemB.pt = sphereBContactPt; // Contact point on sphereB
53  elemB.penetrationDepth = depth;
54 
55  elementsA.push_back(elemA);
56  elementsB.push_back(elemB);
57  }
58 }
59 } // namespace imstk
Vec3d getPosition(DataType type=DataType::PostTransform)
Get the local or global position (post transformed)
Compound Geometry.
Represents a sphere via its position & radius.
Definition: imstkSphere.h:19
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.
Direclty gives a point-direction contact as its collision data.