System.DoubleNumerics by weingartner machinen bau

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

 Vector3

using Common.System.Numerics.Hashing; using System.Globalization; using System.Runtime.CompilerServices; using System.Text; namespace System.DoubleNumerics { public struct Vector3 : IEquatable<Vector3>, IFormattable { public double X; public double Y; public double Z; public static Vector3 Zero => default(Vector3); public static Vector3 One => new Vector3(1, 1, 1); public static Vector3 UnitX => new Vector3(1, 0, 0); public static Vector3 UnitY => new Vector3(0, 1, 0); public static Vector3 UnitZ => new Vector3(0, 0, 1); public override int GetHashCode() { int hashCode = X.GetHashCode(); hashCode = HashHelpers.Combine(hashCode, Y.GetHashCode()); return HashHelpers.Combine(hashCode, Z.GetHashCode()); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public override bool Equals(object obj) { if (obj is Vector3) return Equals((Vector3)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(((IFormattable)X).ToString(format, formatProvider)); stringBuilder.Append(numberGroupSeparator); stringBuilder.Append(' '); stringBuilder.Append(((IFormattable)Y).ToString(format, formatProvider)); stringBuilder.Append(numberGroupSeparator); stringBuilder.Append(' '); stringBuilder.Append(((IFormattable)Z).ToString(format, formatProvider)); stringBuilder.Append('>'); return stringBuilder.ToString(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public double Length() { double d = X * X + Y * Y + Z * Z; return Math.Sqrt(d); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public double LengthSquared() { return X * X + Y * Y + Z * Z; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Distance(Vector3 value1, Vector3 value2) { double num = value1.X - value2.X; double num2 = value1.Y - value2.Y; double num3 = value1.Z - value2.Z; double num4 = num * num + num2 * num2 + num3 * num3; return Math.Sqrt(num4); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double DistanceSquared(Vector3 value1, Vector3 value2) { double num = value1.X - value2.X; double num2 = value1.Y - value2.Y; double num3 = value1.Z - value2.Z; return num * num + num2 * num2 + num3 * num3; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Normalize(Vector3 value) { double d = value.X * value.X + value.Y * value.Y + value.Z * value.Z; double num = Math.Sqrt(d); return new Vector3(value.X / num, value.Y / num, value.Z / num); } [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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Reflect(Vector3 vector, Vector3 normal) { double num = vector.X * normal.X + vector.Y * normal.Y + vector.Z * normal.Z; double num2 = normal.X * num * 2; double num3 = normal.Y * num * 2; double num4 = normal.Z * num * 2; return new Vector3(vector.X - num2, vector.Y - num3, vector.Z - num4); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Clamp(Vector3 value1, Vector3 min, Vector3 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); return new Vector3(x, y, z); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Lerp(Vector3 value1, Vector3 value2, double amount) { return new Vector3(value1.X + (value2.X - value1.X) * amount, value1.Y + (value2.Y - value1.Y) * amount, value1.Z + (value2.Z - value1.Z) * amount); } [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); } [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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 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 Vector3(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)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Add(Vector3 left, Vector3 right) { return left + right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Subtract(Vector3 left, Vector3 right) { return left - right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Multiply(Vector3 left, Vector3 right) { return left * right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Multiply(Vector3 left, double right) { return left * right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Multiply(double left, Vector3 right) { return left * right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Divide(Vector3 left, Vector3 right) { return left / right; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Divide(Vector3 left, double divisor) { return left / divisor; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Negate(Vector3 value) { return -value; } public Vector3(double value) { this = new Vector3(value, value, value); } public Vector3(Vector2 value, double z) { this = new Vector3(value.X, value.Y, z); } public Vector3(double x, double y, double z) { X = x; Y = y; Z = z; } [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 < 3) throw new ArgumentException(string.Format(Strings.Arg_ElementsInSourceIsGreaterThanDestination, index)); array[index] = X; array[index + 1] = Y; array[index + 2] = Z; } public bool Equals(Vector3 other) { return X == other.X && Y == other.Y && Z == other.Z; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static double Dot(Vector3 vector1, Vector3 vector2) { return vector1.X * vector2.X + vector1.Y * vector2.Y + vector1.Z * vector2.Z; } 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); } [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); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 Abs(Vector3 value) { return new Vector3(Math.Abs(value.X), Math.Abs(value.Y), Math.Abs(value.Z)); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Vector3 SquareRoot(Vector3 value) { return new Vector3(Math.Sqrt(value.X), Math.Sqrt(value.Y), Math.Sqrt(value.Z)); } public static Vector3 operator +(Vector3 left, Vector3 right) { return new Vector3(left.X + right.X, left.Y + right.Y, left.Z + right.Z); } public static Vector3 operator -(Vector3 left, Vector3 right) { return new Vector3(left.X - right.X, left.Y - right.Y, left.Z - right.Z); } public static Vector3 operator *(Vector3 left, Vector3 right) { return new Vector3(left.X * right.X, left.Y * right.Y, left.Z * right.Z); } public static Vector3 operator *(Vector3 left, double right) { return left * new Vector3(right); } public static Vector3 operator *(double left, Vector3 right) { return new Vector3(left) * right; } public static Vector3 operator /(Vector3 left, Vector3 right) { return new Vector3(left.X / right.X, left.Y / right.Y, left.Z / right.Z); } public static Vector3 operator /(Vector3 value1, double value2) { double num = 1 / value2; return new Vector3(value1.X * num, value1.Y * num, value1.Z * num); } public static Vector3 operator -(Vector3 value) { return Zero - value; } public static bool operator ==(Vector3 left, Vector3 right) { return left.X == right.X && left.Y == right.Y && left.Z == right.Z; } public static bool operator !=(Vector3 left, Vector3 right) { return left.X != right.X || left.Y != right.Y || left.Z != right.Z; } } }