iMSTK
Interactive Medical Simulation Toolkit
imstkImageData.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 "imstkImageData.h"
8 #include "imstkLogger.h"
9 #include "imstkVecDataArray.h"
10 
11 namespace imstk
12 {
13 void
15 {
17  LOG(INFO) << "Scalar Type: " << static_cast<int>(m_scalarArray->getScalarType());
18  LOG(INFO) << "Number of Components" << m_numComps;
19  LOG(INFO) << "Dimensions: " << m_dims[0] << ", " << m_dims[1] << ", " << m_dims[2];
20  LOG(INFO) << "Spacing: " << m_spacing[0] << ", " << m_spacing[1] << ", " << m_spacing[2];
21  LOG(INFO) << "Origin: " << m_origin[0] << ", " << m_origin[1] << ", " << m_origin[2];
22  LOG(INFO) << "Bounds: ";
23  LOG(INFO) << "\t" << m_bounds[0] << ", " << m_bounds[1];
24  LOG(INFO) << "\t" << m_bounds[2] << ", " << m_bounds[3];
25  LOG(INFO) << "\t" << m_bounds[4] << ", " << m_bounds[5];
26 }
27 
28 double
30 {
31  return (m_dims[0] * m_spacing.x()) *
32  (m_dims[1] * m_spacing.y()) *
33  (m_dims[2] * m_spacing.z());
34 }
35 
36 void*
38 {
39  return m_scalarArray->getVoidPointer();
40 }
41 
42 std::shared_ptr<ImageData>
43 ImageData::cast(ScalarTypeId toType)
44 {
45  // Create image of new type
46  std::shared_ptr<ImageData> results = std::make_shared<ImageData>();
47  results->setOrigin(m_origin);
48  results->setSpacing(m_spacing);
49  results->setScalars(getScalars()->cast(toType), m_numComps, m_dims.data());
50  return results;
51 }
52 
53 const ScalarTypeId
55 {
56  return m_scalarArray->getScalarType();
57 }
58 
59 void
60 ImageData::setScalars(std::shared_ptr<AbstractDataArray> scalars, const int numComps, int* dim)
61 {
62  CHECK(scalars != nullptr);
63  if (dim[0] * dim[1] * dim[2] * numComps != scalars->size())
64  {
65  LOG(WARNING) << "Scalars don't align";
66  }
67  m_scalarArray = scalars;
68  m_dims[0] = dim[0];
69  m_dims[1] = dim[1];
70  m_dims[2] = dim[2];
71  m_numComps = numComps;
72 }
73 
74 void
75 ImageData::allocate(const ScalarTypeId type, const int numComps, const Vec3i& dims, const Vec3d& spacing, const Vec3d& origin)
76 {
77  m_dims = dims;
78  m_origin = origin;
79  setSpacing(spacing);
80  m_numComps = numComps;
81  const int numVals = dims[0] * dims[1] * dims[2];
82  switch (type)
83  {
84  TemplateMacro(m_scalarArray = std::make_shared<DataArray<IMSTK_TT>>(numVals * numComps); );
85  default:
86  LOG(WARNING) << "Tried to allocate unknown scalar type";
87  break;
88  }
89  this->postModified();
90 }
91 
92 void
94 {
95  std::shared_ptr<VecDataArray<double, 3>> vertices =
96  std::make_shared<VecDataArray<double, 3>>(m_dims[0] * m_dims[1] * m_dims[2]);
97  VecDataArray<double, 3>& vertexData = *vertices;
98  const Vec3d shift = m_origin + m_spacing * 0.5;
99  int i = 0;
100  for (int z = 0; z < m_dims[2]; z++)
101  {
102  for (int y = 0; y < m_dims[1]; y++)
103  {
104  for (int x = 0; x < m_dims[0]; x++, i++)
105  {
106  vertexData[i] = Vec3d(x, y, z).cwiseProduct(m_spacing) + shift;
107  }
108  }
109  }
110  setInitialVertexPositions(vertices);
111  setVertexPositions(vertices);
112 }
113 
114 void
116 {
117  if (this->m_scalarArray != nullptr)
118  {
119  this->m_scalarArray = nullptr;
120  }
121  //this->m_dataTransform->Identity();
122  this->postModified();
123 }
124 
125 ImageData*
126 ImageData::cloneImplementation() const
127 {
128  ImageData* geom = new ImageData(*this);
129  // Deal with deep copy members
130  geom->m_scalarArray = m_scalarArray->clone();
131  return geom;
132 }
133 } // namespace imstk
virtual void print() const override
Print the mesh info.
const ScalarTypeId getScalarType() const
Returns the scalar type of the image.
Compound 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.
void postModified()
Post modified event.
double getVolume() override
Returns the volume.
void computePoints()
Generates points in the geometry from the ImageData (center of each voxel)
std::shared_ptr< AbstractDataArray > getScalars() const
Get/Set the scalars.
void setInitialVertexPositions(std::shared_ptr< VecDataArray< double, 3 >> vertices)
Sets initial positions from an array.
void clear() override
Clear the data.
void setVertexPositions(std::shared_ptr< VecDataArray< double, 3 >> positions)
Sets current vertex positions of the mesh.
void print() const override
Print the image data info.
Simple dynamic array implementation that also supports event posting and viewing/facade.
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.