Pyrogenesis  trunk
FixedVector3D.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_VECTOR3D
19 #define INCLUDED_FIXED_VECTOR3D
20 
21 #include "maths/Fixed.h"
22 #include "maths/Sqrt.h"
23 
25 {
26 public:
27  fixed X, Y, Z;
28 
30 
31  CFixedVector3D(fixed X, fixed Y, fixed Z) : X(X), Y(Y), Z(Z) { }
32 
33  /// Vector equality
34  bool operator==(const CFixedVector3D& v) const
35  {
36  return (X == v.X && Y == v.Y && Z == v.Z);
37  }
38 
39  /// Vector inequality
40  bool operator!=(const CFixedVector3D& v) const
41  {
42  return (X != v.X || Y != v.Y || Z != v.Z);
43  }
44 
45  /// Vector addition
47  {
48  return CFixedVector3D(X + v.X, Y + v.Y, Z + v.Z);
49  }
50 
51  /// Vector subtraction
53  {
54  return CFixedVector3D(X - v.X, Y - v.Y, Z - v.Z);
55  }
56 
57  /// Negation
59  {
60  return CFixedVector3D(-X, -Y, -Z);
61  }
62 
63  /// Vector addition
65  {
66  *this = *this + v;
67  return *this;
68  }
69 
70  /// Vector subtraction
72  {
73  *this = *this - v;
74  return *this;
75  }
76 
77 
78  /**
79  * Returns the length of the vector.
80  * Will not overflow if the result can be represented as type 'fixed'.
81  */
82  fixed Length() const
83  {
84  // Do intermediate calculations with 64-bit ints to avoid overflows
85  i32 x = X.GetInternalValue();
86  i32 y = Y.GetInternalValue();
87  i32 z = Z.GetInternalValue();
88  u64 xx = (u64)FIXED_MUL_I64_I32_I32(x, x);
89  u64 yy = (u64)FIXED_MUL_I64_I32_I32(y, y);
90  u64 zz = (u64)FIXED_MUL_I64_I32_I32(z, z);
91  u64 t = xx + yy;
92  CheckUnsignedAdditionOverflow(t, xx, L"Overflow in CFixedVector3D::Length() part 1")
93 
94  u64 d2 = t + zz;
95  CheckUnsignedAdditionOverflow(d2, t, L"Overflow in CFixedVector3D::Length() part 2")
96 
97  u32 d = isqrt64(d2);
98 
99  CheckU32CastOverflow(d, i32, L"Overflow in CFixedVector3D::Length() part 3")
100  fixed r;
101  r.SetInternalValue((i32)d);
102  return r;
103  }
104 
105  /**
106  * Normalize the vector so that length is close to 1.
107  * If length is 0, does nothing.
108  */
109  void Normalize()
110  {
111  fixed l = Length();
112  if (!l.IsZero())
113  {
114  X = X / l;
115  Y = Y / l;
116  Z = Z / l;
117  }
118  }
119 
120  /**
121  * Normalize the vector so that length is close to n.
122  * If length is 0, does nothing.
123  */
124  void Normalize(fixed n)
125  {
126  fixed l = Length();
127  if (!l.IsZero())
128  {
129  X = X.MulDiv(n, l);
130  Y = Y.MulDiv(n, l);
131  Z = Z.MulDiv(n, l);
132  }
133  }
134 
135  /**
136  * Compute the cross product of this vector with another.
137  */
139  {
142  CheckSignedSubtractionOverflow(i64, y_vz, z_vy, L"Overflow in CFixedVector3D::Cross() part 1", L"Underflow in CFixedVector3D::Cross() part 1")
143  i64 x = y_vz - z_vy;
144  x >>= fixed::fract_bits;
145 
148  CheckSignedSubtractionOverflow(i64, z_vx, x_vz, L"Overflow in CFixedVector3D::Cross() part 2", L"Underflow in CFixedVector3D::Cross() part 2")
149  i64 y = z_vx - x_vz;
150  y >>= fixed::fract_bits;
151 
154  CheckSignedSubtractionOverflow(i64, x_vy, y_vx, L"Overflow in CFixedVector3D::Cross() part 3", L"Underflow in CFixedVector3D::Cross() part 3")
155  i64 z = x_vy - y_vx;
156  z >>= fixed::fract_bits;
157 
158  CheckCastOverflow(x, i32, L"Overflow in CFixedVector3D::Cross() part 4", L"Underflow in CFixedVector3D::Cross() part 4")
159  CheckCastOverflow(y, i32, L"Overflow in CFixedVector3D::Cross() part 5", L"Underflow in CFixedVector3D::Cross() part 5")
160  CheckCastOverflow(z, i32, L"Overflow in CFixedVector3D::Cross() part 6", L"Underflow in CFixedVector3D::Cross() part 6")
161  CFixedVector3D ret;
162  ret.X.SetInternalValue((i32)x);
163  ret.Y.SetInternalValue((i32)y);
164  ret.Z.SetInternalValue((i32)z);
165  return ret;
166  }
167 
168  /**
169  * Compute the dot product of this vector with another.
170  */
172  {
176  CheckSignedAdditionOverflow(i64, x, y, L"Overflow in CFixedVector3D::Dot() part 1", L"Underflow in CFixedVector3D::Dot() part 1")
177  i64 t = x + y;
178 
179  CheckSignedAdditionOverflow(i64, t, z, L"Overflow in CFixedVector3D::Dot() part 2", L"Underflow in CFixedVector3D::Dot() part 2")
180  i64 sum = t + z;
181  sum >>= fixed::fract_bits;
182  CheckCastOverflow(sum, i32, L"Overflow in CFixedVector3D::Dot() part 3", L"Underflow in CFixedVector3D::Dot() part 3")
183 
184  fixed ret;
185  ret.SetInternalValue((i32)sum);
186  return ret;
187  }
188 };
189 
190 #endif // INCLUDED_FIXED_VECTOR3D
A simple fixed-point number class.
Definition: Fixed.h:115
int64_t i64
Definition: types.h:35
CFixedVector3D operator-(const CFixedVector3D &v) const
Vector subtraction.
Definition: FixedVector3D.h:52
Definition: Fixed.h:123
#define FIXED_MUL_I64_I32_I32(a, b)
Definition: Fixed.h:36
fixed Length() const
Returns the length of the vector.
Definition: FixedVector3D.h:82
fixed Y
Definition: FixedVector3D.h:27
void Normalize()
Normalize the vector so that length is close to 1.
Definition: FixedVector3D.h:109
T GetInternalValue() const
Definition: Fixed.h:131
CFixedVector3D & operator-=(const CFixedVector3D &v)
Vector subtraction.
Definition: FixedVector3D.h:71
#define CheckSignedSubtractionOverflow(type, left, right, overflowWarning, underflowWarning)
Definition: Fixed.h:54
bool operator!=(const CFixedVector3D &v) const
Vector inequality.
Definition: FixedVector3D.h:40
uint64_t u64
Definition: types.h:40
void SetInternalValue(T n)
Definition: Fixed.h:132
bool operator==(const CFixedVector3D &v) const
Vector equality.
Definition: FixedVector3D.h:34
CFixedVector3D operator+(const CFixedVector3D &v) const
Vector addition.
Definition: FixedVector3D.h:46
CFixedVector3D operator-() const
Negation.
Definition: FixedVector3D.h:58
uint32_t u32
Definition: types.h:39
CFixedVector3D & operator+=(const CFixedVector3D &v)
Vector addition.
Definition: FixedVector3D.h:64
CFixedVector3D Cross(const CFixedVector3D &v)
Compute the cross product of this vector with another.
Definition: FixedVector3D.h:138
CFixedVector3D(fixed X, fixed Y, fixed Z)
Definition: FixedVector3D.h:31
Definition: FixedVector3D.h:24
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
fixed Z
Definition: FixedVector3D.h:27
bool IsZero() const
Returns true if the number is precisely 0.
Definition: Fixed.h:199
fixed X
Definition: FixedVector3D.h:27
#define CheckUnsignedAdditionOverflow(result, operand, overflowWarning)
Definition: Fixed.h:76
CFixedVector3D()
Definition: FixedVector3D.h:29
void Normalize(fixed n)
Normalize the vector so that length is close to n.
Definition: FixedVector3D.h:124
fixed Dot(const CFixedVector3D &v)
Compute the dot product of this vector with another.
Definition: FixedVector3D.h:171
#define CheckCastOverflow(var, targetType, overflowWarning, underflowWarning)
Definition: Fixed.h:66