使用boost的多边形联合

时间:2014-03-11 09:46:18

标签: c++ boost boost-geometry

我正在使用boost :: geometry :: union_来合并随机多边形的向量,这里我写的是:

#include <iostream>
#include <vector>
#include <fstream>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/io/wkt/wkt.hpp>

#include <boost/foreach.hpp>
typedef boost::geometry::model::polygon<boost::geometry::model::d2::point_xy<double> > polygon;


void union_polys(std::vector<polygon> In_polys,std::vector<polygon> &Out_polys)
{
    std::vector<polygon> temp_polys;
    bool *considered = new bool [In_polys.size()];
    for(unsigned i = 0 ; i < In_polys.size() ; i++) considered[i] = false;
    for(unsigned i = 0 ; i < In_polys.size()/2; i++)
    {
        if(!considered[i])
        {
            polygon inetr = In_polys.at(i);
            for(unsigned j = i + 1 ; j < In_polys.size() ; j++)
            {

                if(!considered[j])
                {
                    temp_polys.clear();
                    boost::geometry::union_(inetr, In_polys.at(j) , temp_polys);
                    if(temp_polys.size() == 1)
                    {
                        inetr = temp_polys.at(0);
                        considered[j] = true;
                        j = i;
                    }
                }
            }
            Out_polys.push_back(inetr);
        }
    }
}

int main()
{
    std::ifstream file("test01.txt");

    polygon *poly;
    std::vector<polygon> input;


    while(!file.eof())
    {
        poly = new polygon;
        std::string poly_str;

        std::getline(file, poly_str);

        boost::geometry::read_wkt(poly_str, *poly);
        boost::geometry::correct(*poly);

        input.push_back(*poly);
    }
    std::cout << " input size is : " << input.size() << std::endl;

    std::vector<polygon> output;
    union_polys(input, output);

    std::cout << " output size is : " << output.size() << std::endl;

    int i = 0;
    std::cout << "green || blue:" << std::endl;

    BOOST_FOREACH(polygon const& p, output)
    {
        std::cout << i++ << ": " << boost::geometry::area(p) << std::endl;
    }

    system("pause");
    return 0;
}

我用两个不同的多边形集测试了这个函数union_polys:

test 01 test 02

它适用于第一次测试但是第二次测试失败了,我搜索但是我无法发现错误,任何人都可以帮忙吗?

0 个答案:

没有答案