CTL  0.6.1
Computed Tomography Library
voxelvolume.h
Go to the documentation of this file.
1 #ifndef CTL_VOXELVOLUME_H
2 #define CTL_VOXELVOLUME_H
3 
4 #include "chunk2d.h"
5 
6 namespace CTL {
7 
8 struct VoxelIndex;
9 struct VoxelCoordinates;
10 
16 {
17  uint x, y, z;
18 
19  bool operator==(const VoxelVolumeDimensions& other) const;
20  bool operator!=(const VoxelVolumeDimensions& other) const;
21 
22  std::string info() const;
23  size_t totalNbElements() const;
24 };
25 
31 {
32  float x, y, z;
33 
34  bool operator==(const VoxelVolumeVoxelSize& other) const;
35  bool operator!=(const VoxelVolumeVoxelSize& other) const;
36 
37  bool isIsotropic() const;
38  float product() const;
39 
40  std::string info() const;
41 };
42 
48 {
49  float x, y, z;
50 
51  bool operator==(const VoxelVolumeOffset& other) const;
52  bool operator!=(const VoxelVolumeOffset& other) const;
53 
54  std::string info() const;
55 };
56 
78 template <typename T>
79 class VoxelVolume
80 {
81 private:
82  template <class IteratorType>
84 
85 public:
88  using Offset = VoxelVolumeOffset;
89 
92  using reverse_iterator = std::reverse_iterator<iterator>;
93  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
94 
95  iterator begin();
96  iterator end();
97  const_iterator begin() const;
98  const_iterator end() const;
99  const_iterator cbegin() const;
100  const_iterator cend() const;
101  reverse_iterator rbegin();
102  reverse_iterator rend();
103  const_reverse_iterator rbegin() const;
104  const_reverse_iterator rend() const;
105  const_reverse_iterator crbegin() const;
106  const_reverse_iterator crend() const;
107 
108  // ctors (no data set)
109  explicit VoxelVolume(const Dimensions& nbVoxels);
111  VoxelVolume(uint nbVoxelX, uint nbVoxelY, uint nbVoxelZ);
112  VoxelVolume(uint nbVoxelX, uint nbVoxelY, uint nbVoxelZ, float xSize, float ySize, float zSize);
113 
114  // ctors (with data set)
115  VoxelVolume(const Dimensions& nbVoxels, std::vector<T> data);
116  VoxelVolume(const Dimensions& nbVoxels, const VoxelSize& voxelSize, std::vector<T> data);
117  VoxelVolume(uint nbVoxelX, uint nbVoxelY, uint nbVoxelZ, std::vector<T> data);
118  VoxelVolume(uint nbVoxelX,
119  uint nbVoxelY,
120  uint nbVoxelZ,
121  float xSize,
122  float ySize,
123  float zSize,
124  std::vector<T> data);
125 
126  VoxelVolume(const VoxelVolume&) = default;
127  VoxelVolume(VoxelVolume&&) = default;
128  VoxelVolume& operator=(const VoxelVolume&) = default;
129  VoxelVolume& operator=(VoxelVolume&&) = default;
130 
131  // dtor (virtual)
132  virtual ~VoxelVolume() = default;
133 
134  // factory
135  static VoxelVolume<T> fromChunk2DStack(const std::vector<Chunk2D<T>>& stack);
136  static VoxelVolume<T> ball(float radius, float voxelSize, const T& fillValue);
137  static VoxelVolume<T> cube(uint nbVoxel, float voxelSize, const T& fillValue);
138  static VoxelVolume<T> cylinderX(float radius, float height, float voxelSize, const T& fillValue);
139  static VoxelVolume<T> cylinderY(float radius, float height, float voxelSize, const T& fillValue);
140  static VoxelVolume<T> cylinderZ(float radius, float height, float voxelSize, const T& fillValue);
141 
142  // getter methods
143  size_t allocatedElements() const;
144  const std::vector<T>& constData() const;
145  const std::vector<T>& data() const;
146  std::vector<T>& data();
147  const Dimensions& dimensions() const;
148  bool hasData() const;
149  const Dimensions& nbVoxels() const;
150  const Offset& offset() const;
151  T* rawData();
152  const T* rawData() const;
153  size_t totalVoxelCount() const;
154  const VoxelSize& voxelSize() const;
155 
156  // setter methods
157  void setData(std::vector<T>&& data);
158  void setData(const std::vector<T>& data);
159  void setVolumeOffset(const Offset& offset);
160  void setVolumeOffset(float xOffset, float yOffset, float zOffset);
161  void setVoxelSize(const VoxelSize& size);
162  void setVoxelSize(float xSize, float ySize, float zSize);
163  void setVoxelSize(float isotropicSize);
164 
165  // other methods
166  void allocateMemory();
167  void allocateMemory(const T& initValue);
169  VoxelCoordinates coordinates(uint x, uint y, uint z) const;
172  VoxelIndex index(float x_mm, float y_mm, float z_mm) const;
173  bool isIsotropic() const;
174  void fill(const T& fillValue);
175  void freeMemory();
176  T max() const;
177  T min() const;
178  VoxelVolume<T> reslicedByX(bool reverse = false) const;
179  VoxelVolume<T> reslicedByY(bool reverse = false) const;
180  VoxelVolume<T> reslicedByZ(bool reverse = false) const;
181  Chunk2D<T> sliceX(uint slice) const;
182  Chunk2D<T> sliceY(uint slice) const;
183  Chunk2D<T> sliceZ(uint slice) const;
184  float smallestVoxelSize() const;
186 
187  typename std::vector<T>::reference operator()(uint x, uint y, uint z);
188  typename std::vector<T>::const_reference operator()(uint x, uint y, uint z) const;
189  typename std::vector<T>::reference operator()(const VoxelIndex& index);
190  typename std::vector<T>::const_reference operator()(const VoxelIndex& index) const;
191 
194  VoxelVolume<T>& operator+=(const T& additiveShift);
195  VoxelVolume<T>& operator-=(const T& subtractiveShift);
196  VoxelVolume<T>& operator*=(const T& factor);
197  VoxelVolume<T>& operator/=(const T& divisor);
198 
199  VoxelVolume<T> operator+(const VoxelVolume<T>& other) const;
200  VoxelVolume<T> operator-(const VoxelVolume<T>& other) const;
201  VoxelVolume<T> operator+(const T& additiveShift) const;
202  VoxelVolume<T> operator-(const T& subtractiveShift) const;
203  VoxelVolume<T> operator*(const T& factor) const;
204  VoxelVolume<T> operator/(const T& divisor) const;
205 
206 protected:
208  VoxelSize _size = { 0.0f, 0.0f, 0.0f };
209  Offset _offset = { 0.0f, 0.0f, 0.0f };
210 
211  std::vector<T> _data;
212 
213 private:
214  bool hasEqualSizeAs(const std::vector<T>& other) const;
215 
216  template <class Function>
217  void parallelExecution(const Function& f) const;
218 };
219 
221 {
222  using Generic3DCoord::Generic3DCoord;
223 
224  float& x() { return coord1(); }
225  float& y() { return coord2(); }
226  float& z() { return coord3(); }
227  const float& x() const { return coord1(); }
228  const float& y() const { return coord2(); }
229  const float& z() const { return coord3(); }
230 };
231 
233 {
234  using Generic3DIndex::Generic3DIndex;
235 
236  uint& x() { return idx1(); }
237  uint& y() { return idx2(); }
238  uint& z() { return idx3(); }
239  const uint& x() const { return idx1(); }
240  const uint& y() const { return idx2(); }
241  const uint& z() const { return idx3(); }
242 };
243 
244 // iterators
245 template <class T>
246 template <class IteratorType>
247 class VoxelVolume<T>::VoxelIterator
248 {
249 public:
250  using iterator_category = std::bidirectional_iterator_tag;
251  using value_type = typename IteratorType::value_type;
252  using difference_type = typename IteratorType::difference_type;
253  using pointer = typename IteratorType::pointer;
254  using reference = typename IteratorType::reference;
255 
256  VoxelIterator(IteratorType voxel = {}, const VoxelVolume<T>* ptrToVol = nullptr);
257 
258  friend bool operator==(const VoxelIterator& left, const VoxelIterator& right)
259  {
260  return left._dataItr == right._dataItr;
261  }
262  friend bool operator!=(const VoxelIterator& left, const VoxelIterator& right)
263  {
264  return left._dataItr != right._dataItr;
265  }
266 
267  VoxelIterator& operator++();
268  VoxelIterator operator++(int);
269  VoxelIterator& operator--();
270  VoxelIterator operator--(int);
271 
272  reference operator*() const { return *_dataItr; }
273  pointer operator->() const { return _dataItr.operator->(); }
274 
275  operator VoxelIterator<typename std::vector<T>::const_iterator>() const;
276 
277  reference value() const;
278  VoxelIndex voxelIndex() const;
279 
280 private:
281  IteratorType _dataItr;
282  const VoxelVolume<T>* _ptrToVol;
283 };
284 
285 namespace assist {
286 
287 template <typename ToType, typename FromType, typename ConversionFun = ToType (*)(const FromType&)>
288 VoxelVolume<ToType> convertTo(const VoxelVolume<FromType>& volume,
289  ConversionFun f = [](const FromType& val) { return static_cast<ToType>(val); });
290 float interpolate3D(const VoxelVolume<float>& volume, const mat::Matrix<3, 1>& position);
291 float interpolate3D(const VoxelVolume<float>& volume, double x, double y, double z);
292 
293 } // namespace assist
294 
295 using assist::convertTo;
296 
297 } // namespace CTL
298 
299 #include "voxelvolume.tpp"
300 
303 #endif // CTL_VOXELVOLUME_H
void setData(std::vector< T > &&data)
Definition: voxelvolume.tpp:1130
bool operator!=(const VoxelVolumeDimensions &other) const
Definition: voxelvolume.tpp:812
bool operator==(const VoxelVolumeVoxelSize &other) const
Definition: voxelvolume.tpp:836
static VoxelVolume< T > cube(uint nbVoxel, float voxelSize, const T &fillValue)
Definition: voxelvolume.tpp:182
Definition: voxelvolume.h:220
VoxelVolume< T > & operator-=(const VoxelVolume< T > &other)
Definition: voxelvolume.tpp:624
std::string info() const
Definition: voxelvolume.tpp:820
VoxelVolume< T > operator-(const VoxelVolume< T > &other) const
Definition: voxelvolume.tpp:746
void setVolumeOffset(const Offset &offset)
Definition: voxelvolume.tpp:1080
std::vector< T > _data
The internal data of the volume.
Definition: voxelvolume.h:211
Definition: coordinates.h:24
Chunk2D< T > sliceY(uint slice) const
Definition: voxelvolume.tpp:454
Definition: voxelvolume.h:15
void allocateMemory()
Definition: voxelvolume.tpp:925
const Dimensions & dimensions() const
Definition: voxelvolume.tpp:1120
static VoxelVolume< T > ball(float radius, float voxelSize, const T &fillValue)
Definition: voxelvolume.tpp:199
const Dimensions & nbVoxels() const
Definition: voxelvolume.tpp:1012
bool operator==(const VoxelVolumeDimensions &other) const
Definition: voxelvolume.tpp:804
T min() const
Definition: voxelvolume.tpp:580
void parallelExecution(const Function &f) const
Definition: voxelvolume.tpp:1165
std::vector< T >::reference operator()(uint x, uint y, uint z)
Definition: voxelvolume.tpp:382
T max() const
Definition: voxelvolume.tpp:568
The VoxelVolume class provides a simple container for storage of voxelized 3D volume data.
Definition: ctsystemview.h:14
Definition: voxelvolume.h:83
size_t totalNbElements() const
Definition: voxelvolume.tpp:828
Definition: voxelvolume.h:30
static VoxelVolume< T > cylinderX(float radius, float height, float voxelSize, const T &fillValue)
Definition: voxelvolume.tpp:222
Dimensions _dim
The dimensions of the volume.
Definition: voxelvolume.h:207
void fill(const T &fillValue)
Definition: voxelvolume.tpp:948
The Chunk2D class provides a simple container for storage of 2D image data.
Definition: chunk2d.h:51
Definition: voxelvolume.h:232
bool operator!=(const VoxelVolumeOffset &other) const
Definition: voxelvolume.tpp:888
bool operator==(const VoxelVolumeOffset &other) const
Definition: voxelvolume.tpp:879
const Offset & offset() const
Definition: voxelvolume.tpp:1021
VoxelVolume< T > operator+(const VoxelVolume< T > &other) const
Definition: voxelvolume.tpp:728
VoxelVolume(const Dimensions &nbVoxels)
Definition: voxelvolume.tpp:21
float smallestVoxelSize() const
Definition: voxelvolume.tpp:1071
static VoxelVolume< T > cylinderZ(float radius, float height, float voxelSize, const T &fillValue)
Definition: voxelvolume.tpp:270
void freeMemory()
Definition: voxelvolume.tpp:372
VoxelCoordinates coordinates(const VoxelIndex &index) const
Definition: voxelvolume.tpp:1183
bool isIsotropic() const
Definition: voxelvolume.tpp:1001
bool hasEqualSizeAs(const std::vector< T > &other) const
Definition: voxelvolume.tpp:1155
static VoxelVolume< T > fromChunk2DStack(const std::vector< Chunk2D< T >> &stack)
Definition: voxelvolume.tpp:145
VoxelCoordinates cornerVoxel() const
Definition: voxelvolume.tpp:1210
Definition: voxelvolume.h:47
VoxelVolume< T > reslicedByX(bool reverse=false) const
Definition: voxelvolume.tpp:504
VoxelSize _size
The size of individual voxels (in mm).
Definition: voxelvolume.h:208
Definition: coordinates.h:52
T * rawData()
Definition: voxelvolume.tpp:1032
std::string info() const
Definition: voxelvolume.tpp:896
Chunk2D< T > sliceZ(uint slice) const
Definition: voxelvolume.tpp:480
Offset _offset
The positional offset of the volume (in mm).
Definition: voxelvolume.h:209
unsigned int uint
Qt style alias for unsigned int.
Definition: modulelayout.h:6
static VoxelVolume< T > cylinderY(float radius, float height, float voxelSize, const T &fillValue)
Definition: voxelvolume.tpp:246
Chunk2D< ToType > convertTo(const Chunk2D< FromType > &chunk2d, ConversionFun f=[](const FromType &val) { return static_cast< ToType >(val);})
Returns a Chunk2D<ToType>, where ToType must be specified as a template parameter.
Definition: chunk2d.tpp:737
VoxelVolume< T > & operator/=(const T &divisor)
Definition: voxelvolume.tpp:705
size_t allocatedElements() const
Definition: voxelvolume.tpp:911
VoxelVolume< T > reslicedByZ(bool reverse=false) const
Definition: voxelvolume.tpp:550
VoxelVolume< T > reslicedByY(bool reverse=false) const
Definition: voxelvolume.tpp:527
VoxelVolume< T > operator *(const T &factor) const
Definition: voxelvolume.tpp:782
std::string info() const
Definition: voxelvolume.tpp:853
const std::vector< T > & data() const
Definition: voxelvolume.tpp:969
float product() const
Definition: voxelvolume.tpp:862
void setVoxelSize(const VoxelSize &size)
Definition: voxelvolume.tpp:1099
size_t totalVoxelCount() const
Definition: voxelvolume.tpp:1053
VoxelVolume< T > & operator+=(const VoxelVolume< T > &other)
Definition: voxelvolume.tpp:597
VoxelIndex index(const VoxelCoordinates &coordinates) const
Definition: voxelvolume.tpp:1226
VoxelVolume< T > operator/(const T &divisor) const
Definition: voxelvolume.tpp:793
bool isIsotropic() const
Definition: voxelvolume.tpp:871
bool operator!=(const VoxelVolumeVoxelSize &other) const
Definition: voxelvolume.tpp:845
const VoxelSize & voxelSize() const
Definition: voxelvolume.tpp:1062
bool hasData() const
Definition: voxelvolume.tpp:989
Chunk2D< T > sliceX(uint slice) const
Definition: voxelvolume.tpp:430
VoxelVolume< T > & operator *=(const T &factor)
Definition: voxelvolume.tpp:686
const std::vector< T > & constData() const
Definition: voxelvolume.tpp:960
VoxelCoordinates volumeCorner() const
Definition: voxelvolume.tpp:1260