#region License /* FNA - XNA4 Reimplementation for Desktop Platforms * Copyright 2009-2016 Ethan Lee and the MonoGame Team * * Released under the Microsoft Public License. * See LICENSE for details. */ /* Derived from code by the Mono.Xna Team (Copyright 2006). * Released under the MIT License. See monoxna.LICENSE for details. */ #endregion #region Using Statements using System; using System.ComponentModel; using System.Diagnostics; using System.Text; using Microsoft.Xna.Framework.Design; #endregion namespace Microsoft.Xna.Framework { /// /// Describes a 3D-vector. /// [Serializable] [TypeConverter(typeof(Vector3Converter))] [DebuggerDisplay("{DebugDisplayString,nq}")] public struct Vector3 : IEquatable { #region Public Static Properties /// /// Returns a with components 0, 0, 0. /// public static Vector3 Zero { get { return zero; } } /// /// Returns a with components 1, 1, 1. /// public static Vector3 One { get { return one; } } /// /// Returns a with components 1, 0, 0. /// public static Vector3 UnitX { get { return unitX; } } /// /// Returns a with components 0, 1, 0. /// public static Vector3 UnitY { get { return unitY; } } /// /// Returns a with components 0, 0, 1. /// public static Vector3 UnitZ { get { return unitZ; } } /// /// Returns a with components 0, 1, 0. /// public static Vector3 Up { get { return up; } } /// /// Returns a with components 0, -1, 0. /// public static Vector3 Down { get { return down; } } /// /// Returns a with components 1, 0, 0. /// public static Vector3 Right { get { return right; } } /// /// Returns a with components -1, 0, 0. /// public static Vector3 Left { get { return left; } } /// /// Returns a with components 0, 0, -1. /// public static Vector3 Forward { get { return forward; } } /// /// Returns a with components 0, 0, 1. /// public static Vector3 Backward { get { return backward; } } #endregion #region Internal Properties internal string DebugDisplayString { get { return string.Concat( X.ToString(), " ", Y.ToString(), " ", Z.ToString() ); } } #endregion #region Private Static Fields private static Vector3 zero = new Vector3(0f, 0f, 0f); // Not readonly for performance -flibit private static readonly Vector3 one = new Vector3(1f, 1f, 1f); private static readonly Vector3 unitX = new Vector3(1f, 0f, 0f); private static readonly Vector3 unitY = new Vector3(0f, 1f, 0f); private static readonly Vector3 unitZ = new Vector3(0f, 0f, 1f); private static readonly Vector3 up = new Vector3(0f, 1f, 0f); private static readonly Vector3 down = new Vector3(0f, -1f, 0f); private static readonly Vector3 right = new Vector3(1f, 0f, 0f); private static readonly Vector3 left = new Vector3(-1f, 0f, 0f); private static readonly Vector3 forward = new Vector3(0f, 0f, -1f); private static readonly Vector3 backward = new Vector3(0f, 0f, 1f); #endregion #region Public Fields /// /// The x coordinate of this . /// public float X; /// /// The y coordinate of this . /// public float Y; /// /// The z coordinate of this . /// public float Z; #endregion #region Public Constructors /// /// Constructs a 3d vector with X, Y and Z from three values. /// /// The x coordinate in 3d-space. /// The y coordinate in 3d-space. /// The z coordinate in 3d-space. public Vector3(float x, float y, float z) { this.X = x; this.Y = y; this.Z = z; } /// /// Constructs a 3d vector with X, Y and Z set to the same value. /// /// The x, y and z coordinates in 3d-space. public Vector3(float value) { this.X = value; this.Y = value; this.Z = value; } /// /// Constructs a 3d vector with X, Y from and Z from a scalar. /// /// The x and y coordinates in 3d-space. /// The z coordinate in 3d-space. public Vector3(Vector2 value, float z) { this.X = value.X; this.Y = value.Y; this.Z = z; } #endregion #region Public Methods /// /// Compares whether current instance is equal to specified . /// /// The to compare. /// true if the instances are equal; false otherwise. public override bool Equals(object obj) { if (!(obj is Vector3)) { return false; } Vector3 other = (Vector3) obj; return ( (MathHelper.WithinEpsilon(X, other.X)) && (MathHelper.WithinEpsilon(Y, other.Y)) && (MathHelper.WithinEpsilon(Z, other.Z)) ); } /// /// Compares whether current instance is equal to specified . /// /// The to compare. /// true if the instances are equal; false otherwise. public bool Equals(Vector3 other) { return ( (MathHelper.WithinEpsilon(X, other.X)) && (MathHelper.WithinEpsilon(Y, other.Y)) && (MathHelper.WithinEpsilon(Z, other.Z)) ); } /// /// Gets the hash code of this . /// /// Hash code of this . public override int GetHashCode() { return (int) (this.X + this.Y + this.Z); } /// /// Returns the length of this . /// /// The length of this . public float Length() { float result; DistanceSquared(ref this, ref zero, out result); return (float) Math.Sqrt(result); } /// /// Returns the squared length of this . /// /// The squared length of this . public float LengthSquared() { float result; DistanceSquared(ref this, ref zero, out result); return result; } /// /// Turns this to a unit vector with the same direction. /// public void Normalize() { Normalize(ref this, out this); } /// /// Returns a representation of this in the format: /// {X:[] Y:[] Z:[]} /// /// A representation of this . public override string ToString() { StringBuilder sb = new StringBuilder(32); sb.Append("{X:"); sb.Append(this.X); sb.Append(" Y:"); sb.Append(this.Y); sb.Append(" Z:"); sb.Append(this.Z); sb.Append("}"); return sb.ToString(); } #endregion #region Public Static Methods /// /// Performs vector addition on and . /// /// The first vector to add. /// The second vector to add. /// The result of the vector addition. public static Vector3 Add(Vector3 value1, Vector3 value2) { value1.X += value2.X; value1.Y += value2.Y; value1.Z += value2.Z; return value1; } /// /// Performs vector addition on and /// , storing the result of the /// addition in . /// /// The first vector to add. /// The second vector to add. /// The result of the vector addition. public static void Add(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = value1.X + value2.X; result.Y = value1.Y + value2.Y; result.Z = value1.Z + value2.Z; } /// /// Creates a new that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 3d-triangle. /// /// The first vector of 3d-triangle. /// The second vector of 3d-triangle. /// The third vector of 3d-triangle. /// Barycentric scalar b2 which represents a weighting factor towards second vector of 3d-triangle. /// Barycentric scalar b3 which represents a weighting factor towards third vector of 3d-triangle. /// The cartesian translation of barycentric coordinates. public static Vector3 Barycentric( Vector3 value1, Vector3 value2, Vector3 value3, float amount1, float amount2 ) { return new Vector3( MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2), MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2), MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2) ); } /// /// Creates a new that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 3d-triangle. /// /// The first vector of 3d-triangle. /// The second vector of 3d-triangle. /// The third vector of 3d-triangle. /// Barycentric scalar b2 which represents a weighting factor towards second vector of 3d-triangle. /// Barycentric scalar b3 which represents a weighting factor towards third vector of 3d-triangle. /// The cartesian translation of barycentric coordinates as an output parameter. public static void Barycentric( ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, float amount1, float amount2, out Vector3 result ) { result.X = MathHelper.Barycentric(value1.X, value2.X, value3.X, amount1, amount2); result.Y = MathHelper.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2); result.Z = MathHelper.Barycentric(value1.Z, value2.Z, value3.Z, amount1, amount2); } /// /// Creates a new that contains CatmullRom interpolation of the specified vectors. /// /// The first vector in interpolation. /// The second vector in interpolation. /// The third vector in interpolation. /// The fourth vector in interpolation. /// Weighting factor. /// The result of CatmullRom interpolation. public static Vector3 CatmullRom( Vector3 value1, Vector3 value2, Vector3 value3, Vector3 value4, float amount ) { return new Vector3( MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount), MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount), MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount) ); } /// /// Creates a new that contains CatmullRom interpolation of the specified vectors. /// /// The first vector in interpolation. /// The second vector in interpolation. /// The third vector in interpolation. /// The fourth vector in interpolation. /// Weighting factor. /// The result of CatmullRom interpolation as an output parameter. public static void CatmullRom( ref Vector3 value1, ref Vector3 value2, ref Vector3 value3, ref Vector3 value4, float amount, out Vector3 result ) { result.X = MathHelper.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount); result.Y = MathHelper.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount); result.Z = MathHelper.CatmullRom(value1.Z, value2.Z, value3.Z, value4.Z, amount); } /// /// Clamps the specified value within a range. /// /// The value to clamp. /// The min value. /// The max value. /// The clamped value. public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max) { return new Vector3( MathHelper.Clamp(value1.X, min.X, max.X), MathHelper.Clamp(value1.Y, min.Y, max.Y), MathHelper.Clamp(value1.Z, min.Z, max.Z) ); } /// /// Clamps the specified value within a range. /// /// The value to clamp. /// The min value. /// The max value. /// The clamped value as an output parameter. public static void Clamp( ref Vector3 value1, ref Vector3 min, ref Vector3 max, out Vector3 result ) { result.X = MathHelper.Clamp(value1.X, min.X, max.X); result.Y = MathHelper.Clamp(value1.Y, min.Y, max.Y); result.Z = MathHelper.Clamp(value1.Z, min.Z, max.Z); } /// /// Computes the cross product of two vectors. /// /// The first vector. /// The second vector. /// The cross product of two vectors. public static Vector3 Cross(Vector3 vector1, Vector3 vector2) { Cross(ref vector1, ref vector2, out vector1); return vector1; } /// /// Computes the cross product of two vectors. /// /// The first vector. /// The second vector. /// The cross product of two vectors as an output parameter. public static void Cross(ref Vector3 vector1, ref Vector3 vector2, out Vector3 result) { float x = vector1.Y * vector2.Z - vector2.Y * vector1.Z; float y = -(vector1.X * vector2.Z - vector2.X * vector1.Z); float z = vector1.X * vector2.Y - vector2.X * vector1.Y; result.X = x; result.Y = y; result.Z = z; } /// /// Returns the distance between two vectors. /// /// The first vector. /// The second vector. /// The distance between two vectors. public static float Distance(Vector3 vector1, Vector3 vector2) { float result; DistanceSquared(ref vector1, ref vector2, out result); return (float) Math.Sqrt(result); } /// /// Returns the distance between two vectors. /// /// The first vector. /// The second vector. /// The distance between two vectors as an output parameter. public static void Distance(ref Vector3 value1, ref Vector3 value2, out float result) { DistanceSquared(ref value1, ref value2, out result); result = (float) Math.Sqrt(result); } /// /// Returns the squared distance between two vectors. /// /// The first vector. /// The second vector. /// The squared distance between two vectors. public static float DistanceSquared(Vector3 value1, Vector3 value2) { return ( (value1.X - value2.X) * (value1.X - value2.X) + (value1.Y - value2.Y) * (value1.Y - value2.Y) + (value1.Z - value2.Z) * (value1.Z - value2.Z) ); } /// /// Returns the squared distance between two vectors. /// /// The first vector. /// The second vector. /// The squared distance between two vectors as an output parameter. public static void DistanceSquared( ref Vector3 value1, ref Vector3 value2, out float result ) { result = ( (value1.X - value2.X) * (value1.X - value2.X) + (value1.Y - value2.Y) * (value1.Y - value2.Y) + (value1.Z - value2.Z) * (value1.Z - value2.Z) ); } /// /// Divides the components of a by the components of another . /// /// Source . /// Divisor . /// The result of dividing the vectors. public static Vector3 Divide(Vector3 value1, Vector3 value2) { value1.X /= value2.X; value1.Y /= value2.Y; value1.Z /= value2.Z; return value1; } /// /// Divides the components of a by the components of another . /// /// Source . /// Divisor . /// The result of dividing the vectors as an output parameter. public static void Divide(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = value1.X / value2.X; result.Y = value1.Y / value2.Y; result.Z = value1.Z / value2.Z; } /// /// Divides the components of a by a scalar. /// /// Source . /// Divisor scalar. /// The result of dividing a vector by a scalar. public static Vector3 Divide(Vector3 value1, float value2) { float factor = 1 / value2; value1.X *= factor; value1.Y *= factor; value1.Z *= factor; return value1; } /// /// Divides the components of a by a scalar. /// /// Source . /// Divisor scalar. /// The result of dividing a vector by a scalar as an output parameter. public static void Divide(ref Vector3 value1, float value2, out Vector3 result) { float factor = 1 / value2; result.X = value1.X * factor; result.Y = value1.Y * factor; result.Z = value1.Z * factor; } /// /// Returns a dot product of two vectors. /// /// The first vector. /// The second vector. /// The dot product of two vectors. public static float Dot(Vector3 vector1, Vector3 vector2) { return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z; } /// /// Returns a dot product of two vectors. /// /// The first vector. /// The second vector. /// The dot product of two vectors as an output parameter. public static void Dot(ref Vector3 vector1, ref Vector3 vector2, out float result) { result = ( (vector1.X * vector2.X) + (vector1.Y * vector2.Y) + (vector1.Z * vector2.Z) ); } /// /// Creates a new that contains hermite spline interpolation. /// /// The first position vector. /// The first tangent vector. /// The second position vector. /// The second tangent vector. /// Weighting factor. /// The hermite spline interpolation vector. public static Vector3 Hermite( Vector3 value1, Vector3 tangent1, Vector3 value2, Vector3 tangent2, float amount ) { Vector3 result = new Vector3(); Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result); return result; } /// /// Creates a new that contains hermite spline interpolation. /// /// The first position vector. /// The first tangent vector. /// The second position vector. /// The second tangent vector. /// Weighting factor. /// The hermite spline interpolation vector as an output parameter. public static void Hermite( ref Vector3 value1, ref Vector3 tangent1, ref Vector3 value2, ref Vector3 tangent2, float amount, out Vector3 result ) { result.X = MathHelper.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount); result.Y = MathHelper.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount); result.Z = MathHelper.Hermite(value1.Z, tangent1.Z, value2.Z, tangent2.Z, amount); } /// /// Creates a new that contains linear interpolation of the specified vectors. /// /// The first vector. /// The second vector. /// Weighting value(between 0.0 and 1.0). /// The result of linear interpolation of the specified vectors. public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount) { return new Vector3( MathHelper.Lerp(value1.X, value2.X, amount), MathHelper.Lerp(value1.Y, value2.Y, amount), MathHelper.Lerp(value1.Z, value2.Z, amount) ); } /// /// Creates a new that contains linear interpolation of the specified vectors. /// /// The first vector. /// The second vector. /// Weighting value(between 0.0 and 1.0). /// The result of linear interpolation of the specified vectors as an output parameter. public static void Lerp( ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result ) { result.X = MathHelper.Lerp(value1.X, value2.X, amount); result.Y = MathHelper.Lerp(value1.Y, value2.Y, amount); result.Z = MathHelper.Lerp(value1.Z, value2.Z, amount); } /// /// Creates a new that contains a maximal values from the two vectors. /// /// The first vector. /// The second vector. /// The with maximal values from the two vectors. public static Vector3 Max(Vector3 value1, Vector3 value2) { return new Vector3( MathHelper.Max(value1.X, value2.X), MathHelper.Max(value1.Y, value2.Y), MathHelper.Max(value1.Z, value2.Z) ); } /// /// Creates a new that contains a maximal values from the two vectors. /// /// The first vector. /// The second vector. /// The with maximal values from the two vectors as an output parameter. public static void Max(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = MathHelper.Max(value1.X, value2.X); result.Y = MathHelper.Max(value1.Y, value2.Y); result.Z = MathHelper.Max(value1.Z, value2.Z); } /// /// Creates a new that contains a minimal values from the two vectors. /// /// The first vector. /// The second vector. /// The with minimal values from the two vectors. public static Vector3 Min(Vector3 value1, Vector3 value2) { return new Vector3( MathHelper.Min(value1.X, value2.X), MathHelper.Min(value1.Y, value2.Y), MathHelper.Min(value1.Z, value2.Z) ); } /// /// Creates a new that contains a minimal values from the two vectors. /// /// The first vector. /// The second vector. /// The with minimal values from the two vectors as an output parameter. public static void Min(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = MathHelper.Min(value1.X, value2.X); result.Y = MathHelper.Min(value1.Y, value2.Y); result.Z = MathHelper.Min(value1.Z, value2.Z); } /// /// Creates a new that contains a multiplication of two vectors. /// /// Source . /// Source . /// The result of the vector multiplication. public static Vector3 Multiply(Vector3 value1, Vector3 value2) { value1.X *= value2.X; value1.Y *= value2.Y; value1.Z *= value2.Z; return value1; } /// /// Creates a new that contains a multiplication of and a scalar. /// /// Source . /// Scalar value. /// The result of the vector multiplication with a scalar. public static Vector3 Multiply(Vector3 value1, float scaleFactor) { value1.X *= scaleFactor; value1.Y *= scaleFactor; value1.Z *= scaleFactor; return value1; } /// /// Creates a new that contains a multiplication of and a scalar. /// /// Source . /// Scalar value. /// The result of the multiplication with a scalar as an output parameter. public static void Multiply(ref Vector3 value1, float scaleFactor, out Vector3 result) { result.X = value1.X * scaleFactor; result.Y = value1.Y * scaleFactor; result.Z = value1.Z * scaleFactor; } /// /// Creates a new that contains a multiplication of two vectors. /// /// Source . /// Source . /// The result of the vector multiplication as an output parameter. public static void Multiply(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = value1.X * value2.X; result.Y = value1.Y * value2.Y; result.Z = value1.Z * value2.Z; } /// /// Creates a new that contains the specified vector inversion. /// /// Source . /// The result of the vector inversion. public static Vector3 Negate(Vector3 value) { value = new Vector3(-value.X, -value.Y, -value.Z); return value; } /// /// Creates a new that contains the specified vector inversion. /// /// Source . /// The result of the vector inversion as an output parameter. public static void Negate(ref Vector3 value, out Vector3 result) { result.X = -value.X; result.Y = -value.Y; result.Z = -value.Z; } /// /// Creates a new that contains a normalized values from another vector. /// /// Source . /// Unit vector. public static Vector3 Normalize(Vector3 value) { Normalize(ref value, out value); return value; } /// /// Creates a new that contains a normalized values from another vector. /// /// Source . /// Unit vector as an output parameter. public static void Normalize(ref Vector3 value, out Vector3 result) { float factor; Distance(ref value, ref zero, out factor); factor = 1.0f / factor; result.X = value.X * factor; result.Y = value.Y * factor; result.Z = value.Z * factor; } /// /// Creates a new that contains reflect vector of the given vector and normal. /// /// Source . /// Reflection normal. /// Reflected vector. public static Vector3 Reflect(Vector3 vector, Vector3 normal) { /* I is the original array. * N is the normal of the incident plane. * R = I - (2 * N * ( DotProduct[ I,N] )) */ Vector3 reflectedVector; // Inline the dotProduct here instead of calling method float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z); reflectedVector.X = vector.X - (2.0f * normal.X) * dotProduct; reflectedVector.Y = vector.Y - (2.0f * normal.Y) * dotProduct; reflectedVector.Z = vector.Z - (2.0f * normal.Z) * dotProduct; return reflectedVector; } /// /// Creates a new that contains reflect vector of the given vector and normal. /// /// Source . /// Reflection normal. /// Reflected vector as an output parameter. public static void Reflect(ref Vector3 vector, ref Vector3 normal, out Vector3 result) { /* I is the original array. * N is the normal of the incident plane. * R = I - (2 * N * ( DotProduct[ I,N] )) */ // Inline the dotProduct here instead of calling method. float dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z); result.X = vector.X - (2.0f * normal.X) * dotProduct; result.Y = vector.Y - (2.0f * normal.Y) * dotProduct; result.Z = vector.Z - (2.0f * normal.Z) * dotProduct; } /// /// Creates a new that contains cubic interpolation of the specified vectors. /// /// Source . /// Source . /// Weighting value. /// Cubic interpolation of the specified vectors. public static Vector3 SmoothStep(Vector3 value1, Vector3 value2, float amount) { return new Vector3( MathHelper.SmoothStep(value1.X, value2.X, amount), MathHelper.SmoothStep(value1.Y, value2.Y, amount), MathHelper.SmoothStep(value1.Z, value2.Z, amount) ); } /// /// Creates a new that contains cubic interpolation of the specified vectors. /// /// Source . /// Source . /// Weighting value. /// Cubic interpolation of the specified vectors as an output parameter. public static void SmoothStep( ref Vector3 value1, ref Vector3 value2, float amount, out Vector3 result ) { result.X = MathHelper.SmoothStep(value1.X, value2.X, amount); result.Y = MathHelper.SmoothStep(value1.Y, value2.Y, amount); result.Z = MathHelper.SmoothStep(value1.Z, value2.Z, amount); } /// /// Creates a new that contains subtraction of on from a another. /// /// Source . /// Source . /// The result of the vector subtraction. public static Vector3 Subtract(Vector3 value1, Vector3 value2) { value1.X -= value2.X; value1.Y -= value2.Y; value1.Z -= value2.Z; return value1; } /// /// Creates a new that contains subtraction of on from a another. /// /// Source . /// Source . /// The result of the vector subtraction as an output parameter. public static void Subtract(ref Vector3 value1, ref Vector3 value2, out Vector3 result) { result.X = value1.X - value2.X; result.Y = value1.Y - value2.Y; result.Z = value1.Z - value2.Z; } /// /// Creates a new that contains a transformation of 3d-vector by the specified . /// /// Source . /// The transformation . /// Transformed . public static Vector3 Transform(Vector3 position, Matrix matrix) { Transform(ref position, ref matrix, out position); return position; } /// /// Creates a new that contains a transformation of 3d-vector by the specified . /// /// Source . /// The transformation . /// Transformed as an output parameter. public static void Transform( ref Vector3 position, ref Matrix matrix, out Vector3 result ) { float x = ( (position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41 ); float y = ( (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42 ); float z = ( (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43 ); result.X = x; result.Y = y; result.Z = z; } /// /// Apply transformation on all vectors within array of by the specified and places the results in an another array. /// /// Source array. /// The transformation . /// Destination array. public static void Transform( Vector3[] sourceArray, ref Matrix matrix, Vector3[] destinationArray ) { Debug.Assert( destinationArray.Length >= sourceArray.Length, "The destination array is smaller than the source array." ); /* TODO: Are there options on some platforms to implement * a vectorized version of this? */ for (int i = 0; i < sourceArray.Length; i += 1) { Vector3 position = sourceArray[i]; destinationArray[i] = new Vector3( (position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41, (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42, (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43 ); } } /// /// Apply transformation on vectors within array of by the specified and places the results in an another array. /// /// Source array. /// The starting index of transformation in the source array. /// The transformation . /// Destination array. /// The starting index in the destination array, where the first should be written. /// The number of vectors to be transformed. public static void Transform( Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, Vector3[] destinationArray, int destinationIndex, int length ) { Debug.Assert( sourceArray.Length - sourceIndex >= length, "The source array is too small for the given sourceIndex and length." ); Debug.Assert( destinationArray.Length - destinationIndex >= length, "The destination array is too small for " + "the given destinationIndex and length." ); /* TODO: Are there options on some platforms to implement a * vectorized version of this? */ for (int i = 0; i < length; i += 1) { Vector3 position = sourceArray[sourceIndex + i]; destinationArray[destinationIndex + i] = new Vector3( (position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41, (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42, (position.X * matrix.M13) + (position.Y * matrix.M23) + (position.Z * matrix.M33) + matrix.M43 ); } } /// /// Creates a new that contains a transformation of 3d-vector by the specified , representing the rotation. /// /// Source . /// The which contains rotation transformation. /// Transformed . public static Vector3 Transform(Vector3 value, Quaternion rotation) { Vector3 result; Transform(ref value, ref rotation, out result); return result; } /// /// Creates a new that contains a transformation of 3d-vector by the specified , representing the rotation. /// /// Source . /// The which contains rotation transformation. /// Transformed as an output parameter. public static void Transform( ref Vector3 value, ref Quaternion rotation, out Vector3 result ) { float x = 2 * (rotation.Y * value.Z - rotation.Z * value.Y); float y = 2 * (rotation.Z * value.X - rotation.X * value.Z); float z = 2 * (rotation.X * value.Y - rotation.Y * value.X); result.X = value.X + x * rotation.W + (rotation.Y * z - rotation.Z * y); result.Y = value.Y + y * rotation.W + (rotation.Z * x - rotation.X * z); result.Z = value.Z + z * rotation.W + (rotation.X * y - rotation.Y * x); } /// /// Apply transformation on all vectors within array of by the specified and places the results in an another array. /// /// Source array. /// The which contains rotation transformation. /// Destination array. public static void Transform( Vector3[] sourceArray, ref Quaternion rotation, Vector3[] destinationArray ) { Debug.Assert( destinationArray.Length >= sourceArray.Length, "The destination array is smaller than the source array." ); /* TODO: Are there options on some platforms to implement * a vectorized version of this? */ for (int i = 0; i < sourceArray.Length; i += 1) { Vector3 position = sourceArray[i]; float x = 2 * (rotation.Y * position.Z - rotation.Z * position.Y); float y = 2 * (rotation.Z * position.X - rotation.X * position.Z); float z = 2 * (rotation.X * position.Y - rotation.Y * position.X); destinationArray[i] = new Vector3( position.X + x * rotation.W + (rotation.Y * z - rotation.Z * y), position.Y + y * rotation.W + (rotation.Z * x - rotation.X * z), position.Z + z * rotation.W + (rotation.X * y - rotation.Y * x) ); } } /// /// Apply transformation on vectors within array of by the specified and places the results in an another array. /// /// Source array. /// The starting index of transformation in the source array. /// The which contains rotation transformation. /// Destination array. /// The starting index in the destination array, where the first should be written. /// The number of vectors to be transformed. public static void Transform( Vector3[] sourceArray, int sourceIndex, ref Quaternion rotation, Vector3[] destinationArray, int destinationIndex, int length ) { Debug.Assert( sourceArray.Length - sourceIndex >= length, "The source array is too small for the given sourceIndex and length." ); Debug.Assert( destinationArray.Length - destinationIndex >= length, "The destination array is too small for the " + "given destinationIndex and length." ); /* TODO: Are there options on some platforms to implement * a vectorized version of this? */ for (int i = 0; i < length; i += 1) { Vector3 position = sourceArray[sourceIndex + i]; float x = 2 * (rotation.Y * position.Z - rotation.Z * position.Y); float y = 2 * (rotation.Z * position.X - rotation.X * position.Z); float z = 2 * (rotation.X * position.Y - rotation.Y * position.X); destinationArray[destinationIndex + i] = new Vector3( position.X + x * rotation.W + (rotation.Y * z - rotation.Z * y), position.Y + y * rotation.W + (rotation.Z * x - rotation.X * z), position.Z + z * rotation.W + (rotation.X * y - rotation.Y * x) ); } } /// /// Creates a new that contains a transformation of the specified normal by the specified . /// /// Source which represents a normal vector. /// The transformation . /// Transformed normal. public static Vector3 TransformNormal(Vector3 normal, Matrix matrix) { TransformNormal(ref normal, ref matrix, out normal); return normal; } /// /// Creates a new that contains a transformation of the specified normal by the specified . /// /// Source which represents a normal vector. /// The transformation . /// Transformed normal as an output parameter. public static void TransformNormal( ref Vector3 normal, ref Matrix matrix, out Vector3 result ) { float x = (normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31); float y = (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32); float z = (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33); result.X = x; result.Y = y; result.Z = z; } /// /// Apply transformation on all normals within array of by the specified and places the results in an another array. /// /// Source array. /// The transformation . /// Destination array. public static void TransformNormal( Vector3[] sourceArray, ref Matrix matrix, Vector3[] destinationArray ) { Debug.Assert( destinationArray.Length >= sourceArray.Length, "The destination array is smaller than the source array." ); for (int i = 0; i < sourceArray.Length; i += 1) { Vector3 normal = sourceArray[i]; destinationArray[i].X = (normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31); destinationArray[i].Y = (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32); destinationArray[i].Z = (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33); } } /// /// Apply transformation on normals within array of by the specified and places the results in an another array. /// /// Source array. /// The starting index of transformation in the source array. /// The transformation . /// Destination array. /// The starting index in the destination array, where the first should be written. /// The number of normals to be transformed. public static void TransformNormal( Vector3[] sourceArray, int sourceIndex, ref Matrix matrix, Vector3[] destinationArray, int destinationIndex, int length ) { if (sourceArray == null) { throw new ArgumentNullException("sourceArray"); } if (destinationArray == null) { throw new ArgumentNullException("destinationArray"); } if ((sourceIndex + length) > sourceArray.Length) { throw new ArgumentException( "the combination of sourceIndex and " + "length was greater than sourceArray.Length" ); } if ((destinationIndex + length) > destinationArray.Length) { throw new ArgumentException( "destinationArray is too small to " + "contain the result" ); } for (int i = 0; i < length; i += 1) { Vector3 normal = sourceArray[i + sourceIndex]; destinationArray[i + destinationIndex].X = ( (normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31) ); destinationArray[i + destinationIndex].Y = ( (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32) ); destinationArray[i + destinationIndex].Z = ( (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33) ); } } #endregion #region Public Static Operators /// /// Compares whether two instances are equal. /// /// instance on the left of the equal sign. /// instance on the right of the equal sign. /// true if the instances are equal; false otherwise. public static bool operator ==(Vector3 value1, Vector3 value2) { return ( (MathHelper.WithinEpsilon(value1.X, value2.X)) && (MathHelper.WithinEpsilon(value1.Y, value2.Y)) && (MathHelper.WithinEpsilon(value1.Z, value2.Z)) ); } /// /// Compares whether two instances are not equal. /// /// instance on the left of the not equal sign. /// instance on the right of the not equal sign. /// true if the instances are not equal; false otherwise. public static bool operator !=(Vector3 value1, Vector3 value2) { return !(value1 == value2); } /// /// Adds two vectors. /// /// Source on the left of the add sign. /// Source on the right of the add sign. /// Sum of the vectors. public static Vector3 operator +(Vector3 value1, Vector3 value2) { value1.X += value2.X; value1.Y += value2.Y; value1.Z += value2.Z; return value1; } /// /// Inverts values in the specified . /// /// Source on the right of the sub sign. /// Result of the inversion. public static Vector3 operator -(Vector3 value) { value = new Vector3(-value.X, -value.Y, -value.Z); return value; } /// /// Subtracts a from a . /// /// Source on the left of the sub sign. /// Source on the right of the sub sign. /// Result of the vector subtraction. public static Vector3 operator -(Vector3 value1, Vector3 value2) { value1.X -= value2.X; value1.Y -= value2.Y; value1.Z -= value2.Z; return value1; } /// /// Multiplies the components of two vectors by each other. /// /// Source on the left of the mul sign. /// Source on the right of the mul sign. /// Result of the vector multiplication. public static Vector3 operator *(Vector3 value1, Vector3 value2) { value1.X *= value2.X; value1.Y *= value2.Y; value1.Z *= value2.Z; return value1; } /// /// Multiplies the components of vector by a scalar. /// /// Source on the left of the mul sign. /// Scalar value on the right of the mul sign. /// Result of the vector multiplication with a scalar. public static Vector3 operator *(Vector3 value, float scaleFactor) { value.X *= scaleFactor; value.Y *= scaleFactor; value.Z *= scaleFactor; return value; } /// /// Multiplies the components of vector by a scalar. /// /// Scalar value on the left of the mul sign. /// Source on the right of the mul sign. /// Result of the vector multiplication with a scalar. public static Vector3 operator *(float scaleFactor, Vector3 value) { value.X *= scaleFactor; value.Y *= scaleFactor; value.Z *= scaleFactor; return value; } /// /// Divides the components of a by the components of another . /// /// Source on the left of the div sign. /// Divisor on the right of the div sign. /// The result of dividing the vectors. public static Vector3 operator /(Vector3 value1, Vector3 value2) { value1.X /= value2.X; value1.Y /= value2.Y; value1.Z /= value2.Z; return value1; } /// /// Divides the components of a by a scalar. /// /// Source on the left of the div sign. /// Divisor scalar on the right of the div sign. /// The result of dividing a vector by a scalar. public static Vector3 operator /(Vector3 value, float divider) { float factor = 1 / divider; value.X *= factor; value.Y *= factor; value.Z *= factor; return value; } #endregion } }