/// <summary> /// copy constructor /// </summary> public qdouble(qdouble q) { this.x = q.x; this.y = q.y; this.z = q.z; this.w = q.w; }
/// <summary> /// Create a quaternion from two normalized axis (http://lolengine.net/blog/2013/09/18/beautiful-maths-quaternion-from-vectors) /// </summary> public qdouble(double3 u, double3 v) { var localW = double3.Cross(u, v); var dot = double3.Dot(u, v); var q = new qdouble(localW.x, localW.y, localW.z, 1.0 + dot).Normalized; this.x = q.x; this.y = q.y; this.z = q.z; this.w = q.w; }
/// <summary> /// Calculates a proper spherical interpolation between two quaternions (only works for normalized quaternions). /// </summary> public static qdouble Mix(qdouble x, qdouble y, double a) { var cosTheta = (double)Dot(x, y); if (cosTheta > 1 - float.Epsilon) { return(Lerp(x, y, a)); } else { var angle = System.Math.Acos((double)cosTheta); return((System.Math.Sin((1 - (double)a) * angle) * x + System.Math.Sin((double)a * angle) * y) / System.Math.Sin(angle)); } }
/// <summary> /// Calculates a proper spherical interpolation between two quaternions (only works for normalized quaternions). /// </summary> public static qdouble SLerp(qdouble x, qdouble y, double a) { var z = y; var cosTheta = (double)Dot(x, y); if (cosTheta < 0) { z = -y; cosTheta = -cosTheta; } if (cosTheta > 1 - float.Epsilon) { return(Lerp(x, z, a)); } else { var angle = System.Math.Acos((double)cosTheta); return((System.Math.Sin((1 - (double)a) * angle) * x + System.Math.Sin((double)a * angle) * z) / System.Math.Sin(angle)); } }
/// <summary> /// Tries to convert the string representation of the quaternion into a quaternion representation (using a designated separator), returns false if string was invalid. /// </summary> public static bool TryParse(string s, string sep, out qdouble result) { result = Zero; if (string.IsNullOrEmpty(s)) { return(false); } var kvp = s.Split(new[] { sep }, StringSplitOptions.None); if (kvp.Length != 4) { return(false); } double x = 0.0, y = 0.0, z = 0.0, w = 0.0; var ok = ((double.TryParse(kvp[0].Trim(), out x) && double.TryParse(kvp[1].Trim(), out y)) && (double.TryParse(kvp[2].Trim(), out z) && double.TryParse(kvp[3].Trim(), out w))); result = ok ? new qdouble(x, y, z, w) : Zero; return(ok); }
/// <summary> /// Returns a bool4 from component-wise application of Equal (lhs == rhs). /// </summary> public static bool4 Equal(qdouble lhs, double rhs) => new bool4(lhs.x == rhs, lhs.y == rhs, lhs.z == rhs, lhs.w == rhs);
/// <summary> /// Returns a bool4 from component-wise application of IsPositiveInfinity (double.IsPositiveInfinity(v)). /// </summary> public static bool4 IsPositiveInfinity(qdouble v) => new bool4(double.IsPositiveInfinity(v.x), double.IsPositiveInfinity(v.y), double.IsPositiveInfinity(v.z), double.IsPositiveInfinity(v.w));
/// <summary> /// Returns a bool4 from component-wise application of IsNaN (double.IsNaN(v)). /// </summary> public static bool4 IsNaN(qdouble v) => new bool4(double.IsNaN(v.x), double.IsNaN(v.y), double.IsNaN(v.z), double.IsNaN(v.w));
/// <summary> /// Returns a bool4 from component-wise application of IsFinite (!double.IsNaN(v) && !double.IsInfinity(v)). /// </summary> public static bool4 IsFinite(qdouble v) => new bool4(!double.IsNaN(v.x) && !double.IsInfinity(v.x), !double.IsNaN(v.y) && !double.IsInfinity(v.y), !double.IsNaN(v.z) && !double.IsInfinity(v.z), !double.IsNaN(v.w) && !double.IsInfinity(v.w));
/// <summary> /// Returns a bool4 from component-wise application of IsNaN (double.IsNaN(v)). /// </summary> public static bool4 IsNaN(qdouble v) => qdouble.IsNaN(v);
/// <summary> /// Returns a bool4 from component-wise application of GreaterThanEqual (lhs >= rhs). /// </summary> public static bool4 GreaterThanEqual(double lhs, qdouble rhs) => new bool4(lhs >= rhs.x, lhs >= rhs.y, lhs >= rhs.z, lhs >= rhs.w);
/// <summary> /// Returns a bool4 from component-wise application of GreaterThan (lhs > rhs). /// </summary> public static bool4 GreaterThan(qdouble lhs, double rhs) => new bool4(lhs.x > rhs, lhs.y > rhs, lhs.z > rhs, lhs.w > rhs);
/// <summary> /// Returns a bool4 from component-wise application of IsFinite (!double.IsNaN(v) && !double.IsInfinity(v)). /// </summary> public static bool4 IsFinite(qdouble v) => qdouble.IsFinite(v);
/// <summary> /// Returns the cross product between two quaternions. /// </summary> public static qdouble Cross(qdouble q1, qdouble q2) => new qdouble(q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y, q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z, q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x, q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z);
/// <summary> /// Returns the inner product (dot product, scalar product) of the two quaternions. /// </summary> public static double Dot(qdouble lhs, qdouble rhs) => ((lhs.x * rhs.x + lhs.y * rhs.y) + (lhs.z * rhs.z + lhs.w * rhs.w));
/// <summary> /// Tries to convert the string representation of the quaternion into a quaternion representation (using ', ' as a separator), returns false if string was invalid. /// </summary> public static bool TryParse(string s, out qdouble result) => TryParse(s, ", ", out result);
/// <summary> /// Returns true iff this equals rhs component-wise. /// </summary> public bool Equals(qdouble rhs) => ((x.Equals(rhs.x) && y.Equals(rhs.y)) && (z.Equals(rhs.z) && w.Equals(rhs.w)));
/// <summary> /// Returns a qdouble from component-wise application of Lerp (min * (1-a) + max * a). /// </summary> public static qdouble Lerp(double min, double max, qdouble a) => new qdouble(min * (1 - a.x) + max * a.x, min * (1 - a.y) + max * a.y, min * (1 - a.z) + max * a.z, min * (1 - a.w) + max * a.w);
/// <summary> /// Returns a bool4 from component-wise application of NotEqual (lhs != rhs). /// </summary> public static bool4 NotEqual(qdouble lhs, double rhs) => new bool4(lhs.x != rhs, lhs.y != rhs, lhs.z != rhs, lhs.w != rhs);
/// <summary> /// Returns a bool4 from component-wise application of LesserThanEqual (lhs <= rhs). /// </summary> public static bool4 LesserThanEqual(qdouble lhs, double rhs) => new bool4(lhs.x <= rhs, lhs.y <= rhs, lhs.z <= rhs, lhs.w <= rhs);
/// <summary> /// Returns a bool4 from component-wise application of NotEqual (lhs != rhs). /// </summary> public static bool4 NotEqual(double lhs, qdouble rhs) => new bool4(lhs != rhs.x, lhs != rhs.y, lhs != rhs.z, lhs != rhs.w);
/// <summary> /// Returns a qdouble from component-wise application of Lerp (min * (1-a) + max * a). /// </summary> public static qdouble Lerp(qdouble min, qdouble max, double a) => new qdouble(min.x * (1 - a) + max.x * a, min.y * (1 - a) + max.y * a, min.z * (1 - a) + max.z * a, min.w * (1 - a) + max.w * a);
/// <summary> /// Returns a bool4 from component-wise application of GreaterThanEqual (lhs >= rhs). /// </summary> public static bool4 GreaterThanEqual(qdouble lhs, double rhs) => new bool4(lhs.x >= rhs, lhs.y >= rhs, lhs.z >= rhs, lhs.w >= rhs);
/// <summary> /// Returns a bool4 from component-wise application of IsInfinity (double.IsInfinity(v)). /// </summary> public static bool4 IsInfinity(qdouble v) => qdouble.IsInfinity(v);
/// <summary> /// Returns a bool4 from component-wise application of LesserThan (lhs < rhs). /// </summary> public static bool4 LesserThan(qdouble lhs, double rhs) => new bool4(lhs.x < rhs, lhs.y < rhs, lhs.z < rhs, lhs.w < rhs);
/// <summary> /// Returns a bool4 from component-wise application of IsPositiveInfinity (double.IsPositiveInfinity(v)). /// </summary> public static bool4 IsPositiveInfinity(qdouble v) => qdouble.IsPositiveInfinity(v);
/// <summary> /// Returns a bool4 from component-wise application of LesserThanEqual (lhs <= rhs). /// </summary> public static bool4 LesserThanEqual(double lhs, qdouble rhs) => new bool4(lhs <= rhs.x, lhs <= rhs.y, lhs <= rhs.z, lhs <= rhs.w);
/// <summary> /// Applies squad interpolation of these quaternions /// </summary> public static qdouble Squad(qdouble q1, qdouble q2, qdouble s1, qdouble s2, double h) => Mix(Mix(q1, q2, h), Mix(s1, s2, h), 2 * (1 - h) * h);
/// <summary> /// Returns a qdouble from component-wise application of Lerp (min * (1-a) + max * a). /// </summary> public static qdouble Lerp(qdouble min, double max, qdouble a) => new qdouble(min.x * (1 - a.x) + max * a.x, min.y * (1 - a.y) + max * a.y, min.z * (1 - a.z) + max * a.z, min.w * (1 - a.w) + max * a.w);
/// <summary> /// Returns a hash code for this instance. /// </summary> public static int GetHashCode(qdouble q) => q.GetHashCode();