// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // ----------------------------------------------------------------------------- // Original code from SlimMath project. http://code.google.com/p/slimmath/ // Greetings to SlimDX Group. Original code published with the following license: // ----------------------------------------------------------------------------- /* * Copyright (c) 2007-2011 SlimDX Group * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ using System; using System.Globalization; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace SharpDX { /// /// Represents an axis-aligned bounding box in three dimensional space. /// [StructLayout(LayoutKind.Sequential, Pack = 4)] public struct BoundingBox : IEquatable, IFormattable { /// /// The minimum point of the box. /// public Vector3 Minimum; /// /// The maximum point of the box. /// public Vector3 Maximum; /// /// Initializes a new instance of the struct. /// /// The minimum vertex of the bounding box. /// The maximum vertex of the bounding box. public BoundingBox(Vector3 minimum, Vector3 maximum) { this.Minimum = minimum; this.Maximum = maximum; } /// /// Returns the width of the bounding box /// public float Width { get { return this.Maximum.X - this.Minimum.X; } } /// /// Returns the height of the bounding box /// public float Height { get { return this.Maximum.Y - this.Minimum.Y; } } /// /// Returns the height of the bounding box /// public float Depth { get { return this.Maximum.Z - this.Minimum.Z; } } /// /// Returns the size of the bounding box /// public Vector3 Size { get { return this.Maximum - this.Minimum; } } /// /// Returns the size of the bounding box /// public Vector3 Center { get { return (this.Maximum + this.Minimum) * 0.5f; } } /// /// Retrieves the eight corners of the bounding box. /// /// An array of points representing the eight corners of the bounding box. public Vector3[] GetCorners() { Vector3[] results = new Vector3[8]; GetCorners(results); return results; } /// /// Retrieves the eight corners of the bounding box. /// /// An array of points representing the eight corners of the bounding box. public void GetCorners(Vector3[] corners) { corners[0] = new Vector3(Minimum.X, Maximum.Y, Maximum.Z); corners[1] = new Vector3(Maximum.X, Maximum.Y, Maximum.Z); corners[2] = new Vector3(Maximum.X, Minimum.Y, Maximum.Z); corners[3] = new Vector3(Minimum.X, Minimum.Y, Maximum.Z); corners[4] = new Vector3(Minimum.X, Maximum.Y, Minimum.Z); corners[5] = new Vector3(Maximum.X, Maximum.Y, Minimum.Z); corners[6] = new Vector3(Maximum.X, Minimum.Y, Minimum.Z); corners[7] = new Vector3(Minimum.X, Minimum.Y, Minimum.Z); } /// /// Determines if there is an intersection between the current object and a . /// /// The ray to test. /// Whether the two objects intersected. public bool Intersects(ref Ray ray) { float distance; return Collision.RayIntersectsBox(ref ray, ref this, out distance); } /// /// Determines if there is an intersection between the current object and a . /// /// The ray to test. /// When the method completes, contains the distance of the intersection, /// or 0 if there was no intersection. /// Whether the two objects intersected. public bool Intersects(ref Ray ray, out float distance) { return Collision.RayIntersectsBox(ref ray, ref this, out distance); } /// /// Determines if there is an intersection between the current object and a . /// /// The ray to test. /// When the method completes, contains the point of intersection, /// or if there was no intersection. /// Whether the two objects intersected. public bool Intersects(ref Ray ray, out Vector3 point) { return Collision.RayIntersectsBox(ref ray, ref this, out point); } /// /// Determines if there is an intersection between the current object and a . /// /// The plane to test. /// Whether the two objects intersected. public PlaneIntersectionType Intersects(ref Plane plane) { return Collision.PlaneIntersectsBox(ref plane, ref this); } /* This implementation is wrong /// /// Determines if there is an intersection between the current object and a triangle. /// /// The first vertex of the triangle to test. /// The second vertex of the triangle to test. /// The third vertex of the triangle to test. /// Whether the two objects intersected. public bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3) { return Collision.BoxIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3); } */ /// /// Determines if there is an intersection between the current object and a . /// /// The box to test. /// Whether the two objects intersected. public bool Intersects(ref BoundingBox box) { return Collision.BoxIntersectsBox(ref this, ref box); } /// /// Determines if there is an intersection between the current object and a . /// /// The box to test. /// Whether the two objects intersected. public bool Intersects(BoundingBox box) { return Intersects(ref box); } /// /// Determines if there is an intersection between the current object and a . /// /// The sphere to test. /// Whether the two objects intersected. public bool Intersects(ref BoundingSphere sphere) { return Collision.BoxIntersectsSphere(ref this, ref sphere); } /// /// Determines if there is an intersection between the current object and a . /// /// The sphere to test. /// Whether the two objects intersected. public bool Intersects(BoundingSphere sphere) { return Intersects(ref sphere); } /// /// Determines whether the current objects contains a point. /// /// The point to test. /// The type of containment the two objects have. public ContainmentType Contains(ref Vector3 point) { return Collision.BoxContainsPoint(ref this, ref point); } /// /// Determines whether the current objects contains a point. /// /// The point to test. /// The type of containment the two objects have. public ContainmentType Contains(Vector3 point) { return Contains(ref point); } /* This implementation is wrong /// /// Determines whether the current objects contains a triangle. /// /// The first vertex of the triangle to test. /// The second vertex of the triangle to test. /// The third vertex of the triangle to test. /// The type of containment the two objects have. public ContainmentType Contains(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3) { return Collision.BoxContainsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3); } */ /// /// Determines whether the current objects contains a . /// /// The box to test. /// The type of containment the two objects have. public ContainmentType Contains(ref BoundingBox box) { return Collision.BoxContainsBox(ref this, ref box); } /// /// Determines whether the current objects contains a . /// /// The box to test. /// The type of containment the two objects have. public ContainmentType Contains(BoundingBox box) { return Contains(ref box); } /// /// Determines whether the current objects contains a . /// /// The sphere to test. /// The type of containment the two objects have. public ContainmentType Contains(ref BoundingSphere sphere) { return Collision.BoxContainsSphere(ref this, ref sphere); } /// /// Determines whether the current objects contains a . /// /// The sphere to test. /// The type of containment the two objects have. public ContainmentType Contains(BoundingSphere sphere) { return Contains(ref sphere); } /// /// Constructs a that fully contains the given points. /// /// The points that will be contained by the box. /// When the method completes, contains the newly constructed bounding box. /// Thrown when is null. public static void FromPoints(Vector3[] points, out BoundingBox result) { if (points == null) throw new ArgumentNullException("points"); Vector3 min = new Vector3(float.MaxValue); Vector3 max = new Vector3(float.MinValue); for (int i = 0; i < points.Length; ++i) { Vector3.Min(ref min, ref points[i], out min); Vector3.Max(ref max, ref points[i], out max); } result = new BoundingBox(min, max); } /// /// Constructs a that fully contains the given points. /// /// The points that will be contained by the box. /// The newly constructed bounding box. /// Thrown when is null. public static BoundingBox FromPoints(Vector3[] points) { if (points == null) throw new ArgumentNullException("points"); Vector3 min = new Vector3(float.MaxValue); Vector3 max = new Vector3(float.MinValue); for (int i = 0; i < points.Length; ++i) { Vector3.Min(ref min, ref points[i], out min); Vector3.Max(ref max, ref points[i], out max); } return new BoundingBox(min, max); } /// /// Constructs a from a given sphere. /// /// The sphere that will designate the extents of the box. /// When the method completes, contains the newly constructed bounding box. public static void FromSphere(ref BoundingSphere sphere, out BoundingBox result) { result.Minimum = new Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius); result.Maximum = new Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius); } /// /// Constructs a from a given sphere. /// /// The sphere that will designate the extents of the box. /// The newly constructed bounding box. public static BoundingBox FromSphere(BoundingSphere sphere) { BoundingBox box; box.Minimum = new Vector3(sphere.Center.X - sphere.Radius, sphere.Center.Y - sphere.Radius, sphere.Center.Z - sphere.Radius); box.Maximum = new Vector3(sphere.Center.X + sphere.Radius, sphere.Center.Y + sphere.Radius, sphere.Center.Z + sphere.Radius); return box; } /// /// Constructs a that is as large as the total combined area of the two specified boxes. /// /// The first box to merge. /// The second box to merge. /// When the method completes, contains the newly constructed bounding box. public static void Merge(ref BoundingBox value1, ref BoundingBox value2, out BoundingBox result) { Vector3.Min(ref value1.Minimum, ref value2.Minimum, out result.Minimum); Vector3.Max(ref value1.Maximum, ref value2.Maximum, out result.Maximum); } /// /// Constructs a that is as large as the total combined area of the two specified boxes. /// /// The first box to merge. /// The second box to merge. /// The newly constructed bounding box. public static BoundingBox Merge(BoundingBox value1, BoundingBox value2) { BoundingBox box; Vector3.Min(ref value1.Minimum, ref value2.Minimum, out box.Minimum); Vector3.Max(ref value1.Maximum, ref value2.Maximum, out box.Maximum); return box; } /// /// Tests for equality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has the same value as ; otherwise, false. [MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining public static bool operator ==(BoundingBox left, BoundingBox right) { return left.Equals(ref right); } /// /// Tests for inequality between two objects. /// /// The first value to compare. /// The second value to compare. /// true if has a different value than ; otherwise, false. [MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining public static bool operator !=(BoundingBox left, BoundingBox right) { return !left.Equals(ref right); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { return string.Format(CultureInfo.CurrentCulture, "Minimum:{0} Maximum:{1}", Minimum.ToString(), Maximum.ToString()); } /// /// Returns a that represents this instance. /// /// The format. /// /// A that represents this instance. /// public string ToString(string format) { if (format == null) return ToString(); return string.Format(CultureInfo.CurrentCulture, "Minimum:{0} Maximum:{1}", Minimum.ToString(format, CultureInfo.CurrentCulture), Maximum.ToString(format, CultureInfo.CurrentCulture)); } /// /// Returns a that represents this instance. /// /// The format provider. /// /// A that represents this instance. /// public string ToString(IFormatProvider formatProvider) { return string.Format(formatProvider, "Minimum:{0} Maximum:{1}", Minimum.ToString(), Maximum.ToString()); } /// /// Returns a that represents this instance. /// /// The format. /// The format provider. /// /// A that represents this instance. /// public string ToString(string format, IFormatProvider formatProvider) { if (format == null) return ToString(formatProvider); return string.Format(formatProvider, "Minimum:{0} Maximum:{1}", Minimum.ToString(format, formatProvider), Maximum.ToString(format, formatProvider)); } /// /// Returns a hash code for this instance. /// /// /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. /// public override int GetHashCode() { unchecked { return (Minimum.GetHashCode() * 397) ^ Maximum.GetHashCode(); } } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// [MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining public bool Equals(ref BoundingBox value) { return Minimum == value.Minimum && Maximum == value.Maximum; } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// [MethodImpl((MethodImplOptions)0x100)] // MethodImplOptions.AggressiveInlining public bool Equals(BoundingBox value) { return Equals(ref value); } /// /// Determines whether the specified is equal to this instance. /// /// The to compare with this instance. /// /// true if the specified is equal to this instance; otherwise, false. /// public override bool Equals(object value) { if (!(value is BoundingBox)) return false; var strongValue = (BoundingBox)value; return Equals(ref strongValue); } } }