[Function Response: read_many_files]--- C:\routenodes\src\debug.hpp ---
// 用于调试的辅助函数
#ifndef INCLUDE_DEBUG_HPP_
#define INCLUDE_DEBUG_HPP_
#include
#include
#include
#include
#include
#include
namespace routenodes
{
template
void printIntVec(const std::string& name, const std::vector& v)
{
std::cout << name << " ";
code Copy code
for (size_t i = 0; i < v.size(); ++i)
{
std::cout << v[i];
if (i < v.size() - 1)
{
std::cout << " ";
}
}
std::cout << std::endl;
}
template
void printFloatVec(const std::string& name, const std::vector& v)
{
std::cout << name << " ";
code Copy code
for (size_t i = 0; i < v.size(); ++i)
{
if (v[i] == std::numeric_limits<T>::max())
{
std::cout << std::setw(8) << "X";
}
else
{
std::cout << std::setw(8) << std::fixed << std::setprecision(2) << v[i];
}
if (i < v.size() - 1)
{
std::cout << " ";
}
}
std::cout << std::endl;
}
void printNodeIdxs(const size_t xSize, const size_t ySize)
{
for (size_t j = 0; j < ySize; ++j)
{
for (size_t i = 0; i < xSize; ++i)
{
std::cout << std::setw(2) << i * ySize + j << " ";
}
code Copy code
std::cout << std::endl;
}
}
template
void printDiscreteCanvas(const T& dwsg)
{
for (const auto& rect : dwsg.rects)
{
std::cout << "rect " << rect.left << " " << rect.right << " " << rect.top << " " << rect.bottom << std::endl;
}
code Copy code
std::cout << "startDirection " << static_cast<size_t>(dwsg.startDirection) << std::endl;
std::cout << "endDirection " << static_cast<size_t>(dwsg.endDirection) << std::endl;
}
template
void printGraph(const T& graph)
{
for (size_t i = 0; i < graph.size(); ++i)
{
for (const auto& edge : graph[i])
{
std::cout << std::setw(2) << i << " " << std::setw(2) << edge.nodeIdx << " " << std::setw(8) << std::fixed
<< std::setprecision(2) << edge.weight << " " << edge.isHorizontal << std::endl;
}
}
}
} // namespace routenodes
#endif // INCLUDE_DEBUG_HPP_
--- C:\routenodes\src\routenodes.cpp ---
// 计算所求路径的核心代码
#include "routenodes.hpp"
#include
#include
#include
#include
#include
namespace routenodes
{
// 拐点惩罚系数
const float BendPenalty = 100.0f;
// 路径重叠惩罚系数
const float PathOverlapPenalty = 2.0f;
// 统计出现在离散图上的所有画布坐标
void initXsYs(const CompAndCanvasInfo& wsg, std::vector& xs, std::vector& ys)
{
// 模块扩大一圈余量后的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);
}
code Copy code
// 已有路径的每个拐点扩大一圈余量后的x y坐标可能作为拐点
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 - DefaultMargin);
xs.push_back(point.x);
xs.push_back(point.x + DefaultMargin);
ys.push_back(point.y - DefaultMargin);
ys.push_back(point.y);
ys.push_back(point.y + DefaultMargin);
}
}
// 所求路径的起点与终点
xs.push_back(wsg.startNode.x);
ys.push_back(wsg.startNode.y);
xs.push_back(wsg.endNode.x);
ys.push_back(wsg.endNode.y);
// 排序并去重
sortAndDedup(xs);
sortAndDedup(ys);
}
// 将画布坐标转换为离散图坐标
void initDiscreteCanvas(const CompAndCanvasInfo& wsg, const std::vector& xs, const std::vector& ys,
DiscreteCanvas& dwsg)
{
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
for (const auto& path : wsg.paths)
{
DiscretePath dpath;
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);
dwsg.startDirection = wsg.startNode.direction;
dwsg.endDirection = wsg.endNode.direction;
}
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,
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;
}
}
}
// 对已有路径,根据路径重叠惩罚系数增大权重
// 若已有多条路径重叠,则将惩罚系数再次相乘
for (const auto& path : dwsg.paths)
{
for (size_t k = 0; k < path.size() - 1; ++k)
{
const auto& point = path[k];
const auto& next = path[k + 1];
if (point.y == next.y) // 水平
{
const size_t begin = std::min(point.x, next.x);
const size_t end = std::max(point.x, next.x);
for (size_t i = begin; i < end; ++i)
{
hWeights[i][point.y] *= PathOverlapPenalty;
}
}
else // 竖直
{
const size_t begin = std::min(point.y, next.y);
const size_t end = std::max(point.y, next.y);
for (size_t j = begin; j < end; ++j)
{
vWeights[point.x][j] *= PathOverlapPenalty;
}
}
}
}
// 由于离散矩形是原先的矩形扩大一圈余量得到的,所求路径的起点与终点在离散矩形内部,现在把它们重新连接到离散矩形边缘
// 起点的方向指向模块外
switch (dwsg.startDirection)
{
case Direction::North:
vWeights[dwsg.startNode.x][dwsg.startNode.y - 1] = ys[dwsg.startNode.y] - ys[dwsg.startNode.y - 1];
break;
case Direction::South:
vWeights[dwsg.startNode.x][dwsg.startNode.y] = ys[dwsg.startNode.y + 1] - ys[dwsg.startNode.y];
break;
case Direction::West:
hWeights[dwsg.startNode.x - 1][dwsg.startNode.y] = xs[dwsg.startNode.x] - xs[dwsg.startNode.x - 1];
break;
case Direction::East:
hWeights[dwsg.startNode.x][dwsg.startNode.y] = xs[dwsg.startNode.x + 1] - xs[dwsg.startNode.x];
break;
default:
throw std::runtime_error("未设置所求路径起点的方向");
}
// 终点的方向指向模块内
switch (dwsg.endDirection)
{
case Direction::North:
vWeights[dwsg.endNode.x][dwsg.endNode.y] = ys[dwsg.endNode.y + 1] - ys[dwsg.endNode.y];
break;
case Direction::South:
vWeights[dwsg.endNode.x][dwsg.endNode.y - 1] = ys[dwsg.endNode.y] - ys[dwsg.endNode.y - 1];
break;
case Direction::West:
hWeights[dwsg.endNode.x][dwsg.endNode.y] = xs[dwsg.endNode.x + 1] - xs[dwsg.endNode.x];
break;
case Direction::East:
hWeights[dwsg.endNode.x - 1][dwsg.endNode.y] = xs[dwsg.endNode.x] - xs[dwsg.endNode.x - 1];
break;
default:
throw std::runtime_error("未设置所求路径终点的方向");
}
}
// 离散图的数据结构
// graph[u][k] = {v, weight, isHorizontal}
// k是从节点u到v的边(在从节点u出发的所有边中的编号),weight是这条边的权重,isHorizontal记录这条边是否为水平
struct GraphEdge
{
size_t nodeIdx;
float weight;
bool isHorizontal;
};
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});
}
}
}
}
// 对两条边比较哪个权重更大
struct CompareEdgeWeightGreater
{
bool operator()(const GraphEdge& a, const GraphEdge& b) const
{
return a.weight > b.weight;
}
};
// Dijkstra算法求最短路径
void getShortestPath(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();
std::vector<std::vector> dist(numNodes, std::vector(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, CompareEdgeWeightGreater> pq;
code Copy code
dist[startIdx][startIsHorizontal] = 0.0f;
pq.push({startIdx, 0.0f, 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 > dist[u][curr.isHorizontal])
{
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, 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)
{
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 xs, ys;
initXsYs(wsg, xs, ys);
code Copy code
// 将画布坐标转换为离散图坐标
DiscreteCanvas dwsg;
initDiscreteCanvas(wsg, xs, ys, dwsg);
// 计算离散图所有边的权重
FloatMat hWeights(xs.size() - 1, std::vector<float>(ys.size()));
FloatMat vWeights(xs.size(), std::vector<float>(ys.size() - 1));
initWeights(xs, ys, dwsg, 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;
getShortestPath(graph, startIdx, startIsHorizontal, endIdx, endIsHorizontal, nodeIdxs);
// 将节点编号转换为画布上的二维坐标
for (const auto nodeIdx : nodeIdxs)
{
path.push_back({xs[nodeIdx / ySize], ys[nodeIdx % ySize]});
}
// 去除共线的点
removeCollinear(path);
}
} // namespace routenodes
--- C:\routenodes\src\routenodes.hpp ---
// 主要数据结构与函数接口
#ifndef INCLUDE_ROUTENODES_HPP_
#define INCLUDE_ROUTENODES_HPP_
#include
#include
#include
#include
#ifdef ROUTENODES_SHARED
// 共享库导出符号
#if defined(_WIN32) || defined(WIN32 ) || defined(CYGWIN )
#ifdef ROUTENODES_EXPORTS
#define ROUTENODES_API __declspec(dllexport)
#else
#define ROUTENODES_API __declspec(dllimport)
#endif
#elif (defined(GNUC ) && GNUC >= 4) || defined(clang )
#define ROUTENODES_API attribute ((visibility("default")))
#else // 其他编译器
#define ROUTENODES_API
#endif
#else // not ROUTENODES_SHARED
#define ROUTENODES_API
#endif
namespace routenodes
{
const float FloatMax = std::numeric_limits::max();
const float FloatTol = 1e-3f;
// 路径与模块边缘之间的余量
const float DefaultMargin = 10.0f;
// 路径方向
enum class Direction
{
None,
North,
South,
West,
East,
};
// 路径上的点
struct CustomPoint
{
float x;
float y;
// 路径方向
Direction direction = Direction::None;
};
// 路径
using Path = std::vector;
// 模块信息
struct CompPartInfo
{
// 模块名称,用于在可视化时显示
std::string blockType;
// 模块宽
float width;
// 模块高
float height;
// 模块位置,矩形的左上角坐标
CustomPoint pos;
// 路径与模块边缘之间的余量
float margin = DefaultMargin;
};
// 画布信息
struct CompAndCanvasInfo
{
// 画布宽
float graphicSceneWidth;
// 画布高
float graphicSceneHeight;
code Copy code
// 画布中所有的模块
std::vector<CompPartInfo> compPartInfos;
// 画布中所有的路径
std::vector<Path> paths;
// 所求路径的起点
CustomPoint startNode;
// 所求路径的终点
CustomPoint endNode;
};
// 几何上的矩形
struct Rect
{
float left;
float right;
float top;
float bottom;
code Copy code
// CompPartInfo可以转换为Rect以便于计算
Rect(const CompPartInfo& comp);
// 一条线段也可以视为面积为零的矩形
Rect(const CustomPoint& a, const CustomPoint& b);
};
// 离散化的点、路径、矩形、画布数据结构
struct DiscretePoint
{
size_t x;
size_t y;
};
using DiscretePath = std::vector;
struct DiscreteRect
{
size_t left;
size_t right;
size_t top;
size_t bottom;
};
struct DiscreteCanvas
{
std::vector rects;
std::vector paths;
DiscretePoint startNode;
DiscretePoint endNode;
Direction startDirection;
Direction endDirection;
};
// 两个浮点数是否在浮点误差内相等
bool isClose(const float a, const float b);
// 三点是否共线
bool collinear(const CustomPoint& a, const CustomPoint& b, const CustomPoint& c);
// 对浮点数组排序并去重
void sortAndDedup(std::vector& v);
// 在排好序的浮点数组中用二分法搜索给定数值的编号
size_t searchSorted(const std::vector& v, const float target);
// 在排好序的x y坐标中搜索给定点
DiscretePoint searchSortedPoint(const std::vector& xs, const std::vector& ys, const CustomPoint& p);
// 检验画布中已有的模块与路径是否合理,并在所有路径的起点与终点设置路径方向
extern "C" ROUTENODES_API bool validateCanvas(CompAndCanvasInfo& wsg);
// 计算所求路径
extern "C" ROUTENODES_API void routeNodes(const CompAndCanvasInfo& wsg, Path& path);
} // namespace routenodes
#endif // INCLUDE_ROUTENODES_HPP_
--- C:\routenodes\src\routenodescli.cpp ---
// 用于测试的命令行应用。读取一个JSON文件,计算所求路径,输出到新的JSON文件
#include
#include
#include
#include "routenodes.hpp"
#include "serialize.hpp"
int main(int argc, char* argv[])
{
if (argc != 3)
{
std::cerr << "Usage: routenodescli in.json out.json" << std::endl;
return 1;
}
code Copy code
const std::string in_filename(argv[1]);
const std::string out_filename(argv[2]);
// 读取JSON文件
std::ifstream ifs(in_filename);
if (!ifs.is_open())
{
// 中文输出需要在UTF-8环境下才能正常显示
std::cerr << "无法读取文件" << in_filename << std::endl;
return 1;
}
json j;
ifs >> j;
routenodes::CompAndCanvasInfo canvas;
j.get_to(canvas);
// 检验画布中已有的模块与路径是否合理
const bool ok = routenodes::validateCanvas(canvas);
if (!ok)
{
// validateCanvas已经打印出错误信息
return 1;
}
// 计算所求路径,添加到画布
routenodes::Path path;
routenodes::routeNodes(canvas, path);
canvas.paths.push_back(path);
// 输出JSON文件
std::ofstream ofs(out_filename);
if (!ofs.is_open())
{
std::cerr << "无法写入文件" << out_filename << std::endl;
return 1;
}
const json out_j = canvas;
ofs << out_j.dump(2); // 2空格缩进
return 0;
}
--- C:\routenodes\src\serialize.cpp ---
// JSON序列化
#include "serialize.hpp"
#include
#include
using json = nlohmann::ordered_json;
namespace routenodes
{
// CustomPoint序列化
void to_json(json& j, const CustomPoint& p)
{
std::ostringstream ss;
ss << p.x << "," << p.y;
j = ss.str();
}
void from_json(const json& j, CustomPoint& p)
{
if (j.is_string())
{
// 解析"x,y"
float x, y;
char comma;
std::istringstream ss(j.getstd::string ());
ss >> x >> comma >> y;
p.x = x;
p.y = y;
}
else
{
// 不但读取x y,也读取direction
j.at("x").get_to(p.x);
j.at("y").get_to(p.y);
j.at("direction").get_to(p.direction);
}
}
// CompPartInfo序列化
void to_json(json& j, const CompPartInfo& c)
{
if (!c.blockType.empty())
{
j["BlockType"] = c.blockType;
}
code Copy code
j["Width"] = c.width;
j["Height"] = c.height;
j["Position"] = c.pos;
}
void from_json(const json& j, CompPartInfo& c)
{
if (j.contains("BlockType"))
{
j.at("BlockType").get_to(c.blockType);
}
code Copy code
j.at("Width").get_to(c.width);
j.at("Height").get_to(c.height);
j.at("Position").get_to(c.pos);
}
// CompAndCanvasInfo序列化
void to_json(json& j, const CompAndCanvasInfo& c)
{
j["Canvas"] = {{"Width", c.graphicSceneWidth}, {"Height", c.graphicSceneHeight}};
j["Blocks"] = c.compPartInfos;
code Copy code
if (!c.paths.empty())
{
j["Lines"] = json::array();
for (const auto& path : c.paths)
{
j["Lines"].push_back(json{{"Line", path}});
}
}
j["StartNode"] = c.startNode;
j["EndNode"] = c.endNode;
}
void from_json(const json& j, CompAndCanvasInfo& c)
{
j.at("Canvas").at("Width").get_to(c.graphicSceneWidth);
j.at("Canvas").at("Height").get_to(c.graphicSceneHeight);
j.at("Blocks").get_to(c.compPartInfos);
code Copy code
if (j.contains("Lines"))
{
for (const auto& line : j.at("Lines"))
{
c.paths.push_back(line.at("Line"));
}
}
j.at("StartNode").get_to(c.startNode);
j.at("EndNode").get_to(c.endNode);
}
} // namespace routenodes
--- C:\routenodes\src\serialize.hpp ---
// JSON序列化
#ifndef INCLUDE_SERIALIZE_HPP_
#define INCLUDE_SERIALIZE_HPP_
#include "routenodes.hpp"
#include "third_party/nlohmann/json.hpp"
using json = nlohmann::ordered_json;
namespace routenodes
{
// CustomPoint序列化
void to_json(json& j, const CustomPoint& p);
void from_json(const json& j, CustomPoint& p);
// CompPartInfo序列化
void to_json(json& j, const CompPartInfo& c);
void from_json(const json& j, CompPartInfo& c);
// CompAndCanvasInfo序列化
void to_json(json& j, const CompAndCanvasInfo& c);
void from_json(const json& j, CompAndCanvasInfo& c);
} // namespace routenodes
#endif // INCLUDE_SERIALIZE_HPP_
--- C:\routenodes\src\utils.cpp ---
// 辅助函数
#include
#include
#include
#include
#include
#include "routenodes.hpp"
namespace routenodes
{
// CompPartInfo可以转换为Rect以便于计算
Rect::Rect(const CompPartInfo& comp)
{
left = comp.pos.x;
right = comp.pos.x + comp.width;
top = comp.pos.y;
bottom = comp.pos.y + comp.height;
}
// 一条线段也可以视为面积为零的矩形
Rect::Rect(const CustomPoint& a, const CustomPoint& b)
{
left = std::min(a.x, b.x);
right = std::max(a.x, b.x);
top = std::min(a.y, b.y);
bottom = std::max(a.y, b.y);
}
// 两个浮点数是否在浮点误差内相等
bool isClose(const float a, const float b)
{
return std::abs(a - b) < FloatTol;
}
// 三点是否共线
bool collinear(const CustomPoint& a, const CustomPoint& b, const CustomPoint& c)
{
return isClose((b.x - a.x) * (c.y - b.y) - (b.y - a.y) * (c.x - b.x), 0.0f);
}
// 对浮点数组排序并去重
void sortAndDedup(std::vector& v)
{
std::sort(v.begin(), v.end());
const auto last = std::unique(v.begin(), v.end(), isClose);
v.erase(last, v.end());
}
// 在排好序的浮点数组中用二分法搜索给定数值的编号
// C++20有std::binary_search
size_t searchSorted(const std::vector& v, const float target)
{
const auto it = std::lower_bound(v.cbegin(), v.cend(), target);
if (it != v.cend() && isClose(*it, target))
{
return std::distance(v.cbegin(), it);
}
code Copy code
if (it != v.cbegin())
{
const auto prev_it = std::prev(it);
if (isClose(*prev_it, target))
{
return std::distance(v.cbegin(), prev_it);
}
}
throw std::runtime_error("找不到对应的浮点数值");
}
// 在排好序的x y坐标中搜索给定点
DiscretePoint searchSortedPoint(const std::vector& xs, const std::vector& ys, const CustomPoint& p)
{
return {searchSorted(xs, p.x), searchSorted(ys, p.y)};
}
} // namespace routenodes
--- C:\routenodes\src\validate.cpp ---
// 检验画布中已有的模块与路径是否合理
#include
#include
#include "routenodes.hpp"
namespace routenodes
{
// 两个矩形的距离是否小于余量
// 当模块和路径很多时,可以用四叉树提高碰撞检测的效率,目前未实现
bool rectOverlap(const Rect& a, const Rect& b, const float margin)
{
return !(a.right + margin < b.left || b.right + margin < a.left || a.bottom + margin < b.top ||
b.bottom + margin < a.top);
}
// 两个模块的距离是否小于两者余量之和
bool compOverlap(const CompPartInfo& a, const CompPartInfo& b)
{
return rectOverlap(Rect(a), Rect(b), a.margin + b.margin);
}
// 线段与模块的距离是否小于余量
// 已知线段一定是水平或竖直
bool lineCompOverlap(const CustomPoint& a, const CustomPoint& b, const CompPartInfo& c)
{
return rectOverlap(Rect(a, b), Rect(c), c.margin - FloatTol);
}
// 获取相反方向
Direction reverseDirection(const Direction d)
{
switch (d)
{
case Direction::North:
return Direction::South;
case Direction::South:
return Direction::North;
case Direction::West:
return Direction::East;
case Direction::East:
return Direction::West;
default:
return d;
}
}
// 若该点在任一模块的边缘上,则返回这个模块的编号,并将该点的路径方向设为垂直于边缘向外
// 否则返回compPartInfos.size()
// 不考虑刚好在角上的情况
// 若用来检验终点,之后需要将路径方向设为相反
size_t validateStartNode(CustomPoint& point, const std::vector& compPartInfos)
{
for (size_t i = 0; i < compPartInfos.size(); ++i)
{
const auto& comp = compPartInfos[i];
if (point.x > comp.pos.x && point.x < comp.pos.x + comp.width)
{
if (isClose(point.y, comp.pos.y))
{
point.direction = Direction::North;
return i;
}
else if (isClose(point.y, comp.pos.y + comp.height))
{
point.direction = Direction::South;
return i;
}
}
else if (point.y > comp.pos.y && point.y < comp.pos.y + comp.height)
{
if (isClose(point.x, comp.pos.x))
{
point.direction = Direction::West;
return i;
}
else if (isClose(point.x, comp.pos.x + comp.width))
{
point.direction = Direction::East;
return i;
}
}
}
code Copy code
return compPartInfos.size();
}
// 检验从a到b的线段方向是否与a.direction一致
// 已知线段一定是水平或竖直
bool validateDirection(const CustomPoint& a, const CustomPoint& b)
{
switch (a.direction)
{
case Direction::North:
return b.y < a.y;
case Direction::South:
return b.y > a.y;
case Direction::West:
return b.x < a.x;
case Direction::East:
return b.x > a.x;
default:
return true;
}
}
// 检验画布中已有的模块与路径是否合理,并在所有路径的起点与终点设置路径方向
bool validateCanvas(CompAndCanvasInfo& wsg)
{
bool ok = true;
}
} // namespace routenodes
--- End of content ---