Pyrogenesis  trunk
FixedVector2D.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 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  /// Scalar division by an integer. Must not have n == 0.
84  {
85  return CFixedVector2D(X/n, Y/n);
86  }
87 
88  /**
89  * Multiply by a CFixed. Likely to overflow if both numbers are large,
90  * so we use an ugly name instead of operator* to make it obvious.
91  */
93  {
94  return CFixedVector2D(X.Multiply(n), Y.Multiply(n));
95  }
96 
97  /**
98  * Returns the length of the vector.
99  * Will not overflow if the result can be represented as type 'fixed'.
100  */
101  fixed Length() const
102  {
103  // Do intermediate calculations with 64-bit ints to avoid overflows
104  i32 x = X.GetInternalValue();
105  i32 y = Y.GetInternalValue();
106  u64 xx = (u64)FIXED_MUL_I64_I32_I32(x, x);
107  u64 yy = (u64)FIXED_MUL_I64_I32_I32(y, y);
108  u64 d2 = xx + yy;
109  CheckUnsignedAdditionOverflow(d2, xx, L"Overflow in CFixedVector2D::Length() part 1")
110 
111  u32 d = isqrt64(d2);
112 
113  CheckU32CastOverflow(d, i32, L"Overflow in CFixedVector2D::Length() part 2")
114  fixed r;
115  r.SetInternalValue((i32)d);
116  return r;
117  }
118 
119  /**
120  * Returns -1, 0, +1 depending on whether length is less/equal/greater
121  * than the argument.
122  * Avoids sqrting and overflowing.
123  */
124  int CompareLength(fixed cmp) const
125  {
126  i32 x = X.GetInternalValue(); // abs(x) <= 2^31
127  i32 y = Y.GetInternalValue();
128  u64 xx = (u64)FIXED_MUL_I64_I32_I32(x, x); // xx <= 2^62
129  u64 yy = (u64)FIXED_MUL_I64_I32_I32(y, y);
130  u64 d2 = xx + yy; // d2 <= 2^63 (no overflow)
131 
132  i32 c = cmp.GetInternalValue();
133  u64 c2 = (u64)FIXED_MUL_I64_I32_I32(c, c);
134  if (d2 < c2)
135  return -1;
136  else if (d2 > c2)
137  return +1;
138  else
139  return 0;
140  }
141 
142  /**
143  * Returns -1, 0, +1 depending on whether length is less/equal/greater
144  * than the argument's length.
145  * Avoids sqrting and overflowing.
146  */
147  int CompareLength(const CFixedVector2D& other) const
148  {
149  i32 x = X.GetInternalValue();
150  i32 y = Y.GetInternalValue();
152 
153  i32 ox = other.X.GetInternalValue();
154  i32 oy = other.Y.GetInternalValue();
155  u64 od2 = (u64)FIXED_MUL_I64_I32_I32(ox, ox) + (u64)FIXED_MUL_I64_I32_I32(oy, oy);
156 
157  if (d2 < od2)
158  return -1;
159  else if (d2 > od2)
160  return +1;
161  else
162  return 0;
163  }
164 
165  bool IsZero() const
166  {
167  return (X.IsZero() && Y.IsZero());
168  }
169 
170  /**
171  * Normalize the vector so that length is close to 1.
172  * If length is 0, does nothing.
173  */
174  void Normalize()
175  {
176  if (!IsZero())
177  {
178  fixed l = Length();
179  X = X / l;
180  Y = Y / l;
181  }
182  }
183 
184  /**
185  * Normalize the vector so that length is close to n.
186  * If length is 0, does nothing.
187  */
188  void Normalize(fixed n)
189  {
190  fixed l = Length();
191  if (!l.IsZero())
192  {
193  X = X.MulDiv(n, l);
194  Y = Y.MulDiv(n, l);
195  }
196  }
197 
198  /**
199  * Compute the dot product of this vector with another.
200  */
201  fixed Dot(const CFixedVector2D& v) const
202  {
205  CheckSignedAdditionOverflow(i64, x, y, L"Overflow in CFixedVector2D::Dot() part 1", L"Underflow in CFixedVector2D::Dot() part 1")
206  i64 sum = x + y;
207  sum >>= fixed::fract_bits;
208 
209  CheckCastOverflow(sum, i32, L"Overflow in CFixedVector2D::Dot() part 2", L"Underflow in CFixedVector2D::Dot() part 2")
210  fixed ret;
211  ret.SetInternalValue((i32)sum);
212  return ret;
213  }
214 
216  {
217  return CFixedVector2D(Y, -X);
218  }
219 
220  /**
221  * Rotate the vector by the given angle (anticlockwise).
222  */
224  {
225  fixed s, c;
226  sincos_approx(angle, s, c);
227  return CFixedVector2D(X.Multiply(c) + Y.Multiply(s), Y.Multiply(c) - X.Multiply(s));
228  }
229 };
230 
231 #endif // INCLUDED_FIXED_VECTOR2D
int CompareLength(fixed cmp) const
Returns -1, 0, +1 depending on whether length is less/equal/greater than the argument.
Definition: FixedVector2D.h:124
A simple fixed-point number class.
Definition: Fixed.h:115
fixed Dot(const CFixedVector2D &v) const
Compute the dot product of this vector with another.
Definition: FixedVector2D.h:201
int64_t i64
Definition: types.h:35
Definition: FixedVector2D.h:24
CFixedVector2D operator/(int n) const
Scalar division by an integer. Must not have n == 0.
Definition: FixedVector2D.h:83
Definition: Fixed.h:123
#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.
Definition: FixedVector2D.h:174
bool IsZero() const
Definition: FixedVector2D.h:165
void Normalize(fixed n)
Normalize the vector so that length is close to n.
Definition: FixedVector2D.h:188
CFixedVector2D operator-() const
Negation.
Definition: FixedVector2D.h:57
T GetInternalValue() const
Definition: Fixed.h:131
CFixedVector2D(fixed X, fixed Y)
Definition: FixedVector2D.h:30
CFixedVector2D operator-(const CFixedVector2D &v) const
Vector subtraction.
Definition: FixedVector2D.h:51
uint64_t u64
Definition: types.h:40
void SetInternalValue(T n)
Definition: Fixed.h:132
bool operator!=(const CFixedVector2D &v) const
Vector inequality.
Definition: FixedVector2D.h:39
fixed Y
Definition: FixedVector2D.h:27
uint32_t u32
Definition: types.h:39
CFixedVector2D operator+(const CFixedVector2D &v) const
Vector addition.
Definition: FixedVector2D.h:45
CFixed Multiply(CFixed n) const
Multiply by a CFixed.
Definition: Fixed.h:311
CFixedVector2D Perpendicular() const
Definition: FixedVector2D.h:215
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:331
#define CheckU32CastOverflow(var, targetType, overflowWarning)
Definition: Fixed.h:72
int32_t i32
Definition: types.h:34
CFixedVector2D()
Definition: FixedVector2D.h:29
CFixedVector2D Multiply(fixed n) const
Multiply by a CFixed.
Definition: FixedVector2D.h:92
fixed Length() const
Returns the length of the vector.
Definition: FixedVector2D.h:101
bool operator==(const CFixedVector2D &v) const
Vector equality.
Definition: FixedVector2D.h:33
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
fixed X
Definition: FixedVector2D.h:27
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...
Definition: FixedVector2D.h:147
CFixedVector2D Rotate(fixed angle) const
Rotate the vector by the given angle (anticlockwise).
Definition: FixedVector2D.h:223
#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning)
Definition: Fixed.h:66