3

我正在尝试使用 Boost::Polygon 逐步构建一组多边形。下面代码中的实时数据集取自我在使用 Boost::Polygon 的真实系统中找到的病态输入。

这是最低限度的复制代码(您需要在包含路径中使用 boost 开发标头):

#include <iostream>
#include <string>
#include <vector>

#include "boost/polygon/polygon.hpp"

using namespace std;
using namespace boost::polygon;
using namespace boost::polygon::operators;

/* Utility function to read a polygon from a string. This is ugly, but works for the purpose of this repro case */
polygon_data<float> readPoly(string str)
{
    vector<point_data<float> > result;

    size_t index = 0;
    while(index != string::npos)
    {
        size_t xend = str.find(" ", index);
        size_t yend = str.find(" ", xend + 1);

        float x = atof(str.substr(index, xend - index).c_str());
        float y = atof(str.substr(xend + 1, yend - xend + 1).c_str());
        result.push_back(point_data<float>(x, y));

        if(yend == string::npos)
            break;

        index = yend + 1;
    }

    return polygon_data<float>(result.begin(), result.end());
}

/* Utility function to dump a polygon set to cout, useful for visualizing with the python script */
void dumpPoly(vector<polygon_data<float> > polyset)
{
    for(vector<polygon_data<float> >::iterator it = polyset.begin(); it != polyset.end(); it++)
    {
        polygon_data<float> poly = *it;
        for(polygon_data<float>::iterator_type jt = poly.begin(); jt != poly.end(); jt++)
        {
            cout << (*jt).x() << " " << (*jt).y() << " ";
        }
        cout << endl;
    }
}


int main()
{
    std::vector<polygon_data<float> > data;

    // Construct the polygon set
    data += readPoly("-1309.77, 1323.99, -1324, 1309.76, -1324, -1309.76, -1309.77, -1323.99, -1240.23, -1323.99, -1226, -1309.76, -1226, 1309.76, -1240.23, 1323.99");
    data += readPoly("-1323.99, -1309.77, -1309.76, -1324, 1309.76, -1324, 1323.99, -1309.77, 1323.99, -1240.23, 1309.76, -1226, -1309.76, -1226, -1323.99, -1240.23");
    data += readPoly("1323.99, 1309.77, 1309.76, 1324, -1309.76, 1324, -1323.99, 1309.77, -1323.99, 1240.23, -1309.76, 1226, 1309.76, 1226, 1323.99, 1240.23");
    data += readPoly("-544.771, 686.49, -559, 672.261, -559, -544.761, -544.771, -558.99, -475.229, -558.99, -461, -544.761, -461, 672.261, -475.229, 686.49");
    data += readPoly("-558.99, -544.771, -544.761, -559, 672.261, -559, 686.49, -544.771, 686.49, -475.229, 672.261, -461, -544.761, -461, -558.99, -475.229");
    data += readPoly("686.49, 672.271, 672.261, 686.5, -544.761, 686.5, -558.99, 672.271, -558.99, 602.729, -544.761, 588.5, 672.261, 588.5, 686.49, 602.729");
    data += readPoly("-69.8842, -119.057, -49.7607, -119.057, 219.057, 149.76, 219.057, 169.884, 169.884, 219.057, 149.76, 219.057, -119.057, -49.7607, -119.057, -69.8842");
    data += readPoly("672.271, -558.99, 686.5, -544.761, 686.5, 672.261, 672.271, 686.49, 602.729, 686.49, 588.5, 672.261, 588.5, -544.761, 602.729, -558.99");
    data += readPoly("1309.77, -1323.99, 1324, -1309.76, 1324, 1309.76, 1309.77, 1323.99, 1240.23, 1323.99, 1226, 1309.76, 1226, -1309.76, 1240.23, -1323.99");

    /*

    This alternative dataset shows that boost.polygon can handle nested holes, to some extent

    data += readPoly("100 100 100 1900 1900 1900 1900 100");
    data -= readPoly("200 200 200 1800 1800 1800 1800 200");
    data += readPoly("300 300 300 1700 1700 1700 1700 300");
    data -= readPoly("400 400 400 1600 1600 1600 1600 400");
    data += readPoly("500 500 500 1500 1500 1500 1500 500");
    data -= readPoly("600 600 600 1400 1400 1400 1400 600");

    */

    dumpPoly(data);

    return 0;
}

这是我使用 matplotlib 在 python 中编写的一个极小的可视化脚本。只需将上述程序的输出通过管道传输到此脚本,您就会看到结果。它工作正常,我只是将它包括在内以帮助您帮助我:)

#!/usr/bin/env python 
import matplotlib.pyplot as plt
import sys

lines = sys.stdin.read().split("\n")

for line in lines:
    data = line.split()

    if len(data) == 0:
        continue

    x = []
    y = []
    i = 0
    while i < len(data):
        x.append(float(data[i]))
        y.append(float(data[i+1]))
        i += 2

    plt.fill(x, y)

plt.show()

这是我的期望: 上述程序的预期输出

这是我得到的: 上述程序的错误输出

我的问题是:为什么 Boost::Polygon 会丢弃我的一些输入,我该如何阻止它这样做?

4

1 回答 1

4

来自http://www.boost.org/doc/libs/1_54_0/libs/polygon/doc/index.htm

坐标数据类型是库提供的所有数据类型和算法的模板参数,预计是整数。库中实现的算法不支持浮点坐标数据类型,因为实现浮点鲁棒性意味着一组不同的算法,并且通常针对浮点表示的平台特定假设。

于 2013-10-23T13:10:01.393 回答