SILICON
Loading...
Searching...
No Matches
circuitAutoplacerHeuristics.hpp
Go to the documentation of this file.
1/*
2 Copyright (c) 2026. Giulio Cocconi
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <algorithm>
21#include <compare>
22#include <cstddef>
23#include <span>
24#include <utility>
25
26#include <QPointF>
27#include <QRectF>
28
30
31namespace SILICON::ui::detail {
32
35 std::size_t overlaps = 0;
36 std::size_t directionViolations = 0;
37 std::size_t sharedSegments = 0;
38 std::size_t crossings = 0;
39 double routingCost = 0.0;
40 double area = 0.0;
41
42 auto operator<=>(const AutoplacementQuality&) const = default;
43};
44
46inline qreal portDirectionViolationDistance(const PortSide side, const QPointF& endpoint,
47 const QPointF& peer)
48{
49 qreal outwardDistance = 0.0;
50 switch (side) {
51 case PortSide::LEFT: outwardDistance = endpoint.x() - peer.x(); break;
52 case PortSide::RIGHT: outwardDistance = peer.x() - endpoint.x(); break;
53 case PortSide::UP: outwardDistance = endpoint.y() - peer.y(); break;
54 case PortSide::DOWN: outwardDistance = peer.y() - endpoint.y(); break;
55 }
56 return std::max<qreal>(0.0, -outwardDistance);
57}
58
60inline bool hasBoundsClearance(const QRectF& candidate,
61 const std::span<const QRectF> accepted,
62 const qreal clearance)
63{
64 const qreal halfClearance = clearance / 2.0;
65 const QRectF paddedCandidate =
66 candidate.adjusted(-halfClearance, -halfClearance, halfClearance, halfClearance);
67 return std::ranges::none_of(accepted, [&](const QRectF& bounds) {
68 return paddedCandidate.intersects(
69 bounds.adjusted(-halfClearance, -halfClearance, halfClearance, halfClearance));
70 });
71}
72
74inline bool boundsLieOutsideTarget(const PortSide side, const QRectF& item,
75 const QRectF& target, const qreal clearance)
76{
77 switch (side) {
78 case PortSide::LEFT: return item.right() <= target.left() - clearance;
79 case PortSide::RIGHT: return item.left() >= target.right() + clearance;
80 case PortSide::UP: return item.bottom() <= target.top() - clearance;
81 case PortSide::DOWN: return item.top() >= target.bottom() + clearance;
82 }
83 std::unreachable();
84}
85
86} // namespace SILICON::ui::detail
bool boundsLieOutsideTarget(const PortSide side, const QRectF &item, const QRectF &target, const qreal clearance)
qreal portDirectionViolationDistance(const PortSide side, const QPointF &endpoint, const QPointF &peer)
bool hasBoundsClearance(const QRectF &candidate, const std::span< const QRectF > accepted, const qreal clearance)
auto operator<=>(const AutoplacementQuality &) const =default