Browse Source

added conjugate, changed var name, added 4-d rotation

Dave Schuyler 20 years ago
parent
commit
7492d74426
2 changed files with 86 additions and 2 deletions
  1. 77 2
      panda/src/linmath/lquaternion_src.I
  2. 9 0
      panda/src/linmath/lquaternion_src.h

+ 77 - 2
panda/src/linmath/lquaternion_src.I

@@ -36,6 +36,16 @@ FLOATNAME(LQuaternion)(const FLOATNAME(LVecBase4) &copy) :
 {
 {
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: LQuaternion::Copy Constructor
+//       Access: public
+//  Description:
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LQuaternion)::
+FLOATNAME(LQuaternion)(FLOATTYPE r, const FLOATNAME(LVecBase3) &copy) {
+  set(r, copy[0], copy[1], copy[2]);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: LQuaternion::Constructor
 //     Function: LQuaternion::Constructor
 //       Access: public
 //       Access: public
@@ -55,12 +65,29 @@ INLINE_LINMATH FLOATNAME(LVecBase3) FLOATNAME(LQuaternion)::
 xform(const FLOATNAME(LVecBase3) &v) const {
 xform(const FLOATNAME(LVecBase3) &v) const {
   FLOATNAME(LQuaternion) v_quat(0.0f, v[0], v[1], v[2]);
   FLOATNAME(LQuaternion) v_quat(0.0f, v[0], v[1], v[2]);
 
 
-  FLOATNAME(LQuaternion) inv(_v.data[0], -_v.data[1], -_v.data[2], -_v.data[3]);
-  v_quat = inv * v_quat * (*this);
+  FLOATNAME(LQuaternion) conjugate(
+    _v.data[0], -_v.data[1], -_v.data[2], -_v.data[3]);
+  v_quat = conjugate * v_quat * (*this);
 
 
   return FLOATNAME(LVecBase3)(v_quat[1], v_quat[2], v_quat[3]);
   return FLOATNAME(LVecBase3)(v_quat[1], v_quat[2], v_quat[3]);
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: LQuaternion::xform
+//       Access: Published
+//  Description: Transforms a 4-d vector by the indicated rotation
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LVecBase4) FLOATNAME(LQuaternion)::
+xform(const FLOATNAME(LVecBase4) &v) const {
+  FLOATNAME(LQuaternion) v_quat(v[0], v[1], v[2], v[3]);
+
+  FLOATNAME(LQuaternion) conjugate(
+    _v.data[0], -_v.data[1], -_v.data[2], -_v.data[3]);
+  v_quat = conjugate * v_quat * (*this);
+
+  return FLOATNAME(LVecBase4)(v_quat);
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: LQuaternion::multiply
 //     Function: LQuaternion::multiply
 //       Access: Published
 //       Access: Published
@@ -432,6 +459,54 @@ normalize() {
   return true;
   return true;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: LQuaternion::conjugate
+//       Access: Published
+//  Description: Returns the complex conjugate of this quat.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH FLOATNAME(LQuaternion) FLOATNAME(LQuaternion)::
+conjugate() const {
+  return FLOATNAME(LQuaternion)(_v.v._0, -_v.v._1, -_v.v._2, -_v.v._3);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: LQuaternion::conjugate_from
+//       Access: Published
+//  Description: Computes the conjugate of the other quat, and stores
+//               the result in this quat.  This is a fully general
+//               operation and makes no assumptions about the type of
+//               transform represented by the quat.
+//
+//               The other quat must be a different object than this
+//               quat.  However, if you need to get a conjugate of a
+//               quat in place, see conjugate_in_place.
+//
+//               The return value is true if the quat was
+//               successfully inverted, false if there was a
+//               singularity.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH bool FLOATNAME(LQuaternion)::
+conjugate_from(const FLOATNAME(LQuaternion) &other) {
+  set(other._v.v._0, -other._v.v._1, -other._v.v._2, -other._v.v._3);
+  return true;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: LQuaternion::conjugate_in_place
+//       Access: Published
+//  Description: Sets this to be the conjugate of the current quat.
+//               Returns true if the successful, false if the quat
+//               was singular.
+////////////////////////////////////////////////////////////////////
+INLINE_LINMATH bool FLOATNAME(LQuaternion)::
+conjugate_in_place() {
+  // _v.v._0 = _v.v._0;
+  _v.v._1 = -_v.v._1;
+  _v.v._2 = -_v.v._2;
+  _v.v._3 = -_v.v._3;
+  return true;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: LQuaternion::invert_from
 //     Function: LQuaternion::invert_from
 //       Access: Public
 //       Access: Public

+ 9 - 0
panda/src/linmath/lquaternion_src.h

@@ -24,13 +24,19 @@ class EXPCL_PANDA FLOATNAME(LQuaternion) : public FLOATNAME(LVecBase4) {
 PUBLISHED:
 PUBLISHED:
   INLINE_LINMATH FLOATNAME(LQuaternion)();
   INLINE_LINMATH FLOATNAME(LQuaternion)();
   INLINE_LINMATH FLOATNAME(LQuaternion)(const FLOATNAME(LVecBase4) &copy);
   INLINE_LINMATH FLOATNAME(LQuaternion)(const FLOATNAME(LVecBase4) &copy);
+  INLINE_LINMATH FLOATNAME(LQuaternion)(FLOATTYPE, const FLOATNAME(LVecBase3) &copy);
   INLINE_LINMATH FLOATNAME(LQuaternion)(FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE);
   INLINE_LINMATH FLOATNAME(LQuaternion)(FLOATTYPE, FLOATTYPE, FLOATTYPE, FLOATTYPE);
 
 
   static FLOATNAME(LQuaternion) pure_imaginary(const FLOATNAME(LVector3) &);
   static FLOATNAME(LQuaternion) pure_imaginary(const FLOATNAME(LVector3) &);
 
 
+  INLINE_LINMATH FLOATNAME(LQuaternion) conjugate() const;
+
   INLINE_LINMATH FLOATNAME(LVecBase3)
   INLINE_LINMATH FLOATNAME(LVecBase3)
     xform(const FLOATNAME(LVecBase3) &v) const;
     xform(const FLOATNAME(LVecBase3) &v) const;
 
 
+  INLINE_LINMATH FLOATNAME(LVecBase4)
+    xform(const FLOATNAME(LVecBase4) &v) const;
+
   INLINE_LINMATH FLOATNAME(LQuaternion)
   INLINE_LINMATH FLOATNAME(LQuaternion)
     multiply(const FLOATNAME(LQuaternion) &rhs) const;
     multiply(const FLOATNAME(LQuaternion) &rhs) const;
 
 
@@ -89,6 +95,9 @@ PUBLISHED:
 
 
   INLINE_LINMATH bool normalize();
   INLINE_LINMATH bool normalize();
 
 
+  INLINE_LINMATH bool conjugate_from(const FLOATNAME(LQuaternion) &other);
+  INLINE_LINMATH bool conjugate_in_place();
+
   INLINE_LINMATH bool invert_from(const FLOATNAME(LQuaternion) &other);
   INLINE_LINMATH bool invert_from(const FLOATNAME(LQuaternion) &other);
   INLINE_LINMATH bool invert_in_place();
   INLINE_LINMATH bool invert_in_place();