Skip to content

Commit 4035eff

Browse files
committed
Fix: Handle the possibility of an object with an invalid WPos in a quad tree
1 parent 65ec027 commit 4035eff

File tree

4 files changed

+41
-8
lines changed

4 files changed

+41
-8
lines changed

source/OpenBveApi/Math/Vectors/Vector2.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,14 @@ public void Rotate(double angle)
306306
public bool IsNullVector() {
307307
return this.X == 0.0 & this.Y == 0.0;
308308
}
309-
309+
310+
/// <summary>Tests to see whether the vector is finite (no components are double or infinity.</summary>
311+
/// <returns>A boolean indicating whether the vector is finite</returns>
312+
public static bool IsFinite(Vector2 Vector)
313+
{
314+
return !double.IsNaN(Vector.X) && !double.IsInfinity(Vector.X) && !double.IsNaN(Vector.Y) && !double.IsInfinity(Vector.Y);
315+
}
316+
310317
/// <summary>Checks whether the vector is considered a null vector.</summary>
311318
/// <param name="tolerance">The highest absolute value that each component of the vector may have before the vector is not considered a null vector.</param>
312319
/// <returns>A boolean indicating whether the vector is considered a null vector.</returns>

source/OpenBveApi/Math/Vectors/Vector3.cs

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using OpenBveApi.World;
3+
using SharpCompress.Common;
34
// ReSharper disable UnusedMember.Global
45

56
namespace OpenBveApi.Math {
@@ -656,10 +657,17 @@ public static bool AreColinear(Vector3 a, Vector3 b, Vector3 c) {
656657
public static bool IsNullVector(Vector3 vector) {
657658
return vector.X == 0.0 & vector.Y == 0.0 & vector.Z == 0.0;
658659
}
659-
660-
/// <summary>Gets the euclidean norm of the specified vector.</summary>
661-
/// <param name="vector">The vector.</param>
662-
/// <returns>The euclidean norm.</returns>
660+
661+
/// <summary>Tests to see whether the vector is finite (no components are double or infinity.</summary>
662+
/// <returns>A boolean indicating whether the vector is finite</returns>
663+
public static bool IsFinite(Vector3 Vector)
664+
{
665+
return !double.IsNaN(Vector.X) && !double.IsInfinity(Vector.X) && !double.IsNaN(Vector.Y) && !double.IsInfinity(Vector.Y) && !double.IsNaN(Vector.Z) && !double.IsInfinity(Vector.Z);
666+
}
667+
668+
/// <summary>Gets the euclidean norm of the specified vector.</summary>
669+
/// <param name="vector">The vector.</param>
670+
/// <returns>The euclidean norm.</returns>
663671
public static double Norm(Vector3 vector) {
664672
return System.Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z);
665673
}

source/OpenBveApi/Routes/QuadTree/QuadTree.Bounds.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace OpenBveApi.Routes
1+
using System;
2+
3+
namespace OpenBveApi.Routes
24
{
35
/// <summary>Represents some rectangular bounds on the grid.</summary>
46
public struct QuadTreeBounds
@@ -22,9 +24,25 @@ public struct QuadTreeBounds
2224
/// <param name="far">The far edge, i.e. the highest z-coordinate.</param>
2325
internal QuadTreeBounds(double left, double right, double near, double far)
2426
{
27+
if (double.IsInfinity(left))
28+
{
29+
throw new Exception("Cannot create a grid with infinity bounds: Left");
30+
}
2531
Left = left;
32+
if (double.IsInfinity(right))
33+
{
34+
throw new Exception("Cannot create a grid with infinity bounds: Right");
35+
}
2636
Right = right;
37+
if (double.IsInfinity(near))
38+
{
39+
throw new Exception("Cannot create a grid with infinity bounds: Near");
40+
}
2741
Near = near;
42+
if (double.IsInfinity(far))
43+
{
44+
throw new Exception("Cannot create a grid with infinity bounds: Far");
45+
}
2846
Far = far;
2947
}
3048

source/OpenBveApi/Routes/QuadTree/QuadTree.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ public void Clear()
3737
/// <param name="orientation">The absolute world orientation of the object.</param>
3838
public void Add(ObjectState objectState, Orientation3 orientation)
3939
{
40-
if (Objects.Contains(objectState))
40+
if (Objects.Contains(objectState) || !Vector3.IsFinite(objectState.WorldPosition))
4141
{
42-
// object state is already in the quad tree
42+
// object state is already in the quad tree, or has an invalid world position
4343
return;
4444
}
4545
Objects.Add(objectState);

0 commit comments

Comments
 (0)