// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#if USE_LARGE_WORLDS
using Real = System.Double;
using Mathr = FlaxEngine.Mathd;
#else
using Real = System.Single;
using Mathr = FlaxEngine.Mathf;
#endif
// -----------------------------------------------------------------------------
// Original code from SharpDX project. https://github.com/sharpdx/SharpDX/
// Greetings to Alexandre Mutel. Original code published with the following license:
// -----------------------------------------------------------------------------
// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel
//
// 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.
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* 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.
*/
using System;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace FlaxEngine
{
///
/// Represents a four dimensional mathematical vector.
///
[Unmanaged]
[Serializable]
[StructLayout(LayoutKind.Sequential)]
#if FLAX_EDITOR
[System.ComponentModel.TypeConverter(typeof(TypeConverters.Vector4Converter))]
#endif
public partial struct Vector4 : IEquatable, IFormattable
{
private static readonly string _formatString = "X:{0:F2} Y:{1:F2} Z:{2:F2} W:{3:F2}";
///
/// The X component.
///
public Real X;
///
/// The Y component.
///
public Real Y;
///
/// The Z component.
///
public Real Z;
///
/// The W component.
///
public Real W;
///
/// The size of the type, in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Vector4));
///
/// A with all of its components set to zero.
///
public static readonly Vector4 Zero;
///
/// The X unit (1, 0, 0, 0).
///
public static readonly Vector4 UnitX = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);
///
/// The Y unit (0, 1, 0, 0).
///
public static readonly Vector4 UnitY = new Vector4(0.0f, 1.0f, 0.0f, 0.0f);
///
/// The Z unit (0, 0, 1, 0).
///
public static readonly Vector4 UnitZ = new Vector4(0.0f, 0.0f, 1.0f, 0.0f);
///
/// The W unit (0, 0, 0, 1).
///
public static readonly Vector4 UnitW = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
///
/// A with all of its components set to half.
///
public static readonly Vector4 Half = new Vector4(0.5f, 0.5f, 0.5f, 0.5f);
///
/// A with all of its components set to one.
///
public static readonly Vector4 One = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
///
/// A with all components equal to (or if using 32-bit precision).
///
public static readonly Vector4 Minimum = new Vector4(Real.MinValue);
///
/// A with all components equal to (or if using 32-bit precision).
///
public static readonly Vector4 Maximum = new Vector4(Real.MaxValue);
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public Vector4(float value)
{
X = value;
Y = value;
Z = value;
W = 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.
/// Initial value for the Z component of the vector.
/// Initial value for the W component of the vector.
public Vector4(float x, float y, float z, float w)
{
X = x;
Y = y;
Z = z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// The value that will be assigned to all components.
public Vector4(double value)
{
X = Y = Z = W = (Real)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.
/// Initial value for the Z component of the vector.
/// Initial value for the W component of the vector.
public Vector4(double x, double y, double z, double w)
{
X = (Real)x;
Y = (Real)y;
Z = (Real)z;
W = (Real)w;
}
///
/// Initializes a new instance of the struct.
///
/// A vector containing the values with which to initialize the X, Y, and Z components.
/// Initial value for the W component of the vector.
public Vector4(Vector3 value, Real w)
{
X = value.X;
Y = value.Y;
Z = value.Z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// A vector containing the values with which to initialize the X and Y components.
/// A vector containing the values with which to initialize the Z and W components.
public Vector4(Vector2 xy, Vector2 zw)
{
X = xy.X;
Y = xy.Y;
Z = zw.X;
W = zw.Y;
}
///
/// Initializes a new instance of the struct.
///
/// A vector containing the values with which to initialize the X and Y components.
/// Initial value for the Z component of the vector.
/// Initial value for the W component of the vector.
public Vector4(Vector2 value, Real z, Real w)
{
X = value.X;
Y = value.Y;
Z = z;
W = w;
}
///
/// Initializes a new instance of the struct.
///
/// The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.
/// Thrown when is null.
/// Thrown when contains more or less than four elements.
public Vector4(Real[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
if (values.Length != 4)
throw new ArgumentOutOfRangeException(nameof(values), "There must be four and only four input values for Vector4.");
X = values[0];
Y = values[1];
Z = values[2];
W = values[3];
}
///
/// Gets a value indicting whether this instance is normalized.
///
public bool IsNormalized => Mathr.IsOne(X * X + Y * Y + Z * Z + W * W);
///
/// Gets a value indicting whether this vector is zero
///
public bool IsZero => Mathr.IsZero(X) && Mathr.IsZero(Y) && Mathr.IsZero(Z) && Mathr.IsZero(W);
///
/// Gets a value indicting whether this vector is one
///
public bool IsOne => Mathr.IsOne(X) && Mathr.IsOne(Y) && Mathr.IsOne(Z) && Mathr.IsOne(W);
///
/// Gets a minimum component value
///
public Real MinValue => Mathr.Min(X, Mathr.Min(Y, Mathr.Min(Z, W)));
///
/// Gets a maximum component value
///
public Real MaxValue => Mathr.Max(X, Mathr.Max(Y, Mathr.Max(Z, W)));
///
/// Gets an arithmetic average value of all vector components.
///
public Real AvgValue => (X + Y + Z + W) * (1.0f / 4.0f);
///
/// Gets a sum of the component values.
///
public Real ValuesSum => X + Y + Z + W;
///
/// Gets a vector with values being absolute values of that vector.
///
public Vector4 Absolute => new Vector4(Mathr.Abs(X), Mathr.Abs(Y), Mathr.Abs(Z), Mathr.Abs(W));
///
/// Gets a vector with values being opposite to values of that vector.
///
public Vector4 Negative => new Vector4(-X, -Y, -Z, -W);
///
/// Gets or sets the component at the specified index.
///
/// The value of the X, Y, Z, or W component, depending on the index.
/// The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component.
/// The value of the component at the specified index.
/// Thrown when the is out of the range [0,3].
public Real this[int index]
{
get
{
switch (index)
{
case 0: return X;
case 1: return Y;
case 2: return Z;
case 3: return W;
}
throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, inclusive.");
}
set
{
switch (index)
{
case 0:
X = value;
break;
case 1:
Y = value;
break;
case 2:
Z = value;
break;
case 3:
W = value;
break;
default: throw new ArgumentOutOfRangeException(nameof(index), "Indices for Vector4 run from 0 to 3, 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.
public Real Length => (Real)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
///
/// 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.
public Real LengthSquared => X * X + Y * Y + Z * Z + W * W;
///
/// Converts the vector into a unit vector.
///
public void Normalize()
{
Real length = (Real)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
if (length >= Mathr.Epsilon)
{
Real inverse = 1.0f / length;
X *= inverse;
Y *= inverse;
Z *= inverse;
W *= inverse;
}
}
///
/// Creates an array containing the elements of the vector.
///
/// A four-element array containing the components of the vector.
public Real[] ToArray()
{
return new[] { X, Y, Z, W };
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// When the method completes, contains the sum of the two vectors.
public static void Add(ref Vector4 left, ref Vector4 right, out Vector4 result)
{
result = new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Vector4 Add(Vector4 left, Vector4 right)
{
return new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Performs a component-wise addition.
///
/// The input vector
/// The scalar value to be added to elements
/// The vector with added scalar for each element.
public static void Add(ref Vector4 left, ref Real right, out Vector4 result)
{
result = new Vector4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
///
/// Performs a component-wise addition.
///
/// The input vector
/// The scalar value to be added to elements
/// The vector with added scalar for each element.
public static Vector4 Add(Vector4 left, Real right)
{
return new Vector4(left.X + right, left.Y + right, left.Z + right, left.W + right);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// When the method completes, contains the difference of the two vectors.
public static void Subtract(ref Vector4 left, ref Vector4 right, out Vector4 result)
{
result = new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
public static Vector4 Subtract(Vector4 left, Vector4 right)
{
return new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Performs a component-wise subtraction.
///
/// The input vector
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar for each element.
public static void Subtract(ref Vector4 left, ref Real right, out Vector4 result)
{
result = new Vector4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
///
/// Performs a component-wise subtraction.
///
/// The input vector
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar for each element.
public static Vector4 Subtract(Vector4 left, Real right)
{
return new Vector4(left.X - right, left.Y - right, left.Z - right, left.W - right);
}
///
/// Performs a component-wise subtraction.
///
/// The scalar value to be subtracted from elements
/// The input vector.
/// The vector with subtracted scalar for each element.
public static void Subtract(ref Real left, ref Vector4 right, out Vector4 result)
{
result = new Vector4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
///
/// Performs a component-wise subtraction.
///
/// The scalar value to be subtracted from elements
/// The input vector.
/// The vector with subtracted scalar for each element.
public static Vector4 Subtract(Real left, Vector4 right)
{
return new Vector4(left - right.X, left - right.Y, left - right.Z, left - right.W);
}
///
/// 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.
public static void Multiply(ref Vector4 value, Real scale, out Vector4 result)
{
result = new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 Multiply(Vector4 value, Real scale)
{
return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Multiplies a vector with another by performing component-wise multiplication.
///
/// The first vector to multiply.
/// The second vector to multiply.
/// When the method completes, contains the multiplied vector.
public static void Multiply(ref Vector4 left, ref Vector4 right, out Vector4 result)
{
result = new Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Multiplies a vector with another by performing component-wise multiplication.
///
/// The first vector to multiply.
/// The second vector to multiply.
/// The multiplied vector.
public static Vector4 Multiply(Vector4 left, Vector4 right)
{
return new Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// 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.
public static void Divide(ref Vector4 value, Real scale, out Vector4 result)
{
result = new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 Divide(Vector4 value, Real scale)
{
return new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// When the method completes, contains the scaled vector.
public static void Divide(Real scale, ref Vector4 value, out Vector4 result)
{
result = new Vector4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 Divide(Real scale, Vector4 value)
{
return new Vector4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// When the method completes, contains a vector facing in the opposite direction.
public static void Negate(ref Vector4 value, out Vector4 result)
{
result = new Vector4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
public static Vector4 Negate(Vector4 value)
{
return new Vector4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Returns a containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
///
/// A containing the 4D Cartesian coordinates of vertex 1 of the triangle.
/// A containing the 4D Cartesian coordinates of vertex 2 of the triangle.
/// A containing the 4D 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 4D Cartesian coordinates of the specified point.
public static void Barycentric(ref Vector4 value1, ref Vector4 value2, ref Vector4 value3, float amount1, float amount2, out Vector4 result)
{
result = new Vector4(value1.X + amount1 * (value2.X - value1.X) + amount2 * (value3.X - value1.X),
value1.Y + amount1 * (value2.Y - value1.Y) + amount2 * (value3.Y - value1.Y),
value1.Z + amount1 * (value2.Z - value1.Z) + amount2 * (value3.Z - value1.Z),
value1.W + amount1 * (value2.W - value1.W) + amount2 * (value3.W - value1.W));
}
///
/// Returns a containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
///
/// A containing the 4D Cartesian coordinates of vertex 1 of the triangle.
/// A containing the 4D Cartesian coordinates of vertex 2 of the triangle.
/// A containing the 4D 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 4D Cartesian coordinates of the specified point.
public static Vector4 Barycentric(Vector4 value1, Vector4 value2, Vector4 value3, float amount1, float amount2)
{
Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out Vector4 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 Vector4 value, ref Vector4 min, ref Vector4 max, out Vector4 result)
{
Real x = value.X;
x = x > max.X ? max.X : x;
x = x < min.X ? min.X : x;
Real y = value.Y;
y = y > max.Y ? max.Y : y;
y = y < min.Y ? min.Y : y;
Real z = value.Z;
z = z > max.Z ? max.Z : z;
z = z < min.Z ? min.Z : z;
Real w = value.W;
w = w > max.W ? max.W : w;
w = w < min.W ? min.W : w;
result = new Vector4(x, y, z, w);
}
///
/// Restricts a value to be within a specified range.
///
/// The value to clamp.
/// The minimum value.
/// The maximum value.
/// The clamped value.
public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max)
{
Clamp(ref value, ref min, ref max, out Vector4 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 Vector4 value1, ref Vector4 value2, out Real result)
{
Real x = value1.X - value2.X;
Real y = value1.Y - value2.Y;
Real z = value1.Z - value2.Z;
Real w = value1.W - value2.W;
result = (Real)Math.Sqrt(x * x + y * y + z * z + w * w);
}
///
/// 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 Real Distance(Vector4 value1, Vector4 value2)
{
Real x = value1.X - value2.X;
Real y = value1.Y - value2.Y;
Real z = value1.Z - value2.Z;
Real w = value1.W - value2.W;
return (Real)Math.Sqrt(x * x + y * y + z * z + w * w);
}
///
/// 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.
public static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out Real result)
{
Real x = value1.X - value2.X;
Real y = value1.Y - value2.Y;
Real z = value1.Z - value2.Z;
Real w = value1.W - value2.W;
result = x * x + y * y + z * z + w * w;
}
///
/// Calculates the squared distance between two vectors.
///
/// The first vector.
/// The second vector.
/// The squared distance between the two vectors.
public static Real DistanceSquared(Vector4 value1, Vector4 value2)
{
Real x = value1.X - value2.X;
Real y = value1.Y - value2.Y;
Real z = value1.Z - value2.Z;
Real w = value1.W - value2.W;
return x * x + y * y + z * z + w * w;
}
///
/// Tests whether one vector is near another vector.
///
/// The left vector.
/// The right vector.
/// The epsilon.
/// true if left and right are near another, false otherwise
public static bool NearEqual(Vector4 left, Vector4 right, float epsilon = Mathf.Epsilon)
{
return NearEqual(ref left, ref right, epsilon);
}
///
/// Tests whether one vector is near another vector.
///
/// The left vector.
/// The right vector.
/// The epsilon.
/// true if left and right are near another, false otherwise
public static bool NearEqual(ref Vector4 left, ref Vector4 right, float epsilon = Mathf.Epsilon)
{
return Mathf.WithinEpsilon(left.X, right.X, epsilon) && Mathf.WithinEpsilon(left.Y, right.Y, epsilon) && Mathf.WithinEpsilon(left.Z, right.Z, epsilon) && Mathf.WithinEpsilon(left.W, right.W, epsilon);
}
///
/// 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.
public static void Dot(ref Vector4 left, ref Vector4 right, out Real result)
{
result = left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
}
///
/// Calculates the dot product of two vectors.
///
/// First source vector.
/// Second source vector.
/// The dot product of the two vectors.
public static Real Dot(Vector4 left, Vector4 right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z + left.W * right.W;
}
///
/// Converts the vector into a unit vector.
///
/// The vector to normalize.
/// When the method completes, contains the normalized vector.
public static void Normalize(ref Vector4 value, out Vector4 result)
{
result = value;
result.Normalize();
}
///
/// Converts the vector into a unit vector.
///
/// The vector to normalize.
/// The normalized vector.
public static Vector4 Normalize(Vector4 value)
{
value.Normalize();
return value;
}
///
/// Makes sure that Length of the output vector is always below max and above 0.
///
/// Input Vector.
/// Max Length
public static Vector4 ClampLength(Vector4 vector, Real max)
{
return ClampLength(vector, 0, max);
}
///
/// Makes sure that Length of the output vector is always below max and above min.
///
/// Input Vector.
/// Min Length
/// Max Length
public static Vector4 ClampLength(Vector4 vector, Real min, Real max)
{
ClampLength(vector, min, max, out Vector4 result);
return result;
}
///
/// Makes sure that Length of the output vector is always below max and above min.
///
/// Input Vector.
/// Min Length
/// Max Length
/// The result vector.
public static void ClampLength(Vector4 vector, Real min, Real max, out Vector4 result)
{
result = vector;
Real lenSq = result.LengthSquared;
if (lenSq > max * max)
{
var scaleFactor = max / (Real)Math.Sqrt(lenSq);
result.X *= scaleFactor;
result.Y *= scaleFactor;
result.Z *= scaleFactor;
result.W *= scaleFactor;
}
if (lenSq < min * min)
{
var scaleFactor = min / (Real)Math.Sqrt(lenSq);
result.X *= scaleFactor;
result.Y *= scaleFactor;
result.Z *= scaleFactor;
result.W *= scaleFactor;
}
}
///
/// 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.
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
public static void Lerp(ref Vector4 start, ref Vector4 end, Real amount, out Vector4 result)
{
result.X = Mathr.Lerp(start.X, end.X, amount);
result.Y = Mathr.Lerp(start.Y, end.Y, amount);
result.Z = Mathr.Lerp(start.Z, end.Z, amount);
result.W = Mathr.Lerp(start.W, end.W, 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.
/// Passing a value of 0 will cause to be returned; a value of 1 will cause to be returned.
public static Vector4 Lerp(Vector4 start, Vector4 end, Real amount)
{
Lerp(ref start, ref end, amount, out Vector4 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 Vector4 start, ref Vector4 end, Real amount, out Vector4 result)
{
amount = Mathr.SmoothStep(amount);
Lerp(ref start, ref end, amount, out result);
}
///
/// 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 Vector4 SmoothStep(Vector4 start, Vector4 end, Real amount)
{
SmoothStep(ref start, ref end, amount, out Vector4 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 Vector4 value1, ref Vector4 tangent1, ref Vector4 value2, ref Vector4 tangent2, float amount, out Vector4 result)
{
float squared = amount * amount;
float cubed = amount * squared;
float part1 = 2.0f * cubed - 3.0f * squared + 1.0f;
float part2 = -2.0f * cubed + 3.0f * squared;
float part3 = cubed - 2.0f * squared + amount;
float part4 = cubed - squared;
result = new Vector4(value1.X * part1 + value2.X * part2 + tangent1.X * part3 + tangent2.X * part4,
value1.Y * part1 + value2.Y * part2 + tangent1.Y * part3 + tangent2.Y * part4,
value1.Z * part1 + value2.Z * part2 + tangent1.Z * part3 + tangent2.Z * part4,
value1.W * part1 + value2.W * part2 + tangent1.W * part3 + tangent2.W * 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 Vector4 Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount)
{
Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out Vector4 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 Vector4 value1, ref Vector4 value2, ref Vector4 value3, ref Vector4 value4, float amount, out Vector4 result)
{
float squared = amount * amount;
float 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);
result.Z = 0.5f * (2.0f * value2.Z + (-value1.Z + value3.Z) * amount + (2.0f * value1.Z - 5.0f * value2.Z + 4.0f * value3.Z - value4.Z) * squared + (-value1.Z + 3.0f * value2.Z - 3.0f * value3.Z + value4.Z) * cubed);
result.W = 0.5f * (2.0f * value2.W + (-value1.W + value3.W) * amount + (2.0f * value1.W - 5.0f * value2.W + 4.0f * value3.W - value4.W) * squared + (-value1.W + 3.0f * value2.W - 3.0f * value3.W + value4.W) * 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 Vector4 CatmullRom(Vector4 value1, Vector4 value2, Vector4 value3, Vector4 value4, float amount)
{
CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out Vector4 result);
return result;
}
///
/// Returns a vector containing the largest 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.
public static void Max(ref Vector4 left, ref Vector4 right, out Vector4 result)
{
result.X = left.X > right.X ? left.X : right.X;
result.Y = left.Y > right.Y ? left.Y : right.Y;
result.Z = left.Z > right.Z ? left.Z : right.Z;
result.W = left.W > right.W ? left.W : right.W;
}
///
/// 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.
public static Vector4 Max(Vector4 left, Vector4 right)
{
Max(ref left, ref right, out Vector4 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.
public static void Min(ref Vector4 left, ref Vector4 right, out Vector4 result)
{
result.X = left.X < right.X ? left.X : right.X;
result.Y = left.Y < right.Y ? left.Y : right.Y;
result.Z = left.Z < right.Z ? left.Z : right.Z;
result.W = left.W < right.W ? left.W : right.W;
}
///
/// 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.
public static Vector4 Min(Vector4 left, Vector4 right)
{
Min(ref left, ref right, out Vector4 result);
return result;
}
///
/// Returns the absolute value of a vector.
///
/// The value.
/// A vector which components are less or equal to 0.
public static Vector4 Abs(Vector4 v)
{
return new Vector4(Math.Abs(v.X), Math.Abs(v.Y), Math.Abs(v.Z), Math.Abs(v.W));
}
///
/// Transforms a 4D 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 Vector4 vector, ref Quaternion rotation, out Vector4 result)
{
float x = rotation.X + rotation.X;
float y = rotation.Y + rotation.Y;
float z = rotation.Z + rotation.Z;
float wx = rotation.W * x;
float wy = rotation.W * y;
float wz = rotation.W * z;
float xx = rotation.X * x;
float xy = rotation.X * y;
float xz = rotation.X * z;
float yy = rotation.Y * y;
float yz = rotation.Y * z;
float zz = rotation.Z * z;
result = new Vector4(vector.X * (1.0f - yy - zz) + vector.Y * (xy - wz) + vector.Z * (xz + wy),
vector.X * (xy + wz) + vector.Y * (1.0f - xx - zz) + vector.Z * (yz - wx),
vector.X * (xz - wy) + vector.Y * (yz + wx) + vector.Z * (1.0f - xx - yy),
vector.W);
}
///
/// Transforms a 4D vector by the given rotation.
///
/// The vector to rotate.
/// The rotation to apply.
/// The transformed .
public static Vector4 Transform(Vector4 vector, Quaternion rotation)
{
Transform(ref vector, ref rotation, out Vector4 result);
return result;
}
///
/// Transforms a 4D vector by the given .
///
/// The source vector.
/// The transformation .
/// When the method completes, contains the transformed .
public static void Transform(ref Vector4 vector, ref Matrix transform, out Vector4 result)
{
result = new Vector4(vector.X * transform.M11 + vector.Y * transform.M21 + vector.Z * transform.M31 + vector.W * transform.M41,
vector.X * transform.M12 + vector.Y * transform.M22 + vector.Z * transform.M32 + vector.W * transform.M42,
vector.X * transform.M13 + vector.Y * transform.M23 + vector.Z * transform.M33 + vector.W * transform.M43,
vector.X * transform.M14 + vector.Y * transform.M24 + vector.Z * transform.M34 + vector.W * transform.M44);
}
///
/// Transforms a 4D vector by the given .
///
/// The source vector.
/// The transformation .
/// The transformed .
public static Vector4 Transform(Vector4 vector, Matrix transform)
{
Transform(ref vector, ref transform, out Vector4 result);
return result;
}
///
/// Adds two vectors.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Vector4 operator +(Vector4 left, Vector4 right)
{
return new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
}
///
/// Multiplies a vector with another by performing component-wise multiplication equivalent to .
///
/// The first vector to multiply.
/// The second vector to multiply.
/// The multiplication of the two vectors.
public static Vector4 operator *(Vector4 left, Vector4 right)
{
return new Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
}
///
/// Assert a vector (return it unchanged).
///
/// The vector to assert (unchanged).
/// The asserted (unchanged) vector.
public static Vector4 operator +(Vector4 value)
{
return value;
}
///
/// Subtracts two vectors.
///
/// The first vector to subtract.
/// The second vector to subtract.
/// The difference of the two vectors.
public static Vector4 operator -(Vector4 left, Vector4 right)
{
return new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
}
///
/// Reverses the direction of a given vector.
///
/// The vector to negate.
/// A vector facing in the opposite direction.
public static Vector4 operator -(Vector4 value)
{
return new Vector4(-value.X, -value.Y, -value.Z, -value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 operator *(Real scale, Vector4 value)
{
return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 operator *(Vector4 value, Real scale)
{
return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 operator /(Vector4 value, Real scale)
{
return new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
}
///
/// Scales a vector by the given value.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// The scaled vector.
public static Vector4 operator /(Real scale, Vector4 value)
{
return new Vector4(scale / value.X, scale / value.Y, scale / value.Z, scale / value.W);
}
///
/// Scales a vector by the given value.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The scaled vector.
public static Vector4 operator /(Vector4 value, Vector4 scale)
{
return new Vector4(value.X / scale.X, value.Y / scale.Y, value.Z / scale.Z, value.W / scale.W);
}
///
/// Remainder of value divided by scale.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The remained vector.
public static Vector4 operator %(Vector4 value, Real scale)
{
return new Vector4(value.X % scale, value.Y % scale, value.Z % scale, value.W % scale);
}
///
/// Remainder of value divided by scale.
///
/// The amount by which to scale the vector.
/// The vector to scale.
/// The remained vector.
public static Vector4 operator %(Real value, Vector4 scale)
{
return new Vector4(value % scale.X, value % scale.Y, value % scale.Z, value % scale.W);
}
///
/// Remainder of value divided by scale.
///
/// The vector to scale.
/// The amount by which to scale the vector.
/// The remained vector.
public static Vector4 operator %(Vector4 value, Vector4 scale)
{
return new Vector4(value.X % scale.X, value.Y % scale.Y, value.Z % scale.Z, value.W % scale.W);
}
///
/// Performs a component-wise addition.
///
/// The input vector.
/// The scalar value to be added on elements
/// The vector with added scalar for each element.
public static Vector4 operator +(Vector4 value, Real scalar)
{
return new Vector4(value.X + scalar, value.Y + scalar, value.Z + scalar, value.W + scalar);
}
///
/// Performs a component-wise addition.
///
/// The input vector.
/// The scalar value to be added on elements
/// The vector with added scalar for each element.
public static Vector4 operator +(Real scalar, Vector4 value)
{
return new Vector4(scalar + value.X, scalar + value.Y, scalar + value.Z, scalar + value.W);
}
///
/// Performs a component-wise subtraction.
///
/// The input vector.
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar from each element.
public static Vector4 operator -(Vector4 value, Real scalar)
{
return new Vector4(value.X - scalar, value.Y - scalar, value.Z - scalar, value.W - scalar);
}
///
/// Performs a component-wise subtraction.
///
/// The input vector.
/// The scalar value to be subtracted from elements
/// The vector with subtracted scalar from each element.
public static Vector4 operator -(Real scalar, Vector4 value)
{
return new Vector4(scalar - value.X, scalar - value.Y, scalar - value.Z, scalar - value.W);
}
///
/// Adds a vector to another by performing component-wise addition.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Float4 operator +(Float4 left, Vector4 right)
{
return new Float4(left.X + (float)right.X, left.Y + (float)right.Y, left.Z + (float)right.Z, left.W + (float)right.W);
}
///
/// Subtracts a vector from another by performing component-wise subtraction.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Float4 operator -(Float4 left, Vector4 right)
{
return new Float4(left.X - (float)right.X, left.Y - (float)right.Y, left.Z - (float)right.Z, left.W - (float)right.W);
}
///
/// Multiplies 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.
public static Float4 operator *(Float4 left, Vector4 right)
{
return new Float4(left.X * (float)right.X, left.Y * (float)right.Y, left.Z * (float)right.Z, left.W * (float)right.W);
}
///
/// Adds a vector to another by performing component-wise addition.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Vector4 operator +(Vector4 left, Float4 right)
{
return new Vector4(left.X + (Real)right.X, left.Y + (Real)right.Y, left.Z + (Real)right.Z, left.W + (Real)right.W);
}
///
/// Subtracts a vector from another by performing component-wise subtraction.
///
/// The first vector to add.
/// The second vector to add.
/// The sum of the two vectors.
public static Vector4 operator -(Vector4 left, Float4 right)
{
return new Vector4(left.X - (Real)right.X, left.Y - (Real)right.Y, left.Z - (Real)right.Z, left.W - (Real)right.W);
}
///
/// Multiplies 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.
public static Vector4 operator *(Vector4 left, Float4 right)
{
return new Vector4(left.X * (Real)right.X, left.Y * (Real)right.Y, left.Z * (Real)right.Z, left.W * (Real)right.W);
}
///
/// 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector4 left, Vector4 right)
{
return Mathr.NearEqual(left.X, right.X) && Mathr.NearEqual(left.Y, right.Y) && Mathr.NearEqual(left.Z, right.Z) && Mathr.NearEqual(left.W, right.W);
}
///
/// 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector4 left, Vector4 right)
{
return !left.Equals(ref right);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Float4(Vector4 value)
{
return new Float4((float)value.X, (float)value.Y, (float)value.Z, (float)value.W);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static implicit operator Double4(Vector4 value)
{
return new Double4(value.X, value.Y, value.Z, value.W);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector2(Vector4 value)
{
return new Vector2(value.X, value.Y);
}
///
/// Performs an explicit conversion from to .
///
/// The value.
/// The result of the conversion.
public static explicit operator Vector3(Vector4 value)
{
return new Vector3(value.X, value.Y, value.Z);
}
///
/// Returns a that represents this instance.
///
/// A that represents this instance.
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, _formatString, X, Y, Z, W);
}
///
/// 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, _formatString, X.ToString(format, CultureInfo.CurrentCulture), Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture), W.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, _formatString, X, Y, Z, W);
}
///
/// 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)
return ToString(formatProvider);
return string.Format(formatProvider, "X:{0} Y:{1} Z:{2} W:{3}", X.ToString(format, formatProvider), Y.ToString(format, formatProvider), Z.ToString(format, formatProvider), W.ToString(format, formatProvider));
}
///
/// Returns a hash code for this instance.
///
public override int GetHashCode()
{
unchecked
{
int hashCode = X.GetHashCode();
hashCode = (hashCode * 397) ^ Y.GetHashCode();
hashCode = (hashCode * 397) ^ Z.GetHashCode();
hashCode = (hashCode * 397) ^ W.GetHashCode();
return hashCode;
}
}
///
/// 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(ref Vector4 other)
{
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
///
/// 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Vector4 other)
{
return Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
///
/// 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)
{
return value is Vector4 other && Mathr.NearEqual(other.X, X) && Mathr.NearEqual(other.Y, Y) && Mathr.NearEqual(other.Z, Z) && Mathr.NearEqual(other.W, W);
}
}
}