<Compile Include="System\Numerics\Matrix4x4.cs" />
<Compile Include="System\Numerics\Plane.cs" />
<Compile Include="System\Numerics\Quaternion.cs" />
- <Compile Include="System\Numerics\Vector2.cs" />
- <Compile Include="System\Numerics\Vector2_Intrinsics.cs" />
- <Compile Include="System\Numerics\Vector3.cs" />
- <Compile Include="System\Numerics\Vector3_Intrinsics.cs" />
- <Compile Include="System\Numerics\Vector4.cs" />
- <Compile Include="System\Numerics\Vector4_Intrinsics.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector2.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector2_Intrinsics.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector3.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector3_Intrinsics.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector4.cs" />
+ <Compile Include="$(CommonPath)\CoreLib\System\Numerics\Vector4_Intrinsics.cs" />
</ItemGroup>
<ItemGroup Condition="'$(HasIntrinsics)' == 'true'">
<Compile Include="System\Numerics\VectorMath.cs" />
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Globalization;
-using System.Runtime.CompilerServices;
-using System.Text;
-
-namespace System.Numerics
-{
- /// <summary>
- /// A structure encapsulating two single precision floating point values and provides hardware accelerated methods.
- /// </summary>
- public partial struct Vector2 : IEquatable<Vector2>, IFormattable
- {
- #region Public Static Properties
- /// <summary>
- /// Returns the vector (0,0).
- /// </summary>
- public static Vector2 Zero
- {
- [Intrinsic]
- get
- {
- return new Vector2();
- }
- }
- /// <summary>
- /// Returns the vector (1,1).
- /// </summary>
- public static Vector2 One
- {
- [Intrinsic]
- get
- {
- return new Vector2(1.0f, 1.0f);
- }
- }
- /// <summary>
- /// Returns the vector (1,0).
- /// </summary>
- public static Vector2 UnitX { get { return new Vector2(1.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,1).
- /// </summary>
- public static Vector2 UnitY { get { return new Vector2(0.0f, 1.0f); } }
- #endregion Public Static Properties
-
- #region Public instance methods
- /// <summary>
- /// Returns the hash code for this instance.
- /// </summary>
- /// <returns>The hash code.</returns>
- public override readonly int GetHashCode()
- {
- return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode());
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Object is equal to this Vector2 instance.
- /// </summary>
- /// <param name="obj">The Object to compare against.</param>
- /// <returns>True if the Object is equal to this Vector2; False otherwise.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override readonly bool Equals(object? obj)
- {
- if (!(obj is Vector2))
- return false;
- return Equals((Vector2)obj);
- }
-
- /// <summary>
- /// Returns a String representing this Vector2 instance.
- /// </summary>
- /// <returns>The string representation.</returns>
- public override readonly string ToString()
- {
- return ToString("G", CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector2 instance, using the specified format to format individual elements.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format)
- {
- return ToString(format, CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector2 instance, using the specified format to format individual elements
- /// and the given IFormatProvider.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <param name="formatProvider">The format provider to use when formatting elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format, IFormatProvider? formatProvider)
- {
- StringBuilder sb = new StringBuilder();
- string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
- sb.Append('<');
- sb.Append(this.X.ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(this.Y.ToString(format, formatProvider));
- sb.Append('>');
- return sb.ToString();
- }
-
- /// <summary>
- /// Returns the length of the vector.
- /// </summary>
- /// <returns>The vector's length.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float Length()
- {
- if (Vector.IsHardwareAccelerated)
- {
- float ls = Vector2.Dot(this, this);
- return MathF.Sqrt(ls);
- }
- else
- {
- float ls = X * X + Y * Y;
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the length of the vector squared. This operation is cheaper than Length().
- /// </summary>
- /// <returns>The vector's length squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float LengthSquared()
- {
- if (Vector.IsHardwareAccelerated)
- {
- return Vector2.Dot(this, this);
- }
- else
- {
- return X * X + Y * Y;
- }
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the Euclidean distance between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Distance(Vector2 value1, Vector2 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector2 difference = value1 - value2;
- float ls = Vector2.Dot(difference, difference);
- return MathF.Sqrt(ls);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
-
- float ls = dx * dx + dy * dy;
-
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the Euclidean distance squared between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float DistanceSquared(Vector2 value1, Vector2 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector2 difference = value1 - value2;
- return Vector2.Dot(difference, difference);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
-
- return dx * dx + dy * dy;
- }
- }
-
- /// <summary>
- /// Returns a vector with the same direction as the given vector, but with a length of 1.
- /// </summary>
- /// <param name="value">The vector to normalize.</param>
- /// <returns>The normalized vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Normalize(Vector2 value)
- {
- if (Vector.IsHardwareAccelerated)
- {
- float length = value.Length();
- return value / length;
- }
- else
- {
- float ls = value.X * value.X + value.Y * value.Y;
- float invNorm = 1.0f / MathF.Sqrt(ls);
-
- return new Vector2(
- value.X * invNorm,
- value.Y * invNorm);
- }
- }
-
- /// <summary>
- /// Returns the reflection of a vector off a surface that has the specified normal.
- /// </summary>
- /// <param name="vector">The source vector.</param>
- /// <param name="normal">The normal of the surface being reflected off.</param>
- /// <returns>The reflected vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Reflect(Vector2 vector, Vector2 normal)
- {
- if (Vector.IsHardwareAccelerated)
- {
- float dot = Vector2.Dot(vector, normal);
- return vector - (2 * dot * normal);
- }
- else
- {
- float dot = vector.X * normal.X + vector.Y * normal.Y;
-
- return new Vector2(
- vector.X - 2.0f * dot * normal.X,
- vector.Y - 2.0f * dot * normal.Y);
- }
- }
-
- /// <summary>
- /// Restricts a vector between a min and max value.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="min">The minimum value.</param>
- /// <param name="max">The maximum value.</param>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Clamp(Vector2 value1, Vector2 min, Vector2 max)
- {
- // This compare order is very important!!!
- // We must follow HLSL behavior in the case user specified min value is bigger than max value.
- float x = value1.X;
- x = (min.X > x) ? min.X : x; // max(x, minx)
- x = (max.X < x) ? max.X : x; // min(x, maxx)
-
- float y = value1.Y;
- y = (min.Y > y) ? min.Y : y; // max(y, miny)
- y = (max.Y < y) ? max.Y : y; // min(y, maxy)
-
- return new Vector2(x, y);
- }
-
- /// <summary>
- /// Linearly interpolates between two vectors based on the given weighting.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <param name="amount">Value between 0 and 1 indicating the weight of the second source vector.</param>
- /// <returns>The interpolated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Lerp(Vector2 value1, Vector2 value2, float amount)
- {
- return new Vector2(
- value1.X + (value2.X - value1.X) * amount,
- value1.Y + (value2.Y - value1.Y) * amount);
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Transform(Vector2 position, Matrix3x2 matrix)
- {
- return new Vector2(
- position.X * matrix.M11 + position.Y * matrix.M21 + matrix.M31,
- position.X * matrix.M12 + position.Y * matrix.M22 + matrix.M32);
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Transform(Vector2 position, Matrix4x4 matrix)
- {
- return new Vector2(
- position.X * matrix.M11 + position.Y * matrix.M21 + matrix.M41,
- position.X * matrix.M12 + position.Y * matrix.M22 + matrix.M42);
- }
-
- /// <summary>
- /// Transforms a vector normal by the given matrix.
- /// </summary>
- /// <param name="normal">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 TransformNormal(Vector2 normal, Matrix3x2 matrix)
- {
- return new Vector2(
- normal.X * matrix.M11 + normal.Y * matrix.M21,
- normal.X * matrix.M12 + normal.Y * matrix.M22);
- }
-
- /// <summary>
- /// Transforms a vector normal by the given matrix.
- /// </summary>
- /// <param name="normal">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 TransformNormal(Vector2 normal, Matrix4x4 matrix)
- {
- return new Vector2(
- normal.X * matrix.M11 + normal.Y * matrix.M21,
- normal.X * matrix.M12 + normal.Y * matrix.M22);
- }
-
- /// <summary>
- /// Transforms a vector by the given Quaternion rotation value.
- /// </summary>
- /// <param name="value">The source vector to be rotated.</param>
- /// <param name="rotation">The rotation to apply.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Transform(Vector2 value, Quaternion rotation)
- {
- float x2 = rotation.X + rotation.X;
- float y2 = rotation.Y + rotation.Y;
- float z2 = rotation.Z + rotation.Z;
-
- float wz2 = rotation.W * z2;
- float xx2 = rotation.X * x2;
- float xy2 = rotation.X * y2;
- float yy2 = rotation.Y * y2;
- float zz2 = rotation.Z * z2;
-
- return new Vector2(
- value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2),
- value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2));
- }
- #endregion Public Static Methods
-
- #region Public operator methods
- // all the below methods should be inlined as they are
- // implemented over JIT intrinsics
-
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Add(Vector2 left, Vector2 right)
- {
- return left + right;
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Subtract(Vector2 left, Vector2 right)
- {
- return left - right;
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Multiply(Vector2 left, Vector2 right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Multiply(Vector2 left, float right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Multiply(float left, Vector2 right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Divide(Vector2 left, Vector2 right)
- {
- return left / right;
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="divisor">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Divide(Vector2 left, float divisor)
- {
- return left / divisor;
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Negate(Vector2 value)
- {
- return -value;
- }
- #endregion Public operator methods
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Runtime.CompilerServices;
-
-namespace System.Numerics
-{
- // This file contains the definitions for all of the JIT intrinsic methods and properties that are recognized by the current x64 JIT compiler.
- // The implementation defined here is used in any circumstance where the JIT fails to recognize these members as intrinsic.
- // The JIT recognizes these methods and properties by name and signature: if either is changed, the JIT will no longer recognize the member.
- // Some methods declared here are not strictly intrinsic, but delegate to an intrinsic method. For example, only one overload of CopyTo()
-
- public partial struct Vector2
- {
- /// <summary>
- /// The X component of the vector.
- /// </summary>
- public float X;
- /// <summary>
- /// The Y component of the vector.
- /// </summary>
- public float Y;
-
- #region Constructors
- /// <summary>
- /// Constructs a vector whose elements are all the single specified value.
- /// </summary>
- /// <param name="value">The element to fill the vector with.</param>
- [Intrinsic]
- public Vector2(float value) : this(value, value) { }
-
- /// <summary>
- /// Constructs a vector with the given individual elements.
- /// </summary>
- /// <param name="x">The X component.</param>
- /// <param name="y">The Y component.</param>
- [Intrinsic]
- public Vector2(float x, float y)
- {
- X = x;
- Y = y;
- }
- #endregion Constructors
-
- #region Public Instance Methods
- /// <summary>
- /// Copies the contents of the vector into the given array.
- /// </summary>
- /// <param name="array">The destination array.</param>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly void CopyTo(float[] array)
- {
- CopyTo(array, 0);
- }
-
- /// <summary>
- /// Copies the contents of the vector into the given array, starting from the given index.
- /// </summary>
- /// <exception cref="ArgumentNullException">If array is null.</exception>
- /// <exception cref="RankException">If array is multidimensional.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If index is greater than end of the array or index is less than zero.</exception>
- /// <exception cref="ArgumentException">If number of elements in source vector is greater than those available in destination array
- /// or if there are not enough elements to copy.</exception>
- [Intrinsic]
- public readonly void CopyTo(float[] array, int index)
- {
- if (array == null)
- {
- // Match the JIT's exception type here. For perf, a NullReference is thrown instead of an ArgumentNull.
- throw new NullReferenceException(SR.Arg_NullArgumentNullRef);
- }
- if (index < 0 || index >= array.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.Format(SR.Arg_ArgumentOutOfRangeException, index));
- }
- if ((array.Length - index) < 2)
- {
- throw new ArgumentException(SR.Format(SR.Arg_ElementsInSourceIsGreaterThanDestination, index));
- }
- array[index] = X;
- array[index + 1] = Y;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Vector2 is equal to this Vector2 instance.
- /// </summary>
- /// <param name="other">The Vector2 to compare this instance to.</param>
- /// <returns>True if the other Vector2 is equal to this instance; False otherwise.</returns>
- [Intrinsic]
- public readonly bool Equals(Vector2 other)
- {
- return this.X == other.X && this.Y == other.Y;
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the dot product of two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>The dot product.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Dot(Vector2 value1, Vector2 value2)
- {
- return value1.X * value2.X +
- value1.Y * value2.Y;
- }
-
- /// <summary>
- /// Returns a vector whose elements are the minimum of each of the pairs of elements in the two source vectors.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <returns>The minimized vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Min(Vector2 value1, Vector2 value2)
- {
- return new Vector2(
- (value1.X < value2.X) ? value1.X : value2.X,
- (value1.Y < value2.Y) ? value1.Y : value2.Y);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the maximum of each of the pairs of elements in the two source vectors
- /// </summary>
- /// <param name="value1">The first source vector</param>
- /// <param name="value2">The second source vector</param>
- /// <returns>The maximized vector</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Max(Vector2 value1, Vector2 value2)
- {
- return new Vector2(
- (value1.X > value2.X) ? value1.X : value2.X,
- (value1.Y > value2.Y) ? value1.Y : value2.Y);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the absolute values of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The absolute value vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 Abs(Vector2 value)
- {
- return new Vector2(MathF.Abs(value.X), MathF.Abs(value.Y));
- }
-
- /// <summary>
- /// Returns a vector whose elements are the square root of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The square root vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 SquareRoot(Vector2 value)
- {
- return new Vector2(MathF.Sqrt(value.X), MathF.Sqrt(value.Y));
- }
- #endregion Public Static Methods
-
- #region Public Static Operators
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator +(Vector2 left, Vector2 right)
- {
- return new Vector2(left.X + right.X, left.Y + right.Y);
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator -(Vector2 left, Vector2 right)
- {
- return new Vector2(left.X - right.X, left.Y - right.Y);
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator *(Vector2 left, Vector2 right)
- {
- return new Vector2(left.X * right.X, left.Y * right.Y);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator *(float left, Vector2 right)
- {
- return new Vector2(left, left) * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator *(Vector2 left, float right)
- {
- return left * new Vector2(right, right);
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator /(Vector2 left, Vector2 right)
- {
- return new Vector2(left.X / right.X, left.Y / right.Y);
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="value2">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator /(Vector2 value1, float value2)
- {
- return value1 / new Vector2(value2);
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector2 operator -(Vector2 value)
- {
- return Zero - value;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are equal; False otherwise.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator ==(Vector2 left, Vector2 right)
- {
- return left.Equals(right);
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are not equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are not equal; False if they are equal.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator !=(Vector2 left, Vector2 right)
- {
- return !(left == right);
- }
- #endregion Public Static Operators
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Globalization;
-using System.Runtime.CompilerServices;
-using System.Text;
-
-namespace System.Numerics
-{
- /// <summary>
- /// A structure encapsulating three single precision floating point values and provides hardware accelerated methods.
- /// </summary>
- public partial struct Vector3 : IEquatable<Vector3>, IFormattable
- {
- #region Public Static Properties
- /// <summary>
- /// Returns the vector (0,0,0).
- /// </summary>
- public static Vector3 Zero
- {
- [Intrinsic]
- get
- {
- return new Vector3();
- }
- }
- /// <summary>
- /// Returns the vector (1,1,1).
- /// </summary>
- public static Vector3 One
- {
- [Intrinsic]
- get
- {
- return new Vector3(1.0f, 1.0f, 1.0f);
- }
- }
- /// <summary>
- /// Returns the vector (1,0,0).
- /// </summary>
- public static Vector3 UnitX { get { return new Vector3(1.0f, 0.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,1,0).
- /// </summary>
- public static Vector3 UnitY { get { return new Vector3(0.0f, 1.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,0,1).
- /// </summary>
- public static Vector3 UnitZ { get { return new Vector3(0.0f, 0.0f, 1.0f); } }
- #endregion Public Static Properties
-
- #region Public Instance Methods
-
- /// <summary>
- /// Returns the hash code for this instance.
- /// </summary>
- /// <returns>The hash code.</returns>
- public override readonly int GetHashCode()
- {
- return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode());
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Object is equal to this Vector3 instance.
- /// </summary>
- /// <param name="obj">The Object to compare against.</param>
- /// <returns>True if the Object is equal to this Vector3; False otherwise.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override readonly bool Equals(object? obj)
- {
- if (!(obj is Vector3))
- return false;
- return Equals((Vector3)obj);
- }
-
- /// <summary>
- /// Returns a String representing this Vector3 instance.
- /// </summary>
- /// <returns>The string representation.</returns>
- public override readonly string ToString()
- {
- return ToString("G", CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector3 instance, using the specified format to format individual elements.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format)
- {
- return ToString(format, CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector3 instance, using the specified format to format individual elements
- /// and the given IFormatProvider.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <param name="formatProvider">The format provider to use when formatting elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format, IFormatProvider? formatProvider)
- {
- StringBuilder sb = new StringBuilder();
- string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
- sb.Append('<');
- sb.Append(((IFormattable)this.X).ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(((IFormattable)this.Y).ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(((IFormattable)this.Z).ToString(format, formatProvider));
- sb.Append('>');
- return sb.ToString();
- }
-
- /// <summary>
- /// Returns the length of the vector.
- /// </summary>
- /// <returns>The vector's length.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float Length()
- {
- if (Vector.IsHardwareAccelerated)
- {
- float ls = Vector3.Dot(this, this);
- return MathF.Sqrt(ls);
- }
- else
- {
- float ls = X * X + Y * Y + Z * Z;
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the length of the vector squared. This operation is cheaper than Length().
- /// </summary>
- /// <returns>The vector's length squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float LengthSquared()
- {
- if (Vector.IsHardwareAccelerated)
- {
- return Vector3.Dot(this, this);
- }
- else
- {
- return X * X + Y * Y + Z * Z;
- }
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the Euclidean distance between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Distance(Vector3 value1, Vector3 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector3 difference = value1 - value2;
- float ls = Vector3.Dot(difference, difference);
- return MathF.Sqrt(ls);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
- float dz = value1.Z - value2.Z;
-
- float ls = dx * dx + dy * dy + dz * dz;
-
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the Euclidean distance squared between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float DistanceSquared(Vector3 value1, Vector3 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector3 difference = value1 - value2;
- return Vector3.Dot(difference, difference);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
- float dz = value1.Z - value2.Z;
-
- return dx * dx + dy * dy + dz * dz;
- }
- }
-
- /// <summary>
- /// Returns a vector with the same direction as the given vector, but with a length of 1.
- /// </summary>
- /// <param name="value">The vector to normalize.</param>
- /// <returns>The normalized vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Normalize(Vector3 value)
- {
- if (Vector.IsHardwareAccelerated)
- {
- float length = value.Length();
- return value / length;
- }
- else
- {
- float ls = value.X * value.X + value.Y * value.Y + value.Z * value.Z;
- float length = MathF.Sqrt(ls);
- return new Vector3(value.X / length, value.Y / length, value.Z / length);
- }
- }
-
- /// <summary>
- /// Computes the cross product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>The cross product.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Cross(Vector3 vector1, Vector3 vector2)
- {
- return new Vector3(
- vector1.Y * vector2.Z - vector1.Z * vector2.Y,
- vector1.Z * vector2.X - vector1.X * vector2.Z,
- vector1.X * vector2.Y - vector1.Y * vector2.X);
- }
-
- /// <summary>
- /// Returns the reflection of a vector off a surface that has the specified normal.
- /// </summary>
- /// <param name="vector">The source vector.</param>
- /// <param name="normal">The normal of the surface being reflected off.</param>
- /// <returns>The reflected vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Reflect(Vector3 vector, Vector3 normal)
- {
- if (Vector.IsHardwareAccelerated)
- {
- float dot = Vector3.Dot(vector, normal);
- Vector3 temp = normal * dot * 2f;
- return vector - temp;
- }
- else
- {
- float dot = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z;
- float tempX = normal.X * dot * 2f;
- float tempY = normal.Y * dot * 2f;
- float tempZ = normal.Z * dot * 2f;
- return new Vector3(vector.X - tempX, vector.Y - tempY, vector.Z - tempZ);
- }
- }
-
- /// <summary>
- /// Restricts a vector between a min and max value.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="min">The minimum value.</param>
- /// <param name="max">The maximum value.</param>
- /// <returns>The restricted vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 max)
- {
- // This compare order is very important!!!
- // We must follow HLSL behavior in the case user specified min value is bigger than max value.
- float x = value1.X;
- x = (min.X > x) ? min.X : x; // max(x, minx)
- x = (max.X < x) ? max.X : x; // min(x, maxx)
-
- float y = value1.Y;
- y = (min.Y > y) ? min.Y : y; // max(y, miny)
- y = (max.Y < y) ? max.Y : y; // min(y, maxy)
-
- float z = value1.Z;
- z = (min.Z > z) ? min.Z : z; // max(z, minz)
- z = (max.Z < z) ? max.Z : z; // min(z, maxz)
-
- return new Vector3(x, y, z);
- }
-
- /// <summary>
- /// Linearly interpolates between two vectors based on the given weighting.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <param name="amount">Value between 0 and 1 indicating the weight of the second source vector.</param>
- /// <returns>The interpolated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Lerp(Vector3 value1, Vector3 value2, float amount)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector3 firstInfluence = value1 * (1f - amount);
- Vector3 secondInfluence = value2 * amount;
- return firstInfluence + secondInfluence;
- }
- else
- {
- return new Vector3(
- value1.X + (value2.X - value1.X) * amount,
- value1.Y + (value2.Y - value1.Y) * amount,
- value1.Z + (value2.Z - value1.Z) * amount);
- }
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Transform(Vector3 position, Matrix4x4 matrix)
- {
- return 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);
- }
-
- /// <summary>
- /// Transforms a vector normal by the given matrix.
- /// </summary>
- /// <param name="normal">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 TransformNormal(Vector3 normal, Matrix4x4 matrix)
- {
- return new Vector3(
- normal.X * matrix.M11 + normal.Y * matrix.M21 + normal.Z * matrix.M31,
- normal.X * matrix.M12 + normal.Y * matrix.M22 + normal.Z * matrix.M32,
- normal.X * matrix.M13 + normal.Y * matrix.M23 + normal.Z * matrix.M33);
- }
-
- /// <summary>
- /// Transforms a vector by the given Quaternion rotation value.
- /// </summary>
- /// <param name="value">The source vector to be rotated.</param>
- /// <param name="rotation">The rotation to apply.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Transform(Vector3 value, Quaternion rotation)
- {
- float x2 = rotation.X + rotation.X;
- float y2 = rotation.Y + rotation.Y;
- float z2 = rotation.Z + rotation.Z;
-
- float wx2 = rotation.W * x2;
- float wy2 = rotation.W * y2;
- float wz2 = rotation.W * z2;
- float xx2 = rotation.X * x2;
- float xy2 = rotation.X * y2;
- float xz2 = rotation.X * z2;
- float yy2 = rotation.Y * y2;
- float yz2 = rotation.Y * z2;
- float zz2 = rotation.Z * z2;
-
- return new Vector3(
- value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2) + value.Z * (xz2 + wy2),
- value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2) + value.Z * (yz2 - wx2),
- value.X * (xz2 - wy2) + value.Y * (yz2 + wx2) + value.Z * (1.0f - xx2 - yy2));
- }
- #endregion Public Static Methods
-
- #region Public operator methods
-
- // All these methods should be inlined as they are implemented
- // over JIT intrinsics
-
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Add(Vector3 left, Vector3 right)
- {
- return left + right;
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Subtract(Vector3 left, Vector3 right)
- {
- return left - right;
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Multiply(Vector3 left, Vector3 right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Multiply(Vector3 left, float right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Multiply(float left, Vector3 right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Divide(Vector3 left, Vector3 right)
- {
- return left / right;
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="divisor">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Divide(Vector3 left, float divisor)
- {
- return left / divisor;
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Negate(Vector3 value)
- {
- return -value;
- }
- #endregion Public operator methods
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Runtime.CompilerServices;
-
-namespace System.Numerics
-{
- // This file contains the definitions for all of the JIT intrinsic methods and properties that are recognized by the current x64 JIT compiler.
- // The implementation defined here is used in any circumstance where the JIT fails to recognize these members as intrinsic.
- // The JIT recognizes these methods and properties by name and signature: if either is changed, the JIT will no longer recognize the member.
- // Some methods declared here are not strictly intrinsic, but delegate to an intrinsic method. For example, only one overload of CopyTo()
- // is actually recognized by the JIT, but both are here for simplicity.
-
- public partial struct Vector3
- {
- /// <summary>
- /// The X component of the vector.
- /// </summary>
- public float X;
- /// <summary>
- /// The Y component of the vector.
- /// </summary>
- public float Y;
- /// <summary>
- /// The Z component of the vector.
- /// </summary>
- public float Z;
-
- #region Constructors
- /// <summary>
- /// Constructs a vector whose elements are all the single specified value.
- /// </summary>
- /// <param name="value">The element to fill the vector with.</param>
- [Intrinsic]
- public Vector3(float value) : this(value, value, value) { }
-
- /// <summary>
- /// Constructs a Vector3 from the given Vector2 and a third value.
- /// </summary>
- /// <param name="value">The Vector to extract X and Y components from.</param>
- /// <param name="z">The Z component.</param>
- [Intrinsic]
- public Vector3(Vector2 value, float z) : this(value.X, value.Y, z) { }
-
- /// <summary>
- /// Constructs a vector with the given individual elements.
- /// </summary>
- /// <param name="x">The X component.</param>
- /// <param name="y">The Y component.</param>
- /// <param name="z">The Z component.</param>
- [Intrinsic]
- public Vector3(float x, float y, float z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- #endregion Constructors
-
- #region Public Instance Methods
- /// <summary>
- /// Copies the contents of the vector into the given array.
- /// </summary>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly void CopyTo(float[] array)
- {
- CopyTo(array, 0);
- }
-
- /// <summary>
- /// Copies the contents of the vector into the given array, starting from index.
- /// </summary>
- /// <exception cref="ArgumentNullException">If array is null.</exception>
- /// <exception cref="RankException">If array is multidimensional.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If index is greater than end of the array or index is less than zero.</exception>
- /// <exception cref="ArgumentException">If number of elements in source vector is greater than those available in destination array.</exception>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly void CopyTo(float[] array, int index)
- {
- if (array == null)
- {
- // Match the JIT's exception type here. For perf, a NullReference is thrown instead of an ArgumentNull.
- throw new NullReferenceException(SR.Arg_NullArgumentNullRef);
- }
- if (index < 0 || index >= array.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.Format(SR.Arg_ArgumentOutOfRangeException, index));
- }
- if ((array.Length - index) < 3)
- {
- throw new ArgumentException(SR.Format(SR.Arg_ElementsInSourceIsGreaterThanDestination, index));
- }
- array[index] = X;
- array[index + 1] = Y;
- array[index + 2] = Z;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Vector3 is equal to this Vector3 instance.
- /// </summary>
- /// <param name="other">The Vector3 to compare this instance to.</param>
- /// <returns>True if the other Vector3 is equal to this instance; False otherwise.</returns>
- [Intrinsic]
- public readonly bool Equals(Vector3 other)
- {
- return X == other.X &&
- Y == other.Y &&
- Z == other.Z;
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the dot product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>The dot product.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Dot(Vector3 vector1, Vector3 vector2)
- {
- return vector1.X * vector2.X +
- vector1.Y * vector2.Y +
- vector1.Z * vector2.Z;
- }
-
- /// <summary>
- /// Returns a vector whose elements are the minimum of each of the pairs of elements in the two source vectors.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <returns>The minimized vector.</returns>
- [Intrinsic]
- public static Vector3 Min(Vector3 value1, Vector3 value2)
- {
- return new Vector3(
- (value1.X < value2.X) ? value1.X : value2.X,
- (value1.Y < value2.Y) ? value1.Y : value2.Y,
- (value1.Z < value2.Z) ? value1.Z : value2.Z);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the maximum of each of the pairs of elements in the two source vectors.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <returns>The maximized vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Max(Vector3 value1, Vector3 value2)
- {
- return new Vector3(
- (value1.X > value2.X) ? value1.X : value2.X,
- (value1.Y > value2.Y) ? value1.Y : value2.Y,
- (value1.Z > value2.Z) ? value1.Z : value2.Z);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the absolute values of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The absolute value vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 Abs(Vector3 value)
- {
- return new Vector3(MathF.Abs(value.X), MathF.Abs(value.Y), MathF.Abs(value.Z));
- }
-
- /// <summary>
- /// Returns a vector whose elements are the square root of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The square root vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 SquareRoot(Vector3 value)
- {
- return new Vector3(MathF.Sqrt(value.X), MathF.Sqrt(value.Y), MathF.Sqrt(value.Z));
- }
- #endregion Public Static Methods
-
- #region Public Static Operators
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator +(Vector3 left, Vector3 right)
- {
- return new Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator -(Vector3 left, Vector3 right)
- {
- return new Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator *(Vector3 left, Vector3 right)
- {
- return new Vector3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator *(Vector3 left, float right)
- {
- return left * new Vector3(right);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator *(float left, Vector3 right)
- {
- return new Vector3(left) * right;
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator /(Vector3 left, Vector3 right)
- {
- return new Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z);
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="value2">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator /(Vector3 value1, float value2)
- {
- return value1 / new Vector3(value2);
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector3 operator -(Vector3 value)
- {
- return Zero - value;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are equal; False otherwise.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator ==(Vector3 left, Vector3 right)
- {
- return (left.X == right.X &&
- left.Y == right.Y &&
- left.Z == right.Z);
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are not equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are not equal; False if they are equal.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator !=(Vector3 left, Vector3 right)
- {
- return (left.X != right.X ||
- left.Y != right.Y ||
- left.Z != right.Z);
- }
- #endregion Public Static Operators
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Globalization;
-using System.Runtime.CompilerServices;
-using System.Text;
-
-namespace System.Numerics
-{
- /// <summary>
- /// A structure encapsulating four single precision floating point values and provides hardware accelerated methods.
- /// </summary>
- public partial struct Vector4 : IEquatable<Vector4>, IFormattable
- {
- #region Public Static Properties
- /// <summary>
- /// Returns the vector (0,0,0,0).
- /// </summary>
- public static Vector4 Zero
- {
- [Intrinsic]
- get
- {
- return new Vector4();
- }
- }
- /// <summary>
- /// Returns the vector (1,1,1,1).
- /// </summary>
- public static Vector4 One
- {
- [Intrinsic]
- get
- {
- return new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
- }
- }
- /// <summary>
- /// Returns the vector (1,0,0,0).
- /// </summary>
- public static Vector4 UnitX { get { return new Vector4(1.0f, 0.0f, 0.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,1,0,0).
- /// </summary>
- public static Vector4 UnitY { get { return new Vector4(0.0f, 1.0f, 0.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,0,1,0).
- /// </summary>
- public static Vector4 UnitZ { get { return new Vector4(0.0f, 0.0f, 1.0f, 0.0f); } }
- /// <summary>
- /// Returns the vector (0,0,0,1).
- /// </summary>
- public static Vector4 UnitW { get { return new Vector4(0.0f, 0.0f, 0.0f, 1.0f); } }
- #endregion Public Static Properties
-
- #region Public instance methods
- /// <summary>
- /// Returns the hash code for this instance.
- /// </summary>
- /// <returns>The hash code.</returns>
- public override readonly int GetHashCode()
- {
- return HashCode.Combine(this.X.GetHashCode(), this.Y.GetHashCode(), this.Z.GetHashCode(), this.W.GetHashCode());
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Object is equal to this Vector4 instance.
- /// </summary>
- /// <param name="obj">The Object to compare against.</param>
- /// <returns>True if the Object is equal to this Vector4; False otherwise.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public override readonly bool Equals(object? obj)
- {
- if (!(obj is Vector4))
- return false;
- return Equals((Vector4)obj);
- }
-
- /// <summary>
- /// Returns a String representing this Vector4 instance.
- /// </summary>
- /// <returns>The string representation.</returns>
- public override readonly string ToString()
- {
- return ToString("G", CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector4 instance, using the specified format to format individual elements.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format)
- {
- return ToString(format, CultureInfo.CurrentCulture);
- }
-
- /// <summary>
- /// Returns a String representing this Vector4 instance, using the specified format to format individual elements
- /// and the given IFormatProvider.
- /// </summary>
- /// <param name="format">The format of individual elements.</param>
- /// <param name="formatProvider">The format provider to use when formatting elements.</param>
- /// <returns>The string representation.</returns>
- public readonly string ToString(string? format, IFormatProvider? formatProvider)
- {
- StringBuilder sb = new StringBuilder();
- string separator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator;
- sb.Append('<');
- sb.Append(this.X.ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(this.Y.ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(this.Z.ToString(format, formatProvider));
- sb.Append(separator);
- sb.Append(' ');
- sb.Append(this.W.ToString(format, formatProvider));
- sb.Append('>');
- return sb.ToString();
- }
-
- /// <summary>
- /// Returns the length of the vector. This operation is cheaper than Length().
- /// </summary>
- /// <returns>The vector's length.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float Length()
- {
- if (Vector.IsHardwareAccelerated)
- {
- float ls = Vector4.Dot(this, this);
- return MathF.Sqrt(ls);
- }
- else
- {
- float ls = X * X + Y * Y + Z * Z + W * W;
-
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the length of the vector squared.
- /// </summary>
- /// <returns>The vector's length squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly float LengthSquared()
- {
- if (Vector.IsHardwareAccelerated)
- {
- return Vector4.Dot(this, this);
- }
- else
- {
- return X * X + Y * Y + Z * Z + W * W;
- }
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the Euclidean distance between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Distance(Vector4 value1, Vector4 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector4 difference = value1 - value2;
- float ls = Vector4.Dot(difference, difference);
- return MathF.Sqrt(ls);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
- float dz = value1.Z - value2.Z;
- float dw = value1.W - value2.W;
-
- float ls = dx * dx + dy * dy + dz * dz + dw * dw;
-
- return MathF.Sqrt(ls);
- }
- }
-
- /// <summary>
- /// Returns the Euclidean distance squared between the two given points.
- /// </summary>
- /// <param name="value1">The first point.</param>
- /// <param name="value2">The second point.</param>
- /// <returns>The distance squared.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float DistanceSquared(Vector4 value1, Vector4 value2)
- {
- if (Vector.IsHardwareAccelerated)
- {
- Vector4 difference = value1 - value2;
- return Vector4.Dot(difference, difference);
- }
- else
- {
- float dx = value1.X - value2.X;
- float dy = value1.Y - value2.Y;
- float dz = value1.Z - value2.Z;
- float dw = value1.W - value2.W;
-
- return dx * dx + dy * dy + dz * dz + dw * dw;
- }
- }
-
- /// <summary>
- /// Returns a vector with the same direction as the given vector, but with a length of 1.
- /// </summary>
- /// <param name="vector">The vector to normalize.</param>
- /// <returns>The normalized vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Normalize(Vector4 vector)
- {
- if (Vector.IsHardwareAccelerated)
- {
- float length = vector.Length();
- return vector / length;
- }
- else
- {
- float ls = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W;
- float invNorm = 1.0f / MathF.Sqrt(ls);
-
- return new Vector4(
- vector.X * invNorm,
- vector.Y * invNorm,
- vector.Z * invNorm,
- vector.W * invNorm);
- }
- }
-
- /// <summary>
- /// Restricts a vector between a min and max value.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="min">The minimum value.</param>
- /// <param name="max">The maximum value.</param>
- /// <returns>The restricted vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Clamp(Vector4 value1, Vector4 min, Vector4 max)
- {
- // This compare order is very important!!!
- // We must follow HLSL behavior in the case user specified min value is bigger than max value.
- float x = value1.X;
- x = (min.X > x) ? min.X : x; // max(x, minx)
- x = (max.X < x) ? max.X : x; // min(x, maxx)
-
- float y = value1.Y;
- y = (min.Y > y) ? min.Y : y; // max(y, miny)
- y = (max.Y < y) ? max.Y : y; // min(y, maxy)
-
- float z = value1.Z;
- z = (min.Z > z) ? min.Z : z; // max(z, minz)
- z = (max.Z < z) ? max.Z : z; // min(z, maxz)
-
- float w = value1.W;
- w = (min.W > w) ? min.W : w; // max(w, minw)
- w = (max.W < w) ? max.W : w; // min(w, minw)
-
- return new Vector4(x, y, z, w);
- }
-
- /// <summary>
- /// Linearly interpolates between two vectors based on the given weighting.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <param name="amount">Value between 0 and 1 indicating the weight of the second source vector.</param>
- /// <returns>The interpolated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Lerp(Vector4 value1, Vector4 value2, float amount)
- {
- return new Vector4(
- value1.X + (value2.X - value1.X) * amount,
- value1.Y + (value2.Y - value1.Y) * amount,
- value1.Z + (value2.Z - value1.Z) * amount,
- value1.W + (value2.W - value1.W) * amount);
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector2 position, Matrix4x4 matrix)
- {
- return new Vector4(
- position.X * matrix.M11 + position.Y * matrix.M21 + matrix.M41,
- position.X * matrix.M12 + position.Y * matrix.M22 + matrix.M42,
- position.X * matrix.M13 + position.Y * matrix.M23 + matrix.M43,
- position.X * matrix.M14 + position.Y * matrix.M24 + matrix.M44);
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector3 position, Matrix4x4 matrix)
- {
- return new Vector4(
- 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,
- position.X * matrix.M14 + position.Y * matrix.M24 + position.Z * matrix.M34 + matrix.M44);
- }
-
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="vector">The source vector.</param>
- /// <param name="matrix">The transformation matrix.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector4 vector, Matrix4x4 matrix)
- {
- return new Vector4(
- vector.X * matrix.M11 + vector.Y * matrix.M21 + vector.Z * matrix.M31 + vector.W * matrix.M41,
- vector.X * matrix.M12 + vector.Y * matrix.M22 + vector.Z * matrix.M32 + vector.W * matrix.M42,
- vector.X * matrix.M13 + vector.Y * matrix.M23 + vector.Z * matrix.M33 + vector.W * matrix.M43,
- vector.X * matrix.M14 + vector.Y * matrix.M24 + vector.Z * matrix.M34 + vector.W * matrix.M44);
- }
-
- /// <summary>
- /// Transforms a vector by the given Quaternion rotation value.
- /// </summary>
- /// <param name="value">The source vector to be rotated.</param>
- /// <param name="rotation">The rotation to apply.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector2 value, Quaternion rotation)
- {
- float x2 = rotation.X + rotation.X;
- float y2 = rotation.Y + rotation.Y;
- float z2 = rotation.Z + rotation.Z;
-
- float wx2 = rotation.W * x2;
- float wy2 = rotation.W * y2;
- float wz2 = rotation.W * z2;
- float xx2 = rotation.X * x2;
- float xy2 = rotation.X * y2;
- float xz2 = rotation.X * z2;
- float yy2 = rotation.Y * y2;
- float yz2 = rotation.Y * z2;
- float zz2 = rotation.Z * z2;
-
- return new Vector4(
- value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2),
- value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2),
- value.X * (xz2 - wy2) + value.Y * (yz2 + wx2),
- 1.0f);
- }
-
- /// <summary>
- /// Transforms a vector by the given Quaternion rotation value.
- /// </summary>
- /// <param name="value">The source vector to be rotated.</param>
- /// <param name="rotation">The rotation to apply.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector3 value, Quaternion rotation)
- {
- float x2 = rotation.X + rotation.X;
- float y2 = rotation.Y + rotation.Y;
- float z2 = rotation.Z + rotation.Z;
-
- float wx2 = rotation.W * x2;
- float wy2 = rotation.W * y2;
- float wz2 = rotation.W * z2;
- float xx2 = rotation.X * x2;
- float xy2 = rotation.X * y2;
- float xz2 = rotation.X * z2;
- float yy2 = rotation.Y * y2;
- float yz2 = rotation.Y * z2;
- float zz2 = rotation.Z * z2;
-
- return new Vector4(
- value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2) + value.Z * (xz2 + wy2),
- value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2) + value.Z * (yz2 - wx2),
- value.X * (xz2 - wy2) + value.Y * (yz2 + wx2) + value.Z * (1.0f - xx2 - yy2),
- 1.0f);
- }
-
- /// <summary>
- /// Transforms a vector by the given Quaternion rotation value.
- /// </summary>
- /// <param name="value">The source vector to be rotated.</param>
- /// <param name="rotation">The rotation to apply.</param>
- /// <returns>The transformed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Transform(Vector4 value, Quaternion rotation)
- {
- float x2 = rotation.X + rotation.X;
- float y2 = rotation.Y + rotation.Y;
- float z2 = rotation.Z + rotation.Z;
-
- float wx2 = rotation.W * x2;
- float wy2 = rotation.W * y2;
- float wz2 = rotation.W * z2;
- float xx2 = rotation.X * x2;
- float xy2 = rotation.X * y2;
- float xz2 = rotation.X * z2;
- float yy2 = rotation.Y * y2;
- float yz2 = rotation.Y * z2;
- float zz2 = rotation.Z * z2;
-
- return new Vector4(
- value.X * (1.0f - yy2 - zz2) + value.Y * (xy2 - wz2) + value.Z * (xz2 + wy2),
- value.X * (xy2 + wz2) + value.Y * (1.0f - xx2 - zz2) + value.Z * (yz2 - wx2),
- value.X * (xz2 - wy2) + value.Y * (yz2 + wx2) + value.Z * (1.0f - xx2 - yy2),
- value.W);
- }
- #endregion Public Static Methods
-
- #region Public operator methods
- // All these methods should be inlines as they are implemented
- // over JIT intrinsics
-
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Add(Vector4 left, Vector4 right)
- {
- return left + right;
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Subtract(Vector4 left, Vector4 right)
- {
- return left - right;
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Multiply(Vector4 left, Vector4 right)
- {
- return left * right;
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Multiply(Vector4 left, float right)
- {
- return left * new Vector4(right, right, right, right);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Multiply(float left, Vector4 right)
- {
- return new Vector4(left, left, left, left) * right;
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Divide(Vector4 left, Vector4 right)
- {
- return left / right;
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="divisor">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Divide(Vector4 left, float divisor)
- {
- return left / divisor;
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Negate(Vector4 value)
- {
- return -value;
- }
- #endregion Public operator methods
- }
-}
+++ /dev/null
-// Licensed to the .NET Foundation under one or more agreements.
-// The .NET Foundation licenses this file to you under the MIT license.
-// See the LICENSE file in the project root for more information.
-
-using System.Runtime.CompilerServices;
-
-namespace System.Numerics
-{
- // This file contains the definitions for all of the JIT intrinsic methods and properties that are recognized by the current x64 JIT compiler.
- // The implementation defined here is used in any circumstance where the JIT fails to recognize these members as intrinsic.
- // The JIT recognizes these methods and properties by name and signature: if either is changed, the JIT will no longer recognize the member.
- // Some methods declared here are not strictly intrinsic, but delegate to an intrinsic method. For example, only one overload of CopyTo()
-
- public partial struct Vector4
- {
- /// <summary>
- /// The X component of the vector.
- /// </summary>
- public float X;
- /// <summary>
- /// The Y component of the vector.
- /// </summary>
- public float Y;
- /// <summary>
- /// The Z component of the vector.
- /// </summary>
- public float Z;
- /// <summary>
- /// The W component of the vector.
- /// </summary>
- public float W;
-
- #region Constructors
-
- /// <summary>
- /// Constructs a vector whose elements are all the single specified value.
- /// </summary>
- /// <param name="value">The element to fill the vector with.</param>
- [Intrinsic]
- public Vector4(float value)
- : this(value, value, value, value)
- {
- }
- /// <summary>
- /// Constructs a vector with the given individual elements.
- /// </summary>
- /// <param name="w">W component.</param>
- /// <param name="x">X component.</param>
- /// <param name="y">Y component.</param>
- /// <param name="z">Z component.</param>
- [Intrinsic]
- public Vector4(float x, float y, float z, float w)
- {
- W = w;
- X = x;
- Y = y;
- Z = z;
- }
-
- /// <summary>
- /// Constructs a Vector4 from the given Vector2 and a Z and W component.
- /// </summary>
- /// <param name="value">The vector to use as the X and Y components.</param>
- /// <param name="z">The Z component.</param>
- /// <param name="w">The W component.</param>
- [Intrinsic]
- public Vector4(Vector2 value, float z, float w)
- {
- X = value.X;
- Y = value.Y;
- Z = z;
- W = w;
- }
-
- /// <summary>
- /// Constructs a Vector4 from the given Vector3 and a W component.
- /// </summary>
- /// <param name="value">The vector to use as the X, Y, and Z components.</param>
- /// <param name="w">The W component.</param>
- [Intrinsic]
- public Vector4(Vector3 value, float w)
- {
- X = value.X;
- Y = value.Y;
- Z = value.Z;
- W = w;
- }
- #endregion Constructors
-
- #region Public Instance Methods
- /// <summary>
- /// Copies the contents of the vector into the given array.
- /// </summary>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly void CopyTo(float[] array)
- {
- CopyTo(array, 0);
- }
-
- /// <summary>
- /// Copies the contents of the vector into the given array, starting from index.
- /// </summary>
- /// <exception cref="ArgumentNullException">If array is null.</exception>
- /// <exception cref="RankException">If array is multidimensional.</exception>
- /// <exception cref="ArgumentOutOfRangeException">If index is greater than end of the array or index is less than zero.</exception>
- /// <exception cref="ArgumentException">If number of elements in source vector is greater than those available in destination array.</exception>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public readonly void CopyTo(float[] array, int index)
- {
- if (array == null)
- {
- // Match the JIT's exception type here. For perf, a NullReference is thrown instead of an ArgumentNull.
- throw new NullReferenceException(SR.Arg_NullArgumentNullRef);
- }
- if (index < 0 || index >= array.Length)
- {
- throw new ArgumentOutOfRangeException(nameof(index), SR.Format(SR.Arg_ArgumentOutOfRangeException, index));
- }
- if ((array.Length - index) < 4)
- {
- throw new ArgumentException(SR.Format(SR.Arg_ElementsInSourceIsGreaterThanDestination, index));
- }
- array[index] = X;
- array[index + 1] = Y;
- array[index + 2] = Z;
- array[index + 3] = W;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the given Vector4 is equal to this Vector4 instance.
- /// </summary>
- /// <param name="other">The Vector4 to compare this instance to.</param>
- /// <returns>True if the other Vector4 is equal to this instance; False otherwise.</returns>
- [Intrinsic]
- public readonly bool Equals(Vector4 other)
- {
- return this.X == other.X
- && this.Y == other.Y
- && this.Z == other.Z
- && this.W == other.W;
- }
- #endregion Public Instance Methods
-
- #region Public Static Methods
- /// <summary>
- /// Returns the dot product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>The dot product.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static float Dot(Vector4 vector1, Vector4 vector2)
- {
- return vector1.X * vector2.X +
- vector1.Y * vector2.Y +
- vector1.Z * vector2.Z +
- vector1.W * vector2.W;
- }
-
- /// <summary>
- /// Returns a vector whose elements are the minimum of each of the pairs of elements in the two source vectors.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <returns>The minimized vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Min(Vector4 value1, Vector4 value2)
- {
- return new Vector4(
- (value1.X < value2.X) ? value1.X : value2.X,
- (value1.Y < value2.Y) ? value1.Y : value2.Y,
- (value1.Z < value2.Z) ? value1.Z : value2.Z,
- (value1.W < value2.W) ? value1.W : value2.W);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the maximum of each of the pairs of elements in the two source vectors.
- /// </summary>
- /// <param name="value1">The first source vector.</param>
- /// <param name="value2">The second source vector.</param>
- /// <returns>The maximized vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Max(Vector4 value1, Vector4 value2)
- {
- return new Vector4(
- (value1.X > value2.X) ? value1.X : value2.X,
- (value1.Y > value2.Y) ? value1.Y : value2.Y,
- (value1.Z > value2.Z) ? value1.Z : value2.Z,
- (value1.W > value2.W) ? value1.W : value2.W);
- }
-
- /// <summary>
- /// Returns a vector whose elements are the absolute values of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The absolute value vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 Abs(Vector4 value)
- {
- return new Vector4(MathF.Abs(value.X), MathF.Abs(value.Y), MathF.Abs(value.Z), MathF.Abs(value.W));
- }
-
- /// <summary>
- /// Returns a vector whose elements are the square root of each of the source vector's elements.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The square root vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 SquareRoot(Vector4 value)
- {
- return new Vector4(MathF.Sqrt(value.X), MathF.Sqrt(value.Y), MathF.Sqrt(value.Z), MathF.Sqrt(value.W));
- }
- #endregion Public Static Methods
-
- #region Public static operators
- /// <summary>
- /// Adds two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The summed vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- 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);
- }
-
- /// <summary>
- /// Subtracts the second vector from the first.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The difference vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- 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);
- }
-
- /// <summary>
- /// Multiplies two vectors together.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The product vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- 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);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The source vector.</param>
- /// <param name="right">The scalar value.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 operator *(Vector4 left, float right)
- {
- return left * new Vector4(right);
- }
-
- /// <summary>
- /// Multiplies a vector by the given scalar.
- /// </summary>
- /// <param name="left">The scalar value.</param>
- /// <param name="right">The source vector.</param>
- /// <returns>The scaled vector.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 operator *(float left, Vector4 right)
- {
- return new Vector4(left) * right;
- }
-
- /// <summary>
- /// Divides the first vector by the second.
- /// </summary>
- /// <param name="left">The first source vector.</param>
- /// <param name="right">The second source vector.</param>
- /// <returns>The vector resulting from the division.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- 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);
- }
-
- /// <summary>
- /// Divides the vector by the given scalar.
- /// </summary>
- /// <param name="value1">The source vector.</param>
- /// <param name="value2">The scalar value.</param>
- /// <returns>The result of the division.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 operator /(Vector4 value1, float value2)
- {
- return value1 / new Vector4(value2);
- }
-
- /// <summary>
- /// Negates a given vector.
- /// </summary>
- /// <param name="value">The source vector.</param>
- /// <returns>The negated vector.</returns>
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Vector4 operator -(Vector4 value)
- {
- return Zero - value;
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are equal; False otherwise.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator ==(Vector4 left, Vector4 right)
- {
- return left.Equals(right);
- }
-
- /// <summary>
- /// Returns a boolean indicating whether the two given vectors are not equal.
- /// </summary>
- /// <param name="left">The first vector to compare.</param>
- /// <param name="right">The second vector to compare.</param>
- /// <returns>True if the vectors are not equal; False if they are equal.</returns>
- [Intrinsic]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool operator !=(Vector4 left, Vector4 right)
- {
- return !(left == right);
- }
- #endregion Public static operators
- }
-}