LA_library/quaternion.h

87 lines
3.2 KiB
C
Raw Normal View History

2019-12-30 15:42:10 +01:00
/*
LA: linear algebra C++ interface library
Copyright (C) 2008-2020 Jiri Pittner <jiri.pittner@jh-inst.cas.cz> or <jiri@pittnerovi.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _QUATERNION_H_
#define _QUATERNION_H_
#include <stdlib.h>
template <typename T>
class Quaternion
{
public:
2019-12-30 16:15:12 +01:00
//just plain old data
2019-12-30 15:42:10 +01:00
T q[4];
2019-12-30 16:15:12 +01:00
//
2019-12-30 15:42:10 +01:00
Quaternion(void) {};
2019-12-30 16:15:12 +01:00
Quaternion(const T x, const T u=0, const T v=0, const T w=0) {q[0]=x; q[1]=u; q[2]=v; q[3]=w;}; //quaternion from real
//compiler generates default copy constructor and assignment operator
//operations of quaternions with scalars
Quaternion& operator=(const T x) {q[0]=x; memset(&q[1],0,3*sizeof(T)); return *this;}; //quaternion from real
Quaternion& operator+=(const T rhs) {this->q[0]+=rhs; return *this;};
Quaternion& operator-=(const T rhs) {this->q[0]-=rhs; return *this;};
Quaternion& operator*=(const T rhs) {this->q[0]*=rhs; this->q[1]*=rhs; this->q[2]*=rhs; this->q[3]*=rhs; return *this;};
Quaternion& operator/=(const T rhs) {return *this *= ((T)1/rhs);};
const Quaternion operator+(const T rhs) const {return Quaternion(*this) += rhs;};
const Quaternion operator-(const T rhs) const {return Quaternion(*this) -= rhs;};
const Quaternion operator*(const T rhs) const {return Quaternion(*this) *= rhs;};
const Quaternion operator/(const T rhs) const {return Quaternion(*this) /= rhs;};
//quaternion algebra
Quaternion& operator+=(const Quaternion &rhs) {this->q[0]+=rhs.q[0];this->q[1]+=rhs.q[1];this->q[2]+=rhs.q[2];this->q[3]+=rhs.q[3]; return *this;};
Quaternion& operator-=(const Quaternion &rhs) {this->q[0]-=rhs.q[0];this->q[1]-=rhs.q[1];this->q[2]-=rhs.q[2];this->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
{
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]
);
};
2019-12-30 15:42:10 +01:00
};
2019-12-30 16:15:12 +01:00
//stream I/O
template <typename T>
std::istream& operator>>(std::istream &s, Quaternion<T> &x)
{
s >> x.q[0];
s >> x.q[1];
s >> x.q[2];
s >> x.q[3];
return s;
}
template <typename T>
std::ostream& operator<<(std::ostream &s, const Quaternion<T> &x) {
s << x.q[0]<<" ";
s << x.q[1]<<" ";
s << x.q[2]<<" ";
s << x.q[3];
return s;
}
2019-12-30 15:42:10 +01:00
#endif /* _QUATERNION_H_ */