1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
/**
* @file v4math_test.cpp
* @author Adroit
* @date 2007-03
* @brief v4math test cases.
*
* $LicenseInfo:firstyear=2007&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "../test/lltut.h"
#include "llsd.h"
#include "../m4math.h"
#include "../v4math.h"
#include "../llquaternion.h"
namespace tut
{
struct v4math_data
{
};
typedef test_group<v4math_data> v4math_test;
typedef v4math_test::object v4math_object;
tut::v4math_test v4math_testcase("v4math_h");
template<> template<>
void v4math_object::test<1>()
{
LLVector4 vec4;
ensure("1:LLVector4:Fail to initialize " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4a(x,y,z);
ensure("2:LLVector4:Fail to initialize " ,((x == vec4a.mV[VX]) && (y == vec4a.mV[VY]) && (z == vec4a.mV[VZ])&& (1.0f == vec4a.mV[VW])));
LLVector4 vec4b(x,y,z,w);
ensure("3:LLVector4:Fail to initialize " ,((x == vec4b.mV[VX]) && (y == vec4b.mV[VY]) && (z == vec4b.mV[VZ])&& (w == vec4b.mV[VW])));
const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
LLVector4 vec4c(vec);
ensure("4:LLVector4:Fail to initialize " ,((vec[0] == vec4c.mV[VX]) && (vec[1] == vec4c.mV[VY]) && (vec[2] == vec4c.mV[VZ])&& (vec[3] == vec4c.mV[VW])));
LLVector3 vec3(-2.23f,1.01f,42.3f);
LLVector4 vec4d(vec3);
ensure("5:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4d.mV[VX]) && (vec3.mV[VY] == vec4d.mV[VY]) && (vec3.mV[VZ] == vec4d.mV[VZ])&& (1.f == vec4d.mV[VW])));
F32 w1 = -.234f;
LLVector4 vec4e(vec3,w1);
ensure("6:LLVector4:Fail to initialize " ,((vec3.mV[VX] == vec4e.mV[VX]) && (vec3.mV[VY] == vec4e.mV[VY]) && (vec3.mV[VZ] == vec4e.mV[VZ])&& (w1 == vec4e.mV[VW])));
}
template<> template<>
void v4math_object::test<2>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4;
vec4.setVec(x,y,z);
ensure("1:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
vec4.clearVec();
ensure("2:clearVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (1.0f == vec4.mV[VW])));
vec4.setVec(x,y,z,w);
ensure("3:setVec:Fail to initialize " ,((x == vec4.mV[VX]) && (y == vec4.mV[VY]) && (z == vec4.mV[VZ])&& (w == vec4.mV[VW])));
vec4.zeroVec();
ensure("4:zeroVec:Fail " ,((0 == vec4.mV[VX]) && (0 == vec4.mV[VY]) && (0 == vec4.mV[VZ])&& (0 == vec4.mV[VW])));
LLVector3 vec3(-2.23f,1.01f,42.3f);
vec4.clearVec();
vec4.setVec(vec3);
ensure("5:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (1.f == vec4.mV[VW])));
F32 w1 = -.234f;
vec4.zeroVec();
vec4.setVec(vec3,w1);
ensure("6:setVec:Fail to initialize " ,((vec3.mV[VX] == vec4.mV[VX]) && (vec3.mV[VY] == vec4.mV[VY]) && (vec3.mV[VZ] == vec4.mV[VZ])&& (w1 == vec4.mV[VW])));
const F32 vec[4] = {.112f ,23.2f, -4.2f, -.0001f};
LLVector4 vec4a;
vec4a.setVec(vec);
ensure("7:setVec:Fail to initialize " ,((vec[0] == vec4a.mV[VX]) && (vec[1] == vec4a.mV[VY]) && (vec[2] == vec4a.mV[VZ])&& (vec[3] == vec4a.mV[VW])));
}
template<> template<>
void v4math_object::test<3>()
{
F32 x = 10.f, y = -2.3f, z = -.023f;
LLVector4 vec4(x,y,z);
ensure("magVec:Fail ", is_approx_equal(vec4.magVec(), (F32) sqrt(x*x + y*y + z*z)));
ensure("magVecSquared:Fail ", is_approx_equal(vec4.magVecSquared(), (x*x + y*y + z*z)));
}
template<> template<>
void v4math_object::test<4>()
{
F32 x = 10.f, y = -2.3f, z = -.023f;
LLVector4 vec4(x,y,z);
F32 mag = vec4.normVec();
mag = 1.f/ mag;
ensure("1:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
x = 0.000000001f, y = 0.000000001f, z = 0.000000001f;
vec4.clearVec();
vec4.setVec(x,y,z);
mag = vec4.normVec();
ensure("2:normVec: Fail " ,is_approx_equal(mag*x,vec4.mV[VX]) && is_approx_equal(mag*y, vec4.mV[VY])&& is_approx_equal(mag*z, vec4.mV[VZ]));
}
template<> template<>
void v4math_object::test<5>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4(x,y,z,w);
vec4.abs();
ensure("abs:Fail " ,((x == vec4.mV[VX]) && (-y == vec4.mV[VY]) && (-z == vec4.mV[VZ])&& (-w == vec4.mV[VW])));
vec4.clearVec();
ensure("isExactlyClear:Fail " ,(TRUE == vec4.isExactlyClear()));
vec4.zeroVec();
ensure("isExactlyZero:Fail " ,(TRUE == vec4.isExactlyZero()));
}
template<> template<>
void v4math_object::test<6>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4(x,y,z,w),vec4a;
vec4a = vec4.scaleVec(vec4);
ensure("scaleVec:Fail " ,(is_approx_equal(x*x, vec4a.mV[VX]) && is_approx_equal(y*y, vec4a.mV[VY]) && is_approx_equal(z*z, vec4a.mV[VZ])&& is_approx_equal(w*w, vec4a.mV[VW])));
}
template<> template<>
void v4math_object::test<7>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4(x,y,z,w);
ensure("1:operator [] failed " ,( x == vec4[0]));
ensure("2:operator [] failed " ,( y == vec4[1]));
ensure("3:operator [] failed " ,( z == vec4[2]));
ensure("4:operator [] failed " ,( w == vec4[3]));
x = 23.f, y = -.2361f, z = 3.25;
vec4.setVec(x,y,z);
F32 &ref1 = vec4[0];
ensure("5:operator [] failed " ,( ref1 == vec4[0]));
F32 &ref2 = vec4[1];
ensure("6:operator [] failed " ,( ref2 == vec4[1]));
F32 &ref3 = vec4[2];
ensure("7:operator [] failed " ,( ref3 == vec4[2]));
F32 &ref4 = vec4[3];
ensure("8:operator [] failed " ,( ref4 == vec4[3]));
}
template<> template<>
void v4math_object::test<8>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
const F32 val[16] = {
1.f, 2.f, 3.f, 0.f,
.34f, .1f, -.5f, 0.f,
2.f, 1.23f, 1.234f, 0.f,
.89f, 0.f, 0.f, 0.f
};
LLMatrix4 mat(val);
LLVector4 vec4(x,y,z,w),vec4a;
vec4.rotVec(mat);
vec4a.setVec(x,y,z,w);
vec4a.rotVec(mat);
ensure_equals("1:rotVec: Fail " ,vec4a, vec4);
F32 a = 2.32f, b = -23.2f, c = -34.1112f, d = 1.010112f;
LLQuaternion q(a,b,c,d);
LLVector4 vec4b(a,b,c,d),vec4c;
vec4b.rotVec(q);
vec4c.setVec(a, b, c, d);
vec4c.rotVec(q);
ensure_equals("2:rotVec: Fail " ,vec4b, vec4c);
}
template<> template<>
void v4math_object::test<9>()
{
F32 x = 10.f, y = -2.3f, z = -.023f, w = -2.0f;
LLVector4 vec4(x,y,z,w),vec4a;;
std::ostringstream stream1, stream2;
stream1 << vec4;
vec4a.setVec(x,y,z,w);
stream2 << vec4a;
ensure("operator << failed",(stream1.str() == stream2.str()));
}
template<> template<>
void v4math_object::test<10>()
{
F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
vec4b = vec4a + vec4;
ensure("1:operator+:Fail to initialize " ,(is_approx_equal(x1+x2,vec4b.mV[VX]) && is_approx_equal(y1+y2,vec4b.mV[VY]) && is_approx_equal(z1+z2,vec4b.mV[VZ])));
x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
vec4.clearVec();
vec4a.clearVec();
vec4.setVec(x1,y1,z1);
vec4a +=vec4;
ensure_equals("2:operator+=: Fail to initialize", vec4a,vec4);
vec4a += vec4;
ensure("3:operator+=:Fail to initialize " ,(is_approx_equal(2*x1,vec4a.mV[VX]) && is_approx_equal(2*y1,vec4a.mV[VY]) && is_approx_equal(2*z1,vec4a.mV[VZ])));
}
template<> template<>
void v4math_object::test<11>()
{
F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f, w1 = .23f;
F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f, w2 = 1.3f;
LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2),vec4b;
vec4b = vec4a - vec4;
ensure("1:operator-:Fail to initialize " ,(is_approx_equal(x2-x1,vec4b.mV[VX]) && is_approx_equal(y2-y1,vec4b.mV[VY]) && is_approx_equal(z2-z1,vec4b.mV[VZ])));
x1 = -2.45f, y1 = 2.1f, z1 = 3.0f;
vec4.clearVec();
vec4a.clearVec();
vec4.setVec(x1,y1,z1);
vec4a -=vec4;
ensure_equals("2:operator-=: Fail to initialize" , vec4a,-vec4);
vec4a -=vec4;
ensure("3:operator-=:Fail to initialize " ,(is_approx_equal(-2*x1,vec4a.mV[VX]) && is_approx_equal(-2*y1,vec4a.mV[VY]) && is_approx_equal(-2*z1,vec4a.mV[VZ])));
}
template<> template<>
void v4math_object::test<12>()
{
F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
F32 res = vec4 * vec4a;
ensure("1:operator* failed " ,is_approx_equal(res, x1*x2 + y1*y2 + z1*z2));
vec4a.clearVec();
F32 mulVal = 4.2f;
vec4a = vec4 * mulVal;
ensure("2:operator* failed " ,is_approx_equal(x1*mulVal,vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
vec4a.clearVec();
vec4a = mulVal * vec4 ;
ensure("3:operator* failed " ,is_approx_equal(x1*mulVal, vec4a.mV[VX]) && is_approx_equal(y1*mulVal, vec4a.mV[VY])&& is_approx_equal(z1*mulVal, vec4a.mV[VZ]));
vec4 *= mulVal;
ensure("4:operator*= failed " ,is_approx_equal(x1*mulVal, vec4.mV[VX]) && is_approx_equal(y1*mulVal, vec4.mV[VY])&& is_approx_equal(z1*mulVal, vec4.mV[VZ]));
}
template<> template<>
void v4math_object::test<13>()
{
F32 x1 = 1.f, y1 = 2.f, z1 = -1.1f;
F32 x2 = 1.2f, y2 = 2.5f, z2 = 1.f;
LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2),vec4b;
vec4b = vec4 % vec4a;
ensure("1:operator% failed " ,is_approx_equal(y1*z2 - y2*z1, vec4b.mV[VX]) && is_approx_equal(z1*x2 -z2*x1, vec4b.mV[VY]) && is_approx_equal(x1*y2-x2*y1, vec4b.mV[VZ]));
vec4 %= vec4a;
ensure_equals("operator%= failed " ,vec4,vec4b);
}
template<> template<>
void v4math_object::test<14>()
{
F32 x = 1.f, y = 2.f, z = -1.1f,div = 4.2f;
F32 t = 1.f / div;
LLVector4 vec4(x,y,z), vec4a;
vec4a = vec4/div;
ensure("1:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
x = 1.23f, y = 4.f, z = -2.32f;
vec4.clearVec();
vec4a.clearVec();
vec4.setVec(x,y,z);
vec4a = vec4/div;
ensure("2:operator/ failed " ,is_approx_equal(x*t, vec4a.mV[VX]) && is_approx_equal(y*t, vec4a.mV[VY])&& is_approx_equal(z*t, vec4a.mV[VZ]));
vec4 /= div;
ensure("3:operator/ failed " ,is_approx_equal(x*t, vec4.mV[VX]) && is_approx_equal(y*t, vec4.mV[VY])&& is_approx_equal(z*t, vec4.mV[VZ]));
}
template<> template<>
void v4math_object::test<15>()
{
F32 x = 1.f, y = 2.f, z = -1.1f;
LLVector4 vec4(x,y,z), vec4a;
ensure("operator!= failed " ,(vec4 != vec4a));
vec4a = vec4;
ensure("operator== failed " ,(vec4 ==vec4a));
}
template<> template<>
void v4math_object::test<16>()
{
F32 x = 1.f, y = 2.f, z = -1.1f;
LLVector4 vec4(x,y,z), vec4a;
vec4a = - vec4;
ensure("operator- failed " , (vec4 == - vec4a));
}
template<> template<>
void v4math_object::test<17>()
{
F32 x = 1.f, y = 2.f, z = -1.1f,epsilon = .23425f;
LLVector4 vec4(x,y,z), vec4a(x,y,z);
ensure("1:are_parallel: Fail " ,(TRUE == are_parallel(vec4a,vec4,epsilon)));
x = 21.f, y = 12.f, z = -123.1f;
vec4a.clearVec();
vec4a.setVec(x,y,z);
ensure("2:are_parallel: Fail " ,(FALSE == are_parallel(vec4a,vec4,epsilon)));
}
template<> template<>
void v4math_object::test<18>()
{
F32 x = 1.f, y = 2.f, z = -1.1f;
F32 angle1, angle2;
LLVector4 vec4(x,y,z), vec4a(x,y,z);
angle1 = angle_between(vec4, vec4a);
vec4.normVec();
vec4a.normVec();
angle2 = acos(vec4 * vec4a);
ensure_approximately_equals("1:angle_between: Fail " ,angle1,angle2,8);
F32 x1 = 21.f, y1 = 2.23f, z1 = -1.1f;
LLVector4 vec4b(x,y,z), vec4c(x1,y1,z1);
angle1 = angle_between(vec4b, vec4c);
vec4b.normVec();
vec4c.normVec();
angle2 = acos(vec4b * vec4c);
ensure_approximately_equals("2:angle_between: Fail " ,angle1,angle2,8);
}
template<> template<>
void v4math_object::test<19>()
{
F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, x2 = 1.3f, y2 = 1.f, z2 = 1.f;
F32 val1,val2;
LLVector4 vec4(x1,y1,z1),vec4a(x2,y2,z2);
val1 = dist_vec(vec4,vec4a);
val2 = (F32) sqrt((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
ensure_equals("dist_vec: Fail ",val2, val1);
val1 = dist_vec_squared(vec4,vec4a);
val2 =((x1 - x2)*(x1 - x2) + (y1 - y2)* (y1 - y2) + (z1 - z2)* (z1 -z2));
ensure_equals("dist_vec_squared: Fail ",val2, val1);
}
template<> template<>
void v4math_object::test<20>()
{
F32 x1 =-2.3f, y1 = 2.f,z1 = 1.2f, w1 = -.23f, x2 = 1.3f, y2 = 1.f, z2 = 1.f,w2 = .12f;
F32 val = 2.3f,val1,val2,val3,val4;
LLVector4 vec4(x1,y1,z1,w1),vec4a(x2,y2,z2,w2);
val1 = x1 + (x2 - x1)* val;
val2 = y1 + (y2 - y1)* val;
val3 = z1 + (z2 - z1)* val;
val4 = w1 + (w2 - w1)* val;
LLVector4 vec4b = lerp(vec4,vec4a,val);
ensure("lerp failed", ((val1 ==vec4b.mV[VX])&& (val2 ==vec4b.mV[VY]) && (val3 ==vec4b.mV[VZ])&& (val4 ==vec4b.mV[VW])));
}
template<> template<>
void v4math_object::test<21>()
{
F32 x = 1.f, y = 2.f, z = -1.1f;
LLVector4 vec4(x,y,z);
LLVector3 vec3 = vec4to3(vec4);
ensure("vec4to3 failed", ((x == vec3.mV[VX])&& (y == vec3.mV[VY]) && (z == vec3.mV[VZ])));
LLVector4 vec4a = vec3to4(vec3);
ensure_equals("vec3to4 failed",vec4a,vec4);
}
template<> template<>
void v4math_object::test<22>()
{
F32 x = 1.f, y = 2.f, z = -1.1f;
LLVector4 vec4(x,y,z);
LLSD llsd = vec4.getValue();
LLVector3 vec3(llsd);
LLVector4 vec4a = vec3to4(vec3);
ensure_equals("getValue failed",vec4a,vec4);
}
}
|