Debian Codegraph @

src/transform/castleinternalbasetriangleoctree_raysegment_nonleaf.inc

raw
1 {
2 Copyright 2003-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 { Implementacja funkcji CommonSegment i CommonRay w jednym kodzie.
17 See castleinternaltriangleoctree_raysegmentcollisions.inc for some comments.
18 }
19
20 { CommonRay/Segment begins --------------------------------------------------- }
21
22 function DoNotLeaf: PTriangle;
23 {$ifdef SEGMENT_COLLISION}
24 var
25 RayOrigin: TVector3 absolute Pos1;
26 RayDirection: TVector3;
27 {$endif}
28
29 { For demo_models/vrml_1/flat_triangles_octree_test.wrl,
30 this really needs some tolerance. And SingleEpsilon
31 was too small. }
32 function Box3DContainsTolerant(
33 const pt: TVector3; const box: TBox3D): boolean;
34 begin
35 if box.IsEmpty then exit(false);
36 result := Between(pt[0], Box.Data[0].X - 0.001, Box.Data[1].X + 0.001) and
37 Between(pt[1], Box.Data[0].Y - 0.001, Box.Data[1].Y + 0.001) and
38 Between(pt[2], Box.Data[0].Z - 0.001, Box.Data[1].Z + 0.001);
39 end;
40
41 var
42 { FirstBoxIntr = first ray with box intersection if RayOrigin is outside
43 if the Box or simply RayOrigin if RayOrigin is inside our Box }
44 FirstBoxIntr: TVector3;
45
46 { poczatkowy Subnode, inicjowany na poczatku na SubnodeWithPoint(FirstBoxIntr).
47 Moze byc modyfikowany w pewnym specjalnym przypadku przy okazji obliczania
48 PlaneIntersects }
49 StartSubnode: TOctreeSubnodeIndex;
50
51 { Element [0] tej tablicy okesla przeciecie z plaszczyzna X = MiddlePoint[0],
52 element [1] - z plaszczyzna Y = MiddlePoint[1] itd.
53 Exists mowi czy w ogole promien przecina ta plaszczyzne,
54 jezeli tak to IntrT to T z jakim zaszlo intersection miedzy
55 promieniem FirstBoxIntr, RayDirection (lub odcinkiem FirstBoxIntr, Pos2).
56 Czyli im wieksze T tym dalej przeciecie jest od FirstBoxIntr a wiec
57 i od RayOrigin/Pos1.
58 PlaneIntersectsCount to liczba planes ktore maja Exists.
59 PlaneIntersectsCount moze byc <= 3.}
60 PlaneIntersects: array[0..2]of record
61 Exists: boolean;
62 PlaneIntersectionDistance: Single;
63 end;
64 ExistingPlaneIntersectsCount: integer;
65
66 { posortowane wzgledem SqrDistanceFromRayOrigin (od najmniejszego do najwiekszego)
67 PlaneIntersects, tzn. SortedPlaneIntersects[0] to indeks
68 najmniejszego PlaneIntersects, itd. az
69 do SortedPlaneIntersects[ExistingPlaneIntersectsCount-1] }
70 SortedPlaneIntersects: array[0..2]of integer;
71
72 { SubnodesToCheck to ulozone w kolejnosci Subnode'y ktore powinnismy
73 sprawdzic czy jest w nich przeciecie promienia ze scena. Tzn. najpierw
74 powinnismy sprawdzic Subnode nr 0 (w nim jest punkt RayOrigin), potem
75 Subnode 1 itd - az do SubnodesToCheckCount-1.
76 Jak widac z rozmiaru tej tablicy wiemy ze bedziemy musieli wejsc
77 w maksymalnie 4 subnode'y - bo przeciez bedziemy mieli maksymalnie
78 3 przeciecia z 3 plaszczyznami dzielacymi ten wezel drzewa, co dzieli
79 nam promien na maksymalnie 4 segmenty. }
80 SubnodesToCheck: array[0..3]of TOctreeSubnodeIndex;
81 SubnodesToCheckCount: integer;
82
83 procedure CalculatePlaneIntersects;
84 var i: integer;
85 PlaneIntersect: TVector3;
86 begin
87 ExistingPlaneIntersectsCount := 0;
88
89 for i := 0 to 2 do
90 with PlaneIntersects[i] do
91 begin
92 if SameValue(Box.Data[1].Data[I], Box.Data[0].Data[I]) then
93 begin
94 { This means that the Box is practically flat, so all 3 planes
95 (Box.Data[0].Data[I], MiddlePoint[I], Box.Data[1].Data[I]) are practically
96 coplanar.
97
98 We want in this case to make sure that we actually
99 enter both sides of MiddlePoint[I] plane (since it's pretty random
100 where did we put some flat triangles). So we want to make
101 sure Exists is set to @true.
102
103 Starting StartSubnode[i] should be the one closer the RayOrigin.
104
105 See demo_models/vrml_1/flat_triangles_octree_test.wrl
106 for example when this is needed (with new okDynamicCollisions,
107 this plane is flat within it's octree, even when it's transformed
108 by rotation). }
109
110 { We could here just use TrySimplePlaneRayIntersection always.
111 But that would be unoptimal for segment, since a ray may collide while
112 segment doesn't. }
113
114 Exists:=
115 {$ifdef SEGMENT_COLLISION}
116 TrySimplePlaneSegmentIntersection(PlaneIntersect, PlaneIntersectionDistance,
117 i, MiddlePoint[i], FirstBoxIntr, Pos2)
118 {$else}
119 TrySimplePlaneRayIntersection(PlaneIntersect, PlaneIntersectionDistance,
120 i, MiddlePoint[i], FirstBoxIntr, RayDirection)
121 {$endif};
122
123 if not Exists then
124 begin
125 { Just force Exists to true.
126 We know that FirstBoxIntr is practically lying on MiddlePoint[I] plane,
127 so it's a good candidate. }
128
129 Exists := true;
130 PlaneIntersect := FirstBoxIntr;
131 PlaneIntersectionDistance := 0;
132 end;
133
134 StartSubnode[i] := RayDirection[i] <= 0;
135 end else
136 if IsZero(PointToSimplePlaneDistance(FirstBoxIntr, i, MiddlePoint[i])) and
137 not IsZero(RayDirection[i]) then
138 begin
139 { This means that FirstBoxIntr lies on MiddlePoint[I] plane.
140 Although the box is not flat (this case was eliminated above).
141 Which means that the ray
142 somehow enters the box exactly in the middle, or maybe the
143 RayOrigin was exactly in the middle (e.g. consider RayOrigin = exactly
144 middle point of octree box).
145
146 We handle it specially, because normal code would be numerically
147 unstable: on one hand, StartSubnode may be detected on any side
148 --- it's practically random. On the other hand, whether
149 Exists is set is also random. So various strange things
150 could happen (e.g. StartSubNode is on the farther (RayDirection) side,
151 but intersection is detected too, so we incorrectly go back;
152 or StartSubNode is on the closer (-RayDirection) side,
153 but intersection is not detected, so we incorrectly do not change
154 sides).
155
156 The correct solution in this case is to have StartSubNode
157 on the farther side, and do not see intersection with middle plane. }
158 StartSubnode[i] := RayDirection[i] > 0;
159 Exists := false;
160 end else
161 begin
162 { jezeli przeciecie nie znajduje sie w naszym Box to na pewno nie
163 chcemy go dolaczyc do PlaneIntersect wiec ustawiamy Exists := false;
164 Dzieki temu ze badamy przeciecia od FirstBoxIntr a nie od RayOrigin
165 (a nie od Pos1, w przypadku odcinka) to
166 wiemy ze przeciecie jest juz POTEM jak promien wyjdzie z naszego
167 Boxa (a nie ZANIM promien wszedl do naszego Boxa, co byloby tu troche
168 klopotliwe;) }
169 Exists:=
170 {$ifdef SEGMENT_COLLISION}
171 TrySimplePlaneSegmentIntersection(PlaneIntersect, PlaneIntersectionDistance,
172 i, MiddlePoint[i], FirstBoxIntr, Pos2)
173 {$else}
174 TrySimplePlaneRayIntersection(PlaneIntersect, PlaneIntersectionDistance,
175 i, MiddlePoint[i], FirstBoxIntr, RayDirection)
176 {$endif}
177 and Box.Contains(PlaneIntersect);
178 end;
179
180 if Exists then Inc(ExistingPlaneIntersectsCount);
181 end;
182 end;
183
184 procedure CalculateSortedPlaneIntersects;
185 var SortedPlaneIntersectsCount: integer;
186
187 procedure NextIntersect(c: integer);
188 begin
189 SortedPlaneIntersects[SortedPlaneIntersectsCount] := c;
190 Inc(SortedPlaneIntersectsCount);
191 end;
192
193 var
194 C1, C2, CM: T3DAxis;
195 begin
196 SortedPlaneIntersectsCount := 0;
197
198 {wyznacz c1 i c2, ew. wrzucajac juz pierwsze przeciecie do SortedPlaneIntersects}
199 if ExistingPlaneIntersectsCount = 3 then
200 begin
201 cm := IndexMin(PlaneIntersects[0].PlaneIntersectionDistance,
202 PlaneIntersects[1].PlaneIntersectionDistance,
203 PlaneIntersects[2].PlaneIntersectionDistance);
204 RestOf3DCoords(CM, C1, C2);
205 NextIntersect(cm);
206 end else
207 if not PlaneIntersects[0].Exists then
208 begin c1 := 1; c2 := 2 end else
209 if not PlaneIntersects[1].Exists then
210 begin c1 := 0; c2 := 2 end else
211 {wiec wiemy ze (not PlaneIntersects[2].Exists)}
212 begin c1 := 0; c2 := 1 end;
213
214 case ExistingPlaneIntersectsCount-SortedPlaneIntersectsCount of
215 1: if PlaneIntersects[c1].Exists then
216 NextIntersect(c1) else
217 NextIntersect(c2);
218 2: begin
219 if PlaneIntersects[c1].PlaneIntersectionDistance <
220 PlaneIntersects[c2].PlaneIntersectionDistance then
221 begin NextIntersect(c1); NextIntersect(c2) end else
222 begin NextIntersect(c2); NextIntersect(c1) end;
223 end;
224 end;
225 end;
226
227 procedure CalculateSubnodesToCheck;
228 var i, IntersectPlane: integer;
229 begin
230 SubnodesToCheckCount := ExistingPlaneIntersectsCount+1;
231 SubnodesToCheck[0] := StartSubnode;
232 for i := 1 to ExistingPlaneIntersectsCount do
233 begin
234 SubnodesToCheck[i] := SubnodesToCheck[i-1];
235 { jezeli wchodzimy do tego Subnode'a z powodu przeciecia z plane dzielacym
236 o stalym X (czyli IntersectPlane = 0) to znaczy ze wspolrzedna X promienia
237 przechodzi na druga strone tej plaszczyzny (a pozostale wspolrzedne
238 zostaja w tych czesciach gdzie byly). Dlatego wystarczy zanegowac
239 zmienna b[IntersectPlane] w nowym plane. }
240 IntersectPlane := SortedPlaneIntersects[i-1];
241 SubnodesToCheck[i, IntersectPlane] := not SubnodesToCheck[i, IntersectPlane];
242 end;
243 end;
244
245 var
246 i: integer;
247 SubNode: TBaseTrianglesOctreeNode;
248 begin
249 {$ifdef SEGMENT_COLLISION}
250 RayDirection := Pos2 - Pos1;
251 {$endif}
252
253 if not Box.TryRayEntrance(FirstBoxIntr, RayOrigin, RayDirection) then
254 Exit(nil);
255 StartSubnode := SubnodeWithPoint(FirstBoxIntr);
256
257 CalculatePlaneIntersects;
258 CalculateSortedPlaneIntersects;
259 CalculateSubnodesToCheck;
260
261 { szukamy najblizszego Intersection wchodzac w SubNodes. Zauwazmy ze
262 w calym DoNotLeaf zawsze dzialamy jakby ReturnClosestIntersection = true
263 za wyjatkiem malego szczegoliku ponizej przy "if result <> nil ...". }
264 result := nil;
265 for i := 0 to SubnodesToCheckCount - 1 do
266 begin
267 SubNode := TBaseTrianglesOctreeNode(TreeSubNodes[
268 SubnodesToCheck[i, 0],
269 SubnodesToCheck[i, 1],
270 SubnodesToCheck[i, 2]]);
271 result := SubNode.
272 { jest ponizej bardzo wazne zebysmy nie zmienili Pos1 ani RayOrigin wywolujac
273 sie rekurencyjnie (np. zeby nie przekazac FirstBoxIntr) bo to
274 spowodowaloby ze zmienna IgnoreMarginAtStart powinna byc (byc moze)
275 ustawiona na false. }
276 {$ifdef SEGMENT_COLLISION}
277 CommonSegment(Intersection, IntersectionDistance, Pos1, Pos2,
278 Tag,
279 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
280 TrianglesToIgnoreFunc)
281 {$else}
282 CommonRay(Intersection, IntersectionDistance, RayOrigin, RayDirection,
283 Tag,
284 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
285 TrianglesToIgnoreFunc)
286 {$endif}
287 ;
288
289 { musimy sie tu upewnic ze zwrocone Intersection jest rzeczywiscie
290 w pytanym SubnodesToCheck[i]. Jezeli nie to znaczy ze to przeciecie
291 nalezy do innego subnodea'a i to on je zwroci (a my nie powinnismy
292 teraz wychodzic jesli ReturnClosestIntersection bo byc moze to przeciecie
293 nie jest najblizszym z mozliwych) }
294 if (result <> nil) and
295 ((not ReturnClosestIntersection) or
296 Box3DContainsTolerant(Intersection, SubNode.Box)) then
297 Exit;
298 end;
299 end;
300
301 begin
302 if not IsLeaf then
303 Result := DoNotLeaf else
304 Result :=
305 {$ifdef SEGMENT_COLLISION}
306 CommonSegmentLeaf(Intersection, IntersectionDistance, Pos1, Pos2,
307 Tag,
308 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
309 TrianglesToIgnoreFunc)
310 {$else}
311 CommonRayLeaf(Intersection, IntersectionDistance, RayOrigin, RayDirection,
312 Tag,
313 ReturnClosestIntersection, TriangleToIgnore, IgnoreMarginAtStart,
314 TrianglesToIgnoreFunc)
315 {$endif}
316 ;
317 end;