using System; using ANX.Framework.NonXNA.Development; using System.ComponentModel; using ANX.Framework.Design; // This file is part of the ANX.Framework created by the // "ANX.Framework developer group" and released under the Ms-PL license. // For details see: http://anxframework.codeplex.com/license namespace ANX.Framework { [PercentageComplete(100)] [Developer("floAr")] [TestState(TestStateAttribute.TestState.InProgress)] #if !WINDOWSMETRO [Serializable] [TypeConverter(typeof(RayConverter))] #endif public struct Ray : IEquatable { #region fields /// /// The direction this ray is pointing to. /// public Vector3 Direction; /// /// Starting position of the ray. /// public Vector3 Position; #endregion #region constructors /// /// Initializes a new instance of the struct. /// /// The position. /// The direction. public Ray(Vector3 position, Vector3 direction) { this.Direction = direction; this.Position = position; } #endregion #region public methods /// /// 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() { return Position.GetHashCode() + Direction.GetHashCode(); } /* * Source for implementation : * http://www-gs.informatik.tu-cottbus.de/projektstudium2006/doku/Strahlen_in_der_CG.pdf */ /// /// Test if this intersects with the specified . /// /// The box. /// Distance from start to interesection points public Nullable Intersects(BoundingBox box) { Nullable result; this.Intersects(ref box, out result); return result; } /// /// Test if this intersects with the specified . /// /// The box. /// The result. public void Intersects(ref BoundingBox box, out Nullable result) { box.Intersects(ref this, out result); } /// /// Test if this intersects with the specified . /// /// The box. /// Distance from start to interesection points public Nullable Intersects(BoundingFrustum frustum) { if (frustum == null) { throw new ArgumentNullException("frustum"); } return frustum.Intersects(this); } /// /// Test if this intersects with the specified . /// /// The sphere. /// /// Distance from start to interesection points /// public Nullable Intersects(BoundingSphere sphere) { Nullable result; this.Intersects(ref sphere, out result); return result; } /// /// Test if this intersects with the specified . /// /// The sphere. /// The result. public void Intersects(ref BoundingSphere sphere, out Nullable result) { Vector3 toSphere = Vector3.Subtract(sphere.Center, this.Position); float lengthSquaredToSphere = toSphere.LengthSquared(); float sphereRadiusSquared = sphere.Radius * sphere.Radius; //project the distance to the Sphere onto the Ray float toSphereOnRay = Vector3.Dot(this.Direction, toSphere); //ray starts in sphere if (lengthSquaredToSphere <= sphereRadiusSquared) { result = 0; return; } //if toSphere and this.Direction pointing in different directions if (toSphereOnRay < 0) { result = null; return; } float dist = sphereRadiusSquared + toSphereOnRay * toSphereOnRay - lengthSquaredToSphere; result = (dist < 0) ? null : toSphereOnRay - (float?)Math.Sqrt(dist); } /// /// Test if this intersects with the specified . /// /// The plane. /// /// Distance from start to interesection points /// public Nullable Intersects(Plane plane) { Nullable result; this.Intersects(ref plane, out result); return result; } /// /// Test if this intersects with the specified . /// /// The plane. /// The result. public void Intersects(ref Plane plane, out Nullable result) { //http://www.cs.toronto.edu/~smalik/418/tutorial8_ray_primitive_intersections.pdf float vd = Vector3.Dot(plane.Normal, this.Direction); //As plane and Ray are infinite it intersects in every case, except if the ray is parallel to the plane //no intersection if ray direction and plane normal are orthogional to each other if (vd == 0) { result = null; return; } float v0 = -Vector3.Dot(plane.Normal, this.Position) + plane.D; float t = v0 / vd; result = (this.Direction*t).Length(); } /// /// Returns a that represents this instance. /// /// /// A that represents this instance. /// public override string ToString() { // This may look a bit more ugly, but String.Format should // be avoided cause of it's bad performance! return "{Position:" + Position.ToString() + " Direction:" + Direction.ToString() + "}"; } #endregion #region operator overloading /// /// Implements the operator ==. /// /// First value /// Second value /// /// The result of the operator. /// public static bool operator ==(Ray a, Ray b) { return a.Direction == b.Direction && a.Position == b.Position; } /// /// Implements the operator !=. /// /// First value /// Second value /// /// The result of the operator. /// public static bool operator !=(Ray a, Ray b) { return a.Direction != b.Direction || a.Position != b.Position; } #endregion #region IEquatable implementation /// /// 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 obj) { if (obj is Ray) { return this.Equals((Ray)obj); } return false; } /// /// 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 bool Equals(Ray other) { return this.Direction == other.Direction && this.Position == other.Position; } #endregion } }