#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*
* Contributions by Andy Gill.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace OpenTK.Math
{
///
/// Represents a four-dimensional vector.
///
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector4 : IEquatable
{
#region Fields
///
/// The X component of the Vector4.
///
public float X;
///
/// The Y component of the Vector4.
///
public float Y;
///
/// The Z component of the Vector4.
///
public float Z;
///
/// The Z component of the Vector4.
///
public float W;
///
/// Defines a unit-length Vector4 that points towards the X-axis.
///
public static Vector4 UnitX = new Vector4(1, 0, 0, 0);
///
/// Defines a unit-length Vector4 that points towards the Y-axis.
///
public static Vector4 UnitY = new Vector4(0, 1, 0, 0);
///
/// Defines a unit-length Vector4 that points towards the Z-axis.
///
public static Vector4 UnitZ = new Vector4(0, 0, 1, 0);
///
/// Defines a unit-length Vector4 that points towards the W-axis.
///
public static Vector4 UnitW = new Vector4(0, 0, 0, 1);
///
/// Defines a zero-length Vector4.
///
public static Vector4 Zero = new Vector4(0, 0, 0, 0);
///
/// Defines the size of the Vector4 struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4());
#endregion
#region Constructors
///
/// Constructs a new Vector4.
///
/// The x component of the Vector4.
/// The y component of the Vector4.
/// The z component of the Vector4.
/// The z component of the Vector4.
public Vector4(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
///
/// Constructs a new Vector4 from the given Vector2.
///
/// The Vector2 to copy components from.
public Vector4(Vector2 v)
{
X = v.X;
Y = v.Y;
Z = 0.0f;
W = 0.0f;
}
///
/// Constructs a new Vector4 from the given Vector3.
///
/// The Vector3 to copy components from.
public Vector4(Vector3 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = 0.0f;
}
///
/// Constructs a new Vector4 from the given Vector4.
///
/// The Vector4 to copy components from.
public Vector4(Vector4 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = v.W;
}
#endregion
#region Functions
#region public float Length
///
/// Gets the length (magnitude) of the vector.
///
///
///
public float Length
{
get
{
return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
}
}
#endregion
#region public float LengthFast
///
/// Gets an approximation of the vector length (magnitude).
///
///
/// This property uses an approximation of the square root function to calculate vector magnitude, with
/// an upper error bound of 0.001.
///
///
///
///
public float LengthFast
{
get
{
return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W);
}
}
#endregion
#region public float LengthSquared
///
/// Gets the square of the vector length (magnitude).
///
///
/// This property avoids the costly square root operation required by the Length property. This makes it more suitable
/// for comparisons.
///
///
///
public float LengthSquared
{
get
{
return X * X + Y * Y + Z * Z + W * W;
}
}
#endregion
#region public void Normalize()
///
/// Scales the Vector4 to unit length.
///
public void Normalize()
{
float scale = 1.0f / this.Length;
X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
}
#endregion
#region public void NormalizeFast()
///
/// Scales the Vector4 to approximately unit length.
///
public void NormalizeFast()
{
float scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W);
X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
}
#endregion
#region public void Scale(float sx, float sy, float sz, float sw)
///
/// Scales the current Vector4 by the given amounts.
///
/// The scale of the X component.
/// The scale of the Y component.
/// The scale of the Z component.
/// The scale of the Z component.
public void Scale(float sx, float sy, float sz, float sw)
{
this.X = X * sx;
this.Y = Y * sy;
this.Z = Z * sz;
this.W = W * sw;
}
#endregion
#endregion
#region Operator overloads
public static Vector4 operator +(Vector4 left, Vector4 right)
{
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
left.W += right.W;
return left;
}
public static Vector4 operator -(Vector4 left, Vector4 right)
{
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
left.W -= right.W;
return left;
}
public static Vector4 operator -(Vector4 vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
vec.W = -vec.W;
return vec;
}
public static Vector4 operator *(Vector4 vec, float f)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
vec.W *= f;
return vec;
}
public static Vector4 operator *(float f, Vector4 vec)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
vec.W *= f;
return vec;
}
public static Vector4 operator /(Vector4 vec, float f)
{
float mult = 1.0f / f;
vec.X *= mult;
vec.Y *= mult;
vec.Z *= mult;
vec.W *= mult;
return vec;
}
public static bool operator ==(Vector4 left, Vector4 right)
{
return left.Equals(right);
}
public static bool operator !=(Vector4 left, Vector4 right)
{
return !left.Equals(right);
}
[CLSCompliant(false)]
unsafe public static explicit operator float*(Vector4 v)
{
return &v.X;
}
public static explicit operator IntPtr(Vector4 v)
{
unsafe
{
return (IntPtr)(&v.X);
}
}
#endregion
#region Static functions
#region Add
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static Vector4 Add(Vector4 a, Vector4 b)
{
a.X += b.X;
a.Y += b.Y;
a.Z += b.Z;
a.W += b.W;
return a;
}
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static void Add(ref Vector4 a, ref Vector4 b, out Vector4 result)
{
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
result.W = a.W + b.W;
}
#endregion
#region Sub
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static Vector4 Sub(Vector4 a, Vector4 b)
{
a.X -= b.X;
a.Y -= b.Y;
a.Z -= b.Z;
a.W -= b.W;
return a;
}
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static void Sub(ref Vector4 a, ref Vector4 b, out Vector4 result)
{
result.X = a.X - b.X;
result.Y = a.Y - b.Y;
result.Z = a.Z - b.Z;
result.W = a.W - b.W;
}
#endregion
#region Mult
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static Vector4 Mult(Vector4 a, float f)
{
a.X *= f;
a.Y *= f;
a.Z *= f;
a.W *= f;
return a;
}
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static void Mult(ref Vector4 a, float f, out Vector4 result)
{
result.X = a.X * f;
result.Y = a.Y * f;
result.Z = a.Z * f;
result.W = a.W * f;
}
#endregion
#region Div
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static Vector4 Div(Vector4 a, float f)
{
float mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
a.Z *= mult;
a.W *= mult;
return a;
}
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static void Div(ref Vector4 a, float f, out Vector4 result)
{
float mult = 1.0f / f;
result.X = a.X * mult;
result.Y = a.Y * mult;
result.Z = a.Z * mult;
result.W = a.W * mult;
}
#endregion
#region Min
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static Vector4 Min(Vector4 a, Vector4 b)
{
a.X = a.X < b.X ? a.X : b.X;
a.Y = a.Y < b.Y ? a.Y : b.Y;
a.Z = a.Z < b.Z ? a.Z : b.Z;
a.W = a.W < b.W ? a.W : b.W;
return a;
}
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static void Min(ref Vector4 a, ref Vector4 b, out Vector4 result)
{
result.X = a.X < b.X ? a.X : b.X;
result.Y = a.Y < b.Y ? a.Y : b.Y;
result.Z = a.Z < b.Z ? a.Z : b.Z;
result.W = a.W < b.W ? a.W : b.W;
}
#endregion
#region Max
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static Vector4 Max(Vector4 a, Vector4 b)
{
a.X = a.X > b.X ? a.X : b.X;
a.Y = a.Y > b.Y ? a.Y : b.Y;
a.Z = a.Z > b.Z ? a.Z : b.Z;
a.W = a.W > b.W ? a.W : b.W;
return a;
}
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static void Max(ref Vector4 a, ref Vector4 b, out Vector4 result)
{
result.X = a.X > b.X ? a.X : b.X;
result.Y = a.Y > b.Y ? a.Y : b.Y;
result.Z = a.Z > b.Z ? a.Z : b.Z;
result.W = a.W > b.W ? a.W : b.W;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static Vector4 Clamp(Vector4 vec, Vector4 min, Vector4 max)
{
vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
vec.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
vec.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W;
return vec;
}
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static void Clamp(ref Vector4 vec, ref Vector4 min, ref Vector4 max, out Vector4 result)
{
result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
result.Z = vec.X < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
result.W = vec.Y < min.W ? min.W : vec.W > max.W ? max.W : vec.W;
}
#endregion
#region Normalize
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static Vector4 Normalize(Vector4 vec)
{
float scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
vec.W *= scale;
return vec;
}
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static void Normalize(ref Vector4 vec, out Vector4 result)
{
float scale = 1.0f / vec.Length;
result.X = vec.X * scale;
result.Y = vec.Y * scale;
result.Z = vec.Z * scale;
result.W = vec.W * scale;
}
#endregion
#region NormalizeFast
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static Vector4 NormalizeFast(Vector4 vec)
{
float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W);
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
vec.W *= scale;
return vec;
}
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static void NormalizeFast(ref Vector4 vec, out Vector4 result)
{
float scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z + vec.W * vec.W);
result.X = vec.X * scale;
result.Y = vec.Y * scale;
result.Z = vec.Z * scale;
result.W = vec.W * scale;
}
#endregion
#region Dot
///
/// Caclulate the dot product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static float Dot(Vector4 left, Vector4 right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
}
#endregion
#region Lerp
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static Vector4 Lerp(Vector4 a, Vector4 b, float blend)
{
a.X = blend * (b.X - a.X) + a.X;
a.Y = blend * (b.Y - a.Y) + a.Y;
a.Z = blend * (b.Z - a.Z) + a.Z;
a.W = blend * (b.W - a.W) + a.W;
return a;
}
#endregion
#region Barycentric
///
/// Interpolate 3 Vectors using Barycentric coordinates
///
/// First input Vector
/// Second input Vector
/// Third input Vector
/// First Barycentric Coordinate
/// Second Barycentric Coordinate
/// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise
public static Vector4 BaryCentric(Vector4 a, Vector4 b, Vector4 c, float u, float v)
{
return a + u * (b - a) + v * (c - a);
}
#endregion
#region Transform
///
/// Transform a Vector by the given Matrix
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static Vector4 Transform(Vector4 vec, Matrix4 mat)
{
Vector4 result;
result.X = Vector4.Dot(vec, mat.Column0);
result.Y = Vector4.Dot(vec, mat.Column1);
result.Z = Vector4.Dot(vec, mat.Column2);
result.W = Vector4.Dot(vec, mat.Column3);
return result;
}
#endregion
#endregion
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector4.
///
///
public override string ToString()
{
return String.Format("({0}, {1}, {2}, {3})", X, Y, Z, W);
}
#endregion
#region public override int GetHashCode()
///
/// Returns the hashcode for this instance.
///
/// A System.Int32 containing the unique hashcode for this instance.
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode() ^ W.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
///
/// Indicates whether this instance and a specified object are equal.
///
/// The object to compare to.
/// True if the instances are equal; false otherwise.
public override bool Equals(object obj)
{
if (!(obj is Vector4))
return false;
return this.Equals((Vector4)obj);
}
#endregion
#region IEquatable Members
/// Indicates whether the current vector is equal to another vector.
/// A vector to compare with this vector.
/// true if the current vector is equal to the vector parameter; otherwise, false.
public bool Equals(Vector4 other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z &&
W == other.W;
}
#endregion
}
}