Ravelin
Vector2.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 VECTOR2
8 #error This class is not to be included by the user directly. Use VECTOR2.h or Vector2f.h instead.
9 #endif
10 
11 class ORIGIN2;
12 class POSE2;
13 
15 class VECTOR2
16 {
17  public:
18  VECTOR2(boost::shared_ptr<const POSE2> pose = boost::shared_ptr<const POSE2>()) { this->pose = pose; }
19  VECTOR2(boost::shared_ptr<POSE2> pose) { this->pose = boost::const_pointer_cast<const POSE2>(pose); }
20  VECTOR2(REAL x, REAL y, boost::shared_ptr<const POSE2> pose = boost::shared_ptr<const POSE2>());
21  VECTOR2(REAL x, REAL y, boost::shared_ptr<POSE2> pose);
22  VECTOR2(const REAL* array, boost::shared_ptr<const POSE2> pose = boost::shared_ptr<const POSE2>());
23  VECTOR2(const REAL* array, boost::shared_ptr<POSE2> pose);
24  VECTOR2(const ORIGIN2& o, boost::shared_ptr<POSE2> pose) { this->pose = boost::const_pointer_cast<const POSE2>(pose); operator=(o); }
25  VECTOR2(const ORIGIN2& o, boost::shared_ptr<const POSE2> pose) { this->pose = pose; operator=(o); }
26  REAL dot(const VECTOR2& v) const { return v[0]*_data[0] + v[1]*_data[1]; }
27  static REAL dot(const VECTOR2& v1, const VECTOR2& v2) { return v1[0]*v2[0] + v1[1]*v2[1]; }
28  REAL norm_inf() const { return std::max(std::fabs(_data[0]), std::fabs(_data[1])); }
29  REAL norm() const { return std::sqrt(norm_sq()); }
30  REAL norm_sq() const { return dot(*this, *this); }
31  void normalize() { assert(norm() > std::numeric_limits<REAL>::epsilon()); operator/=(norm()); }
32  static VECTOR2 normalize(const VECTOR2& v) { VECTOR2 w = v; w.normalize(); return w; }
33  unsigned size() const { return 2; }
34  static REAL norm(const VECTOR2& v) { return std::sqrt(norm_sq(v)); }
35  static REAL norm_sq(const VECTOR2& v) { return v.dot(v); }
36  VECTOR2& set_zero() { _data[0] = _data[1] = 0.0; return *this; }
37  VECTOR2& set_one() { _data[0] = _data[1] = 1.0; return *this; }
38  VECTOR2& set_zero(boost::shared_ptr<const POSE2> pose) { _data[0] = _data[1] = 0.0; this->pose = pose; return *this; }
39  VECTOR2& set_one(boost::shared_ptr<const POSE2> pose) { _data[0] = _data[1] = 1.0; this->pose = pose; return *this; }
40  static VECTOR2 zero(boost::shared_ptr<const POSE2> pose = boost::shared_ptr<const POSE2>()) { return VECTOR2(0.0, 0.0, pose); }
41  VECTOR2& operator=(const VECTOR2& v) { _data[0] = v[0]; _data[1] = v[1]; pose = v.pose; return *this; }
42  VECTOR2& operator=(const ORIGIN2& o);
43  VECTOR2 operator+(const VECTOR2& v) const { VECTOR2 w = *this; return w += v; }
44  VECTOR2 operator-(const VECTOR2& v) const { VECTOR2 w = *this; return w -= v; }
45  VECTOR2 operator+(const ORIGIN2& o) const { VECTOR2 v = *this; return v += o; }
46  VECTOR2 operator-(const ORIGIN2& o) const { VECTOR2 v = *this; return v -= o; }
47  VECTOR2& operator+=(const VECTOR2& v);
48  VECTOR2& operator-=(const VECTOR2& v);
49  VECTOR2& operator+=(const ORIGIN2& o);
50  VECTOR2& operator-=(const ORIGIN2& o);
51  VECTOR2& operator*=(REAL scalar) { _data[0] *= scalar; _data[1] *= scalar; return *this; }
52  VECTOR2& operator/=(REAL scalar) { _data[0] /= scalar; _data[1] /= scalar; return *this; }
53  VECTOR2 operator*(REAL scalar) const { VECTOR2 v = *this; v *= scalar; return v; }
54  VECTOR2 operator/(REAL scalar) const { VECTOR2 v = *this; v /= scalar; return v; }
55  VECTOR2 operator-() const { return VECTOR2(-_data[0], -_data[1]); }
56  REAL* data() { return _data; }
57  const REAL* data() const { return _data; }
58  unsigned rows() const { return 2; }
59  unsigned columns() const { return 1; }
60  unsigned inc() const { return 1; }
61  unsigned leading_dim() const { return 2; }
62  REAL& operator[](const unsigned i);
63  const REAL& operator[](const unsigned i) const;
64  VECTOR2& resize(unsigned m, unsigned n, bool preserve = false);
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  REAL* data(unsigned i);
78  const REAL* data(unsigned i) const;
79  const REAL& x() const { return _data[0]; }
80  const REAL& y() const { return _data[1]; }
81  REAL& x() { return _data[0]; }
82  REAL& y() { return _data[1]; }
83 
85  VECTOR2 perp() const { return VECTOR2(_data[1], -_data[0]); }
86 
88  REAL dot_perp(const VECTOR2& v) const { return dot(v.perp()); }
89 
91  boost::shared_ptr<const POSE2> pose;
92 
93  private:
94  REAL _data[2];
95 }; // end class
96 
97 inline VECTOR2 operator*(REAL scalar, const VECTOR2& v) { return v * scalar; }
98 
100 inline std::ostream& operator<<(std::ostream& out, const VECTOR2& v)
101 {
102  out << '[' << v[0] << ',' << ' ' << v[1] << ']' << ' ';
103  return out;
104 };
105 
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:210
A 2D rigid body pose.
Definition: Pose2.h:12
A two-dimensional floating point vector used for computational geometry calculations and with associa...
Definition: Vector2.h:15
VECTOR2 perp() const
Computes the "perp" operator.
Definition: Vector2.h:85
VECTOR2 & operator-=(const VECTOR2 &v)
Subtracts a vector from this.
Definition: Vector2.cpp:223
boost::shared_ptr< const POSE2 > pose
The pose that this vector is defined in.
Definition: Vector2.h:91
VECTOR2 & operator+=(const VECTOR2 &v)
Adds a vector to this.
Definition: Vector2.cpp:208
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
A two-dimensional floating point vector used for computational geometry calculations and without asso...
Definition: Origin2.h:14
A construct for iterating over a rectangular block of a matrix.
Definition: RowIterator.h:12
REAL dot_perp(const VECTOR2 &v) const
Computes the dot "perp" product.
Definition: Vector2.h:88