iMSTK
Interactive Medical Simulation Toolkit
imstkSphere.h
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 #pragma once
8 
9 #include "imstkAnalyticalGeometry.h"
10 #include "imstkMacros.h"
11 
12 namespace imstk
13 {
19 class Sphere : public AnalyticalGeometry
20 {
21 public:
22  Sphere(const Vec3d& pos = Vec3d(0.0, 0.0, 0.0), const double radius = 1.0) :
24  {
25  setPosition(pos);
26  setRadius(radius);
28  }
29 
30  ~Sphere() override = default;
31 
32  IMSTK_TYPE_NAME(Sphere)
33 
34 
35  void print() const override;
38 
42  double getVolume() override { return 4.0 / 3.0 * PI * m_radius * m_radius * m_radius; }
43 
47  double getRadius(DataType type = DataType::PostTransform);
48  void setRadius(const double r);
50 
54  void computeBoundingBox(Vec3d& lowerCorner, Vec3d& upperCorner, const double paddingPercent = 0.0) override;
55 
59  double getFunctionValue(const Vec3d& pos) const override { return (pos - m_positionPostTransform).norm() - m_radiusPostTransform; }
60 
64  void updatePostTransformData() const override;
65 
70  std::unique_ptr<Sphere> clone()
71  {
72  return std::unique_ptr<Sphere>(cloneImplementation());
73  }
74 
75 protected:
76  void applyTransform(const Mat4d& m) override;
77 
78  double m_radius = 1.0;
79  mutable double m_radiusPostTransform = 1.0;
80 
81 private:
82  Sphere* cloneImplementation() const
83  {
84  return new Sphere(*this);
85  }
86 };
87 } // namespace imstk
std::unique_ptr< Sphere > clone()
Polymorphic clone, hides the declaration in superclass return own type.
Definition: imstkSphere.h:70
Base class for any analytical geometrical representation.
void computeBoundingBox(Vec3d &lowerCorner, Vec3d &upperCorner, const double paddingPercent=0.0) override
Get the min, max of the AABB around the sphere.
Definition: imstkSphere.cpp:44
void print() const override
Print the sphere info.
Definition: imstkSphere.cpp:13
Compound Geometry.
double getVolume() override
Returns the volume of the sphere.
Definition: imstkSphere.h:42
void updatePostTransformData() const override
Update the Sphere parameters applying the latest transform.
Definition: imstkSphere.cpp:71
void setPosition(const Vec3d p)
Set the local position.
double m_radius
Radius of the sphere.
Definition: imstkSphere.h:78
double m_radiusPostTransform
Radius of the sphere once transform applied.
Definition: imstkSphere.h:79
double getRadius(DataType type=DataType::PostTransform)
Get/Set the radius of the sphere.
Definition: imstkSphere.cpp:20
Represents a sphere via its position & radius.
Definition: imstkSphere.h:19
double getFunctionValue(const Vec3d &pos) const override
Returns signed distance to surface given position.
Definition: imstkSphere.h:59
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
void applyTransform(const Mat4d &m) override
Apply a user transform directly to (pre-transformed) parameters producing new parameters.
Definition: imstkSphere.cpp:59