Skip to content

Commit

Permalink
BoundingBox
Browse files Browse the repository at this point in the history
  • Loading branch information
DomCR committed Aug 5, 2024
1 parent 95e916a commit eca6f7e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
54 changes: 53 additions & 1 deletion CSMath/BoundingBox.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
namespace CSMath
using System.Collections.Generic;

namespace CSMath
{
/// <summary>
/// Bounding box representation aligned to XYZ axis.
/// </summary>
public struct BoundingBox
{
/// <summary>
/// Instance of a null bounding box.
/// </summary>
public static readonly BoundingBox Null = new BoundingBox(BoundingBoxExtent.Null);

/// <summary>
/// Instance of an infinite bounding box.
/// </summary>
public static readonly BoundingBox Infinite = new BoundingBox(BoundingBoxExtent.Infinite);

public BoundingBoxExtent Extent { get; }

/// <summary>
/// Get the min corner of the bounding box.
/// </summary>
Expand All @@ -26,13 +40,34 @@ public XYZ Center
}
}

private BoundingBox(BoundingBoxExtent extent)
{
this.Extent = extent;
}

public BoundingBox(XYZ point)
{
this.Extent = BoundingBoxExtent.Point;
this.Min = point;
this.Max = point;
}

/// <summary>
/// Bounding box constructor with 2 points.
/// </summary>
/// <param name="min"></param>
/// <param name="max"></param>
public BoundingBox(XYZ min, XYZ max)
{
if (min == max)
{
this.Extent = BoundingBoxExtent.Point;
}
else
{
this.Extent = BoundingBoxExtent.Finite;
}

this.Min = new XYZ(System.Math.Min(min.X, max.X), System.Math.Min(min.Y, max.Y), System.Math.Min(min.Z, max.Z));
this.Max = new XYZ(System.Math.Max(min.X, max.X), System.Math.Max(min.Y, max.Y), System.Math.Max(min.Z, max.Z));
}
Expand Down Expand Up @@ -69,5 +104,22 @@ public BoundingBox Merge(BoundingBox box)

return new BoundingBox(min, max);
}

/// <summary>
/// Create a bounding box from a collection of points.
/// </summary>
/// <param name="points"></param>
/// <returns></returns>
public static BoundingBox FromPoints(IEnumerable<XYZ> points)
{
BoundingBox boundingBox = Null;

foreach (var point in points)
{
boundingBox = boundingBox.Merge(new BoundingBox(point));
}

return boundingBox;
}
}
}
10 changes: 10 additions & 0 deletions CSMath/BoundingBoxExtent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CSMath
{
public enum BoundingBoxExtent
{
Null,
Finite,
Infinite,
Point
}
}
1 change: 1 addition & 0 deletions CSMath/CSMath.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)BoundingBox.cs" />
<Compile Include="$(MSBuildThisFileDirectory)BoundingBoxExtent.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\ILine.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\Line3D.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Geometry\LineExtensions.cs" />
Expand Down

0 comments on commit eca6f7e

Please sign in to comment.