jwezorek
jwezorek

Reputation: 9525

what is boost::geometry::correct doing in this case?

The following code generates the output I expect:

MULTILINESTRING((5 5,4 4),(2 2,1 1))

However, if I remove the call to boost::geometry::correct() it returns the incorrect result:

MULTILINESTRING((5 5,1 1))

Code below:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/linestring.hpp>
#include <boost/geometry/multi/geometries/multi_linestring.hpp>
#include <iostream>

namespace bg = boost::geometry;
namespace bgm = boost::geometry::model;

using point = bgm::point<double, 2, bg::cs::cartesian>;
using polygon = bgm::polygon<point>;
using polyline = bgm::linestring<point>;
using polylines = bgm::multi_linestring<polyline>;

int main()
{
    polygon poly = {
        { {1,5}, {5,5}, {5,1}, {1,1}  },
        { {2,4}, {2,2}, {4,2}, {4,4}  }
    };
    polyline line = { {6,6},{0,0} };

    bg::correct(poly);

    polylines result;
    bg::intersection(poly, line, result);

    std::cout << bg::wkt(result) << "\n";

    return 0;
}

The geometry defined in the above looks like the following. The red line segment and blue polygon with a hole should intersect to the green line segments.

enter image description here

I defined the vertices of the hole in counter-clockwise order as it is typical in computational geometry for holes to have reverse orientation of non-holes (It's also typical for non-holes to have counter-clockwise orientation but boost::geometry seems to default to clockwise). If I flip the orientation of the hole it does not fix the problem (although I do get a different wrong result). I am not sure what else correct could be doing.

Upvotes: 1

Views: 840

Answers (1)

jwezorek
jwezorek

Reputation: 9525

boost::correct() is closing both the inner and outer polygons.

That is, the following returns the expected output:

namespace bg = boost::geometry;
namespace bgm = boost::geometry::model;

using point = bgm::point<double, 2, bg::cs::cartesian>;
using polygon = bgm::polygon<point>;
using polyline = bgm::linestring<point>;
using polylines = bgm::multi_linestring<polyline>;

int main()
{
    polygon poly = {
        { {1,5}, {5,5}, {5,1}, {1, 1}, {1,5}},
        { {2,4}, {2,2}, {4,2}, {4,4}, {2,4}}
    };
    polyline line = { {6,6},{0,0} };
    polylines result;
    bg::intersection(poly, line, result);

    std::cout << bg::wkt(result) << "\n";

    return 0;
}

Upvotes: 1

Related Questions