System.DoubleNumerics by weingartner machinen bau

<PackageReference Include="System.DoubleNumerics" Version="3.1.0" />

 Vector4

using Common.System.Numerics.Hashing; using System.Globalization; using System.Runtime.CompilerServices; using System.Text; namespace System.DoubleNumerics { public struct Vector4 : IEquatable<Vector4>, IFormattable { public double X; public double Y; public double Z; public double W; public static Vector4 Zero => default(Vector4); public static Vector4 One => new Vector4(1, 1, 1, 1); public static Vector4 UnitX => new Vector4(1, 0, 0, 0); public static Vector4 UnitY => new Vector4(0, 1, 0, 0); public static Vector4 UnitZ => new Vector4(0, 0, 1, 0); public static Vector4 UnitW => new Vector4(0, 0, 0, 1); public override int GetHashCode() { int hashCode = X.GetHashCode(); hashCode = HashHelpers.Combine(hashCode, Y.GetHashCode()); hashCode = HashHelpers.Combine(hashCode, Z.GetHashCode()); return HashHelpers.Combine(hashCode, W.GetHashCode()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) { if (obj is Vector4) return Equals((Vector4)obj); return false; } public override string ToString() { return ToString("G", CultureInfo.CurrentCulture); } public string ToString(string format) { return ToString(format, CultureInfo.CurrentCulture); } public string ToString(string format, IFormatProvider formatProvider) { StringBuilder stringBuilder = new StringBuilder(); string numberGroupSeparator = NumberFormatInfo.GetInstance(formatProvider).NumberGroupSeparator; stringBuilder.Append('<'); stringBuilder.Append(X.ToString(format, formatProvider)); stringBuilder.Append(numberGroupSeparator); stringBuilder.Append(' '); stringBuilder.Append(Y.ToString(format, formatProvider)); stringBuilder.Append(numberGroupSeparator); stringBuilder.Append(' '); stringBuilder.Append(Z.ToString(format, formatProvider)); stringBuilder.Append(numberGroupSeparator); stringBuilder.Append(' '); stringBuilder.Append(W.ToString(format, formatProvider)); stringBuilder.Append('>'); return stringBuilder.ToString(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public double Length() { double num = X * X + Y * Y + Z * Z + W * W; return Math.Sqrt(num); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public double LengthSquared() { return X * X + Y * Y + Z * Z + W * W; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Distance(Vector4 value1, Vector4 value2) { double num = value1.X - value2.X; double num2 = value1.Y - value2.Y; double num3 = value1.Z - value2.Z; double num4 = value1.W - value2.W; double num5 = num * num + num2 * num2 + num3 * num3 + num4 * num4; return Math.Sqrt(num5); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double DistanceSquared(Vector4 value1, Vector4 value2) { double num = value1.X - value2.X; double num2 = value1.Y - value2.Y; double num3 = value1.Z - value2.Z; double num4 = value1.W - value2.W; return num * num + num2 * num2 + num3 * num3 + num4 * num4; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Normalize(Vector4 vector) { double num = vector.X * vector.X + vector.Y * vector.Y + vector.Z * vector.Z + vector.W * vector.W; double num2 = 1 / Math.Sqrt(num); return new Vector4(vector.X * num2, vector.Y * num2, vector.Z * num2, vector.W * num2); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Clamp(Vector4 value1, Vector4 min, Vector4 max) { double x = value1.X; x = ((x > max.X) ? max.X : x); x = ((x < min.X) ? min.X : x); double y = value1.Y; y = ((y > max.Y) ? max.Y : y); y = ((y < min.Y) ? min.Y : y); double z = value1.Z; z = ((z > max.Z) ? max.Z : z); z = ((z < min.Z) ? min.Z : z); double w = value1.W; w = ((w > max.W) ? max.W : w); w = ((w < min.W) ? min.W : w); return new Vector4(x, y, z, w); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Lerp(Vector4 value1, Vector4 value2, double 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); } [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); } [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); } [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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Transform(Vector2 value, Quaternion rotation) { double num = rotation.X + rotation.X; double num2 = rotation.Y + rotation.Y; double num3 = rotation.Z + rotation.Z; double num4 = rotation.W * num; double num5 = rotation.W * num2; double num6 = rotation.W * num3; double num7 = rotation.X * num; double num8 = rotation.X * num2; double num9 = rotation.X * num3; double num10 = rotation.Y * num2; double num11 = rotation.Y * num3; double num12 = rotation.Z * num3; return new Vector4(value.X * (1 - num10 - num12) + value.Y * (num8 - num6), value.X * (num8 + num6) + value.Y * (1 - num7 - num12), value.X * (num9 - num5) + value.Y * (num11 + num4), 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Transform(Vector3 value, Quaternion rotation) { double num = rotation.X + rotation.X; double num2 = rotation.Y + rotation.Y; double num3 = rotation.Z + rotation.Z; double num4 = rotation.W * num; double num5 = rotation.W * num2; double num6 = rotation.W * num3; double num7 = rotation.X * num; double num8 = rotation.X * num2; double num9 = rotation.X * num3; double num10 = rotation.Y * num2; double num11 = rotation.Y * num3; double num12 = rotation.Z * num3; return new Vector4(value.X * (1 - num10 - num12) + value.Y * (num8 - num6) + value.Z * (num9 + num5), value.X * (num8 + num6) + value.Y * (1 - num7 - num12) + value.Z * (num11 - num4), value.X * (num9 - num5) + value.Y * (num11 + num4) + value.Z * (1 - num7 - num10), 1); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Transform(Vector4 value, Quaternion rotation) { double num = rotation.X + rotation.X; double num2 = rotation.Y + rotation.Y; double num3 = rotation.Z + rotation.Z; double num4 = rotation.W * num; double num5 = rotation.W * num2; double num6 = rotation.W * num3; double num7 = rotation.X * num; double num8 = rotation.X * num2; double num9 = rotation.X * num3; double num10 = rotation.Y * num2; double num11 = rotation.Y * num3; double num12 = rotation.Z * num3; return new Vector4(value.X * (1 - num10 - num12) + value.Y * (num8 - num6) + value.Z * (num9 + num5), value.X * (num8 + num6) + value.Y * (1 - num7 - num12) + value.Z * (num11 - num4), value.X * (num9 - num5) + value.Y * (num11 + num4) + value.Z * (1 - num7 - num10), value.W); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Add(Vector4 left, Vector4 right) { return left + right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Subtract(Vector4 left, Vector4 right) { return left - right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Multiply(Vector4 left, Vector4 right) { return left * right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Multiply(Vector4 left, double right) { return left * new Vector4(right, right, right, right); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Multiply(double left, Vector4 right) { return new Vector4(left, left, left, left) * right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Divide(Vector4 left, Vector4 right) { return left / right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Divide(Vector4 left, double divisor) { return left / divisor; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Negate(Vector4 value) { return -value; } public Vector4(double value) { this = new Vector4(value, value, value, value); } public Vector4(double x, double y, double z, double w) { W = w; X = x; Y = y; Z = z; } public Vector4(Vector2 value, double z, double w) { X = value.X; Y = value.Y; Z = z; W = w; } public Vector4(Vector3 value, double w) { X = value.X; Y = value.Y; Z = value.Z; W = w; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void CopyTo(double[] array) { CopyTo(array, 0); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void CopyTo(double[] array, int index) { if (array == null) throw new NullReferenceException(Strings.Arg_NullArgumentNullRef); if (index < 0 || index >= array.Length) throw new ArgumentOutOfRangeException("index", string.Format(Strings.Arg_ArgumentOutOfRangeException, index)); if (array.Length - index < 4) throw new ArgumentException(string.Format(Strings.Arg_ElementsInSourceIsGreaterThanDestination, index)); array[index] = X; array[index + 1] = Y; array[index + 2] = Z; array[index + 3] = W; } public bool Equals(Vector4 other) { return X == other.X && Y == other.Y && Z == other.Z && W == other.W; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Dot(Vector4 vector1, Vector4 vector2) { return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z + vector1.W * vector2.W; } [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); } [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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 Abs(Vector4 value) { return new Vector4(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z), Math.Abs(value.W)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector4 SquareRoot(Vector4 value) { return new Vector4(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z), Math.Sqrt(value.W)); } 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); } 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); } 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); } public static Vector4 operator *(Vector4 left, double right) { return left * new Vector4(right); } public static Vector4 operator *(double left, Vector4 right) { return new Vector4(left) * right; } 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); } public static Vector4 operator /(Vector4 value1, double value2) { double num = 1 / value2; return new Vector4(value1.X * num, value1.Y * num, value1.Z * num, value1.W * num); } public static Vector4 operator -(Vector4 value) { return Zero - value; } public static bool operator ==(Vector4 left, Vector4 right) { return left.Equals(right); } public static bool operator !=(Vector4 left, Vector4 right) { return !(left == right); } } }