Boost logo

Geometry :

Subject: [geometry] Polygon difference occasionally incorrect?
From: tknueppel (torstenknueppel_at_[hidden])
Date: 2018-02-23 10:01:57


Hi,

I experience some strange results computing a difference of two polygons. I
posted the code I use below.
I would expect an empty difference, because polygon 1 is within polygon 2
(except for two vertices that touch upon the second polygon's boundary) -
however, I get polygon 1 as result. If I slightly change the first polygon,
so that it doesn't touch the boundary of the first, I get results that I can
comprehend.

The following picture illustrates the situation (polygon 1 is red, polygon 2
is blue):

<http://boost-geometry.203548.n3.nabble.com/file/t496078/difffrage.jpg>

Thanks in advance!
Torsten

#include "stdafx.h"

#include <algorithm> // for reverse, unique
#include <iostream>
#include <string>

#include <boost/geometry/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include <boost/geometry/geometries/multi_polygon.hpp>

BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)

int main(void)
{
        using namespace boost::geometry;

        typedef model::d2::point_xy<double> point_2d;
        typedef model::polygon<point_2d, false, false> polygon_2d;
        typedef model::box<point_2d> box_2d;
        typedef model::multi_polygon<polygon_2d> MultiPolygon;

        polygon_2d poly1;
        polygon_2d poly2;
        MultiPolygon difference;

        poly1.outer().push_back(point_2d( 1.0, -1.0));
        poly1.outer().push_back(point_2d( 1.0, 1.0));
        poly1.outer().push_back(point_2d(-1.0, 1.0));
        poly1.outer().push_back(point_2d(-1.0, -1.0));

        poly2.outer().push_back(point_2d(-0.0947048516123954, 11.2051515495609));
        poly2.outer().push_back(point_2d(-1.61846412356614, -0.922292741982597));
        poly2.outer().push_back(point_2d(-0.500000000000006, -1.06282276938663));
        poly2.outer().push_back(point_2d(1.20904255203811, -0.991244911974210));
        poly2.outer().push_back(point_2d(0.698234950537465, 11.2051515495609));
        
        boost::geometry::difference(poly1, poly2, difference);

        std::cout << "poly 1: " << boost::geometry::dsv(poly1) << std::endl;
        std::cout << "poly 2: " << boost::geometry::dsv(poly2) << std::endl;
        std::cout << "difference: " << boost::geometry::dsv(difference) <<
std::endl;

        return 0;
}

--
Sent from: http://boost-geometry.203548.n3.nabble.com/

Geometry list run by mateusz at loskot.net