File: testcastlequaternions.pas

package info (click to toggle)
castle-game-engine 7.0~alpha.3%2Bdfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 969,476 kB
  • sloc: pascal: 911,523; javascript: 28,186; cpp: 14,157; xml: 9,939; ansic: 9,229; java: 3,653; objc: 2,737; sh: 1,214; makefile: 657; php: 65; lisp: 21; ruby: 8
file content (251 lines) | stat: -rw-r--r-- 10,557 bytes parent folder | download | duplicates (2)
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
// -*- compile-command: "./test_single_testcase.sh TTestCastleQuaternions" -*-
{
  Copyright 2018-2023 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ Tests of CastleQuaternions unit. }
unit TestCastleQuaternions;

interface

uses
  Classes, SysUtils,
  CastleTester, CastleVectors, CastleQuaternions;

type
  TTestCastleQuaternions = class(TCastleTestCase)
  published
    procedure TestRotationMatrix;
    procedure TestDecompose;
    procedure TestDecomposeCastleTransform;
    procedure TestSlerpEpsilon;
  end;

implementation

uses CastleTransform;

procedure TTestCastleQuaternions.TestRotationMatrix;
var
  NewQ, Q: TQuaternion;
  M: TMatrix3;
begin
  Q := QuatFromAxisAngle(Vector3(1, 2, 3), 6.666, true);
  M := Q.ToRotationMatrix3;
  NewQ := QuatFromRotationMatrix(M);

  // Writeln(Q.Data.Vector4.ToString);
  // Writeln(NewQ.Data.Vector4.ToString);

  // Writeln(Q.ToAxisAngle.ToString);
  // Writeln(NewQ.ToAxisAngle.ToString);

  // Writeln(Q.Rotate(Vector3(30, 20, 10)).ToString);
  // Writeln(NewQ.Rotate(Vector3(30, 20, 10)).ToString);

  { easy way to check that quaternions are equal: check that they rotate
    some point in the same way. }
  AssertVectorEquals(
    Q.Rotate(Vector3(30, 20, 10)),
    NewQ.Rotate(Vector3(30, 20, 10)));
end;

procedure TTestCastleQuaternions.TestDecompose;
var
  M: TMatrix4;
  Translation, Scale: TVector3;
  Rotation: TVector4;
begin
  { The order is the same as for @link(TTransformation,Multiply),
    and same as done by TCastleTransform: first translation matrix,
    multiplied by rotation matrix,
    multiplied by scaling matrix. }
  M := TranslationMatrix(Vector3(44, 100, 200)) *
    RotationMatrixRad(6.66, Vector3(1, 2, 3).Normalize) *
    ScalingMatrix(Vector3(9, 8, 7));

  MatrixDecompose(M, Translation, Rotation, Scale);

  AssertVectorEquals(Translation, Vector3(44, 100, 200), 0.1);
  AssertVectorEquals(Rotation.XYZ, Vector3(1, 2, 3).Normalize, 0.1);
  AssertVectorEquals(Scale, Vector3(9, 8, 7), 0.1);

  // Writeln(Translation.ToString);
  // Writeln(Rotation.ToString);
  // Writeln(Scale.ToString);
end;

procedure TTestCastleQuaternions.TestDecomposeCastleTransform;
var
  M: TMatrix4;
  Translation, Scale: TVector3;
  Rotation: TVector4;
  Transform: TCastleTransform;
begin
  M :=
    TranslationMatrix(Vector3(1, 2, 3)) *
    RotationMatrixRad(6.66, Vector3(5, 6, 7).Normalize) *
    ScalingMatrix(Vector3(9, 8, 7)) *
    TranslationMatrix(Vector3(10, 20, 30)) *
    RotationMatrixRad(6.66, Vector3(50, 60, 70).Normalize) *
    ScalingMatrix(Vector3(9, 8, 7));

  MatrixDecompose(M, Translation, Rotation, Scale);

  Transform := TCastleTransform.Create(nil);
  try
    Transform.Translation := Translation;
    Transform.Rotation := Rotation;
    Transform.Scale := Scale;
    AssertMatrixEquals(Transform.Transform, M, 5);
  finally FreeAndNil(Transform) end;
end;

procedure TTestCastleQuaternions.TestSlerpEpsilon;

