Ravelin
Origin2.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 ORIGIN2
8 #error This class is not to be included by the user directly. Use Origin2d.h or Origin2f.h instead.
9 #endif
10 
11 class VECTOR2;
12 
14 class ORIGIN2
15 {
16  public:
17  ORIGIN2() {}
18  ORIGIN2(REAL x, REAL y);
19  ORIGIN2(const REAL* array);
20  explicit ORIGIN2(const VECTOR2& v) { operator=(v); }
21  REAL norm_inf() const { return std::max(std::fabs(_data[0]), std::fabs(_data[1])); }
22  REAL norm() const { return std::sqrt(norm_sq()); }
23  REAL norm_sq() const { return sqr(_data[0]) + sqr(_data[1]); }
24  static REAL norm(const ORIGIN2& v) { return std::sqrt(norm_sq(v)); }
25  static REAL norm_sq(const ORIGIN2& v) { return v.norm_sq(); }
26  ORIGIN2& set_zero() { _data[0] = _data[1] = 0.0; return *this; }
27  ORIGIN2& set_zero(unsigned m) { assert(m==2); return set_zero(); }
28  ORIGIN2& set_zero(unsigned m, unsigned n) { assert(m==2 && n==1); return set_zero(); }
29  static ORIGIN2 zero() { return ORIGIN2(0.0, 0.0); }
30  ORIGIN2& operator=(const VECTOR2& v);
31  ORIGIN2& operator=(const ORIGIN2& v) { _data[0] = v[0]; _data[1] = v[1]; return *this; }
32  ORIGIN2 operator+(const ORIGIN2& v) const { return ORIGIN2(_data[0] + v[0], _data[1] + v[1]); }
33  VECTOR2 operator+(const VECTOR2& p) const;
34  VECTOR2 operator-(const VECTOR2& p) const;
35  ORIGIN2 operator-(const ORIGIN2& v) const { return ORIGIN2(_data[0] - v[0], _data[1] - v[1]); }
36  ORIGIN2& operator+=(const ORIGIN2& v) { _data[0] += v[0]; _data[1] += v[1]; return *this; }
37  ORIGIN2& operator-=(const ORIGIN2& v) { _data[0] -= v[0]; _data[1] -= v[1]; return *this; }
38  ORIGIN2& operator*=(REAL scalar) { _data[0] *= scalar; _data[1] *= scalar; return *this; }
39  ORIGIN2& operator/=(REAL scalar) { _data[0] /= scalar; _data[1] /= scalar; return *this; }
40  ORIGIN2 operator*(REAL scalar) const { ORIGIN2 v = *this; v *= scalar; return v; }
41  ORIGIN2 operator/(REAL scalar) const { ORIGIN2 v = *this; v /= scalar; return v; }
42  ORIGIN2 operator-() const { return ORIGIN2(-_data[0], -_data[1]); }
43  REAL* data() { return _data; }
44  const REAL* data() const { return _data; }
45  REAL& operator[](const unsigned i);
46  const REAL& operator[](const unsigned i) const;
47  REAL* data(unsigned i);
48  const REAL* data(unsigned i) const;
49  const REAL& x() const { return _data[0]; }
50  const REAL& y() const { return _data[1]; }
51  REAL& x() { return _data[0]; }
52  REAL& y() { return _data[1]; }
53  unsigned size() const { return 3; }
54  unsigned rows() const { return 3; }
55  unsigned columns() const { return 1; }
56  unsigned leading_dim() const { return 3; }
57  unsigned inc() const { return 1; }
58  ORIGIN2& resize(unsigned m, bool preserve = false);
59  ORIGIN2& resize(unsigned m, unsigned n, bool preserve = false);
60 
61  private:
62  REAL _data[2];
63  static REAL sqr(REAL x) { return x*x; }
64 }; // end class
65 
66 inline ORIGIN2 operator*(REAL scalar, const ORIGIN2& v) { return v * scalar; }
67 
69 inline std::ostream& operator<<(std::ostream& out, const ORIGIN2& v)
70 {
71  out << '[' << v[0] << ',' << ' ' << v[1] << ']' << ' ';
72  return out;
73 };
74 
ORIGIN2 & resize(unsigned m, bool preserve=false)
Does nothing.
Definition: Origin2.cpp:27
A two-dimensional floating point vector used for computational geometry calculations and with associa...
Definition: Vector2.h:15
A two-dimensional floating point vector used for computational geometry calculations and without asso...
Definition: Origin2.h:14
ORIGIN2 & operator=(const VECTOR2 &v)
Constructs this vector with the given values.
Definition: Origin2.cpp:47
VECTOR2 operator-(const VECTOR2 &p) const
Subtract a vector from this origin to yield a vector.
Definition: Origin2.cpp:62