-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmath_server_testing.lua
More file actions
472 lines (369 loc) · 15.8 KB
/
math_server_testing.lua
File metadata and controls
472 lines (369 loc) · 15.8 KB
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
-- Created by (c)The_GTA.
local output_debug_of_compare = false;
local function fpe(a, b)
-- After all, the FPU is not made to be accurate.
return math.abs(a - b) < 0.001;
end
local function fpe_quot4(a1, a2, a3, a4, b1, b2, b3, b4)
if ( _math_eq(a4, 0) ) then
return _math_eq(b4, 0);
elseif ( _math_eq(b4, 0) ) then
return _math_eq(a4, 0);
end
local d1a = a1/a4;
local d2a = a2/a4;
local d3a = a3/a4;
local d1b = b1/b4;
local d2b = b2/b4;
local d3b = b3/b4;
return fpe(d1a, d1b) and fpe(d2a, d2b) and fpe(d3a, d3b);
end
local function find_inter_result(
inter,
b1lower, b1upper,
a1lowerk1, a1lowerk2, a1upperk1, a1upperk2,
c1lowerk1, c1lowerk2, c1lowerk3, c1lowerk4, c1upperk1, c1upperk2, c1upperk3, c1upperk4
)
local found = false;
for m,n in ipairs(inter) do
if (output_debug_of_compare) then
print(
"(" .. b1lower .. ", " .. b1upper .. ") = (" .. n.b1lower .. ", " .. n.b1upper .. "), " ..
"(" .. a1lowerk1 .. ", " .. a1lowerk2 .. ", " .. a1upperk1 .. ", " .. a1upperk2 .. ") = (" .. n.a1lower[1] .. ", " .. n.a1lower[2] .. ", " .. n.a1upper[1] .. ", " .. n.a1upper[2] .. "), " ..
"(" .. c1lowerk1 .. ", " .. c1lowerk2 .. ", " .. c1lowerk3 .. ", " .. c1lowerk4 .. ", " .. c1upperk1 .. ", " .. c1upperk2 .. ", " .. c1upperk3 .. ", " .. c1upperk4 .. ") = (" ..
n.c1lower[1] .. ", " .. n.c1lower[2] .. ", " .. n.c1lower[3] .. ", " .. n.c1lower[4] .. ", " .. n.c1upper[1] .. ", " .. n.c1upper[2] .. ", " .. n.c1upper[3] .. ", " .. n.c1upper[4] .. ")"
);
end
if (fpe(n.b1lower, b1lower)) and (fpe(n.b1upper, b1upper)) and
(fpe(n.a1lower[1], a1lowerk1)) and (fpe(n.a1lower[2], a1lowerk2)) and
(fpe(n.a1upper[1], a1upperk1)) and (fpe(n.a1upper[2], a1upperk2)) and
(fpe_quot4( n.c1lower[1], n.c1lower[2], n.c1lower[3], n.c1lower[4], c1lowerk1, c1lowerk2, c1lowerk3, c1lowerk4 )) and
(fpe_quot4( n.c1upper[1], n.c1upper[2], n.c1upper[3], n.c1upper[4], c1upperk1, c1upperk2, c1upperk3, c1upperk4 )) then
found = true;
break;
end
end
if not ( found ) then
error( "unit test fail", 2 );
end
if (output_debug_of_compare) then
outputConsole("found");
end
end
local function frustum_unit_test()
-- All those unit tests are verified math on paper.
if (true) then
-- 3D frustum-plane non-linear intersection - example #6
-- https://1drv.ms/u/s!Ao_bx9imD7B8hJ4U2BiAV2LY5lnkxA?e=cgvN1R
local plane = createPlane(
createVector( -2, 0, 2 ),
createVector( 4, 0, 0 ),
createVector( 0, 6, 0 )
);
local frustum = createViewFrustum(
createVector( 0, 0, 0 ),
createVector( 4, 0, 0 ),
createVector( 0, 0, 4 ),
createVector( 0, 8, 0 )
);
local inter = frustum.intersectWithPlane( plane, false );
assert( #inter >= 3 );
find_inter_result(
inter,
2/3, 1,
0, 0, 0, 0,
0, 4, 0, 2, 0, 4, 0, 2
);
find_inter_result(
inter,
2/3, 1,
0, 0, 1, 0,
0, 4, 0, 2, 0, 4, 0, 2
);
find_inter_result(
inter,
2/3, 1,
-1, 0, 0, 0,
0, 4, 0, 2, 0, 4, 0, 2
);
end
if (true) then
-- 3D frustum plane non-linear intersection - example #7
-- https://1drv.ms/u/s!Ao_bx9imD7B8hJ4V1XdLo8fGTa5MXw?e=FEezxh
local plane = createPlane(
createVector(-3, 2, -3),
createVector(4, 0, 0),
createVector(0, 1, 4)
);
local frustum = createViewFrustum(
createVector(0, 0, 0),
createVector(4, 0, 0),
createVector(0, 0, 5),
createVector(0, 10, 0)
);
local inter = frustum.intersectWithPlane( plane, false );
assert( #inter >= 4 );
find_inter_result(
inter,
-1, 2/3,
0, 0, 0, 0,
0, 5/4, -10, -11/4, 0, 5/4, -10, -11/4
);
find_inter_result(
inter,
-1, -4/5,
0, 0, 0, 1,
0, 5/4, -10, -11/4, 0, 5/4, -10, -11/4
);
find_inter_result(
inter,
-4/5, 2/3,
0, 0, -5/44, 10/11,
0, 5/4, -10, -11/4, 0, 5/4, -10, -11/4
);
find_inter_result(
inter,
-1, 2/3,
0, -1, 0, 0,
0, 5/4, -10, -11/4, 0, 5/4, -10, -11/4
);
end
if (true) then
-- 3D frustum-plane non-linear intersection - example #8
-- https://1drv.ms/u/s!Ao_bx9imD7B8hJ4WfvqXobUac3GoGA?e=fytPRS
local plane = createPlane(
createVector(-2, 8, -2),
createVector(1, 1, 0),
createVector(0, 1, 1)
);
local frustum = createViewFrustum(
createVector(0, 0, 0),
createVector(6, 0, 0),
createVector(0, 0, 8),
createVector(0, 10, 0)
);
local inter = frustum.intersectWithPlane( plane );
find_inter_result(
inter,
-15/54, -15/108,
8/30, -1/3, 4/33, -5/33,
6, 8, -10, -12, 6, 8, -10, -12
);
find_inter_result(
inter,
-15/48, -15/54,
8/30, -1/3, 20/3, 5/3,
6, 8, -10, -12, 6, 8, -10, -12
);
find_inter_result(
inter,
-1/8, -1/8,
44/3, 5/3, -4/3, -1/3,
6, 8, -10, -12, 6, 8, -10, -12
);
find_inter_result(
inter,
-15/108, -1/8,
44/3, 5/3, 4/33, -5/33,
6, 8, -10, -12, 6, 8, -10, -12
);
-- Performance test.
if (false) then
for n=1,100 do
frustum.intersectWithPlane( plane, false );
end
end
end
if (true) then
-- 3D frustum-triangle non-linear intersection - example #1
-- https://1drv.ms/u/s!Ao_bx9imD7B8hJ4TCbQ49eP2IDD7Sw?e=WYWd3t
local plane = createPlane(
createVector(-2, 6, -1),
createVector(4, 0, 0),
createVector(0, 0, 2)
);
local frustum = createViewFrustum(
createVector(0, 0, 0),
createVector(6, 0, 0),
createVector(0, 0, 5),
createVector(0, 15, 0)
);
local inter = frustum.intersectWithTrianglePlane(plane);
find_inter_result(
inter,
-1/2, 0,
0, 0, -5/3, 0,
0, 0, 15, 6, 0, 0, 15, 6
);
find_inter_result(
inter,
-1/2, 0,
0, -5/6, 0, 0,
0, 0, 15, 6, 0, 0, 15, 6
);
find_inter_result(
inter,
0, 1/2,
0, -5/6, -5/3, 0,
0, 0, 15, 6, 0, 0, 15, 6
);
end
if (true) then
-- 3D frustum-plane non-linear intesection - example #9
-- https://1drv.ms/u/s!Ao_bx9imD7B8hJ4SjRRGgkCfvrTfPg?e=Uw36JF
local plane = createPlane(
createVector(3, 4, 3),
createVector(1, 0, 0),
createVector(0, 0, 1)
);
local frustum = createViewFrustum(
createVector(0, 0, 0),
createVector(4, 0, 0),
createVector(0, 0, 3),
createVector(0, 8, 0)
);
local inter = frustum.intersectWithPlane(plane);
assert( inter == false );
end
end
frustum_unit_test();
local function output_unit_test(str)
outputDebugString(str);
end
local function tools_unit_test()
local function is_relevant_a1_cond(solutionType)
return ( solutionType == "a1equal" ) or
( solutionType == "a1min" ) or
( solutionType == "a1inferior" ) or
( solutionType == "a1max" ) or
( solutionType == "a1superior" );
end
do
local chain = createConditionAND();
chain.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 1));
chain.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 1));
local sub_or1 = createConditionOR();
sub_or1.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 2));
sub_or1.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 2));
chain.addVar(sub_or1);
local sub_or2 = createConditionOR();
sub_or2.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 3));
sub_or2.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 3));
chain.addVar(sub_or2);
chain = smart_group_condition(chain, is_relevant_a1_cond, is_relevant_a1_cond);
assert( chain.toString() == "( ( a1 >= (-3) AND a1 >= (-2) AND a1 >= (-1) AND a1 <= 1 ) OR ( a1 >= (-3) AND a1 <= 2 AND a1 >= (-1) AND a1 <= 1 ) OR ( a1 <= 3 AND a1 >= (-2) AND a1 >= (-1) AND a1 <= 1 ) OR ( a1 <= 3 AND a1 <= 2 AND a1 >= (-1) AND a1 <= 1 ) )" );
end
do
local chain = createConditionAND();
chain.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 1));
chain.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 1));
local sub_or = createConditionOR();
do
local first_cond = createConditionAND();
first_cond.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 2));
first_cond.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 2));
local subsub_or = createConditionOR();
subsub_or.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 3));
subsub_or.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 3));
first_cond.addVar(subsub_or);
sub_or.addVar(first_cond);
end
do
local second_cond = createConditionAND();
second_cond.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 4));
second_cond.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 5));
sub_or.addVar(second_cond);
end
chain.addVar(sub_or);
chain = smart_group_condition(chain, is_relevant_a1_cond, is_relevant_a1_cond);
-- assert...
end
do
local chain = createConditionAND();
chain.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 1));
chain.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 1));
chain.addVar(createCondition2DLinearInequalityLTEQ(0, 1, 1));
chain.addVar(createCondition2DLinearInequalityLTEQ(0, -1, 1));
do
local orCond = createConditionOR();
do
local andCond = createConditionAND();
andCond.addVar(createConditionC13DLowerBoundNonNeg(0, 0, 1, 0, "pos"));
andCond.addVar(createConditionC13DUpperBoundNonNeg(0, 0, 1, 1, "pos"));
andCond.addVar(createCondition2DLinearEquality(1, 0, 0));
andCond.addVar(createCondition2DLinearEquality(0, -1, 2/3));
orCond.addVar(andCond);
end
do
local andCond = createConditionAND();
andCond.addVar(createCondition2DLinearEquality(1, 0, 0));
andCond.addVar(createConditionC13DEqualityNonNeg(0, 0, 1, 0));
orCond.addVar(andCond);
end
do
local sub_andCond = createConditionAND();
do
local sub_orCond = createConditionOR();
do
local subsub_andCond = createConditionAND();
subsub_andCond.addVar(createConditionC13DLowerBoundNonNeg(0, 0, 1, 0, "pos"));
subsub_andCond.addVar(createConditionC13DUpperBoundNonNeg(3/2, 0, 0, 1/2, "pos"));
subsub_andCond.addVar(createCondition2DLinearInequalityLTEQ(1, 0, -1/3));
sub_orCond.addVar(subsub_andCond);
end
do
local subsub_andCond = createConditionAND();
subsub_andCond.addVar(createConditionC13DLowerBoundNonNeg(0, 0, 1, 0, "pos"));
subsub_andCond.addVar(createConditionC13DUpperBoundNonNeg(0, 0, 1, 1, "pos"));
subsub_andCond.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, 1/3));
sub_orCond.addVar(subsub_andCond);
end
sub_andCond.addVar(sub_orCond);
end
sub_andCond.addVar(createCondition2DLinearInequalityLT(1, 0, 0));
sub_andCond.addVar(createCondition2DLinearEquality(0, -1, 2/3));
orCond.addVar(sub_andCond);
end
do
local sub_andCond = createConditionAND();
sub_andCond.addVar(createCondition2DLinearInequalityLT(1, 0, 0));
sub_andCond.addVar(createConditionC13DEqualityNonNeg(0, 0, 1, 0));
orCond.addVar(sub_andCond);
end
do
local sub_andCond = createConditionAND();
do
local sub_orCond = createConditionOR();
do
local subsub_andCond = createConditionAND();
subsub_andCond.addVar(createConditionC13DLowerBoundNonNeg(0, 0, 1, 0, "pos"));
subsub_andCond.addVar(createConditionC13DUpperBoundNonNeg(3/2, 0, 0, -1/2, "neg"));
subsub_andCond.addVar(createCondition2DLinearInequalityLTEQ(-1, 0, -1/3));
sub_orCond.addVar(subsub_andCond);
end
do
local subsub_andCond = createConditionAND();
subsub_andCond.addVar(createConditionC13DLowerBoundNonNeg(0, 0, 1, 0, "pos"));
subsub_andCond.addVar(createConditionC13DUpperBoundNonNeg(0, 0, 1, 1, "pos"));
subsub_andCond.addVar(createCondition2DLinearInequalityLTEQ(1, 0, 1/3));
sub_orCond.addVar(subsub_andCond);
end
sub_andCond.addVar(sub_orCond);
end
sub_andCond.addVar(createCondition2DLinearInequalityLT(-1, 0, 0));
sub_andCond.addVar(createCondition2DLinearEquality(0, 1, -2/3));
orCond.addVar(sub_andCond);
end
do
local sub_andCond = createConditionAND();
sub_andCond.addVar(createCondition2DLinearInequalityLT(-1, 0, 0));
sub_andCond.addVar(createConditionC13DEqualityNonNeg(0, 0, 1, 0));
orCond.addVar(sub_andCond);
end
chain.addVar(orCond);
end
chain = smart_group_condition(chain, is_relevant_a1_cond, is_relevant_a1_cond);
-- assert...
end
end
tools_unit_test();