Debian Codegraph @

src/scene/castleinternalshapeoctree.pas

raw
1 {
2 Copyright 2005-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Octree for fast 3D collision detection in VRML/X3D shapes (TShapeOctree).
17
18 Contrast this with @link(TTriangleOctree):
19 @link(TTriangleOctree) is an octree based on scene triangles,
20 while @link(TShapeOctree) is an octree based on scene shapes.
21 A scene usually has much more triangles than shapes.
22
23 This octree is the key structure to do scene culling on a per-shape-basis,
24 e.g. to camera frustum, as used by @link(TCastleScene).
25 It can also be used to perform collision detection that starts working per-shape,
26 to choose the shapes that potentially collide, and then run per-triangle collision
27 detection within the shape, using @link(TShape.InternalOctreeTriangles). }
28 unit CastleInternalShapeOctree;
29
30 {$I castleconf.inc}
31 {$I octreeconf.inc}
32
33 interface
34
35 uses SysUtils, CastleBoxes, CastleInternalOctree, CastleShapes, CastleVectors,
36 CastleUtils, CastleInternalBaseTriangleOctree, CastleTriangles;
37
38 const
39 { }
40 DefShapeOctreeMaxDepth = 5;
41 DefShapeOctreeLeafCapacity = 10;
42 DefShapeOctreeLimits: TOctreeLimits = (
43 MaxDepth: DefShapeOctreeMaxDepth;
44 LeafCapacity: DefShapeOctreeLeafCapacity
45 );
46
47 type
48 TShapeOctree = class;
49
50 TShapeOctreeNode = class(TBaseTrianglesOctreeNode)
51 private
52 { These do the job of their counterparts (without Local prefix),
53 except they do not transform resulting triangle and Intersection
54 position back to the world coordinates.
55
56 The idea is that non-local versions with IsXxx do not need to do
57 this transformation, thus saving a tiny amount of time.
58 And the non-local versions without "Is" can just transform
59 the result at the end, if non-nil. }
60 function LocalSphereCollision(const pos: TVector3;
61 const Radius: Single;
62 const TriangleToIgnore: PTriangle;
63 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
64
65 function LocalSphereCollision2D(const pos: TVector2;
66 const Radius: Single;
67 const TriangleToIgnore: PTriangle;
68 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
69
70 function LocalPointCollision2D(const Point: TVector2;
71 const TriangleToIgnore: PTriangle;
72 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
73
74 function LocalBoxCollision(const ABox: TBox3D;
75 const TriangleToIgnore: PTriangle;
76 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
77
78 function LocalSegmentCollision(
79 out Intersection: TVector3;
80 out IntersectionDistance: Single;
81 const Pos1, Pos2: TVector3;
82 const Tag: TMailboxTag;
83 const ReturnClosestIntersection: boolean;
84 const TriangleToIgnore: PTriangle;
85 const IgnoreMarginAtStart: boolean;
86 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
87
88 function LocalRayCollision(
89 out Intersection: TVector3;
90 out IntersectionDistance: Single;
91 const RayOrigin, RayDirection: TVector3;
92 const Tag: TMailboxTag;
93 const ReturnClosestIntersection: boolean;
94 const TriangleToIgnore: PTriangle;
95 const IgnoreMarginAtStart: boolean;
96 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
97 protected
98 function ItemBoundingBox(const ItemIndex: integer): TBox3D; override;
99 procedure PutItemIntoSubNodes(ItemIndex: integer); override;
100
101 function CommonSphereLeaf(const pos: TVector3;
102 const Radius: Single;
103 const TriangleToIgnore: PTriangle;
104 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
105
106 function CommonSphere2DLeaf(const pos: TVector2;
107 const Radius: Single;
108 const TriangleToIgnore: PTriangle;
109 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
110
111 function CommonPoint2DLeaf(const Point: TVector2;
112 const TriangleToIgnore: PTriangle;
113 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
114
115 function CommonBoxLeaf(const ABox: TBox3D;
116 const TriangleToIgnore: PTriangle;
117 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
118
119 function CommonSegmentLeaf(
120 out Intersection: TVector3;
121 out IntersectionDistance: Single;
122 const pos1, pos2: TVector3;
123 const Tag: TMailboxTag;
124 const ReturnClosestIntersection: boolean;
125 const TriangleToIgnore: PTriangle;
126 const IgnoreMarginAtStart: boolean;
127 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
128
129 function CommonRayLeaf(
130 out Intersection: TVector3;
131 out IntersectionDistance: Single;
132 const RayOrigin, RayDirection: TVector3;
133 const Tag: TMailboxTag;
134 const ReturnClosestIntersection: boolean;
135 const TriangleToIgnore: PTriangle;
136 const IgnoreMarginAtStart: boolean;
137 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
138 public
139 function ParentTree: TShapeOctree;
140 function ParentNode: TShapeOctreeNode;
141
142 function SphereCollision(const pos: TVector3;
143 const Radius: Single;
144 const TriangleToIgnore: PTriangle;
145 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
146
147 function IsSphereCollision(const pos: TVector3;
148 const Radius: Single;
149 const TriangleToIgnore: PTriangle;
150 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
151
152 function SphereCollision2D(const pos: TVector2;
153 const Radius: Single;
154 const TriangleToIgnore: PTriangle;
155 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
156
157 function IsSphereCollision2D(const pos: TVector2;
158 const Radius: Single;
159 const TriangleToIgnore: PTriangle;
160 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
161
162 function PointCollision2D(const Point: TVector2;
163 const TriangleToIgnore: PTriangle;
164 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
165
166 function IsPointCollision2D(const Point: TVector2;
167 const TriangleToIgnore: PTriangle;
168 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
169
170 function BoxCollision(const ABox: TBox3D;
171 const TriangleToIgnore: PTriangle;
172 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
173
174 function IsBoxCollision(const ABox: TBox3D;
175 const TriangleToIgnore: PTriangle;
176 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
177
178 function SegmentCollision(
179 out Intersection: TVector3;
180 out IntersectionDistance: Single;
181 const Pos1, Pos2: TVector3;
182 const Tag: TMailboxTag;
183 const ReturnClosestIntersection: boolean;
184 const TriangleToIgnore: PTriangle;
185 const IgnoreMarginAtStart: boolean;
186 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
187
188 function IsSegmentCollision(
189 const Pos1, Pos2: TVector3;
190 const Tag: TMailboxTag;
191 const TriangleToIgnore: PTriangle;
192 const IgnoreMarginAtStart: boolean;
193 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
194
195 function RayCollision(
196 out Intersection: TVector3;
197 out IntersectionDistance: Single;
198 const RayOrigin, RayDirection: TVector3;
199 const Tag: TMailboxTag;
200 const ReturnClosestIntersection: boolean;
201 const TriangleToIgnore: PTriangle;
202 const IgnoreMarginAtStart: boolean;
203 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle; override;
204
205 function IsRayCollision(
206 const RayOrigin, RayDirection: TVector3;
207 const Tag: TMailboxTag;
208 const TriangleToIgnore: PTriangle;
209 const IgnoreMarginAtStart: boolean;
210 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean; override;
211
212 public
213 { For Hierarchical Occlusion Culling }
214 LastVisitedFrameId: Cardinal;
215 Visible: boolean;
216 end;
217
218 TShapeOctree = class(TBaseTrianglesOctree)
219 private
220 FShapesList: TShapeList;
221 FOwnsShapesList: boolean;
222 protected
223 function StatisticsBonus: string; override;
224 public
225 { Constructor.
226 Given TShapeList contents are copied. }
227 constructor Create(const ALimits: TOctreeLimits;
228 const ARootBox: TBox3D; AShapesList: TShapeList);
229 destructor Destroy; override;
230 procedure EnumerateTriangles(EnumerateTriangleFunc: TEnumerateTriangleFunc);
231 override;
232 function TrianglesCount: Cardinal; override;
233 function TotalItemsInOctree: Int64; override;
234
235 function TreeRoot: TShapeOctreeNode;
236 property ShapesList: TShapeList read FShapesList;
237 property OwnsShapesList: boolean read FOwnsShapesList;
238 end;
239
240 implementation
241
242 uses CastleLog;
243
244 { TShapeOctreeNode ---------------------------------------- }
245
246 function TShapeOctreeNode.ItemBoundingBox(const ItemIndex: integer): TBox3D;
247 begin
248 Result := ParentTree.ShapesList[ItemIndex].BoundingBox;
249
250 { We assume below that SubnodesBox is not empty.
251 Only such shapes can be added to TShapeOctree. }
252
253 { For safety, I'm enlarging box a little, to be sure.
254 This way if BoundingBox will lie exactly on one of
255 3 orthogonal planes determined by MiddlePoint then
256 this ItemIndex will be said to collide with both sides
257 of this plane. }
258 Result.ExpandMe(SingleEpsilon);
259 end;
260
261 procedure TShapeOctreeNode.PutItemIntoSubNodes(ItemIndex: integer);
262 var
263 BoxLo, BoxHi: TOctreeSubnodeIndex;
264 SubnodesBox: TBox3D;
265 B0, B1, B2: boolean;
266 begin
267 SubnodesBox := ItemBoundingBox(ItemIndex);
268 SubnodesWithBox(SubnodesBox, BoxLo, BoxHi);
269
270 for B0 := BoxLo[0] to BoxHi[0] do
271 for B1 := BoxLo[1] to BoxHi[1] do
272 for B2 := BoxLo[2] to BoxHi[2] do
273 TreeSubNodes[B0, B1, B2].AddItem(ItemIndex);
274 end;
275
276 function TShapeOctreeNode.ParentTree: TShapeOctree;
277 begin
278 Result := TShapeOctree(InternalParentTree);
279 end;
280
281 function TShapeOctreeNode.ParentNode: TShapeOctreeNode;
282 begin
283 Result := TShapeOctreeNode(InternalParentNode);
284 end;
285
286 function TShapeOctreeNode.CommonSphereLeaf(const Pos: TVector3;
287 const Radius: Single;
288 const TriangleToIgnore: PTriangle;
289 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
290 var
291 I: Integer;
292 Shape: TShape;
293 SafeCollisionsSphereAsBox: Boolean;
294 LocalBox: TBox3D;
295 LocalPos: TVector3;
296 LocalRadius: Single;
297 begin
298 Result := nil;
299 for I := 0 to ItemsIndices.Count - 1 do
300 begin
301 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
302 try
303 { Using SafeCollisionsSphereAsBox means that sphere is actually transformed to a box.
304 This means that it collides as something more than it should.
305 OTOH, this accounts for non-uniform scale. }
306 SafeCollisionsSphereAsBox := not Shape.State.Transformation.UniformScale;
307
308 if SafeCollisionsSphereAsBox then
309 begin
310 // too verbose for normal usage
311 // WritelnWarning('Non-uniform scale, SphereCollision will not work precisely, approximating sphere with larger box');
312 LocalBox := BoundingBox3DFromSphere(Pos, Radius).Transform(
313 Shape.State.Transformation.InverseTransform);
314 Result := Shape.InternalOctreeTriangles.BoxCollision(
315 LocalBox, TriangleToIgnore, TrianglesToIgnoreFunc);
316 end else
317 begin
318 LocalPos := Shape.State.Transformation.InverseTransform.MultPoint(Pos);
319 LocalRadius := Radius / Shape.State.Transformation.Scale;
320 Result := Shape.InternalOctreeTriangles.SphereCollision(
321 LocalPos, LocalRadius, TriangleToIgnore, TrianglesToIgnoreFunc);
322 end;
323 except
324 on ETransformedResultInvalid do Result := nil;
325 end;
326 if Result <> nil then Exit;
327 end;
328 end;
329
330 function TShapeOctreeNode.LocalSphereCollision(const Pos: TVector3;
331 const Radius: Single;
332 const TriangleToIgnore: PTriangle;
333 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
334 begin
335 Result := CommonSphere(pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc);
336 end;
337
338 function TShapeOctreeNode.SphereCollision(const Pos: TVector3;
339 const Radius: Single;
340 const TriangleToIgnore: PTriangle;
341 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
342 begin
343 Result := LocalSphereCollision(Pos, Radius, TriangleToIgnore,
344 TrianglesToIgnoreFunc);
345 if Result <> nil then
346 Result^.UpdateWorld;
347 end;
348
349 function TShapeOctreeNode.IsSphereCollision(const Pos: TVector3;
350 const Radius: Single;
351 const TriangleToIgnore: PTriangle;
352 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
353 begin
354 Result := LocalSphereCollision(Pos, Radius, TriangleToIgnore,
355 TrianglesToIgnoreFunc) <> nil;
356 end;
357
358 function TShapeOctreeNode.CommonSphere2DLeaf(const Pos: TVector2;
359 const Radius: Single;
360 const TriangleToIgnore: PTriangle;
361 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
362 var
363 I: Integer;
364 Shape: TShape;
365 LocalPos: TVector2;
366 LocalRadius: Single;
367 begin
368 Result := nil;
369 for I := 0 to ItemsIndices.Count - 1 do
370 begin
371 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
372 try
373 LocalPos := Shape.State.Transformation.InverseTransform.MultPoint(Pos);
374 LocalRadius := Radius / Shape.State.Transformation.Scale;
375 Result := Shape.InternalOctreeTriangles.SphereCollision2D(
376 LocalPos, LocalRadius, TriangleToIgnore, TrianglesToIgnoreFunc);
377 except
378 on ETransformedResultInvalid do Result := nil;
379 end;
380 if Result <> nil then Exit;
381 end;
382 end;
383
384 function TShapeOctreeNode.LocalSphereCollision2D(const Pos: TVector2;
385 const Radius: Single;
386 const TriangleToIgnore: PTriangle;
387 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
388 begin
389 Result := CommonSphere2D(pos, Radius, TriangleToIgnore, TrianglesToIgnoreFunc);
390 end;
391
392 function TShapeOctreeNode.SphereCollision2D(const Pos: TVector2;
393 const Radius: Single;
394 const TriangleToIgnore: PTriangle;
395 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
396 begin
397 Result := LocalSphereCollision2D(Pos, Radius, TriangleToIgnore,
398 TrianglesToIgnoreFunc);
399 if Result <> nil then
400 Result^.UpdateWorld;
401 end;
402
403 function TShapeOctreeNode.IsSphereCollision2D(const Pos: TVector2;
404 const Radius: Single;
405 const TriangleToIgnore: PTriangle;
406 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
407 begin
408 Result := LocalSphereCollision2D(Pos, Radius, TriangleToIgnore,
409 TrianglesToIgnoreFunc) <> nil;
410 end;
411
412 function TShapeOctreeNode.CommonPoint2DLeaf(const Point: TVector2;
413 const TriangleToIgnore: PTriangle;
414 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
415 var
416 I: Integer;
417 Shape: TShape;
418 LocalPoint: TVector2;
419 begin
420 Result := nil;
421 for I := 0 to ItemsIndices.Count - 1 do
422 begin
423 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
424 try
425 LocalPoint := Shape.State.Transformation.InverseTransform.MultPoint(Point);
426 Result := Shape.InternalOctreeTriangles.PointCollision2D(
427 LocalPoint, TriangleToIgnore, TrianglesToIgnoreFunc);
428 except
429 on ETransformedResultInvalid do Result := nil;
430 end;
431 if Result <> nil then Exit;
432 end;
433 end;
434
435 function TShapeOctreeNode.LocalPointCollision2D(const Point: TVector2;
436 const TriangleToIgnore: PTriangle;
437 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
438 begin
439 Result := CommonPoint2D(Point, TriangleToIgnore, TrianglesToIgnoreFunc);
440 end;
441
442 function TShapeOctreeNode.PointCollision2D(const Point: TVector2;
443 const TriangleToIgnore: PTriangle;
444 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
445 begin
446 Result := LocalPointCollision2D(Point, TriangleToIgnore,
447 TrianglesToIgnoreFunc);
448 if Result <> nil then
449 Result^.UpdateWorld;
450 end;
451
452 function TShapeOctreeNode.IsPointCollision2D(const Point: TVector2;
453 const TriangleToIgnore: PTriangle;
454 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
455 begin
456 Result := LocalPointCollision2D(Point, TriangleToIgnore,
457 TrianglesToIgnoreFunc) <> nil;
458 end;
459
460 function TShapeOctreeNode.CommonBoxLeaf(const ABox: TBox3D;
461 const TriangleToIgnore: PTriangle;
462 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
463 var
464 I: Integer;
465 Shape: TShape;
466 LocalBox: TBox3D;
467 begin
468 { TODO: this is bad, as we transform this box, making larger box.
469 This means that collision is done vs something larger than it should be. }
470 Result := nil;
471 for I := 0 to ItemsIndices.Count - 1 do
472 begin
473 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
474 try
475 LocalBox := ABox.Transform(Shape.State.Transformation.InverseTransform);
476 Result := Shape.InternalOctreeTriangles.BoxCollision(
477 LocalBox, TriangleToIgnore, TrianglesToIgnoreFunc);
478 except
479 on ETransformedResultInvalid do Result := nil;
480 end;
481 if Result <> nil then Exit;
482 end;
483 end;
484
485 function TShapeOctreeNode.LocalBoxCollision(const ABox: TBox3D;
486 const TriangleToIgnore: PTriangle;
487 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
488 begin
489 Result := CommonBox(ABox, TriangleToIgnore, TrianglesToIgnoreFunc);
490 end;
491
492 function TShapeOctreeNode.BoxCollision(const ABox: TBox3D;
493 const TriangleToIgnore: PTriangle;
494 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
495 begin
496 Result := LocalBoxCollision(ABox, TriangleToIgnore,
497 TrianglesToIgnoreFunc);
498 if Result <> nil then
499 Result^.UpdateWorld;
500 end;
501
502 function TShapeOctreeNode.IsBoxCollision(const ABox: TBox3D;
503 const TriangleToIgnore: PTriangle;
504 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
505 begin
506 Result := LocalBoxCollision(ABox, TriangleToIgnore,
507 TrianglesToIgnoreFunc) <> nil;
508 end;
509
510 function TShapeOctreeNode.CommonSegmentLeaf(
511 out Intersection: TVector3;
512 out IntersectionDistance: Single;
513 const Pos1, Pos2: TVector3;
514 const Tag: TMailboxTag;
515 const ReturnClosestIntersection: boolean;
516 const TriangleToIgnore: PTriangle;
517 const IgnoreMarginAtStart: boolean;
518 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
519 var
520 I: Integer;
521 Shape: TShape;
522 LocalPos1, LocalPos2: TVector3;
523 ThisIntersection: TVector3;
524 ThisIntersectionDistance: Single;
525 ThisResult: PTriangle;
526 begin
527 Result := nil;
528
529 if not ReturnClosestIntersection then
530 begin
531 for I := 0 to ItemsIndices.Count - 1 do
532 begin
533 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
534 try
535 LocalPos1 := Shape.State.Transformation.InverseTransform.MultPoint(Pos1);
536 LocalPos2 := Shape.State.Transformation.InverseTransform.MultPoint(Pos2);
537 Result := Shape.SegmentCollision(Tag,
538 Intersection, IntersectionDistance, LocalPos1, LocalPos2,
539 ReturnClosestIntersection,
540 TriangleToIgnore, IgnoreMarginAtStart, TrianglesToIgnoreFunc);
541 except
542 on ETransformedResultInvalid do Result := nil;
543 end;
544
545 if Result <> nil then
546 begin
547 Intersection := Result^.State.Transformation.Transform.MultPoint(Intersection);
548 Exit;
549 end;
550 end;
551 end else
552 begin
553 { To implement ReturnClosestIntersection = true, we use This* variables.
554 We only use This* variables if they
555 indicate closer intersection than currently known. }
556
557 for I := 0 to ItemsIndices.Count - 1 do
558 begin
559 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
560 ThisResult := nil;
561 try
562 LocalPos1 := Shape.State.Transformation.InverseTransform.MultPoint(Pos1);
563 LocalPos2 := Shape.State.Transformation.InverseTransform.MultPoint(Pos2);
564 ThisResult := Shape.SegmentCollision(Tag,
565 ThisIntersection, ThisIntersectionDistance, LocalPos1, LocalPos2,
566 ReturnClosestIntersection,
567 TriangleToIgnore, IgnoreMarginAtStart, TrianglesToIgnoreFunc);
568 except
569 on ETransformedResultInvalid do ;
570 end;
571 if (ThisResult <> nil) and
572 ( (Result = nil) or
573 (ThisIntersectionDistance < IntersectionDistance) ) then
574 begin
575 Intersection := ThisIntersection;
576 IntersectionDistance := ThisIntersectionDistance;
577 Result := ThisResult;
578 end;
579 end;
580
581 if Result <> nil then
582 Intersection := Result^.State.Transformation.Transform.MultPoint(Intersection);
583 end;
584 end;
585
586 function TShapeOctreeNode.LocalSegmentCollision(
587 out Intersection: TVector3;
588 out IntersectionDistance: Single;
589 const Pos1, Pos2: TVector3;
590 const Tag: TMailboxTag;
591 const ReturnClosestIntersection: boolean;
592 const TriangleToIgnore: PTriangle;
593 const IgnoreMarginAtStart: boolean;
594 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
595 begin
596 Result := CommonSegment(
597 Intersection, IntersectionDistance, Pos1, Pos2,
598 Tag,
599 ReturnClosestIntersection, TriangleToIgnore,
600 IgnoreMarginAtStart, TrianglesToIgnoreFunc);
601 end;
602
603 function TShapeOctreeNode.SegmentCollision(
604 out Intersection: TVector3;
605 out IntersectionDistance: Single;
606 const Pos1, Pos2: TVector3;
607 const Tag: TMailboxTag;
608 const ReturnClosestIntersection: boolean;
609 const TriangleToIgnore: PTriangle;
610 const IgnoreMarginAtStart: boolean;
611 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
612 begin
613 Result := LocalSegmentCollision(
614 Intersection, IntersectionDistance, Pos1, Pos2,
615 Tag,
616 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
617 TrianglesToIgnoreFunc);
618
619 { At one point, I also did here
620 Intersection := Result^.State.Transform.MultPoint(Intersection);
621 But it turns out that CommonRay/Segment (for non-leaf nodes) code
622 requires the returned intersection to be in correct (global, for this
623 octree) coordinates --- see it's
624 Box3DContainsTolerant(Intersection, SubNode.Box) test.
625 So Intersection must be transformed back already in CommonSegmentLeaf. }
626 if Result <> nil then
627 Result^.UpdateWorld;
628 end;
629
630 function TShapeOctreeNode.IsSegmentCollision(
631 const Pos1, Pos2: TVector3;
632 const Tag: TMailboxTag;
633 const TriangleToIgnore: PTriangle;
634 const IgnoreMarginAtStart: boolean;
635 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
636 var
637 { We just ignore returned Intersection, IntersectionDistance }
638 Intersection: TVector3;
639 IntersectionDistance: Single;
640 begin
641 Result := LocalSegmentCollision(
642 Intersection, IntersectionDistance, Pos1, Pos2,
643 Tag,
644 {ReturnClosestIntersection}false, TriangleToIgnore, IgnoreMarginAtStart,
645 TrianglesToIgnoreFunc) <> nil;
646 end;
647
648 function TShapeOctreeNode.CommonRayLeaf(
649 out Intersection: TVector3;
650 out IntersectionDistance: Single;
651 const RayOrigin, RayDirection: TVector3;
652 const Tag: TMailboxTag;
653 const ReturnClosestIntersection: boolean;
654 const TriangleToIgnore: PTriangle;
655 const IgnoreMarginAtStart: boolean;
656 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
657 var
658 I: Integer;
659 Shape: TShape;
660 LocalRayOrigin, LocalRayDirection: TVector3;
661 ThisIntersection: TVector3;
662 ThisIntersectionDistance: Single;
663 ThisResult: PTriangle;
664 begin
665 Result := nil;
666
667 if not ReturnClosestIntersection then
668 begin
669 for I := 0 to ItemsIndices.Count - 1 do
670 begin
671 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
672 try
673 LocalRayOrigin := Shape.State.Transformation.InverseTransform.MultPoint(RayOrigin);
674 LocalRayDirection := Shape.State.Transformation.InverseTransform.MultDirection(RayDirection);
675 Result := Shape.RayCollision(Tag,
676 Intersection, IntersectionDistance, LocalRayOrigin, LocalRayDirection,
677 ReturnClosestIntersection,
678 TriangleToIgnore, IgnoreMarginAtStart, TrianglesToIgnoreFunc);
679 except
680 on ETransformedResultInvalid do Result := nil;
681 end;
682
683 if Result <> nil then
684 begin
685 Intersection := Result^.State.Transformation.Transform.MultPoint(Intersection);
686 Exit;
687 end;
688 end;
689 end else
690 begin
691 { To implement ReturnClosestIntersection = true, we use This* variables.
692 We only use This* variables if they
693 indicate closer intersection than currently known. }
694
695 for I := 0 to ItemsIndices.Count - 1 do
696 begin
697 Shape := ParentTree.ShapesList.Items[ItemsIndices.Items[I]];
698 Assert(Shape.InternalOctreeTriangles <> nil);
699 ThisResult := nil;
700 try
701 LocalRayOrigin := Shape.State.Transformation.InverseTransform.MultPoint(RayOrigin);
702 LocalRayDirection := Shape.State.Transformation.InverseTransform.MultDirection(RayDirection);
703 ThisResult := Shape.RayCollision(Tag,
704 ThisIntersection, ThisIntersectionDistance, LocalRayOrigin, LocalRayDirection,
705 ReturnClosestIntersection,
706 TriangleToIgnore, IgnoreMarginAtStart, TrianglesToIgnoreFunc);
707 except
708 on ETransformedResultInvalid do ;
709 end;
710 if (ThisResult <> nil) and
711 ( (Result = nil) or
712 (ThisIntersectionDistance < IntersectionDistance) ) then
713 begin
714 Intersection := ThisIntersection;
715 IntersectionDistance := ThisIntersectionDistance;
716 Result := ThisResult;
717 end;
718 end;
719
720 if Result <> nil then
721 Intersection := Result^.State.Transformation.Transform.MultPoint(Intersection);
722 end;
723 end;
724
725 function TShapeOctreeNode.LocalRayCollision(
726 out Intersection: TVector3;
727 out IntersectionDistance: Single;
728 const RayOrigin, RayDirection: TVector3;
729 const Tag: TMailboxTag;
730 const ReturnClosestIntersection: boolean;
731 const TriangleToIgnore: PTriangle;
732 const IgnoreMarginAtStart: boolean;
733 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
734 begin
735 Result := CommonRay(
736 Intersection, IntersectionDistance, RayOrigin, RayDirection,
737 Tag,
738 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
739 TrianglesToIgnoreFunc);
740 end;
741
742 function TShapeOctreeNode.RayCollision(
743 out Intersection: TVector3;
744 out IntersectionDistance: Single;
745 const RayOrigin, RayDirection: TVector3;
746 const Tag: TMailboxTag;
747 const ReturnClosestIntersection: boolean;
748 const TriangleToIgnore: PTriangle;
749 const IgnoreMarginAtStart: boolean;
750 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): PTriangle;
751 begin
752 Result := LocalRayCollision(Intersection, IntersectionDistance,
753 RayOrigin, RayDirection,
754 Tag,
755 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
756 TrianglesToIgnoreFunc);
757
758 { At one point, I also did here
759 Intersection := Result^.State.Transform.MultPoint(Intersection);
760 But it turns out that CommonRay/Segment (for non-leaf nodes) code
761 requires the returned intersection to be in correct (global, for this
762 octree) coordinates --- see it's
763 Box3DContainsTolerant(Intersection, SubNode.Box) test.
764 So Intersection must be transformed back already in CommonSegmentLeaf. }
765 if Result <> nil then
766 Result^.UpdateWorld;
767 end;
768
769 function TShapeOctreeNode.IsRayCollision(
770 const RayOrigin, RayDirection: TVector3;
771 const Tag: TMailboxTag;
772 const TriangleToIgnore: PTriangle;
773 const IgnoreMarginAtStart: boolean;
774 const TrianglesToIgnoreFunc: TTriangleIgnoreFunc): boolean;
775 var
776 { We just ignore returned Intersection, IntersectionDistance }
777 Intersection: TVector3;
778 IntersectionDistance: Single;
779 begin
780 Result := LocalRayCollision(Intersection, IntersectionDistance,
781 RayOrigin, RayDirection,
782 Tag,
783 {ReturnClosestIntersection}false, TriangleToIgnore, IgnoreMarginAtStart,
784 TrianglesToIgnoreFunc) <> nil;
785 end;
786
787 { TShapeOctree ------------------------------------------ }
788
789 constructor TShapeOctree.Create(const ALimits: TOctreeLimits;
790 const ARootBox: TBox3D; AShapesList: TShapeList);
791 begin
792 inherited Create(ALimits, ARootBox, TShapeOctreeNode, true);
793
794 FShapesList := TShapeList.Create(false);
795 FShapesList.AddRange(AShapesList);
796 FOwnsShapesList := true; // for now always true
797 end;
798
799 destructor TShapeOctree.Destroy;
800 begin
801 if OwnsShapesList then
802 FreeAndNil(FShapesList);
803 inherited;
804 end;
805
806 function TShapeOctree.TreeRoot: TShapeOctreeNode;
807 begin
808 Result := TShapeOctreeNode(InternalTreeRoot);
809 end;
810
811 function TShapeOctree.StatisticsBonus: string;
812 begin
813 Result := NL;
814 if ShapesList.Count = 0 then
815 Result := Result + NL + NL +
816 ' Empty octree - scene has no Shapes, i.e. no visible nodes.' + NL
817 else
818 Result := Result + Format(
819 ' %d items (=Shapes) defined for octree, %d items in octree''s leafs' + NL +
820 ' - so each shape is present in tree about %f times.' + NL +
821 ' (Not counting shapes duplicated in internal nodes.)' + NL,
822 [ShapesList.Count, TotalItemsInLeafs, TotalItemsInLeafs / ShapesList.Count] );
823 end;
824
825 function TShapeOctree.TotalItemsInOctree: Int64;
826 begin
827 Result := ShapesList.Count;
828 end;
829
830 procedure TShapeOctree.EnumerateTriangles(
831 EnumerateTriangleFunc: TEnumerateTriangleFunc);
832 var
833 I: Integer;
834 begin
835 for I := 0 to ShapesList.Count - 1 do
836 ShapesList.Items[I].InternalOctreeTriangles.EnumerateTrianglesUpdateWorld(
837 EnumerateTriangleFunc);
838 end;
839
840 function TShapeOctree.TrianglesCount: Cardinal;
841 var
842 I: Integer;
843 begin
844 Result := 0;
845 for I := 0 to ShapesList.Count - 1 do
846 Result := Result + ShapesList.Items[I].InternalOctreeTriangles.TrianglesCount;
847 end;
848
849 end.