#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
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.
*/
#endregion
using System;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
namespace OpenTK.Math
{
/// Represents a 4D vector using four double-precision floating-point numbers.
[Obsolete("OpenTK.Math functions have been moved to the root OpenTK namespace (reason: XNA compatibility")]
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector4d : IEquatable
{
#region Fields
///
/// The X component of the Vector4d.
///
public double X;
///
/// The Y component of the Vector4d.
///
public double Y;
///
/// The Z component of the Vector4d.
///
public double Z;
///
/// The W component of the Vector4d.
///
public double W;
///
/// Defines a unit-length Vector4d that points towards the X-axis.
///
public static Vector4d UnitX = new Vector4d(1, 0, 0, 0);
///
/// Defines a unit-length Vector4d that points towards the Y-axis.
///
public static Vector4d UnitY = new Vector4d(0, 1, 0, 0);
///
/// Defines a unit-length Vector4d that points towards the Z-axis.
///
public static Vector4d UnitZ = new Vector4d(0, 0, 1, 0);
///
/// Defines a unit-length Vector4d that points towards the W-axis.
///
public static Vector4d UnitW = new Vector4d(0, 0, 0, 1);
///
/// Defines a zero-length Vector4d.
///
public static Vector4d Zero = new Vector4d(0, 0, 0, 0);
///
/// Defines an instance with all components set to 1.
///
public static readonly Vector4d One = new Vector4d(1, 1, 1, 1);
///
/// Defines the size of the Vector4d struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector4d());
#endregion
#region Constructors
///
/// Constructs a new Vector4d.
///
/// The x component of the Vector4d.
/// The y component of the Vector4d.
/// The z component of the Vector4d.
/// The z component of the Vector4d.
public Vector4d(double x, double y, double z, double w)
{
X = x;
Y = y;
Z = z;
W = w;
}
///
/// Constructs a new Vector4d from the given Vector2d.
///
/// The Vector2d to copy components from.
public Vector4d(Vector2d v)
{
X = v.X;
Y = v.Y;
Z = 0.0f;
W = 0.0f;
}
///
/// Constructs a new Vector4d from the given Vector3d.
///
/// The Vector3d to copy components from.
public Vector4d(Vector3d v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = 0.0f;
}
///
/// Constructs a new Vector4d from the specified Vector3d and W component.
///
/// The Vector3d to copy components from.
/// The W component of the new Vector4.
public Vector4d(Vector3 v, double w)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = w;
}
///
/// Constructs a new Vector4d from the given Vector4d.
///
/// The Vector4d to copy components from.
public Vector4d(Vector4d v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
W = v.W;
}
#endregion
#region Public Members
#region Instance
#region public void Add()
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
public void Add(Vector4d right)
{
this.X += right.X;
this.Y += right.Y;
this.Z += right.Z;
this.W += right.W;
}
/// Add the Vector passed as parameter to this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
public void Add(ref Vector4d right)
{
this.X += right.X;
this.Y += right.Y;
this.Z += right.Z;
this.W += right.W;
}
#endregion public void Add()
#region public void Sub()
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
public void Sub(Vector4d right)
{
this.X -= right.X;
this.Y -= right.Y;
this.Z -= right.Z;
this.W -= right.W;
}
/// Subtract the Vector passed as parameter from this instance.
/// Right operand. This parameter is only read from.
[CLSCompliant(false)]
public void Sub(ref Vector4d right)
{
this.X -= right.X;
this.Y -= right.Y;
this.Z -= right.Z;
this.W -= right.W;
}
#endregion public void Sub()
#region public void Mult()
/// Multiply this instance by a scalar.
/// Scalar operand.
public void Mult(double f)
{
this.X *= f;
this.Y *= f;
this.Z *= f;
this.W *= f;
}
#endregion public void Mult()
#region public void Div()
/// Divide this instance by a scalar.
/// Scalar operand.
public void Div(double f)
{
double mult = 1.0 / f;
this.X *= mult;
this.Y *= mult;
this.Z *= mult;
this.W *= mult;
}
#endregion public void Div()
#region public double Length
///
/// Gets the length (magnitude) of the vector.
///
///
///
public double Length
{
get
{
return (double)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
}
}
#endregion
#region public double 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 double LengthFast
{
get
{
return 1.0f / MathHelper.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W);
}
}
#endregion
#region public double 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 double LengthSquared
{
get
{
return X * X + Y * Y + Z * Z + W * W;
}
}
#endregion
#region public void Normalize()
///
/// Scales the Vector4d to unit length.
///
public void Normalize()
{
double scale = 1.0f / this.Length;
X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
}
#endregion
#region public void NormalizeFast()
///
/// Scales the Vector4d to approximately unit length.
///
public void NormalizeFast()
{
double scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W);
X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
}
#endregion
#region public void Scale()
///
/// Scales the current Vector4d 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(double sx, double sy, double sz, double sw)
{
this.X = X * sx;
this.Y = Y * sy;
this.Z = Z * sz;
this.W = W * sw;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
public void Scale(Vector4d scale)
{
this.X *= scale.X;
this.Y *= scale.Y;
this.Z *= scale.Z;
this.W *= scale.W;
}
/// Scales this instance by the given parameter.
/// The scaling of the individual components.
[CLSCompliant(false)]
public void Scale(ref Vector4d scale)
{
this.X *= scale.X;
this.Y *= scale.Y;
this.Z *= scale.Z;
this.W *= scale.W;
}
#endregion public void Scale()
#endregion
#region Static
#region Add
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static Vector4d Add(Vector4d a, Vector4d 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 Vector4d a, ref Vector4d b, out Vector4d 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 Vector4d Sub(Vector4d a, Vector4d 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 Vector4d a, ref Vector4d b, out Vector4d 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 Vector4d Mult(Vector4d a, double 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 Vector4d a, double f, out Vector4d 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 Vector4d Div(Vector4d a, double f)
{
double 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 Vector4d a, double f, out Vector4d result)
{
double 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 Vector4d Min(Vector4d a, Vector4d 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 Vector4d a, ref Vector4d b, out Vector4d 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 Vector4d Max(Vector4d a, Vector4d 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 Vector4d a, ref Vector4d b, out Vector4d 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 Vector4d Clamp(Vector4d vec, Vector4d min, Vector4d 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 Vector4d vec, ref Vector4d min, ref Vector4d max, out Vector4d 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 Vector4d Normalize(Vector4d vec)
{
double 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 Vector4d vec, out Vector4d result)
{
double 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 Vector4d NormalizeFast(Vector4d vec)
{
double 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 Vector4d vec, out Vector4d result)
{
double 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
///
/// Calculate the dot product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static double Dot(Vector4d left, Vector4d right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
}
///
/// Calculate the dot product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static void Dot(ref Vector4d left, ref Vector4d right, out double result)
{
result = 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.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static Vector4d Lerp(Vector4d a, Vector4d b, double 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;
}
///
/// 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.
/// a when blend=0, b when blend=1, and a linear combination otherwise
public static void Lerp(ref Vector4d a, ref Vector4d b, double blend, out Vector4d result)
{
result.X = blend * (b.X - a.X) + a.X;
result.Y = blend * (b.Y - a.Y) + a.Y;
result.Z = blend * (b.Z - a.Z) + a.Z;
result.W = blend * (b.W - a.W) + a.W;
}
#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 Vector4d BaryCentric(Vector4d a, Vector4d b, Vector4d c, double u, double v)
{
return a + u * (b - a) + v * (c - a);
}
/// Interpolate 3 Vectors using Barycentric coordinates
/// First input Vector.
/// Second input Vector.
/// Third input Vector.
/// First Barycentric Coordinate.
/// Second Barycentric Coordinate.
/// Output Vector. 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 void BaryCentric(ref Vector4d a, ref Vector4d b, ref Vector4d c, float u, float v, out Vector4d result)
{
result = a; // copy
Vector4d temp = b; // copy
temp.Sub(ref a);
temp.Mult(u);
result.Add(ref temp);
temp = c; // copy
temp.Sub(ref a);
temp.Mult(v);
result.Add(ref temp);
}
#endregion
#region Transform
/// Transform a Vector by the given Matrix
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static Vector4d Transform(Vector4d vec, Matrix4d mat)
{
Vector4d result;
result.X = Vector4d.Dot(vec, mat.Column0);
result.Y = Vector4d.Dot(vec, mat.Column1);
result.Z = Vector4d.Dot(vec, mat.Column2);
result.W = Vector4d.Dot(vec, mat.Column3);
return result;
}
/// Transform a Vector by the given Matrix
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static void Transform(ref Vector4d vec, ref Matrix4d mat, out Vector4d result)
{
result.X = vec.X * mat.Row0.X +
vec.Y * mat.Row1.X +
vec.Z * mat.Row2.X +
vec.W * mat.Row3.X;
result.Y = vec.X * mat.Row0.Y +
vec.Y * mat.Row1.Y +
vec.Z * mat.Row2.Y +
vec.W * mat.Row3.Y;
result.Z = vec.X * mat.Row0.Z +
vec.Y * mat.Row1.Z +
vec.Z * mat.Row2.Z +
vec.W * mat.Row3.Z;
result.W = vec.X * mat.Row0.W +
vec.Y * mat.Row1.W +
vec.Z * mat.Row2.W +
vec.W * mat.Row3.W;
}
#endregion
#endregion
#region Swizzle
///
/// Gets or sets an OpenTK.Vector2d with the X and Y components of this instance.
///
[XmlIgnore]
public Vector2d Xy { get { return new Vector2d(X, Y); } set { X = value.X; Y = value.Y; } }
///
/// Gets or sets an OpenTK.Vector3d with the X, Y and Z components of this instance.
///
[XmlIgnore]
public Vector3d Xyz { get { return new Vector3d(X, Y, Z); } set { X = value.X; Y = value.Y; Z = value.Z; } }
#endregion
#region Operators
public static Vector4d operator +(Vector4d left, Vector4d right)
{
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
left.W += right.W;
return left;
}
public static Vector4d operator -(Vector4d left, Vector4d right)
{
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
left.W -= right.W;
return left;
}
public static Vector4d operator -(Vector4d vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
vec.W = -vec.W;
return vec;
}
public static Vector4d operator *(Vector4d vec, double f)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
vec.W *= f;
return vec;
}
public static Vector4d operator *(double f, Vector4d vec)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
vec.W *= f;
return vec;
}
public static Vector4d operator /(Vector4d vec, double f)
{
double mult = 1.0f / f;
vec.X *= mult;
vec.Y *= mult;
vec.Z *= mult;
vec.W *= mult;
return vec;
}
public static bool operator ==(Vector4d left, Vector4d right)
{
return left.Equals(right);
}
public static bool operator !=(Vector4d left, Vector4d right)
{
return !left.Equals(right);
}
[CLSCompliant(false)]
unsafe public static explicit operator double*(Vector4d v)
{
return &v.X;
}
public static explicit operator IntPtr(Vector4d v)
{
unsafe
{
return (IntPtr)(&v.X);
}
}
/// Converts OpenTK.Vector4 to OpenTK.Vector4d.
/// The Vector4 to convert.
/// The resulting Vector4d.
public static explicit operator Vector4d(Vector4 v4)
{
return new Vector4d(v4.X, v4.Y, v4.Z, v4.W);
}
/// Converts OpenTK.Vector4d to OpenTK.Vector4.
/// The Vector4d to convert.
/// The resulting Vector4.
public static explicit operator Vector4(Vector4d v4d)
{
return new Vector4((float)v4d.X, (float)v4d.Y, (float)v4d.Z, (float)v4d.W);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector4d.
///
///
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 Vector4d))
return false;
return this.Equals((Vector4d)obj);
}
#endregion
#endregion
#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(Vector4d other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z &&
W == other.W;
}
#endregion
}
}