gwnavruntime/math/vec2f.h Source File

vec2f.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 // primary contact: GUAL - secondary contact: NOBODY
9 #ifndef Navigation_Vec2f_H
10 #define Navigation_Vec2f_H
11 
12 
16 #include <math.h>
17 
18 
19 namespace Kaim
20 {
21 
24 class Vec2f
25 {
26  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
27 
28 public:
29  // ---------------------------------- Creation & Initialization ----------------------------------
30 
32  Vec2f() : x(0.0f), y(0.0f) {}
33 
35  Vec2f(KyFloat32 _x, KyFloat32 _y) : x(_x), y(_y) {}
36 
39  explicit Vec2f(KyFloat32* coords) { Set(coords); }
40 
42  KY_INLINE void Set(KyFloat32 _x, KyFloat32 _y) { x = _x; y = _y; }
43 
46  KY_INLINE void Set(KyFloat32* coords) { x = coords[0]; y = coords[1]; }
47 
48 
49  // -------------------------- Operators --------------------------------
50 
52  KY_INLINE KyFloat32& operator[](KyInt32 i) { return (&x)[i]; }
53 
55  KY_INLINE KyFloat32 operator[](KyInt32 i) const { return (&x)[i]; }
56 
58  KY_INLINE bool operator==(const Vec2f& v) const { return x == v.x && y == v.y; }
59 
61  KY_INLINE bool operator!=(const Vec2f& v) const { return x != v.x || y != v.y; }
62 
64  KY_INLINE bool operator<(const Vec2f& v) const;
65  KY_INLINE bool operator>(const Vec2f& v) const;
66  KY_INLINE bool operator<=(const Vec2f& v) const;
67  KY_INLINE bool operator>=(const Vec2f& v) const;
68 
70  KY_INLINE Vec2f& operator*=(KyFloat32 s) { x *= s; y *= s; return *this; }
71 
73  KY_INLINE Vec2f& operator/=(KyFloat32 d) { return operator*=(1.0f / d); }
74 
77  KY_INLINE Vec2f& operator+=(const Vec2f& v) { x += v.x; y += v.y; return *this; }
78 
81  KY_INLINE Vec2f& operator-=(const Vec2f& v) { x -= v.x; y -= v.y; return *this; }
82 
84  KY_INLINE Vec2f operator*(KyFloat32 s)const { return Vec2f(x * s, y * s); }
85 
87  KY_INLINE Vec2f operator/(KyFloat32 d) const { return operator*(1.0f / d); }
88 
91  KY_INLINE Vec2f operator+(const Vec2f& v) const { return Vec2f(x + v.x, y + v.y); }
92 
95  KY_INLINE Vec2f operator-(const Vec2f& v) const { return Vec2f(x - v.x, y - v.y); }
96 
98  KY_INLINE Vec2f operator-() const { return Vec2f(-x, -y); }
99 
101  KY_INLINE KyFloat32 operator*(const Vec2f& v) const { return x * v.x + y * v.y; } // DOT PRODUCT
102 
104  KY_INLINE KyFloat32 operator^(const Vec2f& v) const { return x * v.y - y * v.x; } // CROSS PRODUCT
105 
106 
107  // -------------------------- Main interface --------------------------------
108 
110  KY_INLINE KyFloat32 GetSquareLength() const { return x * x + y * y; }
111 
113  KY_INLINE KyFloat32 GetLength() const { return Sqrtf(x * x + y * y); }
114 
117  KY_INLINE KyFloat32 Normalize();
118 
124  KY_INLINE KyFloat32 GetNormalized(Vec2f& normalized) const;
125 
127  KY_INLINE Vec2f PerpCW() const { return Vec2f(y, -x); }
128 
130  KY_INLINE Vec2f PerpCCW() const { return Vec2f(-y, x); }
131 
132 
133  // -------------------------- Static methods --------------------------------
134 
136  static KY_INLINE Vec2f Zero() { return Vec2f(0.0f, 0.0f); }
137 
139  static KY_INLINE Vec2f UnitX() { return Vec2f(1.0f, 0.0f); }
140 
142  static KY_INLINE Vec2f UnitY() { return Vec2f(0.0f, 1.0f); }
143 
145  // ---------------------------------- Public Data Members ----------------------------------
146 
147  KyFloat32 x;
148  KyFloat32 y;
149 };
150 
153 inline void SwapEndianness(Endianness::Target e, Vec2f& self)
154 {
155  SwapEndianness(e, self.x);
156  SwapEndianness(e, self.y);
157 }
158 
159 // ----------------------------------- global functions -----------------------------------
161 KY_INLINE Vec2f operator*(KyFloat32 s, const Vec2f& v) { return Vec2f(v.x * s, v.y * s); }
162 
164 KY_INLINE KyFloat32 DotProduct(const Vec2f& v1, const Vec2f& v2) { return (v1.x * v2.x + v1.y * v2.y); }
165 
167 KY_INLINE KyFloat32 CrossProduct(const Vec2f& v1, const Vec2f& v2) { return v1 ^ v2; }
168 
171 KY_INLINE KyFloat32 SquareDistance(const Vec2f& v1, const Vec2f& v2)
172 {
173  const KyFloat32 dx = v2.x - v1.x;
174  const KyFloat32 dy = v2.y - v1.y;
175  return dx * dx + dy * dy;
176 }
177 
179 KY_INLINE KyFloat32 Distance(const Vec2f& v1, const Vec2f& v2)
180 {
181  return Sqrtf(SquareDistance(v1, v2));
182 }
183 
185 KY_INLINE Vec2f GetDir2d(const Vec2f& v1, const Vec2f& v2)
186 {
187  Vec2f AB = v2 - v1;
188  AB.Normalize();
189  return AB;
190 }
191 
194 KY_INLINE KyFloat32 GetAngleRad(const Vec2f& v1, const Vec2f& v2)
195 {
196  const KyFloat32 sqLengthV1 = v1.GetSquareLength();
197  const KyFloat32 sqLengthV2 = v2.GetSquareLength();
198  const KyFloat32 sqLengthProd = sqLengthV1 * sqLengthV2;
199 
200  if (sqLengthProd > 0.0f)
201  {
202  const KyFloat32 lengthProd = (sqLengthProd == 1.0f) ? 1.0f : Sqrtf(sqLengthProd);
203 
204  const KyFloat32 dotProd = v1 * v2;
205  KyFloat32 angleRad = Kaim::Acosf(dotProd / lengthProd);
206 
207  const KyFloat32 crossProd = v1 ^ v2;
208  return ((crossProd >= 0.0f) ? angleRad : (KY_2_PI - angleRad));
209  }
210  else
211  return 0.0f;
212 }
213 
215 KY_INLINE void Rotate(Vec2f& direction, KyFloat32 cosAngle, KyFloat32 sinAngle)
216 {
217  const KyFloat32 formerDirX = direction.x;
218  direction.x = cosAngle * formerDirX - sinAngle * direction.y;
219  direction.y = sinAngle * formerDirX + cosAngle * direction.y;
220 }
221 
222 
223 // ----------------------------------- inline implementation -----------------------------------
224 
225 KY_INLINE bool Vec2f::operator<(const Vec2f& v) const
226 {
227  if (x < v.x) return true;
228  if (x > v.x) return false;
229  return (y < v.y);
230 }
231 
232 KY_INLINE bool Vec2f::operator>(const Vec2f& v) const
233 {
234  if (x > v.x) return true;
235  if (x < v.x) return false;
236  return (y > v.y);
237 }
238 
239 KY_INLINE bool Vec2f::operator<=(const Vec2f& v) const { return !operator>(v); }
240 KY_INLINE bool Vec2f::operator>=(const Vec2f& v) const { return !operator<(v); }
241 
242 
243 KY_INLINE KyFloat32 Vec2f::Normalize()
244 {
245  const KyFloat32 length = GetLength();
246  if (length != 0.0f)
247  operator/=(length);
248  return length;
249 }
250 
251 
252 KY_INLINE KyFloat32 Vec2f::GetNormalized(Vec2f& normalized) const
253 {
254  // don't call Vec2f::Normalize() to avoid LHS
255  const KyFloat32 length = GetLength();
256  if (length != 0.0f)
257  {
258  const KyFloat32 invLength = 1.f / length;
259  normalized.Set(x * invLength, y * invLength);
260  return length;
261  }
262  else
263  {
264  normalized.Set(0.0f, 0.0f);
265  return 0.0f;
266  }
267 }
268 
269 
270 
271 template <class OSTREAM>
272 inline OSTREAM& operator<<(OSTREAM& os, const Vec2f& v)
273 {
274  os << "{" << v.x << ";" << v.y << "}";
275  return os;
276 }
277 
278 } // namespace Kaim
279 
280 #endif
Vec2f()
Creates a vector with coordinates (0,0)
Definition: vec2f.h:37
bool operator<(const Vec2f &v) const
x is compared first. ex: (1,5) < (2,0).="" />
Definition: vec2f.h:249
KyFloat32 GetSquareLength() const
Returns the square of the magnitude of the vector.
Definition: vec2f.h:123
KyFloat32 & operator[](KyInt32 i)
Retrieves the coordinates of the vector. Use [0] for the X axis, or [1] for the Y axis...
Definition: vec2f.h:61
KyFloat32 operator^(const Vec2f &v) const
Returns the magnitude on the Z axis of the cross product between this vector and v.
Definition: vec2f.h:113
KyFloat32 Normalize()
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec2f.h:267
Vec2f GetDir2d(const Vec2f &v1, const Vec2f &v2)
Returns the normalized direction between v1 and v2.
Definition: vec2f.h:208
Vec2f & operator+=(const Vec2f &v)
Adds the X coordinate of v to the X coordinate of this vector, and adds the Y coordinate of v to the ...
Definition: vec2f.h:86
KyFloat32 CrossProduct(const Vec2f &v1, const Vec2f &v2)
Returns the magnitude on the Z axis of the cross product between this vector and v.
Definition: vec2f.h:190
static Vec2f UnitX()
Returns the normalized orientation of the X axis.
Definition: vec2f.h:157
KyFloat32 x
The size of the vector along the X axis.
Definition: vec2f.h:169
Vec2f operator*(KyFloat32 s) const
Multiplies both the X and Y coordinates of the vector by the specified value.
Definition: vec2f.h:93
Vec2f & operator*=(KyFloat32 s)
Multiplies both the X and Y coordinates by s.
Definition: vec2f.h:79
void Set(KyFloat32 _x, KyFloat32 _y)
Sets the coordinates of the vector.
Definition: vec2f.h:47
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
KyFloat32 GetNormalized(Vec2f &normalized) const
Normalizes the vector, making it one unit in length without changing its orientation.
Definition: vec2f.h:276
Vec2f operator-() const
Negates the X and Y coordinates of this vector, effectively flipping it around the origin...
Definition: vec2f.h:107
Vec2f operator+(const Vec2f &v) const
Adds the X coordinate of v to the X coordinate of this vector, and adds the Y coordinate of v to the ...
Definition: vec2f.h:100
KyFloat32 SquareDistance(const Vec2f &v1, const Vec2f &v2)
Returns the square of the distance between v1 and v2.
Definition: vec2f.h:194
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:35
Vec2f PerpCCW() const
Rotates this vector 90 degrees counter-clockwise (negating the Y coordinate).
Definition: vec2f.h:144
This class defines a two-dimensional vector whose coordinates are stored using floating-point numbers...
Definition: vec2f.h:24
KyFloat32 GetAngleRad(const Vec2f &v1, const Vec2f &v2)
Returns the angle in radians between v1 and v2 (CounterClockwise).
Definition: vec2f.h:218
bool operator!=(const Vec2f &v) const
Returns true if this object contains at least one different coordinate from v.
Definition: vec2f.h:70
KyFloat32 Distance(const Vec2f &v1, const Vec2f &v2)
Returns the distance between v1 and v2.
Definition: vec2f.h:202
Definition: gamekitcrowddispersion.h:20
void Rotate(Vec2f &direction, KyFloat32 cosAngle, KyFloat32 sinAngle)
Turns direction accordingly to provided sine & cosine. Positive angles rotate CCW.
Definition: vec2f.h:239
KyFloat32 DotProduct(const Vec2f &v1, const Vec2f &v2)
Returns the dot product of v1 and v2.
Definition: vec2f.h:187
Vec2f operator*(KyFloat32 s, const Vec2f &v)
Multiplies the X and Y coordinates of v by s.
Definition: vec2f.h:184
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
Vec2f & operator-=(const Vec2f &v)
Subtracts the X coordinate of v from the X coordinate of this vector, and subtracts the Y coordinate ...
Definition: vec2f.h:90
Vec2f operator/(KyFloat32 d) const
Divides both the X and Y coordinates of the vector by the specified value.
Definition: vec2f.h:96
static Vec2f Zero()
Returns a vector of zero size: (0,0).
Definition: vec2f.h:154
KyFloat32 GetLength() const
Returns the magnitude of the vector.
Definition: vec2f.h:126
Vec2f PerpCW() const
Rotates this vector 90 degrees clockwise (negating the X coordinate).
Definition: vec2f.h:141
static const KyFloat32 KY_2_PI
Stores the value of twice pi.
Definition: types.h:373
bool operator==(const Vec2f &v) const
Returns true if this object contains the same coordinates as v.
Definition: vec2f.h:67
static Vec2f UnitY()
Returns the normalized orientation of the Y axis.
Definition: vec2f.h:160
Vec2f & operator/=(KyFloat32 d)
Divides both the X and Y coordinates ny d .
Definition: vec2f.h:82
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43
KyFloat32 y
The size of the vector along the Y axis.
Definition: vec2f.h:170