首页 > 解决方案 > 用浮点数提升多边形相交

问题描述

我正在尝试通过标准 Boost.Geometry Intersection 方法使用浮点数而不是双精度数。但是,如果我只是在第一个循环中将所有内容都更改为浮点数,那么这段代码就会崩溃。使用 double 它可以毫无问题地工作。这是错误和代码:

未处理的异常:System.AccessViolationException:试图读取或写入受保护的内存。这通常表明其他内存已损坏

PINVOKE int PolylineIntersectionFloat(
    float* polyline0, size_t polyline0_XYCount,
    float* polyline1, size_t polyline1_XYCount,
    float*& intersection, int& intersection_XYCount) {



    //////////////////////////////////////////////////////////////////////////////////////////
    /// Convert double array to Boost polygons
    //////////////////////////////////////////////////////////////////////////////////////////

    bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon0;
    bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon1;

    for (int i = 0; i < polyline0_XYCount; i += 2) {
        double a = polyline0[i];
        double b = polyline0[i+1];

        bg::append(polygon0.outer(), bg::model::d2::point_xy<float>(a,b));
    }

    for (int i = 0; i < polyline1_XYCount; i += 2) {
        bg::append(polygon1.outer(), bg::model::d2::point_xy<float>(polyline1[i], polyline1[i + 1]));
    }

    //////////////////////////////////////////////////////////////////////////////////////////
    /// Perform 2D Polygon Interserction
    //////////////////////////////////////////////////////////////////////////////////////////
    boost::geometry::correct(polygon0);
    boost::geometry::correct(polygon1);

    typedef bg::model::polygon<boost::geometry::model::d2::point_xy<float>> polygon;
    std::deque<polygon> output;
    bg::intersection(polygon0, polygon1, output);

    //////////////////////////////////////////////////////////////////////////////////////////
    /// Convert to Coordinate Array for Export
    //////////////////////////////////////////////////////////////////////////////////////////


    //std::ofstream myfile;
    //      myfile.open("C:\\Users\\petra\\AppData\\Roaming\\McNeel\\Rhinoceros\\packages\\7.0\\ngon\\3.1.0\\Test.txt");
    //      myfile << "Writing this to a file.\n";
    //      myfile << "Number of Polygons" << output.size();
    //      BOOST_FOREACH(polygon const& p, output)
    //      {
    //          myfile << "Count" << p.outer().size();
    //          myfile << "Count*2" << p.outer().size()*2;
    //      }
    //myfile.close();

    if (output.size() == 0)
        return 0;

    BOOST_FOREACH(polygon const& p, output)
    {


        intersection_XYCount = p.outer().size() * 2;
        intersection = new float[intersection_XYCount];

        //Iterate Polygon Points
        int j = 0;
        for (auto it = boost::begin(bg::exterior_ring(p)); it != boost::end(bg::exterior_ring(p)); ++it)
        {
            float x = bg::get<0>(*it);
            float y = bg::get<1>(*it);
            intersection[j] = x;
            intersection[j + 1] = y;
            j += 2;
        }

        return 1;

    }

    return 0;


}

标签: c++boostintersection

解决方案


推荐阅读