src/layers/edittools.cpp

changeset 305
d891da20abca
parent 301
8ccd6fdb30dc
child 306
6ad27b7d2697
equal deleted inserted replaced
304:5d280bceb713 305:d891da20abca
139 const PartRenderer* renderer) 139 const PartRenderer* renderer)
140 { 140 {
141 painter->drawPolygon(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)}); 141 painter->drawPolygon(QPolygonF{convertWorldPointsToScreenPoints(points, renderer)});
142 } 142 }
143 143
144 static std::vector<std::vector<glm::vec3>> modelActionPoints(const ModelAction& action) 144 //! \brief Conversion function from PlainPolygonElement to ModelElement
145 ModelElement elementFromPolygonAndColor(const PlainPolygonElement& poly, ColorIndex color)
146 {
147 // use std::visit with a templated lambda to resolve the type of poly.
148 return std::visit([color](const auto& resolvedPoly) -> ModelElement {
149 // unlike with normal templates we need to pry out the type out manually
150 using PolygonType = std::decay_t<decltype(resolvedPoly)>;
151 // add color and return as a model element.
152 return Colored<PolygonType>{resolvedPoly, color};
153 }, poly);
154 }
155
156 static std::vector<std::vector<glm::vec3>> polygonsToBeInserted(const ModelAction& action)
145 { 157 {
146 std::vector<std::vector<glm::vec3>> result; 158 std::vector<std::vector<glm::vec3>> result;
147 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) { 159 if (const AppendToModel* append = std::get_if<AppendToModel>(&action)) {
148 const ModelElement& newElement = append->newElement; 160 const ModelElement& newElement = append->newElement;
149 if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) { 161 if (const LineSegment* seg = std::get_if<Colored<LineSegment>>(&newElement)) {
154 } 166 }
155 else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) { 167 else if (const Quadrilateral* quad = std::get_if<Colored<Quadrilateral>>(&newElement)) {
156 result.push_back({quad->p1, quad->p2, quad->p3, quad->p4}); 168 result.push_back({quad->p1, quad->p2, quad->p3, quad->p4});
157 } 169 }
158 else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) { 170 else if (const CircularPrimitive* circ = std::get_if<Colored<CircularPrimitive>>(&newElement)) {
159 rasterize(*circ, [&](const ModelElement& element){ 171 // rasterize the circle down to polygons, and append them to the result.
160 const auto& subpoints = modelActionPoints(AppendToModel{element}); 172 rasterize(*circ, [&](const PlainPolygonElement& poly, const ColorIndex color){
173 AppendToModel append{elementFromPolygonAndColor(poly, color)};
174 const auto& subpoints = polygonsToBeInserted(append);
161 std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result)); 175 std::copy(subpoints.begin(), subpoints.end(), std::back_inserter(result));
162 }); 176 });
163 } 177 }
164 } 178 }
165 return result; 179 return result;
230 void EditTools::renderPreview(QPainter* painter, const void* pensptr) 244 void EditTools::renderPreview(QPainter* painter, const void* pensptr)
231 { 245 {
232 const Pens& pens = *reinterpret_cast<const Pens*>(pensptr); 246 const Pens& pens = *reinterpret_cast<const Pens*>(pensptr);
233 painter->setPen(pens.polygonPen); 247 painter->setPen(pens.polygonPen);
234 for (const ModelAction& action : this->modelActions()) { 248 for (const ModelAction& action : this->modelActions()) {
235 for (const std::vector<glm::vec3>& points : modelActionPoints(action)) { 249 for (const std::vector<glm::vec3>& points : polygonsToBeInserted(action)) {
236 if (points.size() == 2) { 250 if (points.size() == 2) {
237 drawWorldPolyline(painter, points, renderer); 251 drawWorldPolyline(painter, points, renderer);
238 } 252 }
239 else { 253 else {
240 if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) { 254 if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) {

mercurial