#region License /* MoonWorks - Game Development Framework * Copyright 2022 Evan Hemsley */ /* Derived from code by Ethan Lee (Copyright 2009-2021). * Released under the Microsoft Public License. * See fna.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.Diagnostics; using System.Runtime.InteropServices; using System.Text; #endregion namespace MoonWorks.Math.Fixed { /// /// Describes a fixed point 3D-vector. /// [Serializable] [DebuggerDisplay("{DebugDisplayString,nq}")] [StructLayout(LayoutKind.Explicit)] 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(0, 0, 0); // Not readonly for performance -flibit private static readonly Vector3 one = new Vector3(1, 1, 1); private static readonly Vector3 unitX = new Vector3(1, 0, 0); private static readonly Vector3 unitY = new Vector3(0, 1, 0); private static readonly Vector3 unitZ = new Vector3(0, 0, 1); private static readonly Vector3 up = new Vector3(0, 1, 0); private static readonly Vector3 down = new Vector3(0, -1, 0); private static readonly Vector3 right = new Vector3(1, 0, 0); private static readonly Vector3 left = new Vector3(-1, 0, 0); private static readonly Vector3 forward = new Vector3(0, 0, -1); private static readonly Vector3 backward = new Vector3(0, 0, 1); #endregion #region Public Fields /// /// The x coordinate of this . /// [FieldOffset(0)] public Fix64 X; /// /// The y coordinate of this . /// [FieldOffset(8)] public Fix64 Y; /// /// The z coordinate of this . /// [FieldOffset(16)] public Fix64 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(Fix64 x, Fix64 y, Fix64 z) { this.X = x; this.Y = y; this.Z = z; } public Vector3(int x, int y, int z) { this.X = new Fix64(x); this.Y = new Fix64(y); this.Z = new Fix64(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(Fix64 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, Fix64 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) { return (obj is Vector3) && Equals((Vector3) obj); } /// /// 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 (X == other.X && Y == other.Y && Z == other.Z); } /// /// Gets the hash code of this . /// /// Hash code of this . public override int GetHashCode() { return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode(); } /// /// Returns the length of this . /// /// The length of this . public Fix64 Length() { return Fix64.Sqrt((X * X) + (Y * Y) + (Z * Z)); } /// /// Returns the squared length of this . /// /// The squared length of this . public Fix64 LengthSquared() { return (X * X) + (Y * Y) + (Z * Z); } /// /// Turns this to a unit vector with the same direction. /// public void Normalize() { Fix64 factor = Fix64.One / Fix64.Sqrt( (X * X) + (Y * Y) + (Z * Z) ); X *= factor; Y *= factor; Z *= factor; } /// /// 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; } /// /// 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( Fix64.Clamp(value1.X, min.X, max.X), Fix64.Clamp(value1.Y, min.Y, max.Y), Fix64.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 = Fix64.Clamp(value1.X, min.X, max.X); result.Y = Fix64.Clamp(value1.Y, min.Y, max.Y); result.Z = Fix64.Clamp(value1.Z, min.Z, max.Z); } /// /// Clamps the magnitude of the specified vector. /// /// The vector to clamp. /// The maximum length of the vector. /// public static Vector3 ClampMagnitude( Vector3 value, Fix64 maxLength ) { return (value.LengthSquared() > maxLength * maxLength) ? (Vector3.Normalize(value) * maxLength) : value; } /// /// 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) { Fix64 x = vector1.Y * vector2.Z - vector2.Y * vector1.Z; Fix64 y = -(vector1.X * vector2.Z - vector2.X * vector1.Z); Fix64 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 Fix64 Distance(Vector3 vector1, Vector3 vector2) { Fix64 result; DistanceSquared(ref vector1, ref vector2, out result); return Fix64.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 Fix64 result) { DistanceSquared(ref value1, ref value2, out result); result = Fix64.Sqrt(result); } /// /// Returns the squared distance between two vectors. /// /// The first vector. /// The second vector. /// The squared distance between two vectors. public static Fix64 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 Fix64 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, Fix64 value2) { Fix64 factor = Fix64.One / 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, Fix64 value2, out Vector3 result) { Fix64 factor = Fix64.One / 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 Fix64 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 Fix64 result) { result = ( (vector1.X * vector2.X) + (vector1.Y * vector2.Y) + (vector1.Z * vector2.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. public static Vector3 Max(Vector3 value1, Vector3 value2) { return new Vector3( Fix64.Max(value1.X, value2.X), Fix64.Max(value1.Y, value2.Y), Fix64.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 = Fix64.Max(value1.X, value2.X); result.Y = Fix64.Max(value1.Y, value2.Y); result.Z = Fix64.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( Fix64.Min(value1.X, value2.X), Fix64.Min(value1.Y, value2.Y), Fix64.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 = Fix64.Min(value1.X, value2.X); result.Y = Fix64.Min(value1.Y, value2.Y); result.Z = Fix64.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, Fix64 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, Fix64 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) { Fix64 factor = Fix64.One / Fix64.Sqrt( (value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z) ); return new Vector3( value.X * factor, value.Y * factor, 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; Fix64 two = new Fix64(2); // Inline the dotProduct here instead of calling method Fix64 dotProduct = ((vector.X * normal.X) + (vector.Y * normal.Y)) + (vector.Z * normal.Z); reflectedVector.X = vector.X - (two * normal.X) * dotProduct; reflectedVector.Y = vector.Y - (two * normal.Y) * dotProduct; reflectedVector.Z = vector.Z - (two * normal.Z) * dotProduct; return reflectedVector; } /// /// 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, Matrix4x4 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 Matrix4x4 matrix, out Vector3 result ) { Fix64 x = ( (position.X * matrix.M11) + (position.Y * matrix.M21) + (position.Z * matrix.M31) + matrix.M41 ); Fix64 y = ( (position.X * matrix.M12) + (position.Y * matrix.M22) + (position.Z * matrix.M32) + matrix.M42 ); Fix64 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 Matrix4x4 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 Matrix4x4 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 ) { Fix64 two = new Fix64(2); Fix64 x = two * (rotation.Y * value.Z - rotation.Z * value.Y); Fix64 y = two * (rotation.Z * value.X - rotation.X * value.Z); Fix64 z = two * (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); } /// /// /// 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, Matrix4x4 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 Matrix4x4 matrix, out Vector3 result ) { Fix64 x = (normal.X * matrix.M11) + (normal.Y * matrix.M21) + (normal.Z * matrix.M31); Fix64 y = (normal.X * matrix.M12) + (normal.Y * matrix.M22) + (normal.Z * matrix.M32); Fix64 z = (normal.X * matrix.M13) + (normal.Y * matrix.M23) + (normal.Z * matrix.M33); result.X = x; result.Y = y; result.Z = z; } #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 (value1.X == value2.X && value1.Y == value2.Y && 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, Fix64 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 *(Fix64 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, Fix64 divider) { Fix64 factor = Fix64.One / divider; value.X *= factor; value.Y *= factor; value.Z *= factor; return value; } #endregion } }