LA_library/quaternion.cc

103 lines
2.9 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/>.
*/
#include "quaternion.h"
2019-12-30 16:41:36 +01:00
2019-12-31 18:49:39 +01:00
//do not replicate this code in each object file, therefore not in .h
//and instantize the templates for the types needed
template<typename T>
const Quaternion<T> Quaternion<T>::operator*(const Quaternion<T> &rhs) const
{
return Quaternion<T>
(
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]
);
};
2020-01-01 10:22:55 +01:00
template<typename T>
Quaternion<T> Quaternion<T>::rotateby(const Quaternion<T> &rhs, bool rhs_is_normalized)
{
return rhs * *this * rhs.inverse(); //inefficient reference implementation
}
2019-12-31 18:49:39 +01:00
//optionally skip this for microcontrollers if not needed
//note that C++ standard headers should use float version of the functions for T=float
#ifndef AVOID_GONIOM_FUNC
template<typename T>
2020-01-01 10:22:55 +01:00
void normquat2euler(const Quaternion<T> &q, T *e)
2019-12-30 16:41:36 +01:00
{
2019-12-31 18:49:39 +01:00
e[0]= atan2(2*q[1]*q[2]-2*q[0]*q[3],2*q[0]*q[0]+2*q[1]*q[1]-1);
e[1]= -asin(2*q[1]*q[3]+2*q[0]*q[2]);
e[2]= atan2(2*q[2]*q[3]-2*q[0]*q[1],2*q[0]*q[0]+2*q[3]*q[3]-1);
2019-12-30 16:41:36 +01:00
}
2020-01-01 10:22:55 +01:00
template<typename T>
void axis2normquat(const T *axis, const T &angle, Quaternion<T> &q)
{
T a = (T).5*angle;
q[0]=cos(a);
T s=sin(a);
q[1]=axis[0]*s;
q[2]=axis[1]*s;
q[3]=axis[2]*s;
}
template<typename T>
void normquat2axis(const Quaternion<T> &q, T *axis, T &angle)
{
T s = sqrt(q[1]*q[1] + q[2]*q[2] +q[3]*q[3]);
angle = 2*atan2(s,q[0]);
s= 1/s;
axis[0]= q[1]*s;
axis[1]= q[2]*s;
axis[2]= q[3]*s;
}
2019-12-31 18:49:39 +01:00
#endif
2019-12-30 16:41:36 +01:00
2019-12-30 15:42:10 +01:00
2019-12-31 18:49:39 +01:00
//force instantization
#define INSTANTIZE(T) \
template class Quaternion<T>; \
2020-01-01 10:22:55 +01:00
#define INSTANTIZE2(T) \
template void normquat2euler(const Quaternion<T> &q, T *e); \
template void axis2normquat(const T *axis, const T &angle, Quaternion<T> &q); \
template void normquat2axis(const Quaternion<T> &q, T *axis, T &angle); \
2019-12-30 15:42:10 +01:00
2019-12-31 18:49:39 +01:00
INSTANTIZE(float)
#ifndef QUAT_NO_DOUBLE
INSTANTIZE(double)
#endif
2020-01-01 10:22:55 +01:00
#ifndef AVOID_GONIOM_FUNC
INSTANTIZE2(float)
#ifndef QUAT_NO_DOUBLE
INSTANTIZE2(double)
#endif
#endif