~valhalla-routing/+junk/valhalla_2.1.9-0ubuntu1

« back to all changes in this revision

Viewing changes to libvalhalla/valhalla/midgard/vector2.h

  • Committer: valhalla
  • Date: 2017-04-24 20:20:53 UTC
  • Revision ID: valhalla@mapzen.com-20170424202053-7o69b9nwx9ee0tw3
PackagingĀ forĀ 2.1.9-0ubuntu1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef VALHALLA_MIDGARD_VECTOR2_H_
 
2
#define VALHALLA_MIDGARD_VECTOR2_H_
 
3
 
 
4
#include <stdarg.h>
 
5
#include <math.h>
 
6
 
 
7
#include <valhalla/midgard/constants.h>
 
8
#include <valhalla/midgard/point2.h>
 
9
 
 
10
namespace valhalla {
 
11
namespace midgard {
 
12
 
 
13
/**
 
14
 * 2D vector class. float x,y components.
 
15
 * @author  David W. Nesbitt
 
16
 */
 
17
class Vector2 {
 
18
 public:
 
19
  /**
 
20
   * Default constructor
 
21
   */
 
22
  Vector2();
 
23
 
 
24
  /**
 
25
   * Constructor given a point.  Essentially a vector from the
 
26
   * origin to the point.
 
27
   * @param   p  Point.
 
28
   */
 
29
  Vector2(const Point2& p);
 
30
 
 
31
  /**
 
32
   * Constructor given components of the vector.
 
33
   * @param   x   x component of the vector.
 
34
   * @param   y   y component of the vector.
 
35
   */
 
36
  Vector2(const float x, const float y);
 
37
 
 
38
  /**
 
39
   * Constructor from one point to another.
 
40
   * @param   from  Point at origin of the vector.
 
41
   * @param   to    Point at end of vector
 
42
   */
 
43
  Vector2(const Point2& from, const Point2& to);
 
44
 
 
45
  /**
 
46
   * Copy constructor.
 
47
   * @param   w  Vector to copy to the new vector.
 
48
   */
 
49
  Vector2(const Vector2& w);
 
50
 
 
51
  /**
 
52
   * Assignment operator
 
53
   * @param   w  Vector to copy to the current vector.
 
54
   * @return  Returns the address of the current vector.
 
55
   */
 
56
  Vector2& operator =(const Vector2& w);
 
57
 
 
58
  /**
 
59
   * Destructor
 
60
   */
 
61
  ~Vector2();
 
62
 
 
63
  /**
 
64
   * Get the x component.
 
65
   * @return  Returns the x component of the vector.
 
66
   */
 
67
  float x() const;
 
68
 
 
69
  /**
 
70
   * Get the y component.
 
71
   * @return  Returns the y component of the vector.
 
72
   */
 
73
  float y() const;
 
74
 
 
75
  /**
 
76
   * Set the x component.
 
77
   * @param  x  x coordinate value.
 
78
   */
 
79
  void set_x(const float x);
 
80
 
 
81
  /**
 
82
   * Set the y component.
 
83
   * @param  y  y coordinate value.
 
84
   */
 
85
  void set_y(const float y);
 
86
 
 
87
  /**
 
88
   * Set the current vector to the specified components.
 
89
   * @param   x   x component of the vector.
 
90
   * @param   y   y component of the vector.
 
91
   */
 
92
  void Set(const float x, const float y);
 
93
 
 
94
  /**
 
95
   * Set the vector components to those of a point.  Essentially a
 
96
   * vector from the origin to the point.
 
97
   * @param   p  Point.
 
98
   */
 
99
  void Set(const Point2& p);
 
100
 
 
101
  /**
 
102
   * Set the current vector to be from one point to another.
 
103
   * @param   from  Point at origin of the vector.
 
104
   * @param   to    Point at end of vector
 
105
   */
 
106
  void Set(const Point2& from, const Point2& to);
 
107
 
 
108
  /**
 
109
   * Creates a new vector that is the current vector plus the
 
110
   * specified vector.
 
111
   * @param   w  Vector to add to the current vector.
 
112
   * @return   Returns the resulting vector.
 
113
   */
 
114
  Vector2 operator +(const Vector2& w) const;
 
115
 
 
116
  /**
 
117
   * Adds vector w to the current vector.
 
118
   * @param   w  Vector to add to the current vector.
 
119
   * @return  Returns the address of the current vector.
 
120
   */
 
121
  Vector2& operator +=(const Vector2& w);
 
122
 
 
123
  /**
 
124
   * Creates a new vector that is the current vector minus the
 
125
   * specified vector.
 
126
   * @param   w  Vector to subtract from the current vector.
 
127
   * @return   Returns the resulting vector.
 
128
   */
 
129
  Vector2 operator -(const Vector2& w) const;
 
130
 
 
131
  /**
 
132
   * Subtracts vector w from the current vector.
 
133
   * @param   w  Vector to subtract from the current vector.
 
134
   * @return  Returns the address of the current vector.
 
135
   */
 
136
  Vector2& operator -=(const Vector2& w);
 
137
 
 
138
  /**
 
139
   * Creates a new vector that is the current vector multiplied
 
140
   * with the specified scalar.
 
141
   * @param   scalar   Scalar to muliply the vector with.
 
142
   * @return  Returns the resulting vector
 
143
   */
 
144
  Vector2 operator *(const float scalar) const;
 
145
 
 
146
  /**
 
147
   * Multiplies the current vector by a scalar
 
148
   * @param   scalar   Scalar to muliply the vector with.
 
149
   * @return  Returns the address of the current vector.
 
150
   */
 
151
  Vector2& operator *=(const float scalar);
 
152
 
 
153
  /**
 
154
   * Equality operator.
 
155
   * @param   w  Vector to test if equal to the current vector.
 
156
   * @return  Returns true if vector w equals the current vector,
 
157
   *          false otherwise.
 
158
   */
 
159
  bool operator ==(const Vector2& w) const;
 
160
 
 
161
  /**
 
162
   * Computes the dot product of the current vector with the
 
163
   * specified vector.
 
164
   * @param   w  Vector
 
165
   * @return  Returns the dot product (a scalar).
 
166
   */
 
167
  float Dot(const Vector2& w) const;
 
168
 
 
169
  /**
 
170
   * Computes the 2D cross product of current vector with w0.
 
171
   * @param   w  Vector to take the cross product with (current X w)
 
172
   * @return  Returns the magnitude of the resulting vector (which is
 
173
   *          along the z axis)
 
174
   */
 
175
  float Cross(const Vector2& w) const;
 
176
 
 
177
  /**
 
178
   * Get a perpendicular vector to this vector.
 
179
   * @param  clockwise  If true. get the clockwise oriented perpendicular.
 
180
   *                    If false, get the counter-clockwise oriented
 
181
   *                    perpendicular.
 
182
   */
 
183
  Vector2 GetPerpendicular(const bool clockwise = false) const;
 
184
 
 
185
  /**
 
186
   * Computes the norm (length) of the current vector.
 
187
   * @return  Returns the length of the vector.
 
188
   */
 
189
  float Norm() const;
 
190
 
 
191
  /**
 
192
   * Computes the squared norm of a vector
 
193
   * (Useful when absolute distance is not required)
 
194
   * @return  Returns the length squared of the vector.
 
195
   */
 
196
  float NormSquared(void) const;
 
197
 
 
198
  /**
 
199
   * Normalizes the vector.
 
200
   * @return  Returns the address of the current vector.
 
201
   */
 
202
  Vector2& Normalize();
 
203
 
 
204
  /**
 
205
   * Calculates the component of the current vector along the
 
206
   * specified vector
 
207
   * @param   w  Vector to determine component along.
 
208
   * @return  Returns the component of the current vector along w.
 
209
   */
 
210
  float Component(const Vector2& w) const;
 
211
 
 
212
  /**
 
213
   * Creates a new vector that is the projection of the current
 
214
   * vector along the specified vector.
 
215
   * @param   w  Vector to determine projection along.
 
216
   * @return  Returns the new vector.
 
217
   */
 
218
  Vector2 Projection(const Vector2& w) const;
 
219
 
 
220
  /**
 
221
   * Calculates the angle (radians) between the current vector and
 
222
   * the specified vector.
 
223
   * @param   w  Vector to determine angle from current vector.
 
224
   * @return  Returns the angle in radians between the two vectors.
 
225
   */
 
226
  float AngleBetween(const Vector2& w) const;
 
227
 
 
228
  /**
 
229
   * Reflects the current vector given a normal to the reflecting surface.
 
230
   * Assumes the normal is unit length.  Note that if done properly the
 
231
   * magnitude of the reflected vector will equal the magnitude of the
 
232
   * incoming vector.
 
233
   * @param   normal  unit length normal to the vector where reflection occurs
 
234
   * @return  Returns the reflected vector
 
235
   */
 
236
  Vector2 Reflect(const Vector2& normal) const;
 
237
 
 
238
 private:
 
239
  // x,y coordinate of the point
 
240
  float x_;
 
241
  float y_;
 
242
};
 
243
 
 
244
/**
 
245
 * Creates a new vector that is the specified vector multiplied
 
246
 * with the specified scalar.
 
247
 * @param   scalar   Scalar to muliply the vector with.
 
248
 * @param   Vector2  Vector to be multiplied with the scalar
 
249
 * @return  Returns the resulting vector
 
250
 */
 
251
Vector2 operator *(float s, const Vector2 &v);
 
252
 
 
253
}
 
254
}
 
255
 
 
256
#endif  // VALHALLA_MIDGARD_VECTOR2_H_