-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlooktest.em
163 lines (147 loc) · 4.1 KB
/
looktest.em
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
system.import('std/shim/quaternion.em');
var testVecs = [
<1, 0, 0>,
<0, 1, 0>,
<0, 0, 1>,
<1, 1, 0>,
<1, 0, 1>,
<0, 1, 1>,
<1, 1, 1>
];
var NUM_RANDOM_TESTS = 10;
function getRandomVec() {
return <Math.random() * 4 - 2, Math.random() * 4 - 2,
Math.random() * 4 - 2>;
}
function test(dir, up) {
up = up || <0, 1, 0>;
var q = util.Quaternion.fromLookAt(dir, up);
var reason = '';
if(Math.abs(q.length() - 1) > 0.001) {
reason += 'quaternion not normalized\n';
}
var forward = q.mul(<0, 0, -1>);
var newUp = q.mul(<0, 1, 0>);
if((forward - dir.normal()).length() > 0.001) {
reason += 'incorrect forward vector\n';
}
if(Math.abs(newUp.dot(up.normal()) -
dir.normal().cross(up.normal()).length()) > 1e-04) {
reason += 'incorrect up vector\n';
}
if(reason != '') {
system.print('lookAt(' + std.core.pretty(dir) + ', ' +
std.core.pretty(up) + ') failed:\n gave ' +
std.core.pretty(q) + '\n which produces\n forward: ' +
std.core.pretty(forward) + '\n up: ' +
std.core.pretty(newUp) + '\n');
system.print(reason);
return false;
} else {
return true;
}
}
function runTests() {
for(var i in testTypes) {
var failures = testTypes[i]();
if(failures) {
system.print('' + failures + ' tests failed.\n');
return;
} else {
system.print('all tests passed.\n');
}
}
}
function randomBasic() {
var failures = 0;
system.print('Running basic random vector tests...');
for(var i = 0; i < NUM_RANDOM_TESTS; i++) {
if(!test(getRandomVec().normal()))
failures++;
}
return failures;
}
function edgeBasic() {
var failures = 0;
system.print('Running basic edge case tests...');
for(var i in testVecs) {
if(!test(testVecs[i].normal()))
failures++;
if(!test(testVecs[i].normal().neg()))
failures++;
}
return failures;
}
function upBasic() {
var failures = 0;
system.print('Running basic up-vector variation tests...');
for(var i in testVecs) {
if(!test(getRandomVec().normal(), testVecs[i].normal()))
failures++;
if(!test(getRandomVec().normal(), testVecs[i].normal().neg()))
failures++;
}
return failures;
}
function randomUp() {
var failures = 0;
system.print('Running random up-vector tests...');
for(var i = 0; i < NUM_RANDOM_TESTS; i++) {
if(!test(getRandomVec().normal(), getRandomVec().normal()))
failures++;
}
return failures;
}
function denormalized() {
var failures = 0;
system.print('Running denormalized direction tests...');
for(var i = 0; i < NUM_RANDOM_TESTS; i++)
{
if(!test(getRandomVec()))
failures++;
}
return failures;
}
function denormEdge() {
var failures = 0;
system.print('Running denormalized edge case tests...');
for(var i in testVecs) {
if(!test(testVecs[i].scale(2)))
failures++;
if(!test(testVecs[i].scale(.2)))
failures++;
if(!test(testVecs[i].scale(-2)))
failures++;
if(!test(testVecs[i].scale(-.2)))
failures++;
}
return failures;
}
function denormUp() {
var failures = 0;
system.print('Running denormalized up vector tests...');
for(var i = 0; i < NUM_RANDOM_TESTS; i++) {
if(!test(getRandomVec(), getRandomVec()))
failures++;
}
return failures;
}
function denormUpEdge() {
var failures = 0;
system.print('Running denormalized up vector edge case tests...');
for(var i in testVecs) {
if(!test(getRandomVec(), testVecs[i].scale(2)))
failures++;
if(!test(getRandomVec(), testVecs[i].scale(.2)))
failures++;
if(!test(getRandomVec(), testVecs[i].scale(-2)))
failures++;
if(!test(getRandomVec(), testVecs[i].scale(-.2)))
failures++;
}
return failures;
}
var testTypes = [
randomBasic, edgeBasic, upBasic, randomUp,
denormalized, denormEdge, denormUp, denormUpEdge
];