]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGQuat.hxx
Fix line endings
[simgear.git] / simgear / math / SGQuat.hxx
index b466d8966bc4e36fca5683516782352a6dec85fb..256ddb28a8b046b7c4f6dd6707779ba2fed415e1 100644 (file)
@@ -55,10 +55,6 @@ public:
   /// make sure it has at least 4 elements
   explicit SGQuat(const T* d)
   { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
-#ifndef NO_OPENSCENEGRAPH_INTERFACE
-  explicit SGQuat(const osg::Quat& d)
-  { data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
-#endif
 
   /// Return a unit quaternion
   static SGQuat unit(void)
@@ -154,6 +150,17 @@ public:
     return fromRealImag(cos(angle2), T(sin(angle2)/nAxis)*axis);
   }
 
+  /// Create a normalized quaternion just from the imaginary part.
+  /// The imaginary part should point into that axis direction that results in
+  /// a quaternion with a positive real part.
+  /// This is the smallest numerically stable representation of an orientation
+  /// in space. See getPositiveRealImag()
+  static SGQuat fromPositiveRealImag(const SGVec3<T>& imag)
+  {
+    T r = sqrt(SGMisc<T>::max(T(0), T(1) - dot(imag, imag)));
+    return fromRealImag(r, imag);
+  }
+
   /// Return a quaternion that rotates the from vector onto the to vector.
   static SGQuat fromRotateTo(const SGVec3<T>& from, const SGVec3<T>& to)
   {
@@ -330,6 +337,19 @@ public:
     axis *= angle;
   }
 
+  /// Get the imaginary part of the quaternion.
+  /// The imaginary part should point into that axis direction that results in
+  /// a quaternion with a positive real part.
+  /// This is the smallest numerically stable representation of an orientation
+  /// in space. See fromPositiveRealImag()
+  SGVec3<T> getPositiveRealImag() const
+  {
+    if (real(*this) < T(0))
+      return (T(-1)/norm(*this))*imag(*this);
+    else
+      return (T(1)/norm(*this))*imag(*this);
+  }
+
   /// Access by index, the index is unchecked
   const T& operator()(unsigned i) const
   { return data()[i]; }
@@ -376,11 +396,6 @@ public:
   T (&data(void))[4]
   { return _data; }
 
-#ifndef NO_OPENSCENEGRAPH_INTERFACE
-  osg::Quat osg() const
-  { return osg::Quat(data()[0], data()[1], data()[2], data()[3]); }
-#endif
-
   /// Inplace addition
   SGQuat& operator+=(const SGQuat& v)
   { data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }