0
votes

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
Cannot reproduce. Not even with boost 1_56 (I downloaded it just to check). See coliru.stacked-crooked.com/a/1e593be1e51470b5sehe
WAIT. You edited the question 11 minutes ago to introduce the bug?! Did you not test the code that you included in the answer o.Osehe
Sorry, but the problem is real. Since i cannot copy my original codes due to the company's policy, I typed in the codes but it is not totally same with original codes. It cannot reproduce the bug. Just now I found the reason of this issue, so I edited the question.Bin Li
They're lame excuses for not testing the code you posted in your question. It's trivial to do so, even without a compiler (see my comment above, linking to coliru)sehe
It was my bad not to test the code before posting. And thanks for the information of coliru website. I will use it for testing next time before posting.Bin Li

1 Answers

-1
votes

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;
}