Debian Codegraph @

src/scene/x3d/x3dnodes_boundingboxes.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2003-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 { Implement BoundingBox / LocalBoundingBox for geometry nodes. }
18
19 { ----------------------------------------------------------------------------
20 Bounding box utilities for nodes with explicit Coordinates (indexed and not).
21
22 Note that we have two separate implementations for local and non-local
23 bounding box, that is we do not depend on TAbstractGeometryNode methods
24 to automatically calculate one using the other:
25
26 - Calculating LocalBoundingBox explicitly is faster than
27 calculating non-local bbox with dummy identity matrix
28 (like TAbstractGeometryNode.LocalBoundingBox does).
29
30 - Calculating BoundingBox explicitly results in better (more tighter)
31 bbox than using LocalBoundingBox and transforming it.
32 }
33
34 { By default bounding boxes we calculate for meshes are larger than necessary
35 in some situations (e.g. when you have a shape that is rotated).
36 That it because we do 2 optimizations:
37
38 1. Calculate bounding boxes of transformed shapes as
39 a "transformed(bounding box, for all vertexes)".
40 This results in worse box than using "bounding boxm for all trasform(vertex)",
41 but it avoids multiplying each vertex by a matrix.
42
43 2. Calculate bounding boxes of indexed and non-indexed shapes the same way,
44 by just iterating over coordinates, without paying attention to indexes.
45 This results in worse box than looking at indexes
46 (in case coordinates contain distant and unused vertexes) but is faster.
47
48 You can define CASTLE_PRECISE_MESH_BOUNDING_BOXES to restore
49 slower but more precise bounding boxes.
50 }
51 {.$define CASTLE_PRECISE_MESH_BOUNDING_BOXES}
52
53 {$ifdef CASTLE_PRECISE_MESH_BOUNDING_BOXES}
54 type
55 TGI_BBox_Calculator = class
56 public
57 Coord: TMFVec3f;
58 CoordIndex: TMFLong;
59 Geometry: TAbstractGeometryNode;
60 WasSomeValidIndex: boolean;
61 function GetVertexFromIndex(index: integer): TVector3;
62 function GetIndex(indexNum: integer): integer;
63 end;
64
65 function TGI_BBox_Calculator.GetVertexFromIndex(index: integer): TVector3;
66 begin
67 { na pewno index >= 0, CalculateBoundingBoxFromIndices nie bedzie
68 pytalo o indeksy ujemne. }
69 if Index < Coord.Count then
70 begin
71 WasSomeValidIndex := true;
72 Result := Coord.Items.L[Index];
73 end else
74 begin
75 CoordIndex.WritelnWarning_WrongVertexIndex(
76 Geometry.X3DType, Index, Coord.Count);
77 Result := TVector3.Zero;
78 end;
79 end;
80
81 function TGI_BBox_Calculator.GetIndex(IndexNum: integer): integer;
82 begin
83 { nie musimy sprawdzac czy indexNum jest dobry -
84 CalculateBoundingBoxFromIndices bedzie pytalo tylko o indeksy between
85 0..VertIndices.Count-1 }
86 Result := CoordIndex.Items.L[IndexNum];
87 end;
88 {$endif CASTLE_PRECISE_MESH_BOUNDING_BOXES}
89
90 function Coords_BoundingBox(State: TX3DGraphTraverseState;
91 Coord: TMFVec3f): TBox3D; forward;
92 function Coords_LocalBoundingBox(State: TX3DGraphTraverseState;
93 Coord: TMFVec3f): TBox3D; forward;
94
95 function IndexedCoords_BoundingBox(Geometry: TAbstractGeometryNode;
96 State: TX3DGraphTraverseState;
97 Coord: TMFVec3f; CoordIndex: TMFLong): TBox3D;
98 {$ifndef CASTLE_PRECISE_MESH_BOUNDING_BOXES}
99 begin
100 Result := Coords_BoundingBox(State, Coord);
101 {$else}
102 var
103 Calculator: TGI_BBox_Calculator;
104 begin
105 Calculator := TGI_BBox_Calculator.Create;
106 try
107 Calculator.Coord := Coord;
108 Calculator.CoordIndex := CoordIndex;
109 Calculator.Geometry := Geometry;
110 Calculator.WasSomeValidIndex := false;
111 Result := CalculateBoundingBoxFromIndices(
112 {$ifdef FPC} @ {$endif} Calculator.GetIndex,
113 CoordIndex.Count,
114 {$ifdef FPC} @ {$endif} Calculator.GetVertexFromIndex,
115 State.Transformation.Transform);
116 if not Calculator.WasSomeValidIndex then
117 Result := TBox3D.Empty;
118 finally Calculator.Free end;
119 {$endif}
120 end;
121
122 function IndexedCoords_LocalBoundingBox(Geometry: TAbstractGeometryNode;
123 State: TX3DGraphTraverseState;
124 Coord: TMFVec3f; CoordIndex: TMFLong): TBox3D;
125 {$ifndef CASTLE_PRECISE_MESH_BOUNDING_BOXES}
126 begin
127 Result := Coords_LocalBoundingBox(State, Coord);
128 {$else}
129 var
130 Calculator: TGI_BBox_Calculator;
131 begin
132 Calculator := TGI_BBox_Calculator.Create;
133 try
134 Calculator.Coord := Coord;
135 Calculator.CoordIndex := CoordIndex;
136 Calculator.Geometry := Geometry;
137 Calculator.WasSomeValidIndex := false;
138 Result := CalculateBoundingBoxFromIndices(
139 {$ifdef FPC} @ {$endif} Calculator.GetIndex,
140 CoordIndex.Count,
141 {$ifdef FPC} @ {$endif} Calculator.GetVertexFromIndex);
142 if not Calculator.WasSomeValidIndex then
143 Result := TBox3D.Empty;
144 finally Calculator.Free end;
145 {$endif}
146 end;
147
148 function Coords_BoundingBox(State: TX3DGraphTraverseState;
149 Coord: TMFVec3f): TBox3D;
150 {$ifndef CASTLE_PRECISE_MESH_BOUNDING_BOXES}
151 begin
152 Result := Coords_LocalBoundingBox(State, Coord).Transform(
153 State.Transformation.Transform);
154 {$else}
155 begin
156 Result := CalculateBoundingBox(Coord.Items.L,
157 Coord.Items.Count, 0, State.Transformation.Transform);
158 {$endif}
159 end;
160
161 function Coords_LocalBoundingBox(State: TX3DGraphTraverseState;
162 Coord: TMFVec3f): TBox3D;
163 begin
164 Result := CalculateBoundingBox(PVector3(Coord.Items.L),
165 Coord.Items.Count, 0);
166 end;
167
168 { TAbstractGeometryNode --------------------------------------------------------- }
169
170 function TAbstractGeometryNode.CallProxyBoundingBox(
171 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
172 begin
173 Result := ProxyGeometry.BoundingBox(ProxyState, nil, nil);
174 end;
175
176 function TAbstractGeometryNode.BoundingBox(State: TX3DGraphTraverseState;
177 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
178 var
179 C: TMFVec3f;
180 begin
181 {$ifndef CASTLE_PRECISE_MESH_BOUNDING_BOXES}
182 if (State.ShapeNode <> nil) and
183 (not State.ShapeNode.BBox.IsEmpty) then
184 begin
185 Result := State.ShapeNode.BBox.Transform(State.Transformation.Transform); // use precalculated bounding box, fastest
186 end else
187 {$endif}
188 if InternalCoord(State, C) then
189 begin
190 if C <> nil then
191 begin
192 if CoordIndexField <> nil then
193 Result := IndexedCoords_BoundingBox(Self, State, C, CoordIndexField)
194 else
195 Result := Coords_BoundingBox(State, C);
196 end else
197 Result := TBox3D.Empty;
198 end else
199 begin
200 if ProxyGeometry <> nil then
201 Result := CallProxyBoundingBox(ProxyGeometry, ProxyState)
202 else
203 Result := LocalBoundingBox(State, ProxyGeometry, ProxyState).Transform(State.Transformation.Transform);
204 end;
205 end;
206
207 function TAbstractGeometryNode.CallProxyLocalBoundingBox(
208 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
209 begin
210 Result := ProxyGeometry.LocalBoundingBox(ProxyState, nil, nil);
211 end;
212
213 function TAbstractGeometryNode.LocalBoundingBox(State: TX3DGraphTraverseState;
214 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
215 var
216 NewState: TX3DGraphTraverseState;
217 C: TMFVec3f;
218 begin
219 if (State.ShapeNode <> nil) and
220 (not State.ShapeNode.BBox.IsEmpty) then
221 begin
222 Result := State.ShapeNode.BBox; // use precalculated bounding box, fastest
223 end else
224 if InternalCoord(State, C) then
225 begin
226 if C <> nil then
227 begin
228 if CoordIndexField <> nil then
229 Result := IndexedCoords_LocalBoundingBox(Self, State, C, CoordIndexField) else
230 Result := Coords_LocalBoundingBox(State, C);
231 end else
232 Result := TBox3D.Empty;
233 end else
234 begin
235 if ProxyGeometry <> nil then
236 Result := CallProxyLocalBoundingBox(ProxyGeometry, ProxyState) else
237 begin
238 NewState := TX3DGraphTraverseState.CreateCopy(State);
239 try
240 NewState.Transformation.Init;
241 result := BoundingBox(NewState, ProxyGeometry, ProxyState);
242 finally NewState.Free end;
243 end;
244 end;
245 end;
246
247 { ------------------------------------------------------------------------ }
248
249 function Text_LocalBoundingBox(State: TX3DGraphTraverseState;
250 const Size, Spacing: Single;
251 const Justify, JustifyMinor: TX3DFontJustify;
252 const Font: TCastleFont;
253 const Strings: TCastleStringList): TBox3D;
254 var
255 YScale, XScale, MaxRowWidth, MinX, MinY, MaxX, MaxY, Descend: Single;
256 I: integer;
257 begin
258 YScale := Size / Font.Height;
259 { TODO: Use maxEntent, length for X3D and VRML 2.0. Use width for VRML 1.0. }
260 XScale := YScale;
261
262 MaxRowWidth := 0;
263 for I := 0 to Strings.Count - 1 do
264 MaxVar(MaxRowWidth, Font.TextWidth(Strings[I]) * XScale);
265
266 case Justify of
267 fjBegin, fjFirst :
268 begin MinX := 0; MaxX := MaxRowWidth end;
269 fjMiddle: begin MinX := - MaxRowWidth / 2; MaxX := MaxRowWidth / 2 end;
270 fjEnd : begin MinX := - MaxRowWidth; MaxX := 0 end;
271 {$ifndef COMPILER_CASE_ANALYSIS}
272 else raise EInternalError.Create('Text_LocalBoundingBox: Invalid FontStyle Justify value');
273 {$endif}
274 end;
275 case JustifyMinor of
276 fjFirst : begin MaxY := 1; MinY := - (Strings.Count - 1); end;
277 fjBegin : begin MaxY := 0; MinY := - Strings.Count; end;
278 fjMiddle: begin MaxY := - Strings.Count / 2; MinY := -MaxY; end;
279 fjEnd : begin MaxY := Strings.Count; MinY := 0; end;
280 {$ifndef COMPILER_CASE_ANALYSIS}
281 else raise EInternalError.Create('Text_LocalBoundingBox: Invalid FontStyle JustifyMinor value');
282 {$endif}
283 end;
284 MinY := MinY * (Size * Spacing);
285 MaxY := MaxY * (Size * Spacing);
286
287 // for JustifyMinor = fjFirst, the level 0 is at baseline, so lower bbox
288 if JustifyMinor = fjFirst then
289 begin
290 Descend := Size * Font.DescenderHeight / Font.Height;
291 MinY := MinY - (Descend * Spacing);
292 MaxY := MaxY - (Descend * Spacing);
293 end;
294
295 Result := Box3D(
296 Vector3(MinX, MinY, 0),
297 Vector3(MaxX, MaxY, 0));
298 end;
299
300 function TAsciiTextNode_1.LocalBoundingBox(State: TX3DGraphTraverseState;
301 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
302 begin
303 Result := Text_LocalBoundingBox(State,
304 State.VRML1State.FontStyle.FdSize.Value,
305 FdSpacing.Value,
306 Justify, fjFirst,
307 State.VRML1State.FontStyle.Font,
308 FdString.Items);
309 end;
310
311 function TTextNode.LocalBoundingBox(State: TX3DGraphTraverseState;
312 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
313 var
314 Size, Spacing: Single;
315 Justify, JustifyMinor: TX3DFontJustify;
316 begin
317 if FontStyle is TFontStyleNode then // also checks FontStyle <> nil
318 begin
319 Size := TFontStyleNode(FontStyle).FdSize.Value;
320 Spacing := TFontStyleNode(FontStyle).FdSpacing.Value;
321 Justify := TFontStyleNode(FontStyle).Justify;
322 JustifyMinor := TFontStyleNode(FontStyle).JustifyMinor;
323 end else
324 begin
325 Size := TFontStyleNode.DefaultSize;
326 Spacing := TFontStyleNode.DefaultSpacing;
327 Justify := TFontStyleNode.DefaultJustify;
328 JustifyMinor := TFontStyleNode.DefaultJustifyMinor;
329 end;
330
331 Result := Text_LocalBoundingBox(State,
332 Size, Spacing, Justify, JustifyMinor, Font, FdString.Items);
333 end;
334
335 function TTextNode.BoundingBox(State: TX3DGraphTraverseState;
336 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
337 begin
338 { The default implementation in TAbstractGeometryNode.BoundingBox would
339 use ProxyGeometry (generated by TextProxy) to calculate bbox.
340
341 We override it, because we really prefer to use Text_LocalBoundingBox,
342 because it will work OK even when maxDisplayChars is used.
343 maxDisplayChars should change the look,
344 but we don't want it to change the bounding box of the scene.
345 This way animating the text (like in demo-models
346 https://github.com/castle-engine/demo-models/blob/master/animation/simple_tests_interpolation_and_time/animate_text_max_display_chars.x3dv
347 or in "The Unholy Society") doesn't change the text bounding box,
348 making camera and all derived sizes "constant" regardless of maxDisplayChars. }
349
350 Result := LocalBoundingBox(State, ProxyGeometry, ProxyState).Transform(State.Transformation.Transform);
351 end;
352
353 function Cone_BoundingBox(State: TX3DGraphTraverseState;
354 const BottomRadius, Height: Single;
355 const Sides, Bottom, Local: boolean): TBox3D;
356 var points: array [0..4] of TVector3;
357 firstPoint: integer;
358 begin
359 {liczymy bounding box takiego ostroslupa o podstawie z kwadratu.
360 Tak jest prosto (po prostu 5 punktow); jasne ze w latwo sobie wyobrazic
361 taka transformacje ze wyliczony w ten sposob bounding box jest nieco
362 za duzy - ale wydaje mi sie ze nie jest tak zle.}
363 points[0] := Vector3(0, Height/2, 0);
364 points[1] := Vector3(-BottomRadius, -Height/2, -BottomRadius);
365 points[2] := Vector3(-BottomRadius, -Height/2, +BottomRadius);
366 points[3] := Vector3(+BottomRadius, -Height/2, -BottomRadius);
367 points[4] := Vector3(+BottomRadius, -Height/2, +BottomRadius);
368 if Sides then
369 firstPoint := 0 else
370 if Bottom then
371 firstPoint := 1 { nie uwzgledniaj pierwszego punktu = szczytu stozka } else
372 begin result := TBox3D.Empty; exit end;
373
374 if Local then
375 Result := CalculateBoundingBox(@points[firstPoint], High(points)+1-firstPoint, 0)
376 else
377 Result := CalculateBoundingBox(@points[firstPoint], High(points)+1-firstPoint, 0, State.Transformation.Transform);
378 end;
379
380 {$warnings off} // using deprecated to keep supporting VRML 1
381 function TConeNode_1.BoundingBox(State: TX3DGraphTraverseState;
382 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
383 begin
384 Result := Cone_BoundingBox(State,
385 FdBottomRadius.Value, FdHeight.Value,
386 FdParts.Flags[CONE_PARTS_SIDES], FdParts.Flags[CONE_PARTS_BOTTOM], false);
387 end;
388 {$warnings on}
389
390 function TConeNode.BoundingBox(State: TX3DGraphTraverseState;
391 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
392 begin
393 Result := Cone_BoundingBox(State,
394 FdBottomRadius.Value, FdHeight.Value,
395 FdSide.Value, FdBottom.Value, false);
396 end;
397
398 {$warnings off} // using deprecated to keep supporting VRML 1
399 function TConeNode_1.LocalBoundingBox(State: TX3DGraphTraverseState;
400 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
401 begin
402 Result := Cone_BoundingBox(State,
403 FdBottomRadius.Value, FdHeight.Value,
404 FdParts.Flags[CONE_PARTS_SIDES], FdParts.Flags[CONE_PARTS_BOTTOM], true);
405 end;
406 {$warnings on}
407
408 function TConeNode.LocalBoundingBox(State: TX3DGraphTraverseState;
409 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
410 begin
411 Result := Cone_BoundingBox(State,
412 FdBottomRadius.Value, FdHeight.Value,
413 FdSide.Value, FdBottom.Value, true);
414 end;
415
416 function Box_LocalBoundingBox(
417 const SizeX, SizeY, SizeZ: Single;
418 State: TX3DGraphTraverseState): TBox3D;
419 begin
420 result.Data[0] := Vector3(-SizeX/2, -SizeY/2, -SizeZ/2);
421 result.Data[1] := Vector3( SizeX/2, SizeY/2, SizeZ/2);
422 end;
423
424 function TCubeNode_1.LocalBoundingBox(State: TX3DGraphTraverseState;
425 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
426 begin
427 Result := Box_LocalBoundingBox(
428 FdWidth.Value, FdHeight.Value, FdDepth.Value, State);
429 end;
430
431 function TBoxNode.LocalBoundingBox(State: TX3DGraphTraverseState;
432 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
433 begin
434 Result := Box_LocalBoundingBox(
435 FdSize.Value[0], FdSize.Value[1], FdSize.Value[2], State);
436 end;
437
438 function TCubeNode_1.BoundingBox(State: TX3DGraphTraverseState;
439 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
440 begin
441 { Override this, to avoid inherited using slower Proxy method. }
442 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
443 end;
444
445 function TBoxNode.BoundingBox(State: TX3DGraphTraverseState;
446 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
447 begin
448 { Override this, to avoid inherited using slower Proxy method. }
449 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
450 end;
451
452 function Cylinder_LocalBoundingBox(State: TX3DGraphTraverseState;
453 const Height, Radius: Single;
454 const Bottom, Side, Top: boolean): TBox3D;
455 var points: array[0..3]of TVector3;
456 begin
457 {points 0 = bottom start, 1 = bottom end}
458 points[0] := Vector3(-Radius, -Height/2, -Radius);
459 points[1] := Vector3(+Radius, -Height/2, +Radius);
460 {points 2 = top start, 3 = top end}
461 points[2] := Vector3(-Radius, Height/2, -Radius);
462 points[3] := Vector3(+Radius, Height/2, +Radius);
463
464 if Side or (Top and Bottom) then
465 result := Box3D(points[0], points[3]) else
466 if Top then
467 result := Box3D(points[2], points[3]) else
468 if Bottom then
469 result := Box3D(points[0], points[1]) else
470 result := TBox3D.Empty;
471 end;
472
473 {$warnings off} // using deprecated to keep supporting VRML 1
474 function TCylinderNode_1.LocalBoundingBox(State: TX3DGraphTraverseState;
475 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
476 begin
477 Result := Cylinder_LocalBoundingBox(State,
478 FdHeight.Value, FdRadius.Value,
479 FdParts.Flags[CYLINDER_PARTS_BOTTOM],
480 FdParts.Flags[CYLINDER_PARTS_SIDES],
481 FdParts.Flags[CYLINDER_PARTS_TOP]);
482 end;
483 {$warnings on}
484
485 function TCylinderNode.LocalBoundingBox(State: TX3DGraphTraverseState;
486 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
487 begin
488 Result := Cylinder_LocalBoundingBox(State,
489 FdHeight.Value, FdRadius.Value,
490 FdBottom.Value, FdSide.Value, FdTop.Value);
491 end;
492
493 function TCylinderNode_1.BoundingBox(State: TX3DGraphTraverseState;
494 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
495 begin
496 { Override this, to avoid inherited using slower Proxy method. }
497 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
498 end;
499
500 function TCylinderNode.BoundingBox(State: TX3DGraphTraverseState;
501 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
502 begin
503 { Override this, to avoid inherited using slower Proxy method. }
504 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
505 end;
506
507 function Sphere_LocalBoundingBox(const Radius: Single): TBox3D;
508 begin
509 Result := Box3D(
510 Vector3(-Radius, -Radius, -Radius),
511 Vector3(+Radius, +Radius, +Radius) );
512 end;
513
514 function TSphereNode_1.LocalBoundingBox(State: TX3DGraphTraverseState;
515 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
516 begin
517 Result := Sphere_LocalBoundingBox(FdRadius.Value);
518 end;
519
520 function TSphereNode.LocalBoundingBox(State: TX3DGraphTraverseState;
521 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
522 begin
523 Result := Sphere_LocalBoundingBox(FdRadius.Value);
524 end;
525
526 function TSphereNode_1.BoundingBox(State: TX3DGraphTraverseState;
527 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
528 begin
529 { Override this, to avoid inherited using slower Proxy method. }
530 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
531 end;
532
533 function TSphereNode.BoundingBox(State: TX3DGraphTraverseState;
534 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
535 begin
536 { Override this, to avoid inherited using slower Proxy method. }
537 Result := LocalBoundingBox(State, nil, nil).Transform(State.Transformation.Transform);
538 end;
539
540 { TElevationGridNode --------------------------------------------------------- }
541
542 function TElevationGridNode.LocalBoundingBox(State: TX3DGraphTraverseState;
543 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
544 var
545 Y: Single;
546 I: Integer;
547 begin
548 Result := TBox3D.Empty;
549 if IsNotEmpty then
550 begin
551 Result.Data[0].X := 0;
552 Result.Data[0].Z := 0;
553 Result.Data[1].X := FdXSpacing.Value * (FdXDimension.Value - 1);
554 Result.Data[1].Z := FdZSpacing.Value * (FdZDimension.Value - 1);
555
556 { now calculate Result.Data[0].Y and Result.Data[1].Y }
557 Y := FdHeight.Items.L[0];
558 Result.Data[0].Y := Y;
559 Result.Data[1].Y := Y;
560 for I := 1 to FdXDimension.Value * FdZDimension.Value - 1 do
561 begin
562 Y := FdHeight.Items.L[I];
563 MinVar(Result.Data[0].Y, Y);
564 MaxVar(Result.Data[1].Y, Y);
565 end;
566 end;
567 end;
568
569 { TExtrusionNode --------------------------------------------------------- }
570
571 function TExtrusionNode.LocalBoundingBox(State: TX3DGraphTraverseState;
572 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
573 var
574 I, J: Integer;
575 E: TVRMLExtrusion;
576 LastY, LastZ: TVector3;
577 Transform: TMatrix4;
578 Point2d: PVector2;
579 begin
580 Result := TBox3D.Empty;
581
582 E := TVRMLExtrusion.Create;
583 try
584 E.Node := Self;
585 for I := 0 to E.High do
586 begin
587 E.SpineTransformVar(I, LastY, LastZ, Transform);
588 for J := 0 to FdCrossSection.Count - 1 do
589 begin
590 Point2d := PVector2(FdCrossSection.Items.Ptr(J));
591 Result.Include(Transform.MultPoint(
592 Vector3(Point2d^[0], 0, Point2d^[1])));
593 end;
594 end;
595 finally FreeAndNil(E) end;
596 end;
597
598 function TExtrusionNode.BoundingBox(State: TX3DGraphTraverseState;
599 ProxyGeometry: TAbstractGeometryNode; ProxyState: TX3DGraphTraverseState): TBox3D;
600 var
601 I, J: Integer;
602 E: TVRMLExtrusion;
603 LastY, LastZ: TVector3;
604 Transform: TMatrix4;
605 Point2d: PVector2;
606 begin
607 Result := TBox3D.Empty;
608
609 E := TVRMLExtrusion.Create;
610 try
611 E.Node := Self;
612 for I := 0 to E.High do
613 begin
614 E.SpineTransformVar(I, LastY, LastZ, Transform);
615 for J := 0 to FdCrossSection.Count - 1 do
616 begin
617 Point2d := PVector2(FdCrossSection.Items.Ptr(J));
618 Result.Include(State.Transformation.Transform.MultPoint(
619 Transform.MultPoint(
620 Vector3(Point2d^[0], 0, Point2d^[1]))));
621 end;
622 end;
623 finally FreeAndNil(E) end;
624 end;
625
626 { eof ------------------------------------------------------------------------ }