【问题标题】:Ramer-Douglas-Peucker path simplification algorithmRamer-Douglas-Peucker 路径简化算法
【发布时间】:2011-06-26 10:37:28
【问题描述】:

在这里阅读文章后我实现了路径简化算法:

http://losingfight.com/blog/2011/05/30/how-to-implement-a-vector-brush/

它非常适合为我的游戏生成优化的关卡几何图形。但是,我现在用它来清理 * 寻路路径,它有一个奇怪的边缘情况,失败得很惨。

这是它工作的屏幕截图 - 优化从红色圆圈到蓝色圆圈的路径。淡绿色线为a*输出,较浅的白色线为优化路径。

这是失败的截图:

这是我的代码。我将文章中的 ObjC 代码改编为 c++

注意:vec2fvecstd::vector< vec2<float> >,而 'real' 只是一个类型定义的浮点数。

       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 );
    }
}

我真的不知道出了什么问题。我的蜘蛛侠感觉说它在 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 );
    }
}

【问题讨论】:

    标签: c++ algorithm debugging


    【解决方案1】:

    我在您的代码中找不到任何错误。

    一些尝试:

    • 添加一些调试打印语句以检查失败情况下的maxDist 是什么。它应该很低,但如果它很高,那么你就知道你的线段距离代码有问题。
    • 检查您看到的路径是否与您的算法返回的路径实际匹配。如果不是,那么您的路径渲染可能有问题?当路径只有两个点时可能是一个错误?
    • 通过在算法开始时打印出所有坐标来检查您的输入路径是否符合您的预期。

    只要稍微调查一下,就不会花太长时间找到问题的原因。几分钟后,盯着代码是一种非常糟糕的调试方式。

    【讨论】:

      猜你喜欢
      • 2018-11-18
      • 2021-05-16
      • 2016-05-19
      • 1970-01-01
      • 2017-01-24
      • 1970-01-01
      • 2018-08-27
      • 2015-03-08
      • 1970-01-01
      相关资源
      最近更新 更多