iMSTK
Interactive Medical Simulation Toolkit
imstkCylinder.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 "imstkCylinder.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 Cylinder::getRadius(DataType type /* = DataType::PostTransform */)
22 {
23  if (type == DataType::PostTransform)
24  {
26  return m_radiusPostTransform;
27  }
28  return m_radius;
29 }
30 
31 double
32 Cylinder::getLength(DataType type /* = DataType::PostTransform */)
33 {
34  if (type == DataType::PostTransform)
35  {
37  return m_lengthPostTransform;
38  }
39  return m_length;
40 }
41 
42 void
43 Cylinder::setRadius(const double r)
44 {
45  CHECK(r > 0) << "error: radius should be positive.";
46 
47  if (m_radius == r)
48  {
49  return;
50  }
51 
52  m_radius = r;
53  m_transformApplied = false;
54  this->postModified();
55 }
56 
57 void
58 Cylinder::setLength(const double l)
59 {
60  if (l <= 0)
61  {
62  LOG(WARNING) << "error: length is negative.";
63  return;
64  }
65  if (m_length == l)
66  {
67  return;
68  }
69  m_length = l;
70  m_transformApplied = false;
71  this->postModified();
72 }
73 
74 void
75 Cylinder::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 = m_transform.block<3, 1>(0, 0).norm();
98  m_transformApplied = true;
99 }
100 
101 void
102 Cylinder::computeBoundingBox(Vec3d& min, Vec3d& max, const double imstkNotUsed(paddingPercent))
103 {
105 
106  const Vec3d orientationAxes = m_orientationPostTransform.toRotationMatrix().col(1);
107  const Vec3d d = orientationAxes * m_lengthPostTransform * 0.5;
108  const Vec3d p1 = m_positionPostTransform - d;
109  const Vec3d p2 = m_positionPostTransform + d;
110 
111  const Vec3d a = p2 - p1;
112  const double qSqrLength = a.dot(a);
113  const Vec3d e = m_radiusPostTransform * (Vec3d(1.0, 1.0, 1.0) - a.cwiseProduct(a).cwiseQuotient(Vec3d(qSqrLength, qSqrLength, qSqrLength))).cwiseSqrt();
114  min = (p1 - e).cwiseMin(p2 - e);
115  max = (p1 + e).cwiseMax(p2 + e);
116 }
117 } // namespace imstk
Mat4d m_transform
Transformation matrix.
double m_radiusPostTransform
Radius of the cylinder oncee transform applied.
Definition: imstkCylinder.h:88
void computeBoundingBox(Vec3d &min, Vec3d &max, const double paddingPercent)
Get the min, max of the AABB around the cylinder.
void updatePostTransformData() const override
Apply the global transform to the local parameters producing post transformed parameters.
double m_radius
Radius of the cylinder.
Definition: imstkCylinder.h:86
Compound Geometry.
void updatePostTransformData() const override
Update the Cylinder parameters applying the latest transform.
void postModified()
Post modified event.
double m_length
Length of the cylinder.
Definition: imstkCylinder.h:87
void print() const override
Print.
Quatd m_orientationPostTransform
orientation once transform is applied
double getLength(DataType type=DataType::PostTransform)
Get/Set the length of the cylinder.
double getRadius(DataType type=DataType::PostTransform)
Get/Set the radius of the cylinder.
double m_lengthPostTransform
Length of the cylinder onc transform applied.
Definition: imstkCylinder.h:89
void applyTransform(const Mat4d &m) override
Apply a user transform directly to (pre-transformed) parameters producing new parameters.
Vec3d m_positionPostTransform
position once transform applied
void applyTransform(const Mat4d &m) override
Apply a user transform directly to (pre-transformed) parameters producing new parameters.
void print() const override
Print the cylinder info.
DataType
Enumeration for the data to retrieve PreTransform for data where transform matrix is not applied Po...
Definition: imstkGeometry.h:41