Max*_*r88 9 c++ algorithm debugging
我在阅读这篇文章后实现了路径简化算法:
http://losingfight.com/blog/2011/05/30/how-to-implement-a-vector-brush/
它对我来说非常适合为我的游戏生成优化的关卡几何体.但是,我现在正在使用它来清理一个*寻路路径,它有一个奇怪的边缘情况,惨遭失败.
这是它工作的截图 - 优化从红色圆圈到蓝色圆圈的路径.淡绿色线是a*输出,浅白色线是优化路径.

这是一个失败的屏幕截图:

这是我的代码.我将文章中的ObjC代码改编为c ++
注意:vec2fvec是a std::vector< vec2<float> >,'real'只是一个typedef'd float.
void rdpSimplify( const vec2fvec &in, vec2fvec &out, real threshold )
{
if ( in.size() <= 2 )
{
out = in;
return;
}
//
// Find the vertex farthest from the line defined by the start and and of the path
//
real maxDist = 0;
size_t maxDistIndex = 0;
LineSegment line( in.front(), in.back() );
for ( vec2fvec::const_iterator it(in.begin()),end(in.end()); it != end; ++it )
{
real dist = line.distance( *it );
if ( dist > maxDist )
{
maxDist = dist;
maxDistIndex = it - in.begin();
}
}
//
// If the farhtest vertex is greater than our threshold, we need to
// partition and optimize left and right separately
//
if ( maxDist > threshold )
{
//
// Partition 'in' into left and right subvectors, and optimize them
//
vec2fvec left( maxDistIndex+1 ),
right( in.size() - maxDistIndex ),
leftSimplified,
rightSimplified;
std::copy( in.begin(), in.begin() + maxDistIndex + 1, left.begin() );
std::copy( in.begin() + maxDistIndex, in.end(), right.begin() );
rdpSimplify(left, leftSimplified, threshold );
rdpSimplify(right, rightSimplified, threshold );
//
// Stitch optimized left and right into 'out'
//
out.resize( leftSimplified.size() + rightSimplified.size() - 1 );
std::copy( leftSimplified.begin(), leftSimplified.end(), out.begin());
std::copy( rightSimplified.begin() + 1, rightSimplified.end(), out.begin() + leftSimplified.size() );
}
else
{
out.push_back( line.a );
out.push_back( line.b );
}
}
Run Code Online (Sandbox Code Playgroud)
我真的不知道出了什么问题.我的蜘蛛侠意识说它是在std :: copy调用中......我必须在某些情况下复制垃圾.
编辑:我已经重写了算法,删除任何使用迭代器和std :: copy等.它仍然以完全相同的方式失败.
void rdpSimplify( const vec2fvec &in, vec2fvec &out, real threshold )
{
if ( in.size() <= 2 )
{
out = in;
return;
}
//
// Find the vertex farthest from the line defined by the start and and of the path
//
real maxDist = 0;
size_t maxDistIndex = 0;
LineSegment line( in.front(), in.back() );
for ( size_t i = 0, N = in.size(); i < N; i++ )
{
real dist = line.distance( in[i] );
if ( dist > maxDist )
{
maxDist = dist;
maxDistIndex = i;
}
}
//
// If the farthest vertex is greater than our threshold, we need to
// partition and optimize left and right separately
//
if ( maxDist > threshold )
{
//
// Partition 'in' into left and right subvectors, and optimize them
//
vec2fvec left, right, leftSimplified, rightSimplified;
for ( size_t i = 0; i < maxDistIndex + 1; i++ ) left.push_back( in[i] );
for ( size_t i = maxDistIndex; i < in.size(); i++ ) right.push_back( in[i] );
rdpSimplify(left, leftSimplified, threshold );
rdpSimplify(right, rightSimplified, threshold );
//
// Stitch optimized left and right into 'out'
//
out.clear();
for ( size_t i = 0, N = leftSimplified.size(); i < N; i++ ) out.push_back(leftSimplified[i]);
for ( size_t i = 1, N = rightSimplified.size(); i < N; i++ ) out.push_back( rightSimplified[i] );
}
else
{
out.push_back( line.a );
out.push_back( line.b );
}
}
Run Code Online (Sandbox Code Playgroud)
我在你的代码中找不到任何错误。
一些值得尝试的事情:
maxDist失败情况下的内容。它应该非常低,但如果它很高,那么您就知道您的线段距离代码有问题。只要稍微调查一下,应该不会花太长时间就能找到问题的原因。几分钟后,盯着代码进行调试是一种非常糟糕的方法。