/// <summary> /// copy constructor /// </summary> public dquat(dquat 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 dquat(dvec3 u, dvec3 v) { var localW = dvec3.Cross(u, v); var dot = dvec3.Dot(u, v); var q = new dquat(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 dquat Mix(dquat x, dquat y, double a) { var cosTheta = (double)Dot(x, y); if (cosTheta > 1 - float.Epsilon) { return(Lerp(x, y, a)); } else { var angle = Math.Acos((double)cosTheta); return((Math.Sin((1 - (double)a) * angle) * x + Math.Sin((double)a * angle) * y) / 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 dquat 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 dquat(x, y, z, w) : Zero; return(ok); }
/// <summary> /// Calculates a proper spherical interpolation between two quaternions (only works for normalized quaternions). /// </summary> public static dquat SLerp(dquat x, dquat 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 = Math.Acos((double)cosTheta); return((Math.Sin((1 - (double)a) * angle) * x + Math.Sin((double)a * angle) * z) / Math.Sin(angle)); } }
/// <summary> /// Returns a bvec4 from component-wise application of IsNaN (double.IsNaN(v)). /// </summary> public static bvec4 IsNaN(dquat v) => dquat.IsNaN(v);
/// <summary> /// Returns a bvec4 from component-wise application of IsInfinity (double.IsInfinity(v)). /// </summary> public static bvec4 IsInfinity(dquat v) => dquat.IsInfinity(v);
/// <summary> /// Returns true iff this equals rhs type- and component-wise. /// </summary> public static bool Equals(dquat q, object obj) => q.Equals(obj);
/// <summary> /// Returns the number of components (4). /// </summary> public static int Count(dquat q) => q.Count;
/// <summary> /// Returns a string representation of this quaternion using a provided seperator and a format for each component. /// </summary> public static string ToString(dquat q, string sep, string format) => q.ToString(sep, format);
/// <summary> /// Returns a string representation of this quaternion using ', ' as a seperator. /// </summary> public static string ToString(dquat q) => q.ToString();
/// <summary> /// Creates a dmat4 that realizes the rotation of this quaternion /// </summary> public static dmat4 ToMat4(dquat q) => q.ToMat4;
/// <summary> /// Creates a dmat3 that realizes the rotation of this quaternion /// </summary> public static dmat3 ToMat3(dquat q) => q.ToMat3;
/// <summary> /// Rotates this quaternion from an axis and an angle (in radians). /// </summary> public static dquat Rotated(dquat q, double angle, dvec3 v) => q.Rotated(angle, v);
/// <summary> /// Returns the represented euler angles (pitch, yaw, roll) of this quaternion. /// </summary> public static dvec3 EulerAngles(dquat q) => q.EulerAngles;
/// <summary> /// Returns a dquat from component-wise application of Lerp (min * (1-a) + max * a). /// </summary> public static dquat Lerp(dquat min, dquat max, dquat a) => dquat.Lerp(min, max, a);
/// <summary> /// Returns an enumerator that iterates through all components. /// </summary> public static IEnumerator <double> GetEnumerator(dquat q) => q.GetEnumerator();
/// <summary> /// Returns an array with all values /// </summary> public static double[] Values(dquat q) => q.Values;
/// <summary> /// Returns a string representation of this quaternion using a provided seperator. /// </summary> public static string ToString(dquat q, string sep) => q.ToString(sep);
/// <summary> /// Returns the conjugated quaternion /// </summary> public static dquat Conjugate(dquat q) => q.Conjugate;
/// <summary> /// Returns a string representation of this quaternion using a provided seperator and a format and format provider for each component. /// </summary> public static string ToString(dquat q, string sep, string format, IFormatProvider provider) => q.ToString(sep, format, provider);
/// <summary> /// Returns the inverse quaternion /// </summary> public static dquat Inverse(dquat q) => q.Inverse;
/// <summary> /// Returns true iff this equals rhs component-wise. /// </summary> public static bool Equals(dquat q, dquat rhs) => q.Equals(rhs);
/// <summary> /// Returns the cross product between two quaternions. /// </summary> public static dquat Cross(dquat q1, dquat q2) => dquat.Cross(q1, q2);
/// <summary> /// Returns a hash code for this instance. /// </summary> public static int GetHashCode(dquat q) => q.GetHashCode();
/// <summary> /// Calculates a proper spherical interpolation between two quaternions (only works for normalized quaternions). /// </summary> public static dquat Mix(dquat x, dquat y, double a) => dquat.Mix(x, y, a);
/// <summary> /// Returns a bvec4 from component-wise application of IsFinite (!double.IsNaN(v) && !double.IsInfinity(v)). /// </summary> public static bvec4 IsFinite(dquat v) => dquat.IsFinite(v);
/// <summary> /// Calculates a proper spherical interpolation between two quaternions (only works for normalized quaternions). /// </summary> public static dquat SLerp(dquat x, dquat y, double a) => dquat.SLerp(x, y, a);
/// <summary> /// Returns a bvec4 from component-wise application of IsPositiveInfinity (double.IsPositiveInfinity(v)). /// </summary> public static bvec4 IsPositiveInfinity(dquat v) => dquat.IsPositiveInfinity(v);
/// <summary> /// Applies squad interpolation of these quaternions /// </summary> public static dquat Squad(dquat q1, dquat q2, dquat s1, dquat s2, double h) => dquat.Squad(q1, q2, s1, s2, h);