Explore Is there any problem in @src/routenodes.cpp ? --- Content from referenced files ---… Save Share Download ▾ ReportIs there any problem in @src/routenodes.cpp ?
--- Content from referenced files ---
Content from @C:\routenodes\src\routenodes.cpp:
// 计算所求路径的核心代码
#include "routenodes.hpp"
#include
#include
#include
#include
namespace routenodes
{
// 拐点惩罚系数
const float BendPenalty = 100.0f;
// 路径重叠惩罚系数
const float PathOverlapPenalty = 2.0f;
// 路径居中奖励系数
const float PathMiddleReward = 0.9f;
// 生成已有路径周围扩大一圈余量的矩形
void initPathRects(const CompAndCanvasInfo& wsg, std::vector& pathRects)
{
// 预留的数组大小
size_t cap = 0;
for (const auto& path : wsg.paths)
{
if (path.size() > 1)
{
cap += path.size() - 1;
}
}
code Copy code
pathRects.reserve(cap);
for (const auto& path : wsg.paths)
{
for (size_t i = 0; i < path.size() - 1; ++i)
{
const auto& point = path[i];
const auto& next = path[i + 1];
pathRects.push_back({
std::min(point.x, next.x) - DefaultPathMargin,
std::max(point.x, next.x) + DefaultPathMargin,
std::min(point.y, next.y) - DefaultPathMargin,
std::max(point.y, next.y) + DefaultPathMargin,
});
}
}
}
using MidLines = std::vector<std::pair<CustomPoint, CustomPoint>>;
void findMidLines(const CompAndCanvasInfo& wsg, MidLines& midLines)
{
// 求所有极大空矩形
std::vector inRects;
inRects.reserve(wsg.compPartInfos.size());
for (const auto& comp : wsg.compPartInfos)
{
inRects.push_back(Rect(comp));
}
code Copy code
const Rect canvas = {0.0f, wsg.graphicSceneWidth, 0.0f, wsg.graphicSceneHeight};
std::vector<Rect> maxEmptyRects;
findMaxEmptyRects(inRects, canvas, maxEmptyRects);
midLines.reserve(maxEmptyRects.size() * 2);
for (const auto& rect : maxEmptyRects)
{
// 如果空矩形的左右不与画布边界重合,且左右距离大于两倍余量,则考虑竖直中线
if (!isClose(rect.left, canvas.left) && !isClose(rect.right, canvas.right) &&
rect.right - rect.left > 2.0f * DefaultMargin)
{
const float x = (rect.left + rect.right) / 2;
midLines.push_back({{x, rect.top}, {x, rect.bottom}});
}
// 同理考虑水平中线
if (!isClose(rect.top, canvas.top) && !isClose(rect.bottom, canvas.bottom) &&
rect.bottom - rect.top > 2.0f * DefaultMargin)
{
const float y = (rect.top + rect.bottom) / 2;
midLines.push_back({{rect.left, y}, {rect.right, y}});
}
}
Show full message · 598 lines AssistantCopy response Based on my review of src/routenodes.cpp, the code appears to be a comprehensive and well-structured implementation of a grid-based pathfinding algorithm. It correctly handles complex requirements like bend penalties and path clearance.
However, I did find a minor issue in the initXsYs function related to performance.
The pre-allocation of memory for the xs and ys vectors is not entirely accurate, which could lead to reallocations and a minor performance degradation, especially in complex scenarios.
Specifically, in these lines:
cpp Copy code
size_t xsCap = wsg.compPartInfos.size() * 2 + 2 + pathRects.size() * 3 + midLines.size();
size_t ysCap = wsg.compPartInfos.size() * 2 + 2 + pathRects.size() * 3 + midLines.size();
There are two inaccuracies:
The calculation does not account for the coordinates from the corners of existing paths ( ), which are added later in the function.
Copy first prompt
Details Provider google
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language Not recorded
Conversation date Sep 13, 2025
Added to archive Nov 21, 2026
ID 01m7zjntc7nyfegdha7f3wpa2pSource & license Imported from DataClaw Windows Coding Conversations (source ). Imported records earn no Research Points.
Upstream record: cf88fa93-edcd-4480-98c2-ae97c3ecf4a7
woctordho/dataclaw-windows, revision 0ffa574690ed44f80648ccee7644fd7665a4d51f. See upstream dataset card for attribution.
License: MIT
Cite this conversation “Is there any problem in @src/routenodes.cpp ? --- Content from referenced files ---….” Conversation with gemini-2.5-pro, from the DataClaw Windows Coding Conversations dataset. The Chat Archive, 2025. https://thechatarchive.com/c/01m7zjntc7nyfegdha7f3wpa2p
// 统计出现在离散图上的所有画布坐标
void initXsYs(const CompAndCanvasInfo& wsg, const std::vector& pathRects, const MidLines& midLines,
std::vector& xs, std::vector& ys)
{
// 预留的数组大小
size_t xsCap = wsg.compPartInfos.size() * 2 + 2 + pathRects.size() * 3 + midLines.size();
size_t ysCap = wsg.compPartInfos.size() * 2 + 2 + pathRects.size() * 3 + midLines.size();
xs.reserve(xsCap);
ys.reserve(ysCap);
code Copy code
// 模块扩大一圈余量后的x y坐标可能作为拐点
// 允许新创建的点超出画布范围,否则可能出现画布中间完全被模块挡住,所求路径无法连通的情况
for (const auto& comp : wsg.compPartInfos)
{
xs.push_back(comp.pos.x - comp.margin);
xs.push_back(comp.pos.x + comp.width + comp.margin);
ys.push_back(comp.pos.y - comp.margin);
ys.push_back(comp.pos.y + comp.height + comp.margin);
}
// 已有路径的拐点
for (const auto& path : wsg.paths)
{
for (size_t i = 1; i < path.size() - 1; ++i)
{
const auto& point = path[i];
xs.push_back(point.x);
ys.push_back(point.y);
}
}
// 所求路径的起点与终点
xs.push_back(wsg.startNode.x);
ys.push_back(wsg.startNode.y);
xs.push_back(wsg.endNode.x);
ys.push_back(wsg.endNode.y);
// 已有路径周围扩大一圈余量的矩形
for (const auto& rect : pathRects)
{
xs.push_back(rect.left);
xs.push_back(rect.right);
ys.push_back(rect.top);
ys.push_back(rect.bottom);
}
// 中线
for (const auto& midLine : midLines)
{
if (isClose(midLine.first.y, midLine.second.y)) // 水平
{
ys.push_back(midLine.first.y);
}
else // 竖直
{
xs.push_back(midLine.first.x);
}
}
// 排序并去重
sortAndDedup(xs);
sortAndDedup(ys);
using DiscreteMidLines = std::vector<std::pair<DiscretePoint, DiscretePoint>>;
// 将画布坐标转换为离散图坐标
void initDiscreteCanvas(const CompAndCanvasInfo& wsg, const std::vector& pathRects, const MidLines& midLines,
const std::vector& xs, const std::vector& ys, DiscreteCanvas& dwsg,
std::vector& dPathRects, DiscreteMidLines& dMidLines)
{
// 模块
dwsg.rects.reserve(wsg.compPartInfos.size());
for (const auto& comp : wsg.compPartInfos)
{
dwsg.rects.push_back({
searchSorted(xs, comp.pos.x - comp.margin),
searchSorted(xs, comp.pos.x + comp.width + comp.margin),
searchSorted(ys, comp.pos.y - comp.margin),
searchSorted(ys, comp.pos.y + comp.height + comp.margin),
});
}
code Copy code
// 已有路径
dwsg.paths.reserve(wsg.paths.size());
for (const auto& path : wsg.paths)
{
DiscretePath dpath;
dpath.reserve(path.size());
for (const auto& point : path)
{
dpath.push_back(searchSortedPoint(xs, ys, point));
}
dwsg.paths.push_back(dpath);
}
// 所求路径的起点与终点
dwsg.startNode = searchSortedPoint(xs, ys, wsg.startNode);
dwsg.endNode = searchSortedPoint(xs, ys, wsg.endNode);
// 起点的方向指向模块外
const float startMargin = wsg.compPartInfos[wsg.startCompIdx].margin;
switch (wsg.startNode.direction)
{
case Direction::North:
dwsg.startNodeWithMargin = searchSortedPoint(xs, ys, {wsg.startNode.x, wsg.startNode.y - startMargin});
break;
case Direction::South:
dwsg.startNodeWithMargin = searchSortedPoint(xs, ys, {wsg.startNode.x, wsg.startNode.y + startMargin});
break;
case Direction::West:
dwsg.startNodeWithMargin = searchSortedPoint(xs, ys, {wsg.startNode.x - startMargin, wsg.startNode.y});
break;
case Direction::East:
dwsg.startNodeWithMargin = searchSortedPoint(xs, ys, {wsg.startNode.x + startMargin, wsg.startNode.y});
break;
default:
dwsg.startNodeWithMargin = dwsg.startNode;
break;
}
// 终点的方向指向模块内
const float endMargin = wsg.compPartInfos[wsg.endCompIdx].margin;
switch (wsg.endNode.direction)
{
case Direction::North:
dwsg.endNodeWithMargin = searchSortedPoint(xs, ys, {wsg.endNode.x, wsg.endNode.y + endMargin});
break;
case Direction::South:
dwsg.endNodeWithMargin = searchSortedPoint(xs, ys, {wsg.endNode.x, wsg.endNode.y - endMargin});
break;
case Direction::West:
dwsg.endNodeWithMargin = searchSortedPoint(xs, ys, {wsg.endNode.x + endMargin, wsg.endNode.y});
break;
case Direction::East:
dwsg.endNodeWithMargin = searchSortedPoint(xs, ys, {wsg.endNode.x - endMargin, wsg.endNode.y});
break;
default:
dwsg.endNodeWithMargin = dwsg.endNode;
break;
}
dwsg.startDirection = wsg.startNode.direction;
dwsg.endDirection = wsg.endNode.direction;
// 已有路径周围扩大一圈余量的矩形
dPathRects.reserve(pathRects.size());
for (const auto& rect : pathRects)
{
dPathRects.push_back({
searchSorted(xs, rect.left),
searchSorted(xs, rect.right),
searchSorted(ys, rect.top),
searchSorted(ys, rect.bottom),
});
}
// 中线
dMidLines.reserve(midLines.size());
for (const auto& midLine : midLines)
{
dMidLines.push_back({searchSortedPoint(xs, ys, midLine.first), searchSortedPoint(xs, ys, midLine.second)});
}
using FloatMat = std::vector<std::vector>;
// 计算离散图所有边的权重
// hWeights[i][j] = 离散图上的节点(xs[i], ys[j])与(xs[i + 1], ys[j])之间的权重
// vWeights[i][j] = 离散图上的节点(xs[i], ys[j])与(xs[i], ys[j + 1])之间的权重
void initWeights(const std::vector& xs, const std::vector& ys, const DiscreteCanvas& dwsg,
const std::vector& dPathRects, const DiscreteMidLines& dMidLines, FloatMat& hWeights,
FloatMat& vWeights)
{
// 首先将权重设为画布上的距离
for (size_t i = 0; i < xs.size() - 1; ++i)
{
const float d = xs[i + 1] - xs[i];
for (size_t j = 0; j < ys.size(); ++j)
{
hWeights[i][j] = d;
}
}
code Copy code
for (size_t j = 0; j < ys.size() - 1; ++j)
{
const float d = ys[j + 1] - ys[j];
for (size_t i = 0; i < xs.size(); ++i)
{
vWeights[i][j] = d;
}
}
// 禁止路径经过离散矩形内部
// 由于离散矩形是原先的矩形扩大一圈余量得到的,允许路径与离散矩形的边缘重合
for (const auto& rect : dwsg.rects)
{
for (size_t i = rect.left; i < rect.right; ++i)
{
for (size_t j = rect.top + 1; j < rect.bottom; ++j)
{
hWeights[i][j] = FloatMax;
}
}
for (size_t i = rect.left + 1; i < rect.right; ++i)
{
for (size_t j = rect.top; j < rect.bottom; ++j)
{
vWeights[i][j] = FloatMax;
}
}
}
// 由于离散矩形是原先的矩形扩大一圈余量得到的,所求路径的起点与终点在离散矩形内部,现在把它们重新连接到离散矩形边缘
if (dwsg.startNode.y == dwsg.startNodeWithMargin.y) // 水平
{
const size_t begin = std::min(dwsg.startNode.x, dwsg.startNodeWithMargin.x);
const size_t end = std::max(dwsg.startNode.x, dwsg.startNodeWithMargin.x);
for (size_t i = begin; i < end; ++i)
{
hWeights[i][dwsg.startNode.y] = xs[i + 1] - xs[i];
}
}
else // 竖直
{
const size_t begin = std::min(dwsg.startNode.y, dwsg.startNodeWithMargin.y);
const size_t end = std::max(dwsg.startNode.y, dwsg.startNodeWithMargin.y);
for (size_t j = begin; j < end; ++j)
{
vWeights[dwsg.startNode.x][j] = ys[j + 1] - ys[j];
}
}
if (dwsg.endNode.y == dwsg.endNodeWithMargin.y) // 水平
{
const size_t begin = std::min(dwsg.endNode.x, dwsg.endNodeWithMargin.x);
const size_t end = std::max(dwsg.endNode.x, dwsg.endNodeWithMargin.x);
for (size_t i = begin; i < end; ++i)
{
hWeights[i][dwsg.endNode.y] = xs[i + 1] - xs[i];
}
}
else // 竖直
{
const size_t begin = std::min(dwsg.endNode.y, dwsg.endNodeWithMargin.y);
const size_t end = std::max(dwsg.endNode.y, dwsg.endNodeWithMargin.y);
for (size_t j = begin; j < end; ++j)
{
vWeights[dwsg.endNode.x][j] = ys[j + 1] - ys[j];
}
}
// 对每条已有路径,根据路径重叠惩罚系数增大权重
// 若已有多条路径重叠,则将惩罚系数再次相乘
for (const auto& rect : dPathRects)
{
for (size_t i = rect.left; i < rect.right; ++i)
{
for (size_t j = rect.top + 1; j < rect.bottom; ++j)
{
if (hWeights[i][j] != FloatMax)
{
hWeights[i][j] *= PathOverlapPenalty;
}
}
}
for (size_t i = rect.left + 1; i < rect.right; ++i)
{
for (size_t j = rect.top; j < rect.bottom; ++j)
{
if (vWeights[i][j] != FloatMax)
{
vWeights[i][j] *= PathOverlapPenalty;
}
}
}
}
// 对每条中线,根据路径居中奖励系数减小权重
// 中线是按没有扩大一圈余量的矩形计算的,所以可能经过离散矩形内部
for (const auto& midLine : dMidLines)
{
if (midLine.first.y == midLine.second.y) // 水平
{
for (size_t i = midLine.first.x; i < midLine.second.x; ++i)
{
if (hWeights[i][midLine.first.y] != FloatMax)
{
hWeights[i][midLine.first.y] *= PathMiddleReward;
}
}
}
else // 竖直
{
for (size_t j = midLine.first.y; j < midLine.second.y; ++j)
{
if (vWeights[midLine.first.x][j] != FloatMax)
{
vWeights[midLine.first.x][j] *= PathMiddleReward;
}
}
}
}
// 离散图的数据结构
// graph[u][k] = {v, weight, isHorizontal}
// k是从节点u到v的边(在从节点u出发的所有边中的编号),weight是这条边的权重,isHorizontal记录这条边是否为水平
struct GraphEdge
{
size_t nodeIdx;
float weight;
bool isHorizontal;
code Copy code
// 在优先队列中比较顺序时,只比较权重
bool operator>(const GraphEdge& other) const
{
return weight > other.weight;
}
using Graph = std::vector<std::vector>;
// 生成离散图,每个节点到上下左右各有一条边,除非被模块挡住
void initGraph(const FloatMat& hWeights, const FloatMat& vWeights, Graph& graph)
{
const size_t xSize = vWeights.size();
const size_t ySize = hWeights[0].size();
code Copy code
// 将离散图上的二维坐标转换为节点编号
const auto idx = [ySize](const size_t i, const size_t j) { return i * ySize + j; };
for (size_t i = 0; i < xSize - 1; ++i)
{
for (size_t j = 0; j < ySize; ++j)
{
if (hWeights[i][j] != FloatMax)
{
graph[idx(i, j)].push_back({idx(i + 1, j), hWeights[i][j], true});
graph[idx(i + 1, j)].push_back({idx(i, j), hWeights[i][j], true});
}
}
}
for (size_t i = 0; i < xSize; ++i)
{
for (size_t j = 0; j < ySize - 1; ++j)
{
if (vWeights[i][j] != FloatMax)
{
graph[idx(i, j)].push_back({idx(i, j + 1), vWeights[i][j], false});
graph[idx(i, j + 1)].push_back({idx(i, j), vWeights[i][j], false});
}
}
}
// 用A*算法求最短路径
void findShortestPath(const std::vector& xs, const std::vector& ys, const Graph& graph,
const size_t startIdx, const bool startIsHorizontal, const size_t endIdx,
const bool endIsHorizontal, std::vector<size_t>& outNodeIdxs)
{
const size_t numNodes = graph.size();
const size_t ySize = ys.size();
code Copy code
const auto heuristic = [&](size_t nodeIdx)
{ return std::abs(xs[nodeIdx / ySize] - xs[endIdx / ySize]) + std::abs(ys[nodeIdx % ySize] - ys[endIdx % ySize]); };
std::vector<std::vector<float>> dist(numNodes, std::vector<float>(2, FloatMax));
std::vector<std::vector<std::pair<size_t, bool>>> parent(
numNodes, std::vector<std::pair<size_t, bool>>(2, {numNodes, false}));
std::priority_queue<GraphEdge, std::vector<GraphEdge>, std::greater<GraphEdge>> pq;
dist[startIdx][startIsHorizontal] = 0.0f;
pq.push({startIdx, heuristic(startIdx), startIsHorizontal});
while (!pq.empty())
{
const GraphEdge curr = pq.top();
pq.pop();
const size_t u = curr.nodeIdx;
if (u == endIdx && curr.isHorizontal == endIsHorizontal)
{
break;
}
if (curr.weight - heuristic(u) > dist[u][curr.isHorizontal] + FloatTol)
{
continue;
}
for (const auto& next : graph[u])
{
const size_t v = next.nodeIdx;
float weight = next.weight;
if (curr.isHorizontal != next.isHorizontal)
{
weight += BendPenalty;
}
const float nextDist = dist[u][curr.isHorizontal] + weight;
if (nextDist < dist[v][next.isHorizontal])
{
dist[v][next.isHorizontal] = nextDist;
parent[v][next.isHorizontal] = {u, curr.isHorizontal};
pq.push({v, nextDist + heuristic(v), next.isHorizontal});
}
}
}
if (dist[endIdx][endIsHorizontal] != FloatMax)
{
size_t v = endIdx;
bool h = endIsHorizontal;
while (v != numNodes)
{
outNodeIdxs.push_back(v);
const std::pair<size_t, bool> p = parent[v][h];
v = p.first;
h = p.second;
}
}
// 以上输出的节点顺序是从终点到起点,现在将其反转为从起点到终点
std::reverse(outNodeIdxs.begin(), outNodeIdxs.end());
// 去除共线的点
void removeCollinear(Path& path)
{
if (path.size() <= 2)
{
return;
}
code Copy code
for (size_t i = 1; i < path.size() - 1;)
{
if (collinear(path[i - 1], path[i], path[i + 1]))
{
path.erase(path.begin() + i);
}
else
{
++i;
}
}
// 计算所求路径
void routeNodes(const CompAndCanvasInfo& wsg, Path& path)
{
// 生成已有路径周围扩大一圈余量的矩形
std::vector pathRects;
initPathRects(wsg, pathRects);
code Copy code
// 求所有中线
MidLines midLines;
findMidLines(wsg, midLines);
// 统计出现在离散图上的所有画布坐标
std::vector<float> xs, ys;
initXsYs(wsg, pathRects, midLines, xs, ys);
// 将画布坐标转换为离散图坐标
DiscreteCanvas dwsg;
std::vector<DiscreteRect> dPathRects;
DiscreteMidLines dMidLines;
initDiscreteCanvas(wsg, pathRects, midLines, xs, ys, dwsg, dPathRects, dMidLines);
// 计算离散图所有边的权重
FloatMat hWeights(xs.size() - 1, std::vector<float>(ys.size()));
FloatMat vWeights(xs.size(), std::vector<float>(ys.size() - 1));
initWeights(xs, ys, dwsg, dPathRects, dMidLines, hWeights, vWeights);
// 生成离散图
Graph graph(xs.size() * ys.size());
initGraph(hWeights, vWeights, graph);
// 将离散图上的二维坐标转换为节点编号
const size_t ySize = ys.size();
const auto idx = [ySize](const size_t i, const size_t j) { return i * ySize + j; };
// 求最短路径
const size_t startIdx = idx(dwsg.startNode.x, dwsg.startNode.y);
const size_t endIdx = idx(dwsg.endNode.x, dwsg.endNode.y);
const bool startIsHorizontal = (dwsg.startDirection == Direction::West || dwsg.startDirection == Direction::East);
const bool endIsHorizontal = (dwsg.endDirection == Direction::West || dwsg.endDirection == Direction::East);
std::vector<size_t> nodeIdxs;
findShortestPath(xs, ys, graph, startIdx, startIsHorizontal, endIdx, endIsHorizontal, nodeIdxs);
// 将节点编号转换为画布上的二维坐标
path.reserve(nodeIdxs.size());
for (const auto nodeIdx : nodeIdxs)
{
path.push_back({xs[nodeIdx / ySize], ys[nodeIdx % ySize]});
}
// 去除共线的点
removeCollinear(path);
} // namespace routenodes
--- End of content ---
wsg.paths
The term pathRects.size() * 3 is likely a typo. For each rectangle in pathRects, two x-coordinates (left, right) and two y-coordinates (top, bottom) are added. Therefore, the term should probably be pathRects.size() * 2. While this doesn't affect the correctness of the final path, correcting the capacity calculation would make the memory management more efficient. Other than this, the logic in the file seems sound.
Copy first response
Dataset releases Not yet included in a published release.