Boost几何寄存器将Point 2d作为共享指针

时间:2014-09-09 12:07:53

标签: c++ boost shared-ptr boost-geometry

我想用

注册一个自定义的2d点
class CustomPoint{
public:
double X;
double Y;
};

BOOST_GEOMETRY_REGISTER_POINT_2D(CustomPoint, double, cs::cartesian, CustomPoint::X, CustomPoint::Y)

这很有效,我可以注册一个戒指

但是当我使用共享指针时:

typedef std::shared_ptr<CustomPoint> cpPtr;
BOOST_GEOMETRY_REGISTER_POINT_2D(cpPtr, double, cs::cartesian, ?, ?)

我不知道我怎么能用这个宏来获取我的X和Y.那可能吗? boost宏的定义是

  

Field0会员包含第一个(通常是x)坐标

     

Field1会员包含第二个(通常是y)坐标

我可以指向包含我的X coorinate和共享指针的字段吗?

1 个答案:

答案 0 :(得分:2)

您可以定义自己的特征:

using cpPtr = boost::shared_ptr<CustomPoint>;

namespace boost { namespace geometry { namespace traits {
    BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)

    template<> struct access<cpPtr, 0> {
        static inline double get(cpPtr const& p) { return p->X; }
        static inline void set(cpPtr& p, double const& value) { p->X = value; }
    };
    template<> struct access<cpPtr, 1> {
        static inline double get(cpPtr const& p) { return p->Y; }
        static inline void set(cpPtr& p, double const& value) { p->Y = value; }
    };
}}}

查看 Live On Coliru

#include <iostream> 
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/register/point.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>

using namespace boost::geometry;

class CustomPoint{
public:
    double X;
    double Y;
};

using cpPtr = boost::shared_ptr<CustomPoint>;

namespace boost { namespace geometry { namespace traits {
    BOOST_GEOMETRY_DETAIL_SPECIALIZE_POINT_TRAITS(cpPtr, 2, double, cs::cartesian)

    template<> struct access<cpPtr, 0> {
        static inline double get(cpPtr const& p) { return p->X; }
        static inline void set(cpPtr& p, double const& value) { p->X = value; }
    };
    template<> struct access<cpPtr, 1> {
        static inline double get(cpPtr const& p) { return p->Y; }
        static inline void set(cpPtr& p, double const& value) { p->Y = value; }
    };
}}}

int main()
{
    auto p1 = boost::make_shared<CustomPoint>();
    auto p2 = boost::make_shared<CustomPoint>();

    namespace bg = boost::geometry;

    bg::assign_values(p1, 1, 1);
    bg::assign_values(p2, 2, 2);

    double d = bg::distance(p1, p2);

    std::cout << "Distance: " << d << std::endl;

    return 0;
}

打印

Distance: 1.41421