// 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 a three dimensional line based on a point in space and a direction.
///
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Ray : IEquatable, IFormattable
{
///
/// The position in three dimensional space where the ray starts.
///
public Vector3 Position;
///
/// The normalized direction in which the ray points.
///
public Vector3 Direction;
///
/// Initializes a new instance of the struct.
///
/// The position in three dimensional space of the origin of the ray.
/// The normalized direction of the ray.
public Ray(Vector3 position, Vector3 direction)
{
this.Position = position;
this.Direction = direction;
}
///
/// Determines if there is an intersection between the current object and a point.
///
/// The point to test.
/// Whether the two objects intersected.
public bool Intersects(ref Vector3 point)
{
return Collision.RayIntersectsPoint(ref this, ref point);
}
///
/// 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)
{
Vector3 point;
return Collision.RayIntersectsRay(ref this, ref ray, out point);
}
///
/// 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.RayIntersectsRay(ref this, ref ray, out point);
}
///
/// Determines if there is an intersection between the current object and a .
///
/// The plane to test
/// Whether the two objects intersected.
public bool Intersects(ref Plane plane)
{
float distance;
return Collision.RayIntersectsPlane(ref this, ref plane, out distance);
}
///
/// Determines if there is an intersection between the current object and a .
///
/// The plane 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 Plane plane, out float distance)
{
return Collision.RayIntersectsPlane(ref this, ref plane, out distance);
}
///
/// Determines if there is an intersection between the current object and a .
///
/// The plane 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 Plane plane, out Vector3 point)
{
return Collision.RayIntersectsPlane(ref this, ref plane, out point);
}
///
/// 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)
{
float distance;
return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out distance);
}
///
/// 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.
/// 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 Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out float distance)
{
return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out distance);
}
///
/// 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.
/// When the method completes, contains the point of intersection,
/// or if there was no intersection.
/// Whether the two objects intersected.
public bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out Vector3 point)
{
return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out point);
}
///
/// 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)
{
float distance;
return Collision.RayIntersectsBox(ref this, ref box, out distance);
}
///
/// 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 box 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 BoundingBox box, out float distance)
{
return Collision.RayIntersectsBox(ref this, ref box, out distance);
}
///
/// Determines if there is an intersection between the current object and a .
///
/// The box 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 BoundingBox box, out Vector3 point)
{
return Collision.RayIntersectsBox(ref this, ref box, out point);
}
///
/// 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)
{
float distance;
return Collision.RayIntersectsSphere(ref this, ref sphere, out distance);
}
///
/// 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 if there is an intersection between the current object and a .
///
/// The sphere 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 BoundingSphere sphere, out float distance)
{
return Collision.RayIntersectsSphere(ref this, ref sphere, out distance);
}
///
/// Determines if there is an intersection between the current object and a .
///
/// The sphere 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 BoundingSphere sphere, out Vector3 point)
{
return Collision.RayIntersectsSphere(ref this, ref sphere, out point);
}
///
/// Calculates a world space from 2d screen coordinates.
///
/// X coordinate on 2d screen.
/// Y coordinate on 2d screen.
/// .
/// Transformation .
/// Resulting .
public static Ray GetPickRay(int x, int y, ViewportF viewport, Matrix worldViewProjection)
{
var nearPoint = new Vector3(x, y, 0);
var farPoint = new Vector3(x, y, 1);
nearPoint = Vector3.Unproject(nearPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
viewport.MaxDepth, worldViewProjection);
farPoint = Vector3.Unproject(farPoint, viewport.X, viewport.Y, viewport.Width, viewport.Height, viewport.MinDepth,
viewport.MaxDepth, worldViewProjection);
Vector3 direction = farPoint - nearPoint;
direction.Normalize();
return new Ray(nearPoint, direction);
}
///
/// 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 ==(Ray left, Ray 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 !=(Ray left, Ray 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, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
}
///
/// Returns a that represents this instance.
///
/// The format.
///
/// A that represents this instance.
///
public string ToString(string format)
{
return string.Format(CultureInfo.CurrentCulture, "Position:{0} Direction:{1}", Position.ToString(format, CultureInfo.CurrentCulture),
Direction.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, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
}
///
/// Returns a that represents this instance.
///
/// The format.
/// The format provider.
///
/// A that represents this instance.
///
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format(formatProvider, "Position:{0} Direction:{1}", Position.ToString(format, formatProvider),
Direction.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 (Position.GetHashCode() * 397) ^ Direction.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 Ray value)
{
return Position == value.Position && Direction == value.Direction;
}
///
/// 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(Ray 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 Ray))
return false;
var strongValue = (Ray)value;
return Equals(ref strongValue);
}
}
}