glc_matrix4x4.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00025
00026 #include "glc_matrix4x4.h"
00027
00028 #include <QtDebug>
00029
00031
00033
00034 GLC_Matrix4x4& GLC_Matrix4x4::fromEuler(const double angle_x, const double angle_y, const double angle_z)
00035 {
00036 const double A= cos(angle_x);
00037 const double B= sin(angle_x);
00038 const double C= cos(angle_y);
00039 const double D= sin(angle_y);
00040 const double E= cos(angle_z);
00041 const double F= sin(angle_z);
00042
00043 const double AD= A * D;
00044 const double BD= B * D;
00045
00046 m_Matrix[0] = C * E;
00047 m_Matrix[4] = -C * F;
00048 m_Matrix[8] = -D;
00049 m_Matrix[1] = -BD * E + A * F;
00050 m_Matrix[5] = BD * F + A * E;
00051 m_Matrix[9] = -B * C;
00052 m_Matrix[2] = AD * E + B * F;
00053 m_Matrix[6] = -AD * F + B * E;
00054 m_Matrix[10] = A * C;
00055
00056 m_Matrix[12]= 0.0; m_Matrix[13]= 0.0; m_Matrix[14]= 0.0; m_Matrix[3]= 0.0; m_Matrix[7]= 0.0; m_Matrix[11] = 0.0;
00057 m_Matrix[15] = 1.0;
00058
00059 return *this;
00060 }
00061
00062 GLC_Matrix4x4& GLC_Matrix4x4::setColumn(int index, const GLC_Vector3d& vector)
00063 {
00064 Q_ASSERT(index < 4);
00065 index= index * 4;
00066 m_Matrix[index]= vector.x();
00067 m_Matrix[index + 1]= vector.y();
00068 m_Matrix[index + 2]= vector.z();
00069
00070 return *this;
00071 }
00073
00075
00076 QVector<double> GLC_Matrix4x4::toEuler(void) const
00077 {
00078 double angle_x;
00079 double angle_y;
00080 double angle_z;
00081 double tracex, tracey;
00082 angle_y= -asin(m_Matrix[8]);
00083 double C= cos(angle_y);
00084
00085 if (!qFuzzyCompare(C, 0.0))
00086 {
00087 tracex= m_Matrix[10] / C;
00088 tracey= - m_Matrix[9] / C;
00089 angle_x= atan2( tracey, tracex);
00090
00091 tracex= m_Matrix[0] / C;
00092 tracey= - m_Matrix[4] / C;
00093 angle_z= atan2( tracey, tracex);
00094 }
00095 else
00096 {
00097 angle_x= 0.0;
00098 tracex= m_Matrix[5] / C;
00099 tracey= m_Matrix[1] / C;
00100 angle_z= atan2( tracey, tracex);
00101 }
00102 QVector<double> result;
00103 result.append(fmod(angle_x, 2.0 * glc::PI));
00104 result.append(fmod(angle_y, 2.0 * glc::PI));
00105 result.append(fmod(angle_z, 2.0 * glc::PI));
00106
00107 return result;
00108 }
00109
00110 QString GLC_Matrix4x4::toString() const
00111 {
00112 QString result;
00113 for (int i= 0; i < DIMMAT4X4; ++i)
00114 {
00115 result+= (QString::number(m_Matrix[0 + i])) + QString(" ");
00116 result+= (QString::number(m_Matrix[4 + i])) + QString(" ");
00117 result+= (QString::number(m_Matrix[8 + i])) + QString(" ");
00118 result+= (QString::number(m_Matrix[12 + i])) + QString("\n");
00119 }
00120 result.remove(result.size() - 1, 1);
00121 return result;
00122 }