SIRF  3.5.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  void fill(const ImageData& im)
75  {
76  Iterator_const& src = im.begin();
77  Iterator& dst = this->begin();
78  Iterator& end = this->end();
79  for (; dst != end; ++dst, ++src)
80  *dst = *src;
81  }
82  void fill(float v)
83  {
84  Iterator& dst = this->begin();
85  Iterator& end = this->end();
86  FloatRef fr(&v);
87  for (; dst != end; ++dst)
88  *dst = fr;
89  }
91  //virtual void write(const std::string &filename) const = 0;
92  virtual bool operator==(const ImageData& id) const
93  {
94  if (&id == this)
95  return true;
98  if (gi_self != gi_other)
99  return false;
100  float s = 0.0f;
101  float sx = 0.0f;
102  float sy = 0.0f;
103  complex_float_t zx;
104  complex_float_t zy;
105  Iterator_const& x = this->begin();
106  Iterator_const& y = id.begin();
107  for (; x != this->end(); ++x, ++y) {
108  zx = (*x).complex_float();
109  zy = (*y).complex_float();
110  sx += std::abs(zx*zx);
111  sy += std::abs(zy*zy);
112  zx -= zy;
113  s += std::abs(zx*zx);
114  }
115  float t = std::max(sx, sy);
116  bool same = (s <= 1e-6*t);
117  return same;
118  }
119  virtual bool operator!=(const ImageData& id) const
120  {
121  return !(*this == id);
122  }
124  std::shared_ptr<const VoxelisedGeometricalInfo3D > get_geom_info_sptr() const
125  {
126  // If the geometrical info has not been created yet, throw an error
127  if (!_geom_info_sptr)
128  throw std::runtime_error("Geometrical info not initialised. This implies that"
129  " your constructor did not call set_up_geom_info() or there was "
130  "an error. Build in debug mode and lookout for any printed text "
131  "containing '::set_up_geom_info()'.");
132  return _geom_info_sptr;
133  }
135  std::unique_ptr<ImageData> clone() const
136  {
137  return std::unique_ptr<ImageData>(this->clone_impl());
138  }
140  virtual bool is_complex() const { return false; }
142  virtual void reorient(const VoxelisedGeometricalInfo3D &);
144  static bool can_reorient(const VoxelisedGeometricalInfo3D &geom_1, const VoxelisedGeometricalInfo3D &geom_2, const bool throw_error);
146  virtual void set_up_geom_info() = 0;
147  protected:
149  virtual ImageData* clone_impl() const = 0;
151  void set_geom_info(const std::shared_ptr<VoxelisedGeometricalInfo3D> geom_info_sptr) { _geom_info_sptr = geom_info_sptr; }
152  private:
153  std::shared_ptr<VoxelisedGeometricalInfo3D> _geom_info_sptr;
154  };
155 }
156 
157 #endif
Definition: ANumRef.h:53
Definition: DataContainer.h:42
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:92
virtual bool is_complex() const
Is complex? Unless overwridden (Gadgetron), assume not complex.
Definition: ImageData.h:140
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:135
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:124
void set_geom_info(const std::shared_ptr< VoxelisedGeometricalInfo3D > geom_info_sptr)
Set geom info.
Definition: ImageData.h:151
Definition: GeometricalInfo.h:50
Abstract data container.
Definition: GeometricalInfo.cpp:141