src/polygoncache.cpp

Wed, 29 Jun 2022 14:43:42 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 29 Jun 2022 14:43:42 +0300
changeset 306
6ad27b7d2697
parent 305
d891da20abca
child 309
d862721d19a3
permissions
-rw-r--r--

Enable drawing clockwise shapes despite mapbox::earcut rewinding them

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"
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
6 Model* resolve(const QString& name, const ModelId callingModelId, DocumentManager* documents)
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
7 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
8 return documents->findDependencyByName(callingModelId, name);
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
9 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
10
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
11 static PolygonElement transformed(
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
12 PolygonElement element,
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
13 const glm::mat4& transform)
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
14 {
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
15 visitPoints([&transform](glm::vec3& p) {
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
16 p = transform * glm::vec4{p, 1};
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
17 }, element);
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
18 return element;
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
19 }
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
20
211
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
21 PolygonCache* findPolygonCacheForModel(Model* model, DocumentManager* context)
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
22 {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
23 std::optional<ModelId> modelId = context->findIdForModel(model);
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
24 if (modelId.has_value()) {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
25 return context->getPolygonCacheForModel(modelId.value());
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
26 }
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
27 else {
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
28 return nullptr;
b27b90fb993f Now builds again
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 210
diff changeset
29 }
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
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
32 template<typename Fn, typename Fn2>
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
33 static void collectPolygons(
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
34 const ModelElement& element,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
35 Winding& winding,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
36 GetPolygonsContext* context,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
37 Fn&& add,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
38 Fn2&& reserve)
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
39 {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
40 std::visit<void>(overloaded{
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
41 [&](const Colored<LineSegment>& edge) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
42 add({edge, edge.color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
43 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
44 [&](const Colored<Triangle>& triangle) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
45 add({triangle, triangle.color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
46 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
47 [&](const Colored<Quadrilateral>& quad) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
48 add({quad, quad.color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
49 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
50 [&](const Colored<ConditionalEdge>& cedge) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
51 add({cedge, cedge.color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
52 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
53 [&add, context, &reserve](const Colored<SubfileReference>& ref) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
54 Model* dependency = context->documents->findDependencyByName(context->modelId, ref.name);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
55 PolygonCache* cache = nullptr;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
56 if (dependency != nullptr) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
57 cache = findPolygonCacheForModel(dependency, context->documents);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
58 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
59 if (cache != nullptr) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
60 const bool needInverting = glm::determinant(ref.transformation) < 0;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
61 recacheIfNeeded(cache, dependency, context->documents);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
62 reserve(cache->polygons.size());
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
63 for (const PolygonElement& cacheElement : cache->polygons) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
64 PolygonElement polygon = transformed(cacheElement, ref.transformation);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
65 if (needInverting != ref.inverted) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
66 gl::invert(polygon);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
67 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
68 if (polygon.color == MAIN_COLOR) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
69 polygon.color = ref.color;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
70 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
71 add(polygon);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
72 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
73 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
74 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
75 [&add](const Colored<CircularPrimitive>& circ) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
76 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
77 if (color == MAIN_COLOR) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
78 color = circ.color;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
79 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
80 add(PolygonElement{polygon, color});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
81 });
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
82 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
83 [&winding](const Comment& comment) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
84 if (comment.text == QStringLiteral("BFC CERTIFY CW")) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
85 winding = Clockwise;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
86 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
87 else if (comment.text == QStringLiteral("BFC CERTIFY CCW")) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
88 winding = Anticlockwise;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
89 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
90 else if (comment.text == QStringLiteral("BFC NOCERTIFY")) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
91 winding = NoWinding;
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
92 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
93 },
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
94 [](const ModelElement&) {}
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
95 }, element);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
96 }
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
97
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
98 static std::vector<WithId<PolygonElement>> inlinePolygons(
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
99 const Model* model,
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
100 GetPolygonsContext* context)
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
101 {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
102 Winding winding = NoWinding;
210
232e7634cc8a more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
103 std::vector<WithId<PolygonElement>> result;
250
2837b549e616 I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 232
diff changeset
104 for (std::size_t i = 0; i < model->size(); i += 1)
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
105 {
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
106 const ModelElement& element = (*model)[i];
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
107 const ModelId id = model->idAt(i);
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
108 collectPolygons(element, winding, context,
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
109 [&result, winding, id](const PolygonElement& poly){
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
110 result.push_back({poly, id});
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
111 if (winding == Winding::Clockwise) {
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
112 gl::invert(result.back());
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
113 }
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
114 }, [&result](std::size_t incomingsize){
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
115 reserveMore(result, incomingsize);
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
116 });
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
117 }
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
118 return result;
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
119 }
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
120
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
121 void recacheIfNeeded(PolygonCache *cache, Model *model, DocumentManager *documents)
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
122 {
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
123 if (cache->needRecache)
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 {
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
125 cache->polygons.clear();
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
126 const std::optional<ModelId> modelId = documents->findIdForModel(model);
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 if (modelId.has_value())
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
128 {
200
ca23936b455b Giant refactor
Teemu Piippo <teemu@hecknology.net>
parents: 193
diff changeset
129 GetPolygonsContext context{modelId.value(), documents};
305
d891da20abca Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
130 cache->polygons = inlinePolygons(model, &context);
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
131 }
193
b4beff48bb7a Simplify PolygonCache
Teemu Piippo <teemu@hecknology.net>
parents: 183
diff changeset
132 cache->needRecache = false;
150
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
133 }
b6cbba6e29a1 extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
134 }

mercurial