Skip to content

Commit

Permalink
Merge pull request #9 from DomCR/boundingbox-isIn
Browse files Browse the repository at this point in the history
Boundingbox is in
  • Loading branch information
DomCR authored Oct 17, 2024
2 parents 0a0a875 + 2e0fbe7 commit 03f926c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 7 deletions.
35 changes: 35 additions & 0 deletions CSMath.Tests/BoundingBoxTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Xunit;

namespace CSMath.Tests
{
public class BoundingBoxTests
{
[Theory]
[InlineData(0, 0, 0, true)]
[InlineData(11, 0, 0, false)]
[InlineData(11, 0, -11, false)]
[InlineData(5, 0, 5, true)]
[InlineData(10, 10, 10, true)]
public void IsInPointTest(double x, double y, double z, bool isIn)
{
BoundingBox box = new BoundingBox(-10, -10, -10, 10, 10, 10);

XYZ xyz = new XYZ(x, y, z);
Assert.Equal(isIn, box.IsIn(xyz));
}

[Theory]
[InlineData(-10, -10, -10, 10, 10, 10, true, true)]
[InlineData(-5, -5, -5, 5, 5, 5, true, true)]
[InlineData(-5, -5, -5, 5, 50, 50, false, true)]
[InlineData(-50, -60, -50, -11, -11, -11, false, false)]
public void IsInBoxTest(double minX, double minY, double minZ, double maxX, double maxY, double maxZ, bool isIn, bool partialInside)
{
BoundingBox box = new BoundingBox(-10, -10, -10, 10, 10, 10);

BoundingBox test = new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
Assert.Equal(isIn, box.IsIn(test, out bool partialyIn));
Assert.Equal(partialInside, partialyIn);
}
}
}
2 changes: 1 addition & 1 deletion CSMath.Tests/QuaternionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class QuaternionTests
[Fact]
public void CreateFromYawPitchRollTest()
{
XYZ xyz = new XYZ(Utilities.DegToRad(90), 0, 0);
XYZ xyz = new XYZ(MathHelper.DegToRad(90), 0, 0);
Quaternion q = Quaternion.CreateFromYawPitchRoll(xyz);
}
}
Expand Down
46 changes: 46 additions & 0 deletions CSMath/BoundingBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,52 @@ public BoundingBox Merge(BoundingBox box)
return new BoundingBox(min, max);
}

/// <summary>
/// Checks if the given box is in the bounds of this box.
/// </summary>
/// <param name="box"></param>
/// <returns></returns>
public bool IsIn(BoundingBox box)
{
return this.IsIn(box, out _);
}

/// <summary>
/// Checks if the given box is in the bounds of this box.
/// </summary>
/// <param name="box"></param>
/// <param name="partialIn">Flag to notify that on part of the box is inside but not completely.</param>
/// <returns></returns>
public bool IsIn(BoundingBox box, out bool partialIn)
{
bool min = this.IsIn(box.Min);
bool max = this.IsIn(box.Max);

partialIn = min || max;

return min && max;
}

/// <summary>
/// Checks if the point is in the bounds of the box.
/// </summary>
/// <param name="point"></param>
/// <returns></returns>
public bool IsIn(XYZ point)
{
if (this.Min.X > point.X || this.Min.Y > point.Y || this.Min.Z > point.Z)
{
return false;
}

if (this.Max.X < point.X || this.Max.Y < point.Y || this.Max.Z < point.Z)
{
return false;
}

return true;
}

/// <summary>
/// Merge Multiple boxes into the common one.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion CSMath/CSMath.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Compile Include="$(MSBuildThisFileDirectory)IVector.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix4.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Matrix4.Operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Utilities.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MathHelper.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Quaternion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Transform.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VectorExtensions.cs" />
Expand Down
2 changes: 1 addition & 1 deletion CSMath/Utilities.cs → CSMath/MathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace CSMath
{
public static class Utilities
public static class MathHelper
{
/// <summary>
/// Factor for converting radians to degrees.
Expand Down
6 changes: 3 additions & 3 deletions CSMath/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public Quaternion Quaternion
get
{
XYZ rot = new XYZ();
rot[0] = Utilities.DegToRad(this._rotation.X);
rot[1] = Utilities.DegToRad(this._rotation.Y);
rot[2] = Utilities.DegToRad(this._rotation.Z);
rot[0] = MathHelper.DegToRad(this._rotation.X);
rot[1] = MathHelper.DegToRad(this._rotation.Y);
rot[2] = MathHelper.DegToRad(this._rotation.Z);
return Quaternion.CreateFromYawPitchRoll(rot);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CSMath/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public static T RoundZero<T>(this T vector)

for (int i = 0; i < result.Dimension; i++)
{
result[i] = Utilities.IsZero(vector[i]) ? 0 : vector[i];
result[i] = MathHelper.IsZero(vector[i]) ? 0 : vector[i];
}

return result;
Expand Down

0 comments on commit 03f926c

Please sign in to comment.