0.2.0-dev Download ZIP
109 lines ·3.0 KB Raw
| 1 | namespace EvilEngine.GenerativeMotion; |
| 2 | |
| 3 | internal readonly record struct Double3(double X, double Y, double Z) |
| 4 | { |
| 5 | public static Double3 Zero => default; |
| 6 | |
| 7 | public double this[int axis] => axis switch { 0 => X, 1 => Y, _ => Z }; |
| 8 | |
| 9 | public static Double3 operator +(Double3 a, Double3 b) |
| 10 | { |
| 11 | return new Double3(a.X + b.X, a.Y + b.Y, a.Z + b.Z); |
| 12 | } |
| 13 | |
| 14 | public static Double3 operator -(Double3 a, Double3 b) |
| 15 | { |
| 16 | return new Double3(a.X - b.X, a.Y - b.Y, a.Z - b.Z); |
| 17 | } |
| 18 | |
| 19 | public static Double3 operator *(Double3 a, double s) |
| 20 | { |
| 21 | return new Double3(a.X * s, a.Y * s, a.Z * s); |
| 22 | } |
| 23 | |
| 24 | public static Double3 operator /(Double3 a, double s) |
| 25 | { |
| 26 | return new Double3(a.X / s, a.Y / s, a.Z / s); |
| 27 | } |
| 28 | |
| 29 | public double Length() |
| 30 | { |
| 31 | return Math.Sqrt(X * X + Y * Y + Z * Z); |
| 32 | } |
| 33 | |
| 34 | public static double Dot(Double3 a, Double3 b) |
| 35 | { |
| 36 | return a.X * b.X + a.Y * b.Y + a.Z * b.Z; |
| 37 | } |
| 38 | |
| 39 | public static Double3 Cross(Double3 a, Double3 b) |
| 40 | { |
| 41 | return new Double3(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X); |
| 42 | } |
| 43 | |
| 44 | public Double3 With(int axis, double value) |
| 45 | { |
| 46 | return axis switch |
| 47 | { |
| 48 | 0 => this with { X = value }, |
| 49 | 1 => this with { Y = value }, |
| 50 | _ => this with { Z = value } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | public Double3 Add(int axis, double value) |
| 55 | { |
| 56 | return With(axis, this[axis] + value); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | internal readonly record struct DoubleQuat(double X, double Y, double Z, double W) |
| 61 | { |
| 62 | public static DoubleQuat Identity => new(0, 0, 0, 1); |
| 63 | |
| 64 | public static DoubleQuat operator *(DoubleQuat a, DoubleQuat b) |
| 65 | { |
| 66 | return new DoubleQuat( |
| 67 | a.W * b.X + a.X * b.W + a.Y * b.Z - a.Z * b.Y, |
| 68 | a.W * b.Y - a.X * b.Z + a.Y * b.W + a.Z * b.X, |
| 69 | a.W * b.Z + a.X * b.Y - a.Y * b.X + a.Z * b.W, |
| 70 | a.W * b.W - a.X * b.X - a.Y * b.Y - a.Z * b.Z); |
| 71 | } |
| 72 | |
| 73 | public DoubleQuat Inverse() |
| 74 | { |
| 75 | return new DoubleQuat(-X, -Y, -Z, W); |
| 76 | } |
| 77 | |
| 78 | public DoubleQuat Normalized() |
| 79 | { |
| 80 | var length = Math.Sqrt(X * X + Y * Y + Z * Z + W * W); |
| 81 | return new DoubleQuat(X / length, Y / length, Z / length, W / length); |
| 82 | } |
| 83 | |
| 84 | public Double3 Apply(Double3 v) |
| 85 | { |
| 86 | var u = new Double3(X, Y, Z); |
| 87 | var t = Double3.Cross(u, v) * 2.0; |
| 88 | return v + t * W + Double3.Cross(u, t); |
| 89 | } |
| 90 | |
| 91 | public static DoubleQuat FromRotationVector(Double3 v) |
| 92 | { |
| 93 | var angle = v.Length(); |
| 94 | var scale = angle <= 1e-3 |
| 95 | ? 0.5 - angle * angle / 48.0 + angle * angle * angle * angle / 3840.0 |
| 96 | : Math.Sin(angle / 2.0) / angle; |
| 97 | return new DoubleQuat(v.X * scale, v.Y * scale, v.Z * scale, Math.Cos(angle / 2.0)); |
| 98 | } |
| 99 | |
| 100 | public static DoubleQuat Between(Double3 u, Double3 v) |
| 101 | { |
| 102 | u /= u.Length(); |
| 103 | v /= v.Length(); |
| 104 | var axis = Double3.Cross(u, v); |
| 105 | var s = axis.Length(); |
| 106 | var c = Math.Clamp(Double3.Dot(u, v), -1.0, 1.0); |
| 107 | return s < 1e-9 ? Identity : FromRotationVector(axis / s * Math.Atan2(s, c)); |
| 108 | } |
| 109 | } |