Skip to content

Commit

Permalink
Fix: Handle the possibility of an object with an invalid WPos in a qu…
Browse files Browse the repository at this point in the history
…ad tree
  • Loading branch information
leezer3 committed Sep 25, 2024
1 parent 65ec027 commit 4035eff
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
9 changes: 8 additions & 1 deletion source/OpenBveApi/Math/Vectors/Vector2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,14 @@ public void Rotate(double angle)
public bool IsNullVector() {
return this.X == 0.0 & this.Y == 0.0;
}


/// <summary>Tests to see whether the vector is finite (no components are double or infinity.</summary>
/// <returns>A boolean indicating whether the vector is finite</returns>
public static bool IsFinite(Vector2 Vector)
{
return !double.IsNaN(Vector.X) && !double.IsInfinity(Vector.X) && !double.IsNaN(Vector.Y) && !double.IsInfinity(Vector.Y);
}

/// <summary>Checks whether the vector is considered a null vector.</summary>
/// <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>
/// <returns>A boolean indicating whether the vector is considered a null vector.</returns>
Expand Down
16 changes: 12 additions & 4 deletions source/OpenBveApi/Math/Vectors/Vector3.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using OpenBveApi.World;
using SharpCompress.Common;
// ReSharper disable UnusedMember.Global

namespace OpenBveApi.Math {
Expand Down Expand Up @@ -656,10 +657,17 @@ public static bool AreColinear(Vector3 a, Vector3 b, Vector3 c) {
public static bool IsNullVector(Vector3 vector) {
return vector.X == 0.0 & vector.Y == 0.0 & vector.Z == 0.0;
}

/// <summary>Gets the euclidean norm of the specified vector.</summary>
/// <param name="vector">The vector.</param>
/// <returns>The euclidean norm.</returns>

/// <summary>Tests to see whether the vector is finite (no components are double or infinity.</summary>
/// <returns>A boolean indicating whether the vector is finite</returns>
public static bool IsFinite(Vector3 Vector)
{
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);
}

/// <summary>Gets the euclidean norm of the specified vector.</summary>
/// <param name="vector">The vector.</param>
/// <returns>The euclidean norm.</returns>
public static double Norm(Vector3 vector) {
return System.Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z);
}
Expand Down
20 changes: 19 additions & 1 deletion source/OpenBveApi/Routes/QuadTree/QuadTree.Bounds.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OpenBveApi.Routes
using System;

namespace OpenBveApi.Routes
{
/// <summary>Represents some rectangular bounds on the grid.</summary>
public struct QuadTreeBounds
Expand All @@ -22,9 +24,25 @@ public struct QuadTreeBounds
/// <param name="far">The far edge, i.e. the highest z-coordinate.</param>
internal QuadTreeBounds(double left, double right, double near, double far)
{
if (double.IsInfinity(left))
{
throw new Exception("Cannot create a grid with infinity bounds: Left");
}
Left = left;
if (double.IsInfinity(right))
{
throw new Exception("Cannot create a grid with infinity bounds: Right");
}
Right = right;
if (double.IsInfinity(near))
{
throw new Exception("Cannot create a grid with infinity bounds: Near");
}
Near = near;
if (double.IsInfinity(far))
{
throw new Exception("Cannot create a grid with infinity bounds: Far");
}
Far = far;
}

Expand Down
4 changes: 2 additions & 2 deletions source/OpenBveApi/Routes/QuadTree/QuadTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public void Clear()
/// <param name="orientation">The absolute world orientation of the object.</param>
public void Add(ObjectState objectState, Orientation3 orientation)
{
if (Objects.Contains(objectState))
if (Objects.Contains(objectState) || !Vector3.IsFinite(objectState.WorldPosition))
{
// object state is already in the quad tree
// object state is already in the quad tree, or has an invalid world position
return;
}
Objects.Add(objectState);
Expand Down

0 comments on commit 4035eff

Please sign in to comment.