//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
using System.Runtime.InteropServices;
namespace BansheeEngine
{
/** @addtogroup Math
* @{
*/
///
/// A ray in 3D space represented with an origin and direction.
///
[StructLayout(LayoutKind.Sequential), SerializeObject]
public struct Ray // Note: Must match C++ struct Ray
{
public Vector3 origin;
public Vector3 direction;
///
/// Creates a new ray.
///
/// Coordinates for the origin of the ray.
/// Normalized direction of the ray.
public Ray(Vector3 origin, Vector3 direction)
{
this.origin = origin;
this.direction = direction;
}
///
/// Multiples ray by a scalar and retrieves a point along the ray.
///
/// Ray to transform.
/// How far along the ray to retrieve the point.
/// Point along the ray units away from the origin.
public static Vector3 operator*(Ray ray, float t)
{
return ray.origin + ray.direction * t;
}
///
/// Transforms the ray by the specified matrix. If the matrix is affine use
/// as it is faster.
///
/// Matrix to transform the ray by.
public void Transform(Matrix4 matrix)
{
Vector3 end = this * 1.0f;
origin = matrix.Multiply(origin);
end = matrix.Multiply(end);
direction = Vector3.Normalize(end - origin);
}
///
/// Transforms the ray by the specified affine matrix.
///
/// Affine matrix to transform the ray by.
public void TransformAffine(Matrix4 matrix)
{
Vector3 end = this * 1.0f;
origin = matrix.MultiplyDirection(origin);
end = matrix.MultiplyDirection(end);
direction = Vector3.Normalize(end - origin);
}
///
public override string ToString()
{
return String.Format("(origin: {0} direction: {1})", origin, direction);
}
};
/** @} */
}