File: minc_io_fixed_vector.h

package info (click to toggle)
libminc 2.4.07-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,288 kB
  • sloc: ansic: 57,268; cpp: 3,654; sh: 100; makefile: 23; ruby: 18
file content (381 lines) | stat: -rw-r--r-- 8,989 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* ----------------------------- MNI Header -----------------------------------
@NAME       : 
@DESCRIPTION: simple N-dimensional vector that supports arithmetic operations
@COPYRIGHT  :
              Copyright 2006 Vladimir Fonov, McConnell Brain Imaging Centre, 
              Montreal Neurological Institute, McGill University.
              Permission to use, copy, modify, and distribute this
              software and its documentation for any purpose and without
              fee is hereby granted, provided that the above copyright
              notice appear in all copies.  The author and McGill University
              make no representations about the suitability of this
              software for any purpose.  It is provided "as is" without
              express or implied warranty.
---------------------------------------------------------------------------- */
#ifndef MINC_IO_FIXED_VECTOR_H
#define MINC_IO_FIXED_VECTOR_H

#include <limits>

namespace minc
{
  //! fixed size array, which support arithmetic operations
  template<int dim,class I=int> class fixed_vec
  {
  protected:
    I c[dim];
  public:
    //! default constructor, does nothing (i.e data is uninitilized)
    fixed_vec() {}
  
    //! constructor which sets all the elements to the same value
    explicit fixed_vec(I v)
    {
        for(unsigned int i=0;i<dim;i++)
            c[i]=v;
    }

    //! constructor which sets all the elements to be a copy of C-array
    explicit fixed_vec(const I* v)
    {
        for(unsigned int i=0;i<dim;i++)
            c[i]=v[i];
    }
    
    //! conversion to the C array
    I* c_buf()
    {
      return c;
    }
	
    //! conversion to const C array 
    const I* c_buf() const
    {
      return c;
    }

    //! element access operator
    I& operator[](int i)
    {
#ifdef _INDEX_CHECK
      if(i<0 || i>=dim) REPORT_ERROR("Index out of bounds");
#endif //_INDEX_CHECK
      return c[i];
    }
    //! const element access operator
    I operator[](int i) const
    {
#ifdef _INDEX_CHECK
      if(i<0 || i>=dim) REPORT_ERROR("Index out of bounds");
#endif //_INDEX_CHECK
      return c[i];
    }

    //! const element access operator
    I get(int i)
    {
      return (*this)[i];
    }
    //! element writing operator
    void set(int i,I v)
    {
      (*this)[i]=v;
    }

    //! find a maximum of elements
    I max(void) const
    {
      I s=std::numeric_limits < I >::min ();;
      for(unsigned int i=0;i<dim;i++)
        if(c[i]>s) s=c[i];
      return s;
    }
	
    //! find a minimum of elements
    I min(void) const
    {
      I s=std::numeric_limits < I >::max ();;
      for(unsigned int i=0;i<dim;i++)
        if(c[i]<s) s=c[i];
      return s;
    }
	
    //! calculate sum of all elements
    I sum(void) const
    {
      I s=0;
      for(unsigned int i=0;i<dim;i++)
        s+=c[i];
      return s;
    }

    //! modulus squared
    I mod2(void) const
    {
      I s=0;
      for(unsigned int i=0;i<dim;i++)
        s+=c[i]*c[i];
      return s;
    }
    
    //! volume (product of all elements)
    I vol(void) const
    {
      I s=1;
      for(unsigned int i=0;i<dim;i++)
        s*=c[i];
      return s;
    }
	
    //! \name fixed_vec arithmetic operations
    //@{
    fixed_vec<dim,I>& operator *=(const fixed_vec<dim,I>& b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]*=b[i];
      return *this;
    }

