#region --- License ---
/*
Copyright (c) 2006 - 2008 The Open Toolkit library.
Copyright 2013 Xamarin Inc
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#endregion
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Xml.Serialization;
using Robust.Shared.Utility;
namespace Robust.Shared.Maths
{
///
/// Represents a 3D vector using three single-precision floating-point numbers.
///
///
/// The Vector3 structure is suitable for interoperation with unmanaged code requiring three consecutive floats.
///
[Serializable]
[StructLayout(LayoutKind.Sequential)]
public struct Vector3 :
IEquatable,
ISpanFormattable,
IAdditionOperators,
ISubtractionOperators,
IMultiplyOperators,
IMultiplyOperators,
IComparisonOperators
{
#region Fields
///
/// The X component of the Vector3.
///
public float X;
///
/// The Y component of the Vector3.
///
public float Y;
///
/// The Z component of the Vector3.
///
public float Z;
#endregion
#region Constructors
///
/// Constructs a new instance.
///
/// The value that will initialize this instance.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3(float value)
{
X = value;
Y = value;
Z = value;
}
///
/// Constructs a new Vector3.
///
/// The x component of the Vector3.
/// The y component of the Vector3.
/// The z component of the Vector3.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
///
/// Constructs a new Vector3 from the given Vector2.
///
/// The Vector2 to copy components from.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3(Vector2 v)
{
X = v.X;
Y = v.Y;
Z = 0.0f;
}
///
/// Constructs a new Vector3 from the given Vector3.
///
/// The Vector3 to copy components from.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3(Vector3 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
///
/// Constructs a new Vector3 from the given Vector4.
///
/// The Vector4 to copy components from.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector3(Vector4 v)
{
X = v.X;
Y = v.Y;
Z = v.Z;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Deconstruct(out float x, out float y, out float z)
{
x = X;
y = Y;
z = Z;
}
#endregion
#region Public Members
#region Instance
#region public float Length
///
/// Gets the length (magnitude) of the vector.
///
///
public float Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => MathF.Sqrt(LengthSquared);
}
#endregion
#region public float LengthSquared
///
/// Gets the square of the vector length (magnitude).
///
///
/// This property avoids the costly square root operation required by the Length property. This makes it more suitable
/// for comparisons.
///
///
public float LengthSquared
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => X * X + Y * Y + Z * Z;
}
#endregion
#region public void Normalize()
///
/// Scales the Vector3 to unit length.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Normalize()
{
var scale = 1.0f / Length;
X *= scale;
Y *= scale;
Z *= scale;
}
#endregion
#endregion
#region Static
#region Fields
///
/// Defines a unit-length Vector3 that points towards the X-axis.
///
public static readonly Vector3 UnitX = new(1, 0, 0);
///
/// Defines a unit-length Vector3 that points towards the Y-axis.
///
public static readonly Vector3 UnitY = new(0, 1, 0);
///
/// /// Defines a unit-length Vector3 that points towards the Z-axis.
///
public static readonly Vector3 UnitZ = new(0, 0, 1);
///
/// Defines a zero-length Vector3.
///
public static readonly Vector3 Zero = new(0, 0, 0);
///
/// Defines an instance with all components set to 1.
///
public static readonly Vector3 One = new(1, 1, 1);
///
/// Defines the size of the Vector3 struct in bytes.
///
public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3());
#endregion
#region Add
///
/// Adds two vectors.
///
/// Left operand.
/// Right operand.
/// Result of operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Add(Vector3 a, Vector3 b)
{
Add(ref a, ref b, out a);
return a;
}
///
/// Adds two vectors.
///
/// Left operand.
/// Right operand.
/// Result of operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(ref Vector3 a, ref Vector3 b, out Vector3 result)
{
result = new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
}
#endregion
#region Subtract
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Subtract(Vector3 a, Vector3 b)
{
Subtract(ref a, ref b, out a);
return a;
}
///
/// Subtract one Vector from another
///
/// First operand
/// Second operand
/// Result of subtraction
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Subtract(ref Vector3 a, ref Vector3 b, out Vector3 result)
{
result = new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
}
#endregion
#region Multiply
///
/// Multiplies a vector by a scalar.
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Multiply(Vector3 vector, float scale)
{
Multiply(ref vector, scale, out vector);
return vector;
}
///
/// Multiplies a vector by a scalar.
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Multiply(ref Vector3 vector, float scale, out Vector3 result)
{
result = new Vector3(vector.X * scale, vector.Y * scale, vector.Z * scale);
}
///
/// Multiplies a vector by the components a vector (scale).
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Multiply(Vector3 vector, Vector3 scale)
{
Multiply(ref vector, ref scale, out vector);
return vector;
}
///
/// Multiplies a vector by the components of a vector (scale).
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Multiply(ref Vector3 vector, ref Vector3 scale, out Vector3 result)
{
result = new Vector3(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z);
}
#endregion
#region Divide
///
/// Divides a vector by a scalar.
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Divide(Vector3 vector, float scale)
{
Divide(ref vector, scale, out vector);
return vector;
}
///
/// Divides a vector by a scalar.
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Divide(ref Vector3 vector, float scale, out Vector3 result)
{
Multiply(ref vector, 1 / scale, out result);
}
///
/// Divides a vector by the components of a vector (scale).
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Divide(Vector3 vector, Vector3 scale)
{
Divide(ref vector, ref scale, out vector);
return vector;
}
///
/// Divide a vector by the components of a vector (scale).
///
/// Left operand.
/// Right operand.
/// Result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Divide(ref Vector3 vector, ref Vector3 scale, out Vector3 result)
{
result = new Vector3(vector.X / scale.X, vector.Y / scale.Y, vector.Z / scale.Z);
}
#endregion
#region ComponentMin
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ComponentMin(Vector3 a, Vector3 b)
{
a.X = MathF.Min(a.X, b.X);
a.Y = MathF.Min(a.Y, b.Y);
a.Z = MathF.Min(a.Z, b.Z);
return a;
}
///
/// Calculate the component-wise minimum of two vectors
///
/// First operand
/// Second operand
/// The component-wise minimum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ComponentMin(ref Vector3 a, ref Vector3 b, out Vector3 result)
{
result.X = MathF.Min(a.X, b.X);
result.Y = MathF.Min(a.Y, b.Y);
result.Z = MathF.Min(a.Z, b.Z);
}
#endregion
#region ComponentMax
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 ComponentMax(Vector3 a, Vector3 b)
{
a.X = MathF.Max(a.X, b.X);
a.Y = MathF.Max(a.Y, b.Y);
a.Z = MathF.Max(a.Z, b.Z);
return a;
}
///
/// Calculate the component-wise maximum of two vectors
///
/// First operand
/// Second operand
/// The component-wise maximum
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ComponentMax(ref Vector3 a, ref Vector3 b, out Vector3 result)
{
result.X = MathF.Max(a.X, b.X);
result.Y = MathF.Max(a.Y, b.Y);
result.Z = MathF.Max(a.Z, b.Z);
}
#endregion
#region Min
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Min(Vector3 left, Vector3 right)
{
return left.LengthSquared < right.LengthSquared ? left : right;
}
#endregion
#region Max
///
/// Returns the Vector3 with the minimum magnitude
///
/// Left operand
/// Right operand
/// The minimum Vector3
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Max(Vector3 left, Vector3 right)
{
return left.LengthSquared >= right.LengthSquared ? left : right;
}
#endregion
#region Clamp
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Clamp(Vector3 vec, Vector3 min, Vector3 max)
{
vec.X = MathHelper.Clamp(vec.X, min.X, max.X);
vec.Y = MathHelper.Clamp(vec.Y, min.Y, max.Y);
vec.Z = MathHelper.Clamp(vec.Z, min.Z, max.Z);
return vec;
}
///
/// Clamp a vector to the given minimum and maximum vectors
///
/// Input vector
/// Minimum vector
/// Maximum vector
/// The clamped vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Clamp(ref Vector3 vec, ref Vector3 min, ref Vector3 max, out Vector3 result)
{
result.X = MathHelper.Clamp(vec.X, min.X, max.X);
result.Y = MathHelper.Clamp(vec.Y, min.Y, max.Y);
result.Z = MathHelper.Clamp(vec.Z, min.Z, max.Z);
}
#endregion
#region Normalize
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Normalize(Vector3 vec)
{
var scale = 1.0f / vec.Length;
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
return vec;
}
///
/// Scale a vector to unit length
///
/// The input vector
/// The normalized vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Normalize(ref Vector3 vec, out Vector3 result)
{
var scale = 1.0f / vec.Length;
result.X = vec.X * scale;
result.Y = vec.Y * scale;
result.Z = vec.Z * scale;
}
#endregion
#region Dot
///
/// Calculate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float Dot(Vector3 left, Vector3 right)
{
return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
}
///
/// Calculate the dot (scalar) product of two vectors
///
/// First operand
/// Second operand
/// The dot product of the two inputs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Dot(ref Vector3 left, ref Vector3 right, out float result)
{
result = left.X * right.X + left.Y * right.Y + left.Z * right.Z;
}
#endregion
#region Cross
///
/// Caclulate the cross (vector) product of two vectors
///
/// First operand
/// Second operand
/// The cross product of the two inputs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Cross(Vector3 left, Vector3 right)
{
Cross(ref left, ref right, out var result);
return result;
}
///
/// Caclulate the cross (vector) product of two vectors
///
/// First operand
/// Second operand
/// The cross product of the two inputs
/// The cross product of the two inputs
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Cross(ref Vector3 left, ref Vector3 right, out Vector3 result)
{
result = new Vector3(left.Y * right.Z - left.Z * right.Y,
left.Z * right.X - left.X * right.Z,
left.X * right.Y - left.Y * right.X);
}
#endregion
#region Lerp
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Lerp(Vector3 a, Vector3 b, float blend)
{
a.X = MathHelper.Lerp(a.X, b.X, blend);
a.Y = MathHelper.Lerp(a.Y, b.Y, blend);
a.Z = MathHelper.Lerp(a.Z, b.Z, blend);
return a;
}
///
/// Returns a new Vector that is the linear blend of the 2 given Vectors
///
/// First input vector
/// Second input vector
/// The blend factor. a when blend=0, b when blend=1.
/// a when blend=0, b when blend=1, and a linear combination otherwise
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Lerp(ref Vector3 a, ref Vector3 b, float blend, out Vector3 result)
{
result.X = MathHelper.Lerp(a.X, b.X, blend);
result.Y = MathHelper.Lerp(a.Y, b.Y, blend);
result.Z = MathHelper.Lerp(a.Z, b.Z, blend);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 InterpolateCubic(Vector3 preA, Vector3 a, Vector3 b, Vector3 postB, float t)
{
return a +
(b - preA + (preA * 2.0f - a * 5.0f + b * 4.0f - postB + ((a - b) * 3.0f + postB - preA) * t) * t) *
t * 0.5f;
}
#endregion
#region Barycentric
///
/// Interpolate 3 Vectors using Barycentric coordinates
///
/// First input Vector
/// Second input Vector
/// Third input Vector
/// First Barycentric Coordinate
/// Second Barycentric Coordinate
/// a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 BaryCentric(Vector3 a, Vector3 b, Vector3 c, float u, float v)
{
return a + u * (b - a) + v * (c - a);
}
/// Interpolate 3 Vectors using Barycentric coordinates
/// First input Vector.
/// Second input Vector.
/// Third input Vector.
/// First Barycentric Coordinate.
/// Second Barycentric Coordinate.
/// Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void BaryCentric(ref Vector3 a, ref Vector3 b, ref Vector3 c, float u, float v, out Vector3 result)
{
result = a; // copy
var temp = b; // copy
Subtract(ref temp, ref a, out temp);
Multiply(ref temp, u, out temp);
Add(ref result, ref temp, out result);
temp = c; // copy
Subtract(ref temp, ref a, out temp);
Multiply(ref temp, v, out temp);
Add(ref result, ref temp, out result);
}
#endregion
#region Transform
/// Transform a direction vector by the given Matrix
/// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 TransformVector(Vector3 vec, Matrix4 mat)
{
Vector3 v;
v.X = Dot(vec, new Vector3(mat.Column0));
v.Y = Dot(vec, new Vector3(mat.Column1));
v.Z = Dot(vec, new Vector3(mat.Column2));
return v;
}
/// Transform a direction vector by the given Matrix
/// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
///
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformVector(ref Vector3 vec, ref Matrix4 mat, out Vector3 result)
{
result.X = vec.X * mat.Row0.X +
vec.Y * mat.Row1.X +
vec.Z * mat.Row2.X;
result.Y = vec.X * mat.Row0.Y +
vec.Y * mat.Row1.Y +
vec.Z * mat.Row2.Y;
result.Z = vec.X * mat.Row0.Z +
vec.Y * mat.Row1.Z +
vec.Z * mat.Row2.Z;
}
/// Transform a Normal by the given Matrix
///
/// This calculates the inverse of the given matrix, use TransformNormalInverse if you
/// already have the inverse to avoid this extra calculation
///
/// The normal to transform
/// The desired transformation
/// The transformed normal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 TransformNormal(Vector3 norm, Matrix4 mat)
{
mat.Invert();
return TransformNormalInverse(norm, mat);
}
/// Transform a Normal by the given Matrix
///
/// This calculates the inverse of the given matrix, use TransformNormalInverse if you
/// already have the inverse to avoid this extra calculation
///
/// The normal to transform
/// The desired transformation
/// The transformed normal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformNormal(ref Vector3 norm, ref Matrix4 mat, out Vector3 result)
{
var inverse = Matrix4.Invert(mat);
TransformNormalInverse(ref norm, ref inverse, out result);
}
/// Transform a Normal by the (transpose of the) given Matrix
///
/// This version doesn't calculate the inverse matrix.
/// Use this version if you already have the inverse of the desired transform to hand
///
/// The normal to transform
/// The inverse of the desired transformation
/// The transformed normal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 TransformNormalInverse(Vector3 norm, Matrix4 invMat)
{
Vector3 n;
n.X = Dot(norm, new Vector3(invMat.Row0));
n.Y = Dot(norm, new Vector3(invMat.Row1));
n.Z = Dot(norm, new Vector3(invMat.Row2));
return n;
}
/// Transform a Normal by the (transpose of the) given Matrix
///
/// This version doesn't calculate the inverse matrix.
/// Use this version if you already have the inverse of the desired transform to hand
///
/// The normal to transform
/// The inverse of the desired transformation
/// The transformed normal
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformNormalInverse(ref Vector3 norm, ref Matrix4 invMat, out Vector3 result)
{
result.X = norm.X * invMat.Row0.X +
norm.Y * invMat.Row0.Y +
norm.Z * invMat.Row0.Z;
result.Y = norm.X * invMat.Row1.X +
norm.Y * invMat.Row1.Y +
norm.Z * invMat.Row1.Z;
result.Z = norm.X * invMat.Row2.X +
norm.Y * invMat.Row2.Y +
norm.Z * invMat.Row2.Z;
}
/// Transform a Position by the given Matrix
/// The position to transform
/// The desired transformation
/// The transformed position
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 TransformPosition(Vector3 pos, Matrix4 mat)
{
Vector3 p;
p.X = Dot(pos, new Vector3(mat.Column0)) + mat.Row3.X;
p.Y = Dot(pos, new Vector3(mat.Column1)) + mat.Row3.Y;
p.Z = Dot(pos, new Vector3(mat.Column2)) + mat.Row3.Z;
return p;
}
/// Transform a Position by the given Matrix
/// The position to transform
/// The desired transformation
/// The transformed position
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformPosition(ref Vector3 pos, ref Matrix4 mat, out Vector3 result)
{
result.X = pos.X * mat.Row0.X +
pos.Y * mat.Row1.X +
pos.Z * mat.Row2.X +
mat.Row3.X;
result.Y = pos.X * mat.Row0.Y +
pos.Y * mat.Row1.Y +
pos.Z * mat.Row2.Y +
mat.Row3.Y;
result.Z = pos.X * mat.Row0.Z +
pos.Y * mat.Row1.Z +
pos.Z * mat.Row2.Z +
mat.Row3.Z;
}
/// Transform a Vector by the given Matrix
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Transform(Vector3 vec, Matrix4 mat)
{
Transform(ref vec, ref mat, out Vector3 result);
return result;
}
/// Transform a Vector by the given Matrix
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Transform(ref Vector3 vec, ref Matrix4 mat, out Vector4 result)
{
var v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f);
Vector4.Transform(ref v4, ref mat, out result);
}
/// Transform a Vector by the given Matrix
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Transform(ref Vector3 vec, ref Matrix4 mat, out Vector3 result)
{
var v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0f);
Vector4.Transform(ref v4, ref mat, out v4);
result = v4.Xyz;
}
///
/// Transforms a vector by a quaternion rotation.
///
/// The vector to transform.
/// The quaternion to rotate the vector by.
/// The result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 Transform(Vector3 vec, Quaternion quat)
{
Transform(ref vec, ref quat, out var result);
return result;
}
///
/// Transforms a vector by a quaternion rotation.
///
/// The vector to transform.
/// The quaternion to rotate the vector by.
/// The result of the operation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Transform(ref Vector3 vec, ref Quaternion quat, out Vector3 result)
{
// Since vec.W == 0, we can optimize quat * vec * quat^-1 as follows:
// vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec)
var xyz = quat.Xyz;
Cross(ref xyz, ref vec, out var temp);
Multiply(ref vec, quat.W, out var temp2);
Add(ref temp, ref temp2, out temp);
Cross(ref xyz, ref temp, out temp);
Multiply(ref temp, 2, out temp);
Add(ref vec, ref temp, out result);
}
/// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 TransformPerspective(Vector3 vec, Matrix4 mat)
{
TransformPerspective(ref vec, ref mat, out var result);
return result;
}
/// Transform a Vector3 by the given Matrix, and project the resulting Vector4 back to a Vector3
/// The vector to transform
/// The desired transformation
/// The transformed vector
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TransformPerspective(ref Vector3 vec, ref Matrix4 mat, out Vector3 result)
{
var v = new Vector4(vec, 1);
Vector4.Transform(ref v, ref mat, out v);
result.X = v.X / v.W;
result.Y = v.Y / v.W;
result.Z = v.Z / v.W;
}
#endregion
#region CalculateAngle
///
/// Calculates the angle (in radians) between two vectors.
///
/// The first vector.
/// The second vector.
/// Angle (in radians) between the vectors.
/// Note that the returned angle is never bigger than the constant Pi.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float CalculateAngle(Vector3 first, Vector3 second)
{
return MathF.Acos(Dot(first, second) / (first.Length * second.Length));
}
/// Calculates the angle (in radians) between two vectors.
/// The first vector.
/// The second vector.
/// Angle (in radians) between the vectors.
/// Note that the returned angle is never bigger than the constant Pi.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CalculateAngle(ref Vector3 first, ref Vector3 second, out float result)
{
Dot(ref first, ref second, out var temp);
result = MathF.Acos(temp / (first.Length * second.Length));
}
#endregion
#endregion
#region Swizzle
///
/// Gets or sets an OpenTK.Vector2 with the X and Y components of this instance.
///
[XmlIgnore]
public Vector2 Xy
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => new(X, Y);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
X = value.X;
Y = value.Y;
}
}
#endregion
#region Operators
///
/// Adds two instances.
///
/// The first instance.
/// The second instance.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator +(Vector3 left, Vector3 right)
{
left.X += right.X;
left.Y += right.Y;
left.Z += right.Z;
return left;
}
///
/// Subtracts two instances.
///
/// The first instance.
/// The second instance.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(Vector3 left, Vector3 right)
{
left.X -= right.X;
left.Y -= right.Y;
left.Z -= right.Z;
return left;
}
///
/// Negates an instance.
///
/// The instance.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator -(Vector3 vec)
{
vec.X = -vec.X;
vec.Y = -vec.Y;
vec.Z = -vec.Z;
return vec;
}
///
/// Multiplies an instance by a scalar.
///
/// The instance.
/// The scalar.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(Vector3 vec, float scale)
{
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
return vec;
}
///
/// Multiplies an instance by a scalar.
///
/// The scalar.
/// The instance.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(float scale, Vector3 vec)
{
vec.X *= scale;
vec.Y *= scale;
vec.Z *= scale;
return vec;
}
///
/// Component wise multiply two vectors.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator *(Vector3 a, Vector3 b)
{
return new(a.X * b.X, a.Y * b.Y, a.Z * b.Z);
}
///
/// Divides an instance by a scalar.
///
/// The instance.
/// The scalar.
/// The result of the calculation.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Vector3 operator /(Vector3 vec, float scale)
{
var mult = 1.0f / scale;
vec.X *= mult;
vec.Y *= mult;
vec.Z *= mult;
return vec;
}
///
/// Compares two instances for equality.
///
/// The first instance.
/// The second instance.
/// True, if left equals right; false otherwise.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Vector3 left, Vector3 right)
{
return left.Equals(right);
}
///
/// Compares two instances for inequality.
///
/// The first instance.
/// The second instance.
/// True, if left does not equa lright; false otherwise.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Vector3 left, Vector3 right)
{
return !left.Equals(right);
}
#endregion
#region Overrides
#region public override string ToString()
///
/// Returns a System.String that represents the current Vector3.
///
///
public readonly override string ToString()
{
return $"({X}, {Y}, {Z})";
}
public readonly string ToString(string? format, IFormatProvider? formatProvider)
{
return ToString();
}
public readonly bool TryFormat(
Span destination,
out int charsWritten,
ReadOnlySpan format,
IFormatProvider? provider)
{
return FormatHelpers.TryFormatInto(
destination,
out charsWritten,
$"({X}, {Y}, {Z})");
}
#endregion
#region public override int GetHashCode()
///
/// Returns the hashcode for this instance.
///
/// A System.Int32 containing the unique hashcode for this instance.
public override int GetHashCode()
{
return X.GetHashCode() ^ Y.GetHashCode() ^ Z.GetHashCode();
}
#endregion
#region public override bool Equals(object obj)
///
/// Indicates whether this instance and a specified object are equal.
///
/// The object to compare to.
/// True if the instances are equal; false otherwise.
public override bool Equals(object? obj)
{
if (!(obj is Vector3))
return false;
return Equals((Vector3) obj);
}
#endregion
#endregion
#endregion
#region IEquatable Members
/// Indicates whether the current vector is equal to another vector.
/// A vector to compare with this vector.
/// true if the current vector is equal to the vector parameter; otherwise, false.
public bool Equals(Vector3 other)
{
return
X == other.X &&
Y == other.Y &&
Z == other.Z;
}
#endregion
public static bool operator >(Vector3 left, Vector3 right)
{
return left.LengthSquared > right.LengthSquared;
}
public static bool operator >=(Vector3 left, Vector3 right)
{
return left.LengthSquared >= right.LengthSquared;
}
public static bool operator <(Vector3 left, Vector3 right)
{
return left.LengthSquared < right.LengthSquared;
}
public static bool operator <=(Vector3 left, Vector3 right)
{
return left.LengthSquared <= right.LengthSquared;
}
}
}