Debian Codegraph @

tests/code/testcases/testcastlefrustum.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleFrustum" -*-
2 {
3 Copyright 2005-2024 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 unit TestCastleFrustum;
18
19 interface
20
21 uses
22 Classes, SysUtils,
23 CastleTester, CastleVectors, CastleBoxes, CastleFrustum;
24
25 type
26 TTestCastleFrustum = class(TCastleTestCase)
27 private
28 procedure AssertFrustumSphereCollisionPossible(const Frustum: TFrustum;
29 const SphereCenter: TVector3; const SphereRadiusSqt: Single;
30 const GoodResult: TFrustumCollisionPossible);
31 procedure AssertFrustumBox3DCollisionPossible(const Frustum: TFrustum;
32 const Box3D: TBox3D; const GoodResult: TFrustumCollisionPossible);
33 published
34 procedure TestFrustum;
35 procedure TestInfiniteFrustum;
36 procedure TestCompareWithUnoptimizedPlaneCollision;
37 procedure TestTransformFrustum;
38 end;
39
40 implementation
41
42 uses Math,
43 CastleUtils, CastleTimeUtils, CastleProjection, CastleTransform, CastleLog;
44
45 function RandomFrustum(MakeZFarInfinity: boolean): TFrustum;
46
47 function RandomNonZeroVector(const Scale: Float): TVector3;
48 begin
49 repeat
50 Result.X := Random * Scale - Scale/2;
51 Result.Y := Random * Scale - Scale/2;
52 Result.Z := Random * Scale - Scale/2;
53 until not Result.IsPerfectlyZero;
54 end;
55
56 var
57 ZFar: Single;
58 begin
59 if MakeZFarInfinity then
60 ZFar := ZFarInfinity
61 else
62 ZFar := Random * 100 + 100;
63 Result.Init(
64 PerspectiveProjectionMatrixDeg(
65 Random * 30 + 60,
66 Random * 0.5 + 0.7,
67 Random * 5 + 1,
68 ZFar),
69 LookDirMatrix(
70 { Don't randomize camera pos too much, as we want some non-trivial
71 collisions with boxes generated by RandomBox. }
72 RandomNonZeroVector(10),
73 RandomNonZeroVector(1),
74 RandomNonZeroVector(1)));
75 end;
76
77 function RandomBox: TBox3D;
78 var
79 I: Integer;
80 Val1, Val2: Single;
81 begin
82 for I := 0 to 2 do
83 begin
84 Val1 := Random * 20 - 10;
85 Val2 := Random * 20 - 10;
86 OrderUp(Val1, Val2);
87 {$warnings off} // silence FPC warning about Normal uninitialized
88 Result.Data[0].Data[I] := Val1;
89 {$warnings on}
90 Result.Data[1].Data[I] := Val2;
91 end;
92 end;
93
94 procedure TTestCastleFrustum.AssertFrustumSphereCollisionPossible(const Frustum: TFrustum;
95 const SphereCenter: TVector3; const SphereRadiusSqt: Single;
96 const GoodResult: TFrustumCollisionPossible);
97 begin
98 AssertTrue( Frustum.SphereCollisionPossible(SphereCenter,
99 SphereRadiusSqt) = GoodResult);
100
101 AssertTrue( Frustum.SphereCollisionPossibleSimple(SphereCenter,
102 SphereRadiusSqt) = (GoodResult <> fcNoCollision) );
103 end;
104
105 procedure TTestCastleFrustum.AssertFrustumBox3DCollisionPossible(const Frustum: TFrustum;
106 const Box3D: TBox3D; const GoodResult: TFrustumCollisionPossible);
107 begin
108 AssertTrue( Frustum.Box3DCollisionPossible(Box3D) = GoodResult);
109
110 AssertTrue( Frustum.Box3DCollisionPossibleSimple(Box3D) =
111 (GoodResult <> fcNoCollision) );
112 end;
113
114 procedure TTestCastleFrustum.TestFrustum;
115 var
116 Frustum: TFrustum;
117 begin
118 { Calculate testing frustum }
119 Frustum.Init(
120 PerspectiveProjectionMatrixDeg(60, 1, 10, 100),
121 LookDirMatrix(
122 Vector3(10, 10, 10) { eye position },
123 Vector3(1, 0, 0) { look direction },
124 Vector3(0, 0, 1) { up vector } ));
125 AssertTrue(not Frustum.FarInfinity);
126
127 AssertFrustumSphereCollisionPossible(Frustum, Vector3(0, 0, 0), 81,
128 fcNoCollision);
129 { This is between camera pos and near plane }
130 AssertFrustumSphereCollisionPossible(Frustum, Vector3(0, 0, 0), 200,
131 fcNoCollision);
132 { This should collide with frustum, as it crosses near plane }
133 AssertFrustumSphereCollisionPossible(Frustum, Vector3(0, 0, 0), 420,
134 fcSomeCollisionPossible);
135 AssertFrustumSphereCollisionPossible(Frustum, Vector3(50, 10, 10), 1,
136 fcInsideFrustum);
137 { This sphere intersects near plane }
138 AssertFrustumSphereCollisionPossible(Frustum, Vector3(20, 10, 10), 1,
139 fcSomeCollisionPossible);
140
141 AssertFrustumBox3DCollisionPossible(Frustum, TBox3D.Empty, fcNoCollision);
142 AssertFrustumBox3DCollisionPossible(Frustum,
143 Box3D(Vector3(-1, -1, -1), Vector3(9, 9, 9)),
144 fcNoCollision);
145 AssertFrustumBox3DCollisionPossible(Frustum,
146 Box3D(Vector3(50, 10, 10), Vector3(51, 11, 11)),
147 fcInsideFrustum);
148 AssertFrustumBox3DCollisionPossible(Frustum,
149 Box3D(Vector3(19, 10, 10), Vector3(21, 11, 11)),
150 fcSomeCollisionPossible);
151 end;
152
153 procedure TTestCastleFrustum.TestInfiniteFrustum;
154 var
155 Frustum: TFrustum;
156 begin
157 Frustum.Init(
158 PerspectiveProjectionMatrixDeg(60, 1, 10, ZFarInfinity),
159 LookDirMatrix(
160 Vector3(10, 10, 10) { eye position },
161 Vector3(1, 0, 0) { look direction },
162 Vector3(0, 0, 1) { up vector } ));
163
164 AssertTrue(Frustum.Planes[fpFar].X = 0);
165 AssertTrue(Frustum.Planes[fpFar].Y = 0);
166 AssertTrue(Frustum.Planes[fpFar].Z = 0);
167 AssertTrue(Frustum.FarInfinity);
168
169 AssertFrustumSphereCollisionPossible(Frustum, Vector3(0, 0, 0), 81,
170 fcNoCollision);
171 AssertFrustumSphereCollisionPossible(Frustum, Vector3(100, 10, 10), 1,
172 fcInsideFrustum);
173 AssertFrustumSphereCollisionPossible(Frustum, Vector3(0, 0, 0), 400,
174 fcSomeCollisionPossible);
175 end;
176
177 procedure TTestCastleFrustum.TestCompareWithUnoptimizedPlaneCollision;
178 { Compare current Box3DCollisionPossible implementation with older
179 implementation that didn't use smart Box3DPlaneCollision,
180 instead was testing all 8 corners of bounding box.
181 This compares results (should be equal) and speed (hopefully, new
182 implementation is much faster!).
183 }
184 { $define WRITELN_TESTS}
185
186 function OldFrustumBox3DCollisionPossible(
187 const Frustum: TFrustum;
188 const Box: TBox3D): TFrustumCollisionPossible;
189
190 { Note: I tried to optimize this function,
191 since it's crucial for TOctree.EnumerateCollidingOctreeItems,
192 and this is crucial for TCastleScene.RenderFrustumOctree,
193 and this is crucial for overall speed of rendering. }
194
195 var
196 fp: TFrustumPlane;
197 FrustumMultiplyBox: TBox3D;
198
199 function CheckOutsideCorner(const XIndex, YIndex, ZIndex: Cardinal): boolean;
200 begin
201 Result :=
202 { Frustum[fp].X * Box[XIndex].X +
203 Frustum[fp].Y * Box[YIndex].Y +
204 Frustum[fp].Z * Box[ZIndex].Z +
205 optimized version : }
206 FrustumMultiplyBox.Data[XIndex].X +
207 FrustumMultiplyBox.Data[YIndex].Y +
208 FrustumMultiplyBox.Data[ZIndex].Z +
209 Frustum.Planes[fp][3] < 0;
210 end;
211
212 var
213 InsidePlanesCount: Cardinal;
214 LastPlane: TFrustumPlane;
215 begin
216 with Frustum do
217 begin
218 InsidePlanesCount := 0;
219
220 LastPlane := High(FP);
221 AssertTrue(LastPlane = fpFar);
222
223 { If the frustum has far plane in infinity, then ignore this plane.
224 Inc InsidePlanesCount, since the box is inside this infinite plane. }
225 if FarInfinity then
226 begin
227 LastPlane := Pred(LastPlane);
228 Inc(InsidePlanesCount);
229 end;
230
231 { The logic goes like this:
232 if box is on the "outside" of *any* of 6 planes, result is NoCollision
233 if box is on the "inside" of *all* 6 planes, result is InsideFrustum
234 else SomeCollisionPossible. }
235
236 for fp := Low(fp) to LastPlane do
237 begin
238 { This way I need 6 multiplications instead of 8*3=24
239 (in case I would have to execute CheckOutsideCorner 8 times) }
240 FrustumMultiplyBox.Data[0].X := Planes[fp].X * Box.Data[0].X;
241 FrustumMultiplyBox.Data[0].Y := Planes[fp].Y * Box.Data[0].Y;
242 FrustumMultiplyBox.Data[0].Z := Planes[fp].Z * Box.Data[0].Z;
243 FrustumMultiplyBox.Data[1].X := Planes[fp].X * Box.Data[1].X;
244 FrustumMultiplyBox.Data[1].Y := Planes[fp].Y * Box.Data[1].Y;
245 FrustumMultiplyBox.Data[1].Z := Planes[fp].Z * Box.Data[1].Z;
246
247 { I'm splitting code below to two possilibilities.
248 This way I can calculate 7 remaining CheckOutsideCorner
249 calls using code like
250 "... and ... and ..."
251 or
252 "... or ... or ..."
253 , and this means that short-circuit boolean evaluation
254 may usually reduce number of needed CheckOutsideCorner calls
255 (i.e. I will not need to actually call CheckOutsideCorner 8 times
256 per frustum plane). }
257
258 if CheckOutsideCorner(0, 0, 0) then
259 begin
260 if CheckOutsideCorner(0, 0, 1) and
261 CheckOutsideCorner(0, 1, 0) and
262 CheckOutsideCorner(0, 1, 1) and
263 CheckOutsideCorner(1, 0, 0) and
264 CheckOutsideCorner(1, 0, 1) and
265 CheckOutsideCorner(1, 1, 0) and
266 CheckOutsideCorner(1, 1, 1) then
267 { All 8 corners outside }
268 Exit(fcNoCollision);
269 end else
270 begin
271 if not (
272 CheckOutsideCorner(0, 0, 1) or
273 CheckOutsideCorner(0, 1, 0) or
274 CheckOutsideCorner(0, 1, 1) or
275 CheckOutsideCorner(1, 0, 0) or
276 CheckOutsideCorner(1, 0, 1) or
277 CheckOutsideCorner(1, 1, 0) or
278 CheckOutsideCorner(1, 1, 1) ) then
279 { All 8 corners inside }
280 Inc(InsidePlanesCount);
281 end;
282 end;
283
284 if InsidePlanesCount = 6 then
285 Result := fcInsideFrustum else
286 Result := fcSomeCollisionPossible;
287 end;
288 end;
289
290 function OldFrustumBox3DCollisionPossibleSimple(
291 const Frustum: TFrustum;
292 const Box: TBox3D): boolean;
293
294 { Implementation is obviously based on
295 FrustumBox3DCollisionPossible above, see there for more comments. }
296
297 var
298 fp: TFrustumPlane;
299 FrustumMultiplyBox: TBox3D;
300
301 function CheckOutsideCorner(const XIndex, YIndex, ZIndex: Cardinal): boolean;
302 begin
303 Result :=
304 { Planes[fp].X * Box[XIndex].X +
305 Planes[fp].Y * Box[YIndex].Y +
306 Planes[fp].Z * Box[ZIndex].Z +
307 optimized version : }
308 FrustumMultiplyBox.Data[XIndex].X +
309 FrustumMultiplyBox.Data[YIndex].Y +
310 FrustumMultiplyBox.Data[ZIndex].Z +
311 Frustum.Planes[fp][3] < 0;
312 end;
313
314 var
315 LastPlane: TFrustumPlane;
316 begin
317 with Frustum do
318 begin
319 LastPlane := High(FP);
320 AssertTrue(LastPlane = fpFar);
321
322 { If the frustum has far plane in infinity, then ignore this plane. }
323 if FarInfinity then
324 LastPlane := Pred(LastPlane);
325
326 for fp := Low(fp) to LastPlane do
327 begin
328 { This way I need 6 multiplications instead of 8*3=24 }
329 FrustumMultiplyBox.Data[0].X := Planes[fp].X * Box.Data[0].X;
330 FrustumMultiplyBox.Data[0].Y := Planes[fp].Y * Box.Data[0].Y;
331 FrustumMultiplyBox.Data[0].Z := Planes[fp].Z * Box.Data[0].Z;
332 FrustumMultiplyBox.Data[1].X := Planes[fp].X * Box.Data[1].X;
333 FrustumMultiplyBox.Data[1].Y := Planes[fp].Y * Box.Data[1].Y;
334 FrustumMultiplyBox.Data[1].Z := Planes[fp].Z * Box.Data[1].Z;
335
336 if CheckOutsideCorner(0, 0, 0) and
337 CheckOutsideCorner(0, 0, 1) and
338 CheckOutsideCorner(0, 1, 0) and
339 CheckOutsideCorner(0, 1, 1) and
340 CheckOutsideCorner(1, 0, 0) and
341 CheckOutsideCorner(1, 0, 1) and
342 CheckOutsideCorner(1, 1, 0) and
343 CheckOutsideCorner(1, 1, 1) then
344 Exit(false);
345 end;
346
347 Result := true;
348 end;
349 end;
350
351 const
352 Tests = {$ifdef WRITELN_TESTS} 1000000 {$else} 1000 {$endif};
353 var
354 TestCases: array of record
355 Frustum: TFrustum;
356 Box: TBox3D;
357 Result1: TFrustumCollisionPossible;
358 Result2: boolean;
359 end;
360 I: Integer;
361 NoOutsideResults: Cardinal;
362 begin
363 SetLength(TestCases, Tests);
364
365 for I := 0 to Tests - 1 do
366 with TestCases[I] do
367 begin
368 Frustum := RandomFrustum(I > Tests div 2);
369 Box := RandomBox;
370 end;
371
372 {$ifdef WRITELN_TESTS} ProcessTimerBegin; {$endif}
373 for I := 0 to Tests - 1 do
374 with TestCases[I] do
375 begin
376 Result1 := OldFrustumBox3DCollisionPossible(Frustum, Box);
377 end;
378 {$ifdef WRITELN_TESTS}
379 Writeln('Old TFrustum.Box3DCollisionPossible: ', ProcessTimerEnd);
380 {$endif}
381
382 {$ifdef WRITELN_TESTS} ProcessTimerBegin; {$endif}
383 for I := 0 to Tests - 1 do
384 with TestCases[I] do
385 begin
386 AssertTrue(Result1 = Frustum.Box3DCollisionPossible(Box));
387 end;
388 {$ifdef WRITELN_TESTS}
389 Writeln('New TFrustum.Box3DCollisionPossible: ', ProcessTimerEnd);
390 {$endif}
391
392 {$ifdef WRITELN_TESTS} ProcessTimerBegin; {$endif}
393 for I := 0 to Tests - 1 do
394 with TestCases[I] do
395 begin
396 Result2 := OldFrustumBox3DCollisionPossibleSimple(Frustum, Box);
397 end;
398 {$ifdef WRITELN_TESTS}
399 Writeln('Old TFrustum.Box3DCollisionPossibleSimple: ', ProcessTimerEnd);
400 {$endif}
401
402 {$ifdef WRITELN_TESTS} ProcessTimerBegin; {$endif}
403 for I := 0 to Tests - 1 do
404 with TestCases[I] do
405 begin
406 AssertTrue(Result2 = Frustum.Box3DCollisionPossibleSimple(Box));
407 end;
408 {$ifdef WRITELN_TESTS}
409 Writeln('New TFrustum.Box3DCollisionPossibleSimple: ', ProcessTimerEnd);
410 {$endif}
411
412 {$ifdef WRITELN_TESTS}
413
414 NoOutsideResults := 0;
415
416 for I := 0 to Tests - 1 do
417 with TestCases[I] do
418 if Result1 <> fcNoCollision then
419 Inc(NoOutsideResults);
420
421 { How much the random data resembles real-life data, in real-life
422 we may get something significant like 1/6 }
423 Writeln('Ratio of non-outside results: ', (NoOutsideResults/Tests):1:10);
424
425 {$endif}
426 end;
427
428 procedure TTestCastleFrustum.TestTransformFrustum;
429
430 {$define TEST_FRUSTUM_TRANSFORM_SPEED}
431 {$ifdef TEST_FRUSTUM_TRANSFORM_SPEED}
432 procedure DoTestSpeed;
433 const
434 TestsCount = 1000 * 1000;
435 var
436 Frustum1{, Frustum2, Frustum3}: TFrustum;
437 M, MInverse: TMatrix4;
438 T: TTimerResult;
439 I: Integer;
440 begin
441 Frustum1.Init(
442 PerspectiveProjectionMatrixDeg(60, 1, 10, ZFarInfinity),
443 LookDirMatrix(
444 Vector3(10, 10, 10) { eye position },
445 Vector3(1, 0, 0) { look direction },
446 Vector3(0, 0, 1) { up vector } ));
447
448 M := TMatrix4.Identity;
449 MInverse := TMatrix4.Identity;
450 TransformMatricesMult(M, MInverse,
451 Vector3(1, 2, 3),
452 Vector4(4, 5, 6, 7),
453 Vector3(8, 9, 10),
454 Vector4(11, 12, 13, 14),
455 Vector3(15, 16, 17));
456
457 T := Timer;
458 for I := 1 to TestsCount do
459 Frustum1.TransformByInverse(MInverse);
460 WritelnLog('Time of TFrustum.TransformByInverse: %f', [T.ElapsedTime]);
461
462 T := Timer;
463 for I := 1 to TestsCount do
464 Frustum1.Transform(M);
465 WritelnLog('Time of TFrustum.Transform: %f', [T.ElapsedTime]);
466 end;
467 {$endif}
468
469 var
470 Frustum1{, Frustum2, Frustum3}: TFrustum;
471 M, MInverse: TMatrix4;
472 begin
473 Frustum1.Init(
474 PerspectiveProjectionMatrixDeg(60, 1, 10, ZFarInfinity),
475 LookDirMatrix(
476 Vector3(10, 10, 10) { eye position },
477 Vector3(1, 0, 0) { look direction },
478 Vector3(0, 0, 1) { up vector } ));
479
480 M := TMatrix4.Identity;
481 MInverse := TMatrix4.Identity;
482
483 AssertFrustumEquals(Frustum1, Frustum1.Transform(M));
484 AssertFrustumEquals(Frustum1, Frustum1.TransformByInverse(MInverse));
485
486 TransformMatricesMult(M, MInverse,
487 Vector3(0, 0, 0),
488 Vector4(0, 0, 0, 0),
489 Vector3(1, 1, 1),
490 Vector4(0, 0, 0, 0),
491 Vector3(15, 16, 17));
492 AssertFrustumEquals(Frustum1.TransformByInverse(MInverse), Frustum1.Transform(M));
493
494 M := TMatrix4.Identity;
495 MInverse := TMatrix4.Identity;
496 TransformMatricesMult(M, MInverse,
497 Vector3(1, 2, 3),
498 Vector4(4, 5, 6, 7),
499 Vector3(1, 1, 1),
500 Vector4(0, 0, 0, 0),
501 Vector3(15, 16, 17));
502 AssertFrustumEquals(Frustum1.TransformByInverse(MInverse), Frustum1.Transform(M));
503
504 M := TMatrix4.Identity;
505 MInverse := TMatrix4.Identity;
506 TransformMatricesMult(M, MInverse,
507 Vector3(1, 2, 3),
508 Vector4(4, 5, 6, 7),
509 Vector3(10, 10, 10),
510 Vector4(0, 0, 0, 0),
511 Vector3(15, 16, 17));
512 AssertFrustumEquals(Frustum1.TransformByInverse(MInverse), Frustum1.Transform(M));
513
514 M := TMatrix4.Identity;
515 MInverse := TMatrix4.Identity;
516 TransformMatricesMult(M, MInverse,
517 Vector3(1, 2, 3),
518 Vector4(4, 5, 6, 7),
519 Vector3(10, 20, 30),
520 Vector4(0, 0, 0, 0),
521 Vector3(15, 16, 17));
522 // when using non-uniform scaling, we need larger epsilon to pass
523 AssertFrustumEquals(Frustum1.TransformByInverse(MInverse), Frustum1.Transform(M), 0.75);
524
525 M := TMatrix4.Identity;
526 MInverse := TMatrix4.Identity;
527 TransformMatricesMult(M, MInverse,
528 Vector3(1, 2, 3),
529 Vector4(4, 5, 6, 7),
530 Vector3(8, 9, 10),
531 Vector4(11, 12, 13, 14),
532 Vector3(15, 16, 17));
533 // when using non-uniform scaling, we need larger epsilon to pass
534 AssertFrustumEquals(Frustum1.TransformByInverse(MInverse), Frustum1.Transform(M), 1.0);
535
536 {$ifdef TEST_FRUSTUM_TRANSFORM_SPEED}
537 DoTestSpeed;
538 {$endif}
539 end;
540
541 initialization
542 RegisterTest(TTestCastleFrustum);
543 end.