How to use Eigen::Matrix4d as message type in C++ Actor Framework?

166 views Asked by At

I'd like to use the Eigen::Matrix4d class as a message in the CAF. But I can't seem to write a good Inspector for it.

The error is as follows:

usr/local/include/caf/write_inspector.hpp:133:7: error: static assertion failed: 
T is neither inspectable nor default-applicable

I've tried passing the content of the Matrix4d element per element and tried some more elaborate approaches with Boost (boost_serialization_eigen.h), but I just keep getting the same error.

#include <iostream> 
#include <caf/all.hpp>
#include <Eigen/Core>
#include <Eigen/Geometry>
using namespace caf;
using namespace std;
using namespace Eigen;


CAF_BEGIN_TYPE_ID_BLOCK(custom_types, first_custom_type_id)
  CAF_ADD_TYPE_ID(custom_types, (Matrix4d))
CAF_END_TYPE_ID_BLOCK(custom_types)

#include <iostream>

template <class Inspector> 
typename Inspector::result_type inspect(Inspector& f, Matrix4d& m) {
  return f(m.data());
}


void caf_main(actor_system& system) {
    Eigen::Matrix4d Trans; // Your Transformation Matrix
    Trans.setIdentity();   // Set to Identity to make bottom row of Matrix 0,0,0,1
    Trans(0, 0) = 42;
    std::cout << Trans << endl;
    // Spawn the actor
}

// creates a main function for us that calls our caf_main
CAF_MAIN(id_block::custom_types)

I realize this may be a broad question, but any pointers in the right direction are appreciated.

1

There are 1 answers

2
neverlord On

(assuming CAF 0.17)

The inspection DSL doesn't really seem to cover this particular case very well. Probably the best solution for CAF 0.17 at the moment would be specializing on data_processor and calling consume_range:

template <class Derived>
auto inspect(data_processor<Derived>& f, Matrix4d& x) {
  auto range = make_span(x.data(), x.size());
  return f.consume_range(range);
}

This won't work with CAF's automatic string conversion, but you can provide a to_string overload as needed.