Debian Codegraph @

tests/code/testcases/testcastlecameras.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleCameras" -*-
2 {
3 Copyright 2004-2022 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 { Test CastleCameras unit. }
18 unit TestCastleCameras;
19
20 interface
21
22 uses
23 Classes, SysUtils, CastleTester;
24
25 type
26 TTestCameras = class(TCastleTestCase)
27 procedure TestToOrientationAndBack;
28 procedure TestOrientationFromBasicAxes;
29 procedure TestInput;
30 end;
31
32 implementation
33
34 uses CastleUtils, CastleVectors, X3DCameraUtils, Math, CastleCameras, CastleQuaternions,
35 CastleTransform;
36
37 procedure TTestCameras.TestToOrientationAndBack;
38
39 procedure CamOrientToDirUp(const Orient: TVector4; var Dir, Up: TVector3);
40 var OrientAxis: TVector3 absolute Orient;
41 begin
42 Dir := RotatePointAroundAxisRad(Orient[3], Vector3(0, 0, -1), OrientAxis);
43 Up := RotatePointAroundAxisRad(Orient[3], Vector3(0, 1, 0 ), OrientAxis);
44 end;
45
46 const
47 TestOrients: array[0..4]of TVector4 =
48 (
49 { orientacje z lbview }
50 (X: -1; Y: 0; Z: 0; W: 1.5708),
51 (X: -0.590284; Y: 0.769274; Z: 0.244504; W: 0.987861),
52 (X: 0; Y: 0; Z: 1; W: 0),
53 (X: 0; Y: 1; Z: 0; W: 1.5708),
54 { Here original orient.c failed, as far as I remember.
55 Looks like my OrientationFromDirectionUp fixes this. }
56 (X: 0.9133538007736206; Y: -0.23269428312778472; Z: -0.33412325382232666; W: 1.807408332824707)
57 );
58
59 EqEpsilon = 0.01; // larger epsilon for ppc64
60
61 var Dir, Up: TVector3;
62 i: Integer;
63 NewOrient: TVector4;
64 oohFailed: boolean;
65 begin
66 { Uzywam zmiennej oohFailed bo chce wykonac zawsze test dla wszystkich
67 wymienionych orientacji, nawet jesli niektore po drodze zawioda.
68 I tak bede chcial zobaczyc ktorym sie udalo.
69 Dopiero po wykonaniu wszystkich testow rzucam wyjatek jezeli jakis
70 test zakonczyl sie porazka. }
71 oohFailed := false;
72
73 for i := 0 to High(TestOrients) do
74 begin
75 CamOrientToDirUp(TestOrients[i], Dir, Up);
76 NewOrient := OrientationFromDirectionUp(Dir, Up);
77 if not ( (IsZero(NewOrient[3]) and IsZero(TestOrients[i][3])) or
78 TVector4.Equals(NewOrient, TestOrients[i], EqEpsilon) ) then
79 begin
80 Writeln(Format(
81 'failed z TestOrients[%d] = %s' +nl+
82 'Dir = %s' +nl+
83 'Up = %s' +nl+
84 'NewOrient = %s',
85 [ i,
86 TestOrients[i].ToString,
87 Dir.ToString,
88 Up.ToString,
89 NewOrient.ToString ]));
90 oohFailed := true;
91 end;
92 end;
93
94 AssertTrue(not oohFailed);
95 end;
96
97 procedure TTestCameras.TestOrientationFromBasicAxes;
98 const
99 Tests: array [1..18] of record Dir, Up: TVector3; end =
100 (
101 { +X up }
102 (Dir: (X: 0; Y: -1; Z: 0); Up: (X: 1; Y: 0; Z: 0)),
103 (Dir: (X: 0; Y: +1; Z: 0); Up: (X: 1; Y: 0; Z: 0)),
104 (Dir: (X: 0; Y: 0; Z: -1); Up: (X: 1; Y: 0; Z: 0)),
105 (Dir: (X: 0; Y: 0; Z: +1); Up: (X: 1; Y: 0; Z: 0)),
106
107 { +Y up }
108 (Dir: (X: -1; Y: 0; Z: 0); Up: (X: 0; Y: 1; Z: 0)),
109 (Dir: (X: +1; Y: 0; Z: 0); Up: (X: 0; Y: 1; Z: 0)),
110 (Dir: (X: 0; Y: 0; Z: -1); Up: (X: 0; Y: 1; Z: 0)),
111 (Dir: (X: 0; Y: 0; Z: +1); Up: (X: 0; Y: 1; Z: 0)),
112
113 { +Z up }
114 (Dir: (X: -1; Y: 0; Z: 0); Up: (X: 0; Y: 0; Z: 1)),
115 (Dir: (X: +1; Y: 0; Z: 0); Up: (X: 0; Y: 0; Z: 1)),
116 (Dir: (X: 0; Y: -1; Z: 0); Up: (X: 0; Y: 0; Z: 1)),
117 (Dir: (X: 0; Y: +1; Z: 0); Up: (X: 0; Y: 0; Z: 1)),
118
119 { From https://sourceforge.net/apps/phpbb/vrmlengine/viewtopic.php?f=3&t=35
120 The 3rd one fails. }
121 (Dir: (X: 0; Y: -1; Z: 1); Up: (X: 0; Y: 1; Z: 1)),
122 (Dir: (X: 0; Y: -0.71; Z: 0.71); Up: (X: 0; Y: 0.71; Z: 0.71)),
123 (Dir: (X: 0; Y: -0.7099999785423278; Z: 0.7099999785423278); Up: (X: 0; Y: 0.7100000381469726; Z: 0.7099999189376831)),
124
125 (Dir: (X: 7.9712307865520415E-008; Y: 0.410611480474472 ; Z: 0.9117947816848754); Up: (X: -3.5896810857138917E-008; Y: 0.9118096828460693; Z: -0.410611480474472)),
126 (Dir: (X: 0 ; Y: 0.410611480474472 ; Z: 0.9117947816848754); Up: (X: 0; Y: 0.9118096828460693; Z: -0.410611480474472)),
127 (Dir: (X: -0.0031079668551683 ; Y: -0.0010071427095681; Z: -0.9999946355819702); Up: (X: -0.0020249725785106 ; Y: 0.9999974370002747; Z: -0.0010008082026616))
128 );
129
130 procedure DoTest(const Dir, Up: TVector3; const TestName: string);
131 var
132 Orientation: TQuaternion;
133 begin
134 if not IsZero(TVector3.DotProduct(Dir, Up), 0.001) then
135 Fail(TestName + ': Given test dir/up not orthogonal, OrientationQuaternionFromDirectionUp assumes it');
136
137 Orientation := OrientationQuaternionFromDirectionUp(Dir, Up).Conjugate;
138 try
139 AssertTrue(TVector3.Equals(Orientation.Rotate(Dir.Normalize), DefaultX3DCameraDirection, 0.01));
140 AssertTrue(TVector3.Equals(Orientation.Rotate(Up .Normalize), DefaultX3DCameraUp , 0.01));
141 except
142 Writeln('Failed on ', TestName, '. Resulting dir is ',
143 Orientation.Rotate(Dir.Normalize).ToString,
144 ', resulting up is ',
145 Orientation.Rotate(Up.Normalize).ToString);
146 raise;
147 end;
148 end;
149
150 var
151 I: Integer;
152 begin
153 for I := Low(Tests) to High(Tests) do
154 DoTest(Tests[I].Dir, Tests[I].Up, Format('%d', [I]));
155
156 { Now negate the Up vectors, for additional tests }
157 for I := Low(Tests) to High(Tests) do
158 DoTest(Tests[I].Dir, -Tests[I].Up, Format('%d (negated up)', [I]));
159 end;
160
161 procedure TTestCameras.TestInput;
162
163 procedure AssertCamera(const C: TCamera; const Input: TCameraInputs;
164 const IgnoreAllInputs, MouseNavigation: boolean);
165 begin
166 AssertTrue(C.Input = Input);
167 end;
168
169 var
170 E: TExamineCamera;
171 W: TWalkCamera;
172 begin
173 E := TExamineCamera.Create(nil);
174 try
175 AssertCamera(E, TCamera.DefaultInput, false, true);
176 E.Input := [];
177 AssertCamera(E, [], true, false);
178 {$ifdef FPC}
179 {$warnings off}
180 { Consciously using here deprecated IgnoreAllInputs and MouseNavigation (to test it's still Ok) }
181 E.MouseNavigation := true;
182 AssertCamera(E, [ciMouseDragging], false, true);
183 E.IgnoreAllInputs := true;
184 {$warnings on}
185 {$endif}
186 AssertCamera(E, [], true, false);
187 E.Input := [ciNormal];
188 AssertCamera(E, [ciNormal], false, false);
189 finally FreeAndNil(E) end;
190
191 W := TWalkCamera.Create(nil);
192 try
193 AssertCamera(W, TCamera.DefaultInput, false, true);
194 W.Input := [];
195 AssertCamera(W, [], true, false);
196 W.Input := W.Input + [ciMouseDragging];
197 AssertCamera(W, [ciMouseDragging], false, true);
198 {$ifdef FPC}
199 {$warnings off}
200 { Consciously using here deprecated IgnoreAllInputs (to test it's still Ok) }
201 W.IgnoreAllInputs := true;
202 {$warnings on}
203 AssertCamera(W, [], true, false);
204 {$endif}
205 W.Input := [ciNormal];
206 AssertCamera(W, [ciNormal], false, false);
207 finally FreeAndNil(W) end;
208 end;
209
210 initialization
211 RegisterTest(TTestCameras);
212 end.