90 // Therefore we don't need to give the Unofficial type any special |
90 // Therefore we don't need to give the Unofficial type any special |
91 // consideration. |
91 // consideration. |
92 if (partTypeString.startsWith("Unofficial_")) |
92 if (partTypeString.startsWith("Unofficial_")) |
93 partTypeString = partTypeString.mid(strlen("Unofficial_")); |
93 partTypeString = partTypeString.mid(strlen("Unofficial_")); |
94 header.type = typeStrings.value(partTypeString, LDHeader::Part); |
94 header.type = typeStrings.value(partTypeString, LDHeader::Part); |
95 header.qualfiers = 0; |
95 header.qualfiers = {}; |
96 if (tokens.contains("Alias")) |
96 if (tokens.contains("Alias")) |
97 header.qualfiers |= LDHeader::Alias; |
97 header.qualfiers |= LDHeader::Alias; |
98 if (tokens.contains("Physical_Color")) |
98 if (tokens.contains("Physical_Color")) |
99 header.qualfiers |= LDHeader::PhysicalColour; |
99 header.qualfiers |= LDHeader::PhysicalColour; |
100 if (tokens.contains("Flexible_Section")) |
100 if (tokens.contains("Flexible_Section")) |
277 { |
277 { |
278 throw BodyParseError{"colour was not an integer value"}; |
278 throw BodyParseError{"colour was not an integer value"}; |
279 } |
279 } |
280 } |
280 } |
281 |
281 |
282 static Point3D vertexFromStrings( |
282 static glm::vec3 vertexFromStrings( |
283 const QStringList& tokens, |
283 const QStringList& tokens, |
284 const int startingPosition) |
284 const int startingPosition) |
285 { |
285 { |
286 bool ok_x; |
286 bool ok_x; |
287 const float x = tokens[startingPosition].toFloat(&ok_x); |
287 const float x = tokens[startingPosition].toFloat(&ok_x); |
294 throw BodyParseError{"vertex contained illegal co-ordinates"}; |
294 throw BodyParseError{"vertex contained illegal co-ordinates"}; |
295 } |
295 } |
296 return {x, y, z}; |
296 return {x, y, z}; |
297 } |
297 } |
298 |
298 |
299 static Matrix3x3 matrixFromStrings(const QStringList& tokens, const int startingPosition) |
299 static glm::mat4 matrixFromStrings(const QStringList& tokens, const int startingPosition, const int positionStartingIndex) |
300 { |
300 { |
301 Matrix3x3 result; |
301 glm::mat4 result = glm::mat4{1}; |
302 for (int i = 0; i < 9; i += 1) |
302 for (int i = 0; i < 9; i += 1) |
303 { |
303 { |
304 const int row = i / 3; |
304 const int row = i / 3; |
305 const int column = i % 3; |
305 const int column = i % 3; |
306 const int index = i + startingPosition; |
306 const int index = i + startingPosition; |
307 if (index >= tokens.size()) |
307 if (index >= tokens.size()) |
308 { |
308 { |
309 throw BodyParseError{"too few tokens available"}; |
309 throw BodyParseError{"too few tokens available"}; |
310 } |
310 } |
311 bool ok; |
311 bool ok; |
312 result(row, column) = tokens[index].toFloat(&ok); |
312 // note that glm::mat4 is column-major |
|
313 result[column][row] = tokens[index].toFloat(&ok); |
|
314 if (not ok) |
|
315 { |
|
316 throw BodyParseError{"non-numeric values for matrix"}; |
|
317 } |
|
318 } |
|
319 for (int i = 0; i < 3; i += 1) |
|
320 { |
|
321 bool ok; |
|
322 const auto value = tokens[i + positionStartingIndex].toFloat(&ok); |
|
323 result[3][i] = value; |
313 if (not ok) |
324 if (not ok) |
314 { |
325 { |
315 throw BodyParseError{"non-numeric values for matrix"}; |
326 throw BodyParseError{"non-numeric values for matrix"}; |
316 } |
327 } |
317 } |
328 } |
345 if (tokens.size() != 15) |
356 if (tokens.size() != 15) |
346 { |
357 { |
347 throw BodyParseError{"wrong amount of tokens in a type-1 line"}; |
358 throw BodyParseError{"wrong amount of tokens in a type-1 line"}; |
348 } |
359 } |
349 const Color color = colorFromString(tokens[colorPosition]); |
360 const Color color = colorFromString(tokens[colorPosition]); |
350 const Point3D position = vertexFromStrings(tokens, positionPosition); |
361 const glm::mat4 transform = matrixFromStrings(tokens, transformPosition, positionPosition); |
351 const Matrix3x3 transform = matrixFromStrings(tokens, transformPosition); |
|
352 const QString& name = tokens[namePosition]; |
362 const QString& name = tokens[namePosition]; |
353 return std::make_unique<linetypes::SubfileReference>(combine(transform, position), name, color); |
363 return std::make_unique<linetypes::SubfileReference>(transform, name, color); |
354 } |
364 } |
355 |
365 |
356 template<typename T, int NumVertices> |
366 template<typename T, int NumVertices> |
357 static std::unique_ptr<T> parsePolygon( |
367 static std::unique_ptr<T> parsePolygon( |
358 const QString& line, |
368 const QString& line, |
364 if (tokens.size() != 2 + 3 * NumVertices) |
374 if (tokens.size() != 2 + 3 * NumVertices) |
365 { |
375 { |
366 throw BodyParseError{"wrong amount of tokens"}; |
376 throw BodyParseError{"wrong amount of tokens"}; |
367 } |
377 } |
368 const Color color = colorFromString(tokens[colorPosition]); |
378 const Color color = colorFromString(tokens[colorPosition]); |
369 QVector<Point3D> vertices; |
379 QVector<glm::vec3> vertices; |
370 vertices.reserve(NumVertices); |
380 vertices.reserve(NumVertices); |
371 for (int i = 0; i < NumVertices; i += 1) |
381 for (int i = 0; i < NumVertices; i += 1) |
372 { |
382 { |
373 vertices.append(vertexFromStrings(tokens, vertexPosition(i))); |
383 vertices.append(vertexFromStrings(tokens, vertexPosition(i))); |
374 } |
384 } |