Debian Codegraph @

tests/code/testcases/testcastlequaternions.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCastleQuaternions" -*-
2 {
3 Copyright 2018-2023 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 { Tests of CastleQuaternions unit. }
18 unit TestCastleQuaternions;
19
20 interface
21
22 uses
23 Classes, SysUtils,
24 CastleTester, CastleVectors, CastleQuaternions;
25
26 type
27 TTestCastleQuaternions = class(TCastleTestCase)
28 published
29 procedure TestRotationMatrix;
30 procedure TestDecompose;
31 procedure TestDecomposeCastleTransform;
32 procedure TestSlerpEpsilon;
33 end;
34
35 implementation
36
37 uses CastleTransform;
38
39 procedure TTestCastleQuaternions.TestRotationMatrix;
40 var
41 NewQ, Q: TQuaternion;
42 M: TMatrix3;
43 begin
44 Q := QuatFromAxisAngle(Vector3(1, 2, 3), 6.666, true);
45 M := Q.ToRotationMatrix3;
46 NewQ := QuatFromRotationMatrix(M);
47
48 // Writeln(Q.Data.Vector4.ToString);
49 // Writeln(NewQ.Data.Vector4.ToString);
50
51 // Writeln(Q.ToAxisAngle.ToString);
52 // Writeln(NewQ.ToAxisAngle.ToString);
53
54 // Writeln(Q.Rotate(Vector3(30, 20, 10)).ToString);
55 // Writeln(NewQ.Rotate(Vector3(30, 20, 10)).ToString);
56
57 { easy way to check that quaternions are equal: check that they rotate
58 some point in the same way. }
59 AssertVectorEquals(
60 Q.Rotate(Vector3(30, 20, 10)),
61 NewQ.Rotate(Vector3(30, 20, 10)));
62 end;
63
64 procedure TTestCastleQuaternions.TestDecompose;
65 var
66 M: TMatrix4;
67 Translation, Scale: TVector3;
68 Rotation: TVector4;
69 begin
70 { The order is the same as for @link(TTransformation,Multiply),
71 and same as done by TCastleTransform: first translation matrix,
72 multiplied by rotation matrix,
73 multiplied by scaling matrix. }
74 M := TranslationMatrix(Vector3(44, 100, 200)) *
75 RotationMatrixRad(6.66, Vector3(1, 2, 3).Normalize) *
76 ScalingMatrix(Vector3(9, 8, 7));
77
78 MatrixDecompose(M, Translation, Rotation, Scale);
79
80 AssertVectorEquals(Translation, Vector3(44, 100, 200), 0.1);
81 AssertVectorEquals(Rotation.XYZ, Vector3(1, 2, 3).Normalize, 0.1);
82 AssertVectorEquals(Scale, Vector3(9, 8, 7), 0.1);
83
84 // Writeln(Translation.ToString);
85 // Writeln(Rotation.ToString);
86 // Writeln(Scale.ToString);
87 end;
88
89 procedure TTestCastleQuaternions.TestDecomposeCastleTransform;
90 var
91 M: TMatrix4;
92 Translation, Scale: TVector3;
93 Rotation: TVector4;
94 Transform: TCastleTransform;
95 begin
96 M :=
97 TranslationMatrix(Vector3(1, 2, 3)) *
98 RotationMatrixRad(6.66, Vector3(5, 6, 7).Normalize) *
99 ScalingMatrix(Vector3(9, 8, 7)) *
100 TranslationMatrix(Vector3(10, 20, 30)) *
101 RotationMatrixRad(6.66, Vector3(50, 60, 70).Normalize) *
102 ScalingMatrix(Vector3(9, 8, 7));
103
104 MatrixDecompose(M, Translation, Rotation, Scale);
105
106 Transform := TCastleTransform.Create(nil);
107 try
108 Transform.Translation := Translation;
109 Transform.Rotation := Rotation;
110 Transform.Scale := Scale;
111 AssertMatrixEquals(Transform.Transform, M, 5);
112 finally FreeAndNil(Transform) end;
113 end;
114
115 procedure TTestCastleQuaternions.TestSlerpEpsilon;
116
117 { For some particular inputs, SLerp could calculate wrong results if the Epsilon in SLerp
118 will be too large. Below are invalid calculations (with Epsilon = 1E-3)
119 vs valid (Epsilon = 1E-6) on glTF model from https://github.com/castle-engine/castle-engine/issues/342 .
120
121 SinTheta 0.00048828122090000000
122 A 0.19590963423252106
123 -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
124 good: 0.49978336691856384 0.49977511167526245 0.50022482872009277 -0.50021666288375854
125 bad : -0.30385482311248779 -0.30387380719184875 -0.30430680513381958 0.30432581901550293
126 DIFF!
127
128 SinTheta 0.00048828122090000000
129 A 0.5079689621925354
130 -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
131 good: 0.499886155128479 0.49985414743423462 0.50014585256576538 -0.50011396408081055
132 bad : 0.0081317871809005737 0.008093222975730896 0.0078447908163070679 -0.007806241512298584
133 DIFF!
134
135 SinTheta 0.00048828122090000000
136 A 0.7983090877532959
137 -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
138 good: 0.49998176097869873 0.49992763996124268 0.50007236003875732 -0.50001835823059082
139 bad : 0.29840424656867981 0.29834744334220886 0.29827073216438293 -0.29821401834487915
140 DIFF!
141
142 SinTheta 0.00069053389600000000
143 A 0.054238788783550262
144 0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
145 good: -0.50009214878082275 -0.50008833408355713 -0.49991172552108765 0.49990791082382202
146 bad : 0.44588500261306763 0.44588103890419006 0.44564139842987061 -0.44563743472099304
147 DIFF!
148
149 SinTheta 0.00069053389600000000
150 A 0.3701384961605072
151 0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
152 good: -0.49996381998062134 -0.49996179342269897 -0.50003820657730103 0.50003618001937866
153 bad : 0.13004148006439209 0.13003829121589661 0.12968471646308899 -0.12968157231807709
154 DIFF!
155
156 SinTheta 0.00069053389600000000
157 A 0.68378734588623047
158 0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
159 good: -0.49983644485473633 -0.49983620643615723 -0.50016379356384277 0.50016355514526367
160 bad : -0.18355154991149902 -0.18355391919612885 -0.18402071297168732 0.18402302265167236
161 DIFF!
162
163 SinTheta 0.00069053389600000000
164 A 0.9893985390663147
165 0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
166 good: -0.49971228837966919 -0.49971377849578857 -0.50028616189956665 0.50028759241104126
167 bad : -0.48910835385322571 -0.48910996317863464 -0.48968702554702759 0.48968854546546936
168 DIFF!
169
170 These were calculated by test code in SLerp that detects when result of 2 branches
171 would be different:
172
173 if (SinTheta <= 0.001) and (SinTheta > 1E-6) then
174 begin
175 W1 := NegateOneQuaternion * Sin( (1-A) * Theta ) / SinTheta;
176 W2 := Sin( A * Theta ) / SinTheta;
177 Good.Data.Vector4 := (Q1.Data.Vector4 * W1) + (Q2.Data.Vector4 * W2);
178
179 W1 := 1 - A;
180 W2 := A;
181 Bad.Data.Vector4 := (Q1.Data.Vector4 * W1) + (Q2.Data.Vector4 * W2);
182
183 if not TVector4.Equals(Bad.Data.Vector4, Good.Data.Vector4) then
184 begin
185 Writeln('SinTheta ', SinTheta:1:20);
186 Writeln(Format('A %g', [A]));
187 Writeln(Format('%s %s', [Q1.Data.Vector4.ToRawString, Q2.Data.Vector4.ToRawString]));
188 Writeln('good: ', Good.Data.Vector4.ToRawString);
189 Writeln('bad : ', Bad.Data.Vector4.ToRawString);
190 Writeln('DIFF!');
191 end;
192 end;
193 }
194
195 var
196 A: Single;
197 Q1, Q2: TQuaternion;
198 begin
199 A := 0.19590963423252106;
200 Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
201 Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
202 AssertVectorEquals(Vector4(0.49978336691856384, 0.49977511167526245, 0.50022482872009277, -0.50021666288375854),
203 SLerp(A, Q1, Q2).Data.Vector4,
204 0.01);
205
206 A := 0.5079689621925354;
207 Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
208 Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
209 AssertVectorEquals(Vector4(0.499886155128479, 0.49985414743423462, 0.50014585256576538, -0.50011396408081055),
210 SLerp(A, Q1, Q2).Data.Vector4,
211 0.01);
212
213 A := 0.7983090877532959;
214 Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
215 Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
216 AssertVectorEquals(Vector4(0.49998176097869873, 0.49992763996124268, 0.50007236003875732, -0.50001835823059082),
217 SLerp(A, Q1, Q2).Data.Vector4,
218 0.01);
219
220 A := 0.054238788783550262;
221 Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
222 Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
223 AssertVectorEquals(Vector4(-0.50009214878082275, -0.50008833408355713, -0.49991172552108765, 0.49990791082382202),
224 SLerp(A, Q1, Q2).Data.Vector4,
225 0.01);
226
227 A := 0.3701384961605072;
228 Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
229 Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
230 AssertVectorEquals(Vector4(-0.49996381998062134, -0.49996179342269897, -0.50003820657730103, 0.50003618001937866),
231 SLerp(A, Q1, Q2).Data.Vector4,
232 0.01);
233
234 A := 0.68378734588623047;
235 Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
236 Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
237 AssertVectorEquals(Vector4(-0.49983644485473633, -0.49983620643615723, -0.50016379356384277, 0.50016355514526367),
238 SLerp(A, Q1, Q2).Data.Vector4,
239 0.01);
240
241 A := 0.9893985390663147;
242 Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
243 Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
244 AssertVectorEquals(Vector4(-0.49971228837966919, -0.49971377849578857, -0.50028616189956665, 0.50028759241104126),
245 SLerp(A, Q1, Q2).Data.Vector4,
246 0.01);
247 end;
248
249 initialization
250 RegisterTest(TTestCastleQuaternions);
251 end.