10

I have two transformers, a translation and a rotation as follows:

namespace bg = boost::geometry;
namespace trans = bg::strategy::transform;

trans::translate_transformer<point, point> translate(px, py);
trans::rotate_transformer<point, point, bg::radian> rotate(rz);

How do I combine them into one, so that I don't have to call bg::transform twice each time and use an intermediate variable?

Nestor
  • 2,607
  • 2
  • 26
  • 32
  • I'm not entirely sure but I don't think it can be done yet. Hope someone proves me wrong. – owagh Mar 13 '12 at 20:39

1 Answers1

7

Both translate and rotate are affine transformations, i.e., they can be represented using a matrix. Therefore, all you have to do is to create a new transformer whose matrix is equal to the product of the matrices of the two transforms.

trans::ublas_transformer<point, point, 2, 2> translateRotate(prod(rotate.matrix(), translate.matrix()));

Here is a full working example:

#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/strategies/transform/matrix_transformers.hpp>

namespace bg = boost::geometry;
namespace trans = bg::strategy::transform;

typedef bg::model::d2::point_xy<double> point;

int main()
{
    trans::translate_transformer<point, point> translate(0, 1);
    trans::rotate_transformer<point, point, bg::degree> rotate(90);

    trans::ublas_transformer<point, point, 2, 2> translateRotate(prod(rotate.matrix(), translate.matrix()));

    point p;
    translateRotate.apply(point(0, 0), p);
    std::cout << bg::get<0>(p) << " " << bg::get<1>(p) << std::endl;
}

Be very careful regarding the order of the matrices in the multiplication. The example above first translates, then rotates.

user1202136
  • 10,325
  • 3
  • 36
  • 58
  • where does the `prod` function come from? – Agost Biro Jun 01 '17 at 15:47
  • 2
    For future reference: the `prod` function lives in the `boost::numeric::ublas` namespace. Also, starting with Boost 1.64, the line beginning with `trans::ublas_transformer...` should be `trans::matrix_transformer rotateTranslate(translate.matrix() * rotate.matrix());` – Agost Biro Jun 01 '17 at 17:18