| | 1 | | using System.Diagnostics; |
| | 2 | | using System.Linq.Expressions; |
| | 3 | | using System.Text; |
| | 4 | |
|
| | 5 | | namespace Towel.Mathematics; |
| | 6 | |
|
| | 7 | | /// <summary>Standard 4-component quaternion [x, y, z, w]. W is the rotation ammount.</summary> |
| | 8 | | /// <typeparam name="T">The numeric type of this Quaternion.</typeparam> |
| | 9 | | [DebuggerDisplay("{" + nameof(DebuggerString) + "}")] |
| | 10 | | public class Quaternion<T> |
| | 11 | | { |
| | 12 | | internal T _x; |
| | 13 | | internal T _y; |
| | 14 | | internal T _z; |
| | 15 | | internal T _w; |
| | 16 | |
|
| | 17 | | #region Static Properties |
| | 18 | |
|
| | 19 | | /// <summary>Returns an identity quaternion (no rotation).</summary> |
| | 20 | | public static Quaternion<T> Identity |
| | 21 | | { |
| | 22 | | get |
| 0 | 23 | | { |
| 0 | 24 | | return new Quaternion<T>(Constant<T>.Zero, Constant<T>.Zero, Constant<T>.Zero, Constant<T>.One); |
| 0 | 25 | | } |
| | 26 | | } |
| | 27 | |
|
| | 28 | | #endregion |
| | 29 | |
|
| | 30 | | #region Properties |
| | 31 | |
|
| | 32 | | /// <summary>The X component of the quaternion. (axis, NOT rotation ammount)</summary> |
| 0 | 33 | | public T X { get { return _x; } set { _x = value; } } |
| | 34 | | /// <summary>The Y component of the quaternion. (axis, NOT rotation ammount)</summary> |
| 0 | 35 | | public T Y { get { return _y; } set { _y = value; } } |
| | 36 | | /// <summary>The Z component of the quaternion. (axis, NOT rotation ammount)</summary> |
| 0 | 37 | | public T Z { get { return _z; } set { _z = value; } } |
| | 38 | | /// <summary>The W component of the quaternion. (rotation ammount, NOT axis)</summary> |
| 0 | 39 | | public T W { get { return _w; } set { _w = value; } } |
| | 40 | |
|
| | 41 | | #endregion |
| | 42 | |
|
| | 43 | | #region Debugger Properties |
| | 44 | |
|
| | 45 | | internal string DebuggerString |
| | 46 | | { |
| | 47 | | get |
| 0 | 48 | | { |
| 0 | 49 | | StringBuilder stringBuilder = new(); |
| 0 | 50 | | stringBuilder.Append("[ "); |
| 0 | 51 | | stringBuilder.Append(_x); |
| 0 | 52 | | stringBuilder.Append(", "); |
| 0 | 53 | | stringBuilder.Append(_y); |
| 0 | 54 | | stringBuilder.Append(", "); |
| 0 | 55 | | stringBuilder.Append(_z); |
| 0 | 56 | | stringBuilder.Append(", "); |
| 0 | 57 | | stringBuilder.Append(_w); |
| 0 | 58 | | stringBuilder.Append(" ]"); |
| 0 | 59 | | return stringBuilder.ToString(); |
| 0 | 60 | | } |
| | 61 | | } |
| | 62 | |
|
| | 63 | | #endregion |
| | 64 | |
|
| | 65 | | #region Constructors |
| | 66 | |
|
| | 67 | | /// <summary>Constructs a quaternion.</summary> |
| | 68 | | /// <param name="x">The x component of the quaternion.</param> |
| | 69 | | /// <param name="y">The y component of the quaternion.</param> |
| | 70 | | /// <param name="z">The z component of the quaternion.</param> |
| | 71 | | /// <param name="w">The w component of the quaternion.</param> |
| 1610 | 72 | | public Quaternion(T x, T y, T z, T w) { _x = x; _y = y; _z = z; _w = w; } |
| | 73 | |
|
| | 74 | | #endregion |
| | 75 | |
|
| | 76 | | #region Factories |
| | 77 | |
|
| | 78 | | ///// <summary>Creates a quaternion from an axis and rotation.</summary> |
| | 79 | | ///// <param name="axis">The to create the quaternion from.</param> |
| | 80 | | ///// <param name="angle">The angle to create the quaternion from.</param> |
| | 81 | | ///// <returns>The newly created quaternion.</returns> |
| | 82 | | // public static Quaternion<T> FactoryFromAxisAngle(Vector axis, T angle) |
| | 83 | | // { |
| | 84 | | // throw new System.NotImplementedException(); |
| | 85 | | // //if (axis.LengthSquared() is 0.0f) |
| | 86 | | // // return FactoryIdentity; |
| | 87 | | // //T sinAngleOverAxisLength = Calc.Sin(angle / 2) / axis.Length(); |
| | 88 | | // //return Quaternion<T>.Normalize(new Quaternion<T>( |
| | 89 | | // // _multiply(axis.X, sinAngleOverAxisLength), |
| | 90 | | // // axis.Y * sinAngleOverAxisLength, |
| | 91 | | // // axis.Z * sinAngleOverAxisLength, |
| | 92 | | // // Calc.Cos(angle / 2))); |
| | 93 | | // } |
| | 94 | |
|
| | 95 | | /// <summary>Converts a 3x3 rotational matrix into a quaternion.</summary> |
| | 96 | | /// <param name="matrix">The matrix to convert.</param> |
| | 97 | | /// <returns>The rotation expressed as a quaternion.</returns> |
| | 98 | | public static Quaternion<T> Factory_Matrix3x3(Matrix<T> matrix) |
| 0 | 99 | | { |
| | 100 | | // Note: this method needs optimization |
| | 101 | |
|
| 0 | 102 | | if (matrix.Rows != 3 || matrix.Columns != 3) |
| 0 | 103 | | throw new System.ArithmeticException("error converting matrix to quaternion. matrix is not 3x3."); |
| | 104 | |
|
| 0 | 105 | | T w = Statics.Subtraction(Statics.Addition(Constant<T>.One, matrix[0, 0], matrix[1, 1], matrix[2, 2]), Convert<int, |
| 0 | 106 | | return new Quaternion<T>( |
| 0 | 107 | | Statics.Division(Statics.Subtraction(matrix[2, 1], matrix[1, 2]), Statics.Multiplication(Statics.Convert<int, T>(4 |
| 0 | 108 | | Statics.Division(Statics.Subtraction(matrix[0, 2], matrix[2, 0]), Statics.Multiplication(Statics.Convert<int, T>(4 |
| 0 | 109 | | Statics.Division(Statics.Subtraction(matrix[1, 0], matrix[0, 1]), Statics.Multiplication(Statics.Convert<int, T>(4 |
| 0 | 110 | | w); |
| 0 | 111 | | } |
| | 112 | |
|
| | 113 | | /// <summary>Converts a 4x4 rotational matrix into a quaternion.</summary> |
| | 114 | | /// <param name="matrix">The matrix to convert.</param> |
| | 115 | | /// <returns>The rotation expressed as a quaternion.</returns> |
| | 116 | | public static Quaternion<T> Factory_Matrix4x4(Matrix<T> matrix) |
| 0 | 117 | | { |
| | 118 | | // Note: this method needs optimization |
| | 119 | |
|
| 0 | 120 | | matrix = matrix.Transpose(); |
| | 121 | | T w, x, y, z; |
| 0 | 122 | | T diagonal = Statics.Addition(matrix[0, 0], matrix[1, 1], matrix[2, 2]); |
| 0 | 123 | | if (Statics.GreaterThan(diagonal, Constant<T>.Zero)) |
| 0 | 124 | | { |
| 0 | 125 | | T w4 = Statics.Multiplication(Statics.SquareRoot(Statics.Addition(diagonal, Constant<T>.One)), Statics.Convert<int |
| 0 | 126 | | w = Statics.Division(w4, Convert<int, T>(4)); |
| 0 | 127 | | x = Statics.Division(Statics.Subtraction(matrix[2, 1], matrix[1, 2]), w4); |
| 0 | 128 | | y = Statics.Division(Statics.Subtraction(matrix[0, 2], matrix[2, 0]), w4); |
| 0 | 129 | | z = Statics.Division(Statics.Subtraction(matrix[1, 0], matrix[0, 1]), w4); |
| 0 | 130 | | } |
| 0 | 131 | | else if (Statics.GreaterThan(matrix[0, 0], matrix[1, 1]) && Statics.GreaterThan(matrix[0, 0], matrix[2, 2])) |
| 0 | 132 | | { |
| 0 | 133 | | T x4 = Statics.Multiplication(Statics.SquareRoot(Statics.Subtraction(Statics.Subtraction(Statics.Addition(Constant |
| 0 | 134 | | w = Statics.Division(Statics.Subtraction(matrix[2, 1], matrix[1, 2]), x4); |
| 0 | 135 | | x = Statics.Division(x4, Convert<int, T>(4)); |
| 0 | 136 | | y = Statics.Division(Statics.Addition(matrix[0, 1], matrix[1, 0]), x4); |
| 0 | 137 | | z = Statics.Division(Statics.Addition(matrix[0, 2], matrix[2, 0]), x4); |
| 0 | 138 | | } |
| 0 | 139 | | else if (Statics.GreaterThan(matrix[1, 1], matrix[2, 2])) |
| 0 | 140 | | { |
| 0 | 141 | | T y4 = Statics.Multiplication(Statics.SquareRoot(Statics.Subtraction(Statics.Subtraction(Statics.Addition(Constant |
| 0 | 142 | | w = Statics.Division(Statics.Subtraction(matrix[0, 2], matrix[2, 0]), y4); |
| 0 | 143 | | x = Statics.Division(Statics.Addition(matrix[0, 1], matrix[1, 0]), y4); |
| 0 | 144 | | y = Statics.Division(y4, Convert<int, T>(4)); |
| 0 | 145 | | z = Statics.Division(Statics.Addition(matrix[1, 2], matrix[2, 1]), y4); |
| 0 | 146 | | } |
| | 147 | | else |
| 0 | 148 | | { |
| 0 | 149 | | T z4 = Statics.Multiplication(Statics.SquareRoot(Statics.Subtraction(Statics.Subtraction(Statics.Addition(Constant |
| 0 | 150 | | w = Statics.Division(Statics.Subtraction(matrix[1, 0], matrix[0, 1]), z4); |
| 0 | 151 | | x = Statics.Division(Statics.Addition(matrix[0, 2], matrix[2, 0]), z4); |
| 0 | 152 | | y = Statics.Division(Statics.Addition(matrix[1, 2], matrix[2, 1]), z4); |
| 0 | 153 | | z = Statics.Division(z4, Convert<int, T>(4)); |
| 0 | 154 | | } |
| 0 | 155 | | return new Quaternion<T>(x, y, z, w); |
| 0 | 156 | | } |
| | 157 | |
|
| | 158 | | #endregion |
| | 159 | |
|
| | 160 | | #region Mathematics |
| | 161 | |
|
| | 162 | | #region HasZeroMagnitude |
| | 163 | |
|
| | 164 | | /// <summary>Checks quaternion for zero magnitude.</summary> |
| | 165 | | /// <param name="a">The quaternion to check for zero magnitude.</param> |
| | 166 | | /// <returns>True if the quaternion has zero magnitude. False if not.</returns> |
| | 167 | | public static bool GetHasZeroMagnitude(Quaternion<T> a) |
| 0 | 168 | | { |
| 0 | 169 | | return |
| 0 | 170 | | Statics.Equate(a._x, Constant<T>.Zero) && |
| 0 | 171 | | Statics.Equate(a._y, Constant<T>.Zero) && |
| 0 | 172 | | Statics.Equate(a._z, Constant<T>.Zero) && |
| 0 | 173 | | Statics.Equate(a._w, Constant<T>.Zero); |
| 0 | 174 | | } |
| | 175 | |
|
| | 176 | | /// <summary>Checks quaternion for zero magnitude.</summary> |
| | 177 | | public bool HasZeroMagnitude |
| | 178 | | { |
| | 179 | | get |
| 0 | 180 | | { |
| 0 | 181 | | return GetHasZeroMagnitude(this); |
| 0 | 182 | | } |
| | 183 | | } |
| | 184 | |
|
| | 185 | | #endregion |
| | 186 | |
|
| | 187 | | #region Magnitude |
| | 188 | |
|
| | 189 | | /// <summary>Computes the magnitude of this quaternion.</summary> |
| | 190 | | /// <param name="a">The <see cref="Quaternion{T}"/> to get the magnitude of.</param> |
| | 191 | | /// <returns>The magnitude of this quaternion.</returns> |
| | 192 | | public static T GetMagnitude(Quaternion<T> a) |
| 12 | 193 | | { |
| 12 | 194 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 12 | 195 | | return SquareRoot(GetMagnitudeSquared(a)); |
| 12 | 196 | | } |
| | 197 | |
|
| | 198 | | /// <summary>Computes the magnitude of this quaternion.</summary> |
| 12 | 199 | | public T Magnitude => GetMagnitude(this); |
| | 200 | |
|
| | 201 | | #endregion |
| | 202 | |
|
| | 203 | | #region MagnitudeSquared |
| | 204 | |
|
| | 205 | | /// <summary>Computes the magnitude of this quaternion, but doesn't square root it for |
| | 206 | | /// possible optimization purposes.</summary> |
| | 207 | | /// <param name="a">The <see cref="Quaternion{T}"/> to get the magnitude squared of.</param> |
| | 208 | | /// <returns>The squared length of the quaternion.</returns> |
| | 209 | | public static T GetMagnitudeSquared(Quaternion<T> a) |
| 18 | 210 | | { |
| 18 | 211 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 18 | 212 | | return GetMagnitudeSquaredImplementation.Function(a._x, a._y, a._z, a._w); |
| 18 | 213 | | } |
| | 214 | |
|
| | 215 | | internal static class GetMagnitudeSquaredImplementation |
| | 216 | | { |
| 3 | 217 | | internal static Func<T, T, T, T, T> Function = (T x, T y, T z, T w) => |
| 3 | 218 | | { |
| 3 | 219 | | ParameterExpression X = Expression.Parameter(typeof(T)); |
| 3 | 220 | | ParameterExpression Y = Expression.Parameter(typeof(T)); |
| 3 | 221 | | ParameterExpression Z = Expression.Parameter(typeof(T)); |
| 3 | 222 | | ParameterExpression W = Expression.Parameter(typeof(T)); |
| 3 | 223 | | Expression BODY = |
| 3 | 224 | | Expression.Add(Expression.Multiply(X, X), |
| 3 | 225 | | Expression.Add(Expression.Multiply(Y, Y), |
| 3 | 226 | | Expression.Add(Expression.Multiply(Z, Z), |
| 3 | 227 | | Expression.Multiply(W, W)))); |
| 3 | 228 | | Function = Expression.Lambda<Func<T, T, T, T, T>>(BODY, X, Y, Z, W).Compile(); |
| 3 | 229 | | return Function(x, y, z, w); |
| 6 | 230 | | }; |
| | 231 | | } |
| | 232 | |
|
| | 233 | | /// <summary>Computes the magnitude of this quaternion, but doesn't square root it for |
| | 234 | | /// possible optimization purposes.</summary> |
| 6 | 235 | | public T MagnitudeSquared => GetMagnitudeSquared(this); |
| | 236 | |
|
| | 237 | | #endregion |
| | 238 | |
|
| | 239 | | #region Add |
| | 240 | |
|
| | 241 | | /// <summary>Adds two quaternions together.</summary> |
| | 242 | | /// <param name="a">The first quaternion of the addition.</param> |
| | 243 | | /// <param name="b">The second quaternion of the addiiton.</param> |
| | 244 | | /// <param name="c">The result of the addition.</param> |
| | 245 | | public static void Add(Quaternion<T> a, Quaternion<T> b, ref Quaternion<T>? c) |
| 8 | 246 | | { |
| 8 | 247 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 8 | 248 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 8 | 249 | | T x = Addition(a._x, b._x); |
| 8 | 250 | | T y = Addition(a._y, b._y); |
| 8 | 251 | | T z = Addition(a._z, b._z); |
| 8 | 252 | | T w = Addition(a._w, b._w); |
| 8 | 253 | | if (c is null) |
| 8 | 254 | | { |
| 8 | 255 | | c = new Quaternion<T>(x, y, z, w); |
| 8 | 256 | | } |
| | 257 | | else |
| 0 | 258 | | { |
| 0 | 259 | | c._x = x; |
| 0 | 260 | | c._y = y; |
| 0 | 261 | | c._z = z; |
| 0 | 262 | | c._w = w; |
| 0 | 263 | | } |
| 8 | 264 | | } |
| | 265 | |
|
| | 266 | | /// <summary>Adds two quaternions together.</summary> |
| | 267 | | /// <param name="a">The first vector of the addition.</param> |
| | 268 | | /// <param name="b">The second vector of the addiiton.</param> |
| | 269 | | /// <returns>The result of the addition.</returns> |
| | 270 | | public static Quaternion<T> Add(Quaternion<T> a, Quaternion<T> b) |
| 8 | 271 | | { |
| 8 | 272 | | Quaternion<T>? c = null; |
| 8 | 273 | | Add(a, b, ref c); |
| 8 | 274 | | return c!; |
| 8 | 275 | | } |
| | 276 | |
|
| | 277 | | /// <summary>Adds two quaternions together.</summary> |
| | 278 | | /// <param name="a">The first quaternion of the addition.</param> |
| | 279 | | /// <param name="b">The second quaternion of the addition.</param> |
| | 280 | | /// <returns>The result of the addition.</returns> |
| 8 | 281 | | public static Quaternion<T> operator +(Quaternion<T> a, Quaternion<T> b) => Add(a, b); |
| | 282 | |
|
| | 283 | | /// <summary>Adds two quaternions together.</summary> |
| | 284 | | /// <param name="b">The second quaternion of the addititon.</param> |
| | 285 | | /// <param name="c">The result of the addition.</param> |
| 0 | 286 | | public void Add(Quaternion<T> b, ref Quaternion<T>? c) => Add(this, b, ref c); |
| | 287 | |
|
| | 288 | | /// <summary>Adds two quaternions together.</summary> |
| | 289 | | /// <param name="b">The quaternion to add to this one.</param> |
| | 290 | | /// <returns>The result of the addition.</returns> |
| 0 | 291 | | public Quaternion<T> Add(Quaternion<T> b) => this + b; |
| | 292 | |
|
| | 293 | | #endregion |
| | 294 | |
|
| | 295 | | #region Subtract |
| | 296 | |
|
| | 297 | | /// <summary>Subtracts two quaternions.</summary> |
| | 298 | | /// <param name="a">The first quaternion of the subtraction.</param> |
| | 299 | | /// <param name="b">The second quaternion of the subtraction.</param> |
| | 300 | | /// <param name="c">The result of the subtraction.</param> |
| | 301 | | public static void Subtract(Quaternion<T> a, Quaternion<T> b, ref Quaternion<T>? c) |
| 8 | 302 | | { |
| 8 | 303 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 8 | 304 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 8 | 305 | | T x = Subtraction(a._x, b._x); |
| 8 | 306 | | T y = Subtraction(a._y, b._y); |
| 8 | 307 | | T z = Subtraction(a._z, b._z); |
| 8 | 308 | | T w = Subtraction(a._w, b._w); |
| 8 | 309 | | if (c is null) |
| 8 | 310 | | { |
| 8 | 311 | | c = new Quaternion<T>(x, y, z, w); |
| 8 | 312 | | } |
| | 313 | | else |
| 0 | 314 | | { |
| 0 | 315 | | c._x = x; |
| 0 | 316 | | c._y = y; |
| 0 | 317 | | c._z = z; |
| 0 | 318 | | c._w = w; |
| 0 | 319 | | } |
| 8 | 320 | | } |
| | 321 | |
|
| | 322 | | /// <summary>Subtracts two quaternions.</summary> |
| | 323 | | /// <param name="a">The first vector of the subtraction.</param> |
| | 324 | | /// <param name="b">The second vector of the subtraction.</param> |
| | 325 | | /// <returns>The result of the subtraction.</returns> |
| | 326 | | public static Quaternion<T> Subtract(Quaternion<T> a, Quaternion<T> b) |
| 8 | 327 | | { |
| 8 | 328 | | Quaternion<T>? c = null; |
| 8 | 329 | | Subtract(a, b, ref c); |
| 8 | 330 | | return c!; |
| 8 | 331 | | } |
| | 332 | |
|
| | 333 | | /// <summary>Subtracts two quaternions.</summary> |
| | 334 | | /// <param name="a">The first quaternion of the subtraction.</param> |
| | 335 | | /// <param name="b">The second quaternion of the subtraction.</param> |
| | 336 | | /// <returns>The result of the subtraction.</returns> |
| 8 | 337 | | public static Quaternion<T> operator -(Quaternion<T> a, Quaternion<T> b) => Subtract(a, b); |
| | 338 | |
|
| | 339 | | /// <summary>Subtracts two quaternions.</summary> |
| | 340 | | /// <param name="b">The second quaternion of the subtraction.</param> |
| | 341 | | /// <param name="c">The result of the subtraction.</param> |
| 0 | 342 | | public void Subtract(Quaternion<T> b, ref Quaternion<T>? c) => Subtract(this, b, ref c); |
| | 343 | |
|
| | 344 | | /// <summary>Subtracts two quaternions together.</summary> |
| | 345 | | /// <param name="b">The second quaternion of the subtraction.</param> |
| | 346 | | /// <returns>The result of the subtraction.</returns> |
| 0 | 347 | | public Quaternion<T> Subtract(Quaternion<T> b) => this - b; |
| | 348 | |
|
| | 349 | | #endregion |
| | 350 | |
|
| | 351 | | #region Multiply (Quaternion * Quaternion) |
| | 352 | |
|
| | 353 | | /// <summary>Multiplies two quaternions.</summary> |
| | 354 | | /// <param name="a">The first quaternion of the multiplication.</param> |
| | 355 | | /// <param name="b">The second quaternion of the multiplication.</param> |
| | 356 | | /// <param name="c">The resulting quaternion after the multiplication.</param> |
| | 357 | | internal static void Multiply(Quaternion<T> a, Quaternion<T> b, ref Quaternion<T>? c) |
| 0 | 358 | | { |
| 0 | 359 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 360 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 0 | 361 | | T x = QuaternionMiltiplyXYZComponentImplementation.Function(a.X, b.W, a.W, b.X, a.Y, b.Z, a.Z, b.Y); |
| 0 | 362 | | T y = QuaternionMiltiplyXYZComponentImplementation.Function(a.Y, b.W, a.W, b.Y, a.Z, b.X, a.X, b.Z); |
| 0 | 363 | | T z = QuaternionMiltiplyXYZComponentImplementation.Function(a.Z, b.W, a.W, b.Z, a.X, b.Y, a.Y, b.X); |
| 0 | 364 | | T w = QuaternionMiltiplyWComponentImplementation.Function(a.W, b.W, a.X, b.X, a.Y, b.Y, a.Z, b.Z); |
| 0 | 365 | | if (c is null) |
| 0 | 366 | | { |
| 0 | 367 | | c = new Quaternion<T>(x, y, z, w); |
| 0 | 368 | | } |
| | 369 | | else |
| 0 | 370 | | { |
| 0 | 371 | | c._x = x; |
| 0 | 372 | | c._y = y; |
| 0 | 373 | | c._z = z; |
| 0 | 374 | | c._w = w; |
| 0 | 375 | | } |
| 0 | 376 | | } |
| | 377 | |
|
| | 378 | | internal static class QuaternionMiltiplyXYZComponentImplementation |
| | 379 | | { |
| 0 | 380 | | internal static Func<T, T, T, T, T, T, T, T, T> Function = (T a, T b, T c, T d, T e, T f, T g, T h) => |
| 0 | 381 | | { |
| 0 | 382 | | // parameters |
| 0 | 383 | | ParameterExpression A = Expression.Parameter(typeof(T)); |
| 0 | 384 | | ParameterExpression B = Expression.Parameter(typeof(T)); |
| 0 | 385 | | ParameterExpression C = Expression.Parameter(typeof(T)); |
| 0 | 386 | | ParameterExpression D = Expression.Parameter(typeof(T)); |
| 0 | 387 | | ParameterExpression E = Expression.Parameter(typeof(T)); |
| 0 | 388 | | ParameterExpression F = Expression.Parameter(typeof(T)); |
| 0 | 389 | | ParameterExpression G = Expression.Parameter(typeof(T)); |
| 0 | 390 | | ParameterExpression H = Expression.Parameter(typeof(T)); |
| 0 | 391 | | // multiply |
| 0 | 392 | | Expression AB = Expression.Multiply(A, B); |
| 0 | 393 | | Expression CD = Expression.Multiply(C, D); |
| 0 | 394 | | Expression EF = Expression.Multiply(E, F); |
| 0 | 395 | | Expression GH = Expression.Multiply(G, H); |
| 0 | 396 | | // add |
| 0 | 397 | | Expression AB_add_CD = Expression.Add(AB, CD); |
| 0 | 398 | | Expression AB_add_CD_add_EF = Expression.Add(AB_add_CD, EF); |
| 0 | 399 | | // subtract |
| 0 | 400 | | Expression AB_add_CD_add_EF_subtract_GH = Expression.Subtract(AB_add_CD_add_EF, GH); |
| 0 | 401 | | // compile |
| 0 | 402 | | Expression BODY = AB_add_CD_add_EF_subtract_GH; |
| 0 | 403 | | Function = Expression.Lambda<Func<T, T, T, T, T, T, T, T, T>>(BODY, A, B, C, D, E, F, G, H).Compile(); |
| 0 | 404 | | return Function(a, b, c, d, e, f, g, h); |
| 0 | 405 | | }; |
| | 406 | | } |
| | 407 | |
|
| | 408 | | internal static class QuaternionMiltiplyWComponentImplementation |
| | 409 | | { |
| 0 | 410 | | internal static Func<T, T, T, T, T, T, T, T, T> Function = (T a, T b, T c, T d, T e, T f, T g, T h) => |
| 0 | 411 | | { |
| 0 | 412 | | // parameters |
| 0 | 413 | | ParameterExpression A = Expression.Parameter(typeof(T)); |
| 0 | 414 | | ParameterExpression B = Expression.Parameter(typeof(T)); |
| 0 | 415 | | ParameterExpression C = Expression.Parameter(typeof(T)); |
| 0 | 416 | | ParameterExpression D = Expression.Parameter(typeof(T)); |
| 0 | 417 | | ParameterExpression E = Expression.Parameter(typeof(T)); |
| 0 | 418 | | ParameterExpression F = Expression.Parameter(typeof(T)); |
| 0 | 419 | | ParameterExpression G = Expression.Parameter(typeof(T)); |
| 0 | 420 | | ParameterExpression H = Expression.Parameter(typeof(T)); |
| 0 | 421 | | // multiply |
| 0 | 422 | | Expression AB = Expression.Multiply(A, B); |
| 0 | 423 | | Expression CD = Expression.Multiply(C, D); |
| 0 | 424 | | Expression EF = Expression.Multiply(E, F); |
| 0 | 425 | | Expression GH = Expression.Multiply(G, H); |
| 0 | 426 | | // subtract |
| 0 | 427 | | Expression AB_subtract_CD = Expression.Subtract(AB, CD); |
| 0 | 428 | | Expression AB_subtract_CD_subtract_EF = Expression.Subtract(AB_subtract_CD, EF); |
| 0 | 429 | | Expression AB_subtract_CD_subtract_EF_subtract_GH = Expression.Subtract(AB_subtract_CD_subtract_EF, GH); |
| 0 | 430 | | // compile |
| 0 | 431 | | Expression BODY = AB_subtract_CD_subtract_EF_subtract_GH; |
| 0 | 432 | | Function = Expression.Lambda<Func<T, T, T, T, T, T, T, T, T>>(BODY, A, B, C, D, E, F, G, H).Compile(); |
| 0 | 433 | | return Function(a, b, c, d, e, f, g, h); |
| 0 | 434 | | }; |
| | 435 | | } |
| | 436 | |
|
| | 437 | | /// <summary>Multiplies two quaternions.</summary> |
| | 438 | | /// <param name="a">The first quaternion of the multiplication.</param> |
| | 439 | | /// <param name="b">The second quaternion of the multiplication.</param> |
| | 440 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| | 441 | | public static Quaternion<T> Multiply(Quaternion<T> a, Quaternion<T> b) |
| 0 | 442 | | { |
| 0 | 443 | | Quaternion<T>? c = null; |
| 0 | 444 | | Multiply(a, b, ref c); |
| 0 | 445 | | return c!; |
| 0 | 446 | | } |
| | 447 | |
|
| | 448 | | /// <summary>Multiplies two quaternions.</summary> |
| | 449 | | /// <param name="a">The first quaternion of the multiplication.</param> |
| | 450 | | /// <param name="b">The second quaternion of the multiplication.</param> |
| | 451 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| 0 | 452 | | public static Quaternion<T> operator *(Quaternion<T> a, Quaternion<T> b) => Multiply(a, b); |
| | 453 | |
|
| | 454 | | /// <summary>Multiplies two quaternions.</summary> |
| | 455 | | /// <param name="b">The second quaternion of the multiplication.</param> |
| | 456 | | /// <param name="c">The resulting quaternion after the multiplication.</param> |
| 0 | 457 | | public void Multiply(Quaternion<T> b, ref Quaternion<T>? c) => Multiply(this, b, ref c); |
| | 458 | |
|
| | 459 | | /// <summary>Multiplies two quaternions.</summary> |
| | 460 | | /// <param name="b">The second quaternion of the multiplication.</param> |
| | 461 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| 0 | 462 | | public Quaternion<T> Multiply(Quaternion<T> b) => this * b; |
| | 463 | |
|
| | 464 | | #endregion |
| | 465 | |
|
| | 466 | | #region Multiply (Quaternion * Vector) |
| | 467 | |
|
| | 468 | | /// <summary>Multiplies a quaternion and a vector.</summary> |
| | 469 | | /// <param name="a">The quaternion of the multiplication.</param> |
| | 470 | | /// <param name="b">The vector of the multiplication.</param> |
| | 471 | | /// <param name="c">The resulting quaternion after the multiplication.</param> |
| | 472 | | public static void Multiply(Quaternion<T> a, Vector<T> b, ref Quaternion<T>? c) |
| 0 | 473 | | { |
| 0 | 474 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 475 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 0 | 476 | | if (sourceof(b.Dimensions != 3, out string c1)) throw new ArgumentException(c1); |
| 0 | 477 | | T x = QuaternionMiltiplyVectorXYZComponentImplementation.Function(a.W, b.X, a.Y, b.Z, a.Z, b.Y); |
| 0 | 478 | | T y = QuaternionMiltiplyVectorXYZComponentImplementation.Function(a.W, b.Y, a.Z, b.X, a.X, b.Z); |
| 0 | 479 | | T z = QuaternionMiltiplyVectorXYZComponentImplementation.Function(a.W, b.Z, a.X, b.Y, a.Y, b.X); |
| 0 | 480 | | T w = QuaternionMiltiplyVectorWComponentImplementation.Function(a.X, b.X, a.Y, b.Y, a.Z, b.Z); |
| 0 | 481 | | if (c is null) |
| 0 | 482 | | { |
| 0 | 483 | | c = new Quaternion<T>(x, y, z, w); |
| 0 | 484 | | } |
| | 485 | | else |
| 0 | 486 | | { |
| 0 | 487 | | c._x = x; |
| 0 | 488 | | c._y = y; |
| 0 | 489 | | c._z = z; |
| 0 | 490 | | c._w = w; |
| 0 | 491 | | } |
| 0 | 492 | | } |
| | 493 | |
|
| | 494 | | internal static class QuaternionMiltiplyVectorXYZComponentImplementation |
| | 495 | | { |
| 0 | 496 | | internal static Func<T, T, T, T, T, T, T> Function = (T a, T b, T c, T d, T e, T f) => |
| 0 | 497 | | { |
| 0 | 498 | | // parameters |
| 0 | 499 | | ParameterExpression A = Expression.Parameter(typeof(T)); |
| 0 | 500 | | ParameterExpression B = Expression.Parameter(typeof(T)); |
| 0 | 501 | | ParameterExpression C = Expression.Parameter(typeof(T)); |
| 0 | 502 | | ParameterExpression D = Expression.Parameter(typeof(T)); |
| 0 | 503 | | ParameterExpression E = Expression.Parameter(typeof(T)); |
| 0 | 504 | | ParameterExpression F = Expression.Parameter(typeof(T)); |
| 0 | 505 | | // multiply |
| 0 | 506 | | Expression AB = Expression.Multiply(A, B); |
| 0 | 507 | | Expression CD = Expression.Multiply(C, D); |
| 0 | 508 | | Expression EF = Expression.Multiply(E, F); |
| 0 | 509 | | // add |
| 0 | 510 | | Expression AB_add_CD = Expression.Add(AB, CD); |
| 0 | 511 | | // subtract |
| 0 | 512 | | Expression AB_add_CD_subtract_EF = Expression.Subtract(AB_add_CD, EF); |
| 0 | 513 | | // compile |
| 0 | 514 | | Expression BODY = AB_add_CD_subtract_EF; |
| 0 | 515 | | Function = Expression.Lambda<Func<T, T, T, T, T, T, T>>(BODY, A, B, C, D, E, F).Compile(); |
| 0 | 516 | | return Function(a, b, c, d, e, f); |
| 0 | 517 | | }; |
| | 518 | | } |
| | 519 | |
|
| | 520 | | internal static class QuaternionMiltiplyVectorWComponentImplementation |
| | 521 | | { |
| 0 | 522 | | internal static Func<T, T, T, T, T, T, T> Function = (T a, T b, T c, T d, T e, T f) => |
| 0 | 523 | | { |
| 0 | 524 | | // parameters |
| 0 | 525 | | ParameterExpression A = Expression.Parameter(typeof(T)); |
| 0 | 526 | | ParameterExpression B = Expression.Parameter(typeof(T)); |
| 0 | 527 | | ParameterExpression C = Expression.Parameter(typeof(T)); |
| 0 | 528 | | ParameterExpression D = Expression.Parameter(typeof(T)); |
| 0 | 529 | | ParameterExpression E = Expression.Parameter(typeof(T)); |
| 0 | 530 | | ParameterExpression F = Expression.Parameter(typeof(T)); |
| 0 | 531 | | // multiply |
| 0 | 532 | | Expression nAB = Expression.Multiply(Expression.Negate(A), B); |
| 0 | 533 | | Expression CD = Expression.Multiply(C, D); |
| 0 | 534 | | Expression EF = Expression.Multiply(E, F); |
| 0 | 535 | | // subtract |
| 0 | 536 | | Expression nAB_subtract_CD = Expression.Subtract(nAB, CD); |
| 0 | 537 | | Expression nAB_subtract_CD_subtract_EF = Expression.Subtract(nAB_subtract_CD, EF); |
| 0 | 538 | | // compile |
| 0 | 539 | | Expression BODY = nAB_subtract_CD_subtract_EF; |
| 0 | 540 | | Function = Expression.Lambda<Func<T, T, T, T, T, T, T>>(BODY, A, B, C, D, E, F).Compile(); |
| 0 | 541 | | return Function(a, b, c, d, e, f); |
| 0 | 542 | | }; |
| | 543 | | } |
| | 544 | |
|
| | 545 | | /// <summary>Multiplies a quaternion and a vector.</summary> |
| | 546 | | /// <param name="a">The quaternion of the multiplication.</param> |
| | 547 | | /// <param name="b">The vector of the multiplication.</param> |
| | 548 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| | 549 | | public static Quaternion<T> Multiply(Quaternion<T> a, Vector<T> b) |
| 0 | 550 | | { |
| 0 | 551 | | Quaternion<T>? c = null; |
| 0 | 552 | | Multiply(a, b, ref c); |
| 0 | 553 | | return c!; |
| 0 | 554 | | } |
| | 555 | |
|
| | 556 | | /// <summary>Multiplies a quaternion and a vector.</summary> |
| | 557 | | /// <param name="a">The quaternion of the multiplication.</param> |
| | 558 | | /// <param name="b">The vector of the multiplication.</param> |
| | 559 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| 0 | 560 | | public static Quaternion<T> operator *(Quaternion<T> a, Vector<T> b) => Multiply(a, b); |
| | 561 | |
|
| | 562 | | /// <summary>Multiplies a quaternion and a vector.</summary> |
| | 563 | | /// <param name="b">The vector of the multiplication.</param> |
| | 564 | | /// <param name="c">The resulting quaternion after the multiplication.</param> |
| 0 | 565 | | public void Multiply(Vector<T> b, ref Quaternion<T>? c) => Multiply(this, b, ref c); |
| | 566 | |
|
| | 567 | | /// <summary>Multiplies a quaternion and a vector.</summary> |
| | 568 | | /// <param name="b">The vector of the multiplication.</param> |
| | 569 | | /// <returns>The resulting quaternion after the multiplication.</returns> |
| 0 | 570 | | public Quaternion<T> Multiply(Vector<T> b) => this * b; |
| | 571 | |
|
| | 572 | | #endregion |
| | 573 | |
|
| | 574 | | #region Multiply (Quaternion * Scalar) |
| | 575 | |
|
| | 576 | | /// <summary>Multiplies all the values in a quaternion by a scalar.</summary> |
| | 577 | | /// <param name="a">The quaternion to have the values multiplied.</param> |
| | 578 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 579 | | /// <param name="c">The resulting quaternion after the multiplications.</param> |
| | 580 | | internal static void Multiply(Quaternion<T> a, T b, ref Quaternion<T>? c) |
| 8 | 581 | | { |
| 8 | 582 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 8 | 583 | | T x = Multiplication(a._x, b); |
| 8 | 584 | | T y = Multiplication(a._y, b); |
| 8 | 585 | | T z = Multiplication(a._z, b); |
| 8 | 586 | | T w = Multiplication(a._w, b); |
| 8 | 587 | | if (c is null) |
| 8 | 588 | | { |
| 8 | 589 | | c = new Quaternion<T>(x, y, z, w); |
| 8 | 590 | | } |
| | 591 | | else |
| 0 | 592 | | { |
| 0 | 593 | | c._x = x; |
| 0 | 594 | | c._y = y; |
| 0 | 595 | | c._z = z; |
| 0 | 596 | | c._w = w; |
| 0 | 597 | | } |
| 8 | 598 | | } |
| | 599 | |
|
| | 600 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 601 | | /// <param name="a">The matrix to have the values multiplied.</param> |
| | 602 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 603 | | /// <returns>The resulting quaternion after the multiplications.</returns> |
| | 604 | | public static Quaternion<T> Multiply(Quaternion<T> a, T b) |
| 8 | 605 | | { |
| 8 | 606 | | Quaternion<T>? c = null; |
| 8 | 607 | | Multiply(a, b, ref c); |
| 8 | 608 | | return c!; |
| 8 | 609 | | } |
| | 610 | |
|
| | 611 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 612 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 613 | | /// <param name="a">The quaternion to have the values multiplied.</param> |
| | 614 | | /// <returns>The resulting quaternion after the multiplications.</returns> |
| 0 | 615 | | public static Quaternion<T> Multiply(T b, Quaternion<T> a) => Multiply(a, b); |
| | 616 | |
|
| | 617 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 618 | | /// <param name="a">The quaternion to have the values multiplied.</param> |
| | 619 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 620 | | /// <returns>The resulting quaternion after the multiplications.</returns> |
| 8 | 621 | | public static Quaternion<T> operator *(Quaternion<T> a, T b) => Multiply(a, b); |
| | 622 | |
|
| | 623 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 624 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 625 | | /// <param name="a">The quaternion to have the values multiplied.</param> |
| | 626 | | /// <returns>The resulting quaternion after the multiplications.</returns> |
| 0 | 627 | | public static Quaternion<T> operator *(T b, Quaternion<T> a) => Multiply(b, a); |
| | 628 | |
|
| | 629 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 630 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 631 | | /// <param name="c">The resulting quaternion after the multiplications.</param> |
| 0 | 632 | | public void Multiply(T b, ref Quaternion<T>? c) => Multiply(this, b, ref c); |
| | 633 | |
|
| | 634 | | /// <summary>Multiplies all the values in a matrix by a scalar.</summary> |
| | 635 | | /// <param name="b">The scalar to multiply the values by.</param> |
| | 636 | | /// <returns>The resulting matrix after the multiplications.</returns> |
| 0 | 637 | | public Quaternion<T> Multiply(T b) => this * b; |
| | 638 | |
|
| | 639 | | #endregion |
| | 640 | |
|
| | 641 | | #region Rotate |
| | 642 | |
|
| | 643 | | /// <summary>Rotates a vector by a quaternion rotation [v' = qvq'].</summary> |
| | 644 | | /// <param name="a">The quaternion rotation to rotate the vector by.</param> |
| | 645 | | /// <param name="b">The vector to rotate.</param> |
| | 646 | | /// <param name="c">The result of the rotation.</param> |
| | 647 | | public static void Rotate(Quaternion<T> a, Vector<T> b, ref Vector<T>? c) |
| 0 | 648 | | { |
| 0 | 649 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 650 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 0 | 651 | | if (sourceof(b.Dimensions != 3, out string c1)) throw new ArgumentException(c1); |
| 0 | 652 | | if (c is null || c.Dimensions != 3) |
| 0 | 653 | | { |
| 0 | 654 | | c = new Vector<T>(3); |
| 0 | 655 | | } |
| 0 | 656 | | Quaternion<T>? d = null; |
| 0 | 657 | | Multiply(a, b, ref d); |
| 0 | 658 | | Multiply(a.Conjugate(), d!, ref d!); // need to prevent this heep allocation on "a.Conjugate()" in future update |
| 0 | 659 | | c.X = d.X; |
| 0 | 660 | | c.Y = d.Y; |
| 0 | 661 | | c.X = d.W; |
| 0 | 662 | | } |
| | 663 | |
|
| | 664 | | /// <summary>Rotates a vector by a quaternion rotation [v' = qvq'].</summary> |
| | 665 | | /// <param name="a">The quaternion rotation to rotate the vector by.</param> |
| | 666 | | /// <param name="b">The vector to rotate.</param> |
| | 667 | | /// <returns>The result of the rotation.</returns> |
| | 668 | | public static Vector<T> Rotate(Quaternion<T> a, Vector<T> b) |
| 0 | 669 | | { |
| 0 | 670 | | Vector<T>? c = null; |
| 0 | 671 | | Rotate(a, b, ref c); |
| 0 | 672 | | return c!; |
| 0 | 673 | | } |
| | 674 | |
|
| | 675 | | /// <summary>Rotates a vector by a quaternion rotation [v' = qvq'].</summary> |
| | 676 | | /// <param name="b">The vector to rotate.</param> |
| | 677 | | /// <param name="c">The result of the rotation.</param> |
| | 678 | | public void Rotate(Vector<T> b, ref Vector<T>? c) |
| 0 | 679 | | { |
| 0 | 680 | | Rotate(this, b, ref c); |
| 0 | 681 | | } |
| | 682 | |
|
| | 683 | | /// <summary>Rotates a vector by a quaternion rotation [v' = qvq'].</summary> |
| | 684 | | /// <param name="b">The vector to rotate.</param> |
| | 685 | | /// <returns>The result of the rotation.</returns> |
| | 686 | | public Vector<T> Rotate(Vector<T> b) |
| 0 | 687 | | { |
| 0 | 688 | | Vector<T>? c = null; |
| 0 | 689 | | Rotate(this, b, ref c); |
| 0 | 690 | | return c!; |
| 0 | 691 | | } |
| | 692 | |
|
| | 693 | | #endregion |
| | 694 | |
|
| | 695 | | #region Conjugate |
| | 696 | |
|
| | 697 | | /// <summary>Conjugates a quaternion.</summary> |
| | 698 | | /// <param name="a">The quaternion to conjugate.</param> |
| | 699 | | /// <param name="b">The result of the conjugation.</param> |
| | 700 | | public static void Conjugate(Quaternion<T> a, ref Quaternion<T>? b) |
| 8 | 701 | | { |
| 8 | 702 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 8 | 703 | | T x = Negation(a._x); |
| 8 | 704 | | T y = Negation(a._y); |
| 8 | 705 | | T z = Negation(a._z); |
| 8 | 706 | | T w = a._w; |
| 8 | 707 | | if (b is null) |
| 8 | 708 | | { |
| 8 | 709 | | b = new Quaternion<T>(x, y, z, w); |
| 8 | 710 | | } |
| | 711 | | else |
| 0 | 712 | | { |
| 0 | 713 | | b._x = x; |
| 0 | 714 | | b._y = y; |
| 0 | 715 | | b._z = z; |
| 0 | 716 | | b._w = w; |
| 0 | 717 | | } |
| 8 | 718 | | } |
| | 719 | |
|
| | 720 | | /// <summary>Conjugates a quaternion.</summary> |
| | 721 | | /// <param name="a">The quaternion to conjugate.</param> |
| | 722 | | /// <returns>The result of the conjugation.</returns> |
| | 723 | | public static Quaternion<T> Conjugate(Quaternion<T> a) |
| 0 | 724 | | { |
| 0 | 725 | | Quaternion<T>? b = null; |
| 0 | 726 | | Conjugate(a, ref b); |
| 0 | 727 | | return b!; |
| 0 | 728 | | } |
| | 729 | |
|
| | 730 | | /// <summary>Conjugates a quaternion.</summary> |
| | 731 | | /// <param name="b">The result of the conjugation.</param> |
| 0 | 732 | | public void Conjugate(ref Quaternion<T>? b) => Conjugate(this, ref b); |
| | 733 | |
|
| | 734 | | /// <summary>Conjugates a quaternion.</summary> |
| | 735 | | /// <returns>The result of the conjugation.</returns> |
| | 736 | | public Quaternion<T> Conjugate() |
| 8 | 737 | | { |
| 8 | 738 | | Quaternion<T>? b = null; |
| 8 | 739 | | Conjugate(this, ref b); |
| 8 | 740 | | return b!; |
| 8 | 741 | | } |
| | 742 | |
|
| | 743 | | #endregion |
| | 744 | |
|
| | 745 | | #region Normalize |
| | 746 | |
|
| | 747 | | /// <summary>Normalizes a quaternion.</summary> |
| | 748 | | /// <param name="a">The quaternion to normalize.</param> |
| | 749 | | /// <param name="b">The result of the normalization.</param> |
| | 750 | | public static void Normalize(Quaternion<T> a, ref Quaternion<T>? b) |
| 6 | 751 | | { |
| 6 | 752 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 6 | 753 | | T magnitude = a.Magnitude; |
| 6 | 754 | | T x = Division(a._x, magnitude); |
| 6 | 755 | | T y = Division(a._y, magnitude); |
| 6 | 756 | | T z = Division(a._z, magnitude); |
| 6 | 757 | | T w = Division(a._w, magnitude); |
| 6 | 758 | | if (b is null) |
| 6 | 759 | | { |
| 6 | 760 | | b = new Quaternion<T>(x, y, z, w); |
| 6 | 761 | | } |
| | 762 | | else |
| 0 | 763 | | { |
| 0 | 764 | | b._x = x; |
| 0 | 765 | | b._y = y; |
| 0 | 766 | | b._z = z; |
| 0 | 767 | | b._w = w; |
| 0 | 768 | | } |
| 6 | 769 | | } |
| | 770 | |
|
| | 771 | | /// <summary>Normalizes a quaternion.</summary> |
| | 772 | | /// <param name="a">The quaternion to normalize.</param> |
| | 773 | | /// <returns>The result of the normalization.</returns> |
| | 774 | | public static Quaternion<T> Normalize(Quaternion<T> a) |
| 0 | 775 | | { |
| 0 | 776 | | Quaternion<T>? b = null; |
| 0 | 777 | | Normalize(a, ref b); |
| 0 | 778 | | return b!; |
| 0 | 779 | | } |
| | 780 | |
|
| | 781 | | /// <summary>Normalizes a quaternion.</summary> |
| | 782 | | /// <param name="b">The result of the normalization.</param> |
| 0 | 783 | | public void Normalize(ref Quaternion<T>? b) => Normalize(this, ref b); |
| | 784 | |
|
| | 785 | | /// <summary>Normalizes a quaternion.</summary> |
| | 786 | | /// <returns>The result of the normalization.</returns> |
| | 787 | | public Quaternion<T> Normalize() |
| 6 | 788 | | { |
| 6 | 789 | | Quaternion<T>? b = null; |
| 6 | 790 | | Normalize(this, ref b); |
| 6 | 791 | | return b!; |
| 6 | 792 | | } |
| | 793 | |
|
| | 794 | | #endregion |
| | 795 | |
|
| | 796 | | #region Invert |
| | 797 | |
|
| | 798 | | /// <summary>Inverts a quaternion.</summary> |
| | 799 | | /// <param name="a">The quaternion to invert.</param> |
| | 800 | | /// <param name="b">The result of the inversion.</param> |
| | 801 | | public static void Invert(Quaternion<T> a, ref Quaternion<T>? b) |
| 0 | 802 | | { |
| | 803 | | // Note: I think this function is incorrect. Need to research. |
| | 804 | |
|
| 0 | 805 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 806 | | T magnitudeSquared = a.MagnitudeSquared; |
| | 807 | | T x; |
| | 808 | | T y; |
| | 809 | | T z; |
| | 810 | | T w; |
| 0 | 811 | | if (Equate(magnitudeSquared, Constant<T>.Zero)) |
| 0 | 812 | | { |
| 0 | 813 | | x = a._x; |
| 0 | 814 | | y = a._y; |
| 0 | 815 | | z = a._z; |
| 0 | 816 | | w = a._w; |
| 0 | 817 | | } |
| | 818 | | else |
| 0 | 819 | | { |
| 0 | 820 | | x = Multiplication(Negation(a.X), magnitudeSquared); |
| 0 | 821 | | y = Multiplication(Negation(a.Y), magnitudeSquared); |
| 0 | 822 | | z = Multiplication(Negation(a.Z), magnitudeSquared); |
| 0 | 823 | | w = Multiplication(a.W, magnitudeSquared); |
| 0 | 824 | | } |
| 0 | 825 | | if (b is null) |
| 0 | 826 | | { |
| 0 | 827 | | b = new Quaternion<T>(x, y, z, w); |
| 0 | 828 | | } |
| | 829 | | else |
| 0 | 830 | | { |
| 0 | 831 | | b._x = x; |
| 0 | 832 | | b._y = y; |
| 0 | 833 | | b._z = z; |
| 0 | 834 | | b._w = w; |
| 0 | 835 | | } |
| 0 | 836 | | } |
| | 837 | |
|
| | 838 | | /// <summary>Inverts a quaternion.</summary> |
| | 839 | | /// <param name="a">The quaternion to invert.</param> |
| | 840 | | /// <returns>The result of the inversion.</returns> |
| | 841 | | public static Quaternion<T> Invert(Quaternion<T> a) |
| 0 | 842 | | { |
| 0 | 843 | | Quaternion<T>? b = null; |
| 0 | 844 | | Invert(a, ref b); |
| 0 | 845 | | return b!; |
| 0 | 846 | | } |
| | 847 | |
|
| | 848 | | /// <summary>Inverts a quaternion.</summary> |
| | 849 | | /// <param name="b">The result of the inversion.</param> |
| 0 | 850 | | public void Invert(ref Quaternion<T>? b) => Invert(this, ref b); |
| | 851 | |
|
| | 852 | | /// <summary>Inverts a quaternion.</summary> |
| | 853 | | /// <returns>The result of the inversion.</returns> |
| | 854 | | public Quaternion<T> Invert() |
| 0 | 855 | | { |
| 0 | 856 | | Quaternion<T>? b = null; |
| 0 | 857 | | Invert(this, ref b); |
| 0 | 858 | | return b!; |
| 0 | 859 | | } |
| | 860 | |
|
| | 861 | | #endregion |
| | 862 | |
|
| | 863 | | #region LinearInterpolation |
| | 864 | |
|
| | 865 | | /// <summary>Linear interpolation for quaternions.</summary> |
| | 866 | | /// <param name="a">The min of the interpolation.</param> |
| | 867 | | /// <param name="b">The max of the interpolation.</param> |
| | 868 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 869 | | /// <param name="c">The result of the linear interpolation.</param> |
| | 870 | | public static void LinearInterpolation(Quaternion<T> a, Quaternion<T> b, T blend, ref Quaternion<T>? c) |
| 0 | 871 | | { |
| 0 | 872 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 873 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| | 874 | | #warning TODO: sourceof with generic mathematics |
| 0 | 875 | | if (LessThan(blend, Constant<T>.Zero) || GreaterThan(blend, Constant<T>.One)) |
| 0 | 876 | | { |
| 0 | 877 | | throw new ArgumentOutOfRangeException(nameof(blend), blend, "!(0 <= " + nameof(blend) + " <= 1)"); |
| | 878 | | } |
| | 879 | | T x; |
| | 880 | | T y; |
| | 881 | | T z; |
| | 882 | | T w; |
| 0 | 883 | | if (GetHasZeroMagnitude(a)) |
| 0 | 884 | | { |
| 0 | 885 | | if (GetHasZeroMagnitude(b)) |
| 0 | 886 | | { |
| 0 | 887 | | x = Constant<T>.Zero; |
| 0 | 888 | | y = Constant<T>.Zero; |
| 0 | 889 | | z = Constant<T>.Zero; |
| 0 | 890 | | w = Constant<T>.One; |
| 0 | 891 | | } |
| | 892 | | else |
| 0 | 893 | | { |
| 0 | 894 | | x = b._x; |
| 0 | 895 | | y = b._y; |
| 0 | 896 | | z = b._z; |
| 0 | 897 | | w = b._w; |
| 0 | 898 | | } |
| 0 | 899 | | } |
| 0 | 900 | | else if (GetHasZeroMagnitude(b)) |
| 0 | 901 | | { |
| 0 | 902 | | x = a._x; |
| 0 | 903 | | y = a._y; |
| 0 | 904 | | z = a._z; |
| 0 | 905 | | w = a._w; |
| 0 | 906 | | } |
| | 907 | | else |
| 0 | 908 | | { |
| 0 | 909 | | x = Addition(Subtraction(Constant<T>.One, Multiplication(blend, a.X)), Multiplication(blend, b.X)); |
| 0 | 910 | | y = Addition(Subtraction(Constant<T>.One, Multiplication(blend, a.Y)), Multiplication(blend, b.Y)); |
| 0 | 911 | | z = Addition(Subtraction(Constant<T>.One, Multiplication(blend, a.Z)), Multiplication(blend, b.Z)); |
| 0 | 912 | | w = Addition(Subtraction(Constant<T>.One, Multiplication(blend, a.W)), Multiplication(blend, b.W)); |
| 0 | 913 | | } |
| 0 | 914 | | if (c is null) |
| 0 | 915 | | { |
| 0 | 916 | | c = new Quaternion<T>(x, y, z, w); |
| 0 | 917 | | } |
| | 918 | | else |
| 0 | 919 | | { |
| 0 | 920 | | c._x = x; |
| 0 | 921 | | c._y = y; |
| 0 | 922 | | c._z = z; |
| 0 | 923 | | c._w = w; |
| 0 | 924 | | } |
| 0 | 925 | | if (!GetHasZeroMagnitude(c)) |
| 0 | 926 | | { |
| 0 | 927 | | Normalize(c, ref c); |
| 0 | 928 | | } |
| | 929 | | else |
| 0 | 930 | | { |
| 0 | 931 | | c._x = Constant<T>.Zero; |
| 0 | 932 | | c._y = Constant<T>.Zero; |
| 0 | 933 | | c._z = Constant<T>.Zero; |
| 0 | 934 | | c._w = Constant<T>.One; |
| 0 | 935 | | } |
| 0 | 936 | | } |
| | 937 | |
|
| | 938 | | /// <summary>Linear interpolation for quaternions.</summary> |
| | 939 | | /// <param name="a">The min of the interpolation.</param> |
| | 940 | | /// <param name="b">The max of the interpolation.</param> |
| | 941 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 942 | | /// <returns>The result of the linear interpolation.</returns> |
| | 943 | | public static Quaternion<T> LinearInterpolation(Quaternion<T> a, Quaternion<T> b, T blend) |
| 0 | 944 | | { |
| 0 | 945 | | Quaternion<T>? c = null; |
| 0 | 946 | | LinearInterpolation(a, b, blend, ref c); |
| 0 | 947 | | return c!; |
| 0 | 948 | | } |
| | 949 | |
|
| | 950 | | /// <summary>Linear interpolation for quaternions.</summary> |
| | 951 | | /// <param name="b">The max of the interpolation.</param> |
| | 952 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 953 | | /// <param name="c">The result of the linear interpolation.</param> |
| | 954 | | public void LinearInterpolation(Quaternion<T> b, T blend, ref Quaternion<T>? c) => |
| 0 | 955 | | LinearInterpolation(this, b, blend, ref c); |
| | 956 | |
|
| | 957 | | /// <summary>Linear interpolation for quaternions.</summary> |
| | 958 | | /// <param name="b">The max of the interpolation.</param> |
| | 959 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 960 | | /// <returns>The result of the linear interpolation.</returns> |
| | 961 | | public Quaternion<T> LinearInterpolation(Quaternion<T> b, T blend) |
| 0 | 962 | | { |
| 0 | 963 | | Quaternion<T>? c = null; |
| 0 | 964 | | LinearInterpolation(this, b, blend, ref c); |
| 0 | 965 | | return c!; |
| 0 | 966 | | } |
| | 967 | |
|
| | 968 | | #endregion |
| | 969 | |
|
| | 970 | | #region SphericalInterpolation |
| | 971 | |
|
| | 972 | | /// <summary>Spherical interpolation for quaternions.</summary> |
| | 973 | | /// <param name="a">The min of the interpolation.</param> |
| | 974 | | /// <param name="b">The max of the interpolation.</param> |
| | 975 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 976 | | /// <param name="c">The result of the spherical interpolation.</param> |
| | 977 | | #warning TODO |
| | 978 | | [Obsolete("Not Implemented", true)] |
| | 979 | | public static void SphericalInterpolation(Quaternion<T> a, Quaternion<T> b, T blend, ref Quaternion<T>? c) |
| 0 | 980 | | { |
| 0 | 981 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 982 | | if (b is null) throw new ArgumentNullException(nameof(b)); |
| 0 | 983 | | if (LessThan(blend, Constant<T>.Zero) || GreaterThan(blend, Constant<T>.One)) |
| 0 | 984 | | { |
| 0 | 985 | | throw new ArgumentOutOfRangeException(nameof(blend), blend, "!(0 <= " + nameof(blend) + " <= 1)"); |
| | 986 | | } |
| 0 | 987 | | throw new NotImplementedException(); |
| | 988 | | } |
| | 989 | |
|
| | 990 | | /// <summary>Spherical interpolation for quaternions.</summary> |
| | 991 | | /// <param name="a">The min of the interpolation.</param> |
| | 992 | | /// <param name="b">The max of the interpolation.</param> |
| | 993 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 994 | | /// <returns>The result of the spherical interpolation.</returns> |
| | 995 | | public static Quaternion<T> SphericalInterpolation(Quaternion<T> a, Quaternion<T> b, T blend) |
| 0 | 996 | | { |
| 0 | 997 | | Quaternion<T>? c = null; |
| 0 | 998 | | LinearInterpolation(a, b, blend, ref c); |
| 0 | 999 | | return c!; |
| 0 | 1000 | | } |
| | 1001 | |
|
| | 1002 | | /// <summary>Spherical interpolation for quaternions.</summary> |
| | 1003 | | /// <param name="b">The max of the interpolation.</param> |
| | 1004 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 1005 | | /// <param name="c">The result of the spherical interpolation.</param> |
| | 1006 | | public void SphericalInterpolation(Quaternion<T> b, T blend, ref Quaternion<T>? c) => |
| 0 | 1007 | | LinearInterpolation(this, b, blend, ref c); |
| | 1008 | |
|
| | 1009 | | /// <summary>Spherical interpolation for quaternions.</summary> |
| | 1010 | | /// <param name="b">The max of the interpolation.</param> |
| | 1011 | | /// <param name="blend">The blending point of the interpolation.</param> |
| | 1012 | | /// <returns>The result of the spherical interpolation.</returns> |
| | 1013 | | public Quaternion<T> SphericalInterpolation(Quaternion<T> b, T blend) |
| 0 | 1014 | | { |
| 0 | 1015 | | Quaternion<T>? c = null; |
| 0 | 1016 | | LinearInterpolation(this, b, blend, ref c); |
| 0 | 1017 | | return c!; |
| 0 | 1018 | | } |
| | 1019 | |
|
| | 1020 | | #endregion |
| | 1021 | |
|
| | 1022 | | #region ToMatrix3x3 |
| | 1023 | |
|
| | 1024 | | /// <summary>Converts a quaternion into a 3x3 matrix.</summary> |
| | 1025 | | /// <param name="quaternion">The quaternion of the conversion.</param> |
| | 1026 | | /// <returns>The resulting 3x3 matrix.</returns> |
| | 1027 | | public static Matrix<T> ToMatrix3x3(Quaternion<T> quaternion) |
| 0 | 1028 | | { |
| | 1029 | | // Note: this Method needs optimization... |
| | 1030 | |
|
| | 1031 | | #pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not |
| 0 | 1032 | | return new Matrix<T>(3, 3, (int x, int y) => |
| 0 | 1033 | | (x, y) switch |
| 0 | 1034 | | { |
| 0 | 1035 | | (0, 0) => Subtraction(Subtraction(Addition(Multiplication(quaternion.W, quaternion.W), Multiplication(quaternion |
| 0 | 1036 | | (0, 1) => Subtraction(Multiplication(Multiplication(Convert<int, T>(2), quaternion.X), quaternion.Y), Multiplica |
| 0 | 1037 | | (0, 2) => Addition(Multiplication(Multiplication(Convert<int, T>(2), quaternion.X), quaternion.Z), Multiplicatio |
| 0 | 1038 | | (1, 0) => Addition(Multiplication(Multiplication(Convert<int, T>(2), quaternion.X), quaternion.Y), Multiplicatio |
| 0 | 1039 | | (1, 1) => Subtraction(Addition(Subtraction(Multiplication(quaternion.W, quaternion.W), Multiplication(quaternion |
| 0 | 1040 | | (1, 2) => Addition(Multiplication(Multiplication(Convert<int, T>(2), quaternion.Y), quaternion.Z), Multiplicatio |
| 0 | 1041 | | (2, 0) => Subtraction(Multiplication(Multiplication(Convert<int, T>(2), quaternion.X), quaternion.Z), Multiplica |
| 0 | 1042 | | (2, 1) => Subtraction(Multiplication(Multiplication(Convert<int, T>(2), quaternion.Y), quaternion.Z), Multiplica |
| 0 | 1043 | | (2, 2) => Addition(Subtraction(Subtraction(Multiplication(quaternion.W, quaternion.W), Multiplication(quaternion |
| 0 | 1044 | | }); |
| | 1045 | | #pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not |
| 0 | 1046 | | } |
| | 1047 | |
|
| | 1048 | | #endregion |
| | 1049 | |
|
| | 1050 | | #region Equal |
| | 1051 | |
|
| | 1052 | | /// <summary>Does a value equality check.</summary> |
| | 1053 | | /// <param name="a">The first quaternion to check for equality.</param> |
| | 1054 | | /// <param name="b">The second quaternion to check for equality.</param> |
| | 1055 | | /// <returns>True if values are equal, false if not.</returns> |
| | 1056 | | internal static bool Equal(Quaternion<T> a, Quaternion<T> b) |
| 54 | 1057 | | { |
| 54 | 1058 | | if (a is null) |
| 0 | 1059 | | { |
| 0 | 1060 | | if (b is null) |
| 0 | 1061 | | { |
| 0 | 1062 | | return true; |
| | 1063 | | } |
| | 1064 | | else |
| 0 | 1065 | | { |
| 0 | 1066 | | return false; |
| | 1067 | | } |
| | 1068 | | } |
| 54 | 1069 | | if (b is null) |
| 0 | 1070 | | { |
| 0 | 1071 | | return false; |
| | 1072 | | } |
| 54 | 1073 | | return |
| 54 | 1074 | | Statics.Equate(a._x, b._x) && |
| 54 | 1075 | | Statics.Equate(a._y, b._y) && |
| 54 | 1076 | | Statics.Equate(a._z, b._z) && |
| 54 | 1077 | | Statics.Equate(a._w, b._w); |
| 54 | 1078 | | } |
| | 1079 | |
|
| | 1080 | | /// <summary>Does a value equality check.</summary> |
| | 1081 | | /// <param name="a">The first quaternion to check for equality.</param> |
| | 1082 | | /// <param name="b">The second quaternion to check for equality.</param> |
| | 1083 | | /// <returns>True if values are equal, false if not.</returns> |
| 54 | 1084 | | public static bool operator ==(Quaternion<T> a, Quaternion<T> b) => Equal(a, b); |
| | 1085 | |
|
| | 1086 | | /// <summary>Does a value non-equality check.</summary> |
| | 1087 | | /// <param name="a">The first quaternion to check for non-equality.</param> |
| | 1088 | | /// <param name="b">The second quaternion to check for non-equality.</param> |
| | 1089 | | /// <returns>True if values are not equal, false if not.</returns> |
| 0 | 1090 | | public static bool operator !=(Quaternion<T> a, Quaternion<T> b) => !Equal(a, b); |
| | 1091 | |
|
| | 1092 | | /// <summary>Does a value equality check.</summary> |
| | 1093 | | /// <param name="b">The second quaternion to check for equality.</param> |
| | 1094 | | /// <returns>True if values are equal, false if not.</returns> |
| 0 | 1095 | | public bool Equal(Quaternion<T> b) => this == b; |
| | 1096 | |
|
| | 1097 | | #endregion |
| | 1098 | |
|
| | 1099 | | #region Equal (+leniency) |
| | 1100 | |
|
| | 1101 | | /// <summary>Does a value equality check with leniency.</summary> |
| | 1102 | | /// <param name="a">The first quaternion to check for equality.</param> |
| | 1103 | | /// <param name="b">The second quaternion to check for equality.</param> |
| | 1104 | | /// <param name="leniency">How much the values can vary but still be considered equal.</param> |
| | 1105 | | /// <returns>True if values are equal, false if not.</returns> |
| | 1106 | | internal static bool Equal(Quaternion<T> a, Quaternion<T> b, T leniency) |
| 28 | 1107 | | { |
| 28 | 1108 | | if (a is null) |
| 0 | 1109 | | { |
| 0 | 1110 | | if (b is null) |
| 0 | 1111 | | { |
| 0 | 1112 | | return true; |
| | 1113 | | } |
| | 1114 | | else |
| 0 | 1115 | | { |
| 0 | 1116 | | return false; |
| | 1117 | | } |
| | 1118 | | } |
| 28 | 1119 | | if (b is null) |
| 0 | 1120 | | { |
| 0 | 1121 | | return false; |
| | 1122 | | } |
| 28 | 1123 | | return |
| 28 | 1124 | | Statics.EqualToLeniency(a._x, b._x, leniency) && |
| 28 | 1125 | | Statics.EqualToLeniency(a._y, b._y, leniency) && |
| 28 | 1126 | | Statics.EqualToLeniency(a._z, b._z, leniency) && |
| 28 | 1127 | | Statics.EqualToLeniency(a._w, b._w, leniency); |
| 28 | 1128 | | } |
| | 1129 | |
|
| | 1130 | | /// <summary>Does a value equality check with leniency.</summary> |
| | 1131 | | /// <param name="b">The second quaternion to check for equality.</param> |
| | 1132 | | /// <param name="leniency">How much the values can vary but still be considered equal.</param> |
| | 1133 | | /// <returns>True if values are equal, false if not.</returns> |
| 28 | 1134 | | public bool Equal(Quaternion<T> b, T leniency) => Equal(this, b, leniency); |
| | 1135 | |
|
| | 1136 | | #endregion |
| | 1137 | |
|
| | 1138 | | #endregion |
| | 1139 | |
|
| | 1140 | | #region Other Methods |
| | 1141 | |
|
| | 1142 | | #region Clone |
| | 1143 | |
|
| | 1144 | | /// <summary>Creates a copy of a quaternion.</summary> |
| | 1145 | | /// <param name="a">The quaternion to copy.</param> |
| | 1146 | | /// <returns>The copy of this quaternion.</returns> |
| | 1147 | | public static Quaternion<T> Clone(Quaternion<T> a) |
| 0 | 1148 | | { |
| 0 | 1149 | | if (a is null) throw new ArgumentNullException(nameof(a)); |
| 0 | 1150 | | return new Quaternion<T>(a._x, a._y, a._z, a._w); |
| 0 | 1151 | | } |
| | 1152 | |
|
| | 1153 | | /// <summary>Copies this matrix.</summary> |
| | 1154 | | /// <returns>The copy of this matrix.</returns> |
| 0 | 1155 | | public Quaternion<T> Clone() => Clone(this); |
| | 1156 | |
|
| | 1157 | | #endregion |
| | 1158 | |
|
| | 1159 | | #endregion |
| | 1160 | |
|
| | 1161 | | #region Overrides |
| | 1162 | |
|
| | 1163 | | /// <summary>Computes a hash code from the values in this quaternion.</summary> |
| | 1164 | | /// <returns>The computed hash code.</returns> |
| 0 | 1165 | | public override int GetHashCode() => HashCode.Combine(Hash(_x), Hash(_y), Hash(_z), Hash(_w)); |
| | 1166 | |
|
| | 1167 | | /// <summary>Does a reference equality check.</summary> |
| | 1168 | | /// <param name="other">The other operand of the equality check.</param> |
| | 1169 | | /// <returns>True if equal or false.</returns> |
| 0 | 1170 | | public override bool Equals(object? other) => other is Quaternion<T> b && Equal(this, b); |
| | 1171 | |
|
| | 1172 | | #endregion |
| | 1173 | | } |