SILICON
Loading...
Searching...
No Matches
localWireEditor.cpp
Go to the documentation of this file.
1#include "localWireEditor.hpp"
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <map>
7
8#include "wireRouting.hpp"
9
10namespace SILICON::ui {
11namespace {
12bool onSpan(QPointF point, QPointF first, QPointF last)
13{
14 const std::array span{first, last};
15 return core::pointOnOrthogonalRoute(point, span);
16}
17
18bool portAt(std::span<const RoutingComponent> components, QPointF point)
19{
20 for (const auto& component : components)
21 if (std::ranges::find(component.ports, point) != component.ports.end())
22 return true;
23 return false;
24}
25
26bool attachedAt(const RoutingSegment& selected, const RoutingSegment& other,
27 const QPointF point, const QPointF otherTip)
28{
29 if (selected.electricalVertices.empty() || other.electricalVertices.empty())
30 return point == otherTip;
31 for (const auto& [vertex, location] : selected.electricalVertices)
32 if (location == point && vertex)
33 for (const auto& [otherVertex, otherLocation] : other.electricalVertices)
34 if (vertex == otherVertex && otherLocation == otherTip)
35 return true;
36 return false;
37}
38} // namespace
39
40LocalWireEditor::LocalWireEditor(std::span<const RoutingComponent> components,
41 std::span<const RoutingSegment> segments,
42 const qreal gridSize)
44{}
45
46QPointF LocalWireEditor::snap(QPointF point) const
47{
48 if (gridSize > 0) {
49 point.setX(std::round(point.x() / gridSize) * gridSize);
50 point.setY(std::round(point.y() / gridSize) * gridSize);
51 }
52 return point;
53}
54
55const RoutingSegment* LocalWireEditor::find(const uint64_t id) const
56{
57 const auto it = std::ranges::find(segments, id, &RoutingSegment::id);
58 return it == segments.end() ? nullptr : &*it;
59}
60
62 const std::span<const ComponentRouteMove> moves) const
63{
64 using PointKey = std::pair<qreal, qreal>;
65 std::map<PointKey, QPointF> movedPorts;
66 std::vector<QRectF> movedBounds;
67 std::vector<RoutingComponent> movedComponents(components.begin(), components.end());
68 for (const auto& move : moves) {
69 const auto component = std::ranges::find(components, move.id,
71 if (component == components.end())
72 return std::nullopt;
73 const QRectF bounds = move.bounds.normalized();
74 const QPointF delta = bounds.topLeft() - component->bounds.normalized().topLeft();
75 movedBounds.push_back(bounds);
76 auto& movedComponent = movedComponents[component - components.begin()];
77 movedComponent.bounds = bounds;
78 for (auto& port : movedComponent.ports)
79 port += delta;
80 for (const QPointF port : component->ports) {
81 const PointKey key{port.x(), port.y()};
82 if (const auto existing = movedPorts.find(key); existing != movedPorts.end()
83 && existing->second != port + delta)
84 return std::nullopt;
85 movedPorts[key] = port + delta;
86 }
87 }
88
89 std::vector<RoutedSegment> changes;
90 for (const auto& segment : segments) {
91 if (segment.points.size() < 2)
92 return std::nullopt;
93 const auto first = movedPorts.find({segment.points.front().x(),
94 segment.points.front().y()});
95 const auto last = movedPorts.find({segment.points.back().x(),
96 segment.points.back().y()});
97 const bool moveFirst = first != movedPorts.end()
98 && first->second != segment.points.front();
99 const bool moveLast = last != movedPorts.end()
100 && last->second != segment.points.back();
101 if (moveFirst && moveLast)
102 return std::nullopt;
103 if (!moveFirst && !moveLast)
104 continue;
105 auto route = moveAttachedEndpoint(segment, moveFirst,
106 moveFirst ? first->second : last->second);
107 if (!route)
108 return std::nullopt;
109 changes.push_back({segment.id, std::move(*route)});
110 }
111
112 // Validate against destination obstacles; the original component has vacated
113 // its snapshot bounds, which the new wire may legitimately cross.
114 auto result =
115 LocalWireEditor(movedComponents, segments, gridSize).finish(0, std::move(changes));
116 if (!result)
117 return std::nullopt;
118 for (const auto& route : *result)
119 for (const QRectF bounds : movedBounds)
121 std::array{bounds}))
122 return std::nullopt;
123 return result;
124}
125
126std::optional<std::vector<QPointF>> LocalWireEditor::moveAttachedEndpoint(
127 const RoutingSegment& segment, const bool first, const QPointF destination)
128{
129 auto points = segment.points;
130 if (points.size() < 2)
131 return std::nullopt;
132 if (!first)
133 std::ranges::reverse(points);
134 const QPointF old = points.front();
135 if (old == destination)
136 return segment.points;
137 if (points[1].x() == old.x()) {
138 if (destination.x() != old.x())
139 points.insert(points.begin() + 1, {destination.x(), points[1].y()});
140 } else if (destination.y() != old.y()) {
141 points.insert(points.begin() + 1, {points[1].x(), destination.y()});
142 }
143 points.front() = destination;
144 points = core::canonicalizeOrthogonalRoute(std::move(points));
145 if (!first)
146 std::ranges::reverse(points);
147 return points;
148}
149
151 const uint64_t segmentId, const std::size_t index, QPointF position) const
152{
153 const auto* selected = find(segmentId);
154 if (!selected || index == 0 || index + 1 >= selected->points.size())
155 return std::nullopt;
156 const auto& old = selected->points;
157 position = snap(position);
158 std::vector<QPointF> points;
159 points.insert(points.end(), old.begin(), old.begin() + static_cast<std::ptrdiff_t>(index));
160 const bool enteringHorizontal = old[index - 1].y() == old[index].y();
161 points.push_back(enteringHorizontal ? QPointF(position.x(), old[index - 1].y())
162 : QPointF(old[index - 1].x(), position.y()));
163 points.push_back(position);
164 points.push_back(enteringHorizontal ? QPointF(old[index + 1].x(), position.y())
165 : QPointF(position.x(), old[index + 1].y()));
166 points.insert(points.end(), old.begin() + static_cast<std::ptrdiff_t>(index + 1), old.end());
167 std::vector<RoutedSegment> routes{{segmentId, std::move(points)}};
168 const QPointF oldJunction = old[index];
169 for (const auto& other : segments) {
170 if (other.id == segmentId || other.net != selected->net
171 || other.points.size() < 2)
172 continue;
173 for (const bool first : {true, false}) {
174 const QPointF tip = first ? other.points.front() : other.points.back();
175 if (!attachedAt(*selected, other, oldJunction, tip))
176 continue;
177 if (portAt(components, tip))
178 return std::nullopt;
179 auto adjusted = moveAttachedEndpoint(other, first, position);
180 if (!adjusted)
181 return std::nullopt;
182 routes.push_back({other.id, std::move(*adjusted)});
183 break;
184 }
185 }
186 return finish(segmentId, std::move(routes));
187}
188
190 const uint64_t segmentId, const std::size_t index, QPointF displacement) const
191{
192 const auto* selected = find(segmentId);
193 if (!selected || index + 1 >= selected->points.size())
194 return std::nullopt;
195 const auto& old = selected->points;
196 const bool horizontal = old[index].y() == old[index + 1].y();
197 displacement = snap(displacement);
198 if (horizontal)
199 displacement.setX(0);
200 else
201 displacement.setY(0);
202 if (displacement.isNull())
203 return std::nullopt;
204
205 std::vector<QPointF> points = old;
206 const QPointF movedFirst = old[index] + displacement;
207 const QPointF movedLast = old[index + 1] + displacement;
208 if (index == 0)
209 points.insert(points.begin() + 1, movedFirst);
210 else
211 points[index] = movedFirst;
212 const std::size_t lastIndex = index + 1 + (index == 0 ? 1 : 0);
213 if (index + 2 == old.size())
214 points.insert(points.begin() + static_cast<std::ptrdiff_t>(lastIndex), movedLast);
215 else
216 points[lastIndex] = movedLast;
217
218 std::vector<RoutedSegment> routes{{segmentId, std::move(points)}};
219 for (const auto& other : segments) {
220 if (other.id == segmentId || other.net != selected->net
221 || other.points.size() < 2)
222 continue;
223 for (const bool first : {true, false}) {
224 const QPointF tip = first ? other.points.front() : other.points.back();
225 if (!onSpan(tip, old[index], old[index + 1]))
226 continue;
227 if (!attachedAt(*selected, other, tip, tip))
228 continue;
229 if ((index == 0 && tip == old.front())
230 || (index + 2 == old.size() && tip == old.back()))
231 continue;
232 if (portAt(components, tip))
233 return std::nullopt;
234 auto adjusted = moveAttachedEndpoint(other, first, tip + displacement);
235 if (!adjusted)
236 return std::nullopt;
237 routes.push_back({other.id, std::move(*adjusted)});
238 break;
239 }
240 }
241 return finish(segmentId, std::move(routes));
242}
243
245 const uint64_t editedId, std::vector<RoutedSegment> routes) const
246{
247 for (auto& route : routes)
248 route.points = core::canonicalizeOrthogonalRoute(std::move(route.points));
249 if (!valid(routes, editedId))
250 return std::nullopt;
251 std::vector<RoutedSegment> complete;
252 complete.reserve(segments.size());
253 for (const auto& segment : segments) {
254 const auto found = std::ranges::find(routes, segment.id, &RoutedSegment::id);
255 complete.push_back(found == routes.end() ? RoutedSegment{segment.id, segment.points}
256 : *found);
257 }
258 return complete;
259}
260
261bool LocalWireEditor::valid(std::span<const RoutedSegment> routes,
262 const uint64_t editedSegment) const
263{
264 std::map<uint64_t, std::span<const QPointF>> candidate;
265 for (const auto& route : routes) {
266 if (route.points.size() < 2 || !core::isOrthogonalRoute(route.points)
268 return false;
269 candidate[route.id] = route.points;
270 }
271 for (const auto& segment : segments) {
272 const auto points = candidate.contains(segment.id) ? candidate.at(segment.id)
273 : std::span<const QPointF>(segment.points);
274 if (!candidate.contains(segment.id))
275 continue;
276 for (const auto& component : components) {
277 const std::array obstacle{component.bounds};
279 && !core::orthogonalRouteCrossesObstacleInterior(segment.points, obstacle))
280 return false;
281 }
282 for (const auto& other : segments) {
283 if (other.id == segment.id)
284 continue;
285 const auto otherPoints = candidate.contains(other.id) ? candidate.at(other.id)
286 : std::span<const QPointF>(other.points);
287 if (other.net != segment.net) {
288 if (core::orthogonalRoutesShareSegment(points, otherPoints))
289 return false;
290 // A strict crossing is disconnected. New endpoint contact would be ambiguous.
291 for (const QPointF tip : {points.front(), points.back()})
292 if (core::pointOnOrthogonalRoute(tip, otherPoints)
293 && !core::pointOnOrthogonalRoute(tip, other.points))
294 return false;
295 for (const QPointF tip : {otherPoints.front(), otherPoints.back()})
296 if (core::pointOnOrthogonalRoute(tip, points)
297 && !core::pointOnOrthogonalRoute(tip, segment.points))
298 return false;
299 } else {
300 if (!segment.electricalVertices.empty() && !other.electricalVertices.empty()) {
301 for (const auto& [vertex, oldPosition] : segment.electricalVertices)
302 for (const auto& [otherVertex, otherPosition] : other.electricalVertices) {
303 Q_UNUSED(otherPosition);
304 if (!vertex || vertex != otherVertex)
305 continue;
306 std::optional<QPointF> position;
307 const auto endpointPosition = [&](const RoutingSegment& member,
308 std::span<const QPointF> route) {
309 if (member.electricalVertices.front().first == vertex)
310 return std::optional<QPointF>{route.front()};
311 if (member.electricalVertices.size() > 1
312 && member.electricalVertices[1].first == vertex)
313 return std::optional<QPointF>{route.back()};
314 return std::optional<QPointF>{};
315 };
316 position = endpointPosition(segment, points);
317 const auto secondPosition = endpointPosition(other, otherPoints);
318 if (position && secondPosition && *position != *secondPosition)
319 return false;
320 if (!position)
321 position = secondPosition.value_or(oldPosition);
322 if (!core::pointOnOrthogonalRoute(*position, points)
323 || !core::pointOnOrthogonalRoute(*position, otherPoints))
324 return false;
325 }
326 } else {
327 const bool hadContact = core::orthogonalRoutesTouch(segment.points, other.points);
328 if (segment.id != editedSegment && other.id != editedSegment
329 && hadContact && !core::orthogonalRoutesTouch(points, otherPoints))
330 return false;
331 }
332 }
333 }
334 }
335 return true;
336}
337
338} // namespace SILICON::ui
const RoutingSegment * find(uint64_t id) const
Returns the borrowed segment with the given identifier, or nullptr.
std::span< const RoutingSegment > segments
QPointF snap(QPointF point) const
Snaps a scene point to the grid when the grid spacing is positive.
RoutePreview moveBend(uint64_t segmentId, std::size_t index, QPointF position) const
Moves an interior bend and adjusts electrically attached branches.
LocalWireEditor(std::span< const RoutingComponent > components, std::span< const RoutingSegment > segments, qreal gridSize)
Borrows the original geometry for local wire edits.
RoutePreview moveEdge(uint64_t segmentId, std::size_t index, QPointF displacement) const
Offsets an edge perpendicular to its direction, preserving endpoints.
std::span< const RoutingComponent > components
RoutePreview moveComponents(std::span< const ComponentRouteMove > moves) const
Adjusts wire endpoints attached to moved component ports.
RoutePreview finish(uint64_t editedId, std::vector< RoutedSegment > routes) const
Canonicalizes and validates edits, then fills in unchanged snapshot routes.
bool valid(std::span< const RoutedSegment > routes, uint64_t editedSegment) const
Checks route geometry, obstacles, and preservation of electrical contacts.
static std::optional< std::vector< QPointF > > moveAttachedEndpoint(const RoutingSegment &segment, bool first, QPointF destination)
Adjusts one endpoint while keeping its route orthogonal.
std::vector< QPointF > canonicalizeOrthogonalRoute(std::vector< QPointF > points)
Removes consecutive duplicates and redundant collinear interior points.
bool orthogonalRouteCrossesObstacleInterior(const std::span< const QPointF > route, const std::span< const QRectF > obstacles)
bool orthogonalRoutesTouch(const std::span< const QPointF > first, const std::span< const QPointF > second)
Returns whether an endpoint of either route lies on the other route.
bool orthogonalRouteSelfIntersects(const std::span< const QPointF > points)
Detects crossings, touches, or retraced portions within one route.
bool pointOnOrthogonalRoute(const QPointF point, const std::span< const QPointF > route)
Returns whether point lies on an orthogonal polyline.
bool isOrthogonalRoute(const std::span< const QPointF > points)
Returns whether every consecutive route segment is axis-aligned.
bool orthogonalRoutesShareSegment(const std::span< const QPointF > first, const std::span< const QPointF > second)
Returns true when two orthogonal polylines share a positive-length segment.
std::optional< std::vector< RoutedSegment > > RoutePreview
std::vector< std::pair< uint64_t, QPointF > > electricalVertices