Substitute circular primitives in during file parsing

Mon, 20 Jun 2022 22:22:15 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Mon, 20 Jun 2022 22:22:15 +0300
changeset 242
16855456992d
parent 241
c4e2afabcb90
child 243
959469a7e149

Substitute circular primitives in during file parsing

src/model.cpp file | annotate | diff | comparison | revisions
src/model.h file | annotate | diff | comparison | revisions
src/parser.cpp file | annotate | diff | comparison | revisions
--- a/src/model.cpp	Mon Jun 20 22:21:53 2022 +0300
+++ b/src/model.cpp	Mon Jun 20 22:22:15 2022 +0300
@@ -38,23 +38,7 @@
 
 static constexpr const char* circularPrimitiveTypeString(const CircularPrimitive& circ)
 {
-	switch (circ.type) {
-	case CircularPrimitive::Circle:
-		return "edge";
-	case CircularPrimitive::Disc:
-		return "disc";
-	case CircularPrimitive::Cylinder:
-		return "cyli";
-	case CircularPrimitive::CylinderOpen:
-		return "cylo";
-	case CircularPrimitive::CylinderClosed:
-		return "cylc";
-	case CircularPrimitive::DiscNegative:
-		return "ndis";
-	case CircularPrimitive::Chord:
-		return "chrd";
-	}
-	return "";
+	return circularPrimitiveStems[circ.type];
 }
 
 static QString circularPrimitiveFilePath(const CircularPrimitive& circ)
--- a/src/model.h	Mon Jun 20 22:21:53 2022 +0300
+++ b/src/model.h	Mon Jun 20 22:22:15 2022 +0300
@@ -78,6 +78,16 @@
 	glm::mat4 transformation;
 };
 
+constexpr char circularPrimitiveStems[CircularPrimitive::NUM_TYPES][5] = {
+	"edge",
+	"disc",
+	"cyli",
+	"cylo",
+	"cylc",
+	"ndis",
+	"chrd",
+};
+
 Q_DECLARE_METATYPE(CircularPrimitive::Type)
 
 struct CircleToolOptions
--- a/src/parser.cpp	Mon Jun 20 22:21:53 2022 +0300
+++ b/src/parser.cpp	Mon Jun 20 22:22:15 2022 +0300
@@ -139,7 +139,7 @@
 	return {line.mid(1).trimmed()};
 }
 
-static Colored<SubfileReference> parseType1Line(const QStringList& tokens)
+static ModelElement parseType1Line(const QStringList& tokens)
 {
 	constexpr int colorPosition = 1;
 	constexpr int positionPosition = 2; // 2..4
@@ -152,6 +152,23 @@
 	const ldraw::Color color = colorFromString(tokens[colorPosition]);
 	const glm::mat4 transform = matrixFromStrings(tokens, transformPosition, positionPosition);
 	const QString& name = tokens[namePosition];
+	static QRegExp re{R"((?:(\d+)\\)?(\d+)-(\d)+([a-z]+)\.dat)"};
+	if (re.exactMatch(name)) {
+		const auto p = std::find(std::begin(circularPrimitiveStems), std::end(circularPrimitiveStems), re.cap(4));
+		const unsigned int divisions = (re.cap(1).isEmpty()) ? 16 : re.cap(1).toUInt();
+		const unsigned int segments = re.cap(2).toUInt() * divisions / re.cap(3).toUInt();
+		if (p != std::end(circularPrimitiveStems)) {
+			const auto type = static_cast<CircularPrimitive::Type>(p - std::begin(circularPrimitiveStems));
+			return Colored<CircularPrimitive>{
+				CircularPrimitive{
+					.type = type,
+					.fraction = {segments, divisions},
+					.transformation = transform,
+				},
+				color,
+			};
+		}
+	}
 	return Colored<SubfileReference>{
 		{
 			.name = name,

mercurial