TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vector2.h
Go to the documentation of this file.
1 
15 #ifndef G3D_Vector2_h
16 #define G3D_Vector2_h
17 
18 #include <string>
19 
20 #include "G3D/platform.h"
21 #include "G3D/g3dmath.h"
22 #include "G3D/Table.h"
23 #include "G3D/HashTrait.h"
24 #include "G3D/Vector2int16.h"
25 #include "G3D/Vector2unorm16.h"
26 #include "G3D/Random.h"
27 
28 namespace G3D {
29 
30 class Vector2;
31 class Vector3;
32 class Vector4;
33 class Vector2int32;
34 class Any;
35 
40 class Vector2 {
41 private:
42  // Hidden operators
43  bool operator<(const Vector2&) const;
44  bool operator>(const Vector2&) const;
45  bool operator<=(const Vector2&) const;
46  bool operator>=(const Vector2&) const;
47 
48 public:
49  float x;
50  float y;
51 
53  Vector2(const Any& any);
54 
56  Any toAny() const;
57 
59  Vector2();
60  Vector2(class TextInput& t);
61  Vector2(class BinaryInput& b);
62  Vector2(float x, float y);
63  Vector2(float coordinate[2]);
64  Vector2(double coordinate[2]);
65  Vector2(const Vector2& other);
66  Vector2(const Vector2int16& other);
67  Vector2(const Vector2unorm16& other);
68 
69  // explicit because of precision loss
70  explicit Vector2(const Vector2int32& other);
71 
72  Vector2& operator=(const Any& a);
73 
74  void serialize(class BinaryOutput& b) const;
75  void deserialize(class BinaryInput& b);
76 
77  void serialize(class TextOutput& t) const;
78  void deserialize(class TextInput& t);
79 
80  float& operator[](int i);
81  const float& operator[](int i) const;
82 
83  // assignment and comparison
84  Vector2& operator=(const Vector2& other);
85  bool operator==(const Vector2& other) const;
86  bool operator!=(const Vector2& other) const;
87  size_t hashCode() const;
88  bool fuzzyEq(const Vector2& other) const;
89  bool fuzzyNe(const Vector2& other) const;
90 
92  bool isFinite() const;
93 
95  bool isNaN() const {
96  return G3D::isNaN(x) || G3D::isNaN(y);
97  }
98 
100  bool isZero() const;
101 
103  bool isUnit() const;
104 
105  // arithmetic operations
106  Vector2 operator+(const Vector2& v) const;
107  Vector2 operator-(const Vector2& v) const;
108  Vector2 operator*(float s) const;
109 
111  Vector2 pow(float p) const {
112  return Vector2(powf(x, p), powf(y, p));
113  }
114 
116  Vector2 operator*(const Vector2& v) const;
117 
119  Vector2 operator/(const Vector2& v) const;
120  Vector2 operator/(float s) const;
121 
123  Vector2 operator-() const;
124 
126  inline float sum() const {
127  return x + y;
128  }
129 
133  inline Vector2 lerp(const Vector2& v, float alpha) const {
134  return (*this) + (v - *this) * alpha;
135  }
136 
137  inline Vector2 clamp(const Vector2& low, const Vector2& high) const {
138  return Vector2(
139  G3D::clamp(x, low.x, high.x),
140  G3D::clamp(y, low.y, high.y));
141  }
142 
143  inline Vector2 clamp(float low, float high) const {
144  return Vector2(
145  (float)G3D::clamp(x, low, high),
146  (float)G3D::clamp(y, low, high));
147  }
148 
149  // arithmetic updates
150  Vector2& operator+=(const Vector2&);
151  Vector2& operator-=(const Vector2&);
152  Vector2& operator*=(float);
153  Vector2& operator/=(float);
154  Vector2& operator*=(const Vector2&);
155  Vector2& operator/=(const Vector2&);
156 
157  // vector operations
158 
160  float length() const;
161 
166  Vector2 direction() const;
167 
170  Vector2 directionOrZero() const;
171 
177  return direction();
178  }
179 
180  float squaredLength() const;
181  float dot(const Vector2& s) const;
182 
184  Vector2 abs() const {
185  return Vector2(fabs(x), fabs(y));
186  }
187 
189  Vector2 min(const Vector2& v) const;
190 
192  Vector2 max(const Vector2& v) const;
193 
199  Vector2 maxAbs(const Vector2& v) const {
200  return Vector2(::fabsf(x) > ::fabsf(v.x) ? x : v.x, ::fabsf(y) > ::fabsf(v.y) ? y : v.y);
201  }
202 
208  Vector2 minAbs(const Vector2& v) const {
209  return Vector2(::fabsf(x) < ::fabsf(v.x) ? x : v.x, ::fabsf(y) < ::fabsf(v.y) ? y : v.y);
210  }
211 
213  static Vector2 random(Random& r = Random::common());
214 
215  // Special values.
216  // Intentionally not inlined: see Matrix3::identity() for details.
217  static const Vector2& zero();
218  static const Vector2& one();
219  static const Vector2& unitX();
220  static const Vector2& unitY();
221  static const Vector2& inf();
222  static const Vector2& nan();
224  static const Vector2& minFinite();
226  static const Vector2& maxFinite();
227 
228  std::string toString() const;
229 
230  // 2-char swizzles
231 
232  Vector2 xx() const;
233  Vector2 yx() const;
234  Vector2 xy() const;
235  Vector2 yy() const;
236 
237  // 3-char swizzles
238 
239  Vector3 xxx() const;
240  Vector3 yxx() const;
241  Vector3 xyx() const;
242  Vector3 yyx() const;
243  Vector3 xxy() const;
244  Vector3 yxy() const;
245  Vector3 xyy() const;
246  Vector3 yyy() const;
247 
248  // 4-char swizzles
249 
250  Vector4 xxxx() const;
251  Vector4 yxxx() const;
252  Vector4 xyxx() const;
253  Vector4 yyxx() const;
254  Vector4 xxyx() const;
255  Vector4 yxyx() const;
256  Vector4 xyyx() const;
257  Vector4 yyyx() const;
258  Vector4 xxxy() const;
259  Vector4 yxxy() const;
260  Vector4 xyxy() const;
261  Vector4 yyxy() const;
262  Vector4 xxyy() const;
263  Vector4 yxyy() const;
264  Vector4 xyyy() const;
265  Vector4 yyyy() const;
266 
267 };
268 
269 inline Vector2 operator*(double s, const Vector2& v) {
270  return v * (float)s;
271 }
272 
273 inline Vector2 operator*(float s, const Vector2& v) {
274  return v * s;
275 }
276 
277 inline Vector2 operator*(int s, const Vector2& v) {
278  return v * (float)s;
279 }
280 
281 
282 inline Vector2::Vector2 () : x(0.0f), y(0.0f) {
283 }
284 
285 
286 inline Vector2::Vector2(float _x, float _y) : x(_x), y(_y) {
287 }
288 
289 
290 inline Vector2::Vector2 (float afCoordinate[2]) {
291  x = afCoordinate[0];
292  y = afCoordinate[1];
293 }
294 
295 
296 
297 inline Vector2::Vector2 (double afCoordinate[2]) {
298  x = (float)afCoordinate[0];
299  y = (float)afCoordinate[1];
300 }
301 
302 
303 inline Vector2::Vector2 (const Vector2& rkVector) {
304  x = rkVector.x;
305  y = rkVector.y;
306 }
307 
308 
309 inline Vector2::Vector2 (const Vector2int16& v) : x(v.x), y(v.y) {
310 }
311 inline Vector2::Vector2 (const Vector2unorm16& v) : x(float(v.x)), y(float(v.y)) {
312 }
313 
314 inline float& Vector2::operator[] (int i) {
315  return ((float*)this)[i];
316 }
317 
318 
319 inline const float& Vector2::operator[] (int i) const {
320  return ((float*)this)[i];
321 }
322 
323 
324 inline Vector2& Vector2::operator= (const Vector2& rkVector) {
325  x = rkVector.x;
326  y = rkVector.y;
327  return *this;
328 }
329 
330 
331 inline bool Vector2::operator== (const Vector2& rkVector) const {
332  return ( x == rkVector.x && y == rkVector.y);
333 }
334 
335 
336 inline bool Vector2::operator!= (const Vector2& rkVector) const {
337  return ( x != rkVector.x || y != rkVector.y);
338 }
339 
340 
341 inline Vector2 Vector2::operator+ (const Vector2& rkVector) const {
342  return Vector2(x + rkVector.x, y + rkVector.y);
343 }
344 
345 
346 inline Vector2 Vector2::operator- (const Vector2& rkVector) const {
347  return Vector2(x - rkVector.x, y - rkVector.y);
348 }
349 
350 
351 inline Vector2 Vector2::operator* (float fScalar) const {
352  return Vector2(fScalar*x, fScalar*y);
353 }
354 
355 
356 
357 inline Vector2 Vector2::operator- () const {
358  return Vector2( -x, -y);
359 }
360 
361 
362 
363 inline Vector2& Vector2::operator+= (const Vector2& rkVector) {
364  x += rkVector.x;
365  y += rkVector.y;
366  return *this;
367 }
368 
369 
370 
371 inline Vector2& Vector2::operator-= (const Vector2& rkVector) {
372  x -= rkVector.x;
373  y -= rkVector.y;
374  return *this;
375 }
376 
377 
378 
379 inline Vector2& Vector2::operator*= (float fScalar) {
380  x *= fScalar;
381  y *= fScalar;
382  return *this;
383 }
384 
385 
386 
387 
388 inline Vector2& Vector2::operator*= (const Vector2& rkVector) {
389  x *= rkVector.x;
390  y *= rkVector.y;
391  return *this;
392 }
393 
394 
395 
396 inline Vector2& Vector2::operator/= (const Vector2& rkVector) {
397  x /= rkVector.x;
398  y /= rkVector.y;
399  return *this;
400 }
401 
402 
403 inline Vector2 Vector2::operator* (const Vector2& rkVector) const {
404  return Vector2(x * rkVector.x, y * rkVector.y);
405 }
406 
407 
408 
409 inline Vector2 Vector2::operator/ (const Vector2& rkVector) const {
410  return Vector2(x / rkVector.x, y / rkVector.y);
411 }
412 
413 
414 inline float Vector2::squaredLength () const {
415  return x*x + y*y;
416 }
417 
418 
419 inline float Vector2::length () const {
420  return sqrtf(x*x + y*y);
421 }
422 
423 
424 inline Vector2 Vector2::direction () const {
425  float lenSquared = x * x + y * y;
426 
427  if (lenSquared != 1.0f) {
428  return *this / sqrtf(lenSquared);
429  } else {
430  return *this;
431  }
432 }
433 
435  float mag = length();
436  if (mag < 0.0000001f) {
437  return Vector2::zero();
438  } else if (mag < 1.00001f && mag > 0.99999f) {
439  return *this;
440  } else {
441  return *this * (1.0f / mag);
442  }
443 }
444 
445 
446 inline float Vector2::dot (const Vector2& rkVector) const {
447  return x*rkVector.x + y*rkVector.y;
448 }
449 
450 
451 
452 inline Vector2 Vector2::min(const Vector2 &v) const {
453  return Vector2(G3D::min(v.x, x), G3D::min(v.y, y));
454 }
455 
456 
457 
458 inline Vector2 Vector2::max(const Vector2 &v) const {
459  return Vector2(G3D::max(v.x, x), G3D::max(v.y, y));
460 }
461 
462 
463 
464 inline bool Vector2::fuzzyEq(const Vector2& other) const {
465  return G3D::fuzzyEq((*this - other).squaredLength(), 0);
466 }
467 
468 
469 
470 inline bool Vector2::fuzzyNe(const Vector2& other) const {
471  return G3D::fuzzyNe((*this - other).squaredLength(), 0);
472 }
473 
474 
475 
476 inline bool Vector2::isFinite() const {
477  return G3D::isFinite(x) && G3D::isFinite(y);
478 }
479 
480 
481 
482 inline bool Vector2::isZero() const {
483  return G3D::fuzzyEq(fabsf(x) + fabsf(y), 0.0f);
484 }
485 
486 
487 
488 inline bool Vector2::isUnit() const {
489  return G3D::fuzzyEq(squaredLength(), 1.0f);
490 }
491 
492 typedef Vector2 Point2;
493 void serialize(const Vector2& v, class BinaryOutput& b);
494 void deserialize(Vector2& v, class BinaryInput& b);
495 
496 } // namespace G3D
497 
498 template <>
499 struct HashTrait<G3D::Vector2> {
500  static size_t hashCode(const G3D::Vector2& key) {
501  return key.hashCode();
502  }
503 };
504 
505 
506 // Intentionally outside namespace to avoid operator overloading confusion
507 inline G3D::Vector2 operator*(double s, const G3D::Vector2& v) {
508  return v * (float)s;
509 }
510 inline G3D::Vector2 operator*(int s, const G3D::Vector2& v) {
511  return v * (float)s;
512 }
513 
514 #endif
Vector3 yxx() const
Definition: Vector2.cpp:189
Vector4 yxyx() const
Definition: Vector2.cpp:204
Definition: Vector2.h:40
Vector2 clamp(float low, float high) const
Definition: Vector2.h:143
Vector2 yx() const
Definition: Vector2.cpp:182
Vector2 operator*(float s) const
Definition: Vector2.h:351
Vector4 xxxy() const
Definition: Vector2.cpp:207
Vector2 abs() const
Definition: Vector2.h:184
Vector4 yxxy() const
Definition: Vector2.cpp:208
Vector3 xyy() const
Definition: Vector2.cpp:194
Vector2 xy() const
Definition: Vector2.cpp:183
bool isFinite() const
Definition: Vector2.h:476
Definition: BinaryInput.h:69
bool operator!=(const Vector2 &other) const
Definition: Vector2.h:336
Vector2 & operator+=(const Vector2 &)
Definition: Vector2.h:363
Vector4 xyyy() const
Definition: Vector2.cpp:213
Vector4 yyxy() const
Definition: Vector2.cpp:210
Vector4 xxyx() const
Definition: Vector2.cpp:203
Vector2 clamp(const Vector2 &low, const Vector2 &high) const
Definition: Vector2.h:137
Vector3 yyy() const
Definition: Vector2.cpp:195
Vector2 min(const Vector2 &v) const
Definition: Vector2.h:452
float length() const
Definition: Vector2.h:419
bool operator==(const Vector2 &other) const
Definition: Vector2.h:331
void deserialize(class BinaryInput &b)
Definition: Vector2.cpp:119
Definition: HashTrait.h:105
bool operator<=(const Vector2 &) const
Vector4 xxxx() const
Definition: Vector2.cpp:199
Definition: AABox.h:25
bool any(float x)
Definition: g3dmath.h:424
bool isNaN(double x)
Definition: g3dmath.cpp:56
Vector2 yy() const
Definition: Vector2.cpp:184
Vector3 yyx() const
Definition: Vector2.cpp:191
Vector3 yxy() const
Definition: Vector2.cpp:193
bool isZero() const
Definition: Vector2.h:482
void deserialize(std::string &s, BinaryInput &b)
Definition: serialize.h:16
Vector2 lerp(const Vector2 &v, float alpha) const
Definition: Vector2.h:133
Vector4 xyxy() const
Definition: Vector2.cpp:209
bool fuzzyNe(double a, double b)
Definition: g3dmath.h:861
bool fuzzyEq(const Vector2 &other) const
Definition: Vector2.h:464
double clamp(double val, double low, double hi)
Definition: g3dmath.h:571
bool isUnit() const
Definition: Vector2.h:488
Vector4 xxyy() const
Definition: Vector2.cpp:211
static size_t hashCode(const G3D::Vector2 &key)
Definition: Vector2.h:500
Vector3 xxx() const
Definition: Vector2.cpp:188
Vector2 operator-() const
Definition: Vector2.h:357
bool operator<(const Vector2 &) const
Vector2 & operator=(const Any &a)
Definition: Vector2.cpp:49
T max(const T &x, const T &y)
Definition: g3dmath.h:320
Definition: Vector3.h:58
Vector4 yyxx() const
Definition: Vector2.cpp:202
Vector2 Point2
Definition: Vector2.h:492
Vector3 xxy() const
Definition: Vector2.cpp:192
Vector2 directionOrZero() const
Definition: Vector2.h:434
float y
Definition: Vector2.h:50
static const Vector2 & one()
Definition: Vector2.cpp:62
static Vector2 random(Random &r=Random::common())
Definition: Vector2.cpp:150
T min(const T &x, const T &y)
Definition: g3dmath.h:305
Easy loading and saving of human-readable configuration files.
Definition: Any.h:184
Vector2 & operator-=(const Vector2 &)
Definition: Vector2.h:371
void serialize(const std::string &s, BinaryOutput &b)
Definition: serialize.h:12
Vector2()
Definition: Vector2.h:282
float & operator[](int i)
Definition: Vector2.h:314
Vector4 xyyx() const
Definition: Vector2.cpp:205
bool operator>(const Vector2 &) const
Vector2 minAbs(const Vector2 &v) const
Definition: Vector2.h:208
Definition: Vector2unorm16.h:23
Any toAny() const
Definition: Vector2.cpp:55
A simple tokenizer for parsing text files.
Definition: TextInput.h:223
static const Vector2 & unitX()
Definition: Vector2.cpp:72
Vector2 operator+(const Vector2 &v) const
Definition: Vector2.h:341
Vector2 & operator/=(float)
Definition: Vector2.cpp:166
Definition: Vector4.h:39
float x
Definition: Vector2.h:49
Vector2 pow(float p) const
Definition: Vector2.h:111
float squaredLength() const
Definition: Vector2.h:414
Vector2 fastDirection() const
Definition: Vector2.h:176
void serialize(class BinaryOutput &b) const
Definition: Vector2.cpp:125
Definition: TextOutput.h:60
Vector2 direction() const
Definition: Vector2.h:424
float sum() const
Definition: Vector2.h:126
static const Vector2 & minFinite()
Definition: Vector2.cpp:94
Definition: BinaryOutput.h:52
static const Vector2 & unitY()
Definition: Vector2.cpp:77
bool operator>=(const Vector2 &) const
static const Vector2 & nan()
Definition: Vector2.cpp:88
Vector2 maxAbs(const Vector2 &v) const
Definition: Vector2.h:199
Vector4 xyxx() const
Definition: Vector2.cpp:201
bool fuzzyNe(const Vector2 &other) const
Definition: Vector2.h:470
bool isNaN() const
Definition: Vector2.h:95
Vector2 & operator*=(float)
Definition: Vector2.h:379
Vector2 operator/(const Vector2 &v) const
Definition: Vector2.h:409
static const Vector2 & zero()
Definition: Vector2.cpp:67
static const Vector2 & maxFinite()
Definition: Vector2.cpp:100
G3D::Vector2 operator*(double s, const G3D::Vector2 &v)
Definition: Vector2.h:507
Vector2 max(const Vector2 &v) const
Definition: Vector2.h:458
std::string toString() const
Definition: Vector2.cpp:175
Vector4 yxyy() const
Definition: Vector2.cpp:212
Vector4 yyyx() const
Definition: Vector2.cpp:206
Vector3 xyx() const
Definition: Vector2.cpp:190
size_t hashCode() const
Definition: Vector2.cpp:106
Definition: Vector2int16.h:22
static Random & common()
Definition: Random.cpp:16
bool fuzzyEq(double a, double b)
Definition: g3dmath.h:857
G3D::Color3 operator*(float s, const G3D::Color3 &c)
Definition: Color3.h:275
float dot(const Vector2 &s) const
Definition: Vector2.h:446
Vector2 xx() const
Definition: Vector2.cpp:181
bool isFinite(double x)
Definition: g3dmath.h:525
Vector4 yyyy() const
Definition: Vector2.cpp:214
Vector4 yxxx() const
Definition: Vector2.cpp:200
Definition: Random.h:39
static const Vector2 & inf()
Definition: Vector2.cpp:82