我想用注册一个自定义的二维点

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宏的定义是

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

最佳答案

您可以定义自己的特征:

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

10-04 14:48