{ For some particular inputs, SLerp could calculate wrong results if the Epsilon in SLerp
  will be too large. Below are invalid calculations (with Epsilon = 1E-3)
  vs valid (Epsilon = 1E-6) on glTF model from https://github.com/castle-engine/castle-engine/issues/342 .

    SinTheta 0.00048828122090000000
    A 0.19590963423252106
    -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
    good: 0.49978336691856384 0.49977511167526245 0.50022482872009277 -0.50021666288375854
    bad : -0.30385482311248779 -0.30387380719184875 -0.30430680513381958 0.30432581901550293
    DIFF!

    SinTheta 0.00048828122090000000
    A 0.5079689621925354
    -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
    good: 0.499886155128479 0.49985414743423462 0.50014585256576538 -0.50011396408081055
    bad : 0.0081317871809005737 0.008093222975730896 0.0078447908163070679 -0.007806241512298584
    DIFF!

    SinTheta 0.00048828122090000000
    A 0.7983090877532959
    -0.49971881508827209 -0.49972546100616455 -0.50027436017990112 0.50028109550476074 0.50004816055297852 0.4999786913394928 0.50002127885818481 -0.49995192885398865
    good: 0.49998176097869873 0.49992763996124268 0.50007236003875732 -0.50001835823059082
    bad : 0.29840424656867981 0.29834744334220886 0.29827073216438293 -0.29821401834487915
    DIFF!

    SinTheta 0.00069053389600000000
    A 0.054238788783550262
    0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
    good: -0.50009214878082275 -0.50008833408355713 -0.49991172552108765 0.49990791082382202
    bad : 0.44588500261306763 0.44588103890419006 0.44564139842987061 -0.44563743472099304
    DIFF!

    SinTheta 0.00069053389600000000
    A 0.3701384961605072
    0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
    good: -0.49996381998062134 -0.49996179342269897 -0.50003820657730103 0.50003618001937866
    bad : 0.13004148006439209 0.13003829121589661 0.12968471646308899 -0.12968157231807709
    DIFF!

    SinTheta 0.00069053389600000000
    A 0.68378734588623047
    0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
    good: -0.49983644485473633 -0.49983620643615723 -0.50016379356384277 0.50016355514526367
    bad : -0.18355154991149902 -0.18355391919612885 -0.18402071297168732 0.18402302265167236
    DIFF!

    SinTheta 0.00069053389600000000
    A 0.9893985390663147
    0.50011414289474487 0.50011003017425537 0.49988996982574463 -0.49988585710525513 -0.49970793724060059 -0.49970951676368713 -0.5002903938293457 0.50029188394546509
    good: -0.49971228837966919 -0.49971377849578857 -0.50028616189956665 0.50028759241104126
    bad : -0.48910835385322571 -0.48910996317863464 -0.48968702554702759 0.48968854546546936
    DIFF!

  These were calculated by test code in SLerp that detects when result of 2 branches
  would be different:

    if (SinTheta <= 0.001) and (SinTheta > 1E-6) then
    begin
      W1 := NegateOneQuaternion * Sin( (1-A) * Theta ) / SinTheta;
      W2 :=                       Sin(    A  * Theta ) / SinTheta;
      Good.Data.Vector4 := (Q1.Data.Vector4 * W1) + (Q2.Data.Vector4 * W2);

      W1 := 1 - A;
      W2 := A;
      Bad.Data.Vector4 := (Q1.Data.Vector4 * W1) + (Q2.Data.Vector4 * W2);

      if not TVector4.Equals(Bad.Data.Vector4, Good.Data.Vector4) then
      begin
        Writeln('SinTheta ', SinTheta:1:20);
        Writeln(Format('A %g', [A]));
        Writeln(Format('%s %s', [Q1.Data.Vector4.ToRawString, Q2.Data.Vector4.ToRawString]));
        Writeln('good: ', Good.Data.Vector4.ToRawString);
        Writeln('bad : ', Bad.Data.Vector4.ToRawString);
        Writeln('DIFF!');
      end;
    end;
}

var
  A: Single;
  Q1, Q2: TQuaternion;
begin
  A := 0.19590963423252106;
  Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
  Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
  AssertVectorEquals(Vector4(0.49978336691856384, 0.49977511167526245, 0.50022482872009277, -0.50021666288375854),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.5079689621925354;
  Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
  Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
  AssertVectorEquals(Vector4(0.499886155128479, 0.49985414743423462, 0.50014585256576538, -0.50011396408081055),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.7983090877532959;
  Q1.Data.Vector4 := Vector4(-0.49971881508827209, -0.49972546100616455, -0.50027436017990112, 0.50028109550476074);
  Q2.Data.Vector4 := Vector4(0.50004816055297852, 0.4999786913394928, 0.50002127885818481, -0.49995192885398865);
  AssertVectorEquals(Vector4(0.49998176097869873, 0.49992763996124268, 0.50007236003875732, -0.50001835823059082),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.054238788783550262;
  Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
  Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
  AssertVectorEquals(Vector4(-0.50009214878082275, -0.50008833408355713, -0.49991172552108765, 0.49990791082382202),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.3701384961605072;
  Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
  Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
  AssertVectorEquals(Vector4(-0.49996381998062134, -0.49996179342269897, -0.50003820657730103, 0.50003618001937866),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.68378734588623047;
  Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
  Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
  AssertVectorEquals(Vector4(-0.49983644485473633, -0.49983620643615723, -0.50016379356384277, 0.50016355514526367),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);

  A := 0.9893985390663147;
  Q1.Data.Vector4 := Vector4(0.50011414289474487, 0.50011003017425537, 0.49988996982574463, -0.49988585710525513);
  Q2.Data.Vector4 := Vector4(-0.49970793724060059, -0.49970951676368713, -0.5002903938293457, 0.50029188394546509);
  AssertVectorEquals(Vector4(-0.49971228837966919, -0.49971377849578857, -0.50028616189956665, 0.50028759241104126),
    SLerp(A, Q1, Q2).Data.Vector4,
    0.01);
end;

initialization
  RegisterTest(TTestCastleQuaternions);
end.