// Copyright (c) 2013-2015 Robert Rouhani and other contributors (see CONTRIBUTORS file). // Licensed under the MIT License - https://raw.github.com/Robmaister/SharpNav/master/LICENSE using System; #if MONOGAME using Vector3 = Microsoft.Xna.Framework.Vector3; #elif OPENTK using Vector3 = OpenTK.Vector3; #elif SHARPDX using Vector3 = SharpDX.Vector3; #endif namespace SharpNav.Geometry { /// /// A 3d axis-aligned bounding box. /// [Serializable] public struct BBox3 : IEquatable { /// /// The minimum bounds. /// public Vector3 Min; /// /// The maximum bounds. /// public Vector3 Max; /// /// Initializes a new instance of the struct. /// /// The minimum bounds. /// The maximum bounds. public BBox3(Vector3 min, Vector3 max) { Min = min; Max = max; } /// /// Initializes a new instance of the struct. /// /// The minimum on the X axis. /// The minimum on the Y axis. /// The minimum on the Z axis. /// The maximum on the X axis. /// The maximum on the Y axis. /// The maximum on the Z axis. public BBox3(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) { Min.X = minX; Min.Y = minY; Min.Z = minZ; Max.X = maxX; Max.Y = maxY; Max.Z = maxZ; } /// /// Gets the center of the box. /// public Vector3 Center { get { return (Min + Max) / 2; } } /// /// Gets the size of the box. /// public Vector3 Size { get { return Max - Min; } } /// /// Checks whether two boudning boxes are intersecting. /// /// The first bounding box. /// The second bounding box. /// A value indicating whether the two bounding boxes are overlapping. public static bool Overlapping(ref BBox3 a, ref BBox3 b) { return !(a.Min.X > b.Max.X || a.Max.X < b.Min.X || a.Min.Y > b.Max.Y || a.Max.Y < b.Min.Y || a.Min.Z > b.Max.Z || a.Max.Z < b.Min.Z); } /// /// Determines whether a bounding box is valid. Validity consists of having no NaN values and the Min vector /// to be less than the Max vector on all axes. /// /// The bounding box to validate. /// A value indicating whether the bounding box is valid. public static bool IsValid(ref BBox3 b) { //None of the values can be NaN. if (float.IsNaN(b.Min.X) || float.IsNaN(b.Min.Y) || float.IsNaN(b.Min.Z) || float.IsNaN(b.Max.X) || float.IsNaN(b.Max.Y) || float.IsNaN(b.Max.Z)) return false; //The min must be less than the max on all axes. if (b.Min.X > b.Max.X || b.Min.Y > b.Max.Y || b.Min.Z > b.Max.Z) return false; return true; } /// /// Compares two bounding boxes for equality. /// /// The first bounding box. /// The second bounding box. /// A value indicating the equality of the two boxes. public static bool operator ==(BBox3 left, BBox3 right) { return left.Equals(right); } /// /// Compares two bounding boxes for inequality. /// /// The first bounding box. /// The second bounding box. /// A value indicating the inequality of the two boxes. public static bool operator !=(BBox3 left, BBox3 right) { return !(left == right); } /// /// Compares this instance with another bounding box for equality. /// /// Another bounding box. /// A value indicating the equality of the two boxes. public bool Equals(BBox3 other) { return Min == other.Min && Max == other.Max; } /// /// Compares this instance with another object for equality. /// /// An object. /// A value indicating equality between the two objects. public override bool Equals(object obj) { if (obj is BBox3) return this.Equals((BBox3)obj); else return false; } /// /// Generates a unique hashcode for this bouding box instance. /// /// A hash code. public override int GetHashCode() { //TODO write a better hash code return Min.GetHashCode() ^ Max.GetHashCode(); } /// /// Returns a string containing the important information for this instance of . /// /// A human-readable string representation of this instance. public override string ToString() { return "{ Min: " + Min.ToString() + ", Max: " + Max.ToString() + " }"; } } }