Fri, 01 Jul 2022 23:51:16 +0300
Added rudimentary cylinder extrusion into circle tool.
I really had to think which way the vectors are in this one
24 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2020 Teemu Piippo | |
4 | * | |
5 | * This program is free software: you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation, either version 3 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
47 | 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> |
301
8ccd6fdb30dc
Move earcut.h outside src/ directory
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
21 | #include "thirdparty/earcut.h" |
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:
263
diff
changeset
|
22 | #include "src/model.h" |
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
263
diff
changeset
|
23 | #include "src/ui/objecteditor.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:
263
diff
changeset
|
24 | #include "src/gl/partrenderer.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:
263
diff
changeset
|
25 | #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:
263
diff
changeset
|
26 | #include "src/layers/edittools.h" |
306
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
27 | #include "src/invert.h" |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
28 | |
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
|
29 | // Make mapbox::earcut work with glm::vec3 |
244
065db0753f05
a bit of refactor in mapbox::util::nth
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
30 | template<> struct mapbox::util::nth<0, glm::vec3> |
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
|
31 | { |
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:
245
diff
changeset
|
32 | static constexpr float get(const glm::vec3& t) { return t.x; } |
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
|
33 | }; |
244
065db0753f05
a bit of refactor in mapbox::util::nth
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
34 | |
065db0753f05
a bit of refactor in mapbox::util::nth
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
232
diff
changeset
|
35 | template<> struct mapbox::util::nth<1, glm::vec3> |
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
|
36 | { |
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:
245
diff
changeset
|
37 | static constexpr float get(const glm::vec3& t) { return t.y; } |
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
|
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 | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
40 | EditTools::EditTools(QObject* parent) : |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
41 | QObject{parent}, |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
42 | RenderLayer{} |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
43 | { |
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 | |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
46 | EditTools::~EditTools() |
8
44679e468ba9
major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff
changeset
|
47 | { |
9
8b9780700b5e
added saving of splitter state and recent files
Teemu Piippo <teemu@hecknology.net>
parents:
8
diff
changeset
|
48 | } |
36
bbb901b97404
added render style storage
Teemu Piippo <teemu@hecknology.net>
parents:
35
diff
changeset
|
49 | |
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:
245
diff
changeset
|
50 | void EditTools::setEditMode(EditingMode newMode) |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
51 | { |
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:
245
diff
changeset
|
52 | this->mode = newMode; |
311
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
53 | switch (this->mode) { |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
54 | case SelectMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
55 | Q_EMIT this->suggestCursor(Qt::ArrowCursor); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
56 | break; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
57 | case DrawMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
58 | case CircleMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
59 | Q_EMIT this->suggestCursor(Qt::CrossCursor); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
60 | break; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
61 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
62 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
63 | |
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:
245
diff
changeset
|
64 | void EditTools::setGridMatrix(const glm::mat4& newGridMatrix) |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
65 | { |
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:
245
diff
changeset
|
66 | this->gridMatrix = newGridMatrix; |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
67 | this->gridPlane = planeFromTriangle({ |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
68 | 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
|
69 | 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
|
70 | 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
|
71 | }); |
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 | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
74 | void EditTools::setCircleToolOptions(const CircleToolOptions& options) |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
75 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
76 | this->circleToolOptions = options; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
77 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
78 | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
79 | 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
|
80 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
81 | this->mvpMatrix = matrix; |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
82 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
83 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
84 | void EditTools::mouseMoved(const QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
85 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
86 | this->worldPosition = this->renderer->screenToModelCoordinates(event->pos(), this->gridPlane); |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
87 | this->localPosition = event->localPos(); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
88 | if (this->worldPosition.has_value()) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
89 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
90 | // 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
|
91 | // 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
|
92 | // 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
|
93 | // 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
|
94 | // 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
|
95 | // 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
|
96 | 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
|
97 | // 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
|
98 | this->worldPosition = glm::round(*this->worldPosition); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
99 | // 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
|
100 | // grid matrix. |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
101 | 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
|
102 | this->polygon.back() = *this->worldPosition; |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
103 | this->numpoints = this->calcNumPoints(); |
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
|
104 | } |
217
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 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
107 | static QVector<QPointF> convertWorldPointsToScreenPoints( |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
108 | const std::vector<glm::vec3> &worldPoints, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
109 | const PartRenderer* renderer) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
110 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
111 | QVector<QPointF> points2d; |
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:
245
diff
changeset
|
112 | points2d.reserve(static_cast<int>(worldPoints.size())); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
113 | for (const glm::vec3& point : worldPoints) |
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 | points2d.push_back(renderer->modelToScreenCoordinates(point)); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
116 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
117 | return points2d; |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
118 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
119 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
120 | static Winding worldPolygonWinding( |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
121 | const std::vector<glm::vec3> &points, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
122 | const PartRenderer* renderer) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
123 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
124 | return winding(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)}); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
125 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
126 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
127 | static void drawWorldPoint( |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
128 | QPainter* painter, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
129 | const glm::vec3& worldPoint, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
130 | const PartRenderer* 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 | const QPointF center = renderer->modelToScreenCoordinates(worldPoint); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
133 | painter->drawEllipse(inscribe(CircleF{center, 5})); |
125
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
134 | } |
f127982d3412
Move tools under Document instead of MainWindow
Teemu Piippo <teemu@hecknology.net>
parents:
124
diff
changeset
|
135 | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
136 | static void drawWorldPolyline( |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
137 | QPainter *painter, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
138 | const std::vector<glm::vec3> &points, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
139 | const PartRenderer* renderer) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
140 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
141 | painter->drawPolyline(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)}); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
142 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
143 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
144 | static void drawWorldPolygon( |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
145 | QPainter* painter, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
146 | const std::vector<glm::vec3> &points, |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
147 | const PartRenderer* renderer) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
148 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
149 | painter->drawPolygon(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)}); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
150 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
151 | |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
152 | //! \brief Conversion function from PlainPolygonElement to ModelElement |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
153 | ModelElement elementFromPolygonAndColor(const PlainPolygonElement& poly, ColorIndex color) |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
154 | { |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
155 | // use std::visit with a templated lambda to resolve the type of poly. |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
156 | return std::visit([color](const auto& resolvedPoly) -> ModelElement { |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
157 | // unlike with normal templates we need to pry out the type out manually |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
158 | using PolygonType = std::decay_t<decltype(resolvedPoly)>; |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
159 | // add color and return as a model element. |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
160 | return Colored<PolygonType>{resolvedPoly, color}; |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
161 | }, poly); |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
162 | } |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
163 | |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
164 | static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action) |
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
|
165 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
166 | std::vector<std::vector<glm::vec3>> result; |
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
|
167 | 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
|
168 | 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
|
169 | if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
170 | result.push_back({seg->p1, seg->p2}); |
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
|
171 | } |
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
|
172 | else if (const Triangle* tri = std::get_if<Colored<Triangle>>(&newElement)) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
173 | result.push_back({tri->p1, tri->p2, tri->p3}); |
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
|
174 | } |
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
|
175 | else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
176 | result.push_back({quad->p1, quad->p2, quad->p3, quad->p4}); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
177 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
178 | else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) { |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
179 | // rasterize the circle down to polygons, and append them to the result. |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
180 | rasterize(*circ, [&](const PlainPolygonElement& poly, const ColorIndex color){ |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
181 | AppendToModel append{elementFromPolygonAndColor(poly, color)}; |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
182 | const auto& subpoints = polygonsToBeInserted(append); |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
183 | std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result)); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
184 | }); |
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
|
185 | } |
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
|
186 | } |
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
|
187 | 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
|
188 | } |
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
|
189 | |
228 | 190 | namespace { |
191 | struct Pens | |
192 | { | |
193 | const QBrush pointBrush; | |
194 | const QPen pointPen; | |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
195 | const QPen textPen; |
228 | 196 | const QPen polygonPen; |
197 | const QPen badPolygonPen; | |
198 | const QBrush greenPolygonBrush; | |
199 | const QBrush redPolygonBrush; | |
200 | }; | |
201 | } | |
202 | ||
203 | static const Pens brightPens{ | |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
204 | .pointBrush = {Qt::black}, |
228 | 205 | .pointPen = {QBrush{Qt::black}, 2.0}, |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
206 | .textPen = {Qt::black}, |
228 | 207 | .polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}, |
208 | .greenPolygonBrush = {QColor{64, 255, 128, 192}}, | |
209 | .redPolygonBrush = {QColor{255, 96, 96, 192}}, | |
210 | }; | |
211 | ||
212 | static const Pens darkPens{ | |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
213 | .pointBrush = {Qt::white}, |
228 | 214 | .pointPen = {QBrush{Qt::white}, 2.0}, |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
215 | .textPen = {Qt::white}, |
228 | 216 | .polygonPen = {QBrush{Qt::white}, 2.0, Qt::DashLine}, |
217 | .greenPolygonBrush = {QColor{64, 255, 128, 192}}, | |
218 | .redPolygonBrush = {QColor{255, 96, 96, 192}}, | |
219 | }; | |
220 | ||
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
221 | void EditTools::overpaint(QPainter* painter) |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
222 | { |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
223 | painter->save(); |
311
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
224 | if (this->usePolygon()) { |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
225 | const Pens& pens = (this->renderer->isDark() ? darkPens : brightPens); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
226 | this->renderPreview(painter, &pens); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
227 | QFont font; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
228 | font.setBold(true); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
229 | if (this->usePolygon() and this->worldPosition.has_value()) |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
230 | { |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
231 | painter->setRenderHint(QPainter::Antialiasing); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
232 | painter->setPen(pens.pointPen); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
233 | painter->setBrush(pens.greenPolygonBrush); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
234 | const QPointF pos = this->renderer->modelToScreenCoordinates(*this->worldPosition); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
235 | painter->drawEllipse(pos, 5, 5); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
236 | drawBorderedText(painter, pos + QPointF{5, 5}, font, vectorToString(*this->worldPosition)); |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
237 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
238 | } |
245
a41ccc6924e3
improve text rendering
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
244
diff
changeset
|
239 | painter->restore(); |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
240 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
241 | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
242 | const std::vector<ModelAction> EditTools::modelActions() const |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
243 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
244 | switch(this->mode) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
245 | case SelectMode: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
246 | return {}; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
247 | case DrawMode: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
248 | return drawModeActions(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
249 | case CircleMode: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
250 | return circleModeActions(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
251 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
252 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
253 | |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
254 | constexpr QPointF vecToQPoint(const glm::vec2& a) |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
255 | { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
256 | return {a.x, a.y}; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
257 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
258 | |
228 | 259 | void EditTools::renderPreview(QPainter* painter, const void* pensptr) |
260 | { | |
261 | const Pens& pens = *reinterpret_cast<const Pens*>(pensptr); | |
262 | painter->setPen(pens.polygonPen); | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
263 | for (const ModelAction& action : this->modelActions()) { |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
301
diff
changeset
|
264 | for (const std::vector<glm::vec3>& points : polygonsToBeInserted(action)) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
265 | if (points.size() == 2) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
266 | drawWorldPolyline(painter, points, renderer); |
228 | 267 | } |
268 | else { | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
269 | if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
270 | painter->setBrush(pens.greenPolygonBrush); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
271 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
272 | else { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
273 | painter->setBrush(pens.redPolygonBrush); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
274 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
275 | drawWorldPolygon(painter, points, this->renderer); |
228 | 276 | } |
277 | } | |
278 | } | |
279 | painter->setBrush(pens.pointBrush); | |
280 | painter->setPen(pens.pointPen); | |
281 | for (const glm::vec3& point : this->polygon) { | |
282 | drawWorldPoint(painter, point, this->renderer); | |
283 | } | |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
284 | if (this->mode == CircleMode and this->polygon.size() >= 2) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
285 | const glm::vec3 circleOrigin = this->polygon[0]; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
286 | const QPointF originScreen = this->renderer->modelToScreenCoordinates(circleOrigin); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
287 | const auto extremity = [this, &originScreen](const glm::vec3& p){ |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
288 | const QPointF s2 = this->renderer->modelToScreenCoordinates(p); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
289 | const auto intersection = rayRectangleIntersection( |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
290 | rayFromPoints(toVec2(originScreen), toVec2(s2)), |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
291 | this->renderer->rect()); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
292 | if (intersection.has_value()) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
293 | return intersection->position; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
294 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
295 | else { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
296 | return glm::vec2{s2.x(), s2.y()}; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
297 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
298 | }; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
299 | const glm::vec3 zvec = this->gridMatrix[2]; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
300 | const glm::vec2 p1 = extremity(this->polygon[0] + zvec); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
301 | const glm::vec2 p2 = extremity(this->polygon[0] - zvec); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
302 | const glm::vec2 lateral = glm::normalize(glm::mat2{{0, 1}, {-1, 0}} * (p2 - p1)); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
303 | painter->setPen(QPen{Qt::white, 3}); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
304 | painter->drawLine(vecToQPoint(p1), vecToQPoint(p2)); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
305 | constexpr float notchsize = 40.0f; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
306 | for (int a = -30; a <= 30; ++a) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
307 | const glm::vec3 notch = this->polygon[0] + static_cast<float>(a) * zvec; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
308 | const QPointF s_notchcenter = this->renderer->modelToScreenCoordinates(notch); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
309 | const QPointF notch_s1 = s_notchcenter + notchsize * 0.5f * vecToQPoint(lateral); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
310 | const QPointF notch_s2 = s_notchcenter - notchsize * 0.5f * vecToQPoint(lateral); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
311 | painter->drawLine(notch_s1, notch_s2); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
312 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
313 | } |
228 | 314 | } |
315 | ||
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
316 | void EditTools::removeLastPoint() |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
317 | { |
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
|
318 | if (this->polygon.size() > 1) { |
312
2637134bc37c
Fix right click to delete not really working properly
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
311
diff
changeset
|
319 | this->polygon.erase(this->polygon.end() - 2); |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
320 | this->numpoints = this->calcNumPoints(); |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
321 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
322 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
323 | |
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
|
324 | bool EditTools::isCloseToExistingPoints() const |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
325 | { |
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
|
326 | 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
|
327 | 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
|
328 | 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
|
329 | 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
|
330 | }); |
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 | } |
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 | 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
|
333 | 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
|
334 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
335 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
336 | |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
337 | std::size_t EditTools::calcNumPoints() const |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
338 | { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
339 | std::size_t result = this->polygon.size(); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
340 | if (this->isCloseToExistingPoints()) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
341 | result -= 1; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
342 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
343 | return result; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
344 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
345 | |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
346 | EditingMode EditTools::currentEditingMode() const |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
347 | { |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
348 | return this->mode; |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
349 | } |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
350 | |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
351 | void EditTools::mouseClick(const QMouseEvent* event) |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
352 | { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
353 | switch(this->mode) { |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
354 | case SelectMode: |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
355 | if (event->button() == Qt::LeftButton) { |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
307
diff
changeset
|
356 | const ElementId highlighted = this->renderer->pick(event->pos()); |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
357 | Q_EMIT this->select({highlighted}, false); |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
358 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
359 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
360 | case DrawMode: |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
361 | 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
|
362 | if (isCloseToExistingPoints()) { |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
363 | this->closeShape(); |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
364 | } |
214
8e1fe64ce4e3
begin refactor of gl side
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
365 | else { |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
366 | this->polygon.push_back(*this->worldPosition); |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
367 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
368 | } |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
369 | break; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
370 | case CircleMode: |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
371 | if (event->button() == Qt::LeftButton and this->worldPosition.has_value()) { |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
372 | if (this->polygon.size() == 3) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
373 | this->closeShape(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
374 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
375 | else { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
376 | this->polygon.push_back(*this->worldPosition); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
377 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
378 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
379 | break; |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
380 | } |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
381 | if (event->button() == Qt::RightButton and this->polygon.size() > 1) { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
382 | this->removeLastPoint(); |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
383 | } |
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
|
384 | } |
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 | |
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 | 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
|
387 | { |
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 | 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
|
389 | 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
|
390 | }; |
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 | |
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 | 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
|
393 | 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
|
394 | 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
|
395 | { |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
396 | 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
|
397 | 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
|
398 | 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
|
399 | 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
|
400 | 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
|
401 | for (std::size_t i = 0; i < indices.size(); i += 3) { |
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:
245
diff
changeset
|
402 | const auto add = [&](const std::size_t o1, const std::size_t o2, const std::size_t o3){ |
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
|
403 | 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
|
404 | 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
|
405 | }; |
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 | 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
|
407 | 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
|
408 | 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
|
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 | 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
|
411 | 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
|
412 | 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
|
413 | 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
|
414 | 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
|
415 | }; |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
416 | 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
|
417 | 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
|
418 | // 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
|
419 | 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
|
420 | 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
|
421 | 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
|
422 | // .. 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
|
423 | 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
|
424 | // 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
|
425 | 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
|
426 | 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
|
427 | // 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
|
428 | 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
|
429 | 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
|
430 | 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
|
431 | 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
|
432 | 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
|
433 | 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
|
434 | }; |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
435 | 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
|
436 | 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
|
437 | 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
|
438 | 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
|
439 | 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
|
440 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
441 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
442 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
443 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
444 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
445 | 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
|
446 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
447 | |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
448 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
449 | const std::vector<ModelAction> EditTools::circleModeActions() const |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
450 | { |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
451 | std::vector<ModelAction> result; |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
452 | if (this->numpoints == 3) { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
453 | const glm::vec3 x = polygon[1] - polygon[0]; |
314
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
454 | const opt<float> cyliheight = [&]() -> opt<float> { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
455 | const Plane plane{ |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
456 | .normal = glm::normalize(this->renderer->cameraVector(this->localPosition)), |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
457 | .anchor = this->polygon[0], |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
458 | }; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
459 | const opt<glm::vec3> p = this->renderer->screenToModelCoordinates(this->localPosition, plane); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
460 | if (p.has_value()) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
461 | const glm::vec3 heightvec = glm::normalize(glm::vec3{gridMatrix[2]}); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
462 | return std::round(glm::dot(*p - polygon[0], heightvec)); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
463 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
464 | else { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
465 | return {}; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
466 | } |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
467 | }(); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
468 | if (cyliheight.has_value()) { |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
469 | glm::mat4 transform{ |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
470 | glm::vec4{x, 0}, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
471 | *cyliheight * this->gridMatrix[2], |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
472 | glm::vec4{glm::cross(glm::vec3{-this->gridMatrix[2]}, x), 0}, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
473 | glm::vec4{this->polygon[0], 1}, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
474 | }; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
475 | Colored<CircularPrimitive> circ{ |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
476 | CircularPrimitive{ |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
477 | .type = this->circleToolOptions.type, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
478 | .fraction = this->circleToolOptions.fraction, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
479 | .transformation = transform, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
480 | }, |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
481 | MAIN_COLOR |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
482 | }; |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
483 | result.push_back(AppendToModel{.newElement = circ}); |
4642ba1218e8
Added rudimentary cylinder extrusion into circle tool.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
312
diff
changeset
|
484 | } |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
485 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
486 | return result; |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
487 | } |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
488 | |
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
489 | const std::vector<ModelAction> EditTools::drawModeActions() const |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
490 | { |
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
|
491 | 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
|
492 | if (this->numpoints == 2) { |
310 | 493 | result.push_back(AppendToModel{edge(this->polygon[0], this->polygon[1])}); |
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
|
494 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
495 | 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
|
496 | 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
|
497 | 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
|
498 | 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
|
499 | 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
|
500 | 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
|
501 | polygon2d.push_back(inverseGrid * glm::vec4{this->polygon[i], 1}); |
306
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
502 | } |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
503 | // mapbox::earcut will always produce a CCW polygon, so if we're drawing |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
504 | // a CW polygon, we should invert the result afterwards |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
505 | const float shouldInvert = glm::dot( |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
506 | glm::vec3{inverseGrid[2]}, |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
507 | glm::cross(this->polygon[0] - this->polygon[1], this->polygon[2] - this->polygon[1])); |
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
|
508 | 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
|
509 | 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
|
510 | MergedTriangles mergedTriangles = mergeTriangles(indices, this->polygon); |
306
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
511 | for (Quadrilateral& quad : mergedTriangles.quadrilaterals) { |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
512 | if (shouldInvert < 0) { |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
513 | invert(quad); |
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
514 | } |
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
|
515 | 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
|
516 | .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
|
517 | }); |
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
|
518 | } |
ce81db996275
Use Mapbox's ear clipping algorithm to handle drawing any simple polygon
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
222
diff
changeset
|
519 | 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
|
520 | if (mergedTriangles.cutTriangles.find(i) == mergedTriangles.cutTriangles.end()) { |
307 | 521 | Colored<Triangle> tri = triangle( |
522 | this->polygon[indices[i]], | |
523 | this->polygon[indices[i + 1]], | |
524 | this->polygon[indices[i + 2]]); | |
306
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
525 | if (shouldInvert < 0) { |
307 | 526 | invert(tri); |
306
6ad27b7d2697
Enable drawing clockwise shapes despite mapbox::earcut rewinding them
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
527 | } |
307 | 528 | result.push_back(AppendToModel{tri}); |
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
|
529 | } |
197
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
530 | } |
0e729e681a2c
move drawState to Document
Teemu Piippo <teemu@hecknology.net>
parents:
191
diff
changeset
|
531 | } |
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
|
532 | 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
|
533 | } |
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
|
534 | |
311
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
535 | bool EditTools::usePolygon() const |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
536 | { |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
537 | switch (this->mode) { |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
538 | case SelectMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
539 | return false; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
540 | case DrawMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
541 | case CircleMode: |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
542 | return true; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
543 | } |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
544 | return {}; |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
545 | } |
fab454611f9b
Use different cursors depending on edit mode
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
310
diff
changeset
|
546 | |
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
|
547 | 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
|
548 | { |
232
8efa3a33172e
Add base code for circular primitives
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
228
diff
changeset
|
549 | for (const ModelAction& action : this->modelActions()) { |
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
|
550 | 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
|
551 | } |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
214
diff
changeset
|
552 | this->polygon.clear(); |
225
551c136b459e
Fix crash involving polygon being too empty
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
223
diff
changeset
|
553 | 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
|
554 | } |