using System; using System.Runtime.InteropServices; namespace BansheeEngine { /** @addtogroup Math * @{ */ /// /// A plane represented by a normal and a distance. /// [StructLayout(LayoutKind.Sequential), SerializeObject] public struct Plane // Note: Must match C++ class Plane { /// /// The "positive side" of the plane is the half space to which the plane normal points. The "negative side" is the /// other half space. The flag "no side" indicates the plane itself. /// public enum Side { None, Positive, Negative, Both }; public Vector3 normal; public float d; /// /// Creates a plane from a normal and a distance from the plane. /// /// Plane normal pointing in the positive half-space. /// Distance of the plane from the origin, along the normal. public Plane(Vector3 normal, float d) { this.normal = normal; this.d = d; } /// /// Creates a plane from three points on the plane. The points must not be colinear. /// /// A point in the plane. /// A point in the plane. /// A point in the plane. public Plane(Vector3 a, Vector3 b, Vector3 c) { Vector3 e0 = b - a; Vector3 e1 = c - a; normal = Vector3.Cross(e0, e1); normal.Normalize(); d = Vector3.Dot(normal, a); } /// /// Returns a distance from point to plane. /// /// Point to test. /// /// Distance to the plane. Will be positive if the point is on the positive side of the plane, negative if on the /// negative side of the plane, or zero if the point is on the plane. /// float GetDistance(Vector3 point) { return Vector3.Dot(normal, point) - d; } /// /// Returns the side of the plane where the point is located on. No side signifies the point is on the plane. /// /// Point to test. /// Extra depth of the plane to help with precision issues. /// Side on the plane the point is located on. public Side GetSide(Vector3 point, float epsilon = 0.0f) { float dist = GetDistance(point); if (dist > epsilon) return Side.Positive; if (dist < -epsilon) return Side.Negative; return Side.None; } } /** @} */ }