-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransformmatrixgenerator.cpp
353 lines (285 loc) · 10.7 KB
/
transformmatrixgenerator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
transformmatrixgenerator.cpp (part of GNSS-Stylus)
Copyright (C) 2020-2021 Pasi Nuutinmaki (gnssstylist<at>sci<dot>fi)
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 <https://www.gnu.org/licenses/>.
*/
#include "transformmatrixgenerator.h"
#if 0
#define TRANSFORMMATRIXGENERATORDEBUGOUTPUT
#include <QtDebug>
inline QDebug operator<<(QDebug dbg, const Eigen::Matrix3d& m)
{
dbg << QString::number(m(0,0),'f',3) << ", " << QString::number(m(0,1),'f',3) << ", " << QString::number(m(0,2),'f',3) << "\n" <<
QString::number(m(1,0),'f',3) << ", " << QString::number(m(1,1),'f',3) << ", " << QString::number(m(1,2),'f',3) << "\n" <<
QString::number(m(2,0),'f',3) << ", " << QString::number(m(2,1),'f',3) << ", " << QString::number(m(2,2),'f',3);
return dbg.space();
}
inline QDebug operator<<(QDebug dbg, const Eigen::Transform<double, 3, Eigen::Affine> t)
{
Eigen::Matrix4d m = t.matrix();
dbg << QString::number(m(0,0),'f',3) << ", " << QString::number(m(0,1),'f',3) << ", " << QString::number(m(0,2),'f',3) << ", " << QString::number(m(0,3),'f',3) << "\n" <<
QString::number(m(1,0),'f',3) << ", " << QString::number(m(1,1),'f',3) << ", " << QString::number(m(1,2),'f',3) << ", " << QString::number(m(1,3),'f',3) << "\n" <<
QString::number(m(2,0),'f',3) << ", " << QString::number(m(2,1),'f',3) << ", " << QString::number(m(2,2),'f',3) << ", " << QString::number(m(2,3),'f',3) << "\n" <<
QString::number(m(3,0),'f',3) << ", " << QString::number(m(3,1),'f',3) << ", " << QString::number(m(3,2),'f',3) << ", " << QString::number(m(3,3),'f',3);
return dbg.space();
}
#endif
TransformMatrixGenerator::TransformMatrixGenerator()
{
}
TransformMatrixGenerator::Item::Item(const QByteArray& text, const int lineNumber, const int firstCol, const int lastCol)
{
this->text = text;
this->lineNumber =lineNumber;
this->firstCol = firstCol;
this->lastCol = lastCol;
};
Eigen::Transform<double, 3, Eigen::Affine> TransformMatrixGenerator::generate(const QStringList &lines)
{
int lineNumber = 0;
QVector<Item> command;
QByteArray subString;
int firstCol = -1;
QVector<Eigen::Transform<double, 3, Eigen::Affine>> matrices;
while (lineNumber < lines.count())
{
QString line = lines[lineNumber];
int i = 0;
// int endCol = -1;
while (i < line.length())
{
char character = line.at(i).toLatin1();
if (!character)
{
Issue error;
error.text = "Only Latin-1 (\"8-bit ascii\") characters allowed in non-comment sections.";
error.item.lineNumber = lineNumber;
error.item.firstCol = i;
error.item.lastCol = i;
throw error;
}
else if ((line.length() >= (i + 2)) && (character == '/') && (line.at(i+1).toLatin1() == '/'))
{
// Rest of the line is comment -> Skip it
break;
}
else if ((character == ' ') || (character == '\t'))
{
// Command/argument separator
if (!subString.isEmpty())
{
Item newItem;
newItem.text = subString;
newItem.lineNumber = lineNumber;
newItem.firstCol = firstCol;
newItem.lastCol = i - 1;
command.push_back(newItem);
subString.clear();
}
}
else if (character == ';')
{
if (!subString.isEmpty())
{
Item newItem;
newItem.text = subString;
newItem.lineNumber = lineNumber;
newItem.firstCol = firstCol;
newItem.lastCol = i - 1;
command.push_back(newItem);
subString.clear();
}
if (command.size() == 0)
{
break;
}
matrices.push_back(processCommand(command));
command.clear();
}
else
{
// Non-control character -> add it to the buffer
if (subString.length() == 0)
{
firstCol = i;
}
subString += character;
}
i++;
}
if (!subString.isEmpty())
{
Item newItem;
newItem.text = subString;
newItem.lineNumber = lineNumber;
newItem.firstCol = firstCol;
newItem.lastCol = i - 1;
command.push_back(newItem);
subString.clear();
}
lineNumber++;
}
if (!command.isEmpty())
{
Issue error;
error.text = "Unterminated command in the end.";
error.item = command.at(0);
throw error;
}
Eigen::Transform<double, 3, Eigen::Affine> matrix;
matrix = matrix.Identity();
for (int i = matrices.size() - 1; i >= 0; i--)
{
matrix = matrix * matrices.at(i);
}
#ifdef TRANSFORMMATRIXGENERATORDEBUGOUTPUT
qDebug() << "Final transform matrix generated:";
qDebug() << matrix;
#endif
return matrix;
}
Eigen::Transform<double, 3, Eigen::Affine> TransformMatrixGenerator::processCommand(const QVector<Item>& command)
{
QByteArray cmd0 = command.at(0).text.toLower();
Eigen::Transform<double, 3, Eigen::Affine> matrix = matrix.Identity();
if (cmd0 == "rotate")
{
matrix = cmd_Rotate(command);
}
else if (cmd0 == "translate")
{
matrix = cmd_Translate(command);
}
else if (cmd0 == "multiply")
{
matrix = cmd_Multiply(command);
}
else
{
Issue error;
error.text = "Unknown command \"" + command.at(0).text + "\"";
error.item = command.at(0);
throw error;
}
#ifdef TRANSFORMMATRIXGENERATORDEBUGOUTPUT
qDebug() << "Transform matrix generated by command \"" << command.at(0).text << "\" at line " +
QString::number(command.at(0).lineNumber);
qDebug() << matrix;
#endif
return matrix;
}
QVector<double> TransformMatrixGenerator::convertItemsToDoubles(const QVector<Item>& command, const unsigned int startItem, const unsigned int numOfItems)
{
// Tautologous (and keep other code that way!) checkArgumentCount(command, startItem + numOfItems - 1);
QVector<double> retVals;
for (unsigned int i = 0; i < numOfItems; i++)
{
bool convOk;
Item arg = command.at(startItem + i);
double val = arg.text.toDouble(&convOk);
if (!convOk)
{
Issue error;
error.text = "Can not convert argument to double (real).";
error.item = arg;
throw error;
}
retVals.push_back(val);
}
return retVals;
}
double TransformMatrixGenerator::getAngleMultiplier(const Item& item)
{
if (item.text.toLower() == "rad")
{
return 1;
}
else if (item.text.toLower() == "deg")
{
return 2 * M_PI / 360;
}
else
{
Issue error;
error.text = "Invalid angle unit.";
error.item = item;
throw error;
}
}
void TransformMatrixGenerator::checkArgumentCount(const QVector<Item>& command, const int argsNeeded)
{
int argNum = command.count() - 1;
if (argNum < argsNeeded)
{
Issue error;
error.text = "Not enough arguments. Required " + QString::number(argsNeeded) + ", got " + QString::number(argNum);
error.item = command.at(0);
throw error;
}
else if (argNum > argsNeeded)
{
Issue error;
error.text = "Too many arguments. Required " + QString::number(argsNeeded) + ", got " + QString::number(argNum);
error.item = command.at(argsNeeded + 1);
throw error;
}
}
Eigen::Transform<double, 3, Eigen::Affine> TransformMatrixGenerator::cmd_Rotate(const QVector<Item>& command)
{
Eigen::Transform<double, 3, Eigen::Affine> matrix = matrix.Identity();
checkArgumentCount(command, 3);
Eigen::Vector3d rotationAxis;
QByteArray axisName = command.at(1).text.toLower();
if ((axisName == "n") || (axisName == "x") || (axisName == "roll"))
{
rotationAxis = Eigen::Vector3d::UnitX();
}
else if ((axisName == "e") || (axisName == "y") || (axisName == "pitch"))
{
rotationAxis = Eigen::Vector3d::UnitY();
}
else if ((axisName == "d") || (axisName == "z") || (axisName == "yaw") || (axisName == "heading"))
{
rotationAxis = Eigen::Vector3d::UnitZ();
}
else
{
Issue error;
error.text = "Unknown axis \"" + command.at(1).text + "\"";
error.item = command.at(1);
throw error;
}
QVector<double> angle = convertItemsToDoubles(command, 2, 1);
double angleMultiplier = getAngleMultiplier(command.at(3));
matrix.rotate(Eigen::AngleAxisd(angle.at(0) * angleMultiplier, rotationAxis));
return matrix;
}
Eigen::Transform<double, 3, Eigen::Affine> TransformMatrixGenerator::cmd_Translate(const QVector<Item>& command)
{
Eigen::Transform<double, 3, Eigen::Affine> matrix = matrix.Identity();
checkArgumentCount(command, 3);
QVector<double> translationValues = convertItemsToDoubles(command, 1, 3);
Eigen::Vector3d translation(translationValues.at(0), translationValues.at(1), translationValues.at(2));
matrix.translate(translation);
return matrix;
}
Eigen::Transform<double, 3, Eigen::Affine> TransformMatrixGenerator::cmd_Multiply(const QVector<Item>& command)
{
Eigen::Transform<double, 3, Eigen::Affine> matrix = matrix.Identity();
checkArgumentCount(command, 12);
QVector<double> translationValues = convertItemsToDoubles(command, 1, 12);
matrix.matrix() << translationValues.at(0), translationValues.at(1), translationValues.at(2), translationValues.at(3),
translationValues.at(4), translationValues.at(5), translationValues.at(6), translationValues.at(7),
translationValues.at(8), translationValues.at(9), translationValues.at(10), translationValues.at(11),
0, 0, 0, 1;
return matrix;
}