#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;
namespace OpenTK.Math
{
/// A 3-dimensional vector using double-precision floating point numbers.
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector3d : IEquatable
{
#region Fields
///
/// The X component of the Vector3.
///
public double X;
///
/// The Y component of the Vector3.
///
public double Y;
///
/// The Z component of the Vector3.
///
public double Z;
#endregion
#region Constructors
///
/// Constructs a new Vector3.
///
/// The x component of the Vector3.
/// The y component of the Vector3.
/// The z component of the Vector3.
public Vector3d(double x, double y, double z)
{
X = x;
Y = y;
Z = z;
}
///
/// Constructs a new instance from the given Vector2d.
///
/// The Vector2d to copy components from.
public Vector3d(Vector2d v)
{
X = v.X;
Y = v.Y;
Z = 0.0f;
}
///
/// Constructs a new instance from the given Vector3d.
///
/// The Vector3d to copy components from.
public Vector3d(Vector3d v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
///
/// Constructs a new instance from the given Vector4d.
///
/// The Vector4d to copy components from.
public Vector3d(Vector4d v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
#endregion
#region Public Members
#region Instance
#region public double Length
///
/// Gets the length (magnitude) of the vector.
///
///
///
public double Length
{
get
{
return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z);
}
}
#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 / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y + Z * Z);
}
}
#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;
}
}
#endregion
#region public void Normalize()
///
/// Scales the Vector3d to unit length.
///
public void Normalize()
{
double scale = 1.0f / this.Length;
X *= scale;
Y *= scale;
Z *= scale;
}
#endregion
#region public void NormalizeFast()
///
/// Scales the Vector3d to approximately unit length.
///
public void NormalizeFast()
{
double scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z);
X *= scale;
Y *= scale;
Z *= scale;
}
#endregion
#region public void Scale(double sx, double sy, double sz)
///
/// Scales the current Vector3d by the given amounts.
///
/// The scale of the X component.
/// The scale of the Y component.
/// The scale of the Z component.
public void Scale(double sx, double sy, double sz)
{
this.X = X * sx;
this.Y = Y * sy;
this.Z = Z * sz;
}
#endregion
#endregion
#region Static
#region Fields
///
/// Defines a unit-length Vector3d that points towards the X-axis.
///
public static readonly Vector3d UnitX = new Vector3d(1, 0, 0);
///
/// Defines a unit-length Vector3d that points towards the Y-axis.
///
public static readonly Vector3d UnitY = new Vector3d(0, 1, 0);
///
/// /// Defines a unit-length Vector3d that points towards the Z-axis.
///
public static readonly Vector3d UnitZ = new Vector3d(0, 0, 1);
///
/// Defines a zero-length Vector3.
///
public static readonly Vector3d Zero = new Vector3d(0, 0, 0);
///
/// Defines the size of the Vector3d struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3d());
#endregion
#region Add
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static Vector3d Add(Vector3d a, Vector3d b)
{
a.X += b.X;
a.Y += b.Y;
a.Z += b.Z;
return a;
}
///
/// Add two Vectors
///
/// First operand
/// Second operand
/// Result of addition
public static void Add(ref Vector3d a, ref Vector3d b, out Vector3d result)
{
result.X = a.X + b.X;
result.Y = a.Y + b.Y;
result.Z = a.Z + b.Z;
}
#endregion
#region Sub
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static Vector3d Sub(Vector3d a, Vector3d b)
{
a.X -= b.X;
a.Y -= b.Y;
a.Z -= b.Z;
return a;
}
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
public static void Sub(ref Vector3d a, ref Vector3d b, out Vector3d result)
{
result.X = a.X - b.X;
result.Y = a.Y - b.Y;
result.Z = a.Z - b.Z;
}
#endregion
#region Mult
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static Vector3d Mult(Vector3d a, double f)
{
a.X *= f;
a.Y *= f;
a.Z *= f;
return a;
}
///
/// Multiply a vector and a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the multiplication
public static void Mult(ref Vector3d a, double f, out Vector3d result)
{
result.X = a.X * f;
result.Y = a.Y * f;
result.Z = a.Z * f;
}
#endregion
#region Div
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static Vector3d Div(Vector3d a, double f)
{
double mult = 1.0f / f;
a.X *= mult;
a.Y *= mult;
a.Z *= mult;
return a;
}
///
/// Divide a vector by a scalar
///
/// Vector operand
/// Scalar operand
/// Result of the division
public static void Div(ref Vector3d a, double f, out Vector3d result)
{
double mult = 1.0f / f;
result.X = a.X * mult;
result.Y = a.Y * mult;
result.Z = a.Z * mult;
}
#endregion
#region ComponentMin
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static Vector3d ComponentMin(Vector3d a, Vector3d 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;
return a;
}
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
public static void ComponentMin(ref Vector3d a, ref Vector3d b, out Vector3d 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;
}
#endregion
#region ComponentMax
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static Vector3d ComponentMax(Vector3d a, Vector3d 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;
return a;
}
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
public static void ComponentMax(ref Vector3d a, ref Vector3d b, out Vector3d 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;
}
#endregion
#region Min
///
/// Returns the Vector3d with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static Vector3d Min(Vector3d left, Vector3d right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
#endregion
#region Max
///
/// Returns the Vector3d with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
public static Vector3d Max(Vector3d left, Vector3d right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
public static Vector3d Clamp(Vector3d vec, Vector3d min, Vector3d 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.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
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 Vector3d vec, ref Vector3d min, ref Vector3d max, out Vector3d 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.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
}
#endregion
#region Normalize
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static Vector3d Normalize(Vector3d vec)
{
double scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
return vec;
}
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
public static void Normalize(ref Vector3d vec, out Vector3d result)
{
double scale = 1.0f / vec.Length;
result.X = vec.X * scale;
result.Y = vec.Y * scale;
result.Z = vec.Z * scale;
}
#endregion
#region NormalizeFast
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static Vector3d NormalizeFast(Vector3d vec)
{
double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z);
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
return vec;
}
///
/// Scale a vector to approximately unit length
///
/// The input vector
/// The normalized vector
public static void NormalizeFast(ref Vector3d vec, out Vector3d result)
{
double scale = Functions.InverseSqrtFast(vec.X * vec.X + vec.Y * vec.Y + vec.Z * vec.Z);
result.X = vec.X * scale;
result.Y = vec.Y * scale;
result.Z = vec.Z * scale;
}
#endregion
#region Dot
///
/// Caclulate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
public static double Dot(Vector3d left, Vector3d right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
}
#endregion
#region Cross
///
/// Caclulate the cross (vector) product of two vectors
///
/// First operand
/// Second operand
/// The cross product of the two inputs
public static Vector3d Cross(Vector3d left, Vector3d right)
{
double
x = left.Y * right.Z - left.Z * right.Y,
y = left.Z * right.X - left.X * right.Z,
z = left.X * right.Y - left.Y * right.X;
left.X = x;
left.Y = y;
left.Z = z;
return left;
}
///
/// Caclulate the cross (vector) product of two vectors
///
/// First operand
/// Second operand
/// The cross product of the two inputs
/// The cross product of the two inputs
public static void Cross(ref Vector3d left, ref Vector3d right, out Vector3d result)
{
result.X = left.Y * right.Z - left.Z * right.Y;
result.Y = left.Z * right.X - left.X * right.Z;
result.Z = left.X * right.Y - left.Y * right.X;
}
#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 Vector3d Lerp(Vector3d a, Vector3d 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;
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 Vector3d BaryCentric(Vector3d a, Vector3d b, Vector3d c, double u, double v)
{
return a + u * (b - a) + v * (c - a);
}
#endregion
#region Transform
///
/// Transform a direction vector by the given Matrix
/// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static Vector3d TransformVector(Vector3d vec, Matrix4d mat)
{
return new Vector3d(
Vector3d.Dot(vec, new Vector3d(mat.Column0)),
Vector3d.Dot(vec, new Vector3d(mat.Column1)),
Vector3d.Dot(vec, new Vector3d(mat.Column2)));
}
///
/// Transform a Normal by the given Matrix
///
///
/// This calculates the inverse of the given matrix, use TransformNormalInverse if you
/// already have the inverse to avoid this extra calculation
///
/// The normal to transform
/// The desired transformation
/// The transformed normal
public static Vector3d TransformNormal(Vector3d norm, Matrix4d mat)
{
mat.Invert();
return TransformNormalInverse(norm, mat);
}
///
/// Transform a Normal by the (transpose of the) given Matrix
///
///
/// This version doesn't calculate the inverse matrix.
/// Use this version if you already have the inverse of the desired transform to hand
///
/// The normal to transform
/// The inverse of the desired transformation
/// The transformed normal
public static Vector3d TransformNormalInverse(Vector3d norm, Matrix4d invMat)
{
return new Vector3d(
Vector3d.Dot(norm, new Vector3d(invMat.Row0)),
Vector3d.Dot(norm, new Vector3d(invMat.Row1)),
Vector3d.Dot(norm, new Vector3d(invMat.Row2)));
}
///
/// Transform a Position by the given Matrix
///
/// The position to transform
/// The desired transformation
/// The transformed position
public static Vector3d TransformPosition(Vector3d pos, Matrix4d mat)
{
return new Vector3d(
Vector3d.Dot(pos, new Vector3d(mat.Column0)) + mat.Row3.X,
Vector3d.Dot(pos, new Vector3d(mat.Column1)) + mat.Row3.Y,
Vector3d.Dot(pos, new Vector3d(mat.Column2)) + mat.Row3.Z);
}
///
/// Transform a Vector by the given Matrix
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static Vector4d Transform(Vector3d vec, Matrix4d mat)
{
Vector4d v4 = new Vector4d(vec.X, vec.Y, vec.Z, 1.0f);
return new Vector4d(
Vector4d.Dot(v4, mat.Column0),
Vector4d.Dot(v4, mat.Column1),
Vector4d.Dot(v4, mat.Column2),
Vector4d.Dot(v4, mat.Column3));
}
///
/// Transform a Vector3d by the given Matrix, and project the resulting Vector4 back to a Vector3
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
public static Vector3d TransformPerspective(Vector3d vec, Matrix4d mat)
{
Vector4d h = Transform(vec, mat);
return new Vector3d(h.X / h.W, h.Y / h.W, h.Z / h.W);
}
#endregion
#region CalculateAngle
///
/// Calculates the angle (in radians) between two vectors.
///
/// The first vector.
/// The second vector.
/// Angle (in radians) between the vectors.
/// Note that the returned angle is never bigger than the constant Pi.
public static double CalculateAngle(Vector3d first, Vector3d second)
{
return (float)System.Math.Acos((Vector3d.Dot(first, second)) / (first.Length * second.Length));
}
#endregion
#endregion
#region Operators
public static Vector3d operator +(Vector3d left, Vector3d right)
{
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;
}
public static Vector3d operator -(Vector3d left, Vector3d right)
{
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;
}
public static Vector3d operator -(Vector3d vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
return vec;
}
public static Vector3d operator *(Vector3d vec, double f)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
return vec;
}
public static Vector3d operator *(double f, Vector3d vec)
{
vec.X *= f;
vec.Y *= f;
vec.Z *= f;
return vec;
}
public static Vector3d operator /(Vector3d vec, double f)
{
double mult = 1.0f / f;
vec.X *= mult;
vec.Y *= mult;
vec.Z *= mult;
return vec;
}
public static bool operator ==(Vector3d left, Vector3d right)
{
return left.Equals(right);
}
public static bool operator !=(Vector3d left, Vector3d right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector3.
///
///
public override string ToString()
{
return String.Format("({0}, {1}, {2})", X, Y, Z);
}
#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();
}
#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 Vector3))
return false;
return this.Equals((Vector3)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(Vector3d other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z;
}
#endregion
}
}