JEMRIS  2.8.1
open-source MRI simulations
NDData.h
1 #ifndef __ND_DATA_H__
2 #define __ND_DATA_H__
3 
4 #include "TPOI.h"
5 
6 #include <H5public.h>
7 
8 
9 #include <iostream>
10 #include <numeric>
11 #include <string>
12 #include <vector>
13 #include <algorithm>
14 #include <assert.h>
15 
16 template<class T>
17 struct T_minmax {
18  T min;
19  T max;
20 };
21 template<class T> inline std::ostream&
22 operator<< (std::ostream& os, const T_minmax<T>& mm) {
23  return os << "[" << mm.min << ", " << mm.max << "]";
24 }
25 
26 template<class T> inline static T
27 _prod (const T a, const T b) {return (a*b);}
28 template<class T> inline static T
29 prod (const std::vector<T>& v) {
30  return std::accumulate(v.begin(), v.end(), (T)1, _prod<T>);
31 }
32 template<class T> inline static T
33 sum (const std::vector<T>& v) {
34  return std::accumulate(v.begin(), v.end(), (T)0);
35 }
36 template<class T> inline const T_minmax<T>
37 minmax (const std::vector<T>& v) {
38  T_minmax<T> ret;
39  for (size_t i = 0; i < v.size(); ++i){
40  ret.min = std::min(ret.min,v[i]);
41  ret.max = std::max(ret.max,v[i]);
42  }
43  return ret;
44 }
45 
46 const static std::string SLASH ("/");
47 const static std::string DSLASH ("//");
48 
52 template<class T>
53 class NDData {
54 
55 
56 protected:
57 
58  std::vector<size_t> _dims;
59  std::vector<T> _data;
61 public:
62 
66  NDData () {};
67 
73  NDData (const size_t n0) {
74  _dims.resize(1,n0);
75  _data.resize(prod(_dims));
76  }
77 
84  NDData (const size_t n0, const size_t n1) {
85  _dims.resize(2);
86  _dims[0] = n0;
87  _dims[1] = n1;
88  _data.resize(prod(_dims));
89  }
90 
98  NDData (const size_t n0, const size_t n1, const size_t n2) {
99  _dims.resize(3);
100  _dims[0] = n0;
101  _dims[1] = n1;
102  _dims[2] = n2;
103  _data.resize(prod(_dims));
104  }
105 
113  NDData (const size_t n0, const size_t n1, const size_t n2, const size_t n3) {
114  _dims.resize(3);
115  _dims[0] = n0;
116  _dims[1] = n1;
117  _dims[2] = n2;
118  _dims[3] = n3;
119  _data.resize(prod(_dims));
120  }
121 
127  NDData (const std::vector<hsize_t>& dims) {
128  _dims.resize(dims.size());
129  std::reverse_copy (dims.begin(), dims.end(), _dims.begin());
130  _data.resize(prod(_dims));
131  }
132 
138  NDData (const std::vector<size_t>& dims) {
139  _dims = dims;
140  _data.resize(prod(_dims));
141  }
142 
148  NDData (const NDData& data) {
149  *this = data;
150  }
151 
157  NDData& operator= (const NDData& data) {
158  _dims = data._dims;
159  _data = data._data;
160  return *this;
161  }
162 
166  inline size_t Dim (const size_t n = 0) const { return _dims[n]; }
167 
168  inline std::vector<size_t> Dims () const { return _dims; }
169 
170  inline size_t Size () const { return _data.size(); }
171 
172  inline size_t NDim() const { return _dims.size(); }
173 
174  std::ostream& Print (std::ostream& os) const {
175  os << "dims(";
176  for (size_t i = 0; i < _dims.size(); ++i)
177  os << _dims[i] << " ";
178  os << ") range(" << minmax(_data) << ")";
179  return os;
180  }
181 
182  inline bool Empty () const { return _data.empty(); }
183 
184  inline T& operator[] (const size_t p) { return _data[p]; }
185 
186  inline T operator[] (const size_t p) const { return _data[p]; }
187 
188  inline T& operator() (const size_t p) { return _data[p]; }
189 
190  inline T operator() (const size_t p) const { return _data[p]; }
191 
192  inline T& operator() (const size_t n0, const size_t n1)
193  { return _data[n1*Dim(0)+n0]; }
194 
195  inline T operator() (const size_t n0, const size_t n1) const
196  { return _data[n1*Dim(0)+n0]; }
197 
198  inline T& operator() (const size_t n0, const size_t n1, const size_t n2) {
199  return _data[n2*Dim(0)*Dim(1)+n1*Dim(0)+n0];
200  }
201 
202  inline T operator() (const size_t n0, const size_t n1, const size_t n2) const {
203  return _data[n2*Dim(0)*Dim(1)+n1*Dim(0)+n0];
204  }
205 
206  inline const T* Ptr (const size_t n = 0) const { return &_data[n]; }
207 
208  inline T* Ptr (const size_t n = 0) { return &_data[n]; }
209 
210  inline std::vector<T> Data () const { return _data; }
211 
212 };
213 
214 
215 template <class T> inline static NDData<T>
216 cumtrapz (const NDData<T>& data,
217  std::vector<T>& times = std::vector<T>(),
218  std::vector<size_t> meta = std::vector<size_t>()) {
219 
220  if (!(times.empty()))
221  assert (times.size() == data.Dim(0));
222  else
223  times = std::vector<T> (data.Dim(0),1.);
224 
225  if (!(meta.empty()))
226  assert (meta.size() == data.Dim(0));
227  else
228  meta = std::vector<size_t> (data.Dim(0),0);
229 
230  NDData<T> ret (data.Dims());
231  size_t ncol = ret.Size()/ret.Dim(0);
232  size_t csz = ret.Dim(0);
233 
234  for (size_t i = 0, os = i*csz; i < ncol; ++i)
235  for (size_t j = 1; j < csz; ++j) {
236  if (check_bit(meta[j], REFOCUS_T))
237  ret[os+j] = - ret[os+j-1];
238  else if (check_bit(meta[j], EXCITE_T))
239  ret[os+j] = 0.;
240  else
241  ret[os+j] = ret[os+j-1];
242  ret[os+j] += .5 * (data[os+j] + data[os+j-1]) * (times[os+j] - times[os+j-1]);
243  }
244 
245  return ret;
246 
247 }
248 
249 
250 template <class T> inline static bool
251 ismatrix (const NDData<T>& data) {
252  return data.Dims().size() == 2;
253 }
254 
255 template <class T> inline static NDData<T>
256 transpose (const NDData<T>& data) {
257  assert (ismatrix(data));
258  NDData<T> ret (data.Dim(1),data.Dim(0));
259  for (size_t j = 0; j < data.Dim(1); ++j)
260  for (size_t i = 0; i < data.Dim(0); i++)
261  ret(j,i) = data(i,j);
262  return ret;
263 }
264 
265 
266 template <class T> inline std::ostream&
267 operator<< (std::ostream& os, NDData<T>& di) {
268  return di.Print(os);
269 }
270 
271 #endif
Implementation of JEMRIS TPOI.
Definition: NDData.h:17
NDData(const size_t n0)
1D-data (convenience constructor)
Definition: NDData.h:73
NDData(const std::vector< size_t > &dims)
ND-Data.
Definition: NDData.h:138
NDData(const std::vector< hsize_t > &dims)
ND-Data.
Definition: NDData.h:127
NDData()
Default constructor.
Definition: NDData.h:66
NDData(const size_t n0, const size_t n1)
2D-data (convenience constructor)
Definition: NDData.h:84
std::vector< T > _data
Definition: NDData.h:59
NDData(const size_t n0, const size_t n1, const size_t n2, const size_t n3)
4D-data (convenience constructor)
Definition: NDData.h:113
NDData(const NDData &data)
Copy constructor.
Definition: NDData.h:148
std::vector< size_t > _dims
Definition: NDData.h:58
Simple nd-data structure.
Definition: NDData.h:53
NDData(const size_t n0, const size_t n1, const size_t n2)
3D-data (convenience constructor)
Definition: NDData.h:98

-- last change 17.06.2016 | Tony Stoecker | Imprint --