    fixed_vec<dim,I>& operator *=(const I b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]*=b;
      return *this;
    }

    fixed_vec<dim,I>& operator +=(const fixed_vec<dim,I>& b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]+=b[i];
      return *this;
    }

    fixed_vec<dim,I>& operator -=(const fixed_vec<dim,I>& b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]-=b[i];
      return *this;
    }

    fixed_vec<dim,I>& operator /=(const fixed_vec<dim,I>& b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]/=b[i];
      return *this;
    }

    fixed_vec<dim,I>& operator /=(const I b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]/=b;
      return *this;
    }

    fixed_vec<dim,I> operator /(const I b)
    {
      fixed_vec<dim,I> tmp;
      for(unsigned int i=0;i<dim;i++)
        tmp[i]=c[i]/b;
      return tmp;
    }

    fixed_vec<dim,I> operator *(const I b)
    {
      fixed_vec<dim,I> tmp;
      for(unsigned int i=0;i<dim;i++)
        tmp[i]=c[i]*b;
      return tmp;
    }

    fixed_vec<dim,I> operator -(const fixed_vec<dim,I>& b)
    {
      fixed_vec<dim,I> tmp;
      for(unsigned int i=0;i<dim;i++)
        tmp[i]=c[i]-b[i];
      return tmp;
    }

    fixed_vec<dim,I> operator +(const fixed_vec<dim,I>& b)
    {
      fixed_vec<dim,I> tmp;
      for(unsigned int i=0;i<dim;i++)
        tmp[i]=c[i]+b[i];
      return tmp;
    }

    //@}

    fixed_vec( const fixed_vec& b )
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]=b[i];
    }

    //! assignment operator, copies contents
    fixed_vec<dim,I>& operator=(const fixed_vec<dim,I>& b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]=b[i];
      return *this;
    }

    //! assignment operator, copies contents, assumes that b have at least this size
    fixed_vec<dim,I>& operator=(const I* b)
    {
      for(unsigned int i=0;i<dim;i++)
        c[i]=b[i];
      return *this;
    }

    //! assignment operator, sets all elements to the same value
    fixed_vec<dim,I>& operator=(const I b)
    {
      for(unsigned int i=0;i<dim;i++) c[i]=b;
      return *this;
    }

    //! inequality operator, does element wise equality check
    bool operator!=(const fixed_vec<dim,I>& b) const
    {
      for(int i=0;i<dim;i++) if(c[i]!=b[i]) return true;
      return false;
    }
	
    //! equality operator, does element wise equality check
    bool operator==(const fixed_vec<dim,I>& b) const
    {
      for(int i=0;i<dim;i++) if(c[i]!=b[i]) return false;
      return true;
    }
    
    //! reverse the order of elements
    void reverse(void)
    {
      for(int i=0;i<dim/2;i++)
      {
        I tmp=c[i];
        c[i]=c[dim-i-1];
        c[dim-i-1]=tmp;
      }
    }
  };

  //! element wise division
	template<int dim,class I> fixed_vec<dim,I> operator/(const fixed_vec<dim,I> &l,const fixed_vec<dim,I> &r)
	{
		fixed_vec<dim,I> out=l;
		out/=r;
		return out; //this is not efficient  - no return value optimisation
	}
  
	//! element wise multiplication
	template<int dim,class I> fixed_vec<dim,I> operator*(const fixed_vec<dim,I> &l,const fixed_vec<dim,I> &r)
	{
		fixed_vec<dim,I> out=l;
		out*=r;
		return out; //this is not efficient  - no return value optimisation
	}
  
	//! element wise addition
	template<int dim,class I> fixed_vec<dim,I> operator+(const fixed_vec<dim,I> &l,const fixed_vec<dim,I> &r)
	{
		fixed_vec<dim,I> out=l;
		out+=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
  //! element wise subtraction
	template<int dim,class I> fixed_vec<dim,I> operator-(const fixed_vec<dim,I> &l,const fixed_vec<dim,I> &r)
	{
		fixed_vec<dim,I> out=l;
		out-=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
	//! divide all elements by a value
	template<int dim,class I> fixed_vec<dim,I> operator/(const fixed_vec<dim,I> &l,I r)
	{
		fixed_vec<dim,I> out=l;
		out/=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
  //! multiply all elements by a value 
	template<int dim,class I> fixed_vec<dim,I> operator*(const fixed_vec<dim,I> &l,I r)
	{
		fixed_vec<dim,I> out=l;
		out*=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
  //! add a value to all elements 
	template<int dim,class I> fixed_vec<dim,I> operator+(const fixed_vec<dim,I> &l,I r)
	{
		fixed_vec<dim,I> out=l;
		out+=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
  //! subtract a value from all elements 
	template<int dim,class I> fixed_vec<dim,I> operator-(const fixed_vec<dim,I> &l,I r)
	{
		fixed_vec<dim,I> out=l;
		out-=r;
		return out; //this is not efficient  - no return value optimisation
	}
	
	//! create 1d fixed_vec
	template<class I> fixed_vec<1,I> IDX(I i)
	{
		fixed_vec<1,I> d;
		d[0]=i;
		return d;
	}
	
	//! create 2d fixed_vec
	template<class I> fixed_vec<2,I> IDX(I i,I j)
	{
		fixed_vec<2,I> d;
		d[0]=i;
		d[1]=j;
		return d;
	}
	
	//! create 3d fixed_vec
	template<class I> fixed_vec<3,I> IDX(I i,I j,I k)
	{
		fixed_vec<3,I> d;
		d[0]=i;
		d[1]=j;
		d[2]=k;
		return d;
	}
	
	//! create 4d fixed_vec
	template<class I> fixed_vec<4,I> IDX(I i,I j,I k,I l)
	{
		fixed_vec<3,I> d;
		d[0]=i;
		d[1]=j;
		d[2]=k;
		d[3]=l;
		return d;
	}
	
	//!average value of a vector
	template<class T,int d>T AVG(const fixed_vec<d,T> &v)
	{
		return v.sum()/d;
	}	
  
	//!dot product of two vectors
  template<class T,int d>T dot(const fixed_vec<d,T> &v1,const fixed_vec<d,T> &v2)
  {
    T val=0;
    for(int i=0;i<d;i++) val+=v1[i]*v2[i];
    return val;
  }	
	
}

#endif //MINC_IO_FIXED_VECTOR_H