iMSTK
Interactive Medical Simulation Toolkit
imstkCapsule.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 "imstkCapsule.h"
8 #include "imstkLogger.h"
9 
10 namespace imstk
11 {
12 void
14 {
16  LOG(INFO) << "Radius: " << m_radius;
17  LOG(INFO) << "Length: " << m_length;
18 }
19 
20 double
21 Capsule::getRadius(DataType type /* = DataType::PostTransform */)
22 {
23  if (type == DataType::PostTransform)
24  {
26  return m_radiusPostTransform;
27  }
28  return m_radius;
29 }
30 
31 void
32 Capsule::setRadius(const double r)
33 {
34  CHECK(r > 0) << "Capsule::setRadius error: radius should be positive.";
35  m_radius = r;
36  m_transformApplied = false;
37  this->postModified();
38 }
39 
40 double
41 Capsule::getLength(DataType type /* = DataType::PostTransform */)
42 {
43  if (type == DataType::PostTransform)
44  {
46  return m_lengthPostTransform;
47  }
48  return m_length;
49 }
50 
51 void
52 Capsule::setLength(const double l)
53 {
54  CHECK(l >= 0) << "Capsule::setLength error: length should be positive.";
55  m_length = l;
56  m_transformApplied = false;
57  this->postModified();
58 }
59 
60 double
61 Capsule::getFunctionValue(const Vec3d& x) const
62 {
63  // Two lines points
64  const Vec3d orientationAxes = m_orientationPostTransform.toRotationMatrix().col(1);
65  const Vec3d a = m_positionPostTransform + 0.5 * orientationAxes * m_lengthPostTransform;
66  const Vec3d b = 2.0 * m_positionPostTransform - a;
67 
68  const Vec3d pa = x - a;
69  const Vec3d ba = b - a;
70  const double h = std::min(std::max(pa.dot(ba) / ba.dot(ba), 0.0), 1.0);
71  return (pa - ba * h).norm() - m_radiusPostTransform;
72 }
73 
74 void
75 Capsule::applyTransform(const Mat4d& m)
76 {
78  const double s = std::sqrt(Vec3d(
79  m.block<3, 1>(0, 0).squaredNorm(),
80  m.block<3, 1>(0, 1).squaredNorm(),
81  m.block<3, 1>(0, 2).squaredNorm()).maxCoeff());
82  this->setRadius(m_radius * s);
83  this->setLength(m_length * s);
84  this->postModified();
85 }
86 
87 void
89 {
90  if (m_transformApplied)
91  {
92  return;
93  }
95  const double s = getScaling().maxCoeff();
98  m_transformApplied = true;
99 }
100 
101 void
102 Capsule::computeBoundingBox(Vec3d& min, Vec3d& max, const double imstkNotUsed(paddingPercent))
103 {
105 
106  const Vec3d orientationAxes = m_orientationPostTransform.toRotationMatrix().col(1);
107  const Vec3d l = m_lengthPostTransform * 0.5 * orientationAxes;
108  const Vec3d p1 = m_positionPostTransform - l;
109  const Vec3d p2 = m_positionPostTransform + l;
110 
111  min = p1.cwiseMin(p2);
112  max = p1.cwiseMax(p2);
115 }
116 } // namespace imstk
void print() const override
Print the capsule info.
double getLength(DataType type=DataType::PostTransform)
Get/Set the length of the capsule.
virtual void print() const
Print.
double getFunctionValue(const Vec3d &x) const override
Returns the signed distance to the capsule.
void updatePostTransformData() const override
Apply the global transform to the local parameters producing post transformed parameters.
Compound Geometry.
void applyTransform(const Mat4d &m) override
Apply a user transform directly to (pre-transformed) parameters producing new parameters.
Vec3d getScaling() const
Get/Set scaling.
double m_radius
Radius of the hemispheres at the end of the capsule.
Definition: imstkCapsule.h:90
void postModified()
Post modified event.
double m_length
Length between the centers of two hemispheres.
Definition: imstkCapsule.h:92
Quatd m_orientationPostTransform
orientation once transform is applied
void computeBoundingBox(Vec3d &min, Vec3d &max, const double paddingPercent)
Get the min, max of the AABB around the capsule.
void applyTransform(const Mat4d &m) override
Apply a user transform directly to (pre-transformed) parameters producing new parameters.
double getRadius(DataType type=DataType::PostTransform)
Get/Set the radius of the capsule.
double m_radiusPostTransform
Radius after transform.
Definition: imstkCapsule.h:91
void updatePostTransformData() const override
Update the Capsule parameters applying the latest transform.
Vec3d m_positionPostTransform
position once transform applied
DataType
Enumeration for the data to retrieve PreTransform for data where transform matrix is not applied Po...
Definition: imstkGeometry.h:41
double m_lengthPostTransform
Length after transform.
Definition: imstkCapsule.h:93