Ravelin
Origin3.h
1 /****************************************************************************
2  * Copyright 2013 Evan Drumwright
3  * This library is distributed under the terms of the Apache V2.0
4  * License (obtainable from http://www.apache.org/licenses/LICENSE-2.0).
5  ****************************************************************************/
6 
7 #ifndef ORIGIN3
8 #error This class is not to be included by the user directly. Use Origin3d.h or Origin3d.h instead.
9 #endif
10 
11 class VECTOR3;
13 class SHAREDVECTORN;
14 
16 class ORIGIN3
17 {
18  public:
19  ORIGIN3() {}
20  ORIGIN3(REAL x, REAL y, REAL z);
21  ORIGIN3(const REAL* array);
22  ORIGIN3(const CONST_SHAREDVECTORN& v) { operator=(v); }
23  ORIGIN3(const SHAREDVECTORN& v) { operator=(v); }
24  explicit ORIGIN3(const VECTOR3& v) { operator=(v); }
25  REAL norm_inf() const { return std::max(std::max(std::fabs(_data[0]), std::fabs(_data[1])), std::fabs(_data[2])); }
26  REAL norm() const { return std::sqrt(norm_sq()); }
27  REAL norm_sq() const { return sqr(_data[0]) + sqr(_data[1]) + sqr(_data[2]); }
28  static REAL norm(const ORIGIN3& v) { return std::sqrt(norm_sq(v)); }
29  static REAL norm_sq(const ORIGIN3& v) { return v.norm_sq(); }
30  void normalize() { assert(norm() > std::numeric_limits<REAL>::epsilon()); operator/=(norm()); }
31  static ORIGIN3 normalize(const ORIGIN3& v) { ORIGIN3 w = v; w.normalize(); return w; }
32  REAL dot(const ORIGIN3& v) const { return _data[0]*v._data[0] + _data[1]*v._data[1] + _data[2]*v._data[2]; }
33  static REAL dot(const ORIGIN3& v1, const ORIGIN3& v2) { return v1.dot(v2); }
34  ORIGIN3& set_zero() { _data[0] = _data[1] = _data[2] = (REAL) 0.0; return *this; }
35  ORIGIN3& set_zero(unsigned m) { assert(m==3); return set_zero(); }
36  ORIGIN3& set_zero(unsigned m, unsigned n) { assert(m==3 && n==1); return set_zero(); }
37  static ORIGIN3 zero() { return ORIGIN3((REAL) 0.0, (REAL) 0.0, (REAL) 0.0); }
38  ORIGIN3& operator=(const ORIGIN3& o);
39  ORIGIN3& operator=(const VECTOR3& v);
41  ORIGIN3& operator=(const SHAREDVECTORN& v);
42  ORIGIN3 operator+(const ORIGIN3& o) const;
43  VECTOR3 operator+(const VECTOR3& v) const;
44  ORIGIN3 operator-(const ORIGIN3& v) const;
45  VECTOR3 operator-(const VECTOR3& v) const;
46  ORIGIN3& operator+=(const ORIGIN3& v);
47  ORIGIN3& operator-=(const ORIGIN3& v);
48  ORIGIN3& operator*=(REAL scalar) { _data[0] *= scalar; _data[1] *= scalar; _data[2] *= scalar; return *this; }
49  ORIGIN3& operator/=(REAL scalar) { _data[0] /= scalar; _data[1] /= scalar; _data[2] /= scalar; return *this; }
50  ORIGIN3 operator*(REAL scalar) const { ORIGIN3 v = *this; v *= scalar; return v; }
51  ORIGIN3 operator/(REAL scalar) const { ORIGIN3 v = *this; v /= scalar; return v; }
52  ORIGIN3 operator-() const { return ORIGIN3(-_data[0], -_data[1], -_data[2]); }
53  REAL* data() { return _data; }
54  const REAL* data() const { return _data; }
55  REAL& operator[](const unsigned i);
56  const REAL& operator[](const unsigned i) const;
57  REAL* data(unsigned i);
58  const REAL* data(unsigned i) const;
59  const REAL& x() const { return _data[0]; }
60  const REAL& y() const { return _data[1]; }
61  const REAL& z() const { return _data[2]; }
62  REAL& x() { return _data[0]; }
63  REAL& y() { return _data[1]; }
64  REAL& z() { return _data[2]; }
65  COLUMN_ITERATOR begin() { return column_iterator_begin(); }
66  CONST_COLUMN_ITERATOR begin() const { return column_iterator_begin(); }
67  COLUMN_ITERATOR end() { return column_iterator_end(); }
68  CONST_COLUMN_ITERATOR end() const { return column_iterator_end(); }
69  COLUMN_ITERATOR column_iterator_begin();
70  CONST_COLUMN_ITERATOR column_iterator_begin() const;
71  COLUMN_ITERATOR column_iterator_end();
72  CONST_COLUMN_ITERATOR column_iterator_end() const;
73  ROW_ITERATOR row_iterator_begin();
74  CONST_ROW_ITERATOR row_iterator_begin() const;
75  ROW_ITERATOR row_iterator_end();
76  CONST_ROW_ITERATOR row_iterator_end() const;
77  unsigned size() const { return 3; }
78  unsigned rows() const { return 3; }
79  unsigned columns() const { return 1; }
80  unsigned inc() const { return 1; }
81  unsigned leading_dim() const { return 3; }
82  ORIGIN3& resize(unsigned m, bool preserve = false);
83  ORIGIN3& resize(unsigned m, unsigned n, bool preserve = false);
84  static ORIGIN3 cross(const ORIGIN3& v1, const ORIGIN3& v2);
85 
86  private:
87  REAL _data[3];
88  static REAL sqr(REAL x) { return x*x; }
89 }; // end class
90 
91 inline ORIGIN3 operator*(REAL scalar, const ORIGIN3& v) { return v * scalar; }
92 
94 inline std::ostream& operator<<(std::ostream& out, const ORIGIN3& v)
95 {
96  out << "[" << v[0] << " " << v[1] << " " << v[2] << "] ";
97 
98  return out;
99 };
100 
ORIGIN3 & operator=(const ORIGIN3 &o)
Assigns the values from one origin to this origin.
Definition: Origin3.cpp:67
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:210
ORIGIN3 & operator+=(const ORIGIN3 &v)
Adds two origins.
Definition: Origin3.cpp:116
ORIGIN3 operator+(const ORIGIN3 &o) const
Adds two origins.
Definition: Origin3.cpp:96
ORIGIN3 operator-(const ORIGIN3 &v) const
Subtracts one origin from another.
Definition: Origin3.cpp:135
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:77
A generic N-dimensional floating point vector.
Definition: SharedVectorN.h:15
A three-dimensional floating point vector used for representing points and vectors in 3D with associa...
Definition: Vector3.h:15
ORIGIN3 & operator-=(const ORIGIN3 &v)
Subtracts an origin from this.
Definition: Origin3.cpp:145
A three-dimensional floating point vector used for representing points and vectors in 3D and without ...
Definition: Origin3.h:16
A construct for iterating over a rectangular block of a matrix.
Definition: ColumnIterator.h:12
A construct for iterating over a rectangular block of a matrix.
Definition: ColumnIterator.h:215
static ORIGIN3 cross(const ORIGIN3 &v1, const ORIGIN3 &v2)
Computes the cross-product of two vectors.
Definition: Origin3.cpp:294
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:12
ORIGIN3 & resize(unsigned m, bool preserve=false)
Does nothing.
Definition: Origin3.cpp:76