File: testx3dnodesoptimizedproxy.pas

package info (click to toggle)
castle-game-engine 5.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 164,776 kB
  • ctags: 30,841
  • sloc: pascal: 168,882; cpp: 1,340; objc: 730; makefile: 492; sh: 477; xml: 434; php: 1
file content (323 lines) | stat: -rw-r--r-- 11,294 bytes parent folder | download
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
{ -*- compile-command: "./compile_console.sh" -*- }
{
  Copyright 2010-2014 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.

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

unit TestX3DNodesOptimizedProxy;

interface

uses
  Classes, SysUtils, fpcunit, testutils, testregistry, CastleVectors, X3DNodes;

type
  TTestX3DNodesOptimizedProxy = class(TTestCase)
  published
    { Some of the geometry nodes, even though they have Proxy,
      have also optimized versions for some operations (like calculating
      bboxes).

      Test that they are actually used (not falling back on
      proxy), by giving them a nasty proxy that will bomb when you try
      to use it's methods. }
    procedure TestGeometryUsesOptimizedMethods;
  end;

implementation

uses CastleBoxes, CastleShapes;

{ TNastyProxy ---------------------------------------------------------------- }

type
  ENastyProxy = class(Exception);

  TNastyProxy = class(TAbstractGeometryNode)
    function BoundingBox(State: TX3DGraphTraverseState;
      ProxyGeometry: TAbstractGeometryNode;
      ProxyState: TX3DGraphTraverseState): TBox3D; override;
    function LocalBoundingBox(State: TX3DGraphTraverseState;
      ProxyGeometry: TAbstractGeometryNode;
      ProxyState: TX3DGraphTraverseState): TBox3D; override;
    function VerticesCount(State: TX3DGraphTraverseState; OverTriangulate: boolean;
      ProxyGeometry: TAbstractGeometryNode;
      ProxyState: TX3DGraphTraverseState): Cardinal; override;
    function TrianglesCount(State: TX3DGraphTraverseState; OverTriangulate: boolean;
      ProxyGeometry: TAbstractGeometryNode;
      ProxyState: TX3DGraphTraverseState): Cardinal; override;
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

function TNastyProxy.BoundingBox(State: TX3DGraphTraverseState;
  ProxyGeometry: TAbstractGeometryNode;
  ProxyState: TX3DGraphTraverseState): TBox3D;
begin
  raise ENastyProxy.Create('Something tried to use unoptimized BoundingBox');
  Result := EmptyBox3D; { silence compiler warnings }
end;

function TNastyProxy.LocalBoundingBox(State: TX3DGraphTraverseState;
  ProxyGeometry: TAbstractGeometryNode;
  ProxyState: TX3DGraphTraverseState): TBox3D;
begin
  raise ENastyProxy.Create('Something tried to use unoptimized LocalBoundingBox');
  Result := EmptyBox3D; { silence compiler warnings }
end;

function TNastyProxy.VerticesCount(State: TX3DGraphTraverseState; OverTriangulate: boolean;
  ProxyGeometry: TAbstractGeometryNode;
  ProxyState: TX3DGraphTraverseState): Cardinal;
begin
  raise ENastyProxy.Create('Something tried to use unoptimized VerticesCount');
  Result := 0; { silence compiler warnings }
end;

function TNastyProxy.TrianglesCount(State: TX3DGraphTraverseState; OverTriangulate: boolean;
  ProxyGeometry: TAbstractGeometryNode;
  ProxyState: TX3DGraphTraverseState): Cardinal;
begin
  raise ENastyProxy.Create('Something tried to use unoptimized TrianglesCount');
  Result := 0; { silence compiler warnings }
end;

function TNastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  raise ENastyProxy.Create('Something tried to use Proxy on TNastyProxy');
  Result := nil; { silence compiler warnings }
end;

{ Geometry nodes returning TNastyProxy --------------------------------------- }

type
  TConeNode_1_NastyProxy = class(TConeNode_1)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TConeNode_2_NastyProxy = class(TConeNode_2)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TCylinderNode_1_NastyProxy = class(TCylinderNode_1)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TCylinderNode_2_NastyProxy = class(TCylinderNode_2)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TBoxNode_NastyProxy = class(TBoxNode)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TCubeNode_1_NastyProxy = class(TCubeNode_1)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TSphereNode_1_NastyProxy = class(TSphereNode_1)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

  TSphereNode_2_NastyProxy = class(TSphereNode_2)
    function Proxy(var State: TX3DGraphTraverseState;
      const OverTriangulate: boolean): TAbstractGeometryNode; override;
  end;

function TConeNode_1_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TConeNode_2_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TCylinderNode_1_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TCylinderNode_2_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TBoxNode_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TCubeNode_1_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TSphereNode_1_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

function TSphereNode_2_NastyProxy.Proxy(var State: TX3DGraphTraverseState;
  const OverTriangulate: boolean): TAbstractGeometryNode;
begin
  Result := TNastyProxy.Create(NodeName, BaseUrl);
end;

{ TTestX3DNodesOptimizedProxy ----------------------------------------------- }

procedure TTestX3DNodesOptimizedProxy.TestGeometryUsesOptimizedMethods;
var
  { Suffix O means OverTriangulate,
           NO means not OverTriangulate }
  NastyGeometry, GoodGeometry, ProxyGeometryO, ProxyGeometryNO: TAbstractGeometryNode;
  State, ProxyStateO, ProxyStateNO: TX3DGraphTraverseState;
  NastyShape, ProxyShapeO, ProxyShapeNO: TShape;

  procedure FinalizeNode;
  begin
    FreeAndNil(ProxyShapeO);
    FreeAndNil(ProxyGeometryO);

    if ProxyStateO <> State then
      FreeAndNil(ProxyStateO) else
      ProxyStateO := nil;

    FreeAndNil(ProxyShapeNO);
    FreeAndNil(ProxyGeometryNO);

    if ProxyStateNO <> State then
      FreeAndNil(ProxyStateNO) else
      ProxyStateNO := nil;

    FreeAndNil(GoodGeometry);
    FreeAndNil(NastyShape);
    FreeAndNil(NastyGeometry);
  end;

  procedure InitializeNode(NodeClass: TX3DNodeClass; GoodNodeClass: TX3DNodeClass);
  begin
    FinalizeNode;
    NastyGeometry := (NodeClass.Create('', '')) as TAbstractGeometryNode;
    NastyShape := TShape.Create(nil, NastyGeometry, TX3DGraphTraverseState.CreateCopy(State), nil);

    { create also proxy, inside it's own shape.
      This can be used to test that proxy results, *if* they would be used,
      would be the same. }
    GoodGeometry := (GoodNodeClass.Create('', '')) as TAbstractGeometryNode;

    ProxyStateO := State;
    ProxyGeometryO := GoodGeometry.Proxy(ProxyStateO, true);
    if ProxyGeometryO <> nil then
      ProxyShapeO := TShape.Create(nil, ProxyGeometryO, TX3DGraphTraverseState.CreateCopy(ProxyStateO), nil) else
      ProxyShapeO := nil;

    ProxyStateNO := State;
    ProxyGeometryNO := GoodGeometry.Proxy(ProxyStateNO, false);
    if ProxyGeometryNO <> nil then
      ProxyShapeNO := TShape.Create(nil, ProxyGeometryNO, TX3DGraphTraverseState.CreateCopy(ProxyStateNO), nil) else
      ProxyShapeNO := nil;
  end;

  procedure AssertBoxEqual(const B1, B2: TBox3D);
  begin
    Assert(FloatsEqual(B1.Data[0][0], B2.Data[0][0], 0.1));
    Assert(FloatsEqual(B1.Data[0][1], B2.Data[0][1], 0.1));
    Assert(FloatsEqual(B1.Data[0][2], B2.Data[0][2], 0.1));
    Assert(FloatsEqual(B1.Data[1][0], B2.Data[1][0], 0.1));
    Assert(FloatsEqual(B1.Data[1][1], B2.Data[1][1], 0.1));
    Assert(FloatsEqual(B1.Data[1][2], B2.Data[1][2], 0.1));
  end;

  { Check node has optimized (not using proxy) versions of
    BoundingBox, LocalBoundingBox, TrianglesCount.
    And check their results match results of the proxy. }
  procedure CheckNodeBBoxAndTrisCount;
  begin
    AssertBoxEqual(NastyShape.BoundingBox, ProxyShapeO.BoundingBox);
    AssertBoxEqual(NastyShape.BoundingBox, ProxyShapeNO.BoundingBox);
    AssertBoxEqual(NastyShape.LocalBoundingBox, ProxyShapeO.LocalBoundingBox);
    AssertBoxEqual(NastyShape.LocalBoundingBox, ProxyShapeNO.LocalBoundingBox);
    Assert(NastyShape.TrianglesCount(false) = ProxyShapeNO.TrianglesCount(false));
    Assert(NastyShape.TrianglesCount(true ) = ProxyShapeO .TrianglesCount(true ));
  end;

begin
  NastyGeometry := nil;
  GoodGeometry := nil;
  NastyShape := nil;
  ProxyShapeO := nil;
  ProxyShapeNO := nil;
  ProxyStateO := nil;
  ProxyStateNO := nil;
  ProxyGeometryO := nil;
  ProxyGeometryNO := nil;
  State := TX3DGraphTraverseState.Create;
  try

    { create each node, and try to call the methods that should
      be optimized (should *not* call the proxy).
      For nodes that we know *have* some proxy, we can also compare
      do the optimized results match the proxy results.
      So we test do optimized BoundingBox etc.
      1. are used
      and
      2. return correct result }
    InitializeNode(TConeNode_1_NastyProxy, TConeNode_1);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TConeNode_2_NastyProxy, TConeNode_2);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TCylinderNode_1_NastyProxy, TCylinderNode_1);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TCylinderNode_2_NastyProxy, TCylinderNode_2);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TBoxNode_NastyProxy, TBoxNode);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TCubeNode_1_NastyProxy, TCubeNode_1);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TSphereNode_1_NastyProxy, TSphereNode_1);
    CheckNodeBBoxAndTrisCount;

    InitializeNode(TSphereNode_2_NastyProxy, TSphereNode_2);
    CheckNodeBBoxAndTrisCount;
  finally
    FinalizeNode;
    FreeAndNil(State);
  end;
end;

initialization
 RegisterTest(TTestX3DNodesOptimizedProxy);
end.