From 20a34fbc52f8e31eaba1e2849d48cd0f6f20a0a7 Mon Sep 17 00:00:00 2001 From: jiri Date: Sat, 4 Jan 2020 21:58:49 +0000 Subject: [PATCH] *** empty log message *** --- quaternion.cc | 36 +++++++++++++++++++++++++++++++----- quaternion.h | 16 +++++++++++----- vecmat3.h | 8 +++++--- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/quaternion.cc b/quaternion.cc index a937165..b329415 100644 --- a/quaternion.cc +++ b/quaternion.cc @@ -24,17 +24,43 @@ template -const Quaternion Quaternion::operator*(const Quaternion &rhs) const +Quaternion Quaternion::operator*(const Quaternion &rhs) const { return Quaternion ( - this->q[0]*rhs.q[0]-this->q[1]*rhs.q[1]-this->q[2]*rhs.q[2]-this->q[3]*rhs.q[3], - this->q[0]*rhs.q[1]+this->q[1]*rhs.q[0]+this->q[2]*rhs.q[3]-this->q[3]*rhs.q[2], - this->q[0]*rhs.q[2]+this->q[2]*rhs.q[0]+this->q[3]*rhs.q[1]-this->q[1]*rhs.q[3], - this->q[0]*rhs.q[3]+this->q[3]*rhs.q[0]+this->q[1]*rhs.q[2]-this->q[2]*rhs.q[1] + q[0]*rhs.q[0]-q[1]*rhs.q[1]-q[2]*rhs.q[2]-q[3]*rhs.q[3], + q[0]*rhs.q[1]+q[1]*rhs.q[0]+q[2]*rhs.q[3]-q[3]*rhs.q[2], + q[0]*rhs.q[2]+q[2]*rhs.q[0]+q[3]*rhs.q[1]-q[1]*rhs.q[3], + q[0]*rhs.q[3]+q[3]*rhs.q[0]+q[1]*rhs.q[2]-q[2]*rhs.q[1] ); }; +template +Quaternion Quaternion::times_vec3(const T *rhs) const +{ +return Quaternion + ( + -q[1]*rhs[0]-q[2]*rhs[1]-q[3]*rhs[2], + q[0]*rhs[0]+q[2]*rhs[2]-q[3]*rhs[1], + q[0]*rhs[1]+q[3]*rhs[0]-q[1]*rhs[2], + q[0]*rhs[2]+q[1]*rhs[1]-q[2]*rhs[0] + ); +}; + +template +Quaternion Quaternion::vec3_times(const T *lhs) const +{ +return Quaternion + ( + -lhs[0]*q[1]-lhs[1]*q[2]-lhs[2]*q[3], + lhs[0]*q[0]+lhs[1]*q[3]-lhs[2]*q[2], + lhs[1]*q[0]+lhs[2]*q[1]-lhs[0]*q[3], + lhs[2]*q[0]+lhs[0]*q[2]-lhs[1]*q[1] + ); +}; + + + //basically the same code as in normquat2rotmat, but avoiding extra storage template diff --git a/quaternion.h b/quaternion.h index 3e6d086..1fa7491 100644 --- a/quaternion.h +++ b/quaternion.h @@ -21,10 +21,11 @@ #ifndef _QUATERNION_H_ #define _QUATERNION_H_ -#include +#ifndef AVOID_STDSTREAM #include +#endif +#include #include -#include #include @@ -61,9 +62,12 @@ public: const Quaternion operator-() const {Quaternion r(*this); r.q[0]= -r.q[0]; r.q[1]= -r.q[1]; r.q[2]= -r.q[2]; r.q[3]= -r.q[3]; return r;}; //unary minus Quaternion& operator+=(const Quaternion &rhs) {q[0]+=rhs.q[0];q[1]+=rhs.q[1];q[2]+=rhs.q[2];q[3]+=rhs.q[3]; return *this;}; Quaternion& operator-=(const Quaternion &rhs) {q[0]-=rhs.q[0];q[1]-=rhs.q[1];q[2]-=rhs.q[2];q[3]-=rhs.q[3]; return *this;}; - const Quaternion operator+(const Quaternion &rhs) const {return Quaternion(*this) += rhs;}; - const Quaternion operator-(const Quaternion &rhs) const {return Quaternion(*this) -= rhs;}; - const Quaternion operator*(const Quaternion &rhs) const; + Quaternion operator+(const Quaternion &rhs) const {return Quaternion(*this) += rhs;}; + Quaternion operator-(const Quaternion &rhs) const {return Quaternion(*this) -= rhs;}; + Quaternion operator*(const Quaternion &rhs) const; //regular product + Quaternion times_vec3(const T *rhs) const; //save flops for quaternions representing vectors + Quaternion vec3_times(const T *rhs) const; //save flops for quaternions representing vectors + Quaternion& conjugateme(void) {q[1] = -q[1]; q[2] = -q[2]; q[3] = -q[3]; return *this;} Quaternion conjugate(void) const {return Quaternion(*this).conjugateme();} T dot(const Quaternion &rhs) const {return q[0]*rhs.q[0] + q[1]*rhs.q[1] + q[2]*rhs.q[2] + q[3]*rhs.q[3];}; @@ -84,6 +88,7 @@ public: //stream I/O +#ifndef AVOID_STDSTREAM template std::istream& operator>>(std::istream &s, Quaternion &x) { @@ -102,6 +107,7 @@ s << x.q[2]<<" "; s << x.q[3]; return s; } +#endif diff --git a/vecmat3.h b/vecmat3.h index 72cf24c..8d49251 100644 --- a/vecmat3.h +++ b/vecmat3.h @@ -24,8 +24,10 @@ #define _VECMAT3_H_ #include +#ifndef AVOID_STDSTREAM #include -#include +#endif +#include #include //forward declaration @@ -169,6 +171,7 @@ public: //stream I/O +#ifndef AVOID_STDSTREAM template std::istream& operator>>(std::istream &s, Vec3 &x) { @@ -208,8 +211,7 @@ s << x.q[1][0]<<" "<< x.q[1][1]<<" " << x.q[1][2]<