Debian Codegraph @

tests/code/testcases/testcastlecubemaps.pas

raw
1 // -*- compile-command: "./test_single_testcase.sh TTestCubeMap" -*-
2 {
3 Copyright 2008-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 unit TestCastleCubeMaps;
18
19 interface
20
21 uses
22 Classes, SysUtils, Generics.Collections, CastleTester;
23
24 type
25 TTestCubeMap = class(TCastleTestCase)
26 published
27 procedure TestReverse;
28 procedure TestCubeMapSolidAngle;
29 end;
30
31 implementation
32
33 uses CastleVectors, CastleInternalCubeMaps, Math;
34
35 procedure TTestCubeMap.TestReverse;
36 var
37 Side, NewSide: TCubeMapSide;
38 Pixel, NewPixel: Cardinal;
39 Dir: TVector3;
40 begin
41 for Side := Low(Side) to High(Side) do
42 for Pixel := 0 to Sqr(CubeMapSize) - 1 do
43 begin
44 Dir := CubeMapDirection(Side, Pixel);
45 DirectionToCubeMap(Dir, NewSide, NewPixel);
46 AssertTrue(NewSide = Side);
47 AssertTrue(NewPixel = Pixel);
48 end;
49 end;
50
51 type
52 TTesterSingle = class
53 procedure DoTest(const TestCase: TCastleTestCase);
54 end;
55
56 TTesterDouble = class
57 procedure DoTest(const TestCase: TCastleTestCase);
58 end;
59
60 TTesterExtended = class
61 procedure DoTest(const TestCase: TCastleTestCase);
62 end;
63
64 procedure TTesterSingle.DoTest(const TestCase: TCastleTestCase);
65 var
66 Side: TCubeMapSide;
67 Pixel: Cardinal;
68 SphereArea, SphereArea2: Single;
69 begin
70 SphereArea := 0;
71 SphereArea2 := 0;
72
73 for Side := Low(Side) to High(Side) do
74 for Pixel := 0 to Sqr(CubeMapSize) - 1 do
75 begin
76 SphereArea := SphereArea + CubeMapSolidAngle(Side, Pixel);
77 SphereArea2 := SphereArea2 + (4 * Pi)/(6*Sqr(CubeMapSize));
78 end;
79
80 { I use SphereArea2 only to test the accuracy of addition:
81 remember that even if CubeMapSolidAngle would be perfect,
82 adding "6*Sqr(CubeMapSize)" values together always has some precision
83 error accumulated.
84
85 Results comparing SphereArea2 with (4 * Pi) (accuracy of addition):
86
87 Single:
88 12.566516876220703
89 12.566370614359173
90 | - difference here
91
92 Double:
93 12.566370614359418
94 12.566370614359173
95 | - difference here
96
97 Extended:
98 12.566370614359173
99 12.566370614359173
100
101 | no visible difference of these digits.
102 Looked at 30 first decimal digits,
103 still no difference.
104 }
105
106 {
107 Writeln(Format('%g', [SphereArea]));
108 // Writeln(Format('%g', [SphereArea2]));
109 Writeln(Format('%g', [4 * Pi]));
110 }
111
112 { CubeMapSolidAngle is a gross approximation now, so we allow quite large
113 error. }
114 TestCase.AssertSameValue(4 * Pi, SphereArea, 0.02);
115 end;
116
117
118 procedure TTesterDouble.DoTest(const TestCase: TCastleTestCase);
119 var
120 Side: TCubeMapSide;
121 Pixel: Cardinal;
122 SphereArea, SphereArea2: Double;
123 begin
124 SphereArea := 0;
125 SphereArea2 := 0;
126
127 for Side := Low(Side) to High(Side) do
128 for Pixel := 0 to Sqr(CubeMapSize) - 1 do
129 begin
130 SphereArea := SphereArea + CubeMapSolidAngle(Side, Pixel);
131 SphereArea2 := SphereArea2 + (4 * Pi)/(6*Sqr(CubeMapSize));
132 end;
133
134 { I use SphereArea2 only to test the accuracy of addition:
135 remember that even if CubeMapSolidAngle would be perfect,
136 adding "6*Sqr(CubeMapSize)" values together always has some precision
137 error accumulated.
138
139 Results comparing SphereArea2 with (4 * Pi) (accuracy of addition):
140
141 Single:
142 12.566516876220703
143 12.566370614359173
144 | - difference here
145
146 Double:
147 12.566370614359418
148 12.566370614359173
149 | - difference here
150
151 Extended:
152 12.566370614359173
153 12.566370614359173
154
155 | no visible difference of these digits.
156 Looked at 30 first decimal digits,
157 still no difference.
158 }
159
160 {
161 Writeln(Format('%g', [SphereArea]));
162 // Writeln(Format('%g', [SphereArea2]));
163 Writeln(Format('%g', [4 * Pi]));
164 }
165
166 { CubeMapSolidAngle is a gross approximation now, so we allow quite large
167 error. }
168 TestCase.AssertSameValue(4 * Pi, SphereArea, 0.02);
169 end;
170
171
172 procedure TTesterExtended.DoTest(const TestCase: TCastleTestCase);
173 var
174 Side: TCubeMapSide;
175 Pixel: Cardinal;
176 SphereArea, SphereArea2: Extended;
177 begin
178 SphereArea := 0;
179 SphereArea2 := 0;
180
181 for Side := Low(Side) to High(Side) do
182 for Pixel := 0 to Sqr(CubeMapSize) - 1 do
183 begin
184 SphereArea := SphereArea + CubeMapSolidAngle(Side, Pixel);
185 SphereArea2 := SphereArea2 + (4 * Pi)/(6*Sqr(CubeMapSize));
186 end;
187
188 { I use SphereArea2 only to test the accuracy of addition:
189 remember that even if CubeMapSolidAngle would be perfect,
190 adding "6*Sqr(CubeMapSize)" values together always has some precision
191 error accumulated.
192
193 Results comparing SphereArea2 with (4 * Pi) (accuracy of addition):
194
195 Single:
196 12.566516876220703
197 12.566370614359173
198 | - difference here
199
200 Double:
201 12.566370614359418
202 12.566370614359173
203 | - difference here
204
205 Extended:
206 12.566370614359173
207 12.566370614359173
208
209 | no visible difference of these digits.
210 Looked at 30 first decimal digits,
211 still no difference.
212 }
213
214 {
215 Writeln(Format('%g', [SphereArea]));
216 // Writeln(Format('%g', [SphereArea2]));
217 Writeln(Format('%g', [4 * Pi]));
218 }
219
220 { CubeMapSolidAngle is a gross approximation now, so we allow quite large
221 error. }
222 TestCase.AssertSameValue(4 * Pi, SphereArea, 0.02);
223 end;
224
225 procedure TTestCubeMap.TestCubeMapSolidAngle;
226 var
227 TS: TTesterSingle;
228 TD: TTesterDouble;
229 TE: TTesterExtended;
230 begin
231 TS := TTesterSingle.Create;
232 TS.DoTest(Self);
233 TS.Free;
234
235 TD := TTesterDouble.Create;
236 TD.DoTest(Self);
237 TD.Free;
238
239 TE := TTesterExtended.Create;
240 TE.DoTest(Self);
241 TE.Free;
242 end;
243
244 initialization
245 RegisterTest(TTestCubeMap);
246 end.