Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FixedVector2D.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_FIXED_VECTOR2D
19 #define INCLUDED_FIXED_VECTOR2D
20 
21 #include "maths/Fixed.h"
22 #include "maths/Sqrt.h"
23 
25 {
26 public:
27  fixed X, Y;
28 
30  CFixedVector2D(fixed X, fixed Y) : X(X), Y(Y) { }
31 
32  /// Vector equality
33  bool operator==(const CFixedVector2D& v) const
34  {
35  return (X == v.X && Y == v.Y);
36  }
37 
38  /// Vector inequality
39  bool operator!=(const CFixedVector2D& v) const
40  {
41  return (X != v.X || Y != v.Y);
42  }
43 
44  /// Vector addition
46  {
47  return CFixedVector2D(X + v.X, Y + v.Y);
48  }
49 
50  /// Vector subtraction
52  {
53  return CFixedVector2D(X - v.X, Y - v.Y);
54  }
55 
56  /// Negation
58  {
59  return CFixedVector2D(-X, -Y);
60  }
61 
62  /// Vector addition
64  {
65  *this = *this + v;
66  return *this;
67  }
68 
69  /// Vector subtraction
71  {
72  *this = *this - v;
73  return *this;
74  }
75 
76  /// Scalar multiplication by an integer
78  {
79  return CFixedVector2D(X*n, Y*n);
80  }
81 
82  /**
83  * Multiply by a CFixed. Likely to overflow if both numbers are large,
84  * so we use an ugly name instead of operator* to make it obvious.
85  */
87  {
88  return CFixedVector2D(X.Multiply(n), Y.Multiply(n));
89  }
90 
91  /**
92  * Returns the length of the vector.
93  * Will not overflow if the result can be represented as type 'fixed'.
94  */
95  fixed Length() const
96  {
97  // Do intermediate calculations with 64-bit ints to avoid overflows
98  i32 x = X.GetInternalValue();
99  i32 y = Y.GetInternalValue();
100  u64 xx = (u64)FIXED_MUL_I64_I32_I32(x, x);
101  u64 yy = (u64)FIXED_MUL_I64_I32_I32(y, y);
102  u64 d2 = xx + yy;
103  CheckUnsignedAdditionOverflow(d2, xx, L"Overflow in CFixedVector2D::Length() part 1")
104 
105  u32 d = isqrt64(d2);
106 
107  CheckU32CastOverflow(d, i32, L"Overflow in CFixedVector2D::Length() part 2")
108  fixed r;
109  r.SetInternalValue((i32)d);
110  return r;
111  }
112 
113  /**
114  * Returns -1, 0, +1 depending on whether length is less/equal/greater
115  * than the argument.
116  * Avoids sqrting and overflowing.
117  */
118  int CompareLength(fixed cmp) const
119  {
120  i32 x = X.GetInternalValue(); // abs(x) <= 2^31
121  i32 y = Y.GetInternalValue();
122  u64 xx = (u64)FIXED_MUL_I64_I32_I32(x, x); // xx <= 2^62
123  u64 yy = (u64)FIXED_MUL_I64_I32_I32(y, y);
124  u64 d2 = xx + yy; // d2 <= 2^63 (no overflow)
125 
126  i32 c = cmp.GetInternalValue();
127  u64 c2 = (u64)FIXED_MUL_I64_I32_I32(c, c);
128  if (d2 < c2)
129  return -1;
130  else if (d2 > c2)
131  return +1;
132  else
133  return 0;
134  }
135 
136  /**
137  * Returns -1, 0, +1 depending on whether length is less/equal/greater
138  * than the argument's length.
139  * Avoids sqrting and overflowing.
140  */
141  int CompareLength(const CFixedVector2D& other) const
142  {
143  i32 x = X.GetInternalValue();
144  i32 y = Y.GetInternalValue();
146 
147  i32 ox = other.X.GetInternalValue();
148  i32 oy = other.Y.GetInternalValue();
149  u64 od2 = (u64)FIXED_MUL_I64_I32_I32(ox, ox) + (u64)FIXED_MUL_I64_I32_I32(oy, oy);
150 
151  if (d2 < od2)
152  return -1;
153  else if (d2 > od2)
154  return +1;
155  else
156  return 0;
157  }
158 
159  bool IsZero() const
160  {
161  return (X.IsZero() && Y.IsZero());
162  }
163 
164  /**
165  * Normalize the vector so that length is close to 1.
166  * If length is 0, does nothing.
167  */
168  void Normalize()
169  {
170  if (!IsZero())
171  {
172  fixed l = Length();
173  X = X / l;
174  Y = Y / l;
175  }
176  }
177 
178  /**
179  * Normalize the vector so that length is close to n.
180  * If length is 0, does nothing.
181  */
182  void Normalize(fixed n)
183  {
184  fixed l = Length();
185  if (!l.IsZero())
186  {
187  X = X.MulDiv(n, l);
188  Y = Y.MulDiv(n, l);
189  }
190  }
191 
192  /**
193  * Compute the dot product of this vector with another.
194  */
196  {
199  CheckSignedAdditionOverflow(i64, x, y, L"Overflow in CFixedVector2D::Dot() part 1", L"Underflow in CFixedVector2D::Dot() part 1")
200  i64 sum = x + y;
201  sum >>= fixed::fract_bits;
202 
203  CheckCastOverflow(sum, i32, L"Overflow in CFixedVector2D::Dot() part 2", L"Underflow in CFixedVector2D::Dot() part 2")
204  fixed ret;
205  ret.SetInternalValue((i32)sum);
206  return ret;
207  }
208 
210  {
211  return CFixedVector2D(Y, -X);
212  }
213 
214  /**
215  * Rotate the vector by the given angle (anticlockwise).
216  */
218  {
219  fixed s, c;
220  sincos_approx(angle, s, c);
221  return CFixedVector2D(X.Multiply(c) + Y.Multiply(s), Y.Multiply(c) - X.Multiply(s));
222  }
223 };
224 
225 #endif // INCLUDED_FIXED_VECTOR2D
int CompareLength(fixed cmp) const
Returns -1, 0, +1 depending on whether length is less/equal/greater than the argument.
A simple fixed-point number class.
Definition: Fixed.h:115
#define FIXED_MUL_I64_I32_I32(a, b)
Definition: Fixed.h:36
CFixedVector2D operator*(int n) const
Scalar multiplication by an integer.
Definition: FixedVector2D.h:77
CFixedVector2D & operator+=(const CFixedVector2D &v)
Vector addition.
Definition: FixedVector2D.h:63
void Normalize()
Normalize the vector so that length is close to 1.
bool IsZero() const
#define i32
Definition: types.h:36
void Normalize(fixed n)
Normalize the vector so that length is close to n.
CFixedVector2D operator-() const
Negation.
Definition: FixedVector2D.h:57
T GetInternalValue() const
Definition: Fixed.h:131
CFixedVector2D(fixed X, fixed Y)
Definition: FixedVector2D.h:30
fixed Dot(const CFixedVector2D &v)
Compute the dot product of this vector with another.
CFixedVector2D Rotate(fixed angle)
Rotate the vector by the given angle (anticlockwise).
CFixedVector2D operator-(const CFixedVector2D &v) const
Vector subtraction.
Definition: FixedVector2D.h:51
void SetInternalValue(T n)
Definition: Fixed.h:132
bool operator!=(const CFixedVector2D &v) const
Vector inequality.
Definition: FixedVector2D.h:39
CFixedVector2D Perpendicular()
CFixedVector2D operator+(const CFixedVector2D &v) const
Vector addition.
Definition: FixedVector2D.h:45
CFixed Multiply(CFixed n) const
Multiply by a CFixed.
Definition: Fixed.h:290
u32 isqrt64(u64 n)
64-bit integer square root.
Definition: Sqrt.cpp:23
#define CheckSignedAdditionOverflow(type, left, right, overflowWarning, underflowWarning)
Definition: Fixed.h:60
CFixed MulDiv(CFixed m, CFixed d) const
Compute this*m/d.
Definition: Fixed.h:310
#define CheckU32CastOverflow(var, targetType, overflowWarning)
Definition: Fixed.h:72
CFixedVector2D Multiply(fixed n) const
Multiply by a CFixed.
Definition: FixedVector2D.h:86
fixed Length() const
Returns the length of the vector.
Definition: FixedVector2D.h:95
#define u64
Definition: types.h:42
bool operator==(const CFixedVector2D &v) const
Vector equality.
Definition: FixedVector2D.h:33
#define i64
Definition: types.h:37
#define u32
Definition: types.h:41
bool IsZero() const
Returns true if the number is precisely 0.
Definition: Fixed.h:199
CFixedVector2D & operator-=(const CFixedVector2D &v)
Vector subtraction.
Definition: FixedVector2D.h:70
#define CheckUnsignedAdditionOverflow(result, operand, overflowWarning)
Definition: Fixed.h:76
void sincos_approx(CFixed_15_16 a, CFixed_15_16 &sin_out, CFixed_15_16 &cos_out)
Compute sin(a) and cos(a).
Definition: Fixed.cpp:187
int CompareLength(const CFixedVector2D &other) const
Returns -1, 0, +1 depending on whether length is less/equal/greater than the argument&#39;s length...
#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning)
Definition: Fixed.h:66