1 | #include "math/TransformMatrix.h" |
---|
2 | |
---|
3 | using namespace projection; |
---|
4 | using namespace gmtl; |
---|
5 | |
---|
6 | /** |
---|
7 | * Constructor. |
---|
8 | * |
---|
9 | * Default transform matrix value is identity matrix. |
---|
10 | */ |
---|
11 | TransformMatrix::TransformMatrix() |
---|
12 | { |
---|
13 | reset (); |
---|
14 | } |
---|
15 | |
---|
16 | /** |
---|
17 | * Destructor. |
---|
18 | */ |
---|
19 | TransformMatrix::~TransformMatrix() |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | /** |
---|
24 | * Retrieve the trasform matrix. |
---|
25 | * |
---|
26 | * @return Trasform matrix. |
---|
27 | */ |
---|
28 | gmtl::Matrix44f TransformMatrix::getMatrix() const |
---|
29 | { |
---|
30 | return makeTrans<Matrix44f>(m_pos) * |
---|
31 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[0]), Vec3f(0.0f, 1.0f, 0.0f))) * |
---|
32 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[1]), Vec3f(1.0f, 0.0f, 0.0f))) * |
---|
33 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[2]), Vec3f(0.0f, 0.0f, 1.0f))) * |
---|
34 | makeScale<Matrix44f>(m_scale); |
---|
35 | } |
---|
36 | |
---|
37 | /** |
---|
38 | * Retrieve the trasform matrix without scaling. |
---|
39 | * |
---|
40 | * @return Trasform matrix without scaling. |
---|
41 | */ |
---|
42 | gmtl::Matrix44f TransformMatrix::getMatrixWOScaling() const |
---|
43 | { |
---|
44 | return makeTrans<Matrix44f>(m_pos) * |
---|
45 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[0]), Vec3f(0.0f, 1.0f, 0.0f))) * |
---|
46 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[1]), Vec3f(1.0f, 0.0f, 0.0f))) * |
---|
47 | makeRot<Matrix44f>(AxisAnglef(Math::deg2Rad(m_rot[2]), Vec3f(0.0f, 0.0f, 1.0f))); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Retrieve transform parameters like 'Rotate 45 1 0 0;'. |
---|
52 | * |
---|
53 | * @param numSpaces Number of space characters for indent. |
---|
54 | * @return Transform params. |
---|
55 | */ |
---|
56 | QString TransformMatrix::getParams(int space) const |
---|
57 | { |
---|
58 | QString spc; |
---|
59 | spc.fill(' ', space); |
---|
60 | |
---|
61 | QString result; |
---|
62 | if (m_scale != Vec3f(1.0f, 1.0f, 1.0f)) |
---|
63 | result += spc + QString("Scale %1 %2 %3;\n").arg(m_scale[0]).arg(m_scale[1]).arg(m_scale[2]); |
---|
64 | if (m_rot[2] != 0.0) |
---|
65 | result += spc + QString("Rotate %1 0 0 1;\n").arg(m_rot[2]); |
---|
66 | if (m_rot[1] != 0.0) |
---|
67 | result += spc + QString("Rotate %1 1 0 0;\n").arg(m_rot[1]); |
---|
68 | if (m_rot[0] != 0.0) |
---|
69 | result += spc + QString("Rotate %1 0 1 0;\n").arg(m_rot[0]); |
---|
70 | if (m_pos != Vec3f(0.0f, 0.0f, 0.0f)) |
---|
71 | result += spc + QString("Translate %1 %2 %3;\n").arg(m_pos[0]).arg(m_pos[1]).arg(m_pos[2]); |
---|
72 | |
---|
73 | return result; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * Restore the transform matrix from XML data. |
---|
78 | * |
---|
79 | * @param element Parent XML element of the transform matrix data. |
---|
80 | */ |
---|
81 | void TransformMatrix::initFromDOMElement(const QDomElement& element) |
---|
82 | { |
---|
83 | QDomElement pos = element.firstChildElement("position"); |
---|
84 | m_pos[0] = pos.attribute("x").toFloat(); |
---|
85 | m_pos[1] = pos.attribute("y").toFloat(); |
---|
86 | m_pos[2] = pos.attribute("z").toFloat(); |
---|
87 | |
---|
88 | QDomElement rot = element.firstChildElement("rotation"); |
---|
89 | m_rot[0] = rot.attribute("h").toFloat(); |
---|
90 | m_rot[1] = rot.attribute("p").toFloat(); |
---|
91 | m_rot[2] = rot.attribute("r").toFloat(); |
---|
92 | |
---|
93 | if (!element.firstChildElement("scaling").isNull()) |
---|
94 | { |
---|
95 | QDomElement scale = element.firstChildElement("scaling"); |
---|
96 | m_scale[0] = scale.attribute("x").toFloat(); |
---|
97 | m_scale[1] = scale.attribute("y").toFloat(); |
---|
98 | m_scale[2] = scale.attribute("z").toFloat(); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Store the current transform matrix as XML data. |
---|
104 | * |
---|
105 | * @param name XML node name of the data. |
---|
106 | * @param doc XML document to store the data. |
---|
107 | * @return Current transform matrix data as XML data. |
---|
108 | */ |
---|
109 | QDomElement TransformMatrix::domElement(const QString& name, QDomDocument& doc) const |
---|
110 | { |
---|
111 | QDomElement de = doc.createElement(name); |
---|
112 | |
---|
113 | QDomElement pos = doc.createElement("position"); |
---|
114 | pos.setAttribute("x", QString::number(m_pos[0])); |
---|
115 | pos.setAttribute("y", QString::number(m_pos[1])); |
---|
116 | pos.setAttribute("z", QString::number(m_pos[2])); |
---|
117 | de.appendChild(pos); |
---|
118 | |
---|
119 | QDomElement rot = doc.createElement("rotation"); |
---|
120 | rot.setAttribute("h", QString::number(m_rot[0])); |
---|
121 | rot.setAttribute("p", QString::number(m_rot[1])); |
---|
122 | rot.setAttribute("r", QString::number(m_rot[2])); |
---|
123 | de.appendChild(rot); |
---|
124 | |
---|
125 | QDomElement scale = doc.createElement("scaling"); |
---|
126 | scale.setAttribute("x", QString::number(m_scale[0])); |
---|
127 | scale.setAttribute("y", QString::number(m_scale[1])); |
---|
128 | scale.setAttribute("z", QString::number(m_scale[2])); |
---|
129 | de.appendChild(scale); |
---|
130 | |
---|
131 | return de; |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Reset the matrix. |
---|
136 | */ |
---|
137 | void TransformMatrix::reset() |
---|
138 | { |
---|
139 | m_pos.set(0.0f, 0.0f, 0.0f); |
---|
140 | m_rot.set(0.0f, 0.0f, 0.0f); |
---|
141 | m_scale.set(1.0f, 1.0f, 1.0f); |
---|
142 | m_bUseScale = true; |
---|
143 | } |
---|