gpt4 book ai didi

c++ - Boost geometry 将 Point 2d 注册为共享指针

转载 作者:行者123 更新时间:2023-11-30 04:01:27 33 4
gpt4 key购买 nike

我想注册一个自定义的 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 Member containing first (usually x) coordinate

Field1 Member containing second (usually y) coordinate

我可以指向包含我的 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

关于c++ - Boost geometry 将 Point 2d 注册为共享指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25744244/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com