// Copyright (c) Xenko contributors. (https://xenko.com)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace math
{
///
/// Represents a two dimensional mathematical vector with double-precision floats.
///
[DataContract( Name = "double2")]
[DataStyle(DataStyle.Compact)]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Double2 : IEquatable, IFormattable
{
///
/// The size of the type, in bytes.
///
public static readonly int SizeInBytes = lib.Util.SizeOf();
///
/// A with all of its components set to zero.
///
public static readonly Double2 Zero = new Double2();
///
/// The X unit (1, 0).
///
public static readonly Double2 UnitX = new Double2(1.0, 0.0);
///
/// The Y unit (0, 1).
///
public static readonly Double2 UnitY = new Double2(0.0, 1.0);
///
/// A with all of its components set to one.
///
public static readonly Double2 One = new Double2(1.0, 1.0);
///
/// The X component of the vector.
///
[DataMember( Order = 0 )]
public double X;
///
/// The Y component of the vector.
///
[DataMember( Order = 1 )]
public double Y;
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public Double2(double value)
{
X = value;
Y = value;
}
///
/// Initializes a new instance of the struct.
///
/// Initial value for the X component of the vector.
/// Initial value for the Y component of the vector.
public Double2(double x, double y)
{
X = x;
Y = y;
}
///
/// Initializes a new instance of the struct.
///
/// The values to assign to the X and Y components of the vector. This must be an array with two elements.
/// Thrown when is null.
/// Thrown when contains more or less than two elements.
public Double2(double[] values)
{
if (values == null)
throw new ArgumentNullException("values");
if (values.Length != 2)
throw new ArgumentOutOfRangeException("values", "There must be two and only two input values for Double2.");
X = values[0];
Y = values[1];
}
///
/// Initializes a new instance of the struct.
///
/// The Vector2 to construct the Double2 from.
public Double2(Vec2 v)
{
X = v.X;
Y = v.Y;
}
///
/// Gets a value indicting whether this instance is normalized.
///
public bool IsNormalized
{
get { return Math.Abs((X * X) + (Y * Y) - 1f) < MathUtil.ZeroTolerance; }
}
///
/// Gets or sets the component at the specified index.
///
/// The value of the X or Y component, depending on the index.
/// The index of the component to access. Use 0 for the X component and 1 for the Y component.
/// The value of the component at the specified index.
/// Thrown when the is out of the range [0, 1].
public double this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
}
throw new ArgumentOutOfRangeException("index", "Indices for Double2 run from 0 to 1, inclusive.");
}
set
{
switch (index)
{
case 0: X = value; break;
case 1: Y = value; break;
default: throw new ArgumentOutOfRangeException("index", "Indices for Double2 run from 0 to 1, inclusive.");
}
}
}
///
/// Calculates the length of the vector.
///
/// The length of the vector.
///
/// may be preferred when only the relative length is needed
/// and speed is of the essence.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double Length()
{
return (double)Math.Sqrt((X * X) + (Y * Y));
}
///
/// Calculates the squared length of the vector.
///
/// The squared length of the vector.
///
/// This method may be preferred to when only a relative length is needed
/// and speed is of the essence.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public double LengthSquared()
{
return (X * X) + (Y * Y);
}
///
/// Converts the vector into a unit vector.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize()
{
double length = Length();
if (length > MathUtil.ZeroTolerance)
{
double inv = 1.0 / length;
X *= inv;
Y *= inv;
}
}
///
/// Creates an array containing the elements of the vector.
///
/// A two-element array containing the components of the vector.
public double[] ToArray()
{
return new double[] { X, Y };
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// When the method completes, contains the sum of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(ref Double2 left, ref Double2 right, out Double2 result)
{
result = new Double2(left.X + right.X, left.Y + right.Y);
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Add(Double2 left, Double2 right)
{
return new Double2(left.X + right.X, left.Y + right.Y);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// When the method completes, contains the difference of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Subtract(ref Double2 left, ref Double2 right, out Double2 result)
{
result = new Double2(left.X - right.X, left.Y - right.Y);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Subtract(Double2 left, Double2 right)
{
return new Double2(left.X - right.X, left.Y - right.Y);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// When the method completes, contains the scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Multiply(ref Double2 value, double scale, out Double2 result)
{
result = new Double2(value.X * scale, value.Y * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Multiply(Double2 value, double scale)
{
return new Double2(value.X * scale, value.Y * scale);
}
///
/// Modulates a vector with another by performing component-wise multiplication.
///
/// The first vector to modulate.
/// The second vector to modulate.
/// When the method completes, contains the modulated vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Modulate(ref Double2 left, ref Double2 right, out Double2 result)
{
result = new Double2(left.X * right.X, left.Y * right.Y);
}
///
/// Modulates a vector with another by performing component-wise multiplication.
///
/// The first vector to modulate.
/// The second vector to modulate.
/// The modulated vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Modulate(Double2 left, Double2 right)
{
return new Double2(left.X * right.X, left.Y * right.Y);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// When the method completes, contains the scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Divide(ref Double2 value, double scale, out Double2 result)
{
result = new Double2(value.X / scale, value.Y / scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Divide(Double2 value, double scale)
{
return new Double2(value.X / scale, value.Y / scale);
}
///
/// Demodulates a vector with another by performing component-wise division.
///
/// The first vector to demodulate.
/// The second vector to demodulate.
/// When the method completes, contains the demodulated vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Demodulate(ref Double2 left, ref Double2 right, out Double2 result)
{
result = new Double2(left.X / right.X, left.Y / right.Y);
}
///
/// Demodulates a vector with another by performing component-wise division.
///
/// The first vector to demodulate.
/// The second vector to demodulate.
/// The demodulated vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Demodulate(Double2 left, Double2 right)
{
return new Double2(left.X / right.X, left.Y / right.Y);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// When the method completes, contains a vector facing in the opposite direction.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Negate(ref Double2 value, out Double2 result)
{
result = new Double2(-value.X, -value.Y);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Negate(Double2 value)
{
return new Double2(-value.X, -value.Y);
}
///
/// Returns a containing the 2D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 2D triangle.
///
/// A containing the 2D Cartesian coordinates of vertex 1 of the triangle.
/// A containing the 2D Cartesian coordinates of vertex 2 of the triangle.
/// A containing the 2D Cartesian coordinates of vertex 3 of the triangle.
/// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ).
/// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ).
/// When the method completes, contains the 2D Cartesian coordinates of the specified point.
public static void Barycentric(ref Double2 value1, ref Double2 value2, ref Double2 value3, double amount1, double amount2, out Double2 result)
{
result = new Double2((value1.X + (amount1 * (value2.X - value1.X))) + (amount2 * (value3.X - value1.X)),
(value1.Y + (amount1 * (value2.Y - value1.Y))) + (amount2 * (value3.Y - value1.Y)));
}
///
/// Returns a containing the 2D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 2D triangle.
///
/// A containing the 2D Cartesian coordinates of vertex 1 of the triangle.
/// A containing the 2D Cartesian coordinates of vertex 2 of the triangle.
/// A containing the 2D Cartesian coordinates of vertex 3 of the triangle.
/// Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in ).
/// Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in ).
/// A new containing the 2D Cartesian coordinates of the specified point.
public static Double2 Barycentric(Double2 value1, Double2 value2, Double2 value3, double amount1, double amount2)
{
Double2 result;
Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result);
return result;
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// When the method completes, contains the clamped value.
public static void Clamp(ref Double2 value, ref Double2 min, ref Double2 max, out Double2 result)
{
double x = value.X;
x = (x > max.X) ? max.X : x;
x = (x < min.X) ? min.X : x;
double y = value.Y;
y = (y > max.Y) ? max.Y : y;
y = (y < min.Y) ? min.Y : y;
result = new Double2(x, y);
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// The clamped value.
public static Double2 Clamp(Double2 value, Double2 min, Double2 max)
{
Double2 result;
Clamp(ref value, ref min, ref max, out result);
return result;
}
///
/// Calculates the distance between two vectors.
///
/// The first vector.
/// The second vector.
/// When the method completes, contains the distance between the two vectors.
///
/// may be preferred when only the relative distance is needed
/// and speed is of the essence.
///
public static void Distance(ref Double2 value1, ref Double2 value2, out double result)
{
double x = value1.X - value2.X;
double y = value1.Y - value2.Y;
result = (double)Math.Sqrt((x * x) + (y * y));
}
///
/// Calculates the distance between two vectors.
///
/// The first vector.
/// The second vector.
/// The distance between the two vectors.
///
/// may be preferred when only the relative distance is needed
/// and speed is of the essence.
///
public static double Distance(Double2 value1, Double2 value2)
{
double x = value1.X - value2.X;
double y = value1.Y - value2.Y;
return (double)Math.Sqrt((x * x) + (y * y));
}
///
/// Calculates the squared distance between two vectors.
///
/// The first vector.
/// The second vector
/// When the method completes, contains the squared distance between the two vectors.
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
///
public static void DistanceSquared(ref Double2 value1, ref Double2 value2, out double result)
{
double x = value1.X - value2.X;
double y = value1.Y - value2.Y;
result = (x * x) + (y * y);
}
///
/// Calculates the squared distance between two vectors.
///
/// The first vector.
/// The second vector.
/// The squared distance between the two vectors.
/// Distance squared is the value before taking the square root.
/// Distance squared can often be used in place of distance if relative comparisons are being made.
/// For example, consider three points A, B, and C. To determine whether B or C is further from A,
/// compare the distance between A and B to the distance between A and C. Calculating the two distances
/// involves two square roots, which are computationally expensive. However, using distance squared
/// provides the same information and avoids calculating two square roots.
///
public static double DistanceSquared(Double2 value1, Double2 value2)
{
double x = value1.X - value2.X;
double y = value1.Y - value2.Y;
return (x * x) + (y * y);
}
///
/// Calculates the dot product of two vectors.
///
/// First source vector.
/// Second source vector.
/// When the method completes, contains the dot product of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Dot(ref Double2 left, ref Double2 right, out double result)
{
result = (left.X * right.X) + (left.Y * right.Y);
}
///
/// Calculates the dot product of two vectors.
///
/// First source vector.
/// Second source vector.
/// The dot product of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static double Dot(Double2 left, Double2 right)
{
return (left.X * right.X) + (left.Y * right.Y);
}
///
/// Converts the vector into a unit vector.
///
/// The vector to normalize.
/// When the method completes, contains the normalized vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Normalize(ref Double2 value, out Double2 result)
{
result = value;
result.Normalize();
}
///
/// Converts the vector into a unit vector.
///
/// The vector to normalize.
/// The normalized vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Normalize(Double2 value)
{
value.Normalize();
return value;
}
///
/// Performs a linear interpolation between two vectors.
///
/// Start vector.
/// End vector.
/// Value between 0 and 1 indicating the weight of .
/// When the method completes, contains the linear interpolation of the two vectors.
///
/// This method performs the linear interpolation based on the following formula.
/// start + (end - start) * amount
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
///
public static void Lerp(ref Double2 start, ref Double2 end, double amount, out Double2 result)
{
result.X = start.X + ((end.X - start.X) * amount);
result.Y = start.Y + ((end.Y - start.Y) * amount);
}
///
/// Performs a linear interpolation between two vectors.
///
/// Start vector.
/// End vector.
/// Value between 0 and 1 indicating the weight of .
/// The linear interpolation of the two vectors.
///
/// This method performs the linear interpolation based on the following formula.
/// start + (end - start) * amount
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
///
public static Double2 Lerp(Double2 start, Double2 end, double amount)
{
Double2 result;
Lerp(ref start, ref end, amount, out result);
return result;
}
///
/// Performs a cubic interpolation between two vectors.
///
/// Start vector.
/// End vector.
/// Value between 0 and 1 indicating the weight of .
/// When the method completes, contains the cubic interpolation of the two vectors.
public static void SmoothStep(ref Double2 start, ref Double2 end, double amount, out Double2 result)
{
amount = (amount > 1.0) ? 1.0 : ((amount < 0.0) ? 0.0 : amount);
amount = (amount * amount) * (3.0f - (2.0f * amount));
result.X = start.X + ((end.X - start.X) * amount);
result.Y = start.Y + ((end.Y - start.Y) * amount);
}
///
/// Performs a cubic interpolation between two vectors.
///
/// Start vector.
/// End vector.
/// Value between 0 and 1 indicating the weight of .
/// The cubic interpolation of the two vectors.
public static Double2 SmoothStep(Double2 start, Double2 end, double amount)
{
Double2 result;
SmoothStep(ref start, ref end, amount, out result);
return result;
}
///
/// Performs a Hermite spline interpolation.
///
/// First source position vector.
/// First source tangent vector.
/// Second source position vector.
/// Second source tangent vector.
/// Weighting factor.
/// When the method completes, contains the result of the Hermite spline interpolation.
public static void Hermite(ref Double2 value1, ref Double2 tangent1, ref Double2 value2, ref Double2 tangent2, double amount, out Double2 result)
{
double squared = amount * amount;
double cubed = amount * squared;
double part1 = ((2.0f * cubed) - (3.0f * squared)) + 1.0;
double part2 = (-2.0f * cubed) + (3.0f * squared);
double part3 = (cubed - (2.0f * squared)) + amount;
double part4 = cubed - squared;
result.X = (((value1.X * part1) + (value2.X * part2)) + (tangent1.X * part3)) + (tangent2.X * part4);
result.Y = (((value1.Y * part1) + (value2.Y * part2)) + (tangent1.Y * part3)) + (tangent2.Y * part4);
}
///
/// Performs a Hermite spline interpolation.
///
/// First source position vector.
/// First source tangent vector.
/// Second source position vector.
/// Second source tangent vector.
/// Weighting factor.
/// The result of the Hermite spline interpolation.
public static Double2 Hermite(Double2 value1, Double2 tangent1, Double2 value2, Double2 tangent2, double amount)
{
Double2 result;
Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
return result;
}
///
/// Performs a Catmull-Rom interpolation using the specified positions.
///
/// The first position in the interpolation.
/// The second position in the interpolation.
/// The third position in the interpolation.
/// The fourth position in the interpolation.
/// Weighting factor.
/// When the method completes, contains the result of the Catmull-Rom interpolation.
public static void CatmullRom(ref Double2 value1, ref Double2 value2, ref Double2 value3, ref Double2 value4, double amount, out Double2 result)
{
double squared = amount * amount;
double cubed = amount * squared;
result.X = 0.5f * ((((2.0f * value2.X) + ((-value1.X + value3.X) * amount)) +
(((((2.0f * value1.X) - (5.0f * value2.X)) + (4.0f * value3.X)) - value4.X) * squared)) +
((((-value1.X + (3.0f * value2.X)) - (3.0f * value3.X)) + value4.X) * cubed));
result.Y = 0.5f * ((((2.0f * value2.Y) + ((-value1.Y + value3.Y) * amount)) +
(((((2.0f * value1.Y) - (5.0f * value2.Y)) + (4.0f * value3.Y)) - value4.Y) * squared)) +
((((-value1.Y + (3.0f * value2.Y)) - (3.0f * value3.Y)) + value4.Y) * cubed));
}
///
/// Performs a Catmull-Rom interpolation using the specified positions.
///
/// The first position in the interpolation.
/// The second position in the interpolation.
/// The third position in the interpolation.
/// The fourth position in the interpolation.
/// Weighting factor.
/// A vector that is the result of the Catmull-Rom interpolation.
public static Double2 CatmullRom(Double2 value1, Double2 value2, Double2 value3, Double2 value4, double amount)
{
Double2 result;
CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
return result;
}
///
/// Returns a vector containing the smallest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// When the method completes, contains an new vector composed of the largest components of the source vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Max(ref Double2 left, ref Double2 right, out Double2 result)
{
result.X = (left.X > right.X) ? left.X : right.X;
result.Y = (left.Y > right.Y) ? left.Y : right.Y;
}
///
/// Returns a vector containing the largest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// A vector containing the largest components of the source vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Max(Double2 left, Double2 right)
{
Double2 result;
Max(ref left, ref right, out result);
return result;
}
///
/// Returns a vector containing the smallest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// When the method completes, contains an new vector composed of the smallest components of the source vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Min(ref Double2 left, ref Double2 right, out Double2 result)
{
result.X = (left.X < right.X) ? left.X : right.X;
result.Y = (left.Y < right.Y) ? left.Y : right.Y;
}
///
/// Returns a vector containing the smallest components of the specified vectors.
///
/// The first source vector.
/// The second source vector.
/// A vector containing the smallest components of the source vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 Min(Double2 left, Double2 right)
{
Double2 result;
Min(ref left, ref right, out result);
return result;
}
///
/// Returns the reflection of a vector off a surface that has the specified normal.
///
/// The source vector.
/// Normal of the surface.
/// When the method completes, contains the reflected vector.
/// Reflect only gives the direction of a reflection off a surface, it does not determine
/// whether the original vector was close enough to the surface to hit it.
public static void Reflect(ref Double2 vector, ref Double2 normal, out Double2 result)
{
double dot = (vector.X * normal.X) + (vector.Y * normal.Y);
result.X = vector.X - ((2.0f * dot) * normal.X);
result.Y = vector.Y - ((2.0f * dot) * normal.Y);
}
///
/// Returns the reflection of a vector off a surface that has the specified normal.
///
/// The source vector.
/// Normal of the surface.
/// The reflected vector.
/// Reflect only gives the direction of a reflection off a surface, it does not determine
/// whether the original vector was close enough to the surface to hit it.
public static Double2 Reflect(Double2 vector, Double2 normal)
{
Double2 result;
Reflect(ref vector, ref normal, out result);
return result;
}
///
/// Orthogonalizes a list of vectors.
///
/// The list of orthogonalized vectors.
/// The list of vectors to orthogonalize.
///
/// Orthogonalization is the process of making all vectors orthogonal to each other. This
/// means that any given vector in the list will be orthogonal to any other given vector in the
/// list.
/// Because this method uses the modified Gram-Schmidt process, the resulting vectors
/// tend to be numerically unstable. The numeric stability decreases according to the vectors
/// position in the list so that the first vector is the most stable and the last vector is the
/// least stable.
///
/// Thrown when or is null.
/// Thrown when is shorter in length than .
public static void Orthogonalize(Double2[] destination, params Double2[] source)
{
//Uses the modified Gram-Schmidt process.
//q1 = m1
//q2 = m2 - ((q1 ⋅ m2) / (q1 ⋅ q1)) * q1
//q3 = m3 - ((q1 ⋅ m3) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m3) / (q2 ⋅ q2)) * q2
//q4 = m4 - ((q1 ⋅ m4) / (q1 ⋅ q1)) * q1 - ((q2 ⋅ m4) / (q2 ⋅ q2)) * q2 - ((q3 ⋅ m4) / (q3 ⋅ q3)) * q3
//q5 = ...
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
Double2 newvector = source[i];
for (int r = 0; r < i; ++r)
{
newvector -= (Double2.Dot(destination[r], newvector) / Double2.Dot(destination[r], destination[r])) * destination[r];
}
destination[i] = newvector;
}
}
///
/// Orthonormalizes a list of vectors.
///
/// The list of orthonormalized vectors.
/// The list of vectors to orthonormalize.
///
/// Orthonormalization is the process of making all vectors orthogonal to each
/// other and making all vectors of unit length. This means that any given vector will
/// be orthogonal to any other given vector in the list.
/// Because this method uses the modified Gram-Schmidt process, the resulting vectors
/// tend to be numerically unstable. The numeric stability decreases according to the vectors
/// position in the list so that the first vector is the most stable and the last vector is the
/// least stable.
///
/// Thrown when or is null.
/// Thrown when is shorter in length than .
public static void Orthonormalize(Double2[] destination, params Double2[] source)
{
//Uses the modified Gram-Schmidt process.
//Because we are making unit vectors, we can optimize the math for orthogonalization
//and simplify the projection operation to remove the division.
//q1 = m1 / |m1|
//q2 = (m2 - (q1 ⋅ m2) * q1) / |m2 - (q1 ⋅ m2) * q1|
//q3 = (m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2) / |m3 - (q1 ⋅ m3) * q1 - (q2 ⋅ m3) * q2|
//q4 = (m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3) / |m4 - (q1 ⋅ m4) * q1 - (q2 ⋅ m4) * q2 - (q3 ⋅ m4) * q3|
//q5 = ...
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
Double2 newvector = source[i];
for (int r = 0; r < i; ++r)
{
newvector -= Double2.Dot(destination[r], newvector) * destination[r];
}
newvector.Normalize();
destination[i] = newvector;
}
}
///
/// Transforms a 2D vector by the given rotation.
///
/// The vector to rotate.
/// The rotation to apply.
/// When the method completes, contains the transformed .
public static void Transform(ref Double2 vector, ref Quaternion rotation, out Double2 result)
{
double x = rotation.X + rotation.X;
double y = rotation.Y + rotation.Y;
double z = rotation.Z + rotation.Z;
double wz = rotation.W * z;
double xx = rotation.X * x;
double xy = rotation.X * y;
double yy = rotation.Y * y;
double zz = rotation.Z * z;
result = new Double2((vector.X * (1.0 - yy - zz)) + (vector.Y * (xy - wz)), (vector.X * (xy + wz)) + (vector.Y * (1.0 - xx - zz)));
}
///
/// Transforms a 2D vector by the given rotation.
///
/// The vector to rotate.
/// The rotation to apply.
/// The transformed .
public static Double2 Transform(Double2 vector, Quaternion rotation)
{
Double2 result;
Transform(ref vector, ref rotation, out result);
return result;
}
///
/// Transforms an array of vectors by the given rotation.
///
/// The array of vectors to transform.
/// The rotation to apply.
/// The array for which the transformed vectors are stored.
/// This array may be the same array as .
/// Thrown when or is null.
/// Thrown when is shorter in length than .
public static void Transform(Double2[] source, ref Quaternion rotation, Double2[] destination)
{
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
double x = rotation.X + rotation.X;
double y = rotation.Y + rotation.Y;
double z = rotation.Z + rotation.Z;
double wz = rotation.W * z;
double xx = rotation.X * x;
double xy = rotation.X * y;
double yy = rotation.Y * y;
double zz = rotation.Z * z;
double num1 = (1.0 - yy - zz);
double num2 = (xy - wz);
double num3 = (xy + wz);
double num4 = (1.0 - xx - zz);
for (int i = 0; i < source.Length; ++i)
{
destination[i] = new Double2(
(source[i].X * num1) + (source[i].Y * num2),
(source[i].X * num3) + (source[i].Y * num4));
}
}
///
/// Transforms a 2D vector by the given .
///
/// The source vector.
/// The transformation .
/// When the method completes, contains the transformed .
public static void Transform(ref Double2 vector, ref Matrix transform, out Double4 result)
{
result = new Double4(
(vector.X * transform.M11) + (vector.Y * transform.M21) + transform.M41,
(vector.X * transform.M12) + (vector.Y * transform.M22) + transform.M42,
(vector.X * transform.M13) + (vector.Y * transform.M23) + transform.M43,
(vector.X * transform.M14) + (vector.Y * transform.M24) + transform.M44);
}
///
/// Transforms a 2D vector by the given .
///
/// The source vector.
/// The transformation .
/// The transformed .
public static Double4 Transform(Double2 vector, Matrix transform)
{
Double4 result;
Transform(ref vector, ref transform, out result);
return result;
}
///
/// Transforms an array of 2D vectors by the given .
///
/// The array of vectors to transform.
/// The transformation .
/// The array for which the transformed vectors are stored.
/// Thrown when or is null.
/// Thrown when is shorter in length than .
public static void Transform(Double2[] source, ref Matrix transform, Double4[] destination)
{
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
Transform(ref source[i], ref transform, out destination[i]);
}
}
///
/// Performs a coordinate transformation using the given .
///
/// The coordinate vector to transform.
/// The transformation .
/// When the method completes, contains the transformed coordinates.
///
/// A coordinate transform performs the transformation with the assumption that the w component
/// is one. The four dimensional vector obtained from the transformation operation has each
/// component in the vector divided by the w component. This forces the wcomponent to be one and
/// therefore makes the vector homogeneous. The homogeneous vector is often prefered when working
/// with coordinates as the w component can safely be ignored.
///
public static void TransformCoordinate(ref Double2 coordinate, ref Matrix transform, out Double2 result)
{
Double4 vector = new Double4();
vector.X = (coordinate.X * transform.M11) + (coordinate.Y * transform.M21) + transform.M41;
vector.Y = (coordinate.X * transform.M12) + (coordinate.Y * transform.M22) + transform.M42;
vector.Z = (coordinate.X * transform.M13) + (coordinate.Y * transform.M23) + transform.M43;
vector.W = 1f / ((coordinate.X * transform.M14) + (coordinate.Y * transform.M24) + transform.M44);
result = new Double2(vector.X * vector.W, vector.Y * vector.W);
}
///
/// Performs a coordinate transformation using the given .
///
/// The coordinate vector to transform.
/// The transformation .
/// The transformed coordinates.
///
/// A coordinate transform performs the transformation with the assumption that the w component
/// is one. The four dimensional vector obtained from the transformation operation has each
/// component in the vector divided by the w component. This forces the wcomponent to be one and
/// therefore makes the vector homogeneous. The homogeneous vector is often prefered when working
/// with coordinates as the w component can safely be ignored.
///
public static Double2 TransformCoordinate(Double2 coordinate, Matrix transform)
{
Double2 result;
TransformCoordinate(ref coordinate, ref transform, out result);
return result;
}
///
/// Performs a coordinate transformation on an array of vectors using the given .
///
/// The array of coordinate vectors to trasnform.
/// The transformation .
/// The array for which the transformed vectors are stored.
/// This array may be the same array as .
/// Thrown when or is null.
/// Thrown when is shorter in length than .
///
/// A coordinate transform performs the transformation with the assumption that the w component
/// is one. The four dimensional vector obtained from the transformation operation has each
/// component in the vector divided by the w component. This forces the wcomponent to be one and
/// therefore makes the vector homogeneous. The homogeneous vector is often prefered when working
/// with coordinates as the w component can safely be ignored.
///
public static void TransformCoordinate(Double2[] source, ref Matrix transform, Double2[] destination)
{
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
TransformCoordinate(ref source[i], ref transform, out destination[i]);
}
}
///
/// Performs a normal transformation using the given .
///
/// The normal vector to transform.
/// The transformation .
/// When the method completes, contains the transformed normal.
///
/// A normal transform performs the transformation with the assumption that the w component
/// is zero. This causes the fourth row and fourth collumn of the matrix to be unused. The
/// end result is a vector that is not translated, but all other transformation properties
/// apply. This is often prefered for normal vectors as normals purely represent direction
/// rather than location because normal vectors should not be translated.
///
public static void TransformNormal(ref Double2 normal, ref Matrix transform, out Double2 result)
{
result = new Double2(
(normal.X * transform.M11) + (normal.Y * transform.M21),
(normal.X * transform.M12) + (normal.Y * transform.M22));
}
///
/// Performs a normal transformation using the given .
///
/// The normal vector to transform.
/// The transformation .
/// The transformed normal.
///
/// A normal transform performs the transformation with the assumption that the w component
/// is zero. This causes the fourth row and fourth collumn of the matrix to be unused. The
/// end result is a vector that is not translated, but all other transformation properties
/// apply. This is often prefered for normal vectors as normals purely represent direction
/// rather than location because normal vectors should not be translated.
///
public static Double2 TransformNormal(Double2 normal, Matrix transform)
{
Double2 result;
TransformNormal(ref normal, ref transform, out result);
return result;
}
///
/// Performs a normal transformation on an array of vectors using the given .
///
/// The array of normal vectors to transform.
/// The transformation .
/// The array for which the transformed vectors are stored.
/// This array may be the same array as .
/// Thrown when or is null.
/// Thrown when is shorter in length than .
///
/// A normal transform performs the transformation with the assumption that the w component
/// is zero. This causes the fourth row and fourth collumn of the matrix to be unused. The
/// end result is a vector that is not translated, but all other transformation properties
/// apply. This is often prefered for normal vectors as normals purely represent direction
/// rather than location because normal vectors should not be translated.
///
public static void TransformNormal(Double2[] source, ref Matrix transform, Double2[] destination)
{
if (source == null)
throw new ArgumentNullException("source");
if (destination == null)
throw new ArgumentNullException("destination");
if (destination.Length < source.Length)
throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
for (int i = 0; i < source.Length; ++i)
{
TransformNormal(ref source[i], ref transform, out destination[i]);
}
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator +(Double2 left, Double2 right)
{
return new Double2(left.X + right.X, left.Y + right.Y);
}
///
/// Assert a vector (return it unchanged).
///
/// The vector to assert (unchange).
/// The asserted (unchanged) vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator +(Double2 value)
{
return value;
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator -(Double2 left, Double2 right)
{
return new Double2(left.X - right.X, left.Y - right.Y);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator -(Double2 value)
{
return new Double2(-value.X, -value.Y);
}
///
/// Modulates a vector with another by performing component-wise multiplication.
///
/// The first vector to multiply.
/// The second vector to multiply.
/// The multiplication of the two vectors.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator *(Double2 left, Double2 right)
{
return new Double2(left.X * right.X, left.Y * right.Y);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator *(double scale, Double2 value)
{
return new Double2(value.X * scale, value.Y * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator *(Double2 value, double scale)
{
return new Double2(value.X * scale, value.Y * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator /(Double2 value, double scale)
{
return new Double2(value.X / scale, value.Y / scale);
}
///
/// Divides a numerator by a vector.
///
/// The numerator.
/// The value.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator /(double numerator, Double2 value)
{
return new Double2(numerator / value.X, numerator / value.Y);
}
///
/// Divides a vector by the given vector, component-wise.
///
/// The vector to scale.
/// The by.
/// The scaled vector.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Double2 operator /(Double2 value, Double2 by)
{
return new Double2(value.X / by.X, value.Y / by.Y);
}
///
/// 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.
public static bool operator ==(Double2 left, Double2 right)
{
return left.Equals(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.
public static bool operator !=(Double2 left, Double2 right)
{
return !left.Equals(right);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vec2(Double2 value)
{
return new Vec2((float)value.X, (float)value.Y);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Double2(Vec2 value)
{
return new Double2(value);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Half2(Double2 value)
{
return new Half2((Half)value.X, (Half)value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Double2(Half2 value)
{
return new Double2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Double3(Double2 value)
{
return new Double3(value, 0.0);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Double4(Double2 value)
{
return new Double4(value, 0.0, 0.0);
}
///
/// Returns a that represents this instance.
///
///
/// A that represents this instance.
///
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1}", X, Y);
}
///
/// Returns a that represents this instance.
///
/// The format.
///
/// A that represents this instance.
///
public string ToString(string format)
{
if (format == null)
return ToString();
return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1}", X.ToString(format, CultureInfo.CurrentCulture), Y.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, "X:{0} Y:{1}", X, Y);
}
///
/// Returns a that represents this instance.
///
/// The format.
/// The format provider.
///
/// A that represents this instance.
///
public string ToString(string format, IFormatProvider formatProvider)
{
if (format == null)
ToString(formatProvider);
return string.Format(formatProvider, "X:{0} Y:{1}", X.ToString(format, formatProvider), Y.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()
{
return X.GetHashCode() + Y.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.
///
public bool Equals(Double2 other)
{
return ((double)Math.Abs(other.X - X) < MathUtil.ZeroTolerance &&
(double)Math.Abs(other.Y - Y) < MathUtil.ZeroTolerance);
}
///
/// 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 == null)
return false;
if (value.GetType() != GetType())
return false;
return Equals((Double2)value);
}
#if WPFInterop
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator System.Windows.Point(Double2 value)
{
return new System.Windows.Point(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Double2(System.Windows.Point value)
{
return new Double2(value.X, value.Y);
}
#endif
#if XnaInterop
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Microsoft.Xna.Framework.Vector2(Double2 value)
{
return new Microsoft.Xna.Framework.Vector2(value.X, value.Y);
}
///
/// Performs an implicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Double2(Microsoft.Xna.Framework.Vector2 value)
{
return new Double2(value.X, value.Y);
}
#endif
}
}