Markus Heitz
Markus Heitz

Reputation: 85

boost.Geometry operators

I would like to use operators in boost.Geometry instead of multiply_value, add_point, dot_product ... . Do I have to define these myself?

#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);
}

Upvotes: 2

Views: 979

Answers (1)

Gus Monod
Gus Monod

Reputation: 110

The official Boost Geometry doc does not indicate any arithmetic operator (check at letter O).

Theoretically, you should be able to define a wrapper yourself, but bear in mind that there are two ways of adding or multiplying: multiply_point and multiply_value.

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

and

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

But the types of the arguments are interchangeable by the compiler, which means that it will not know which to choose if these two functions had the same name.

That means that you will have to choose which operation to execute when a multiplication is made, as well as choose the order of the operands, so that it is not ambiguous for the compiler.

Here is an example of how to do it, so that Point b{z * a} compiles:

// 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;
}

Note that Point b{a * z} will not compile with this solution, and neither will Point c{a * b}.

Example of order of the operands causing problem

Example of multiply_value or multiply_point causing problem

Upvotes: 1

Related Questions