Image Utilities (IU)
 All Data Structures Namespaces Functions Variables Typedefs Enumerations Friends Groups Pages
vector.h
1 #pragma once
2 
3 #include <type_traits>
4 #include <initializer_list>
5 
6 #include "coredefs.h"
7 
9 namespace iu {
11 template<typename PixelType, unsigned int Ndim>
13 {
14 IU_ASSERT(Ndim > 0)
15 
16 public:
19  {
20  for (unsigned int i = 0; i < Ndim; i++)
21  {
22  data_[i] = static_cast<PixelType>(0.0);
23  }
24  }
25 
29  VectorBase(const PixelType& value)
30  {
31  this->fill(value);
32  }
33 
37  VectorBase(std::initializer_list<PixelType> list)
38  {
39  if (list.size() != Ndim)
40  {
41  std::stringstream msg;
42  msg << "Length of initializer list (" << list.size();
43  msg << ") does not match number of size dimensions (" << Ndim << ").";
44  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
45  }
46 
47  unsigned int i = 0;
48  for (auto elem : list)
49  {
50  data_[i] = elem;
51  ++i;
52  }
53  }
54 
56  virtual ~VectorBase()
57  {
58  }
59 
61  inline PixelType operator[](unsigned int i) const
62  {
63  return data_[i];
64  }
65 
67  inline PixelType &operator[](unsigned int i)
68  {
69  return data_[i];
70  }
71 
73  static unsigned int ndim()
74  {
75  return Ndim;
76  }
77 
79  void fill(const PixelType& value)
80  {
81  for (unsigned int i = 0; i < Ndim; i++)
82  {
83  data_[i] = value;
84  }
85  }
86 
88  template<typename T = std::ostream>
89  friend typename std::enable_if<(Ndim == 1), T&>::type operator<<(
90  std::ostream & out, VectorBase<PixelType, Ndim> const& v)
91  {
92  out << "[" << v[0] << "]";
93  return out;
94  }
95 
97  template<typename T = std::ostream>
98  friend typename std::enable_if<(Ndim > 1), T&>::type operator<<(
99  std::ostream & out, VectorBase<PixelType, Ndim> const& v)
100  {
101  out << "[";
102  for (int i = 0; i < static_cast<int>(Ndim - 1); i++)
103  {
104  out << v[i] << ", ";
105  }
106  out << v[Ndim - 1] << "]";
107  return out;
108  }
109 
110 protected:
112  PixelType data_[Ndim];
113 
114 private:
116  VectorBase(const VectorBase& from);
118  VectorBase& operator=(const VectorBase& from);
119 };
120 
122 
123 template<typename PixelType, unsigned int Ndim>
125  const VectorBase<PixelType, Ndim> &v2)
126 {
127  for (unsigned int i = 0; i < Ndim; i++)
128  {
129  if (v1[i] != v2[i])
130  return false;
131  }
132  return true;
133 }
134 
136 template<typename PixelType, unsigned int Ndim>
138  const VectorBase<PixelType, Ndim> &v2)
139 {
140  for (unsigned int i = 0; i < Ndim; i++)
141  {
142  if (v1[i] != v2[i])
143  return true;
144  }
145  return false;
146 }
147 
149 
153 template<typename PixelType, unsigned int Ndim>
154 class Vector: public VectorBase<PixelType, Ndim>
155 {
156 public:
158  Vector() :
159  VectorBase<PixelType, Ndim>()
160  {
161  }
162 
166  Vector(const PixelType& value) :
167  VectorBase<PixelType, Ndim>(value)
168  {
169  }
170 
174  Vector(std::initializer_list<PixelType> list) :
175  VectorBase<PixelType, Ndim>(list)
176  {
177 
178  }
179 
182  {
183  }
184 
186  inline Vector<PixelType, Ndim> operator+(const PixelType &scalar) const
187  {
189  for (unsigned int i = 0; i < Ndim; i++)
190  {
191  v[i] = this->data_[i] + scalar;
192  }
193  return v;
194  }
195 
198  {
200  for (unsigned int i = 0; i < Ndim; i++)
201  {
202  v2[i] = this->data_[i] + v1[i];
203  }
204  return v2;
205  }
206 
208  inline Vector<PixelType, Ndim> operator-(const PixelType &scalar) const
209  {
211  for (unsigned int i = 0; i < Ndim; i++)
212  {
213  v[i] = this->data_[i] - scalar;
214  }
215  return v;
216  }
217 
220  {
222  for (unsigned int i = 0; i < Ndim; i++)
223  {
224  v2[i] = this->data_[i] - v1[i];
225  }
226  return v2;
227  }
228 
230  inline Vector<PixelType, Ndim> operator*(const PixelType &scalar) const
231  {
233  for (unsigned int i = 0; i < Ndim; i++)
234  {
235  v[i] = this->data_[i] * scalar;
236  }
237  return v;
238  }
239 
242  {
244  for (unsigned int i = 0; i < Ndim; i++)
245  {
246  v2[i] = this->data_[i] * v1[i];
247  }
248  return v2;
249  }
250 
252  void operator+=(const PixelType &scalar)
253  {
254  for (unsigned int i = 0; i < Ndim; i++)
255  {
256  this->data_[i] += scalar;
257  }
258  }
259 
262  {
263  for (unsigned int i = 0; i < Ndim; i++)
264  {
265  this->data_[i] += v2[i];
266  }
267  }
268 
270  void operator-=(const PixelType &scalar)
271  {
272  for (unsigned int i = 0; i < Ndim; i++)
273  {
274  this->data_[i] -= scalar;
275  }
276  }
277 
280  {
281  for (unsigned int i = 0; i < Ndim; i++)
282  {
283  this->data_[i] -= v2[i];
284  }
285  }
286 
288  void operator*=(const PixelType &scalar)
289  {
290  for (unsigned int i = 0; i < Ndim; i++)
291  {
292  this->data_[i] *= scalar;
293  }
294  }
295 
298  {
299  for (unsigned int i = 0; i < Ndim; i++)
300  {
301  this->data_[i] *= v2[i];
302  }
303  }
304 
306  Vector operator/(const PixelType scalar) const
307  {
308  IU_ASSERT(scalar != 0);
309  PixelType invFactor = 1 / scalar;
310  return operator*(invFactor);
311  }
312 
314  Vector(const Vector& from)
315  {
316  for (unsigned int i = 0; i < Ndim; i++)
317  this->data_[i] = from[i];
318  }
319 
321  Vector& operator=(const Vector& from)
322  {
323  for (unsigned int i = 0; i < Ndim; i++)
324  this->data_[i] = from[i];
325  return *this;
326  }
327 };
328 
330 
331 template<unsigned int Ndim>
332 class SizeBase: public VectorBase<unsigned int, Ndim>
333 {
334 public:
335  // Constructor. Init the size vector with ones.
336  SizeBase() :
338  {
339  this->fill(1);
340  }
341 
345  SizeBase(unsigned int value) :
346  VectorBase<unsigned int, Ndim>(value)
347  {
348  }
349 
353  SizeBase(std::initializer_list<unsigned int> list) :
354  VectorBase<unsigned int, Ndim>(list)
355  {
356  }
357 
359  virtual ~SizeBase()
360  {
361  }
362 
364  unsigned int numel() const
365  {
366  unsigned int num_elements = this->data_[0];
367  for (unsigned int i = 1; i < Ndim; i++)
368  {
369  if (this->data_[i] == 0)
370  {
371  std::stringstream msg;
372  msg << "Zero size elements are not allowed. (" << *this << ")";
373  throw IuException(msg.str(), __FILE__, __FUNCTION__, __LINE__);
374  }
375 
376  num_elements *= this->data_[i];
377  }
378  return num_elements;
379  }
380 
382  template<typename ScalarType>
383  SizeBase operator*(const ScalarType& scalar) const
384  {
385  SizeBase<Ndim> v;
386  for (unsigned int i = 0; i < Ndim; i++)
387  {
388  v[i] = static_cast<unsigned int>(this->data_[i] * scalar + 0.5f);
389  }
390  return v;
391  }
392 
394  template<typename ScalarType>
395  void operator*=(const ScalarType &scalar)
396  {
397  for (unsigned int i = 0; i < Ndim; i++)
398  {
399  this->data_[i] =
400  static_cast<unsigned int>(this->data_[i] * scalar + 0.5f);
401  ;
402  }
403  }
404 
406  template<typename ScalarType>
407  SizeBase operator/(const ScalarType& scalar) const
408  {
409  IU_ASSERT(scalar != 0);
410  double invFactor = 1.0 / static_cast<double>(scalar);
411  return operator*(invFactor);
412  }
413 
415  template<typename ScalarType>
416  void operator/=(const ScalarType &scalar)
417  {
418  IU_ASSERT(scalar != 0);
419  double invFactor = 1.0 / static_cast<double>(scalar);
420  operator*=(invFactor);
421  }
422 
424  SizeBase(const SizeBase& from)
425  {
426  for (unsigned int i = 0; i < Ndim; i++)
427  this->data_[i] = from[i];
428  }
429 
432  {
433  for (unsigned int i = 0; i < Ndim; i++)
434  this->data_[i] = from[i];
435  return *this;
436  }
437 
442  unsigned int* ptr()
443  {
444  return this->data_;
445  }
446 
451  const unsigned int* ptr() const
452  {
453  return reinterpret_cast<const unsigned int*>(this->data_);
454  }
455 };
456 
458 
459 template<unsigned int Ndim>
460 class Size: public SizeBase<Ndim>
461 {
462 public:
464  Size() :
465  SizeBase<Ndim>()
466  {
467  }
468 
472  Size(unsigned int value) :
473  SizeBase<Ndim>(value)
474  {
475  }
476 
480  Size(std::initializer_list<unsigned int> list) :
481  SizeBase<Ndim>(list)
482  {
483  }
484 
487  {
488  }
489 
491  Size(const Size& from) :
492  SizeBase<Ndim>(from)
493  {
494  }
495 
497  Size(const SizeBase<Ndim>& from) :
498  SizeBase<Ndim>(from)
499  {
500  }
501 
503  Size& operator=(const Size& from)
504  {
506  return *this;
507  }
508 
511  {
513  return *this;
514  }
515 
516 };
517 
519 
524 template<>
525 class Size<2> : public SizeBase<2>
526 {
527 public:
529  unsigned int& width;
531  unsigned int& height;
532 
534  Size() :
535  SizeBase<2>(), width(this->data_[0]), height(this->data_[1])
536  {
537  }
538 
542  Size(unsigned int value) :
543  SizeBase<2>(value), width(this->data_[0]), height(this->data_[1])
544  {
545  }
546 
550  Size(std::initializer_list<unsigned int> list) :
551  SizeBase<2>(list), width(this->data_[0]), height(this->data_[1])
552  {
553  }
554 
559  Size(unsigned int width, unsigned int height) :
560  SizeBase<2>(), width(this->data_[0]), height(this->data_[1])
561  {
562  data_[0] = width;
563  data_[1] = height;
564  }
565 
568  {
569  }
570 
572  Size(const Size& from) :
573  SizeBase<2>(from), width(this->data_[0]), height(this->data_[1])
574  {
575  }
576 
578  Size(const SizeBase& from) :
579  SizeBase<2>(from), width(this->data_[0]), height(this->data_[1])
580  {
581  }
582 
584  Size& operator=(const Size& from)
585  {
586  SizeBase::operator=(from);
587  return *this;
588  }
589 
591  Size& operator=(const SizeBase<2>& from)
592  {
593  SizeBase::operator=(from);
594  return *this;
595  }
596 };
597 
599 
604 template<>
605 class Size<3> : public SizeBase<3>
606 {
607 public:
609  unsigned int& width;
611  unsigned int& height;
613  unsigned int& depth;
614 
616  Size() :
617  SizeBase<3>(), width(this->data_[0]), height(this->data_[1]),
618  depth(this->data_[2])
619  {
620  }
621 
625  Size(unsigned int value) :
626  SizeBase<3>(value), width(this->data_[0]), height(this->data_[1]),
627  depth(this->data_[2])
628  {
629  }
630 
634  Size(std::initializer_list<unsigned int> list) :
635  SizeBase<3>(list), width(this->data_[0]), height(this->data_[1]),
636  depth(this->data_[2])
637  {
638  }
639 
645  Size(unsigned int width, unsigned int height, unsigned int depth) :
646  SizeBase<3>(), width(this->data_[0]), height(this->data_[1]),
647  depth(this->data_[2])
648  {
649  data_[0] = width;
650  data_[1] = height;
651  data_[2] = depth;
652  }
653 
656  {
657  }
658 
660  Size(const Size& from) :
661  SizeBase<3>(from), width(this->data_[0]), height(this->data_[1]),
662  depth(this->data_[2])
663  {
664  }
665 
667  Size(const SizeBase& from) :
668  SizeBase<3>(from), width(this->data_[0]), height(this->data_[1]),
669  depth(this->data_[2])
670  {
671  }
672 
674  Size& operator=(const Size& from)
675  {
676  SizeBase::operator=(from);
677  return *this;
678  }
679 
681  Size& operator=(const SizeBase<3>& from)
682  {
683  SizeBase::operator=(from);
684  return *this;
685  }
686 };
687 
688 } //namespace iu
689 
Size & operator=(const SizeBase< 2 > &from)
Definition: vector.h:591
unsigned int numel() const
Definition: vector.h:364
SizeBase(std::initializer_list< unsigned int > list)
Definition: vector.h:353
Size(const Size &from)
Definition: vector.h:660
const unsigned int * ptr() const
Definition: vector.h:451
Size(const SizeBase< Ndim > &from)
Definition: vector.h:497
unsigned int & depth
Definition: vector.h:613
virtual ~VectorBase()
Definition: vector.h:56
VectorBase()
Definition: vector.h:18
Size(std::initializer_list< unsigned int > list)
Definition: vector.h:634
VectorBase(std::initializer_list< PixelType > list)
Definition: vector.h:37
Base class for N-dimensional vectors.
Definition: vector.h:12
Size(unsigned int value)
Definition: vector.h:472
Size(unsigned int width, unsigned int height)
Definition: vector.h:559
void operator+=(const PixelType &scalar)
Definition: vector.h:252
~Vector()
Definition: vector.h:181
friend std::enable_if<(Ndim==1), T & >::type operator<<(std::ostream &out, VectorBase< PixelType, Ndim > const &v)
Definition: vector.h:89
Size(unsigned int width, unsigned int height, unsigned int depth)
Definition: vector.h:645
Vector< PixelType, Ndim > operator+(const PixelType &scalar) const
Definition: vector.h:186
SizeBase operator*(const ScalarType &scalar) const
Definition: vector.h:383
unsigned int & height
Definition: vector.h:531
PixelType data_[Ndim]
Definition: vector.h:112
unsigned int * ptr()
Definition: vector.h:442
Vector(std::initializer_list< PixelType > list)
Definition: vector.h:174
unsigned int & height
Definition: vector.h:611
SizeBase & operator=(const SizeBase &from)
Definition: vector.h:431
void operator-=(const Vector< PixelType, Ndim > &v2)
Definition: vector.h:279
Size & operator=(const Size &from)
Definition: vector.h:503
~Size()
Definition: vector.h:486
bool operator!=(const VectorBase< PixelType, Ndim > &v1, const VectorBase< PixelType, Ndim > &v2)
Definition: vector.h:137
Vector(const Vector &from)
Definition: vector.h:314
Vector< PixelType, Ndim > operator-(const PixelType &scalar) const
Definition: vector.h:208
Exceptions with additional error information.
Definition: coredefs.h:32
VectorBase(const PixelType &value)
Definition: vector.h:29
SizeBase operator/(const ScalarType &scalar) const
Definition: vector.h:407
void operator*=(const ScalarType &scalar)
Definition: vector.h:395
unsigned int & width
Definition: vector.h:529
Main Class for N-dimensional vectors.
Definition: vector.h:154
Size & operator=(const SizeBase< 3 > &from)
Definition: vector.h:681
Vector(const PixelType &value)
Definition: vector.h:166
Size(unsigned int value)
Definition: vector.h:542
static unsigned int ndim()
Definition: vector.h:73
void fill(const PixelType &value)
Definition: vector.h:79
Size(const Size &from)
Definition: vector.h:572
Size(const SizeBase &from)
Definition: vector.h:667
Size(std::initializer_list< unsigned int > list)
Definition: vector.h:550
Vector< PixelType, Ndim > operator-(Vector< PixelType, Ndim > &v1) const
Definition: vector.h:219
void operator-=(const PixelType &scalar)
Definition: vector.h:270
Vector & operator=(const Vector &from)
Definition: vector.h:321
Vector< PixelType, Ndim > operator*(const PixelType &scalar) const
Definition: vector.h:230
bool operator==(const VectorBase< PixelType, Ndim > &v1, const VectorBase< PixelType, Ndim > &v2)
Definition: vector.h:124
Vector operator/(const PixelType scalar) const
Definition: vector.h:306
void operator*=(const PixelType &scalar)
Definition: vector.h:288
Size(unsigned int value)
Definition: vector.h:625
SizeBase(const SizeBase &from)
Definition: vector.h:424
Size & operator=(const Size &from)
Definition: vector.h:584
PixelType operator[](unsigned int i) const
Definition: vector.h:61
Size(std::initializer_list< unsigned int > list)
Definition: vector.h:480
Base class for N-dimensional unsigned int vectors (size vectors).
Definition: vector.h:332
Size()
Definition: vector.h:464
unsigned int & width
Definition: vector.h:609
Size & operator=(const SizeBase< Ndim > &from)
Definition: vector.h:510
SizeBase(unsigned int value)
Definition: vector.h:345
void operator*=(const Vector< PixelType, Ndim > &v2)
Definition: vector.h:297
Size(const SizeBase &from)
Definition: vector.h:578
void operator+=(const Vector< PixelType, Ndim > &v2)
Definition: vector.h:261
~Size()
Definition: vector.h:567
Size()
Definition: vector.h:616
void operator/=(const ScalarType &scalar)
Definition: vector.h:416
Size()
Definition: vector.h:534
Size & operator=(const Size &from)
Definition: vector.h:674
virtual ~SizeBase()
Definition: vector.h:359
~Size()
Definition: vector.h:655
Main class for N-dimensional unsigned int vectors (size vectors).
Definition: vector.h:460
PixelType & operator[](unsigned int i)
Definition: vector.h:67
Size(const Size &from)
Definition: vector.h:491
Vector()
Definition: vector.h:158
Vector< PixelType, Ndim > operator*(Vector< PixelType, Ndim > &v1) const
Definition: vector.h:241
Vector< PixelType, Ndim > operator+(Vector< PixelType, Ndim > &v1) const
Definition: vector.h:197