diff --git a/CSMath/BoundingBox.cs b/CSMath/BoundingBox.cs index 4e291d2..c5c286b 100644 --- a/CSMath/BoundingBox.cs +++ b/CSMath/BoundingBox.cs @@ -1,10 +1,24 @@ -namespace CSMath +using System.Collections.Generic; + +namespace CSMath { /// /// Bounding box representation aligned to XYZ axis. /// public struct BoundingBox { + /// + /// Instance of a null bounding box. + /// + public static readonly BoundingBox Null = new BoundingBox(BoundingBoxExtent.Null); + + /// + /// Instance of an infinite bounding box. + /// + public static readonly BoundingBox Infinite = new BoundingBox(BoundingBoxExtent.Infinite); + + public BoundingBoxExtent Extent { get; } + /// /// Get the min corner of the bounding box. /// @@ -26,6 +40,18 @@ public XYZ Center } } + private BoundingBox(BoundingBoxExtent extent) + { + this.Extent = extent; + } + + public BoundingBox(XYZ point) + { + this.Extent = BoundingBoxExtent.Point; + this.Min = point; + this.Max = point; + } + /// /// Bounding box constructor with 2 points. /// @@ -33,6 +59,15 @@ public XYZ Center /// 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)); } @@ -69,5 +104,22 @@ public BoundingBox Merge(BoundingBox box) return new BoundingBox(min, max); } + + /// + /// Create a bounding box from a collection of points. + /// + /// + /// + public static BoundingBox FromPoints(IEnumerable points) + { + BoundingBox boundingBox = Null; + + foreach (var point in points) + { + boundingBox = boundingBox.Merge(new BoundingBox(point)); + } + + return boundingBox; + } } } diff --git a/CSMath/BoundingBoxExtent.cs b/CSMath/BoundingBoxExtent.cs new file mode 100644 index 0000000..2f521db --- /dev/null +++ b/CSMath/BoundingBoxExtent.cs @@ -0,0 +1,10 @@ +namespace CSMath +{ + public enum BoundingBoxExtent + { + Null, + Finite, + Infinite, + Point + } +} diff --git a/CSMath/CSMath.projitems b/CSMath/CSMath.projitems index 491b0d2..450de48 100644 --- a/CSMath/CSMath.projitems +++ b/CSMath/CSMath.projitems @@ -10,6 +10,7 @@ +