1 /* Copyright (C) 2010 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
18 #include "lib/self_test.h"
20 #include "maths/FixedVector3D.h"
22 #define TS_ASSERT_VEC_EQUALS(v, x, y, z) \
23 TS_ASSERT_EQUALS(v.X.ToDouble(), x); \
24 TS_ASSERT_EQUALS(v.Y.ToDouble(), y); \
25 TS_ASSERT_EQUALS(v.Z.ToDouble(), z);
27 #define TS_ASSERT_VEC_DELTA(v, x, y, z, delta) \
28 TS_ASSERT_DELTA(v.X.ToDouble(), x, delta); \
29 TS_ASSERT_DELTA(v.Y.ToDouble(), y, delta); \
30 TS_ASSERT_DELTA(v.Z.ToDouble(), z, delta);
32 class TestFixedVector3D
: public CxxTest::TestSuite
37 CFixedVector3D
v1 (fixed::FromInt(1), fixed::FromInt(2), fixed::FromInt(3));
38 CFixedVector3D
v2 (fixed::FromInt(10), fixed::FromInt(20), fixed::FromInt(30));
40 TS_ASSERT_VEC_EQUALS(v1
, 1, 2, 3);
41 TS_ASSERT_VEC_EQUALS(v2
, 10, 20, 30);
42 TS_ASSERT_VEC_EQUALS(v3
, 0, 0, 0);
44 TS_ASSERT_VEC_EQUALS(v3
, 11, 22, 33);
46 TS_ASSERT_VEC_EQUALS(v3
, 21, 42, 63);
48 TS_ASSERT_VEC_EQUALS(v3
, 11, 22, 33);
50 TS_ASSERT_VEC_EQUALS(v3
, -9, -18, -27);
52 TS_ASSERT_VEC_EQUALS(v3
, 9, 18, 27);
57 CFixedVector3D
v1 (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
58 TS_ASSERT_EQUALS(v1
.Length().ToDouble(), 13.0);
61 max
.SetInternalValue((i32
)0x7fffffff);
62 CFixedVector3D
v2 (max
, fixed::FromInt(0), fixed::FromInt(0));
63 TS_ASSERT_EQUALS(v2
.Length().ToDouble(), max
.ToDouble());
66 large
.SetInternalValue((i32
)((double)0x7fffffff/sqrt(3.0))+1); // largest value that shouldn't cause overflow
67 CFixedVector3D
v3 (large
, large
, large
);
68 TS_ASSERT_DELTA(v3
.Length().ToDouble(), sqrt(3.0)*large
.ToDouble(), 0.01);
73 CFixedVector3D
v0 (fixed::FromInt(0), fixed::FromInt(0), fixed::FromInt(0));
75 TS_ASSERT_VEC_EQUALS(v0
, 0.0, 0.0, 0.0);
77 CFixedVector3D
v1 (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
79 TS_ASSERT_VEC_DELTA(v1
, 3.0/13.0, 4.0/13.0, 12.0/13.0, 0.01);
82 max
.SetInternalValue((i32
)0x7fffffff);
83 CFixedVector3D
v2 (max
, fixed::FromInt(0), fixed::FromInt(0));
85 TS_ASSERT_VEC_EQUALS(v2
, 1.0, 0.0, 0.0);
88 large
.SetInternalValue((i32
)((double)0x7fffffff/sqrt(3.0))+1); // largest value that shouldn't cause overflow
89 CFixedVector3D
v3 (large
, large
, large
);
91 TS_ASSERT_VEC_DELTA(v3
, 1.0/sqrt(3.0), 1.0/sqrt(3.0), 1.0/sqrt(3.0), 0.01);
94 void test_NormalizeTo()
97 CFixedVector3D
v (fixed::FromInt(0), fixed::FromInt(0), fixed::FromInt(0));
98 v
.Normalize(fixed::FromInt(1));
99 TS_ASSERT_VEC_EQUALS(v
, 0.0, 0.0, 0.0);
103 CFixedVector3D
v (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
104 v
.Normalize(fixed::FromInt(0));
105 TS_ASSERT_VEC_EQUALS(v
, 0.0, 0.0, 0.0);
109 CFixedVector3D
v (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
110 v
.Normalize(fixed::FromInt(1));
111 TS_ASSERT_VEC_DELTA(v
, 3.0/13.0, 4.0/13.0, 12.0/13.0, 0.01);
115 CFixedVector3D
v (fixed::FromInt(3000), fixed::FromInt(4000), fixed::FromInt(12000));
116 v
.Normalize(fixed::FromInt(1));
117 TS_ASSERT_VEC_DELTA(v
, 3.0/13.0, 4.0/13.0, 12.0/13.0, 0.01);
121 CFixedVector3D
v (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
122 v
.Normalize(fixed::FromInt(100));
123 TS_ASSERT_VEC_DELTA(v
, 300.0/13.0, 400.0/13.0, 1200.0/13.0, 0.01);
127 CFixedVector3D
v (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
128 v
.Normalize(fixed::FromInt(1)/100);
129 TS_ASSERT_VEC_DELTA(v
, 3.0/1300.0, 4.0/1300.0, 12.0/1300.0, 0.0001);
133 CFixedVector3D
v (fixed::FromInt(3), fixed::FromInt(4), fixed::FromInt(12));
134 v
.Normalize(fixed::FromInt(1)/10000);
135 TS_ASSERT_VEC_DELTA(v
, 3.0/130000.0, 4.0/130000.0, 12.0/130000.0, 0.0001);
141 CFixedVector3D
v1 (fixed::FromInt(5), fixed::FromInt(6), fixed::FromInt(7));
142 CFixedVector3D
v2 (fixed::FromInt(8), fixed::FromInt(9), fixed::FromInt(-10));
143 CFixedVector3D v3
= v1
.Cross(v2
);
144 TS_ASSERT_VEC_EQUALS(v3
, 6*-10 - 7*9, 7*8 - 5*-10, 5*9 - 8*6);
149 CFixedVector3D
v1 (fixed::FromInt(5), fixed::FromInt(6), fixed::FromInt(7));
150 CFixedVector3D
v2 (fixed::FromInt(8), fixed::FromInt(9), fixed::FromInt(-10));
151 TS_ASSERT_EQUALS(v1
.Dot(v2
).ToDouble(), 5*8 + 6*9 + 7*-10);