JEMRIS 2.9.1
open-source MRI simulations
Loading...
Searching...
No Matches
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
16template<class T>
17struct T_minmax {
18 T min;
19 T max;
20};
21template<class T> inline std::ostream&
22operator<< (std::ostream& os, const T_minmax<T>& mm) {
23 return os << "[" << mm.min << ", " << mm.max << "]";
24}
25
26template<class T> inline static T
27_prod (const T a, const T b) {return (a*b);}
28template<class T> inline static T
29prod (const std::vector<T>& v) {
30 return std::accumulate(v.begin(), v.end(), (T)1, _prod<T>);
31}
32template<class T> inline static T
33sum (const std::vector<T>& v) {
34 return std::accumulate(v.begin(), v.end(), (T)0);
35}
36template<class T> inline const T_minmax<T>
37minmax (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
46const static std::string SLASH ("/");
47const static std::string DSLASH ("//");
48
52template<class T>
53class NDData {
54
55
56protected:
57
58 std::vector<size_t> _dims;
59 std::vector<T> _data;
61public:
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 T& operator () (const size_t n0, const size_t n1, const size_t n2, const size_t n3) {
207 return _data[n3 * Dim(2) * Dim(1) * Dim(0) +
208 n2 * Dim(1) * Dim(0) + n1 * Dim(0) + n0];
209 }
210
211 inline T operator () (const size_t n0, const size_t n1, const size_t n2, const size_t n3) const {
212 return _data[n3 * Dim(2) * Dim(1) * Dim(0) +
213 n2 * Dim(1) * Dim(0) + n1 * Dim(0) + n0];
214 }
215
216
217 inline const T* Ptr (const size_t n = 0) const { return &_data[n]; }
218
219 inline T* Ptr (const size_t n = 0) { return &_data[n]; }
220
221 inline std::vector<T> Data () const { return _data; }
222
223};
224
225
226template <class T> inline static NDData<T>
227cumtrapz (const NDData<T>& data,
228 std::vector<T>& times = std::vector<T>(),
229 std::vector<size_t> meta = std::vector<size_t>()) {
230
231 if (!(times.empty()))
232 assert (times.size() == data.Dim(0));
233 else
234 times = std::vector<T> (data.Dim(0),1.);
235
236 if (!(meta.empty()))
237 assert (meta.size() == data.Dim(0));
238 else
239 meta = std::vector<size_t> (data.Dim(0),0);
240
241 NDData<T> ret (data.Dims());
242 size_t ncol = ret.Size()/ret.Dim(0);
243 size_t csz = ret.Dim(0);
244
245 for (size_t i = 0, os = i*csz; i < ncol; ++i)
246 for (size_t j = 1; j < csz; ++j) {
247 if (check_bit(meta[j], REFOCUS_T))
248 ret[os+j] = - ret[os+j-1];
249 else if (check_bit(meta[j], EXCITE_T))
250 ret[os+j] = 0.;
251 else
252 ret[os+j] = ret[os+j-1];
253 ret[os+j] += .5 * (data[os+j] + data[os+j-1]) * (times[os+j] - times[os+j-1]);
254 }
255
256 return ret;
257
258}
259
260
261template <class T> inline static bool
262ismatrix (const NDData<T>& data) {
263 return data.Dims().size() == 2;
264}
265
266template <class T> inline static NDData<T>
267transpose (const NDData<T>& data) {
268 assert (ismatrix(data));
269 NDData<T> ret (data.Dim(1),data.Dim(0));
270 for (size_t j = 0; j < data.Dim(1); ++j)
271 for (size_t i = 0; i < data.Dim(0); i++)
272 ret(j,i) = data(i,j);
273 return ret;
274}
275
276
277template <class T> inline std::ostream&
278operator<< (std::ostream& os, NDData<T>& di) {
279 return di.Print(os);
280}
281
282#endif
Implementation of JEMRIS TPOI.
Simple nd-data structure.
Definition: NDData.h:53
std::vector< size_t > _dims
Definition: NDData.h:58
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 std::vector< hsize_t > &dims)
ND-Data.
Definition: NDData.h:127
NDData(const NDData &data)
Copy constructor.
Definition: NDData.h:148
NDData()
Default constructor.
Definition: NDData.h:66
NDData(const std::vector< size_t > &dims)
ND-Data.
Definition: NDData.h:138
std::vector< T > _data
Definition: NDData.h:59
NDData(const size_t n0, const size_t n1, const size_t n2)
3D-data (convenience constructor)
Definition: NDData.h:98
NDData(const size_t n0)
1D-data (convenience constructor)
Definition: NDData.h:73
NDData & operator=(const NDData &data)
Assignement.
Definition: NDData.h:157
NDData(const size_t n0, const size_t n1)
2D-data (convenience constructor)
Definition: NDData.h:84
Definition: NDData.h:17

-- last change 03.10.2023 | Tony Stoecker | Imprint | Data Protection --