src/polygoncache.cpp

Sun, 09 Apr 2023 16:30:33 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 09 Apr 2023 16:30:33 +0300
changeset 367
df61cd3d5ab4
parent 358
ef90ed0a5720
child 372
b2914aaeec1a
permissions
-rw-r--r--

Also connect up the "Delete" action

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: 250
diff changeset
1 #include "src/circularprimitive.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: 250
diff changeset
2 #include "src/documentmanager.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: 250
diff changeset
3 #include "src/invert.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: 250
diff changeset
4 #include "src/polygoncache.h"
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
5 #include "src/parser.h"
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
7 QTextDocument* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents)
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
8 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
9 return documents->findDependencyByName(callingModelId, name);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
10 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
11
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
12 static PolygonElement transformed(
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
13 PolygonElement element,
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
14 const glm::mat4& transform)
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
15 {
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
16 visitPoints([&transform](glm::vec3& p) {
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
17 p = transform * glm::vec4{p, 1};
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
18 }, element);
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
19 return element;
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
20 }
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
21
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
22 PolygonCache* findPolygonCacheForModel(QTextDocument* model, DocumentManager* context)
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
23 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
24 std::optional<ModelId> modelId = context->findIdForModel(model);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
25 if (modelId.has_value()) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
26 return context->getPolygonCacheForModel(modelId.value());
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
27 }
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
28 else {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
29 return nullptr;
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
30 }
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
31 }
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
32
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
33 constexpr bool n_xor(bool a)
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
34 {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
35 return a;
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
36 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
37
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
38 template<typename... Args>
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
39 constexpr bool n_xor(bool a, Args&&... rest)
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
40 {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
41 return a xor n_xor(rest...);
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
42 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
43
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
44 namespace {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
45 struct GetPolygonsContext
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
46 {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
47 bool invertnext = false;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
48 ModelId modelId;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
49 class DocumentManager* documents;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
50 };
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
51 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
52
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
53 template<typename Fn, typename Fn2>
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
54 static void inlineSubfileReference(
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
55 const PolygonCache::vector_type& polygons,
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
56 const Colored<SubfileReference>& ref,
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
57 GetPolygonsContext* context,
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
58 Fn&& add,
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
59 Fn2&& reserve)
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
60 {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
61 const bool needInverting = 0
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
62 ^ (glm::determinant(ref.transformation) < 0)
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
63 ^ (context->invertnext);
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
64 reserve(polygons.size());
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
65 for (const PolygonElement& cacheElement : polygons) {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
66 PolygonElement polygon = transformed(cacheElement, ref.transformation);
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
67 if (needInverting) {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
68 gl::invert(polygon);
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
69 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
70 if (polygon.color == MAIN_COLOR) {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
71 polygon.color = ref.color;
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
72 }
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
73 add(1, polygon);
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
74 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
75 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
76
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
77 static QTextDocument* findDependency(const SubfileReference& ref, GetPolygonsContext* context)
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
78 {
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
79 return context->documents->findDependencyByName(context->modelId, ref.name);
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
80 }
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
81
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
82 template<typename Fn, typename Fn2>
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
83 static void collectPolygons(
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
84 const ParsedLine& element,
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
85 Winding& winding,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
86 GetPolygonsContext* context,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
87 Fn&& add,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
88 Fn2&& reserve)
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
89 {
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
90 bool foundinvertnext = false;
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
91 std::visit<void>(overloaded{
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
92 [&](const LineType0& line0) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
93 const QString text = line0.value.text.simplified();
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
94 if (text == QStringLiteral("BFC INVERTNEXT")) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
95 context->invertnext = true;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
96 foundinvertnext = true;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
97 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
98 else if (text == QStringLiteral("BFC CERTIFY CW")) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
99 winding = Clockwise;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
100 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
101 else if (text == QStringLiteral("BFC CERTIFY CCW")) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
102 winding = Anticlockwise;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
103 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
104 else if (text == QStringLiteral("BFC NOCERTIFY")) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
105 winding = NoWinding;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
106 }
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
107 },
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
108 [&](const LineType2& line2) {
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
109 add(2, {line2.value, line2.value.color});
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
110 },
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
111 [&](const LineType3& line3) {
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
112 add(3, {line3.value, line3.value.color});
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
113 },
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
114 [&](const LineType4& line4) {
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
115 add(4, {line4.value, line4.value.color});
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
116 },
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
117 [&](const LineType5& line5) {
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
118 add(5, {line5.value, line5.value.color});
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
119 },
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
120 [&add, context, &reserve](const LineType1& line1) {
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
121 QTextDocument* const dependency = findDependency(line1.value, context);
318
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
122 if (PolygonCache* cache = (dependency != nullptr)
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
123 ? findPolygonCacheForModel(dependency, context->documents)
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
124 : nullptr
216f02b50b0a Reduce indent level
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 309
diff changeset
125 ) {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
126 recacheIfNeeded(cache, dependency, context->documents);
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
127 inlineSubfileReference(cache->polygons, line1.value, context, add, reserve);
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
128 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
129 },
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
130 #if 0
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
131 [&add](const Colored<CircularPrimitive>& circ) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
132 rasterize(circ, [&](const PlainPolygonElement& polygon, ColorIndex color){
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
133 if (color == MAIN_COLOR) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
134 color = circ.color;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
135 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
136 add(PolygonElement{polygon, color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
137 });
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
138 },
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
139 #endif
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
140 }, element);
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
141 if (not foundinvertnext) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
142 context->invertnext = false;
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
143 }
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
144 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
145
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
146 static std::vector<WithId<PolygonElement>> inlinePolygons(
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
147 const QTextDocument* model,
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
148 GetPolygonsContext* context)
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
149 {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
150 Winding winding = NoWinding;
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
151 std::vector<WithId<PolygonElement>> result;
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
152 int i = 0;
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
153 const auto add = [&result, &winding, &i](int lineno, const PolygonElement& poly){
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
154 result.push_back({poly, i});
358
ef90ed0a5720 Hopefully fixed all problems with determining polygon winding
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 338
diff changeset
155 if (lineno != 1 and winding == Winding::Clockwise) {
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
156 gl::invert(result.back());
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
157 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
158 };
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
159 const auto reserve = [&result](std::size_t incomingsize){
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
160 reserveMore(result, incomingsize);
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
161 };
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
162 for (const QString& line : model->toPlainText().split("\n")) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
163 const opt<ParsedLine> parsedline = parse(line);
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
164 if (parsedline.has_value()) {
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
165 collectPolygons(*parsedline, winding, context, add, reserve);
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
166 }
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
167 ++i;
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
168 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
169 return result;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
170 }
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
171
338
719b909a7d2b Delete unused code
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 333
diff changeset
172 void recacheIfNeeded(PolygonCache *cache, QTextDocument *model, DocumentManager *documents)
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
173 {
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
174 if (cache->needRecache)
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
175 {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
176 cache->polygons.clear();
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
177 const std::optional<ModelId> modelId = documents->findIdForModel(model);
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
178 if (modelId.has_value())
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
179 {
333
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
180 GetPolygonsContext context{
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
181 .invertnext = false,
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
182 .modelId = *modelId,
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
183 .documents = documents,
07e65a4c6611 Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 318
diff changeset
184 };
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
185 cache->polygons = inlinePolygons(model, &context);
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
186 }
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
187 cache->needRecache = false;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
188 }
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
189 }

mercurial