| 1 |
// -*- compile-command: "./test_single_testcase.sh TTestCastleBoxes" -*- |
| 2 |
{ |
| 3 |
Copyright 2007-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 |
{ Test CastleBoxes unit. } |
| 18 |
unit TestCastleBoxes; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses |
| 23 |
Classes, SysUtils, CastleTester; |
| 24 |
|
| 25 |
type |
| 26 |
TTestCastleBoxes = class(TCastleTestCase) |
| 27 |
published |
| 28 |
procedure TestIsCenteredBox3DPlaneCollision; |
| 29 |
procedure TestBox3DPlaneCollision; |
| 30 |
procedure TestIsBox3DTriangleCollision; |
| 31 |
procedure TestIsBox3DTriangleCollisionEpsilons; |
| 32 |
procedure TestBox3DTransform; |
| 33 |
procedure TestBoxTransform1; |
| 34 |
procedure TestBox3DMaximumPlane; |
| 35 |
procedure TestBox3DMinimumPlane; |
| 36 |
procedure TestBox3DPointDistance; |
| 37 |
procedure Test2D; |
| 38 |
procedure TestPointDistances; |
| 39 |
procedure TestDirectionDistances; |
| 40 |
end; |
| 41 |
|
| 42 |
implementation |
| 43 |
|
| 44 |
uses CastleVectors, CastleUtils, CastleBoxes, CastleStringUtils, CastleTimeUtils, |
| 45 |
CastleLog, |
| 46 |
TestCastleVectors, CastleTriangles; |
| 47 |
|
| 48 |
procedure TTestCastleBoxes.TestIsCenteredBox3DPlaneCollision; |
| 49 |
begin |
| 50 |
{ box 10, 1, 1 with a plane that crosses 0,0,0 point always collides } |
| 51 |
AssertTrue(IsCenteredBox3DPlaneCollision( |
| 52 |
Vector3(10, 1, 1), Vector4(0, 0, 1, 0))); |
| 53 |
AssertTrue(IsCenteredBox3DPlaneCollision( |
| 54 |
Vector3(10, 1, 1), Vector4(0, 1, 0, 0))); |
| 55 |
AssertTrue(IsCenteredBox3DPlaneCollision( |
| 56 |
Vector3(10, 1, 1), Vector4(1, 0, 0, 0))); |
| 57 |
AssertTrue(IsCenteredBox3DPlaneCollision( |
| 58 |
Vector3(10, 1, 1), Vector4(123, 456, 789, 0))); |
| 59 |
|
| 60 |
AssertTrue(not IsCenteredBox3DPlaneCollision( |
| 61 |
Vector3(10, 1, 1), Vector4(0, 0, -1, 5))); |
| 62 |
AssertTrue(not IsCenteredBox3DPlaneCollision( |
| 63 |
Vector3(10, 1, 1), Vector4(0, -1, 0, 5))); |
| 64 |
AssertTrue(IsCenteredBox3DPlaneCollision( |
| 65 |
Vector3(10, 1, 1), Vector4(-1, 0, 0, 5))); |
| 66 |
end; |
| 67 |
|
| 68 |
procedure TTestCastleBoxes.TestBox3DPlaneCollision; |
| 69 |
|
| 70 |
procedure AssertBox3DPlaneCollision(const Box: TBox3D; |
| 71 |
const Plane: TVector4; CollisionResult: TPlaneCollision); |
| 72 |
begin |
| 73 |
AssertTrue(Box.PlaneCollision(Plane) = CollisionResult); |
| 74 |
{ Check by the way Box3DPlaneCollisionInside, Box3DPlaneCollisionOutside } |
| 75 |
AssertTrue(Box.PlaneCollisionInside(Plane) = (CollisionResult = pcInside)); |
| 76 |
AssertTrue(Box.PlaneCollisionOutside(Plane) = (CollisionResult = pcOutside)); |
| 77 |
end; |
| 78 |
|
| 79 |
var |
| 80 |
Box: TBox3D; |
| 81 |
Plane: TVector4; |
| 82 |
begin |
| 83 |
Box.Data[0] := Vector3(-10, -1, -1); |
| 84 |
Box.Data[1] := Vector3( 10, 1, 1); |
| 85 |
|
| 86 |
{ box 10, 1, 1 with a plane that crosses 0,0,0 point always collides } |
| 87 |
AssertBox3DPlaneCollision( |
| 88 |
Box, Vector4(0, 0, 1, 0), pcIntersecting); |
| 89 |
AssertBox3DPlaneCollision( |
| 90 |
Box, Vector4(0, 1, 0, 0), pcIntersecting); |
| 91 |
AssertBox3DPlaneCollision( |
| 92 |
Box, Vector4(1, 0, 0, 0), pcIntersecting); |
| 93 |
AssertBox3DPlaneCollision( |
| 94 |
Box, Vector4(123, 456, 789, 0), pcIntersecting); |
| 95 |
|
| 96 |
{ test inside/outside difference } |
| 97 |
AssertBox3DPlaneCollision( |
| 98 |
Box3D(Vector3(-1, -1, 10), |
| 99 |
Vector3( 1, 1, 20)), |
| 100 |
Vector4(0, 0, 1, 0), pcOutside); |
| 101 |
AssertBox3DPlaneCollision( |
| 102 |
Box3D(Vector3(-1, -1, -20), |
| 103 |
Vector3( 1, 1, -10)), |
| 104 |
Vector4(0, 0, 1, 0), pcInside); |
| 105 |
|
| 106 |
{ basic test for pcNone } |
| 107 |
AssertBox3DPlaneCollision(TBox3D.Empty, |
| 108 |
Vector4(0, 0, 1, 0), pcNone); |
| 109 |
|
| 110 |
AssertBox3DPlaneCollision( |
| 111 |
Box, Vector4(0, 0, -1, 5), pcOutside); |
| 112 |
AssertBox3DPlaneCollision( |
| 113 |
Box, Vector4(0, -1, 0, 5), pcOutside); |
| 114 |
AssertBox3DPlaneCollision( |
| 115 |
Box, Vector4(-1, 0, 0, 5), pcIntersecting); |
| 116 |
|
| 117 |
Box := Box.Translate(Vector3(0, 1000, 0)); |
| 118 |
|
| 119 |
AssertBox3DPlaneCollision( |
| 120 |
Box, Vector4(0, 0, 1, 0), pcIntersecting); |
| 121 |
AssertBox3DPlaneCollision( |
| 122 |
Box, Vector4(0, 1, 0, 0), pcOutside); |
| 123 |
AssertBox3DPlaneCollision( |
| 124 |
Box, Vector4(1, 0, 0, 0), pcIntersecting); |
| 125 |
|
| 126 |
Plane.X := 0; |
| 127 |
Plane.Y := 0; |
| 128 |
Plane.Z := 1; |
| 129 |
Plane.W := 1.980401039E+00; |
| 130 |
Box.Data[0].X := 2.837333679E-01; |
| 131 |
Box.Data[0].Y := -9.844776917E+01; |
| 132 |
Box.Data[0].Z := -1.980401039E+00; |
| 133 |
Box.Data[1].X := 1.283623352E+02; |
| 134 |
Box.Data[1].Y := 3.240192413E+00; |
| 135 |
Box.Data[1].Z := 3.100979996E+01; |
| 136 |
AssertBox3DPlaneCollision(Box, Plane, pcIntersecting); |
| 137 |
end; |
| 138 |
|
| 139 |
procedure TTestCastleBoxes.TestIsBox3DTriangleCollision; |
| 140 |
|
| 141 |
procedure RandomTrianglesTest( |
| 142 |
const XRandomness, YRandomness, ZRandomness: Integer); |
| 143 |
var |
| 144 |
Triangle: TTriangle3; |
| 145 |
Box: TBox3D; |
| 146 |
V0, V1, V2: TVector3; |
| 147 |
I: Integer; |
| 148 |
begin |
| 149 |
{ random triangle completely within the box } |
| 150 |
Box.Data[0] := Vector3(10, 10, 10); |
| 151 |
Box.Data[1] := Vector3(20, 20, 20); |
| 152 |
for I := 1 to 50 do |
| 153 |
begin |
| 154 |
repeat |
| 155 |
Triangle.Data[0] := Vector3(12 + Random(8) * XRandomness, 12 + Random(8) * YRandomness, 12 + Random(8) * ZRandomness); |
| 156 |
Triangle.Data[1] := Vector3(12 + Random(8) * XRandomness, 12 + Random(8) * YRandomness, 12 + Random(8) * ZRandomness); |
| 157 |
Triangle.Data[2] := Vector3(12 + Random(8) * XRandomness, 12 + Random(8) * YRandomness, 12 + Random(8) * ZRandomness); |
| 158 |
until Triangle.IsValid; |
| 159 |
AssertTrue(Box.IsTriangleCollision(Triangle)); |
| 160 |
end; |
| 161 |
|
| 162 |
{ random triangle completely outside the box (but chosen to collide) } |
| 163 |
Box.Data[0] := Vector3(10, 10, 10); |
| 164 |
Box.Data[1] := Vector3(20, 20, 20); |
| 165 |
for I := 1 to 50 do |
| 166 |
begin |
| 167 |
repeat |
| 168 |
V0 := Vector3(10 + Random(10) * XRandomness, 10 + Random(10) * YRandomness, 10 + Random(10) * ZRandomness); |
| 169 |
V1 := Vector3(10 + Random(10) * XRandomness, 10 + Random(10) * YRandomness, 10 + Random(10) * ZRandomness); |
| 170 |
V2 := -(V0 + V1); |
| 171 |
Triangle.Data[0] := Vector3(15, 15, 15) + V0; |
| 172 |
Triangle.Data[1] := Vector3(15, 15, 15) + V1; |
| 173 |
Triangle.Data[2] := Vector3(15, 15, 15) + V2; |
| 174 |
until Triangle.IsValid; |
| 175 |
|
| 176 |
AssertTrue(Box.IsTriangleCollision(Triangle)); |
| 177 |
|
| 178 |
Triangle.Data[0] := Triangle.Data[0] + Vector3(100, 100, 100); |
| 179 |
Triangle.Data[1] := Triangle.Data[1] + Vector3(100, 100, 100); |
| 180 |
Triangle.Data[2] := Triangle.Data[2] + Vector3(100, 100, 100); |
| 181 |
AssertTrue(not Box.IsTriangleCollision(Triangle)); |
| 182 |
end; |
| 183 |
|
| 184 |
{ random triangle with 1 point inside the box, other 2 outside } |
| 185 |
Box.Data[0] := Vector3(10, 10, 10); |
| 186 |
Box.Data[1] := Vector3(20, 20, 20); |
| 187 |
for I := 1 to 50 do |
| 188 |
begin |
| 189 |
repeat |
| 190 |
V0 := Vector3(10 + Random(10) * XRandomness, 10 + Random(10) * YRandomness, 10 + Random(10) * ZRandomness); |
| 191 |
V1 := Vector3(10 + Random(10) * XRandomness, 10 + Random(10) * YRandomness, 10 + Random(10) * ZRandomness); |
| 192 |
Triangle.Data[0] := Vector3(15, 15, 15) + V0; |
| 193 |
Triangle.Data[1] := Vector3(15, 15, 15) + V1; |
| 194 |
Triangle.Data[2] := Vector3(15, 15, 15); |
| 195 |
until Triangle.IsValid; |
| 196 |
|
| 197 |
AssertTrue(Box.IsTriangleCollision(Triangle)); |
| 198 |
|
| 199 |
Triangle.Data[0] := Triangle.Data[0] + Vector3(100, 100, 100); |
| 200 |
Triangle.Data[1] := Triangle.Data[1] + Vector3(100, 100, 100); |
| 201 |
Triangle.Data[2] := Triangle.Data[2] + Vector3(100, 100, 100); |
| 202 |
AssertTrue(not Box.IsTriangleCollision(Triangle)); |
| 203 |
end; |
| 204 |
end; |
| 205 |
|
| 206 |
var |
| 207 |
Triangle: TTriangle3; |
| 208 |
Box: TBox3D; |
| 209 |
begin |
| 210 |
Triangle.Data[0] := Vector3(0, 0, 0); |
| 211 |
Triangle.Data[1] := Vector3(10, 0, 0); |
| 212 |
Triangle.Data[2] := Vector3(10, 10, 0); |
| 213 |
Box.Data[0] := Vector3(-10, -1, -1); |
| 214 |
Box.Data[1] := Vector3( 10, 1, 1); |
| 215 |
|
| 216 |
AssertTrue(Box.IsTriangleCollision(Triangle)); |
| 217 |
|
| 218 |
Box := Box.Translate(Vector3(0, 0, 0.5)); |
| 219 |
AssertTrue(Box.IsTriangleCollision(Triangle)); |
| 220 |
|
| 221 |
Box := Box.Translate(Vector3(0, 0, 1.0)); |
| 222 |
AssertTrue(not Box.IsTriangleCollision(Triangle)); |
| 223 |
|
| 224 |
RandomTrianglesTest(1, 1, 1); |
| 225 |
|
| 226 |
{ make one coord locked, and perform the same tests as above } |
| 227 |
RandomTrianglesTest(0, 1, 1); |
| 228 |
RandomTrianglesTest(1, 0, 1); |
| 229 |
RandomTrianglesTest(1, 1, 0); |
| 230 |
end; |
| 231 |
|
| 232 |
procedure TTestCastleBoxes.TestIsBox3DTriangleCollisionEpsilons; |
| 233 |
var |
| 234 |
Epsilon: Single; |
| 235 |
|
| 236 |
{ Modified version of IsCenteredBox3DPlaneCollision and IsBox3DTriangleCollision |
| 237 |
that use Epsilon variable here. Also, use Single precision calculations. } |
| 238 |
|
| 239 |
function IsCenteredBox3DPlaneCollision( |
| 240 |
const BoxHalfSize: TVector3; |
| 241 |
const Plane: TVector4): boolean; |
| 242 |
{ Implementation of this is based on |
| 243 |
[http://jgt.akpeters.com/papers/AkenineMoller01/tribox.html] |
| 244 |
planeBoxOverlap routine, by Tomas Akenine-Moller, |
| 245 |
mentioned in his paper [http://jgt.akpeters.com/papers/AkenineMoller01/] |
| 246 |
about "Fast 3D Triangle-Box Overlap Testing", downloadable from |
| 247 |
[http://www.cs.lth.se/home/Tomas_Akenine_Moller/pubs/tribox.pdf]. |
| 248 |
|
| 249 |
The idea: we need to test plane equation with only two points |
| 250 |
(instead of eight points, as in naive version). Think about the plane |
| 251 |
normal vector; imagine 8 box points projected on this vector; now |
| 252 |
we can find 2 box points, one that has minimal value when projected |
| 253 |
on normal vector, and one that has maximum value. Now you need to test |
| 254 |
is the plane between these two points. } |
| 255 |
var |
| 256 |
I: Integer; |
| 257 |
VMin, VMax: TVector3; |
| 258 |
begin |
| 259 |
for I := 0 to 2 do |
| 260 |
if Plane[I] > 0 then |
| 261 |
begin |
| 262 |
VMin.Data[I] := -BoxHalfSize[I]; |
| 263 |
VMax.Data[I] := BoxHalfSize[I]; |
| 264 |
end else |
| 265 |
begin |
| 266 |
VMin.Data[I] := BoxHalfSize[I]; |
| 267 |
VMax.Data[I] := -BoxHalfSize[I]; |
| 268 |
end; |
| 269 |
|
| 270 |
{ If VMin is above the plane (plane equation is > 0), then VMax |
| 271 |
is also above, no need to test anything else. } |
| 272 |
if Plane.X * VMin.X + |
| 273 |
Plane.Y * VMin.Y + |
| 274 |
Plane.Z * VMin.Z + |
| 275 |
Plane.W > Epsilon then |
| 276 |
Exit(false); |
| 277 |
|
| 278 |
{ So VMin is <= plane. So if VMax is >= 0, then there's a collision. } |
| 279 |
Result := Plane.X * VMax.X + |
| 280 |
Plane.Y * VMax.Y + |
| 281 |
Plane.Z * VMax.Z + |
| 282 |
Plane.W >= Epsilon; |
| 283 |
end; |
| 284 |
|
| 285 |
function IsBox3DTriangleCollision( |
| 286 |
const Box: TBox3D; |
| 287 |
const Triangle: TTriangle3): boolean; |
| 288 |
|
| 289 |
{ Implementation of this is based on |
| 290 |
[http://jgt.akpeters.com/papers/AkenineMoller01/tribox.html], |
| 291 |
by Tomas Akenine-Moller, described |
| 292 |
in his paper [http://jgt.akpeters.com/papers/AkenineMoller01/] |
| 293 |
"Fast 3D Triangle-Box Overlap Testing", downloadable from |
| 294 |
[http://www.cs.lth.se/home/Tomas_Akenine_Moller/pubs/tribox.pdf]. |
| 295 |
|
| 296 |
Use separating axis theorem to test overlap between triangle and box |
| 297 |
need to test for overlap in these directions: |
| 298 |
1) the (x,y,z)-directions |
| 299 |
2) normal of the triangle |
| 300 |
3) crossproduct(edge from tri, (x,y,z)-direction) |
| 301 |
this gives 3x3=9 more tests |
| 302 |
} |
| 303 |
var |
| 304 |
TriangleMoved: TTriangle3; |
| 305 |
BoxHalfSize: TVector3; |
| 306 |
|
| 307 |
{ ======================== X-tests ======================== } |
| 308 |
function AXISTEST_X01(const a, b, fa, fb: Single): boolean; |
| 309 |
var |
| 310 |
p0, p2, rad, min, max: Single; |
| 311 |
begin |
| 312 |
p0 := a * TriangleMoved.Data[0].Y - b * TriangleMoved.Data[0].Z; |
| 313 |
p2 := a * TriangleMoved.Data[2].Y - b * TriangleMoved.Data[2].Z; |
| 314 |
if p0<p2 then begin min := p0; max := p2; end else |
| 315 |
begin min := p2; max := p0; end; |
| 316 |
rad := fa * BoxHalfSize.Y + fb * BoxHalfSize.Z; |
| 317 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 318 |
end; |
| 319 |
|
| 320 |
function AXISTEST_X2(const a, b, fa, fb: Single): boolean; |
| 321 |
var |
| 322 |
p0, p1, rad, min, max: Single; |
| 323 |
begin |
| 324 |
p0 := a * TriangleMoved.Data[0].Y - b * TriangleMoved.Data[0].Z; |
| 325 |
p1 := a * TriangleMoved.Data[1].Y - b * TriangleMoved.Data[1].Z; |
| 326 |
if p0<p1 then begin min := p0; max := p1; end else |
| 327 |
begin min := p1; max := p0; end; |
| 328 |
rad := fa * BoxHalfSize.Y + fb * BoxHalfSize.Z; |
| 329 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 330 |
end; |
| 331 |
|
| 332 |
{ ======================== Y-tests ======================== } |
| 333 |
function AXISTEST_Y02(const a, b, fa, fb: Single): boolean; |
| 334 |
var |
| 335 |
p0, p2, rad, min, max: Single; |
| 336 |
begin |
| 337 |
p0 := -a * TriangleMoved.Data[0].X + b * TriangleMoved.Data[0].Z; |
| 338 |
p2 := -a * TriangleMoved.Data[2].X + b * TriangleMoved.Data[2].Z; |
| 339 |
if p0<p2 then begin min := p0; max := p2; end else |
| 340 |
begin min := p2; max := p0; end; |
| 341 |
rad := fa * BoxHalfSize.X + fb * BoxHalfSize.Z; |
| 342 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 343 |
end; |
| 344 |
|
| 345 |
function AXISTEST_Y1(const a, b, fa, fb: Single): boolean; |
| 346 |
var |
| 347 |
p0, p1, rad, min, max: Single; |
| 348 |
begin |
| 349 |
p0 := -a * TriangleMoved.Data[0].X + b * TriangleMoved.Data[0].Z; |
| 350 |
p1 := -a * TriangleMoved.Data[1].X + b * TriangleMoved.Data[1].Z; |
| 351 |
if p0<p1 then begin min := p0; max := p1; end else |
| 352 |
begin min := p1; max := p0; end; |
| 353 |
rad := fa * BoxHalfSize.X + fb * BoxHalfSize.Z; |
| 354 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 355 |
end; |
| 356 |
|
| 357 |
{ ======================== Z-tests ======================== } |
| 358 |
function AXISTEST_Z12(const a, b, fa, fb: Single): boolean; |
| 359 |
var |
| 360 |
p1, p2, rad, min, max: Single; |
| 361 |
begin |
| 362 |
p1 := a * TriangleMoved.Data[1].X - b * TriangleMoved.Data[1].Y; |
| 363 |
p2 := a * TriangleMoved.Data[2].X - b * TriangleMoved.Data[2].Y; |
| 364 |
if p2<p1 then begin min := p2; max := p1; end else |
| 365 |
begin min := p1; max := p2; end; |
| 366 |
rad := fa * BoxHalfSize.X + fb * BoxHalfSize.Y; |
| 367 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 368 |
end; |
| 369 |
|
| 370 |
function AXISTEST_Z0(const a, b, fa, fb: Single): boolean; |
| 371 |
var |
| 372 |
p0, p1, rad, min, max: Single; |
| 373 |
begin |
| 374 |
p0 := a * TriangleMoved.Data[0].X - b * TriangleMoved.Data[0].Y; |
| 375 |
p1 := a * TriangleMoved.Data[1].X - b * TriangleMoved.Data[1].Y; |
| 376 |
if p0<p1 then begin min := p0; max := p1; end else |
| 377 |
begin min := p1; max := p0; end; |
| 378 |
rad := fa * BoxHalfSize.X + fb * BoxHalfSize.Y; |
| 379 |
Result := (min > rad + Epsilon) or (max < -rad - Epsilon); |
| 380 |
end; |
| 381 |
|
| 382 |
procedure FindMinMax(const x0, x1, x2: Single; out min, max: Single); |
| 383 |
begin |
| 384 |
min := x0; |
| 385 |
max := x0; |
| 386 |
if x1 < min then min := x1 else |
| 387 |
if x1 > max then max := x1; |
| 388 |
if x2 < min then min := x2 else |
| 389 |
if x2 > max then max := x2; |
| 390 |
end; |
| 391 |
|
| 392 |
var |
| 393 |
BoxCenter: TVector3; |
| 394 |
I: Integer; |
| 395 |
TriangleEdges: array [0..2] of TVector3; |
| 396 |
EdgeAbs: TVector3; |
| 397 |
min, max: Single; |
| 398 |
Plane: TVector4; |
| 399 |
PlaneDir: TVector3 absolute Plane; |
| 400 |
begin |
| 401 |
if Box.IsEmpty then |
| 402 |
Exit(false); |
| 403 |
|
| 404 |
{ calculate BoxCenter and BoxHalfSize } |
| 405 |
for I := 0 to 2 do |
| 406 |
begin |
| 407 |
BoxCenter.Data[I] := (Box.Data[0].Data[I] + Box.Data[1].Data[I]) / 2; |
| 408 |
BoxHalfSize.Data[I] := (Box.Data[1].Data[I] - Box.Data[0].Data[I]) / 2; |
| 409 |
end; |
| 410 |
|
| 411 |
{ calculate TriangleMoved (Triangle shifted by -BoxCenter, |
| 412 |
so that we can treat the BoxHalfSize as centered around origin) } |
| 413 |
TriangleMoved.Data[0] := Triangle.Data[0] - BoxCenter; |
| 414 |
TriangleMoved.Data[1] := Triangle.Data[1] - BoxCenter; |
| 415 |
TriangleMoved.Data[2] := Triangle.Data[2] - BoxCenter; |
| 416 |
|
| 417 |
{ calculate TriangleMoved edges } |
| 418 |
TriangleEdges[0] := TriangleMoved.Data[1] - TriangleMoved.Data[0]; |
| 419 |
TriangleEdges[1] := TriangleMoved.Data[2] - TriangleMoved.Data[1]; |
| 420 |
TriangleEdges[2] := TriangleMoved.Data[0] - TriangleMoved.Data[2]; |
| 421 |
|
| 422 |
{ tests 3) } |
| 423 |
EdgeAbs.X := Abs(TriangleEdges[0].X); |
| 424 |
EdgeAbs.Y := Abs(TriangleEdges[0].Y); |
| 425 |
EdgeAbs.Z := Abs(TriangleEdges[0].Z); |
| 426 |
if AXISTEST_X01(TriangleEdges[0].Z, TriangleEdges[0].Y, EdgeAbs.Z, EdgeAbs.Y) then Exit(false); |
| 427 |
if AXISTEST_Y02(TriangleEdges[0].Z, TriangleEdges[0].X, EdgeAbs.Z, EdgeAbs.X) then Exit(false); |
| 428 |
if AXISTEST_Z12(TriangleEdges[0].Y, TriangleEdges[0].X, EdgeAbs.Y, EdgeAbs.X) then Exit(false); |
| 429 |
|
| 430 |
EdgeAbs.X := Abs(TriangleEdges[1].X); |
| 431 |
EdgeAbs.Y := Abs(TriangleEdges[1].Y); |
| 432 |
EdgeAbs.Z := Abs(TriangleEdges[1].Z); |
| 433 |
if AXISTEST_X01(TriangleEdges[1].Z, TriangleEdges[1].Y, EdgeAbs.Z, EdgeAbs.Y) then Exit(false); |
| 434 |
if AXISTEST_Y02(TriangleEdges[1].Z, TriangleEdges[1].X, EdgeAbs.Z, EdgeAbs.X) then Exit(false); |
| 435 |
if AXISTEST_Z0 (TriangleEdges[1].Y, TriangleEdges[1].X, EdgeAbs.Y, EdgeAbs.X) then Exit(false); |
| 436 |
|
| 437 |
EdgeAbs.X := Abs(TriangleEdges[2].X); |
| 438 |
EdgeAbs.Y := Abs(TriangleEdges[2].Y); |
| 439 |
EdgeAbs.Z := Abs(TriangleEdges[2].Z); |
| 440 |
if AXISTEST_X2 (TriangleEdges[2].Z, TriangleEdges[2].Y, EdgeAbs.Z, EdgeAbs.Y) then Exit(false); |
| 441 |
if AXISTEST_Y1 (TriangleEdges[2].Z, TriangleEdges[2].X, EdgeAbs.Z, EdgeAbs.X) then Exit(false); |
| 442 |
if AXISTEST_Z12(TriangleEdges[2].Y, TriangleEdges[2].X, EdgeAbs.Y, EdgeAbs.X) then Exit(false); |
| 443 |
|
| 444 |
{ tests 1) |
| 445 |
first test overlap in the (x,y,z)-directions |
| 446 |
find min, max of the triangle each direction, and test for overlap in |
| 447 |
that direction -- this is equivalent to testing a minimal AABB around |
| 448 |
the triangle against the AABB } |
| 449 |
|
| 450 |
{ test in X-direction } |
| 451 |
FindMinMax(TriangleMoved.Data[0].X, TriangleMoved.Data[1].X, TriangleMoved.Data[2].X, min, max); |
| 452 |
if (min > boxhalfsize.X + Epsilon) or |
| 453 |
(max < -boxhalfsize.X - Epsilon) then Exit(false); |
| 454 |
|
| 455 |
{ test in Y-direction } |
| 456 |
FindMinMax(TriangleMoved.Data[0].Y, TriangleMoved.Data[1].Y, TriangleMoved.Data[2].Y, min, max); |
| 457 |
if (min > boxhalfsize.Y + Epsilon) or |
| 458 |
(max < -boxhalfsize.Y - Epsilon) then Exit(false); |
| 459 |
|
| 460 |
{ test in Z-direction } |
| 461 |
FindMinMax(TriangleMoved.Data[0].Z, TriangleMoved.Data[1].Z, TriangleMoved.Data[2].Z, min, max); |
| 462 |
if (min > boxhalfsize.Z + Epsilon) or |
| 463 |
(max < -boxhalfsize.Z - Epsilon) then Exit(false); |
| 464 |
|
| 465 |
{ tests 2) |
| 466 |
test if the box intersects the plane of the triangle |
| 467 |
compute plane equation of triangle: normal*x+d=0 } |
| 468 |
PlaneDir := TVector3.CrossProduct(TriangleEdges[0], TriangleEdges[1]); |
| 469 |
Plane.W := -TVector3.DotProduct(PlaneDir, TriangleMoved.Data[0]); |
| 470 |
if not IsCenteredBox3DPlaneCollision(BoxHalfSize, Plane) then |
| 471 |
Exit(false); |
| 472 |
|
| 473 |
Result := true; { box and triangle overlaps } |
| 474 |
end; |
| 475 |
|
| 476 |
var |
| 477 |
Triangle: TTriangle3; |
| 478 |
Box: TBox3D; |
| 479 |
|
| 480 |
procedure DoTest(const TestName: string; CorrectResult: boolean); |
| 481 |
begin |
| 482 |
try |
| 483 |
{ These writelns were used to experimentally check that 1e-4 is still too small, |
| 484 |
and 1e-3 is enough for these tests... That's assuming that we have |
| 485 |
IsBox3DTriangleCollision implementation based on Single type. } |
| 486 |
{ |
| 487 |
Epsilon := 1e-3; |
| 488 |
Write(IsBox3DTriangleCollision(Box, Triangle), ' '); |
| 489 |
Epsilon := 1e-6; |
| 490 |
Write(IsBox3DTriangleCollision(Box, Triangle), ' '); |
| 491 |
Write(CorrectResult, ' '); |
| 492 |
Write(IsBox3DTriangleCollision(Box, Triangle), ' '); |
| 493 |
Writeln;} |
| 494 |
AssertTrue(IsBox3DTriangleCollision(Box, Triangle) = CorrectResult); |
| 495 |
AssertTrue(Box.IsTriangleCollision(Triangle) = CorrectResult); |
| 496 |
except |
| 497 |
on E: EAssertionFailedError do |
| 498 |
begin |
| 499 |
E.Message := E.Message + ' (test name: ' + TestName + ')'; |
| 500 |
raise; |
| 501 |
end; |
| 502 |
end; |
| 503 |
end; |
| 504 |
|
| 505 |
const |
| 506 |
A = 1.980401039123535; |
| 507 |
var |
| 508 |
OldEpsilon: Single; |
| 509 |
begin |
| 510 |
Epsilon := 1e-5; |
| 511 |
|
| 512 |
Box.Data[0].X := -7.721179485321045; |
| 513 |
Box.Data[0].Y := -3.115305423736572; |
| 514 |
Box.Data[0].Z := 26.886024475097656; |
| 515 |
Box.Data[1].X := 0.283733367919922; |
| 516 |
Box.Data[1].Y := 3.240192413330078; |
| 517 |
Box.Data[1].Z := 28.947912216186523; |
| 518 |
Triangle.Data[0].X := -7.759810924530029; |
| 519 |
Triangle.Data[0].Y := 6.43093835606123E-006; |
| 520 |
Triangle.Data[0].Z := 28.172618865966797; |
| 521 |
Triangle.Data[1].X := -7.610710620880127; |
| 522 |
Triangle.Data[1].Y := -1.513858914375305; |
| 523 |
Triangle.Data[1].Z := 31.17262077331543; |
| 524 |
Triangle.Data[2].X := -7.759810924530029; |
| 525 |
Triangle.Data[2].Y := 6.43093835606123E-006; |
| 526 |
Triangle.Data[2].Z := 31.17262077331543; |
| 527 |
(* DoTest('1', false |
| 528 |
{ TODO: |
| 529 |
Not sure what the result should be... ? But it sure depends on the epsilon used in |
| 530 |
IsBox3DTriangleCollision. Test on Double values shows that this should be false. |
| 531 |
}); *) |
| 532 |
|
| 533 |
Box.Data[0].X := 0.283733367919922; |
| 534 |
Box.Data[0].Y := -98.447769165039062; |
| 535 |
Box.Data[0].Z := -A; |
| 536 |
Box.Data[1].X := 128.36233520507812; |
| 537 |
Box.Data[1].Y := 3.240192413330078; |
| 538 |
Box.Data[1].Z := 31.009799957275391; |
| 539 |
Triangle.Data[0].X := 25.288267135620117; |
| 540 |
Triangle.Data[0].Y := 8.671939849853516; |
| 541 |
Triangle.Data[0].Z := -A; |
| 542 |
Triangle.Data[1].X := 16.125827789306641; |
| 543 |
Triangle.Data[1].Y := -21.297039031982422; |
| 544 |
Triangle.Data[1].Z := -A; |
| 545 |
Triangle.Data[2].X := 19.586576461791992; |
| 546 |
Triangle.Data[2].Y := -26.554182052612305; |
| 547 |
Triangle.Data[2].Z := -A; |
| 548 |
|
| 549 |
{ Looks like for this test, even larger Epsilon is needed. |
| 550 |
At least under x86_64 (tested on Linux with fpc 2.2.4 and trunk on 2009-08-21, |
| 551 |
tested again with FPC 3.1.1 from 2015-11). |
| 552 |
And probably on armel / armhf too. } |
| 553 |
OldEpsilon := Epsilon; |
| 554 |
Epsilon := 1e-3; |
| 555 |
|
| 556 |
// TODO: This doesn't work with Box.IsTriangleCollision based on Double. |
| 557 |
// Is this test even correct, maybe it should be false, if the more precise |
| 558 |
// calculation (although with stricter Epsilon) fails? |
| 559 |
// We need to calculate this in some independent way, to be sure... |
| 560 |
//DoTest('2', true); |
| 561 |
try |
| 562 |
AssertTrue(IsBox3DTriangleCollision(Box, Triangle) = true); |
| 563 |
except |
| 564 |
on E: EAssertionFailedError do |
| 565 |
begin |
| 566 |
E.Message := E.Message + ' (test name: 2)'; |
| 567 |
raise; |
| 568 |
end; |
| 569 |
end; |
| 570 |
|
| 571 |
Epsilon := OldEpsilon; |
| 572 |
|
| 573 |
Box.Data[0].X := 0.283733367919922; |
| 574 |
Box.Data[0].Y := -47.603790283203125; |
| 575 |
Box.Data[0].Z := -A; |
| 576 |
Box.Data[1].X := 64.323036193847656; |
| 577 |
Box.Data[1].Y := 3.240192413330078; |
| 578 |
Box.Data[1].Z := 14.514699935913086; |
| 579 |
{ Triangle as before } |
| 580 |
DoTest('3', true); |
| 581 |
|
| 582 |
Box.Data[0].X := 0.283733367919922; |
| 583 |
Box.Data[0].Y := -47.603790283203125; |
| 584 |
Box.Data[0].Z := -A; |
| 585 |
Box.Data[1].X := 32.303382873535156; |
| 586 |
Box.Data[1].Y := -22.181798934936523; |
| 587 |
Box.Data[1].Z := 6.267149448394775; |
| 588 |
{ Triangle as before } |
| 589 |
DoTest('4', true); |
| 590 |
|
| 591 |
Box.Data[0].X := 16.293558120727539; |
| 592 |
Box.Data[0].Y := 3.240192413330078; |
| 593 |
Box.Data[0].Z := -A; |
| 594 |
Box.Data[1].X := 24.298469543457031; |
| 595 |
Box.Data[1].Y := 9.59568977355957; |
| 596 |
Box.Data[1].Z := 0.081486582756042; |
| 597 |
Triangle.Data[0].X := 25.288267135620117; |
| 598 |
Triangle.Data[0].Y := 8.671939849853516; |
| 599 |
Triangle.Data[0].Z := -A; |
| 600 |
Triangle.Data[1].X := -0.731123030185699; |
| 601 |
Triangle.Data[1].Y := 32.452774047851562; |
| 602 |
Triangle.Data[1].Z := -A; |
| 603 |
Triangle.Data[2].X := -0.382562607526779; |
| 604 |
Triangle.Data[2].Y := 26.646867752075195; |
| 605 |
Triangle.Data[2].Z := -A; |
| 606 |
DoTest('5', true); |
| 607 |
|
| 608 |
Box.Data[0].X := -17.727319717407227; |
| 609 |
Box.Data[0].Y := 4.829066753387451; |
| 610 |
Box.Data[0].Z := 5.751677513122559; |
| 611 |
Box.Data[1].X := -15.726092338562012; |
| 612 |
Box.Data[1].Y := 6.417941093444824; |
| 613 |
Box.Data[1].Z := 6.267149448394775; |
| 614 |
Triangle.Data[0].X := -6.18981409072876; |
| 615 |
Triangle.Data[0].Y := 2.234785079956055; |
| 616 |
Triangle.Data[0].Z := 29.618535995483398; |
| 617 |
Triangle.Data[1].X := -20.651203155517578; |
| 618 |
Triangle.Data[1].Y := 5.486495018005371; |
| 619 |
Triangle.Data[1].Z := -0.132393002510071; |
| 620 |
Triangle.Data[2].X := -6.149000644683838; |
| 621 |
Triangle.Data[2].Y := 2.083860397338867; |
| 622 |
Triangle.Data[2].Z := 29.618535995483398; |
| 623 |
(* DoTest('6', false |
| 624 |
{ TODO: |
| 625 |
Not sure what the result should be... ? But it sure depends on the epsilon used in |
| 626 |
IsBox3DTriangleCollision. Test on Double values shows that this should be false. |
| 627 |
}); *) |
| 628 |
end; |
| 629 |
|
| 630 |
function BoxTransformSlower(const Box: TBox3D; const Matrix: TMatrix4): TBox3D; |
| 631 |
var |
| 632 |
BoxPoints: TBoxCorners; |
| 633 |
i: integer; |
| 634 |
begin |
| 635 |
if Box.IsEmpty then |
| 636 |
Exit(TBox3D.Empty); |
| 637 |
|
| 638 |
Box.Corners(BoxPoints); |
| 639 |
for i := 0 to 7 do BoxPoints[i] := Matrix.MultPoint(BoxPoints[i]); |
| 640 |
|
| 641 |
{ Non-optimized version: |
| 642 |
Result := CalculateBoundingBox(@BoxPoints, 8, 0); |
| 643 |
|
| 644 |
But it turns out that the code below, that does essentially the same |
| 645 |
thing as CalculateBoundingBox implementation, works noticeably faster. |
| 646 |
This is noticeable on "The Castle" with many creatures: then a considerable |
| 647 |
time is spend inside TCreature.BoundingBox, that must calculate |
| 648 |
transformed bounding boxes. |
| 649 |
} |
| 650 |
|
| 651 |
Result.Data[0] := BoxPoints[0]; |
| 652 |
Result.Data[1] := BoxPoints[0]; |
| 653 |
for I := 1 to High(BoxPoints) do |
| 654 |
begin |
| 655 |
if BoxPoints[I].X < Result.Data[0].X then Result.Data[0].X := BoxPoints[I].X; |
| 656 |
if BoxPoints[I].Y < Result.Data[0].Y then Result.Data[0].Y := BoxPoints[I].Y; |
| 657 |
if BoxPoints[I].Z < Result.Data[0].Z then Result.Data[0].Z := BoxPoints[I].Z; |
| 658 |
if BoxPoints[I].X > Result.Data[1].X then Result.Data[1].X := BoxPoints[I].X; |
| 659 |
if BoxPoints[I].Y > Result.Data[1].Y then Result.Data[1].Y := BoxPoints[I].Y; |
| 660 |
if BoxPoints[I].Z > Result.Data[1].Z then Result.Data[1].Z := BoxPoints[I].Z; |
| 661 |
end; |
| 662 |
end; |
| 663 |
|
| 664 |
procedure TTestCastleBoxes.TestBox3DTransform; |
| 665 |
{ Test Box3DTransform for correctness and speed. |
| 666 |
Compare with Slower implementation, that should be slower |
| 667 |
(on non-projection matrices) but give the same results. } |
| 668 |
|
| 669 |
function RandomBox: TBox3D; |
| 670 |
var |
| 671 |
I: Integer; |
| 672 |
Val1, Val2: Single; |
| 673 |
begin |
| 674 |
for I := 0 to 2 do |
| 675 |
begin |
| 676 |
repeat |
| 677 |
Val1 := 50 - Random * 100; |
| 678 |
Val2 := 50 - Random * 100; |
| 679 |
until Abs(Val2 - Val1) > 1; // do not accept too close Val1 and Val2 values |
| 680 |
OrderUp(Val1, Val2); |
| 681 |
{$warnings off} // silence FPC warning about Normal uninitialized |
| 682 |
Result.Data[0].Data[I] := Val1; |
| 683 |
{$warnings on} |
| 684 |
Result.Data[1].Data[I] := Val2; |
| 685 |
end; |
| 686 |
end; |
| 687 |
|
| 688 |
var |
| 689 |
Box: TBox3D; |
| 690 |
I: Integer; |
| 691 |
Matrix: TMatrix4; |
| 692 |
begin |
| 693 |
for I := 0 to 1000 do |
| 694 |
begin |
| 695 |
Box := RandomBox; |
| 696 |
Matrix := RandomMatrix; |
| 697 |
try |
| 698 |
AssertBoxesEqual(BoxTransformSlower(Box, Matrix), Box.Transform(Matrix), 0.01); |
| 699 |
except |
| 700 |
on E: Exception do |
| 701 |
begin |
| 702 |
WritelnWarning('TestBox3DTransform failed at test with RandomMatrix:' + NL + |
| 703 |
'Box: %s' + NL + |
| 704 |
'Matrix: %s' + NL + |
| 705 |
'Resulting BoxTransformSlower: %s' + NL + |
| 706 |
'Resulting Box.Transform: %s', [ |
| 707 |
Box.ToString, |
| 708 |
Matrix.ToString, |
| 709 |
BoxTransformSlower(Box, Matrix).ToString, |
| 710 |
Box.Transform(Matrix).ToString |
| 711 |
]); |
| 712 |
raise; |
| 713 |
end; |
| 714 |
end; |
| 715 |
end; |
| 716 |
|
| 717 |
for I := 0 to 1000 do |
| 718 |
begin |
| 719 |
Box := RandomBox; |
| 720 |
Matrix := RandomNonProjectionMatrix; |
| 721 |
try |
| 722 |
AssertBoxesEqual(BoxTransformSlower(Box, Matrix), Box.Transform(Matrix), 0.01); |
| 723 |
except |
| 724 |
on E: Exception do |
| 725 |
begin |
| 726 |
WritelnWarning('TestBox3DTransform failed at test with RandomNonProjectionMatrix:' + NL + |
| 727 |
'Box: %s' + NL + |
| 728 |
'Matrix: %s', [ |
| 729 |
Box.ToString, |
| 730 |
Matrix.ToString |
| 731 |
]); |
| 732 |
raise; |
| 733 |
end; |
| 734 |
end; |
| 735 |
end; |
| 736 |
|
| 737 |
{ $define BOX3D_TRANSFORM_SPEED_TEST} |
| 738 |
{$ifdef BOX3D_TRANSFORM_SPEED_TEST} |
| 739 |
Writeln('On possibly projection matrix:'); |
| 740 |
|
| 741 |
Box := RandomBox; |
| 742 |
Matrix := RandomMatrix; |
| 743 |
|
| 744 |
ProcessTimerBegin; |
| 745 |
for I := 0 to 1000000 do BoxTransformSlower(Box, Matrix); |
| 746 |
Writeln(Format('Slower: %f', [ProcessTimerEnd])); |
| 747 |
|
| 748 |
ProcessTimerBegin; |
| 749 |
for I := 0 to 1000000 do Box3DTransform(Box, Matrix); |
| 750 |
Writeln(Format('Box3DTransform: %f', [ProcessTimerEnd])); |
| 751 |
|
| 752 |
Writeln('On non-projection matrix:'); |
| 753 |
|
| 754 |
Box := RandomBox; |
| 755 |
Matrix := RandomNonProjectionMatrix; |
| 756 |
|
| 757 |
ProcessTimerBegin; |
| 758 |
for I := 0 to 1000000 do BoxTransformSlower(Box, Matrix); |
| 759 |
Writeln(Format('Slower: %f', [ProcessTimerEnd])); |
| 760 |
|
| 761 |
ProcessTimerBegin; |
| 762 |
for I := 0 to 1000000 do Box3DTransform(Box, Matrix); |
| 763 |
Writeln(Format('Box3DTransform: %f', [ProcessTimerEnd])); |
| 764 |
{$endif BOX3D_TRANSFORM_SPEED_TEST} |
| 765 |
end; |
| 766 |
|
| 767 |
procedure TTestCastleBoxes.TestBoxTransform1; |
| 768 |
|
| 769 |
{ Failed case randomly generated on |
| 770 |
https://github.com/castle-engine/castle-engine/actions/runs/4406347867/jobs/7735050663 |
| 771 |
} |
| 772 |
|
| 773 |
var |
| 774 |
Box: TBox3D; |
| 775 |
Matrix: TMatrix4; |
| 776 |
begin |
| 777 |
Box.Data[0] := Vector3(-32.89, 14.65, -35.63); |
| 778 |
Box.Data[1] := Vector3(5.94, 17.09, -17.10); |
| 779 |
|
| 780 |
Matrix.Columns[0] := Vector4(-28.41, 5.40, 35.79, -3.68); |
| 781 |
Matrix.Columns[1] := Vector4(1.35, -11.64, -38.26, -20.80); |
| 782 |
Matrix.Columns[2] := Vector4(-26.66, -46.77, -10.37, -32.90); |
| 783 |
Matrix.Columns[3] := Vector4(10.26, -36.64, -14.72, -48.60); |
| 784 |
|
| 785 |
AssertBoxesEqual(BoxTransformSlower(Box, Matrix), Box.Transform(Matrix), 0.01); |
| 786 |
end; |
| 787 |
|
| 788 |
procedure TTestCastleBoxes.TestBox3DMaximumPlane; |
| 789 |
begin |
| 790 |
try |
| 791 |
TBox3D.Empty.MaximumPlane(Vector3(1, 1, 1)); |
| 792 |
except |
| 793 |
on E: EBox3DEmpty do { Ok }; |
| 794 |
end; |
| 795 |
|
| 796 |
AssertVectorEquals(Vector4(-1, 0, 0, 2), Box3D( |
| 797 |
Vector3(2, 3, 4), |
| 798 |
Vector3(50, 60, 70)).MaximumPlane(Vector3(-1, 0, 0))); |
| 799 |
|
| 800 |
AssertVectorEquals(Vector4(0, 0, -1, 4), Box3D( |
| 801 |
Vector3(2, 3, 4), |
| 802 |
Vector3(50, 60, 70)).MaximumPlane(Vector3(0, 0, -1))); |
| 803 |
|
| 804 |
AssertVectorEquals(Vector4(1, 1, 1, |
| 805 |
{ 50 + 60 + 70 + Result.Data.W = 0 } |
| 806 |
- 50 - 60 - 70 |
| 807 |
), Box3D( |
| 808 |
Vector3(2, 3, 4), |
| 809 |
Vector3(50, 60, 70)).MaximumPlane(Vector3(1, 1, 1))); |
| 810 |
end; |
| 811 |
|
| 812 |
procedure TTestCastleBoxes.TestBox3DMinimumPlane; |
| 813 |
begin |
| 814 |
try |
| 815 |
TBox3D.Empty.MinimumPlane(Vector3(1, 1, 1)); |
| 816 |
except |
| 817 |
on E: EBox3DEmpty do { Ok }; |
| 818 |
end; |
| 819 |
|
| 820 |
AssertVectorEquals(Vector4(1, 0, 0, -2), Box3D( |
| 821 |
Vector3(2, 3, 4), |
| 822 |
Vector3(50, 60, 70)).MinimumPlane(Vector3(1, 0, 0))); |
| 823 |
|
| 824 |
AssertVectorEquals(Vector4(0, 0, 1, -4), Box3D( |
| 825 |
Vector3(2, 3, 4), |
| 826 |
Vector3(50, 60, 70)).MinimumPlane(Vector3(0, 0, 1))); |
| 827 |
|
| 828 |
AssertVectorEquals(Vector4(1, 1, 1, |
| 829 |
{ 2 + 3 + 4 + Result.Data.W = 0 } |
| 830 |
- 2 - 3 - 4 |
| 831 |
), Box3D( |
| 832 |
Vector3(2, 3, 4), |
| 833 |
Vector3(50, 60, 70)).MinimumPlane(Vector3(1, 1, 1))); |
| 834 |
end; |
| 835 |
|
| 836 |
procedure TTestCastleBoxes.TestBox3DPointDistance; |
| 837 |
const |
| 838 |
Box: TBox3D = (Data: ( |
| 839 |
(X: 1; Y: 2; Z: 3), |
| 840 |
(X: 4; Y: 5; Z: 6) |
| 841 |
)); |
| 842 |
Epsilon = 0.0001; |
| 843 |
begin |
| 844 |
{ check point inside box case } |
| 845 |
AssertSameValue(0, Box.PointDistance(Vector3(1, 2, 3)), 0); |
| 846 |
AssertSameValue(0, Box.PointDistance(Vector3(3, 4, 5)), 0); |
| 847 |
{ check point <-> box side case } |
| 848 |
AssertSameValue(4, Box.PointDistance(Vector3(3, 4, 10))); |
| 849 |
AssertSameValue(3, Box.PointDistance(Vector3(3, 4, 0))); |
| 850 |
{ check point <-> box edge case } |
| 851 |
AssertSameValue(Sqrt( Sqr(10-6) + Sqr(10-5) ), |
| 852 |
Box.PointDistance(Vector3(3, 10, 10)), |
| 853 |
Epsilon); |
| 854 |
AssertSameValue(Sqrt( Sqr(0-2) + Sqr(0-3) ), |
| 855 |
Box.PointDistance(Vector3(3, 0, 0)), |
| 856 |
Epsilon); |
| 857 |
{ check point <-> box corner case } |
| 858 |
AssertSameValue(Sqrt( Sqr(10-6) + Sqr(10-5) + Sqr(10-4) ), |
| 859 |
Box.PointDistance(Vector3(10, 10, 10)), |
| 860 |
Epsilon); |
| 861 |
AssertSameValue(Sqrt( Sqr(0-2) + Sqr(0-3) + Sqr(0-1) ), |
| 862 |
Box.PointDistance(Vector3(0, 0, 0)), |
| 863 |
Epsilon); |
| 864 |
end; |
| 865 |
|
| 866 |
procedure TTestCastleBoxes.TestPointDistances; |
| 867 |
var |
| 868 |
MinDistance, MaxDistance: Single; |
| 869 |
begin |
| 870 |
Box3D( |
| 871 |
Vector3(10, 10, 10), |
| 872 |
Vector3(20, 20, 20) |
| 873 |
).PointDistances(Vector3(40, 40, 40), MinDistance, MaxDistance); |
| 874 |
AssertSameValue(PointsDistance( |
| 875 |
Vector3(40, 40, 40), |
| 876 |
Vector3(10, 10, 10)), MaxDistance); |
| 877 |
AssertSameValue(PointsDistance( |
| 878 |
Vector3(40, 40, 40), |
| 879 |
Vector3(20, 20, 20)), MinDistance); |
| 880 |
end; |
| 881 |
|
| 882 |
procedure TTestCastleBoxes.TestDirectionDistances; |
| 883 |
var |
| 884 |
MinDistance, MaxDistance: Single; |
| 885 |
begin |
| 886 |
Box3D( |
| 887 |
Vector3(10, 10, 10), |
| 888 |
Vector3(20, 20, 20) |
| 889 |
).DirectionDistances(Vector3(100, 100, 0), Vector3(0, 0, 1), MinDistance, MaxDistance); |
| 890 |
AssertSameValue(10, MinDistance); |
| 891 |
AssertSameValue(20, MaxDistance); |
| 892 |
|
| 893 |
(* MaxDistanceAlongDirection not used, so not defined for now. |
| 894 |
|
| 895 |
MaxDistance := Box3D( |
| 896 |
Vector3(10, 10, 10), |
| 897 |
Vector3(20, 20, 20) |
| 898 |
).MaxDistanceAlongDirection(Vector3(100, 100, 0), Vector3(0, 0, 1)); |
| 899 |
AssertSameValue(20, MaxDistance); |
| 900 |
*) |
| 901 |
|
| 902 |
Box3D( |
| 903 |
Vector3(10, 10, 10), |
| 904 |
Vector3(20, 20, 20) |
| 905 |
).DirectionDistances(Vector3(100, 100, 0), Vector3(0, 0, -1), MinDistance, MaxDistance); |
| 906 |
AssertSameValue(-20, MinDistance); |
| 907 |
AssertSameValue(-10, MaxDistance); |
| 908 |
|
| 909 |
(* MaxDistanceAlongDirection not used, so not defined for now. |
| 910 |
|
| 911 |
MaxDistance := Box3D( |
| 912 |
Vector3(10, 10, 10), |
| 913 |
Vector3(20, 20, 20) |
| 914 |
).MaxDistanceAlongDirection(Vector3(100, 100, 0), Vector3(0, 0, -1)); |
| 915 |
AssertSameValue(-10, MaxDistance); |
| 916 |
*) |
| 917 |
end; |
| 918 |
|
| 919 |
procedure TTestCastleBoxes.Test2D; |
| 920 |
const |
| 921 |
Box: TBox3D = (Data: ( |
| 922 |
(X: 1; Y: 2; Z: 3), |
| 923 |
(X: 4; Y: 5; Z: 6) |
| 924 |
)); |
| 925 |
Box2: TBox3D = (Data: ( |
| 926 |
(X: 1; Y: 2; Z: 3), |
| 927 |
(X: 2; Y: 5; Z: 13) |
| 928 |
)); |
| 929 |
begin |
| 930 |
AssertTrue(Box.Contains2D(Vector3(2, 3, 10), 2)); |
| 931 |
AssertTrue(not Box.Contains2D(Vector3(2, 3, 10), 0)); |
| 932 |
AssertTrue(not Box.Contains2D(Vector3(2, 3, 10), 1)); |
| 933 |
// This is disallowed at compile-time now, as argument is T3DAxis |
| 934 |
// try |
| 935 |
// Box.Contains2D(Vector3(2, 3, 10), 3); |
| 936 |
// Fail('Above Contains2D with IgnoreIndex = 3 should raise exception'); |
| 937 |
// except end; |
| 938 |
|
| 939 |
AssertSameValue(Sqrt(Sqr(5) + Sqr(6)), Box.Radius2D(0), 0.01); |
| 940 |
AssertSameValue(Sqrt(Sqr(4) + Sqr(6)), Box.Radius2D(1), 0.01); |
| 941 |
AssertSameValue(Sqrt(Sqr(4) + Sqr(5)), Box.Radius2D(2), 0.01); |
| 942 |
// This is disallowed at compile-time now, as argument is T3DAxis |
| 943 |
// try |
| 944 |
// Box.Radius2D(3); |
| 945 |
// Fail('Above Radius2D with IgnoreIndex = 3 should raise exception'); |
| 946 |
// except end; |
| 947 |
|
| 948 |
AssertSameValue(Sqrt(Sqr(4) + Sqr(5) + Sqr(6)), Box.Radius, 0.01); |
| 949 |
|
| 950 |
AssertSameValue(Sqrt(Sqr(3) + Sqr(3) + Sqr(3)), Box.Diagonal, 0.01); |
| 951 |
AssertSameValue(Sqrt(Sqr(1) + Sqr(10) + Sqr(3)), Box2.Diagonal, 0.01); |
| 952 |
end; |
| 953 |
|
| 954 |
initialization |
| 955 |
RegisterTest(TTestCastleBoxes); |
| 956 |
end. |