128 for (std::size_t i = 1; i < points.size(); ++i) {
129 const QPointF& a = points[i - 1];
130 const QPointF& b = points[i];
134 bool adjacent =
true;
135 for (std::size_t j = i + 1; j < points.size(); ++j) {
136 const QPointF& c = points[j - 1];
137 const QPointF& d = points[j];
140 const bool sharesVertex = adjacent;
143 const bool firstHorizontal = a.y() == b.y();
144 const bool secondHorizontal = c.y() == d.y();
145 if (firstHorizontal == secondHorizontal) {
146 const qreal firstAxis = firstHorizontal ? a.y() : a.x();
147 const qreal secondAxis = secondHorizontal ? c.y() : c.x();
148 if (firstAxis != secondAxis)
150 const qreal overlapStart = firstHorizontal
151 ? std::max(std::min(a.x(), b.x()), std::min(c.x(), d.x()))
152 : std::max(std::min(a.y(), b.y()), std::min(c.y(), d.y()));
153 const qreal overlapEnd = firstHorizontal
154 ? std::min(std::max(a.x(), b.x()), std::max(c.x(), d.x()))
155 : std::min(std::max(a.y(), b.y()), std::max(c.y(), d.y()));
156 if (overlapStart < overlapEnd || (!sharesVertex && overlapStart == overlapEnd))
159 const QPointF& h1 = firstHorizontal ? a : c;
160 const QPointF& h2 = firstHorizontal ? b : d;
161 const QPointF& v1 = firstHorizontal ? c : a;
162 const QPointF& v2 = firstHorizontal ? d : b;
163 if (!sharesVertex && v1.x() >= std::min(h1.x(), h2.x())
164 && v1.x() <= std::max(h1.x(), h2.x())
165 && h1.y() >= std::min(v1.y(), v2.y())
166 && h1.y() <= std::max(v1.y(), v2.y()))
231 const std::span<const QPointF> second)
233 auto segmentsOverlap = [](
const QPointF& a,
const QPointF& b,
const QPointF& c,
235 const bool firstHorizontal = a.y() == b.y();
236 const bool secondHorizontal = c.y() == d.y();
237 if (firstHorizontal != secondHorizontal)
240 if (firstHorizontal) {
243 const auto firstMin = std::min(a.x(), b.x());
244 const auto firstMax = std::max(a.x(), b.x());
245 const auto secondMin = std::min(c.x(), d.x());
246 const auto secondMax = std::max(c.x(), d.x());
247 return std::max(firstMin, secondMin) < std::min(firstMax, secondMax);
252 const auto firstMin = std::min(a.y(), b.y());
253 const auto firstMax = std::max(a.y(), b.y());
254 const auto secondMin = std::min(c.y(), d.y());
255 const auto secondMax = std::max(c.y(), d.y());
256 return std::max(firstMin, secondMin) < std::min(firstMax, secondMax);
259 for (std::size_t i = 1; i < first.size(); ++i) {
260 for (std::size_t j = 1; j < second.size(); ++j) {
261 if (segmentsOverlap(first[i - 1], first[i], second[j - 1], second[j]))
269 const std::span<const QPointF> second)
271 auto segmentsCreateJunction = [&](
const QPointF& a,
const QPointF& b,
const QPointF& c,
273 const bool firstHorizontal = a.y() == b.y();
274 const bool secondHorizontal = c.y() == d.y();
275 if (firstHorizontal && secondHorizontal) {
276 return a.y() == c.y()
277 && std::max(std::min(a.x(), b.x()), std::min(c.x(), d.x()))
278 <= std::min(std::max(a.x(), b.x()), std::max(c.x(), d.x()));
280 if (!firstHorizontal && !secondHorizontal) {
281 return a.x() == c.x()
282 && std::max(std::min(a.y(), b.y()), std::min(c.y(), d.y()))
283 <= std::min(std::max(a.y(), b.y()), std::max(c.y(), d.y()));
286 const QPointF& horizontalStart = firstHorizontal ? a : c;
287 const QPointF& horizontalEnd = firstHorizontal ? b : d;
288 const QPointF& verticalStart = firstHorizontal ? c : a;
289 const QPointF& verticalEnd = firstHorizontal ? d : b;
290 const QPointF intersection(verticalStart.x(), horizontalStart.y());
291 const bool liesOnBoth =
292 intersection.x() >= std::min(horizontalStart.x(), horizontalEnd.x())
293 && intersection.x() <= std::max(horizontalStart.x(), horizontalEnd.x())
294 && intersection.y() >= std::min(verticalStart.y(), verticalEnd.y())
295 && intersection.y() <= std::max(verticalStart.y(), verticalEnd.y());
298 return intersection == first.front() || intersection == first.back()
299 || intersection == second.front() || intersection == second.back();
302 for (std::size_t i = 1; i < first.size(); ++i) {
303 for (std::size_t j = 1; j < second.size(); ++j) {
304 if (segmentsCreateJunction(first[i - 1], first[i], second[j - 1], second[j]))
312 const std::span<const QPointF> second)
314 std::size_t crossings = 0;
315 for (std::size_t i = 1; i < first.size(); ++i) {
316 for (std::size_t j = 1; j < second.size(); ++j) {
317 const QPointF& a = first[i - 1];
318 const QPointF& b = first[i];
319 const QPointF& c = second[j - 1];
320 const QPointF& d = second[j];
321 const bool firstHorizontal = a.y() == b.y();
322 const bool secondHorizontal = c.y() == d.y();
323 if (firstHorizontal == secondHorizontal)
326 const QPointF& horizontalStart = firstHorizontal ? a : c;
327 const QPointF& horizontalEnd = firstHorizontal ? b : d;
328 const QPointF& verticalStart = firstHorizontal ? c : a;
329 const QPointF& verticalEnd = firstHorizontal ? d : b;
330 if (verticalStart.x() > std::min(horizontalStart.x(), horizontalEnd.x())
331 && verticalStart.x() < std::max(horizontalStart.x(), horizontalEnd.x())
332 && horizontalStart.y() > std::min(verticalStart.y(), verticalEnd.y())
333 && horizontalStart.y() < std::max(verticalStart.y(), verticalEnd.y()))
403 const std::function<
bool()>& progress)
405 using Point = std::pair<int, int>;
406 using Edge = std::pair<Point, Point>;
408 auto pointKey = [](
const QPointF& point) {
409 return Point{
static_cast<int>(point.x()),
static_cast<int>(point.y())};
411 auto edgeKey = [](Point a, Point b) {
416 auto pointOnSegment = [](
const Point& point,
const Edge& segment) {
417 const auto& [a, b] = segment;
418 if (a.first == b.first)
419 return point.first == a.first && point.second >= std::min(a.second, b.second)
420 && point.second <= std::max(a.second, b.second);
421 return point.second == a.second && point.first >= std::min(a.first, b.first)
422 && point.first <= std::max(a.first, b.first);
425 std::vector<Edge> sourceSegments;
426 std::set<Point> terminals;
427 for (
const auto& route : routes) {
428 if (progress && !progress())
430 if (route.size() > 1 && route.front() == route.back())
431 throw std::invalid_argument(
"A wire route must have distinct endpoints");
432 if (route.size() >= 2) {
433 terminals.insert(pointKey(route.front()));
434 terminals.insert(pointKey(route.back()));
436 for (std::size_t i = 1; i < route.size(); ++i) {
437 const Point a = pointKey(route[i - 1]);
438 const Point b = pointKey(route[i]);
440 sourceSegments.push_back(edgeKey(a, b));
444 std::set<Edge> edges;
445 for (std::size_t i = 0; i < sourceSegments.size(); ++i) {
446 if (progress && !progress())
448 const Edge& segment = sourceSegments[i];
449 std::set<Point> cuts = {segment.first, segment.second};
451 for (std::size_t j = 0; j < sourceSegments.size(); ++j) {
452 if (progress && j % 256 == 0 && !progress())
456 const Edge& other = sourceSegments[j];
457 if (pointOnSegment(other.first, segment))
458 cuts.insert(other.first);
459 if (pointOnSegment(other.second, segment))
460 cuts.insert(other.second);
462 const bool vertical = segment.first.first == segment.second.first;
463 const bool otherVertical = other.first.first == other.second.first;
464 if (vertical != otherVertical) {
465 const Point intersection = vertical
466 ? Point{segment.first.first, other.first.second}
467 : Point{other.first.first, segment.first.second};
468 if (pointOnSegment(intersection, segment) && pointOnSegment(intersection, other))
469 cuts.insert(intersection);
473 std::vector<Point> ordered(cuts.begin(), cuts.end());
474 if (segment.first.first == segment.second.first) {
475 std::ranges::sort(ordered, {}, [](
const Point& point) {
return point.second; });
477 for (std::size_t j = 1; j < ordered.size(); ++j)
478 edges.insert(edgeKey(ordered[j - 1], ordered[j]));
484 std::map<Point, Point> parents;
485 for (
const auto& [a, b] : edges) {
486 parents.emplace(a, a);
487 parents.emplace(b, b);
489 auto root = [&parents](Point point) {
490 while (parents.at(point) != point) {
491 parents.at(point) = parents.at(parents.at(point));
492 point = parents.at(point);
496 std::erase_if(edges, [&](
const Edge& edge) {
497 const Point first = root(edge.first);
498 const Point second = root(edge.second);
501 parents.at(second) = first;
505 std::map<Point, std::set<Point>> adjacency;
506 for (
const auto& [a, b] : edges) {
507 adjacency[a].insert(b);
508 adjacency[b].insert(a);
514 std::deque<Point> leaves;
515 for (
const auto& [point, neighbors] : adjacency)
516 if (neighbors.size() == 1 && !terminals.contains(point))
517 leaves.push_back(point);
518 while (!leaves.empty()) {
519 const Point point = leaves.front();
521 auto& neighbors = adjacency.at(point);
522 if (neighbors.size() != 1)
524 const Point neighbor = *neighbors.begin();
526 adjacency.at(neighbor).erase(point);
527 edges.erase(edgeKey(point, neighbor));
528 if (adjacency.at(neighbor).size() == 1 && !terminals.contains(neighbor))
529 leaves.push_back(neighbor);
532 std::set<Edge> visited;
533 std::vector<std::vector<QPointF>> result;
534 auto appendPath = [&](
const Point& start,
const Point& firstNeighbor) {
535 std::vector<QPointF> path = {QPointF(start.first, start.second)};
536 Point previous = start;
537 Point current = firstNeighbor;
538 visited.insert(edgeKey(previous, current));
541 path.emplace_back(current.first, current.second);
542 if (adjacency.at(current).size() != 2 || terminals.contains(current))
544 const auto& neighbors = adjacency.at(current);
545 const Point next = *std::ranges::find_if(
546 neighbors, [&](
const Point& candidate) {
return candidate != previous; });
547 const Edge nextEdge = edgeKey(current, next);
548 if (visited.contains(nextEdge))
552 visited.insert(nextEdge);
557 for (
const auto& [point, neighbors] : adjacency) {
558 if (progress && !progress())
560 if (neighbors.size() == 2 && !terminals.contains(point))
562 for (
const Point& neighbor : neighbors) {
563 if (!visited.contains(edgeKey(point, neighbor)))
564 appendPath(point, neighbor);
567 if (visited.size() != edges.size())
568 throw std::logic_error(
"Wire route merging left an unvisited cycle");
573 const std::vector<QRectF>& obstacleRects,
574 const qreal gridSize)
579 Avoid::Router router(Avoid::OrthogonalRouting);
582 QPointF routedStart = start;
583 QPointF routedEnd = end;
585 for (
const QRectF& obstacleRect : obstacleRects) {
586 const QRectF normalized = obstacleRect.normalized();
587 routedStart = nearestPointOutside(routedStart, normalized);
588 routedEnd = nearestPointOutside(routedEnd, normalized);
591 std::vector<Avoid::Rectangle> obstacles;
592 obstacles.reserve(obstacleRects.size());
594 for (
const QRectF& obstacleRect : obstacleRects) {
595 const QRectF normalized = obstacleRect.normalized();
596 if (normalized.isEmpty())
599 obstacles.push_back(Avoid::rectangleFromQRectF(normalized));
602 std::vector<Avoid::ShapeRef*> shapeRefs;
603 shapeRefs.reserve(obstacles.size());
604 unsigned int nextId = 1;
606 for (
auto& obstacle : obstacles) {
607 shapeRefs.push_back(
new Avoid::ShapeRef(&router, obstacle, nextId++));
611 new Avoid::ConnRef(&router, Avoid::ConnEnd(Avoid::pointFromQPointF(routedStart)),
612 Avoid::ConnEnd(Avoid::pointFromQPointF(routedEnd)), nextId);
613 connector->setRoutingType(Avoid::ConnType_Orthogonal);
615 if (!router.processTransaction())
620 if (routedPoints.empty())
625 if (routedPoints.front() != routedStart && routedPoints.front() != start)
626 routedPoints.insert(routedPoints.begin(), routedStart);
628 if (routedPoints.front() != start)
629 routedPoints.insert(routedPoints.begin(), start);
631 if (routedPoints.back() != routedEnd && routedPoints.back() != end)
632 routedPoints.push_back(routedEnd);
634 if (routedPoints.back() != end)
635 routedPoints.push_back(end);