12bool onSpan(QPointF point, QPointF first, QPointF last)
14 const std::array span{first, last};
18bool portAt(std::span<const RoutingComponent> components, QPointF point)
20 for (
const auto& component : components)
21 if (std::ranges::find(component.ports, point) != component.ports.end())
27 const QPointF point,
const QPointF otherTip)
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)
41 std::span<const RoutingSegment>
segments,
62 const std::span<const ComponentRouteMove> moves)
const
64 using PointKey = std::pair<qreal, qreal>;
65 std::map<PointKey, QPointF> movedPorts;
66 std::vector<QRectF> movedBounds;
68 for (
const auto& move : moves) {
69 const auto component = std::ranges::find(
components, move.id,
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)
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)
85 movedPorts[key] = port + delta;
89 std::vector<RoutedSegment> changes;
90 for (
const auto& segment :
segments) {
91 if (segment.points.size() < 2)
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)
103 if (!moveFirst && !moveLast)
106 moveFirst ? first->second : last->second);
109 changes.push_back({segment.id, std::move(*route)});
118 for (
const auto& route : *result)
119 for (
const QRectF bounds : movedBounds)
127 const RoutingSegment& segment,
const bool first,
const QPointF destination)
129 auto points = segment.
points;
130 if (points.size() < 2)
133 std::ranges::reverse(points);
134 const QPointF old = points.front();
135 if (old == destination)
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()});
143 points.front() = destination;
146 std::ranges::reverse(points);
151 const uint64_t segmentId,
const std::size_t index, QPointF position)
const
153 const auto* selected =
find(segmentId);
154 if (!selected || index == 0 || index + 1 >= selected->points.size())
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)
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))
182 routes.push_back({other.id, std::move(*adjusted)});
186 return finish(segmentId, std::move(routes));
190 const uint64_t segmentId,
const std::size_t index, QPointF displacement)
const
192 const auto* selected =
find(segmentId);
193 if (!selected || index + 1 >= selected->points.size())
195 const auto& old = selected->points;
196 const bool horizontal = old[index].y() == old[index + 1].y();
197 displacement =
snap(displacement);
199 displacement.setX(0);
201 displacement.setY(0);
202 if (displacement.isNull())
205 std::vector<QPointF> points = old;
206 const QPointF movedFirst = old[index] + displacement;
207 const QPointF movedLast = old[index + 1] + displacement;
209 points.insert(points.begin() + 1, movedFirst);
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);
216 points[lastIndex] = movedLast;
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)
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]))
227 if (!attachedAt(*selected, other, tip, tip))
229 if ((index == 0 && tip == old.front())
230 || (index + 2 == old.size() && tip == old.back()))
237 routes.push_back({other.id, std::move(*adjusted)});
241 return finish(segmentId, std::move(routes));
245 const uint64_t editedId, std::vector<RoutedSegment> routes)
const
247 for (
auto& route : routes)
249 if (!
valid(routes, editedId))
251 std::vector<RoutedSegment> complete;
253 for (
const auto& segment :
segments) {
255 complete.push_back(found == routes.end() ?
RoutedSegment{segment.id, segment.points}
262 const uint64_t editedSegment)
const
264 std::map<uint64_t, std::span<const QPointF>> candidate;
265 for (
const auto& route : routes) {
269 candidate[route.id] = route.points;
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))
277 const std::array obstacle{component.bounds};
282 for (
const auto& other :
segments) {
283 if (other.id == segment.id)
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) {
291 for (
const QPointF tip : {points.front(), points.back()})
295 for (
const QPointF tip : {otherPoints.front(), otherPoints.back()})
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)
306 std::optional<QPointF> position;
308 std::span<const QPointF> route) {
310 return std::optional<QPointF>{route.front()};
313 return std::optional<QPointF>{route.back()};
314 return std::optional<QPointF>{};
316 position = endpointPosition(segment, points);
317 const auto secondPosition = endpointPosition(other, otherPoints);
318 if (position && secondPosition && *position != *secondPosition)
321 position = secondPosition.value_or(oldPosition);
328 if (segment.id != editedSegment && other.id != editedSegment
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< QPointF > points
std::vector< std::pair< uint64_t, QPointF > > electricalVertices