src/document.cpp

Wed, 15 Jun 2022 13:03:33 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 15 Jun 2022 13:03:33 +0300
changeset 229
38d86002d548
parent 228
948867719906
child 232
8efa3a33172e
permissions
-rw-r--r--

Add About Qt menu entry

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 21
diff changeset
18
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents: 39
diff changeset
19 #include <QMouseEvent>
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
20 #include <QPainter>
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
21 #include "algorithm/earcut.h"
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #include "document.h"
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #include "model.h"
191
d355d4c52d51 made editing tools not a polymorphic class tree
Teemu Piippo <teemu@hecknology.net>
parents: 188
diff changeset
24 #include "ui/objecteditor.h"
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
25 #include "gl/partrenderer.h"
125
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
26
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
27 // Make mapbox::earcut work with glm::vec3
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
28 namespace mapbox {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
29 namespace util {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
30 template<> struct nth<0, glm::vec3>
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
31 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
32 static constexpr float get(const glm::vec3& t) { return t.x; };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
33 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
34 template<> struct nth<1, glm::vec3>
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
35 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
36 static constexpr float get(const glm::vec3& t) { return t.y; };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
37 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
38 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
39 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
40
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
41 EditTools::EditTools(QObject* parent) :
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
42 QObject{parent},
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
43 RenderLayer{}
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
47 EditTools::~EditTools()
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 {
9
8b9780700b5e added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
49 }
36
bbb901b97404 added render style storage
Teemu Piippo <teemu@hecknology.net>
parents: 35
diff changeset
50
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
51 void EditTools::setEditMode(EditingMode mode)
125
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
52 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
53 this->mode = mode;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
54 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
55
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
56 void EditTools::setGridMatrix(const glm::mat4& gridMatrix)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
57 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
58 this->gridMatrix = gridMatrix;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
59 this->gridPlane = planeFromTriangle({
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
60 this->gridMatrix * glm::vec4{0, 0, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
61 this->gridMatrix * glm::vec4{1, 0, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
62 this->gridMatrix * glm::vec4{0, 1, 0, 1},
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
63 });
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
64 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
65
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
66 void EditTools::mvpMatrixChanged(const glm::mat4& matrix)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
67 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
68 this->mvpMatrix = matrix;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
69 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
70
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
71 void EditTools::mouseMoved(const QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
72 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
73 this->worldPosition = this->renderer->screenToModelCoordinates(event->pos(), this->gridPlane);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
74 if (this->worldPosition.has_value())
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
75 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
76 // Snap the position to grid. This procedure is basically the "change of basis" and almost follows the
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
77 // A⁻¹ × M × A formula which is used to perform a transformation in some other coordinate system, except
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
78 // we actually use the inverted matrix first and the regular one last to perform the transformation of
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
79 // grid coordinates in our XY coordinate system. Also, we're rounding the coordinates which is obviously
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
80 // not a linear transformation, but fits the pattern anyway.
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
81 // First transform the coordinates to the XY plane...
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
82 this->worldPosition = glm::inverse(this->gridMatrix) * glm::vec4{*this->worldPosition, 1};
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
83 // Then round the coordinates to integer precision...
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
84 this->worldPosition = glm::round(*this->worldPosition);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
85 // And finally transform it back to grid coordinates by transforming it with the
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
86 // grid matrix.
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
87 this->worldPosition = this->gridMatrix * glm::vec4{*this->worldPosition, 1};
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
88 this->polygon.back() = *this->worldPosition;
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
89 }
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
90 this->numpoints = this->polygon.size();
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
91 if (this->isCloseToExistingPoints()) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
92 this->numpoints -= 1;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
93 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
94 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
95
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
96 static QVector<QPointF> convertWorldPointsToScreenPoints(
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
97 const std::vector<glm::vec3> &worldPoints,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
98 const PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
99 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
100 QVector<QPointF> points2d;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
101 points2d.reserve(worldPoints.size());
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
102 for (const glm::vec3& point : worldPoints)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
103 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
104 points2d.push_back(renderer->modelToScreenCoordinates(point));
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
105 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
106 return points2d;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
107 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
108
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
109 static Winding worldPolygonWinding(
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
110 const std::vector<glm::vec3> &points,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
111 const PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
112 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
113 return winding(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)});
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
114 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
115
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
116 static void drawWorldPoint(
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
117 QPainter* painter,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
118 const glm::vec3& worldPoint,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
119 const PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
120 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
121 const QPointF center = renderer->modelToScreenCoordinates(worldPoint);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
122 painter->drawEllipse(inscribe(CircleF{center, 5}));
125
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
123 }
f127982d3412 Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents: 124
diff changeset
124
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
125 static void drawWorldPolyline(
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
126 QPainter *painter,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
127 const std::vector<glm::vec3> &points,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
128 const PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
129 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
130 painter->drawPolyline(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)});
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
131 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
132
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
133 static void drawWorldPolygon(
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
134 QPainter* painter,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
135 const std::vector<glm::vec3> &points,
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
136 const PartRenderer* renderer)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
137 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
138 painter->drawPolygon(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)});
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
139 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
140
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
141 static opt<std::vector<glm::vec3>> modelActionPoints(const ModelAction& action)
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
142 {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
143 opt<std::vector<glm::vec3>> result;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
144 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
145 const ModelElement& newElement = append->newElement;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
146 if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
147 result = {seg->p1, seg->p2};
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
148 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
149 else if (const Triangle* tri = std::get_if<Colored<Triangle>>(&newElement)) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
150 result = {tri->p1, tri->p2, tri->p3};
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
151 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
152 else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
153 result = {quad->p1, quad->p2, quad->p3, quad->p4};
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
154 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
155 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
156 return result;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
157 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
158
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
159 namespace {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
160 struct Pens
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
161 {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
162 const QBrush pointBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
163 const QPen pointPen;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
164 const QPen polygonPen;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
165 const QPen badPolygonPen;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
166 const QBrush greenPolygonBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
167 const QBrush redPolygonBrush;
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
168 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
169 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
170
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
171 static const Pens brightPens{
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
172 .pointBrush = {Qt::white},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
173 .pointPen = {QBrush{Qt::black}, 2.0},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
174 .polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
175 .greenPolygonBrush = {QColor{64, 255, 128, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
176 .redPolygonBrush = {QColor{255, 96, 96, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
177 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
178
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
179 static const Pens darkPens{
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
180 .pointBrush = {Qt::black},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
181 .pointPen = {QBrush{Qt::white}, 2.0},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
182 .polygonPen = {QBrush{Qt::white}, 2.0, Qt::DashLine},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
183 .greenPolygonBrush = {QColor{64, 255, 128, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
184 .redPolygonBrush = {QColor{255, 96, 96, 192}},
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
185 };
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
186
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
187 void EditTools::overpaint(QPainter* painter)
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
188 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
189 const Pens& pens = (this->renderer->isDark() ? darkPens : brightPens);
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
190 this->renderPreview(painter, &pens);
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
191 if (this->worldPosition.has_value())
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
192 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
193 painter->setRenderHint(QPainter::Antialiasing);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
194 painter->setPen(Qt::white);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
195 painter->setBrush(Qt::green);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
196 const QPointF pos = this->renderer->modelToScreenCoordinates(*this->worldPosition);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
197 painter->drawEllipse(pos, 5, 5);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
198 painter->drawText(pos + QPointF{5, 5}, vectorToString(*this->worldPosition));
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
199 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
200 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
201
228
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
202 void EditTools::renderPreview(QPainter* painter, const void* pensptr)
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
203 {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
204 const Pens& pens = *reinterpret_cast<const Pens*>(pensptr);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
205 painter->setPen(pens.polygonPen);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
206 for (const ModelAction& action : this->actions()) {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
207 const std::vector<glm::vec3> points = modelActionPoints(action).value_or(std::vector<glm::vec3>{});
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
208 if (points.size() == 2) {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
209 drawWorldPolyline(painter, points, renderer);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
210 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
211 else {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
212 if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
213 painter->setBrush(pens.greenPolygonBrush);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
214 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
215 else {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
216 painter->setBrush(pens.redPolygonBrush);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
217 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
218 drawWorldPolygon(painter, points, this->renderer);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
219 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
220 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
221 painter->setBrush(pens.pointBrush);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
222 painter->setPen(pens.pointPen);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
223 for (const glm::vec3& point : this->polygon) {
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
224 drawWorldPoint(painter, point, this->renderer);
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
225 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
226 }
948867719906 refactor
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 227
diff changeset
227
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
228 void EditTools::removeLastPoint()
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
229 {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
230 if (this->polygon.size() > 1) {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
231 this->polygon.erase(this->polygon.end() - 1);
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
232 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
233 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
234
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
235 bool EditTools::isCloseToExistingPoints() const
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
236 {
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
237 if (this->worldPosition.has_value()) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
238 const glm::vec3& pos = *this->worldPosition;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
239 return std::any_of(this->polygon.begin(), this->polygon.end() - 1, [&pos](const glm::vec3& p){
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
240 return isclose(pos, p);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
241 });
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
242 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
243 else {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
244 return false;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
245 }
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
246 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
247
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
248 EditingMode EditTools::currentEditingMode() const
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
249 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
250 return this->mode;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
251 }
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
252
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
253 void EditTools::mouseClick(const QMouseEvent* event)
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
254 {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
255 switch(this->mode) {
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
256 case SelectMode:
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
257 if (event->button() == Qt::LeftButton) {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
258 const ModelId highlighted = this->renderer->pick(event->pos());
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
259 Q_EMIT this->select({highlighted}, false);
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
260 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
261 break;
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
262 case DrawMode:
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
263 if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) {
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
264 if (isCloseToExistingPoints()) {
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
265 this->closeShape();
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
266 }
214
8e1fe64ce4e3 begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 205
diff changeset
267 else {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
268 this->polygon.push_back(*this->worldPosition);
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
269 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
270 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
271 else if (true
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
272 and event->button() == Qt::RightButton
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
273 and this->polygon.size() > 1
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
274 ) {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
275 this->removeLastPoint();
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
276 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
277 break;
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
278 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
279 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
280
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
281 constexpr float distancesquared(const glm::vec3& p1, const glm::vec3& p2)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
282 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
283 const float dx = p2.x - p1.x;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
284 const float dy = p2.y - p1.y;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
285 const float dz = p2.z - p1.z;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
286 return (dx * dx) + (dy * dy) + (dz * dz);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
287 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
288
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
289 inline float area(const Quadrilateral& q)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
290 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
291 return 0.5 * (
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
292 glm::length(glm::cross(q.p2 - q.p1, q.p3 - q.p1)) +
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
293 glm::length(glm::cross(q.p3 - q.p2, q.p4 - q.p2))
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
294 );
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
295 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
296
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
297 inline float energy(const Quadrilateral& q)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
298 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
299 const float L2 = distancesquared(q.p1, q.p2)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
300 + distancesquared(q.p2, q.p3)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
301 + distancesquared(q.p3, q.p4)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
302 + distancesquared(q.p4, q.p1);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
303 return 1 - 6.928203230275509 * area(q) / L2;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
304 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
305
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
306 struct MergedTriangles
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
307 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
308 std::vector<Quadrilateral> quadrilaterals;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
309 std::set<std::size_t> cutTriangles;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
310 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
311
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
312 static MergedTriangles mergeTriangles(
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
313 const std::vector<std::uint16_t>& indices,
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
314 const std::vector<glm::vec3>& polygon)
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
315 {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
316 MergedTriangles result;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
317 using indextype = std::uint16_t;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
318 using indexpair = std::pair<indextype, indextype>;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
319 struct boundaryinfo { indextype third; std::size_t triangleid; };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
320 std::map<indexpair, boundaryinfo> boundaries;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
321 for (std::size_t i = 0; i < indices.size(); i += 3) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
322 const auto add = [&](int o1, int o2, int o3){
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
323 const auto key = std::make_pair(indices[i + o1], indices[i + o2]);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
324 boundaries[key] = {indices[i + o3], i};
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
325 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
326 add(0, 1, 2);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
327 add(1, 2, 0);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
328 add(2, 0, 1);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
329 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
330 std::vector<std::array<indextype, 4>> quadindices;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
331 std::vector<Quadrilateral> quads;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
332 bool repeat = true;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
333 const auto iscut = [&result](const std::size_t i){
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
334 return result.cutTriangles.find(i) != result.cutTriangles.end();
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
335 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
336 while (repeat) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
337 repeat = false;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
338 // Go through triangle boundaries
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
339 for (const auto& it1 : boundaries) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
340 const indexpair& pair1 = it1.first;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
341 const boundaryinfo& boundary1 = it1.second;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
342 // .. the ones we haven't already merged anyway
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
343 if (not iscut(boundary1.triangleid)) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
344 // Look for its inverse boundary to find the touching triangle
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
345 const auto pair2 = std::make_pair(pair1.second, pair1.first);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
346 const auto it2 = boundaries.find(pair2);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
347 // Also if that hasn't been cut
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
348 if (it2 != boundaries.end() and not iscut(it2->second.triangleid)) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
349 const Quadrilateral quad{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
350 polygon[pair1.first],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
351 polygon[it2->second.third],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
352 polygon[pair1.second],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
353 polygon[boundary1.third],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
354 };
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
355 if (isConvex(quad)) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
356 result.quadrilaterals.push_back(quad);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
357 result.cutTriangles.insert(boundary1.triangleid);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
358 result.cutTriangles.insert(it2->second.triangleid);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
359 repeat = true;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
360 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
361 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
362 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
363 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
364 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
365 return result;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
366 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
367
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
368 const std::vector<ModelAction> EditTools::actions() const
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
369 {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
370 std::vector<ModelAction> result;
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
371 if (this->numpoints == 2) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
372 result.push_back(AppendToModel{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
373 .newElement = Colored<LineSegment>{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
374 LineSegment{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
375 .p1 = this->polygon[0],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
376 .p2 = this->polygon[1],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
377 },
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
378 EDGE_COLOR,
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
379 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
380 });
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
381 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
382 else if (this->numpoints > 2) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
383 const glm::mat4 inverseGrid = glm::inverse(this->gridMatrix);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
384 std::vector<std::vector<glm::vec3>> polygons{1};
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
385 std::vector<glm::vec3>& polygon2d = polygons.back();
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
386 polygon2d.reserve(this->numpoints);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
387 for (std::size_t i = 0; i < this->numpoints; ++i) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
388 polygon2d.push_back(inverseGrid * glm::vec4{this->polygon[i], 1});
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
389 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
390 using indextype = std::uint16_t;
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
391 const std::vector<indextype> indices = mapbox::earcut<std::uint16_t>(polygons);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
392 MergedTriangles mergedTriangles = mergeTriangles(indices, this->polygon);
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
393 for (const Quadrilateral& quad : mergedTriangles.quadrilaterals) {
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
394 result.push_back(AppendToModel{
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
395 .newElement = Colored<Quadrilateral>{quad, MAIN_COLOR},
204
52e10e8d88cc Concentrate model editing into one coroutine inside main()
Teemu Piippo <teemu@hecknology.net>
parents: 203
diff changeset
396 });
223
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
397 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
398 for (std::size_t i = 0; i < indices.size(); i += 3) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
399 if (mergedTriangles.cutTriangles.find(i) == mergedTriangles.cutTriangles.end()) {
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
400 result.push_back(AppendToModel{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
401 .newElement = Colored<Triangle>{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
402 Triangle{
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
403 .p1 = this->polygon[indices[i]],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
404 .p2 = this->polygon[indices[i + 1]],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
405 .p3 = this->polygon[indices[i + 2]],
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
406 },
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
407 MAIN_COLOR,
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
408 }
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
409 });
ce81db996275 Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 222
diff changeset
410 }
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
411 }
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
412 }
222
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
413 return result;
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
414 }
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
415
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
416 void EditTools::closeShape()
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
417 {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
418 for (const ModelAction& action : this->actions()) {
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
419 Q_EMIT this->modelAction(action);
72b456f2f3c2 Edit tools: get rid of the preview polygon and render the result-to-be
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
420 }
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 214
diff changeset
421 this->polygon.clear();
225
551c136b459e Fix crash involving polygon being too empty
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 223
diff changeset
422 this->polygon.push_back(this->worldPosition.value_or(glm::vec3{0, 0, 0}));
197
0e729e681a2c move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents: 191
diff changeset
423 }

mercurial