view src/main.cpp @ 11:398894c52b69

Changed mind. Sticking with current design. Matrix22<T> is done.
author Eris Caffee <discordia@eldalin.com>
date Fri, 07 Oct 2011 13:33:48 -0500
parents 81d2aa42a860
children 1d2b25d4517f
line source
1 #include <iostream>
2 #include <iomanip>
3 #include <limits>
4 #include <string>
6 using namespace std;
8 #include "Math.h"
9 using namespace arda::Math;
11 ////////////////////////////////////////////////////////////////////////////////
12 void test_vector(void)
13 {
14 string s1, s2;
16 {
17 cout << "===============================================================" << endl <<
18 "Testing Vector constructors" << endl;
19 Vector2i v2i_1(0);
20 Vector2i v2i_2(1);
21 Vector2i v2i_3(1, 2);
22 Vector2i v2i_4(v2i_3);
24 cout << setw(40) << "v2i_1: " << v2i_1.getstring(s1) << endl;
25 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
26 cout << setw(40) << "v2i_3: " << v2i_3.getstring(s1) << endl;
27 cout << setw(40) << "v2i_4: " << v2i_4.getstring(s1) << endl;
29 Vector3f v3f_1(0);
30 Vector3f v3f_2(1);
31 Vector3f v3f_3(1, 2, 3);
32 Vector3f v3f_4(v3f_3);
34 cout << setw(40) << "v3f_1: " << v3f_1.getstring(s1) << endl;
35 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
36 cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl;
37 cout << setw(40) << "v3f_4: " << v3f_4.getstring(s1) << endl;
39 Vector4d v4d_1(0);
40 Vector4d v4d_2(1);
41 Vector4d v4d_3(1, 2, 3, 4);
42 Vector4d v4d_4(v4d_3);
44 cout << setw(40) << "v4d_1: " << v4d_1.getstring(s1) << endl;
45 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
46 cout << setw(40) << "v4d_3: " << v4d_3.getstring(s1) << endl;
47 cout << setw(40) << "v4d_4: " << v4d_4.getstring(s1) << endl;
48 }
50 {
51 cout << "===============================================================" << endl <<
52 "Testing Vector array indexing" << endl;
53 Vector2i v2i(1,2);
54 Vector3f v3f(1.1f, 2.2f, 3.3f);
55 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
57 cout << setw(40) << "v2i: " << v2i[0] << ", " << v2i[1] << endl;
58 cout << setw(40) << "v3f: " << v3f[0] << ", " << v3f[1] << ", " << v3f[2] << endl;
59 cout << setw(40) << "v4d: " << v4d[0] << ", " << v4d[1] << ", " << v4d[2] << ", " << v4d[3] << endl;
60 }
62 {
63 cout << "===============================================================" << endl <<
64 "Testing Vector assignment" << endl;
65 Vector2i v2i(1,2);
66 Vector3f v3f(1.1f, 2.2f, 3.3f);
67 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
68 Vector2i v2i_2(0);
69 Vector3f v3f_2(0);
70 Vector4d v4d_2(0);
72 cout << "Before assignment" << endl;
73 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
74 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
75 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
76 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
77 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
78 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
80 v2i_2 = v2i;
81 v3f_2 = v3f;
82 v4d_2 = v4d;
83 cout << "After assignment by =" << endl;
84 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
85 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
86 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
88 v2i_2.assign(1, 1);
89 v3f_2.assign(2.2, 2.2, 2.2);
90 v4d_2.assign(3.3, 3.3, 3.3, 3.3);
91 cout << "After assignment by assign()" << endl;
92 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
93 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
94 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
95 }
97 {
98 cout << "===============================================================" << endl <<
99 "Testing Vector comparison" << endl;
100 Vector2i v2i(1,2);
101 Vector3f v3f(1.1f, 2.2f, 3.3f);
102 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
103 Vector2i v2i_2(1,2);
104 Vector3f v3f_2(1.1f, 2.2f, 3.3f);
105 Vector4d v4d_2(1.1, 2.2, 3.3, 4.4);
106 Vector2i v2i_3(0);
107 Vector3f v3f_3(0);
108 Vector4d v4d_3(0);
110 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
111 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
112 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
113 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
114 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
115 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
116 cout << setw(40) << "v2i_3: " << v2i_3.getstring(s1) << endl;
117 cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl;
118 cout << setw(40) << "v4d_3: " << v4d_3.getstring(s1) << endl;
119 cout << boolalpha;
120 cout << setw(40) << "v2i == v2i_2: " << (v2i == v2i_2) << endl;
121 cout << setw(40) << "v2i == v2i_3: " << (v2i == v2i_3) << endl;
122 cout << setw(40) << "v2i != v2i_2: " << (v2i != v2i_2) << endl;
123 cout << setw(40) << "v2i != v2i_3: " << (v2i != v2i_3) << endl;
124 cout << setw(40) << "v3f == v3f_2: " << (v3f == v3f_2) << endl;
125 cout << setw(40) << "v3f == v3f_3: " << (v3f == v3f_3) << endl;
126 cout << setw(40) << "v3f != v3f_2: " << (v3f != v3f_2) << endl;
127 cout << setw(40) << "v3f != v3f_3: " << (v3f != v3f_3) << endl;
128 cout << setw(40) << "v4d == v4d_2: " << (v4d == v4d_2) << endl;
129 cout << setw(40) << "v4d == v4d_3: " << (v4d == v4d_3) << endl;
130 cout << setw(40) << "v4d != v4d_2: " << (v4d != v4d_2) << endl;
131 cout << setw(40) << "v4d != v4d_3: " << (v4d != v4d_3) << endl;
132 }
134 {
135 cout << "===============================================================" << endl <<
136 "Testing Vector addition" << endl;
137 Vector2i v2i(1,2);
138 Vector3f v3f(1.1f, 2.2f, 3.3f);
139 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
140 Vector2i v2i_2(3,4);
141 Vector3f v3f_2(4.4, 5.5, 6.6);
142 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
143 Vector2i v2i_3(0);
144 Vector3f v3f_3(0);
145 Vector4d v4d_3(0);
147 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
148 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
149 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
150 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
151 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
152 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
153 v2i_3 = v2i + v2i_2;
154 v3f_3 = v3f + v3f_2;
155 v4d_3 = v4d + v4d_2;
156 cout << setw(40) << "v2i_3 = v2i + v2i_2: " << v2i_3.getstring(s1) << endl;
157 cout << setw(40) << "v3f_3 = v3f + v3f_2: " << v3f_3.getstring(s1) << endl;
158 cout << setw(40) << "v4d_3 = v4d + v4d_2: " << v4d_3.getstring(s1) << endl;
159 v2i_3 += v2i_2;
160 v3f_3 += v3f_2;
161 v4d_3 += v4d_3;
162 cout << setw(40) << "v2i_3 += v2i_2: " << v2i_3.getstring(s1) << endl;
163 cout << setw(40) << "v3f_3 += v3f_2: " << v3f_3.getstring(s1) << endl;
164 cout << setw(40) << "v4d_3 += v4d_3: " << v4d_3.getstring(s1) << endl;
165 }
167 {
168 cout << "===============================================================" << endl <<
169 "Testing Vector subtraction" << endl;
170 Vector2i v2i(1,2);
171 Vector3f v3f(1.1f, 2.2f, 3.3f);
172 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
173 Vector2i v2i_2(3,4);
174 Vector3f v3f_2(4.4, 5.5, 6.6);
175 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
176 Vector2i v2i_3(0);
177 Vector3f v3f_3(0);
178 Vector4d v4d_3(0);
180 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
181 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
182 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
183 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
184 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
185 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
186 v2i_3 = v2i - v2i_2;
187 v3f_3 = v3f - v3f_2;
188 v4d_3 = v4d - v4d_2;
189 cout << setw(40) << "v2i_3 = v2i - v2i_2: " << v2i_3.getstring(s1) << endl;
190 cout << setw(40) << "v3f_3 = v3f - v3f_2: " << v3f_3.getstring(s1) << endl;
191 cout << setw(40) << "v4d_3 = v4d - v4d_2: " << v4d_3.getstring(s1) << endl;
192 v2i_3 -= v2i_2;
193 v3f_3 -= v3f_2;
194 v4d_3 -= v4d_3;
195 cout << setw(40) << "v2i_3 -= v2i_2: " << v2i_3.getstring(s1) << endl;
196 cout << setw(40) << "v3f_3 -= v3f_2: " << v3f_3.getstring(s1) << endl;
197 cout << setw(40) << "v4d_3 -= v4d_3: " << v4d_3.getstring(s1) << endl;
198 }
200 {
201 cout << "===============================================================" << endl <<
202 "Testing Vector scalar multiplication" << endl;
203 Vector2i v2i(1,2);
204 Vector3f v3f(1.1f, 2.2f, 3.3f);
205 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
206 Vector2i v2i_2(0);
207 Vector3f v3f_2(0);
208 Vector4d v4d_2(0);
209 int i = 2;
210 float f = 2.f;
211 double d = 2.0;
213 cout << setw(40) << "i: " << i << endl;
214 cout << setw(40) << "f: " << f << endl;
215 cout << setw(40) << "d: " << d << endl;
216 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
217 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
218 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
219 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
220 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
221 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
222 v2i_2 = v2i * i;
223 v3f_2 = v3f * f;
224 v4d_2 = v4d * d;
225 cout << setw(40) << "v2i_2 = v2i * i: " << v2i_2.getstring(s1) << endl;
226 cout << setw(40) << "v3f_2 = v3f * f: " << v3f_2.getstring(s1) << endl;
227 cout << setw(40) << "v4d_2 = v4d * d: " << v4d_2.getstring(s1) << endl;
228 v2i_2 *= i;
229 v3f_2 *= f;
230 v4d_2 *= d;
231 cout << setw(40) << "v2i_2 *= i: " << v2i_2.getstring(s1) << endl;
232 cout << setw(40) << "v3f_2 *= f: " << v3f_2.getstring(s1) << endl;
233 cout << setw(40) << "v4d_2 *= d: " << v4d_2.getstring(s1) << endl;
234 }
236 {
237 cout << "===============================================================" << endl <<
238 "Testing Vector scalar division" << endl;
239 Vector2i v2i(1,2);
240 Vector3f v3f(1.1f, 2.2f, 3.3f);
241 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
242 Vector2i v2i_2(0);
243 Vector3f v3f_2(0);
244 Vector4d v4d_2(0);
245 int i = 2;
246 float f = 2.f;
247 double d = 2.0;
249 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
250 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
251 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
252 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
253 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
254 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
255 v2i_2 = v2i / i;
256 v3f_2 = v3f / f;
257 v4d_2 = v4d / d;
258 cout << setw(40) << "v2i_2 = v2i / i: " << v2i_2.getstring(s1) << endl;
259 cout << setw(40) << "v3f_2 = v3f / f: " << v3f_2.getstring(s1) << endl;
260 cout << setw(40) << "v4d_2 = v4d / d: " << v4d_2.getstring(s1) << endl;
261 v2i_2 /= i;
262 v3f_2 /= f;
263 v4d_2 /= d;
264 cout << setw(40) << "v2i_2 /= i: " << v2i_2.getstring(s1) << endl;
265 cout << setw(40) << "v3f_2 /= f: " << v3f_2.getstring(s1) << endl;
266 cout << setw(40) << "v4d_2 /= d: " << v4d_2.getstring(s1) << endl;
267 }
269 {
270 cout << "===============================================================" << endl <<
271 "Testing Vector cross product" << endl;
272 Vector3f v3f(1.1f, 2.2f, 3.3f);
273 Vector3f v3f_2(4.4, 5.5, 6.6);
274 Vector3f v3f_3(0);
275 Vector3f v3f_4(0);
276 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
277 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
278 cout << setw(40) << "v3f_3: " << v3f_3.getstring(s1) << endl;
279 v3f.cross(v3f_2, v3f_3);
280 cout << setw(40) << "v3f.cross(v3f_2, v3f_3): " << v3f_3.getstring(s1) << endl;
281 v3f_4 = 2.0 * v3f.cross(v3f_2);
282 cout << setw(40) << "2.0 * v3f.cross(v3f_2,): " << v3f_4.getstring(s1) << endl;
283 v3f_4.assign(0,0,0);
284 v3f.cross(v3f_2, v3f_4);
285 cout << setw(40) << "v3f.cross(v3f_2, v3f_4): " << v3f_4.getstring(s1) << endl;
286 }
288 {
289 cout << "===============================================================" << endl <<
290 "Testing Vector dot product" << endl;
291 Vector2i v2i(1, 2);
292 Vector2i v2i_2(3, 4);
293 Vector3f v3f(1.1f, 2.2f, 3.3f);
294 Vector3f v3f_2(4.4, 5.5, 6.6);
295 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
296 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
297 int i;
298 float f;
299 double d;
300 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
301 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
302 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
303 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
304 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
305 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
306 i = v2i.dot(v2i_2);
307 cout << setw(40) << "i = v2i.dot(v2i_2): " << i << endl;
308 f = v3f.dot(v3f_2);
309 cout << setw(40) << "f = v3f.dot(v3f_2): " << f << endl;
310 d = v4d.dot(v4d_2);
311 cout << setw(40) << "d = v4d.dot(v4d_2): " << d << endl;
312 }
314 {
315 cout << "===============================================================" << endl <<
316 "Testing Vector length" << endl;
317 Vector2i v2i(1, 2);
318 Vector3f v3f(1.1f, 2.2f, 3.3f);
319 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
320 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
321 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
322 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
323 cout << setw(40) << "v2i.length(): " << v2i.length() << endl;
324 cout << setw(40) << "v3f.length(): " << v3f.length() << endl;
325 cout << setw(40) << "v4d.length(): " << v4d.length() << endl;
326 }
328 {
329 cout << "===============================================================" << endl <<
330 "Testing Vector normalize" << endl;
331 Vector2f v2f(1.1, 2.2);
332 Vector3f v3f(1.1f, 2.2f, 3.3f);
333 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
334 cout << setw(40) << "v2f: " << v2f.getstring(s1) << endl;
335 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
336 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
337 v2f.normalize();
338 cout << setw(40) << "v2f.normalize() " << v2f.getstring(s1) << endl;
339 v3f.normalize();
340 cout << setw(40) << "v3f.normalize() " << v3f.getstring(s1) << endl;
341 v4d.normalize();
342 cout << setw(40) << "v4d.normalize() " << v4d.getstring(s1) << endl;
343 cout << setw(40) << "v2f.length(): " << v2f.length() << endl;
344 cout << setw(40) << "v3f.length(): " << v3f.length() << endl;
345 cout << setw(40) << "v4d.length(): " << v4d.length() << endl;
346 }
348 {
349 cout << "===============================================================" << endl <<
350 "Testing Vector get_angle" << endl;
351 Vector2i v2i(1, 2);
352 Vector2i v2i_2(3, 4);
353 Vector3f v3f(1.1f, 2.2f, 3.3f);
354 Vector3f v3f_2(4.4, 5.5, 6.6);
355 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
356 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
357 double d;
358 cout << setw(40) << "v2i: " << v2i.getstring(s1) << endl;
359 cout << setw(40) << "v2i_2: " << v2i_2.getstring(s1) << endl;
360 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
361 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
362 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
363 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
364 d = v2i.get_angle(v2i_2);
365 cout << setw(40) << "d = v2i.get_angle(v2i_2): " << d << endl;
366 d = v3f.get_angle(v3f_2);
367 cout << setw(40) << "d = v3f.get_angle(v3f_2): " << d << endl;
368 d = v4d.get_angle(v4d_2);
369 cout << setw(40) << "d = v4d.get_angle(v4d_2): " << d << endl;
370 }
372 {
373 cout << "===============================================================" << endl <<
374 "Testing Vector get_anglen" << endl;
375 Vector2f v2f(1.1, 2.2);
376 Vector2f v2f_2(3.3, 4.4);
377 Vector3f v3f(1.1f, 2.2f, 3.3f);
378 Vector3f v3f_2(4.4, 5.5, 6.6);
379 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
380 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
381 double d;
382 v2f.normalize();
383 v2f_2.normalize();
384 v3f.normalize();
385 v3f_2.normalize();
386 v4d.normalize();
387 v4d_2.normalize();
388 cout << setw(40) << "v2f: " << v2f.getstring(s1) << endl;
389 cout << setw(40) << "v2f_2: " << v2f_2.getstring(s1) << endl;
390 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
391 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
392 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
393 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
394 d = v2f.get_anglen(v2f_2);
395 cout << setw(40) << "d = v2f.get_anglen(v2f_2): " << d << endl;
396 d = v3f.get_anglen(v3f_2);
397 cout << setw(40) << "d = v3f.get_anglen(v3f_2): " << d << endl;
398 d = v4d.get_anglen(v4d_2);
399 cout << setw(40) << "d = v4d.get_anglen(v4d_2): " << d << endl;
400 }
402 {
403 cout << "===============================================================" << endl <<
404 "Testing Vector get_proj" << endl;
405 Vector2f v2f(1.1, 2.2);
406 Vector2f v2f_2(3.3, 4.4);
407 Vector2f v2f_3(0);
408 Vector3f v3f(1.1f, 2.2f, 3.3f);
409 Vector3f v3f_2(4.4, 5.5, 6.6);
410 Vector3f v3f_3(0);
411 Vector4d v4d(1.1, 2.2, 3.3, 4.4);
412 Vector4d v4d_2(5.5, 6.6, 7.7, 8.8);
413 Vector4d v4d_3(0);
414 cout << setw(40) << "v2f: " << v2f.getstring(s1) << endl;
415 cout << setw(40) << "v2f_2: " << v2f_2.getstring(s1) << endl;
416 cout << setw(40) << "v3f: " << v3f.getstring(s1) << endl;
417 cout << setw(40) << "v3f_2: " << v3f_2.getstring(s1) << endl;
418 cout << setw(40) << "v4d: " << v4d.getstring(s1) << endl;
419 cout << setw(40) << "v4d_2: " << v4d_2.getstring(s1) << endl;
420 v2f_3 = v2f.proj(v2f_2);
421 cout << setw(40) << "v2f_3 = v2f.proj(v3f_2): " << v2f_3.getstring(s1) << endl;
422 v3f_3 = v3f.proj(v3f_2);
423 cout << setw(40) << "v3f_3 = v3f.proj(v3f_2): " << v3f_3.getstring(s1) << endl;
424 v4d_3 = v4d.proj(v4d_2);
425 cout << setw(40) << "v4d_3 = v4d.proj(v4d_2): " << v4d_3.getstring(s1) << endl;
426 v2f_3.assign(0,0);
427 v3f_3.assign(0,0,0);
428 v4d_3.assign(0,0,0,0);
429 v2f.proj(v2f_2, v2f_3);
430 cout << setw(40) << "v2f.proj(v2f_2, v2f_3): " << v2f_3.getstring(s1) << endl;
431 v3f.proj(v3f_2, v3f_3);
432 cout << setw(40) << "v3f.proj(v3f_2, v3f_3): " << v3f_3.getstring(s1) << endl;
433 v4d.proj(v4d_2, v4d_3);
434 cout << setw(40) << "v4d.proj(v4d_2, v4d_3): " << v4d_3.getstring(s1) << endl;
435 }
437 }
439 ////////////////////////////////////////////////////////////////////////////////
440 void test_matrix(void)
441 {
442 string s1, s2;
444 {
445 cout << "===============================================================" << endl <<
446 "Testing Maxtrix constructors" << endl;
447 Matrix22i m22i_1(0);
448 Matrix22i m22i_2(1);
449 Matrix22i m22i_3(1, 2, 3, 4);
450 Matrix22i m22i_4(m22i_3);
452 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
453 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
454 cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl;
455 cout << setw(40) << "m22i_4: " << m22i_4.getstring(s1) << endl;
456 }
458 {
459 cout << "===============================================================" << endl <<
460 "Testing Maxtrix array indexing" << endl;
461 Matrix22i m22i(1, 2, 3, 4);
463 cout << setw(40) << "m22i: " << m22i[0] << " " << m22i[1] << " " << m22i[2] << " " << m22i[3] << " "<< endl;
464 }
466 {
467 cout << "===============================================================" << endl <<
468 "Testing Maxtrix array assignment" << endl;
469 Matrix22i m22i_1(1, 2, 3, 4);
470 Matrix22i m22i_2(5, 6, 7, 8);
472 cout << "Before assignment" << endl;
473 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
474 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
476 m22i_2 = m22i_1;
477 cout << "After assignment by = " << endl;
478 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
480 m22i_2.assign(9, 10, 11, 12);
481 cout << "After assignment by assign" << endl;
482 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
483 }
485 {
486 cout << "===============================================================" << endl <<
487 "Testing Maxtrix comparison" << endl;
488 Matrix22i m22i_1(1, 2, 3, 4);
489 Matrix22i m22i_2(1, 2, 3, 4);
490 Matrix22i m22i_3(0);
492 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
493 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
494 cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl;
495 cout << boolalpha;
496 cout << setw(40) << "m22i_1 == m22i_2: " << (m22i_1 == m22i_2) << endl;
497 cout << setw(40) << "m22i_1 == m22i_3: " << (m22i_2 == m22i_3) << endl;
498 cout << setw(40) << "m22i_1 != m22i_2: " << (m22i_1 != m22i_2) << endl;
499 cout << setw(40) << "m22i_1 != m22i_3: " << (m22i_2 != m22i_3) << endl;
500 }
502 {
503 cout << "===============================================================" << endl <<
504 "Testing Maxtrix addition" << endl;
505 Matrix22i m22i_1(1, 2, 3, 4);
506 Matrix22i m22i_2(5, 6, 7, 8);
507 Matrix22i m22i_3(0);
509 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
510 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
511 cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl;
512 m22i_3 = m22i_1 + m22i_2;
513 cout << setw(40) << "m22i_3 = m22i_1 + m22i_2: " << m22i_3.getstring(s1) << endl;
515 m22i_3 += m22i_1;
516 cout << setw(40) << "m22i_3 += m22i_1: " << m22i_3.getstring(s1) << endl;
517 }
519 {
520 cout << "===============================================================" << endl <<
521 "Testing Maxtrix subtraction" << endl;
522 Matrix22i m22i_1(1, 2, 3, 4);
523 Matrix22i m22i_2(5, 6, 7, 8);
524 Matrix22i m22i_3(0);
526 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
527 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
528 cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl;
529 m22i_3 = m22i_1 - m22i_2;
530 cout << setw(40) << "m22i_3 = m22i_1 - m22i_2: " << m22i_3.getstring(s1) << endl;
532 m22i_3 -= m22i_1;
533 cout << setw(40) << "m22i_3 -= m22i_1: " << m22i_3.getstring(s1) << endl;
534 }
536 {
537 cout << "===============================================================" << endl <<
538 "Testing Matrix scalar multiplication" << endl;
539 Matrix22i m22i_1(1, 2, 3, 4);
540 Matrix22i m22i_2(0);
541 int i = 2;
543 cout << setw(40) << "i: " << i << endl;
544 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
545 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
546 m22i_2 = m22i_1 *i;
547 cout << setw(40) << "m22i_2 = m22i_1 * i: " << m22i_2.getstring(s1) << endl;
548 m22i_2 = i * m22i_1;
549 cout << setw(40) << "m22i_2 = i * m22i_1: " << m22i_2.getstring(s1) << endl;
550 m22i_2 *= i;
551 cout << setw(40) << "m22i_2 *= i: " << m22i_2.getstring(s1) << endl;
552 }
554 {
555 cout << "===============================================================" << endl <<
556 "Testing Matrix scalar division" << endl;
557 Matrix22i m22i_1(1, 2, 3, 4);
558 Matrix22i m22i_2(0);
559 int i = 2;
561 cout << setw(40) << "i: " << i << endl;
562 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
563 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
564 m22i_2 = m22i_1 / i;
565 cout << setw(40) << "m22i_2 = m22i_1 / i: " << m22i_2.getstring(s1) << endl;
566 m22i_1 /= i;
567 cout << setw(40) << "m22i_1 /= i: " << m22i_2.getstring(s1) << endl;
568 }
570 {
571 cout << "===============================================================" << endl <<
572 "Testing Maxtrix multiplication" << endl;
573 Matrix22i m22i_1(1, 2, 3, 4);
574 Matrix22i m22i_2(5, 6, 7, 8);
575 Matrix22i m22i_3(0);
577 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
578 cout << setw(40) << "m22i_2: " << m22i_2.getstring(s1) << endl;
579 cout << setw(40) << "m22i_3: " << m22i_3.getstring(s1) << endl;
580 m22i_3 = m22i_1 * m22i_2;
581 cout << setw(40) << "m22i_3 = m22i_1 * m22i_2: " << m22i_3.getstring(s1) << endl;
582 }
584 {
585 cout << "===============================================================" << endl <<
586 "Testing Maxtrix det" << endl;
587 Matrix22i m22i_1(1, 2, 3, 4);
588 double d;
590 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
591 d = det(m22i_1);
592 cout << setw(40) << "d = det(m22i_1): " << d << endl;
593 }
595 {
596 cout << "===============================================================" << endl <<
597 "Testing Maxtrix transpose" << endl;
598 Matrix22i m22i_1(1, 2, 3, 4);
599 Matrix22i m22i_2;
601 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
602 m22i_2 = transpose(m22i_1);
603 cout << setw(40) << "m22i_2 = transpose(m22i_1): " << m22i_2.getstring(s1) << endl;
604 }
606 {
607 cout << "===============================================================" << endl <<
608 "Testing Maxtrix setidentity" << endl;
609 Matrix22i m22i_1(1, 2, 3, 4);
611 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
612 m22i_1.setidentity();
613 cout << setw(40) << "m22i_1.setidentity(): " << m22i_1.getstring(s1) << endl;
614 }
616 {
617 cout << "===============================================================" << endl <<
618 "Testing Matrix getrow() and getcol()" << endl;
619 Matrix22i m22i_1(1, 2, 3, 4);
620 Vector2i v2i_1(0);
622 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
623 v2i_1 = m22i_1.getrow(0);
624 cout << setw(40) << "v2i_1 = m22i_1.getrow(0): " << v2i_1.getstring(s1) << endl;
625 v2i_1 = m22i_1.getrow(1);
626 cout << setw(40) << "v2i_1 = m22i_1.getrow(1): " << v2i_1.getstring(s1) << endl;
627 v2i_1 = m22i_1.getcol(0);
628 cout << setw(40) << "v2i_1 = m22i_1.getcol(0): " << v2i_1.getstring(s1) << endl;
629 v2i_1 = m22i_1.getcol(1);
630 cout << setw(40) << "v2i_1 = m22i_1.getcol(1): " << v2i_1.getstring(s1) << endl;
631 }
633 {
634 cout << "===============================================================" << endl <<
635 "Testing Matrix setrow() and setcol()" << endl;
636 Matrix22i m22i_1;
637 Vector2i v2i_1(2, 3);
639 m22i_1.setidentity();
640 cout << setw(40) << "m22i_1: " << m22i_1.getstring(s1) << endl;
641 m22i_1.setrow(0, v2i_1);
642 cout << setw(40) << "m22i_1.setrow(0, v2i_1): " << m22i_1.getstring(s1) << endl;
644 m22i_1.setidentity();
645 m22i_1.setrow(1, v2i_1);
646 cout << setw(40) << "m22i_1.setrow(1, v2i_1): " << m22i_1.getstring(s1) << endl;
648 m22i_1.setidentity();
649 m22i_1.setrow(1, 4, 5);
650 cout << setw(40) << "m22i_1.setrow(1, 4, 5): " << m22i_1.getstring(s1) << endl;
652 m22i_1.setidentity();
653 m22i_1.setcol(0, v2i_1);
654 cout << setw(40) << "m22i_1.setcol(0, v2i_1): " << m22i_1.getstring(s1) << endl;
656 m22i_1.setidentity();
657 m22i_1.setcol(1, v2i_1);
658 cout << setw(40) << "m22i_1.setcol(1, v2i_1): " << m22i_1.getstring(s1) << endl;
660 m22i_1.setidentity();
661 m22i_1.setcol(1, 4, 5);
662 cout << setw(40) << "m22i_1.setcol(1, 4, 5): " << m22i_1.getstring(s1) << endl;
663 }
664 }
666 ////////////////////////////////////////////////////////////////////////////////
667 void test_all(void)
668 {
669 test_vector();
670 test_matrix();
671 }
673 ////////////////////////////////////////////////////////////////////////////////
674 void show_menu(void)
675 {
676 cout << endl << endl
677 << "Test what?" << endl
678 << "0) All" << endl << endl
679 << "1) Vector" << endl
680 << "2) Matrix" << endl
681 << endl
682 << "99 Quit" << endl;
683 }
685 ////////////////////////////////////////////////////////////////////////////////
686 int main (int argc, char * argv[])
687 {
688 int retval = 0;
690 try
691 {
692 int choice = -1;
694 while (choice != 99)
695 {
696 show_menu();
697 cin >> choice;
698 if(!cin)
699 {
700 cin.clear();
701 cin.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
702 }
703 if (choice != 99)
704 {
705 switch (choice)
706 {
707 case 0:
708 test_all();
709 break;
710 case 1:
711 test_vector();
712 break;
713 case 2:
714 test_matrix();
715 break;
716 default:
717 cout << "Unrecognized choice. Please try again." << endl;
718 }
719 choice = -1;
720 }
721 }
722 }
723 catch (const std::exception & error)
724 {
725 string e = "Caught exception: ";
726 e += error.what();
727 cerr << e << endl;
728 retval = 1;
729 }
731 return retval;;
733 }