iMSTK
Interactive Medical Simulation Toolkit
imstkUnidirectionalPlaneToSphereCD.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 "imstkUnidirectionalPlaneToSphereCD.h"
8 #include "imstkCollisionData.h"
9 #include "imstkCollisionUtils.h"
10 #include "imstkPlane.h"
11 #include "imstkSphere.h"
12 
13 namespace imstk
14 {
15 UnidirectionalPlaneToSphereCD::UnidirectionalPlaneToSphereCD()
16 {
17  setRequiredInputType<Plane>(0);
18  setRequiredInputType<Sphere>(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<Plane> plane = std::dynamic_pointer_cast<Plane>(geomA);
29  std::shared_ptr<Sphere> sphere = std::dynamic_pointer_cast<Sphere>(geomB);
30 
31  // Get geometry properties
32  const Vec3d spherePos = sphere->getPosition();
33  const double r = sphere->getRadius();
34  const Vec3d planePos = plane->getPosition();
35  const Vec3d n = plane->getNormal();
36 
37  Vec3d planeContactPt, sphereContactPt;
38  Vec3d planeContactNormal, sphereContactNormal;
39  double depth;
40  if (CollisionUtils::testPlaneToSphere(
41  planePos, n,
42  spherePos, r,
43  planeContactPt, planeContactNormal,
44  sphereContactPt, sphereContactNormal,
45  depth))
46  {
48  elemA.dir = planeContactNormal; // Direction to resolve plane
49  elemA.pt = planeContactPt;
50  elemA.penetrationDepth = depth;
51 
53  elemB.dir = sphereContactNormal; // Direction to resolve sphere
54  elemB.pt = sphereContactPt;
55  elemB.penetrationDepth = depth;
56 
57  elementsA.push_back(elemA);
58  elementsB.push_back(elemB);
59  }
60 }
61 } // namespace imstk
Vec3d getPosition(DataType type=DataType::PostTransform)
Get the local or global position (post transformed)
Represents and infinite plane, width can be used for visual purposes.
Definition: imstkPlane.h:19
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.