Mon, 04 Jul 2022 01:20:36 +0300
Add basic syntax highlighting
| 3 | 1 | /* | 
| 2 | * LDForge: LDraw parts authoring CAD | |
| 24 | 3 | * Copyright (C) 2013 - 2020 Teemu Piippo | 
| 3 | 4 | * | 
| 5 | * This program is free software: you can redistribute it and/or modify | |
| 6 | * it under the terms of the GNU General Public License as published by | |
| 7 | * the Free Software Foundation, either version 3 of the License, or | |
| 8 | * (at your option) any later version. | |
| 9 | * | |
| 10 | * This program is distributed in the hope that it will be useful, | |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 13 | * GNU General Public License for more details. | |
| 14 | * | |
| 15 | * You should have received a copy of the GNU General Public License | |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
| 17 | */ | |
| 18 | ||
| 
259
 
c27612f0eac0
- Made it build under Qt6
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
242 
diff
changeset
 | 
19 | #include <QRegExp> | 
| 
 
c27612f0eac0
- Made it build under Qt6
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
242 
diff
changeset
 | 
20 | #include <QIODevice> | 
| 
264
 
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
259 
diff
changeset
 | 
21 | #include "src/ldrawalgorithm.h" | 
| 
 
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
259 
diff
changeset
 | 
22 | #include "src/model.h" | 
| 
 
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
259 
diff
changeset
 | 
23 | #include "src/parser.h" | 
| 3 | 24 | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
25 | struct BodyParseError | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
26 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
27 | QString message; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
28 | }; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
29 | |
| 3 | 30 | /* | 
| 31 | * Constructs an LDraw parser | |
| 32 | */ | |
| 
328
 
3ea38fd469ca
Replace item view with a text editor
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
327 
diff
changeset
 | 
33 | Parser::Parser(QTextStream& stream, QObject* parent) : | 
| 3 | 34 | QObject {parent}, | 
| 
328
 
3ea38fd469ca
Replace item view with a text editor
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
327 
diff
changeset
 | 
35 | stream {stream} {} | 
| 3 | 36 | |
| 37 | /* | |
| 38 | * Reads a single line from the device. | |
| 39 | */ | |
| 40 | QString Parser::readLine() | |
| 41 | { | |
| 
328
 
3ea38fd469ca
Replace item view with a text editor
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
327 
diff
changeset
 | 
42 | return this->stream.readLine().trimmed(); | 
| 3 | 43 | } | 
| 44 | ||
| 45 | /** | |
| 46 | * @brief Parses the model body into the given model. | |
| 47 | * @param editor Handle to model edit context | |
| 48 | */ | |
| 152 | 49 | void Parser::parseBody(Model& model) | 
| 3 | 50 | { | 
| 51 | bool invertNext = false; | |
| 
328
 
3ea38fd469ca
Replace item view with a text editor
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
327 
diff
changeset
 | 
52 | while (not this->stream.atEnd()) | 
| 3 | 53 | { | 
| 
144
 
5d73a6717321
Fix handling of "BFC INVERTNEXT" with multiple inner whitespaces
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
141 
diff
changeset
 | 
54 | // Some LDraw parts such as 53588.dat can contain "BFC INVERTNEXT" with multiple inner whitespaces. | 
| 
 
5d73a6717321
Fix handling of "BFC INVERTNEXT" with multiple inner whitespaces
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
141 
diff
changeset
 | 
55 | // So we need to pass the string through QString::simplified to catch these cases. | 
| 327 | 56 | const QString line = this->readLine().trimmed(); | 
| 
176
 
cd9d6bf6f649
Simplify parsing (removed header parsing)
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
152 
diff
changeset
 | 
57 | if (line == "0 BFC INVERTNEXT" or line == "0 BFC CERTIFY INVERTNEXT") | 
| 3 | 58 | { | 
| 59 | invertNext = true; | |
| 60 | continue; | |
| 61 | } | |
| 200 | 62 | ModelElement element = parseLDrawLine(line); | 
| 3 | 63 | if (invertNext) | 
| 64 | { | |
| 200 | 65 | element = inverted(element); | 
| 3 | 66 | } | 
| 200 | 67 | model.append(element); | 
| 3 | 68 | invertNext = false; | 
| 69 | } | |
| 70 | } | |
| 71 | ||
| 
35
 
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
33 
diff
changeset
 | 
72 | static ldraw::Color colorFromString(const QString& colorString) | 
| 
4
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
73 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
74 | bool colorSucceeded; | 
| 
35
 
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
33 
diff
changeset
 | 
