std::vector<std::pair<float, float>> YAstar::simplifyPath(std::vector<std::pair<float, float>>& path, float threshold){
// DP 简化路径
int index0=0, index1=path.size()-1;
auto noStart = this->simplifyPathDP(path, index0, index1, threshold);
noStart.insert(noStart.begin(), path.front());
return noStart;
}请问%M%处应该填写什么?
递归调用丢失了关键点
当路径不能被简化时,您需要保留 path[maxIndex] 这个点。您的递归调用是:
auto path0 = this->simplifyPathDP(path, index0, maxIndex, threshold);
auto path1 = this->simplifyPathDP(path, maxIndex + 1, index1, threshold); // 问题在这里
path0 的结果会包含 maxIndex(或者它内部的关键点)。但 path1 从 maxIndex + 1 开始,这意味着 path[maxIndex] 这个点本身被丢弃了,没有加入到最终路径中。正确的做法应该是让第二段递归从 maxIndex 开始。
std::vector<std::pair<float, float>> YAstar::simplifyPathDP(std::vector<std::pair<float, float>>& path, int index0, int index1, float& threshold) {
// DP 简化路径
if (index1 - index0 <= 1) {
std::vector<std::pair<float, float>> oppath;
// 修正1:返回终点以保持逻辑一致性
oppath.emplace_back(path[index1]);
return oppath;
}
bool isInObstacle = lineInObsticle(path[index0], path[index1]);
float maxDistance = 0;
int maxIndex = index0;
std::vector<float> distances(index1 - index0 - 1);
std::transform(std::execution::seq, path.begin() + index0 + 1, path.begin() + index1, distances.begin(), [&](auto& p) {
return distanceFromLine(path[index0].first, path[index0].second, path[index1].first, path[index1].second, p.first, p.second);
});
// 找到最大距离和对应的索引
auto maxIt = std::max_element(distances.begin(), distances.end());
maxDistance = *maxIt;
maxIndex = std::distance(distances.begin(), maxIt) + index0 + 1;
if (maxDistance > threshold || isInObstacle) {
// 如果不能简化,递归分割
auto path0 = this->simplifyPathDP(path, index0, maxIndex, threshold);
// 修正2:第二段递归应从 maxIndex 开始,以保留该关键点
auto path1 = this->simplifyPathDP(path, maxIndex, index1, threshold);
// 合并结果。path0 包含了 path...maxIndex 的简化结果
// path1 包含了 path[maxIndex]...path[index1] 的简化结果
// insert 会将 path1 的所有元素追加到 path0 的末尾
path0.insert(path0.end(), path1.begin(), path1.end());
return path0;
} else {
// 如果可以简化,只返回终点
std::vector<std::pair<float, float>> oppath;
oppath.emplace_back(path[index1]);
return oppath;
}
}
// simplifyPath 函数看起来是正确的,它正确地处理了路径的第一个点。
std::vector<std::pair<float, float>> YAstar::simplifyPath(std::vector<std::pair<float, float>>& path, float threshold) {
if (path.size() < 3) {
return path; // 如果路径点少于3个,无需简化
}
int index0 = 0, index1 = path.size() - 1;
// simplifyPathDP 返回的是从第一个点之后到最后一个点的所有关键点
auto simplified_suffix = this->simplifyPathDP(path, index0, index1, threshold);
// 将路径的原始起点加回到最前面
simplified_suffix.insert(simplified_suffix.begin(), path.front());
return simplified_suffix;
}
#include <vector>
#include <utility>
#include <numeric>
#include <algorithm>
#include <execution>
#include <iterator> // for std::distance
// 假设这些函数已在类 YAstar 中定义
class YAstar {
public:
// 假设这些辅助函数存在
bool lineInObsticle(const std::pair<float, float>& p1, const std::pair<float, float>& p2);
float distanceFromLine(float x1, float y1, float x2, float y2, float px, float py);
std::vector<std::pair<float, float>> simplifyPathDP(std::vector<std::pair<float, float>>& path, int index0, int index1, float& threshold);
std::vector<std::pair<float, float>> simplifyPath(std::vector<std::pair<float, float>>& path, float threshold);
};
// 修正后的 simplifyPathDP 函数
std::vector<std::pair<float, float>> YAstar::simplifyPathDP(std::vector<std::pair<float, float>>& path, int index0, int index1, float& threshold) {
// DP 简化路径
// %M% 处填写 1
if (index1 - index0 <= 1) {
std::vector<std::pair<float, float>> oppath;
// 修正 1: 基准情形应返回路径段的终点,保持逻辑一致性
oppath.emplace_back(path[index1]);
return oppath;
}
bool isInObstacle = lineInObsticle(path[index0], path[index1]);
float maxDistance = 0;
int maxIndex = index0;
// 中间点的数量
size_t intermediate_points_count = index1 - index0 - 1;
if (intermediate_points_count > 0) {
std::vector<float> distances(intermediate_points_count);
// 计算每个点到直线的距离
std::transform(std::execution::seq, path.begin() + index0 + 1, path.begin() + index1, distances.begin(), [&](const auto& p) {
return distanceFromLine(path[index0].first, path[index0].second, path[index1].first, path[index1].second, p.first, p.second);
});
// 修正 4: 更直接地获取最大距离和其索引
auto maxDistIt = std::max_element(distances.begin(), distances.end());
maxDistance = *maxDistIt;
maxIndex = std::distance(distances.begin(), maxDistIt) + index0 + 1;
}
if (maxDistance > threshold || isInObstacle) {
// 如果不能简化,递归处理
auto path0 = this->simplifyPathDP(path, index0, maxIndex, threshold);
// 修正 2: 第二段递归应从 maxIndex 开始,以保留该关键点
auto path1 = this->simplifyPathDP(path, maxIndex, index1, threshold);
// 合并两个子路径的结果
path0.insert(path0.end(), path1.begin(), path1.end());
return path0;
} else {
// 如果可以简化,则此段路径只保留终点
std::vector<std::pair<float, float>> oppath;
oppath.emplace_back(path[index1]);
return oppath;
}
}
// 修正后的 simplifyPath 函数
std::vector<std::pair<float, float>> YAstar::simplifyPath(std::vector<std::pair<float, float>>& path, float threshold) {
// 修正 3: 处理路径点数过少的边缘情况
if (path.size() < 3) {
return path;
}
// DP 简化路径
int index0 = 0, index1 = path.size() - 1;
// simplifyPathDP 返回的是从第一个点之后到最后一个点的所有关键点
auto simplified_suffix = this->simplifyPathDP(path, index0, index1, threshold);
// 将路径的原始起点加回到结果的最前面
simplified_suffix.insert(simplified_suffix.begin(), path.front());
return simplified_suffix;
}