#include "vectorops.h" #include #include #include #include #include #include #include #include #include #ifdef HAVE_CLIPPER2 #include namespace C2 = Clipper2Lib; #endif namespace c2d { namespace vec { // Flatten curves at `tol` mm (same trick as gcodeexport: Qt flattens at a // fixed 0.5 path-unit chord error, so scale up first). static QList finePolygons(const QPainterPath &path, double tol) { // tol is a public parameter; 0 would scale every coordinate to inf and // silently return no rings at all rather than an error. const double k = 0.5 / (tol > 1e-9 ? tol : 0.005); QList out = path.toSubpathPolygons(QTransform::fromScale(k, k)); for (QPolygonF &poly : out) for (QPointF &pt : poly) pt /= k; return out; } // Clipper returns pieces that only touch at a corner (a circle inscribed in // a square leaves four corner pieces) as one ring that visits that corner // twice. Split such a ring into its simple loops. static QVector splitAtTouches(QPolygonF ring) { if (ring.size() > 1 && ring.first() == ring.last()) ring.removeLast(); auto same = [](const QPointF &a, const QPointF &b) { return std::fabs(a.x() - b.x()) < 1e-4 && std::fabs(a.y() - b.y()) < 1e-4; }; QVector out; QPolygonF cur; for (const QPointF &p : ring) { int k = -1; for (int i = 0; i + 1 < cur.size(); ++i) // not the point just before if (same(cur.at(i), p)) { k = i; break; } if (k >= 0) { QPolygonF loop = cur.mid(k); if (loop.size() >= 3) out << loop; cur.resize(k + 1); } else { cur << p; } } if (cur.size() >= 3) out << cur; return out; } QVector fillFace(const QVector &closed, const QPointF &at, double tol) { QVector in, out; for (const QPainterPath &p : closed) { QPainterPath eo = p; eo.setFillRule(Qt::OddEvenFill); (eo.contains(at) ? in : out) << eo; } if (in.isEmpty()) return {}; auto pathOf = [](const QVector &rings) { QPainterPath r; r.setFillRule(Qt::OddEvenFill); for (const QPolygonF &g : rings) { r.addPolygon(g); r.closeSubpath(); } return r; }; QVector face = in.size() == 1 ? booleanRings(in, {}, BoolOp::Union, tol) : booleanRings(in, {}, BoolOp::Intersect, tol); if (!out.isEmpty() && !face.isEmpty()) face = booleanRings({pathOf(face)}, out, BoolOp::Subtract, tol); { QVector loops; for (const QPolygonF &r : face) loops += splitAtTouches(r); face = loops; } // Flattened, a circle inscribed in a square no longer touches it: the // corner pieces hang together by slits a few microns wide. Shrinking and // regrowing by twice the flattening tolerance cuts those (corners come // back rounded by that much, far below anything a bit can cut). if (!face.isEmpty()) { const double d = 2 * (tol > 1e-9 ? tol : 0.005); const QVector shrunk = offsetRings({pathOf(face)}, {}, -d, tol); const QVector opened = shrunk.isEmpty() ? shrunk : offsetRings({pathOf(shrunk)}, {}, d, tol); if (!opened.isEmpty()) face = opened; } // The piece holding `at`: the smallest ring around it is its outline; // its holes are the rings inside that outline not inside another of them. int outer = -1; double best = 0; for (int i = 0; i < face.size(); ++i) if (face.at(i).containsPoint(at, Qt::OddEvenFill)) { const double a = std::fabs(ringArea(face.at(i))); if (outer < 0 || a < best) { outer = i; best = a; } } if (outer < 0) return {}; const QPolygonF &o = face.at(outer); auto within = [](const QPolygonF &inner, const QPolygonF &around) { for (const QPointF &q : inner) if (!around.containsPoint(q, Qt::OddEvenFill)) return false; return true; }; QVector holes; for (int i = 0; i < face.size(); ++i) if (i != outer && !face.at(i).containsPoint(at, Qt::OddEvenFill) && within(face.at(i), o) && std::fabs(ringArea(face.at(i))) < best) holes << i; QVector res{o}; for (int i : holes) { bool nested = false; for (int k : holes) if (k != i && std::fabs(ringArea(face.at(k))) > std::fabs(ringArea(face.at(i))) && within(face.at(i), face.at(k))) nested = true; if (!nested) res << face.at(i); } return res; } double ringArea(const QPolygonF &ring) { double a = 0; const int n = ring.size(); for (int i = 0; i < n; ++i) { const QPointF &p = ring.at(i); const QPointF &q = ring.at((i + 1) % n); a += p.x() * q.y() - q.x() * p.y(); } return a / 2.0; } bool isClosed(const Element &e) { if (e.geometryType != QLatin1String("path")) return true; for (const QJsonValue &v : e.raw.value("point_type").toArray()) if (v.toInt() == 4) return true; return false; } #ifdef HAVE_CLIPPER2 static const double kScale = 1000.0; // integer µm static C2::Path64 toPath64(const QPolygonF &poly, bool trimClosed = true) { C2::Path64 p; const int n = trimClosed && poly.isClosed() ? poly.size() - 1 : poly.size(); p.reserve(size_t(n)); // Clamped: a coordinate past ~1e15 units overflows Clipper's integer maths // and shapes vanish silently. Infinity clamps by its sign; NaN becomes 0. auto q = [](double v) { const double s = v * kScale; return std::isnan(s) ? qint64(0) : qRound64(qBound(-1e15, s, 1e15)); }; for (int i = 0; i < n; ++i) p.push_back(C2::Point64(q(poly.at(i).x()), q(poly.at(i).y()))); return p; } // Even-odd union of every closed subpath of `paths`: nested contours become // holes — how CC fills a single element (text counters, a ring) and how its // toolpaths treat a set of nested vectors. static C2::Paths64 regionOf(const QVector &paths, double tol) { C2::Paths64 subj; for (const QPainterPath &pp : paths) for (const QPolygonF &poly : finePolygons(pp, tol)) if (poly.size() > 2) subj.push_back(toPath64(poly)); if (subj.empty()) return subj; return C2::Union(subj, C2::FillRule::EvenOdd); } // Fill union across *elements*: each element is its own even-odd region // (oriented outer+/hole- by Clipper), and those regions weld non-zero, so // two overlapping squares merge and a shape fully inside another vanishes // into it — CC's Boolean model. static C2::Paths64 fillUnion(const QVector &paths, double tol) { C2::Paths64 all; for (const QPainterPath &pp : paths) { const C2::Paths64 r = regionOf({pp}, tol); all.insert(all.end(), r.begin(), r.end()); } if (all.empty()) return all; return C2::Union(all, C2::FillRule::NonZero); } static QVector rings(const C2::Paths64 &paths, double tol) { // Drop the collinear vertices the flattening left behind (within tol). const C2::Paths64 simple = C2::SimplifyPaths(paths, tol * kScale, false); QVector out; for (const C2::Path64 &p : simple) { if (p.size() < 3) continue; QPolygonF ring; ring.reserve(int(p.size())); for (const C2::Point64 &pt : p) ring.append(QPointF(pt.x / kScale, pt.y / kScale)); if (std::fabs(ringArea(ring)) < tol * tol) continue; out.append(ring); } return out; } QVector booleanRings(const QVector &subjects, const QVector &clips, BoolOp op, double tol) { if (subjects.isEmpty()) return {}; C2::Paths64 result; switch (op) { case BoolOp::Union: result = fillUnion(subjects, tol); break; case BoolOp::Subtract: { const C2::Paths64 s = fillUnion(subjects, tol); const C2::Paths64 c = fillUnion(clips, tol); result = c.empty() ? s : C2::Difference(s, c, C2::FillRule::NonZero); break; } case BoolOp::Intersect: { result = regionOf({subjects.first()}, tol); for (int i = 1; i < subjects.size() && !result.empty(); ++i) result = C2::Intersect(result, regionOf({subjects.at(i)}, tol), C2::FillRule::NonZero); break; } } return rings(result, tol); } // The region a set of elements encloses, for an offset: an element wholly // inside an odd number of others is a hole (a ring drawn as two circles), // while elements that only overlap weld together. One even-odd pass over // everything would turn an overlap into a hole. static C2::Paths64 nestedRegion(const QVector &paths, double tol) { struct Region { C2::Paths64 paths; C2::Rect64 box; double area; }; auto covers = [](const Region &outer, const Region &inner) { // inner within outer return C2::Difference(inner.paths, outer.paths, C2::FillRule::NonZero).empty(); }; QVector regions; for (const QPainterPath &pp : paths) { C2::Paths64 r = regionOf({pp}, tol); if (r.empty()) continue; Region reg{std::move(r), {}, 0.0}; reg.box = C2::GetBounds(reg.paths); reg.area = std::fabs(C2::Area(reg.paths)); // An exact duplicate of an earlier element (a shape selected with its // copy) adds nothing; kept, it would nest inside the first as a hole. bool dup = false; for (const Region &k : regions) if (k.box == reg.box && covers(k, reg) && covers(reg, k)) { dup = true; break; } if (!dup) regions.append(std::move(reg)); } // Containment is pairwise, but the bounding-box test rejects almost every // pair before Clipper runs, so the same rule holds at any element count. QVector depth(regions.size(), 0); int deepest = 0; for (int i = 0; i < regions.size(); ++i) { const Region &ri = regions.at(i); for (int j = 0; j < regions.size(); ++j) { if (i == j) continue; const Region &rj = regions.at(j); if (!rj.box.Contains(ri.box) || ri.area > rj.area * (1 + 1e-9) + 1.0) continue; // Mutually covering (equal within Clipper's grid but not caught // above): only the earlier one counts as the outer. if (covers(rj, ri) && !(j > i && covers(ri, rj))) ++depth[i]; } deepest = qMax(deepest, depth.at(i)); } C2::Paths64 out; for (int d = 0; d <= deepest; ++d) { C2::Paths64 level; for (int i = 0; i < regions.size(); ++i) if (depth.at(i) == d) level.insert(level.end(), regions.at(i).paths.begin(), regions.at(i).paths.end()); if (level.empty()) continue; out = d % 2 == 0 ? C2::Union(out, level, C2::FillRule::NonZero) : C2::Difference(out, level, C2::FillRule::NonZero); } return out; } QVector offsetRings(const QVector &closed, const QVector &open, double delta, double tol) { const double arcTol = tol * kScale; C2::Paths64 out; if (!closed.isEmpty()) { const C2::Paths64 region = nestedRegion(closed, tol); if (!region.empty()) out = C2::InflatePaths(region, delta * kScale, C2::JoinType::Round, C2::EndType::Polygon, 2.0, arcTol); } if (!open.isEmpty() && !qFuzzyIsNull(delta)) { C2::Paths64 lines; for (const QPainterPath &pp : open) for (const QPolygonF &poly : finePolygons(pp, tol)) if (poly.size() > 1) // an open path ending on its start keeps that last side lines.push_back(toPath64(poly, false)); const C2::Paths64 stroke = C2::InflatePaths(lines, std::fabs(delta) * kScale, C2::JoinType::Round, C2::EndType::Round, 2.0, arcTol); out.insert(out.end(), stroke.begin(), stroke.end()); } // A single union pass normalizes orientation and welds overlapping // stroke outlines with the region offsets. if (out.empty()) return {}; return rings(C2::Union(out, C2::FillRule::NonZero), tol); } #else // !HAVE_CLIPPER2 — approximate Qt path booleans, no offsetting. static QPainterPath regionPath(const QVector &paths, double tol) { QPainterPath r; r.setFillRule(Qt::OddEvenFill); for (const QPainterPath &pp : paths) for (const QPolygonF &poly : finePolygons(pp, tol)) if (poly.size() > 2) r.addPolygon(poly); return r.simplified(); } static QVector ringsOf(const QPainterPath &p, double tol) { QVector out; for (QPolygonF poly : p.toSubpathPolygons()) { if (poly.isClosed()) poly.removeLast(); if (poly.size() > 2 && std::fabs(ringArea(poly)) >= tol * tol) out.append(poly); } return out; } QVector booleanRings(const QVector &subjects, const QVector &clips, BoolOp op, double tol) { if (subjects.isEmpty()) return {}; QPainterPath r = regionPath({subjects.first()}, tol); if (op == BoolOp::Union) for (int i = 1; i < subjects.size(); ++i) r = r.united(regionPath({subjects.at(i)}, tol)); if (op == BoolOp::Subtract) for (const QPainterPath &c : clips) r = r.subtracted(regionPath({c}, tol)); else if (op == BoolOp::Intersect) for (int i = 1; i < subjects.size(); ++i) r = r.intersected(regionPath({subjects.at(i)}, tol)); return ringsOf(r, tol); } QVector offsetRings(const QVector &, const QVector &, double, double) { return {}; } #endif QVector elementsFromRings(const QVector &rings, const QJsonObject &layer) { QVector out; for (const QPolygonF &ring : rings) { QVector pts; pts.reserve(ring.size()); for (const QPointF &p : ring) pts.append(p); out.append(Element::makePath(pts, true, layer)); } return out; } static QJsonObject layerOf(const QVector &inputs) { return inputs.isEmpty() ? QJsonObject() : inputs.first().raw.value("layer").toObject(); } QVector booleanElements(const QVector &inputs, BoolOp op, double tol) { QVector subjects, clips; for (int i = 0; i < inputs.size(); ++i) { if (!isClosed(inputs.at(i))) continue; if (op == BoolOp::Subtract && i > 0) clips.append(inputs.at(i).painterPath); else subjects.append(inputs.at(i).painterPath); } return elementsFromRings(booleanRings(subjects, clips, op, tol), layerOf(inputs)); } QVector offsetElements(const QVector &inputs, double delta, double tol) { QVector closed, open; for (const Element &e : inputs) (isClosed(e) ? closed : open).append(e.painterPath); return elementsFromRings(offsetRings(closed, open, delta, tol), layerOf(inputs)); } // ---- alignment ---------------------------------------------------------- // QRectF in CC's Y-up space: top() is the *minimum* y (bottom edge on the // board) and bottom() the maximum. Spell it out to keep the code readable. static double minX(const QRectF &r) { return r.left(); } static double maxX(const QRectF &r) { return r.right(); } static double minY(const QRectF &r) { return r.top(); } static double maxY(const QRectF &r) { return r.bottom(); } QVector alignDeltas(const QVector &boxes, Align mode, const QRectF &ref) { QVector d; d.reserve(boxes.size()); for (const QRectF &b : boxes) { switch (mode) { case Align::Left: d.append({minX(ref) - minX(b), 0}); break; case Align::Right: d.append({maxX(ref) - maxX(b), 0}); break; case Align::HCenter: d.append({ref.center().x() - b.center().x(), 0}); break; case Align::Bottom: d.append({0, minY(ref) - minY(b)}); break; case Align::Top: d.append({0, maxY(ref) - maxY(b)}); break; case Align::VCenter: d.append({0, ref.center().y() - b.center().y()}); break; } } return d; } QVector centerDeltas(const QVector &boxes, Center mode, const QRectF &stock) { // By hand: isNull() and united() both take a box of no size (a zero-length // line) for "no box", which left such a vector out of the group. Only the // box of an empty path, QRectF(), is no box. QRectF all; bool have = false; for (const QRectF &b : boxes) { if (b == QRectF()) continue; if (!have) { all = b; have = true; continue; } all = QRectF(QPointF(qMin(all.left(), b.left()), qMin(all.top(), b.top())), QPointF(qMax(all.right(), b.right()), qMax(all.bottom(), b.bottom()))); } const QPointF shift = stock.center() - all.center(); const QPointF d(mode == Center::Vertical ? 0.0 : shift.x(), mode == Center::Horizontal ? 0.0 : shift.y()); return QVector(boxes.size(), d); } QVector distributeDeltas(const QVector &boxes, Axis axis) { const int n = boxes.size(); QVector d(n, QPointF(0, 0)); if (n < 3) return d; auto coord = [axis](const QRectF &b) { return axis == Axis::Horizontal ? b.center().x() : b.center().y(); }; QVector order(n); std::iota(order.begin(), order.end(), 0); std::sort(order.begin(), order.end(), [&](int a, int b) { return coord(boxes.at(a)) < coord(boxes.at(b)); }); const double first = coord(boxes.at(order.first())); const double last = coord(boxes.at(order.last())); const double step = (last - first) / (n - 1); for (int rank = 0; rank < n; ++rank) { const int i = order.at(rank); const double shift = first + rank * step - coord(boxes.at(i)); d[i] = axis == Axis::Horizontal ? QPointF(shift, 0) : QPointF(0, shift); } return d; } // ---- mirror and array ------------------------------------------------------ static QPointF jsonPt(const QJsonValue &v) { const QJsonArray a = v.toArray(); return a.size() == 2 ? QPointF(a.at(0).toDouble(), a.at(1).toDouble()) : QPointF(); } static QJsonArray jsonXY(const QPointF &p) { return QJsonArray{p.x(), p.y()}; } // Every anchor and handle of a model, as one list. static QVector modelPoints(const PathModel &m) { QVector out; for (const SubPath &s : m.subs) for (const PathNode &n : s.nodes) out << n.p << n.in << n.out; return out; } // Same outline up to node order and direction: the anchors and handles of // the two models match one to one. Enough to tell that a rectangle or a // regular polygon came out of a transform as the same shape. static bool sameOutline(const PathModel &a, const PathModel &b) { const QVector pa = modelPoints(a), pb = modelPoints(b); if (pa.size() != pb.size() || pa.isEmpty()) return false; QVector used(pb.size(), false); for (const QPointF &p : pa) { bool found = false; for (int i = 0; i < pb.size(); ++i) { if (!used.at(i) && QLineF(p, pb.at(i)).length() < 1e-6) { used[i] = true; found = true; break; } } if (!found) return false; } return true; } static PathModel mapModel(const PathModel &m, const QTransform &t) { PathModel out = m; const bool flip = t.determinant() < 0; for (SubPath &s : out.subs) { for (PathNode &n : s.nodes) { n.p = t.map(n.p); n.in = t.map(n.in); n.out = t.map(n.out); } if (!flip || s.nodes.size() < 2) continue; // A reflection turns CCW into CW. Walk the subpath backwards (a closed // one keeps its start node) so the winding the file had is kept; the // handles swap sides along with the direction. QVector r; const int n = s.nodes.size(); if (s.closed) { r << s.nodes.first(); for (int i = n - 1; i >= 1; --i) r << s.nodes.at(i); } else { for (int i = n - 1; i >= 0; --i) r << s.nodes.at(i); } for (PathNode &node : r) std::swap(node.in, node.out); s.nodes = r; } return out; } Element transformElement(const Element &e, const QTransform &t) { if (t.isIdentity()) return e; if (t.type() <= QTransform::TxTranslate) { Element c = e; c.translate(t.dx(), t.dy()); return c; } if (e.geometryType == QLatin1String("text")) { // Text is placed by its own row-major 3x3; compose ours after it. QJsonObject o = e.raw; const QJsonArray a = o.value("transform").toArray(); QTransform xf; if (a.size() == 9) xf = QTransform(a.at(0).toDouble(), a.at(1).toDouble(), a.at(3).toDouble(), a.at(4).toDouble(), a.at(6).toDouble(), a.at(7).toDouble()); QTransform n = xf * t; // A uniform scale goes into the text's own sizes (font height, arc // radius and centre, and the outlines, which are scaled as they are // rather than laid out again), so Height in Properties stays the // drawn size. Only rotation / reflection is left in the transform. // A stretch cannot be a font size and stays in the transform. const double sx = std::hypot(t.m11(), t.m12()), sy = std::hypot(t.m21(), t.m22()); const bool similar = std::fabs(sx - sy) < 1e-9 * qMax(1.0, sx) && std::fabs(t.m11() * t.m21() + t.m12() * t.m22()) < 1e-9 * qMax(1.0, sx * sy); if (similar && sx > 1e-12 && std::fabs(sx - 1.0) > 1e-12) { const double k = sx; QTransform shrink; shrink.scale(1.0 / k, 1.0 / k); n = shrink * n; o.insert("font_height", o.value("font_height").toDouble(10.0) * k); if (o.contains("arc_radius")) o.insert("arc_radius", o.value("arc_radius").toDouble() * k); if (o.value("arc_center").toArray().size() == 2) o.insert("arc_center", jsonXY(jsonPt(o.value("arc_center")) * k)); QJsonArray rendered; for (const QJsonValue &cv : o.value("rendered").toArray()) { QJsonArray contour; for (const QJsonValue &pv : cv.toArray()) contour.append(jsonXY(jsonPt(pv) * k)); rendered.append(contour); } o.insert("rendered", rendered); } // The fold leaves (1/k)*k rounding noise (0.9999999999999999) and // a turn there and back leaves ~1e-17 off-diagonals: snap them, so a // plain text reads as plain again. auto snap = [](double v) { for (double to : {0.0, 1.0, -1.0}) if (std::fabs(v - to) < 1e-12) return to; return v; }; o.insert("transform", QJsonArray{snap(n.m11()), snap(n.m12()), 0, snap(n.m21()), snap(n.m22()), 0, n.dx(), n.dy(), 1}); return Element::fromJson(o); } const bool shape = e.geometryType == QLatin1String("circle") || e.geometryType == QLatin1String("rectangle") || e.geometryType == QLatin1String("regular_polygon"); if (shape && e.raw.contains("center")) { const QPointF c = jsonPt(e.raw.value("center")); const QPointF nc = t.map(c); if (Element::isPoint(e)) { // A sketch point has no size: it only moves, never stretches. Element moved = e; moved.translate(nc.x() - c.x(), nc.y() - c.y()); return moved; } // How the transform scales: lengths along x and y, and whether it // keeps angles (rotation / reflection / uniform scale). const double sx = std::hypot(t.m11(), t.m12()), sy = std::hypot(t.m21(), t.m22()); const bool similar = std::fabs(sx - sy) < 1e-9 * qMax(1.0, sx) && std::fabs(t.m11() * t.m21() + t.m12() * t.m22()) < 1e-9 * qMax(1.0, sx * sy); const bool rigid = similar && std::fabs(sx - 1.0) < 1e-9; Element cand; if (rigid) { cand = e; const QPointF moved = nc - c; cand.translate(moved.x(), moved.y()); // A circle is the same circle under any rotation or reflection. if (e.geometryType == QLatin1String("circle")) return cand; } else { QHash p; p.insert("cx", nc.x()); p.insert("cy", nc.y()); if (e.geometryType == QLatin1String("circle")) { if (!similar) // stretched: an ellipse, which only a path can be return Element::withPathModel(e, mapModel(Element::pathModel(e), t)); p.insert("radius", e.raw.value("radius").toDouble() * sx); return Element::regen(e, p); } if (e.geometryType == QLatin1String("rectangle")) { if (similar) { p.insert("width", e.raw.value("width").toDouble() * sx); p.insert("height", e.raw.value("height").toDouble() * sx); } else if (std::fabs(t.m12()) < 1e-12 && std::fabs(t.m21()) < 1e-12) { p.insert("width", e.raw.value("width").toDouble() * std::fabs(t.m11())); p.insert("height", e.raw.value("height").toDouble() * std::fabs(t.m22())); } } else if (similar) { p.insert("radius", e.raw.value("radius").toDouble() * sx); } cand = Element::regen(e, p); } const PathModel target = mapModel(Element::pathModel(e), t); if (sameOutline(Element::pathModel(cand), target)) return cand; // symmetric under t: a rectangle flipped, a hexagon turned by 60 deg if (similar && e.geometryType == QLatin1String("regular_polygon") && !target.isEmpty() && !target.subs.first().nodes.isEmpty()) { // Still a regular polygon, just turned: the rotation key is the // angle of its first vertex about the center. const QPointF v = target.subs.first().nodes.first().p - nc; QHash p; p.insert("cx", nc.x()); p.insert("cy", nc.y()); p.insert("radius", e.raw.value("radius").toDouble() * sx); p.insert("rotation", qRadiansToDegrees(std::atan2(v.y(), v.x()))); const Element poly = Element::regen(e, p); if (sameOutline(Element::pathModel(poly), target)) return poly; } return Element::withPathModel(e, target); } return Element::withPathModel(e, mapModel(Element::pathModel(e), t)); } QTransform mirrorTransform(const QRectF &box, Axis axis) { const QPointF c = box.center(); QTransform t; t.translate(c.x(), c.y()); if (axis == Axis::Horizontal) t.scale(-1, 1); else t.scale(1, -1); t.translate(-c.x(), -c.y()); return t; } QVector gridTransforms(const QRectF &box, int cols, int rows, double gapX, double gapY) { QVector out; if (cols < 1 || rows < 1) return out; const double stepX = box.width() + gapX, stepY = box.height() + gapY; for (int r = 0; r < rows; ++r) for (int c = 0; c < cols; ++c) if (r || c) out.append(QTransform::fromTranslate(c * stepX, r * stepY)); return out; } QVector circularTransforms(const QRectF &box, QPointF center, int count, double spanDeg, bool rotate) { QVector out; if (count < 2) return out; const double step = std::fabs(spanDeg) >= 360.0 ? (spanDeg < 0 ? -360.0 : 360.0) / count : spanDeg / (count - 1); const QPointF b = box.center(); for (int i = 1; i < count; ++i) { const double a = i * step; QTransform turn; turn.translate(center.x(), center.y()); turn.rotate(a); turn.translate(-center.x(), -center.y()); if (rotate) { out.append(turn); } else { const QPointF d = turn.map(b) - b; out.append(QTransform::fromTranslate(d.x(), d.y())); } } return out; } QVector pathTransforms(const QPainterPath &path, int count, double spacing, bool orient) { QVector out; if (count < 2) return out; // The path's first subpath, flattened finely (Qt's own flattening is // coarse at millimetre scale). const double k = 100.0; QList polys = path.toSubpathPolygons(QTransform::fromScale(k, k)); if (polys.isEmpty() || polys.first().size() < 2) return out; QPolygonF poly = polys.first(); for (QPointF &p : poly) p /= k; QVector acc{0.0}; for (int i = 1; i < poly.size(); ++i) acc.append(acc.last() + QLineF(poly.at(i - 1), poly.at(i)).length()); const double total = acc.last(); if (total < 1e-9) return out; const bool closed = QLineF(poly.first(), poly.last()).length() < 1e-6; // Point and direction (degrees) at arc length s. auto at = [&](double s, QPointF *pt, double *deg) { // On a vertex, the segment leaving it (as for the original at 0): a // copy on a square's corner turned one edge too little. int i = int(std::upper_bound(acc.begin(), acc.end(), s + 1e-6) - acc.begin()); i = qBound(1, i, int(poly.size()) - 1); while (i < poly.size() - 1 && acc.at(i) - acc.at(i - 1) < 1e-12) ++i; const QLineF seg(poly.at(i - 1), poly.at(i)); const double len = acc.at(i) - acc.at(i - 1); *pt = len > 1e-12 ? seg.pointAt(qBound(0.0, (s - acc.at(i - 1)) / len, 1.0)) : seg.p2(); *deg = std::atan2(seg.dy(), seg.dx()) * 180.0 / M_PI; }; QPointF p0; double d0 = 0; at(0, &p0, &d0); for (int i = 1; i < count; ++i) { const double s = spacing > 0 ? i * spacing : closed ? total * i / count : total * i / (count - 1); if (s > total + 1e-9) break; // Round a closed path back at the start: a copy there would sit on // the original (stacked vectors cancel in a pocket). if (closed && s > total - 0.01) break; QPointF p; double d = 0; at(s, &p, &d); // A copy that lands on the start (a figure-eight crossing it, a tiny // circle) is skipped; the path goes on past it. if (closed && QLineF(p, p0).length() < 0.05) continue; QTransform t; t.translate(p.x(), p.y()); if (orient) t.rotate(d - d0); t.translate(-p0.x(), -p0.y()); out.append(t); } return out; } QVector copyElements(const QVector &els, const QTransform &t) { QHash groups; QVector out; for (const Element &e : els) { QJsonObject o = transformElement(e, t).raw; o.insert("id", QUuid::createUuid().toString()); QJsonArray g; for (const QJsonValue &v : o.value("group_id").toArray()) { if (!v.isString()) { g.append(v); continue; } QString &mapped = groups[v.toString()]; if (mapped.isEmpty()) mapped = QUuid::createUuid().toString(); g.append(mapped); } o.insert("group_id", g); out.append(Element::fromJson(o)); } return out; } // ---- rotate / scale -------------------------------------------------------- QTransform rotateTransform(QPointF center, double deg) { QTransform t; t.translate(center.x(), center.y()); t.rotate(deg); t.translate(-center.x(), -center.y()); return t; } QTransform scaleTransform(QPointF anchor, double sx, double sy) { QTransform t; t.translate(anchor.x(), anchor.y()); t.scale(sx, sy); t.translate(-anchor.x(), -anchor.y()); return t; } // ---- fillet / chamfer ------------------------------------------------------- namespace { QPointF unit(const QPointF &v) { const double l = std::hypot(v.x(), v.y()); return l > 0 ? v / l : QPointF(); } double len(const QPointF &v) { return std::hypot(v.x(), v.y()); } // The segment arriving at / leaving node i is a straight line. bool straightIn(const SubPath &s, int i) { const int n = s.nodes.size(); if (!s.closed && i == 0) return false; const PathNode &prev = s.nodes.at((i - 1 + n) % n); return !prev.hasOut() && !s.nodes.at(i).hasIn(); } bool straightOut(const SubPath &s, int i) { const int n = s.nodes.size(); if (!s.closed && i == n - 1) return false; const PathNode &next = s.nodes.at((i + 1) % n); return !s.nodes.at(i).hasOut() && !next.hasIn(); } // A sharp corner between two straight segments, turning by more than 1° // (anything less is a flattened curve, not a corner). bool isCorner(const SubPath &s, int i) { const int n = s.nodes.size(); if (n < 3 && s.closed) return false; if (!straightIn(s, i) || !straightOut(s, i)) return false; const QPointF p = s.nodes.at(i).p; const QPointF a = s.nodes.at((i - 1 + n) % n).p - p; const QPointF b = s.nodes.at((i + 1) % n).p - p; if (len(a) < 1e-9 || len(b) < 1e-9) return false; const double cosA = QPointF::dotProduct(unit(a), unit(b)); return cosA > -std::cos(qDegreesToRadians(1.0)); } } // namespace PathModel cornerModel(const PathModel &m, CornerStyle style, double size, int *corners) { PathModel out; int count = 0; for (const SubPath &s : m.subs) { const int n = s.nodes.size(); QVector corner(n, false); for (int i = 0; i < n; ++i) corner[i] = isCorner(s, i); SubPath r; r.closed = s.closed; for (int i = 0; i < n; ++i) { const PathNode &node = s.nodes.at(i); if (!corner.at(i) || size <= 0) { r.nodes.append(node); continue; } const int ip = (i - 1 + n) % n, in = (i + 1) % n; const QPointF p = node.p; const QPointF a = s.nodes.at(ip).p - p, b = s.nodes.at(in).p - p; const QPointF u = unit(a), v = unit(b); const double phi = std::acos(qBound(-1.0, QPointF::dotProduct(u, v), 1.0)); // How far along each edge the cut starts: a neighbouring corner // that is being cut too gets the other half of the shared edge. const double room = qMin(len(a) * (corner.at(ip) ? 0.5 : 1.0), len(b) * (corner.at(in) ? 0.5 : 1.0)); double d = style == CornerStyle::Fillet ? size / std::tan(phi / 2) : size; d = qMin(d, room); if (d < 1e-9) { r.nodes.append(node); continue; } PathNode t1, t2; t1.p = t1.in = t1.out = p + u * d; t2.p = t2.in = t2.out = p + v * d; if (style == CornerStyle::Fillet) { const double radius = d * std::tan(phi / 2); const double sweep = M_PI - phi; const double h = 4.0 / 3.0 * std::tan(sweep / 4) * radius; t1.out = t1.p - u * h; t2.in = t2.p - v * h; } r.nodes << t1 << t2; ++count; } // Two cuts that met in the middle of an edge leave two nodes on one // spot: fuse them (the first's incoming handle, the second's outgoing). for (int i = 0; r.nodes.size() > 2 && i < r.nodes.size();) { const int j = i + 1 < r.nodes.size() ? i + 1 : (r.closed ? 0 : -1); if (j < 0 || j == i || len(r.nodes.at(i).p - r.nodes.at(j).p) > 1e-9) { ++i; continue; } PathNode f = r.nodes.at(i); f.out = r.nodes.at(j).out; if (f.hasIn() && f.hasOut()) f.kind = PathNode::Smooth; r.nodes[i] = f; r.nodes.removeAt(j); if (j < i) --i; } out.subs.append(r); } if (corners) *corners = count; return out; } Element cornerElement(const Element &e, CornerStyle style, double size, int *corners) { if (corners) *corners = 0; if (e.geometryType == QLatin1String("text") || e.geometryType == QLatin1String("circle")) return e; int k = 0; const PathModel m = cornerModel(Element::pathModel(e), style, size, &k); if (corners) *corners = k; return k ? Element::withPathModel(e, m) : e; } } // namespace vec } // namespace c2d