75 | const ldraw::Color color = {colorString.toInt(&colorSucceeded)}; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
76 | if (colorSucceeded) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
77 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
78 | return color; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
79 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
80 | else | 
| 
4
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
81 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
82 | throw BodyParseError{"colour was not an integer value"}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
83 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
84 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
85 | |
| 
33
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
86 | static glm::vec3 vertexFromStrings( | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
87 | const QStringList& tokens, | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
88 | const int startingPosition) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
89 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
90 | bool ok_x; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
91 | const float x = tokens[startingPosition].toFloat(&ok_x); | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
92 | bool ok_y; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
93 | const float y = tokens[startingPosition + 1].toFloat(&ok_y); | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
94 | bool ok_z; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
95 | const float z = tokens[startingPosition + 2].toFloat(&ok_z); | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
96 | if (not ok_x or not ok_y or not ok_z) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
97 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
98 | throw BodyParseError{"vertex contained illegal co-ordinates"}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
99 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
100 | return {x, y, z}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
101 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
102 | |
| 115 | 103 | static glm::mat4 matrixFromStrings( | 
| 104 | const QStringList& tokens, | |
| 105 | const int startingPosition, | |
| 106 | const int positionStartingIndex) | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
107 | { | 
| 
33
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
108 | glm::mat4 result = glm::mat4{1}; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
109 | for (int i = 0; i < 9; i += 1) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
110 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
111 | const int row = i / 3; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
112 | const int column = i % 3; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
113 | const int index = i + startingPosition; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
114 | if (index >= tokens.size()) | 
| 
4
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
115 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
116 | throw BodyParseError{"too few tokens available"}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
117 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
118 | bool ok; | 
| 
33
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
119 | // note that glm::mat4 is column-major | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
120 | result[column][row] = tokens[index].toFloat(&ok); | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
121 | if (not ok) | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
122 | { | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
123 | throw BodyParseError{"non-numeric values for matrix"}; | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
124 | } | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
125 | } | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
126 | for (int i = 0; i < 3; i += 1) | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
127 | { | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
128 | bool ok; | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
129 | const auto value = tokens[i + positionStartingIndex].toFloat(&ok); | 
| 
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
130 | result[3][i] = value; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
131 | if (not ok) | 
| 
4
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
132 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
133 | throw BodyParseError{"non-numeric values for matrix"}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
134 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
135 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
136 | return result; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
137 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
138 | |
| 200 | 139 | static Comment parseType0Line(const QString& line) | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
140 | { | 
| 200 | 141 | return {line.mid(1).trimmed()}; | 
| 
4
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
142 | } | 
| 
 
68988ebc2a68
added regular expressions for the parser
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
3 
diff
changeset
 | 
143 | |
| 
242
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
144 | static ModelElement parseType1Line(const QStringList& tokens) | 
| 5 | 145 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
146 | constexpr int colorPosition = 1; | 
| 
23
 
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
21 
diff
changeset
 | 
147 | constexpr int positionPosition = 2; // 2..4 | 
| 
 
3387a84ddaba
fixed a pile of nonsense that caused subfiles to go haywire
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
21 
diff
changeset
 | 
148 | constexpr int transformPosition = 5; // 5..13 | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
149 | constexpr int namePosition = 14; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
150 | if (tokens.size() != 15) | 
| 5 | 151 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
152 | throw BodyParseError{"wrong amount of tokens in a type-1 line"}; | 
| 5 | 153 | } | 
| 
35
 
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
33 
diff
changeset
 | 
154 | const ldraw::Color color = colorFromString(tokens[colorPosition]); | 
| 
33
 
4c41bfe2ec6e
replaced matrix and vertex classes with glm
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
26 
diff
changeset
 | 
155 | const glm::mat4 transform = matrixFromStrings(tokens, transformPosition, positionPosition); | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
156 | const QString& name = tokens[namePosition]; | 
| 
242
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
157 | static QRegExp re{R"((?:(\d+)\\)?(\d+)-(\d)+([a-z]+)\.dat)"}; | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
158 | if (re.exactMatch(name)) { | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
159 | const auto p = std::find(std::begin(circularPrimitiveStems), std::end(circularPrimitiveStems), re.cap(4)); | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
160 | const unsigned int divisions = (re.cap(1).isEmpty()) ? 16 : re.cap(1).toUInt(); | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
161 | const unsigned int segments = re.cap(2).toUInt() * divisions / re.cap(3).toUInt(); | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
162 | if (p != std::end(circularPrimitiveStems)) { | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
163 | const auto type = static_cast<CircularPrimitive::Type>(p - std::begin(circularPrimitiveStems)); | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
164 | return Colored<CircularPrimitive>{ | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
165 | CircularPrimitive{ | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
166 | .type = type, | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
167 | .fraction = {segments, divisions}, | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
168 | .transformation = transform, | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
169 | }, | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
170 | color, | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
171 | }; | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
172 | } | 
| 
 
16855456992d
Substitute circular primitives in during file parsing
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
206 
diff
changeset
 | 
173 | } | 
| 200 | 174 | return Colored<SubfileReference>{ | 
| 175 | { | |
| 176 | .name = name, | |
| 177 | .transformation = transform, | |
| 178 | }, | |
| 179 | color, | |
| 180 | }; | |
| 5 | 181 | } | 
| 182 | ||
| 200 | 183 | template<int NumVertices> | 
| 184 | static auto parsePolygon(const QStringList& tokens) | |
| 5 | 185 | { | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
186 | constexpr int colorPosition = 1; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
187 | auto vertexPosition = [](int n) { return 2 + 3*n; }; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
188 | if (tokens.size() != 2 + 3 * NumVertices) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
189 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
190 | throw BodyParseError{"wrong amount of tokens"}; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
191 | } | 
| 
35
 
