SIRF  3.8.0
ImageData.h
1 /*
2 SyneRBI Synergistic Image Reconstruction Framework (SIRF)
3 Copyright 2018 - 2020 Rutherford Appleton Laboratory STFC
4 Copyright 2018 - 2020 University College London
5 
6 This is software developed for the Collaborative Computational
7 Project in Synergistic Reconstruction for Biomedical Imaging (formerly CCP PETMR)
8 (http://www.ccpsynerbi.ac.uk/).
9 
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13 http://www.apache.org/licenses/LICENSE-2.0
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 
20 */
21 #pragma once
22 
23 #ifndef SIRF_ABSTRACT_IMAGE_DATA_TYPE
24 #define SIRF_ABSTRACT_IMAGE_DATA_TYPE
25 
26 #include "sirf/common/ANumRef.h"
27 #include "sirf/common/DataContainer.h"
28 #include "sirf/common/ANumRef.h"
29 #include "sirf/common/GeometricalInfo.h"
30 
36 namespace sirf {
37  class ImageData : public DataContainer
38  {
39  public:
40  virtual ~ImageData() {}
41  virtual Dimensions dimensions() const = 0; // to go to DataContainer eventually
42  //virtual void get_data(void* data) const = 0;
43  //virtual void set_data(const void* data) = 0;
44  class Iterator {
45  public:
46  virtual ~Iterator() {}
47  virtual Iterator& operator++() = 0;
48  virtual ANumRef& operator*() = 0;
49  virtual bool operator==(const Iterator&) const = 0;
50  virtual bool operator!=(const Iterator&) const = 0;
51  };
53  public:
54  virtual ~Iterator_const() {}
55  virtual Iterator_const& operator++() = 0;
56  virtual const ANumRef& operator*() const = 0;
57  virtual bool operator==(const Iterator_const&) const = 0;
58  virtual bool operator!=(const Iterator_const&) const = 0;
59  };
60  virtual Iterator& begin() = 0;
61  virtual Iterator_const& begin() const = 0;
62  virtual Iterator& end() = 0;
63  virtual Iterator_const& end() const = 0;
64  virtual void scale(float s) = 0;
65  virtual bool ordered() const
66  {
67  return true;
68  }
69  void copy(Iterator_const& src, Iterator& dst, Iterator& end) const
70  {
71  for (; dst != end; ++dst, ++src)
72  *dst = *src;
73  }
74  size_t size() const
75  {
76  Dimensions dim = dimensions();
77  if (is_empty())
78  return 0;
79  size_t n = 1;
80  for (std::map<std::string, int>::iterator it = dim.begin(); it != dim.end(); ++it) {
81  n *= it->second;
82  }
83  return n;
84  }
85 
86  void fill(const ImageData& im)
87  {
88  Iterator_const& src = im.begin();
89  Iterator& dst = this->begin();
90  Iterator& end = this->end();
91  for (; dst != end; ++dst, ++src)
92  *dst = *src;
93  }
94  void fill(float v)
95  {
96  Iterator& dst = this->begin();
97  Iterator& end = this->end();
98  FloatRef fr(&v);
99  for (; dst != end; ++dst)
100  *dst = fr;
101  }
103  //virtual void write(const std::string &filename) const = 0;
104  virtual bool operator==(const ImageData& id) const
105  {
106  if (&id == this)
107  return true;
110  if (gi_self != gi_other)
111  return false;
112  float s = 0.0f;
113  float sx = 0.0f;
114  float sy = 0.0f;
115  complex_float_t zx;
116  complex_float_t zy;
117  Iterator_const& x = this->begin();
118  Iterator_const& y = id.begin();
119  for (; x != this->end(); ++x, ++y) {
120  zx = (*x).complex_float();
121  zy = (*y).complex_float();
122  sx += std::abs(zx*zx);
123  sy += std::abs(zy*zy);
124  zx -= zy;
125  s += std::abs(zx*zx);
126  }
127  float t = std::max(sx, sy);
128  bool same = (s <= 1e-6*t);
129  return same;
130  }
131  virtual bool operator!=(const ImageData& id) const
132  {
133  return !(*this == id);
134  }
136  std::shared_ptr<const VoxelisedGeometricalInfo3D > get_geom_info_sptr() const
137  {
138  // If the geometrical info has not been created yet, throw an error
139  if (!_geom_info_sptr)
140  throw std::runtime_error("Geometrical info not initialised. This implies that"
141  " your constructor did not call set_up_geom_info() or there was "
142  "an error. Build in debug mode and lookout for any printed text "
143  "containing '::set_up_geom_info()'.");
144  return _geom_info_sptr;
145  }
147  std::unique_ptr<ImageData> clone() const
148  {
149  return std::unique_ptr<ImageData>(this->clone_impl());
150  }
152  virtual bool is_complex() const { return false; }
154  virtual void reorient(const VoxelisedGeometricalInfo3D &);
156  static bool can_reorient(const VoxelisedGeometricalInfo3D &geom_1, const VoxelisedGeometricalInfo3D &geom_2, const bool throw_error);
158  virtual void set_up_geom_info() = 0;
159  protected:
161  virtual ImageData* clone_impl() const = 0;
163  void set_geom_info(const std::shared_ptr<VoxelisedGeometricalInfo3D> geom_info_sptr) { _geom_info_sptr = geom_info_sptr; }
164  private:
165  std::shared_ptr<VoxelisedGeometricalInfo3D> _geom_info_sptr;
166  };
167 }
168 
169 #endif
Definition: ANumRef.h:53
Abstract data container with numerical operations.
Definition: DataContainer.h:58
Definition: GeometricalInfo.h:32
Definition: ImageData.h:52
Definition: ImageData.h:44
Definition: ImageData.h:38
virtual ImageData * clone_impl() const =0
Clone helper function. Don't use.
static bool can_reorient(const VoxelisedGeometricalInfo3D &geom_1, const VoxelisedGeometricalInfo3D &geom_2, const bool throw_error)
Can reorient? (check dimensions and spacing)
Definition: ImageData.cpp:30
virtual bool operator==(const ImageData &id) const
Write image to file.
Definition: ImageData.h:104
virtual bool is_complex() const
Is complex? Unless overwridden (Gadgetron), assume not complex.
Definition: ImageData.h:152
virtual void set_up_geom_info()=0
Populate the geometrical info metadata (from the image's own metadata)
std::unique_ptr< ImageData > clone() const
Clone and return as unique pointer.
Definition: ImageData.h:147
virtual void reorient(const VoxelisedGeometricalInfo3D &)
Reorient image. Requires that dimesions and spacing match.
Definition: ImageData.cpp:25
std::shared_ptr< const VoxelisedGeometricalInfo3D > get_geom_info_sptr() const
Get geometrical info.
Definition: ImageData.h:136
void set_geom_info(const std::shared_ptr< VoxelisedGeometricalInfo3D > geom_info_sptr)
Set geom info.
Definition: ImageData.h:163
Definition: GeometricalInfo.h:50
Abstract base class for SIRF image data.
Definition: GeometricalInfo.cpp:141