iMSTK
Interactive Medical Simulation Toolkit
imstkImageData.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 "imstkMacros.h"
10 #include "imstkPointSet.h"
11 #include "imstkTypes.h"
12 
13 namespace imstk
14 {
15 class AbstractDataArray;
16 
22 class ImageData : public PointSet
23 {
24 public:
25  ImageData() = default;
26  ~ImageData() override = default;
27 
28  IMSTK_TYPE_NAME(ImageData)
29 
30 
31  void print() const override;
34 
38  double getVolume() override;
39 
43  const Vec3i& getDimensions() const { return m_dims; }
44 
48  int getNumComponents() const { return m_numComps; }
49 
53  void* getVoidPointer();
54 
58  inline size_t getScalarIndex(int x, int y, int z = 0) { return getScalarIndex(x, y, z, m_dims, m_numComps); }
59  inline size_t getScalarIndex(const Vec3i& imgCoord) { return getScalarIndex(imgCoord[0], imgCoord[1], imgCoord[2], m_dims, m_numComps); }
61 
66  inline static size_t getScalarIndex(int x, int y, int z, const Vec3i& dims, int numComps) { return (x + dims[0] * (y + z * dims[1])) * numComps; }
67 
68  std::shared_ptr<ImageData> cast(ScalarTypeId type);
69 
73  const Vec3d& getInvSpacing() const { return m_invSpacing; }
74 
78  const ScalarTypeId getScalarType() const;
79 
83  const Vec6d& getBounds()
84  {
85  const Vec3d size = Vec3d(
86  m_spacing.x() * m_dims[0],
87  m_spacing.y() * m_dims[1],
88  m_spacing.z() * m_dims[2]);
89 
90  m_bounds[0] = m_origin.x();
91  m_bounds[1] = m_origin.x() + size.x();
92  m_bounds[2] = m_origin.y();
93  m_bounds[3] = m_origin.y() + size.y();
94  m_bounds[4] = m_origin.z();
95  m_bounds[5] = m_origin.z() + size.z();
96 
97  return m_bounds;
98  }
99 
100  void computeBoundingBox(Vec3d& lowerCorner, Vec3d& upperCorner, const double imstkNotUsed(paddingPercent)) override
101  {
102  const Vec6d& bounds = getBounds();
103  lowerCorner = Vec3d(bounds[0], bounds[2], bounds[4]);
104  upperCorner = Vec3d(bounds[1], bounds[3], bounds[5]);
105  }
106 
110  std::shared_ptr<AbstractDataArray> getScalars() const { return m_scalarArray; }
111  void setScalars(std::shared_ptr<AbstractDataArray> scalars, const int numComps, int* dim);
113 
117  const Vec3d& getOrigin() const { return m_origin; }
118  void setOrigin(const Vec3d& origin) { m_origin = origin; }
120 
124  const Vec3d& getSpacing() const { return m_spacing; }
125  void setSpacing(const Vec3d& spacing)
126  {
127  m_spacing = spacing;
128  m_invSpacing = Vec3d(1.0 / spacing[0], 1.0 / spacing[1], 1.0 / spacing[2]);
129  }
130 
132 
136  void allocate(const ScalarTypeId type, const int numComps, const Vec3i& dims, const Vec3d& spacing = Vec3d(1.0, 1.0, 1.0), const Vec3d& origin = Vec3d(0.0, 0.0, 0.0));
137 
141  void computePoints();
142 
146  void clear() override;
147 
152  std::unique_ptr<ImageData> clone()
153  {
154  return std::unique_ptr<ImageData>(cloneImplementation());
155  }
156 
157 private:
158  // ImageData does not use transform, consider splitting into separate class
159  using Geometry::translate;
160  using Geometry::rotate;
161  using Geometry::scale;
162  using Geometry::transform;
163  using Geometry::setRotation;
164  using Geometry::setScaling;
165  using Geometry::setTranslation;
166  using Geometry::setTransform;
167 
168 protected:
169  std::shared_ptr<AbstractDataArray> m_scalarArray = nullptr;
170  Vec3i m_dims = Vec3i(0, 0, 0);
171  int m_numComps = 1;
172  Vec3d m_origin = Vec3d(0.0, 0.0, 0.0);
173  Vec3d m_spacing = Vec3d(1.0, 1.0, 1.0);
174  Vec3d m_invSpacing = Vec3d(1.0, 1.0, 1.0);
175  Vec6d m_bounds = Vec6d::Zero();
176 
177 private:
178  ImageData* cloneImplementation() const;
179 };
180 } // namespace imstk
Base class for all geometries represented by discrete points and elements The pointsets follow a pipe...
Definition: imstkPointSet.h:25
std::unique_ptr< ImageData > clone()
Polymorphic clone, hides the declaration in superclass return own type.
void rotate(const Quatd &q, TransformType type=TransformType::ConcatenateToTransform)
Rotate the geometry in Cartesian space.
size_t getScalarIndex(int x, int y, int z=0)
Returns index of data in scalar array given structured image coordinate, does no bounds checking...
const Vec3i & getDimensions() const
Returns the dimensions of the image.
const ScalarTypeId getScalarType() const
Returns the scalar type of the image.
Compound Geometry.
void transform(const Mat4d &T, TransformType type=TransformType::ConcatenateToTransform)
Applies a rigid transform to the geometry.
void allocate(const ScalarTypeId type, const int numComps, const Vec3i &dims, const Vec3d &spacing=Vec3d(1.0, 1.0, 1.0), const Vec3d &origin=Vec3d(0.0, 0.0, 0.0))
Allocate image by type.
const Vec3d & getInvSpacing() const
Returns inv spacing of the image.
static size_t getScalarIndex(int x, int y, int z, const Vec3i &dims, int numComps)
Returns index of data in scalar array given structured image coordinate, dimensions, and number of components does no bounds checking.
double getVolume() override
Returns the volume.
void computePoints()
Generates points in the geometry from the ImageData (center of each voxel)
void scale(const Vec3d &scaling, TransformType type=TransformType::ConcatenateToTransform)
Scale in Cartesian directions.
std::shared_ptr< AbstractDataArray > getScalars() const
Get/Set the scalars.
const Vec3d & getSpacing() const
Get/Set the spacing between pixels/voxels of the image.
const Vec6d & getBounds()
Returns bounds of the image (voxel to voxel)
void clear() override
Clear the data.
void translate(const Vec3d &t, TransformType type=TransformType::ConcatenateToTransform)
Translate the geometry in Cartesian space.
int getNumComponents() const
Returns the number of components of the image.
const Vec3d & getOrigin() const
Get/Set the origin of the image.
void print() const override
Print the image data info.
Class to represent 1, 2, or 3D image data (i.e. structured points)
void * getVoidPointer()
Returns a pointer to the underlying storage of the image.