促进几何体操作符

boost.Geometry operators

本文关键字:操作符 几何体      更新时间:2023-10-16

我想在boost中使用运算符。几何体而不是多重值、add_point、dot_product。我必须自己定义这些吗?

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point.hpp>
namespace bg = boost::geometry;
using Point = bg::model::point<double, 3, bg::cs::cartesian>;
using namespace bg;
void test()
{
    const double z{2.0};
    const Point a{1.0,2.0,-1.0};
    // this doesn't compile:
    //      const Point b{z*a};
    //      const Point c{a+b};
    // instead I have to do this:
    Point b{a};
    multiply_value(b, z);
    Point c{5.0,1.0,0.0};
    add_point(c, b);
}

官方的Boost Geometry文档没有指示任何算术运算符(请检查字母O)。

理论上,您应该能够自己定义包装器,但请记住有两种加法或乘法方式multiply_pointmultiply_value

template<typename Point1, typename Point2>
void multiply_point(Point1 & p1, Point2 const & p2)

template<typename Point>
void multiply_value(Point & p, typename detail::param< Point >::type value)

但参数的类型可以由编译器互换,这意味着如果这两个函数的名称相同,它将不知道该选择哪一个。

这意味着必须选择在进行乘法时执行的操作,以及选择操作数的顺序,这样编译器就不会有歧义。

以下是如何做到这一点的示例,以便Point b{z * a}编译:

// For multiply value
template<typename Point>
Point operator*(const Point & p, typename detail::param< Point >::type value) {
    Point result{p};
    multiply_value(result, value);
    return result;
}

请注意,Point b{a * z}不使用此解决方案编译Point c{a * b}也不会。

导致问题的操作数顺序示例

multiply_valuemultiply_point导致问题的示例