#include "math/TransformMatrix.h" using namespace projection; using namespace gmtl; /** * Constructor. * * Default transform matrix value is identity matrix. */ TransformMatrix::TransformMatrix() { reset (); } /** * Destructor. */ TransformMatrix::~TransformMatrix() { } /** * Retrieve the trasform matrix. * * @return Trasform matrix. */ gmtl::Matrix44f TransformMatrix::getMatrix() const { return makeTrans(m_pos) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[0]), Vec3f(0.0f, 1.0f, 0.0f))) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[1]), Vec3f(1.0f, 0.0f, 0.0f))) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[2]), Vec3f(0.0f, 0.0f, 1.0f))) * makeScale(m_scale); } /** * Retrieve the trasform matrix without scaling. * * @return Trasform matrix without scaling. */ gmtl::Matrix44f TransformMatrix::getMatrixWOScaling() const { return makeTrans(m_pos) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[0]), Vec3f(0.0f, 1.0f, 0.0f))) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[1]), Vec3f(1.0f, 0.0f, 0.0f))) * makeRot(AxisAnglef(Math::deg2Rad(m_rot[2]), Vec3f(0.0f, 0.0f, 1.0f))); } /** * Retrieve transform parameters like 'Rotate 45 1 0 0;'. * * @param numSpaces Number of space characters for indent. * @return Transform params. */ QString TransformMatrix::getParams(int space) const { QString spc; spc.fill(' ', space); QString result; if (m_scale != Vec3f(1.0f, 1.0f, 1.0f)) result += spc + QString("Scale %1 %2 %3;\n").arg(m_scale[0]).arg(m_scale[1]).arg(m_scale[2]); if (m_rot[2] != 0.0) result += spc + QString("Rotate %1 0 0 1;\n").arg(m_rot[2]); if (m_rot[1] != 0.0) result += spc + QString("Rotate %1 1 0 0;\n").arg(m_rot[1]); if (m_rot[0] != 0.0) result += spc + QString("Rotate %1 0 1 0;\n").arg(m_rot[0]); if (m_pos != Vec3f(0.0f, 0.0f, 0.0f)) result += spc + QString("Translate %1 %2 %3;\n").arg(m_pos[0]).arg(m_pos[1]).arg(m_pos[2]); return result; } /** * Restore the transform matrix from XML data. * * @param element Parent XML element of the transform matrix data. */ void TransformMatrix::initFromDOMElement(const QDomElement& element) { QDomElement pos = element.firstChildElement("position"); m_pos[0] = pos.attribute("x").toFloat(); m_pos[1] = pos.attribute("y").toFloat(); m_pos[2] = pos.attribute("z").toFloat(); QDomElement rot = element.firstChildElement("rotation"); m_rot[0] = rot.attribute("h").toFloat(); m_rot[1] = rot.attribute("p").toFloat(); m_rot[2] = rot.attribute("r").toFloat(); if (!element.firstChildElement("scaling").isNull()) { QDomElement scale = element.firstChildElement("scaling"); m_scale[0] = scale.attribute("x").toFloat(); m_scale[1] = scale.attribute("y").toFloat(); m_scale[2] = scale.attribute("z").toFloat(); } } /** * Store the current transform matrix as XML data. * * @param name XML node name of the data. * @param doc XML document to store the data. * @return Current transform matrix data as XML data. */ QDomElement TransformMatrix::domElement(const QString& name, QDomDocument& doc) const { QDomElement de = doc.createElement(name); QDomElement pos = doc.createElement("position"); pos.setAttribute("x", QString::number(m_pos[0])); pos.setAttribute("y", QString::number(m_pos[1])); pos.setAttribute("z", QString::number(m_pos[2])); de.appendChild(pos); QDomElement rot = doc.createElement("rotation"); rot.setAttribute("h", QString::number(m_rot[0])); rot.setAttribute("p", QString::number(m_rot[1])); rot.setAttribute("r", QString::number(m_rot[2])); de.appendChild(rot); QDomElement scale = doc.createElement("scaling"); scale.setAttribute("x", QString::number(m_scale[0])); scale.setAttribute("y", QString::number(m_scale[1])); scale.setAttribute("z", QString::number(m_scale[2])); de.appendChild(scale); return de; } /** * Reset the matrix. */ void TransformMatrix::reset() { m_pos.set(0.0f, 0.0f, 0.0f); m_rot.set(0.0f, 0.0f, 0.0f); m_scale.set(1.0f, 1.0f, 1.0f); m_bUseScale = true; }