My Project
3d/vector.hh
Go to the documentation of this file.
1 /* -*- mia-c++ -*-
2  *
3  * This file is part of MIA - a toolbox for medical image analysis
4  * Copyright (c) Leipzig, Madrid 1999-2017 Gert Wollny
5  *
6  * MIA is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with MIA; if not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef __MIA_3DVECTOR_HH
22 #define __MIA_3DVECTOR_HH 1
23 
24 #include <typeinfo>
25 #include <assert.h>
26 #include <stdexcept>
27 #include <math.h>
28 #include <complex>
29 #include <iostream>
30 #include <type_traits>
31 
32 #include <mia/core/defines.hh>
33 #include <mia/core/type_traits.hh>
35 #include <mia/3d/defines3d.hh>
36 
38 
47 template < class T >
48 class T3DVector
49 {
50 public:
52  T x;
54  T y;
56  T z;
57 
59  typedef T value_type;
60 
62  T3DVector(): x(T()), y(T()), z(T()) {};
63 
65  explicit T3DVector(int dim): x(T()), y(T()), z(T())
66  {
67  assert(dim == 3);
68  }
69 
71  T3DVector(const T3DVector<T>& other) = default;
72 
74  T3DVector<T>& operator = (const T3DVector<T>& other) = default;
75 
77  T3DVector(const T& x_, const T& y_, const T& z_):
78  x(x_), y(y_), z(z_)
79  {
80  }
81 
83  template <class in> explicit T3DVector(const T3DVector<in>& org):
84  x(T(org.x)), y(T(org.y)), z(T(org.z))
85  {
86  }
87 
89  template <class in>
91  {
92  x = org.x;
93  y = org.y;
94  z = org.z;
95  return *this;
96  }
97 
99  double norm2()const
100  {
101  return x * x + y * y + z * z;
102  }
103 
105  double product() const
106  {
107  return x * y * z;
108  }
110  double norm()const
111  {
112  return sqrt(norm2());
113  }
114 
116  int size() const
117  {
118  return 3;
119  }
120 
122  void fill(T v)
123  {
124  x = y = z = v;
125  }
126 
135  const T operator [](size_t i) const
136  {
137  assert(i < 3);
138 
139  switch (i) {
140  case 0:
141  return x;
142 
143  case 1:
144  return y;
145 
146  case 2:
147  return z;
148 
149  default:
150  throw std::logic_error("Access to vectorelement out of range");
151  }
152  }
153 
162  T& operator [](size_t i)
163  {
164  assert(i < 3);
165 
166  switch (i) {
167  case 0:
168  return x;
169 
170  case 1:
171  return y;
172 
173  case 2:
174  return z;
175 
176  default:
177  throw std::logic_error("Access to vectorelement out of range");
178  }
179  }
180 
183  {
184  x += a.x;
185  y += a.y;
186  z += a.z;
187  return *this;
188  }
189 
192  {
193  x -= a.x;
194  y -= a.y;
195  z -= a.z;
196  return *this;
197  }
198 
200  T3DVector<T>& operator *=(const double a)
201  {
202  x = T(x * a);
203  y = T(y * a);
204  z = T(z * a);
205  return *this;
206  }
207 
210  {
211  x = T(x * a.x);
212  y = T(y * a.y);
213  z = T(z * a.z);
214  return *this;
215  }
216 
217 
219  T3DVector<T>& operator /=(const double a)
220  {
221  assert(a != 0.0);
222  x = T(x / a);
223  y = T (y / a);
224  z = T(z / a);
225  return *this;
226  }
227 
229  {
230  return T3DVector<T>(-x, -y, -z);
231  }
232 
234  void write(std::ostream& os)const
235  {
236  os << x << "," << y << "," << z;
237  }
238 
240  void read(std::istream& is)
241  {
242  char c;
243  T r, s, t;
244  is >> c;
245 
246  // if we get the opening delimiter '<' then we also expect the closing '>'
247  // otherwise just read three coma separated values.
248  // could use the BOOST lexicel cast for better error handling
249  if (c == '<') {
250  is >> r;
251  is >> c;
252 
253  if (c != ',') {
254  is.clear(std::ios::badbit);
255  return;
256  }
257 
258  is >> s;
259  is >> c;
260 
261  if (c != ',') {
262  is.clear(std::ios::badbit);
263  return;
264  }
265 
266  is >> t;
267  is >> c;
268 
269  if (c != '>') {
270  is.clear(std::ios::badbit);
271  return;
272  }
273 
274  x = r;
275  y = s;
276  z = t;
277  } else {
278  is.putback(c);
279  is >> r;
280  is >> c;
281 
282  if (c != ',') {
283  is.clear(std::ios::badbit);
284  return;
285  }
286 
287  is >> s;
288  is >> c;
289 
290  if (c != ',') {
291  is.clear(std::ios::badbit);
292  return;
293  }
294 
295  is >> t;
296  x = r;
297  y = s;
298  z = t;
299  }
300  }
301 
303  const T3DVector<T>& xyz()const
304  {
305  return *this;
306  }
307 
309  const T3DVector<T> xzy()const
310  {
311  return T3DVector<T>(x, z, y);
312  }
313 
315  const T3DVector<T> yxz()const
316  {
317  return T3DVector<T>(y, x, z);
318  }
319 
321  const T3DVector<T> yzx()const
322  {
323  return T3DVector<T>(y, z, x);
324  }
325 
327  const T3DVector<T> zyx()const
328  {
329  return T3DVector<T>(z, y, x);
330  }
331 
333  const T3DVector<T> zxy()const
334  {
335  return T3DVector<T>(z, x, y);
336  }
337 
339  static T3DVector<T> _1;
340 
342  static T3DVector<T> _0;
343 
345  static const unsigned int elements;
346 };
347 
348 
350 
351  static const int vector_3d_bit = 0x40000;
352 
353  static bool is_vector3d(int type)
354  {
355  return type & vector_3d_bit;
356  }
357 };
358 
359 template <typename T>
361  static const int value = attribute_type<T>::value | vector_3d_bit;
362 };
363 
364 
366 template <typename T>
367 struct atomic_data<T3DVector<T>> {
368  typedef T type;
369  static const int size;
370 };
371 
372 template <typename T>
373 const int atomic_data<T3DVector<T>>::size = 3;
375 
376 
384 template <typename T>
386 {
387  return T3DVector<T>(
388  a.y * b.z - b.y * a.z,
389  a.z * b.x - b.z * a.x,
390  a.x * b.y - b.x * a.y
391  );
392 }
393 
394 
396 template <class T> double fabs(const T3DVector<T>& t)
397 {
398  return t.norm();
399 }
400 
402 template <class T> double dot(const T3DVector<T>& a, const T3DVector<T>& b)
403 {
404  return a.x * b.x + a. y * b.y + a.z * b.z;
405 }
406 
407 
410 
413 
416 
417 
418 
420 template <class T>
421 std::ostream& operator << (std::ostream& os, const T3DVector<T>& v)
422 {
423  v.write(os);
424  return os;
425 }
426 
428 template <class T>
429 std::istream& operator >> (std::istream& is, T3DVector<T>& v)
430 {
431  v.read(is);
432  return is;
433 }
434 
435 
437 template <class T>
438 inline const T3DVector<T> operator +(const T3DVector<T>& a, const T3DVector<T>& b)
439 {
440  T3DVector<T> tmp(a);
441  tmp += b;
442  return tmp;
443 }
444 
453 template <typename T, typename S>
455 {
456  return T3DVector<T>(a.x + b.x, a.y + b.y, a.z + b.z);
457 }
458 
459 
461 template <class T>
462 inline const T3DVector<T> operator -(const T3DVector<T>& a, const T3DVector<T>& b)
463 {
464  T3DVector<T> tmp(a);
465  tmp -= b;
466  return tmp;
467 }
468 
470 template <class T>
471 inline const T3DVector<T> operator *(const T3DVector<T>& a, const T3DVector<T>& b)
472 {
473  return T3DVector<T>(b.x * a.x, b.y * a.y, b.z * a.z);
474 }
475 
477 template <class T>
478 inline const T3DVector<T> operator /(const T3DVector<T>& a, double f)
479 {
480  assert(f != T());
481  T3DVector<T> tmp (a);
482  tmp /= f;
483  return tmp;
484 }
485 
490 template <class T>
491 inline const T3DVector<T> operator / (const T3DVector<T>& a, const T3DVector<T>& b)
492 {
493  assert(b.x != 0.0 && b.y != 0.0 && b.z != 0.0);
494  return T3DVector<T>(a.x / b.x, a.y / b.y, a.z / b.z);
495 }
496 
497 
499 template <class T>
500 inline const T3DVector<T> operator *(const T3DVector<T>& a, double f)
501 {
502  T3DVector<T> tmp (a);
503  tmp *= f;
504  return tmp;
505 }
506 
507 
509 template <class T>
510 inline const T3DVector<T> operator *(double f, const T3DVector<T>& a)
511 {
512  return a * f;
513 }
514 
515 
517 template <class T>
518 inline const T3DVector<T> operator ^(const T3DVector<T>& a, const T3DVector<T>& b)
519 {
520  return T3DVector<T>( a.y * b.z - b.y * a.z,
521  a.z * b.x - b.z * a.x,
522  a.x * b.y - b.x * a.y);
523 }
524 
526 template <class T>
527 inline bool operator == (const T3DVector<T>& a, const T3DVector<T>& b)
528 {
529  return (b.x == a.x && b.y == a.y && b.z == a.z);
530 }
531 
533 template <class T>
534 inline bool operator != (const T3DVector<T>& a, const T3DVector<T>& b)
535 {
536  return ! (a == b);
537 }
538 
540 template <class T>
541 bool operator < (const T3DVector<T>& a, const T3DVector<T>& b)
542 {
543  return (a.x < b.x && a.y < b.y && a.z < b.z);
544 }
545 
547 template <class T>
548 bool operator <= (const T3DVector<T>& b, const T3DVector<T>& a)
549 {
550  return (b.x <= a.x && b.y <= a.y && b.z <= a.z);
551 }
552 
554 template <class T>
555 bool operator > (const T3DVector<T>& b, const T3DVector<T>& a)
556 {
557  return (b.x > a.x && b.y > a.y && b.z > a.z);
558 }
559 
561 template <class T>
562 bool operator >= (const T3DVector<T>& b, const T3DVector<T>& a)
563 {
564  return (b.x >= a.x && b.y >= a.y && b.z >= a.z);
565 }
566 template <typename T >
568 
569 template <typename T >
571 
572 template <typename T>
573 struct less_then<T3DVector<T>> {
574  bool operator() (const T3DVector<T>& a, const T3DVector<T>& b) const
575  {
576  return a.z < b.z ||
577  (a.z == b.z &&
578  (a.y < b.y || (a.y == b.y && a.x < b.x)));
579  }
580 };
581 
582 
583 
585 
586 
587 #endif
T3DVector::value_type
T value_type
typedef for generic programming
Definition: 3d/vector.hh:59
operator+
const T3DVector< T > operator+(const T3DVector< T > &a, const T3DVector< T > &b)
vector addition
Definition: 3d/vector.hh:438
operator<=
bool operator<=(const T3DVector< T > &b, const T3DVector< T > &a)
comparison less or equal, returns true if all components of a are less or equal then those of b
Definition: 3d/vector.hh:548
T3DVector::operator[]
const T operator[](size_t i) const
Definition: 3d/vector.hh:135
T3DVector::xzy
const T3DVector< T > xzy() const
swizzle operator
Definition: 3d/vector.hh:309
operator<<
std::ostream & operator<<(std::ostream &os, const T3DVector< T > &v)
stream output operator for 3DVector
Definition: 3d/vector.hh:421
T3DVector::fill
void fill(T v)
Fill the vector elements with value v.
Definition: 3d/vector.hh:122
T3DVector::operator-
T3DVector operator-() const
Definition: 3d/vector.hh:228
operator==
bool operator==(const T3DVector< T > &a, const T3DVector< T > &b)
comparison operator equal
Definition: 3d/vector.hh:527
C3DBounds
T3DVector< unsigned int > C3DBounds
A unsinged int 3D Vector (used for 3D field sizes)
Definition: 3d/vector.hh:415
NS_MIA_BEGIN
#define NS_MIA_BEGIN
conveniance define to start the mia namespace
Definition: defines.hh:33
operator>
bool operator>(const T3DVector< T > &b, const T3DVector< T > &a)
comparison greater, returns true if all components of a are greater then those of b
Definition: 3d/vector.hh:555
T3DVector::operator*=
T3DVector< T > & operator*=(const double a)
inplace multiplication
Definition: 3d/vector.hh:200
T3DVector::xyz
const T3DVector< T > & xyz() const
swizzle operator
Definition: 3d/vector.hh:303
C3DDVector
T3DVector< double > C3DDVector
A double 3D Vector.
Definition: 3d/vector.hh:412
T3DVector::x
T x
vector element
Definition: 3d/vector.hh:52
EAttributeType_3d
Definition: 3d/vector.hh:349
attribute_type
Definition: attributetype.hh:64
NS_MIA_END
#define NS_MIA_END
conveniance define to end the mia namespace
Definition: defines.hh:36
T3DVector::T3DVector
T3DVector()
standart constructor
Definition: 3d/vector.hh:62
operator-
const T3DVector< T > operator-(const T3DVector< T > &a, const T3DVector< T > &b)
vector subtraction
Definition: 3d/vector.hh:462
EAttributeType
Definition: attributetype.hh:31
T3DVector::norm2
double norm2() const
square of Euclidian norm of the vector
Definition: 3d/vector.hh:99
T3DVector::operator=
T3DVector< T > & operator=(const T3DVector< T > &other)=default
we provide the default copy mechanisms
T3DVector::T3DVector
T3DVector(int dim)
create a zero-vector, dim must be 3
Definition: 3d/vector.hh:65
T3DVector::product
double product() const
Definition: 3d/vector.hh:105
T3DVector::T3DVector
T3DVector(const T &x_, const T &y_, const T &z_)
constructor to construct vector from values
Definition: 3d/vector.hh:77
T3DVector::read
void read(std::istream &is)
read the vector from a formatted string
Definition: 3d/vector.hh:240
defines3d.hh
T3DVector::operator-=
T3DVector< T > & operator-=(const T3DVector< T > &a)
inplace subtraction
Definition: 3d/vector.hh:191
fabs
double fabs(const T3DVector< T > &t)
A way to get the norm of a T3DVector using faba syntax.
Definition: 3d/vector.hh:396
T3DVector::zyx
const T3DVector< T > zyx() const
swizzle operator
Definition: 3d/vector.hh:327
operator^
const T3DVector< T > operator^(const T3DVector< T > &a, const T3DVector< T > &b)
3D vector cross product
Definition: 3d/vector.hh:518
operator>=
bool operator>=(const T3DVector< T > &b, const T3DVector< T > &a)
comparison greater or equal, returns true if all components of a are greater or equal then those of b
Definition: 3d/vector.hh:562
EAttributeType_3d::vector_3d_bit
static const int vector_3d_bit
Definition: 3d/vector.hh:351
operator<
bool operator<(const T3DVector< T > &a, const T3DVector< T > &b)
comparison less, returns true if all components of a are less then those of b
Definition: 3d/vector.hh:541
T3DVector::norm
double norm() const
Definition: 3d/vector.hh:110
C3DFVector
T3DVector< float > C3DFVector
A float 3D Vector.
Definition: 3d/vector.hh:409
T3DVector::_1
static T3DVector< T > _1
declare the vector (1,1,1)
Definition: 3d/vector.hh:339
T3DVector::operator/=
T3DVector< T > & operator/=(const double a)
inplace divisison by a scalar
Definition: 3d/vector.hh:219
operator>>
std::istream & operator>>(std::istream &is, T3DVector< T > &v)
stream input operator for 3DVector
Definition: 3d/vector.hh:429
operator*
const T3DVector< T > operator*(const T3DVector< T > &a, const T3DVector< T > &b)
vector scalar product
Definition: 3d/vector.hh:471
dot
double dot(const T3DVector< T > &a, const T3DVector< T > &b)
Definition: 3d/vector.hh:402
cross
T3DVector< T > cross(const T3DVector< T > &a, const T3DVector< T > &b)
Definition: 3d/vector.hh:385
T3DVector::yzx
const T3DVector< T > yzx() const
swizzle operator
Definition: 3d/vector.hh:321
T3DVector::write
void write(std::ostream &os) const
print out the formatted vector to the stream
Definition: 3d/vector.hh:234
EAttributeType_3d::is_vector3d
static bool is_vector3d(int type)
Definition: 3d/vector.hh:353
T3DVector::zxy
const T3DVector< T > zxy() const
swizzle operator
Definition: 3d/vector.hh:333
T3DVector::y
T y
vector element
Definition: 3d/vector.hh:54
T3DVector::T3DVector
T3DVector(const T3DVector< in > &org)
type casting copy constructor
Definition: 3d/vector.hh:83
attributetype.hh
operator/
const T3DVector< T > operator/(const T3DVector< T > &a, double f)
vector division by scalar
Definition: 3d/vector.hh:478
attribute_type::value
static const int value
Definition: attributetype.hh:65
type_traits.hh
T3DVector
A simple 3D vector type.
Definition: 3d/vector.hh:48
defines.hh
operator!=
bool operator!=(const T3DVector< T > &a, const T3DVector< T > &b)
comparison operator not equal
Definition: 3d/vector.hh:534
T3DVector::_0
static T3DVector< T > _0
declare the vector (0,0,0)
Definition: 3d/vector.hh:342
T3DVector::yxz
const T3DVector< T > yxz() const
swizzle operator
Definition: 3d/vector.hh:315
T3DVector::operator+=
T3DVector< T > & operator+=(const T3DVector< T > &a)
inplace addition
Definition: 3d/vector.hh:182
T3DVector::size
int size() const
Definition: 3d/vector.hh:116
T3DVector::z
T z
vector element
Definition: 3d/vector.hh:56
T3DVector::elements
static const unsigned int elements
the number of elements this vector holds (=3)
Definition: 3d/vector.hh:345