boost::geometry polygon's outer() has wrong coordinates after polygon is rotated

1.4k views Asked by At

I'm using boost 1.56-rhel5 version. After a polygon is rotated, when I use outer() to get all the points of the polygon, and then get their coordinates, there is wrong coordinate result.

Here is my code:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <iostream>

int main()
{
    typedef boost::geometry::model::d2::point_xy<double>    point_type;
    typedef boost::geometry::model::polygon<point_type>     polygon_type;

    polygon_type poly;
    polygon_type poly2;

    boost::geometry::read_wkt( "POLYGON((0 0 0 2 1 2 1 0))", poly);

    // Rotate poly to get poly2
    boost::geometry::strategy::transform::rotate_transformer<boost::geometry::degree, double, 2, 2> rotate (180);
    boost::geometry::transform(poly, poly2, rotate);

    // Print the coordinates of poly2
    std::cout << "poly2's coordinates:" << std::endl;
    for (unsigned i = 0; i != poly2.outer().size(); ++i) {
        int x = boost::geometry::get<0>(poly2.outer()[i]);
        int y = boost::geometry::get<1>(poly2.outer()[i]);
        std::cout << " " << x;
        std::cout << " " << y;
    }
    std::cout << std::endl;

    // Verify whether poly2 is correctly rotated
    point_type p2(-2,-2);
    std::cout << "Distance between p2 and poly2: " << boost::geometry::distance(p2, poly2) << std::endl;
}

Here is print result:

poly2's coordinates:

0 0 0 -2 0 -2 -1 0

Distance between p2 and poly2: 1

Howerve the expected result is:

poly2's coordinates:

0 0 0 -2 -1 -2 -1 0

Distance between p2 and poly2: 1

The "Distance between p2 and poly2: 1" is correct, which means the rotate works well. But poly2's coordinates is wrong. The third point should be -1 -2, not 0 -2.

Is this a bug of boost::geometry? Or I have any misunderstanding or use it in wrong way?

1

There are 1 answers

3
Bin Li On

It is due to using integers to store the coordinates. When assign a double value to an integer, it is cast. For example, -0.9999999 becomes 0. Should use double type to store instead.

// Print the coordinates of poly2
std::cout << "poly2's coordinates:" << std::endl;
for (unsigned i = 0; i != poly2.outer().size(); ++i) {
    int x = boost::geometry::get<0>(poly2.outer()[i]);
    int y = boost::geometry::get<1>(poly2.outer()[i]);
    std::cout << " " << x;
    std::cout << " " << y;
}

Should change to:

// Print the coordinates of poly2
std::cout << "poly2's coordinates:" << std::endl;
for (unsigned i = 0; i != poly2.outer().size(); ++i) {
    double x = boost::geometry::get<0>(poly2.outer()[i]);
    double y = boost::geometry::get<1>(poly2.outer()[i]);
    std::cout << " " << x;
    std::cout << " " << y;
}