iMSTK
Interactive Medical Simulation Toolkit
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
imstkSignedDistanceField.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 "imstkDataArray.h"
10 #include "imstkImageData.h"
11 #include "imstkImplicitGeometry.h"
12 
13 namespace imstk
14 {
15 //class ImageData;
16 //template<typename T> class DataArray;
17 
27 {
28 public:
34  SignedDistanceField(std::shared_ptr<ImageData> imageData);
35  ~SignedDistanceField() override = default;
36 
37  IMSTK_TYPE_NAME(SignedDistanceField)
38 
39 
40  double getFunctionValue(const Vec3d& pos) const;
43 
48  inline double getFunctionValueCoord(const Vec3i& coord) const
49  {
50  if (coord[0] < m_imageDataSdf->getDimensions()[0] && coord[0] > 0
51  && coord[1] < m_imageDataSdf->getDimensions()[1] && coord[1] > 0
52  && coord[2] < m_imageDataSdf->getDimensions()[2] && coord[2] > 0)
53  {
54  return (*m_scalars)[m_imageDataSdf->getScalarIndex(coord)] * m_scale;
55  }
56  else
57  {
58  return std::numeric_limits<double>::min();
59  }
60  }
61 
65  const Vec6d& getBounds() const { return m_bounds; }
66 
70  void setScale(const double scale) { m_scale = scale; }
71 
75  double getScale() const { return m_scale; }
76 
80  std::shared_ptr<ImageData> getImage() const { return m_imageDataSdf; }
81 
82  void computeBoundingBox(Vec3d& min, Vec3d& max, const double paddingPercent) override;
83 
88  std::unique_ptr<SignedDistanceField> clone()
89  {
90  return std::unique_ptr<SignedDistanceField>(cloneImplementation());
91  }
92 
93 protected:
94  std::shared_ptr<ImageData> m_imageDataSdf;
95 
96  Vec3d m_invSpacing;
97  Vec6d m_bounds;
98  Vec3d m_shift;
99  double m_scale;
100 
101  std::shared_ptr<DataArray<double>> m_scalars;
102 
103 private:
104  SignedDistanceField* cloneImplementation() const
105  {
106  SignedDistanceField* geom = new SignedDistanceField(*this);
107  // Deal with deep copy members
108  geom->m_imageDataSdf = m_imageDataSdf->clone();
109  geom->m_scalars = std::dynamic_pointer_cast<DataArray<double>>(geom->m_imageDataSdf->getScalars());
110  return geom;
111  }
112 };
113 } // namespace imstk
const Vec6d & getBounds() const
Returns the bounds of the field.
SignedDistanceField(std::shared_ptr< ImageData > imageData)
Constructor.
Compound Geometry.
std::unique_ptr< SignedDistanceField > clone()
Polymorphic clone, hides the declaration in superclass return own type.
void scale(const Vec3d &scaling, TransformType type=TransformType::ConcatenateToTransform)
Scale in Cartesian directions.
double getFunctionValueCoord(const Vec3i &coord) const
Returns signed distance to surface at coordinate inlined for performance.
void setScale(const double scale)
Set the isotropic scale that is used/multplied with samples.
double getScale() const
Get the isotropic scale.
void computeBoundingBox(Vec3d &min, Vec3d &max, const double paddingPercent) override
Compute the bounding box for the geometry.
Structured field of signed distances implemented with ImageData The SDF differs in that when you scal...
std::shared_ptr< ImageData > getImage() const
Get the SDF as a float image.
Class that can represent the geometry of multiple implicit geometries as boolean functions One may su...
double getFunctionValue(const Vec3d &pos) const
Returns signed distance to surface at pos, returns clamped/nearest if out of bounds.