SILICON
Loading...
Searching...
No Matches
boundaryIoPlacement.cpp
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
19
20#include <algorithm>
21#include <cmath>
22#include <cstddef>
23#include <limits>
24#include <numeric>
25#include <ranges>
26#include <utility>
27
29namespace {
30
31 bool hasClearance(const QRectF& candidate, std::span<const QRectF> accepted,
32 const qreal clearance)
33 {
34 const qreal halfClearance = clearance / 2.0;
35 const QRectF paddedCandidate =
36 candidate.adjusted(-halfClearance, -halfClearance, halfClearance, halfClearance);
37 return std::ranges::none_of(accepted, [&](const QRectF& bounds) {
38 return paddedCandidate.intersects(
39 bounds.adjusted(-halfClearance, -halfClearance, halfClearance, halfClearance));
40 });
41 }
42
43 QPointF outwardStep(const PortSide side, const qreal grid)
44 {
45 switch (side) {
46 case PortSide::LEFT: return {grid, 0.0};
47 case PortSide::RIGHT: return {-grid, 0.0};
48 case PortSide::UP: return {0.0, grid};
49 case PortSide::DOWN: return {0.0, -grid};
50 }
51 std::unreachable();
52 }
53
54 qreal tangentCoordinate(const QPointF& point, const PortSide side)
55 {
56 return side == PortSide::UP || side == PortSide::DOWN ? point.x() : point.y();
57 }
58
59 qreal tangentStart(const QRectF& bounds, const PortSide side)
60 {
61 return side == PortSide::UP || side == PortSide::DOWN ? bounds.left() : bounds.top();
62 }
63
64 qreal tangentSize(const QRectF& bounds, const PortSide side)
65 {
66 return side == PortSide::UP || side == PortSide::DOWN ? bounds.width()
67 : bounds.height();
68 }
69
70 QPointF tangentStep(const PortSide side, const qreal distance)
71 {
72 return side == PortSide::UP || side == PortSide::DOWN ? QPointF{distance, 0.0}
73 : QPointF{0.0, distance};
74 }
75
76} // namespace
77
79 std::span<const BoundaryIoLaneItem> items,
80 std::span<const QRectF> fixedObstacles,
81 const qreal componentClearance,
82 const qreal obstaclePadding, const qreal grid)
83{
84 BoundaryIoLaneResult result{.positions = std::vector<QPointF>(items.size()),
85 .bounds = std::vector<QRectF>(items.size())};
86 if (items.empty())
87 return result;
88
89 std::vector<std::size_t> order(items.size());
90 std::iota(order.begin(), order.end(), 0);
91 std::ranges::sort(order, [&](const std::size_t lhs, const std::size_t rhs) {
92 const auto lhsKey = std::pair{tangentCoordinate(items[lhs].preferredPort, side),
93 items[lhs].stableOrder};
94 const auto rhsKey = std::pair{tangentCoordinate(items[rhs].preferredPort, side),
95 items[rhs].stableOrder};
96 return lhsKey < rhsKey;
97 });
98
99 // Find the closest non-overlapping positions along the common boundary lane. This is
100 // an isotonic regression: after subtracting the mandatory preceding widths and gaps,
101 // the remaining coordinates only need to be nondecreasing. Pooling adjacent
102 // violations spreads a collision around its preferred port instead of pushing every
103 // later symbol in one direction.
104 struct Block {
105 std::size_t begin;
106 std::size_t end;
107 qreal sum;
108 };
109 std::vector<qreal> offsets(items.size());
110 std::vector<qreal> desired(items.size());
111 std::vector<Block> blocks;
112 for (std::size_t orderedIndex = 0; orderedIndex < order.size(); ++orderedIndex) {
113 const std::size_t itemIndex = order[orderedIndex];
114 if (orderedIndex != 0) {
115 const std::size_t previous = order[orderedIndex - 1];
116 offsets[orderedIndex] = offsets[orderedIndex - 1]
117 + tangentSize(items[previous].preferredBounds, side)
118 + componentClearance;
119 }
120 desired[orderedIndex] =
121 tangentStart(items[itemIndex].preferredBounds, side) - offsets[orderedIndex];
122 blocks.push_back({orderedIndex, orderedIndex + 1, desired[orderedIndex]});
123 while (blocks.size() >= 2) {
124 const Block& previous = blocks[blocks.size() - 2];
125 const Block& current = blocks.back();
126 const qreal previousMean =
127 previous.sum / static_cast<qreal>(previous.end - previous.begin);
128 const qreal currentMean =
129 current.sum / static_cast<qreal>(current.end - current.begin);
130 if (previousMean <= currentMean)
131 break;
132 Block merged{previous.begin, current.end, previous.sum + current.sum};
133 blocks.pop_back();
134 blocks.back() = merged;
135 }
136 }
137
138 std::vector<qreal> tangentDeltas(items.size());
139 for (const Block& block : blocks) {
140 const qreal mean = block.sum / static_cast<qreal>(block.end - block.begin);
141 for (std::size_t orderedIndex = block.begin; orderedIndex < block.end;
142 ++orderedIndex) {
143 const std::size_t itemIndex = order[orderedIndex];
144 const qreal continuousDelta =
145 mean + offsets[orderedIndex]
146 - tangentStart(items[itemIndex].preferredBounds, side);
147 tangentDeltas[itemIndex] = std::round(continuousDelta / grid) * grid;
148 }
149 }
150
151 // Rounding onto the scene grid can reduce a gap by one grid step. Repair it in the
152 // stable tangential order while keeping every component in the same normal lane.
153 qreal previousEnd = -std::numeric_limits<qreal>::infinity();
154 for (const std::size_t itemIndex : order) {
155 const qreal start =
156 tangentStart(items[itemIndex].preferredBounds, side) + tangentDeltas[itemIndex];
157 if (start < previousEnd + componentClearance) {
158 const qreal correction =
159 std::ceil((previousEnd + componentClearance - start) / grid) * grid;
160 tangentDeltas[itemIndex] += correction;
161 }
162 previousEnd = tangentStart(items[itemIndex].preferredBounds, side)
163 + tangentDeltas[itemIndex]
164 + tangentSize(items[itemIndex].preferredBounds, side);
165 }
166
167 // If another side occupies a corner, move this whole row/column outward together.
168 // A uniform normal translation retains the aligned boundary edge.
169 const QPointF step = outwardStep(side, grid);
170 const qreal clearance = std::max(componentClearance, obstaclePadding);
171 for (std::size_t lane = 0;; ++lane) {
172 const QPointF normalDelta = static_cast<qreal>(lane) * step;
173 bool clear = true;
174 for (std::size_t i = 0; i < items.size(); ++i) {
175 const QPointF delta = normalDelta + tangentStep(side, tangentDeltas[i]);
176 if (!hasClearance(items[i].preferredBounds.translated(delta), fixedObstacles,
177 clearance)) {
178 clear = false;
179 break;
180 }
181 }
182 if (!clear)
183 continue;
184
185 for (std::size_t i = 0; i < items.size(); ++i) {
186 const QPointF delta = normalDelta + tangentStep(side, tangentDeltas[i]);
187 result.positions[i] = items[i].preferredPosition + delta;
188 result.bounds[i] = items[i].preferredBounds.translated(delta);
189 }
190 break;
191 }
192
193 return result;
194}
195
196} // namespace SILICON::ui::detail
BoundaryIoLaneResult placeBoundaryIoInLanes(const PortSide side, std::span< const BoundaryIoLaneItem > items, std::span< const QRectF > fixedObstacles, const qreal componentClearance, const qreal obstaclePadding, const qreal grid)