ID field intermittently lost in custom point class

后端 未结 2 2106
予麋鹿
予麋鹿 2020-12-07 00:14

I\'m building a C++ program which needs to handle geometry. I have been trying to get boost::geometry to work, but I am having the following issue. My points ne

相关标签:
2条回答
  • 2020-12-07 00:23

    You carry out a BOOST_GEOMETRY_REGISTER_POINT_2D_GET_SET. The thing is, this tells the library how to set the x and y values. It never tells the library how to set an ID value (or that it is even there).

    So, the library will simply construct an identical point, logically, by using the setters for x and y, and of course, you'll be stuck with no id.

    0 讨论(0)
  • 2020-12-07 00:42

    As sehe pointed out, the library only knows how to access X and Y coordinates of My_Point. Furthermore, the rotate_transformer only knows how to rotate the geometrical part of your Points, it isn't aware that you're storing IDs and that you'd like to copy them. You could try to write your own strategy for this. Something like (not tested):

    struct my_rotate_transformer
        : public strategy::transform::rotate_transformer<degree, double, 2, 2>
    {
        typedef strategy::transform::rotate_transformer<degree, double, 2, 2> base_t;
    
        my_rotate_transformer(double angle)
            : base_t(angle)
        {}  
    
        template <typename P1, typename P2>
        bool apply(P1 const& p1, P2& p2) const
        {
            p2.Set_ID(p1.Get_ID());
            return base_t::apply(p1, p2);
        }
    }
    

    It's similar to the way how std::transform() can be used. You must pass a UnaryOperation which transforms the elements of a Range the way you like it. In Boost.Geometry strategies are used for this purpose.

    Btw, it's a simple case, you could just manually copy/set the IDs.

    Another thing is that bg::transform() works for arbitrary Geometry so you could just pass Polygons there (however you need another Polygon for this):

    polygon<My_Point> poly_in;
    polygon<My_Point> poly_out;
    bg::transform(poly_in, poly_out, my_rotate_transformer(45))
    

    Using append() you can directly append Points to Polygon. There is no need to use temporary std::vector, I think.

    Also, have in mind that some algorithms creates entirely new Geometries, containing new Points, e.g. intersection() or convex_hull() so IDs probably shouldn't be copied, or not all of them.

    And last but not least, I'm guessing that some algorithms may cause problems in your scenario, it probably depends on the algorithm. So feel free to ask questions. Consider also subscribing to Boost.Geometry mailing list. It's a good place for getting in touch with the developers, proposing new features, reporting bugs, etc.

    0 讨论(0)
提交回复
热议问题