98906a94732f
renamed the linetypes namespace to ldraw namespace and added more structures to it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
33 
diff
changeset
 | 
192 | const ldraw::Color color = colorFromString(tokens[colorPosition]); | 
| 
77
 
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
35 
diff
changeset
 | 
193 | std::array<glm::vec3, NumVertices> vertices; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
194 | for (int i = 0; i < NumVertices; i += 1) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
195 | { | 
| 
77
 
028798a72591
added some meta stuff, simplified quadrilateral splitting and tested it
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
35 
diff
changeset
 | 
196 | vertices[unsigned_cast(i)] = vertexFromStrings(tokens, vertexPosition(i)); | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
197 | } | 
| 200 | 198 | return std::make_pair(vertices, color); | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
199 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
200 | |
| 200 | 201 | ModelElement parseLDrawLine(QString line) | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
202 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
203 | try | 
| 5 | 204 | { | 
| 
259
 
c27612f0eac0
- Made it build under Qt6
 
Teemu Piippo <teemu.s.piippo@gmail.com> 
parents: 
242 
diff
changeset
 | 
205 | const QStringList tokens = line.simplified().split(" "); | 
| 12 | 206 | if (tokens.empty() or tokens == QStringList{{""}}) | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
207 | { | 
| 200 | 208 | return Empty{}; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
209 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
210 | bool ok_code; | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
211 | const int code = tokens[0].toInt(&ok_code); | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
212 | if (not ok_code) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
213 | { | 
| 
206
 
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
201 
diff
changeset
 | 
214 | throw BodyParseError{QObject::tr("line type was not an integer")}; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
215 | } | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
216 | switch (code) | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
217 | { | 
| 
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
218 | case 0: | 
| 200 | 219 | return parseType0Line(line); | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
220 | case 1: | 
| 200 | 221 | return parseType1Line(tokens); | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
222 | case 2: | 
| 200 | 223 | { | 
| 224 | const auto pair = parsePolygon<2>(tokens); | |
| 225 | return Colored<LineSegment>{{pair.first[0], pair.first[1]}, pair.second}; | |
| 226 | } | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
227 | case 3: | 
| 200 | 228 | { | 
| 229 | const auto pair = parsePolygon<3>(tokens); | |
| 230 | return Colored<Triangle>{{pair.first[0], pair.first[1], pair.first[2]}, pair.second | |
| 231 | }; | |
| 232 | } | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
233 | case 4: | 
| 200 | 234 | { | 
| 235 | const auto pair = parsePolygon<4>(tokens); | |
| 236 | const Quadrilateral quad{pair.first[0], pair.first[1], pair.first[2], pair.first[3]}; | |
| 237 | return Colored<Quadrilateral>{quad, pair.second}; | |
| 238 | } | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
239 | case 5: | 
| 200 | 240 | { | 
| 241 | const auto pair = parsePolygon<4>(tokens); | |
| 242 | const ConditionalEdge cedge{pair.first[0], pair.first[1], pair.first[2], pair.first[3]}; | |
| 243 | return Colored<ConditionalEdge>{cedge, pair.second}; | |
| 244 | } | |
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
245 | default: | 
| 
206
 
654661eab7f3
More refactor, merged main.h, basics.h and utility.h into one header file basics.h and removed plenty of unused code
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
201 
diff
changeset
 | 
246 | throw BodyParseError{QObject::tr("bad line type '%1'").arg(code)}; | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
247 | } | 
| 5 | 248 | } | 
| 
8
 
44679e468ba9
major update with many things
 
Teemu Piippo <teemu@hecknology.net> 
parents: 
5 
diff
changeset
 | 
249 | catch(const BodyParseError& error) | 
| 5 | 250 | { | 
| 200 | 251 | return ParseError{line}; | 
| 5 | 252 | } | 
| 253 | } |