我正在尝试使用Boost Spirit X3解析一个Eigen::Matrix3f
矩阵,其中输入字符串为matrix(a,b,c,d,tx,ty)
,生成的本征3x3矩阵如下:
[a, c, tx
b, d, ty
0, 0, 1]
我已经可以合成std::vector<float>
属性,但是我想合成Eigen::Matrix3f
。合成Eigen::Matrix3f
(或与此相关的任何其他自定义矩阵类型)的最佳方法是什么?
答案 0 :(得分:0)
我使用Semantic Actions
找到了一个解决方案,代码如下:
auto identity = [&](auto& ctx){x3::_val(ctx) = Eigen::Matrix3f::Identity();};
auto a = [&](auto& ctx){x3::_val(ctx)(0,0) = x3::_attr(ctx);};
auto b = [&](auto& ctx){x3::_val(ctx)(1,0) = x3::_attr(ctx);};
auto c = [&](auto& ctx){x3::_val(ctx)(0,1) = x3::_attr(ctx);};
auto d = [&](auto& ctx){x3::_val(ctx)(1,1) = x3::_attr(ctx);};
auto tx = [&](auto& ctx){x3::_val(ctx)(0,2) = x3::_attr(ctx);};
auto ty = [&](auto& ctx){x3::_val(ctx)(1,2) = x3::_attr(ctx);};
auto matRule = x3::rule<class MatId, Eigen::Matrix3f>() = x3::eps[identity] >> x3::no_case["matrix"]
>> "("
>> x3::float_[a]
>> ","
>> x3::float_[b]
>> ","
>> x3::float_[c]
>> ","
>> x3::float_[d]
>> ","
>> x3::float_[tx]
>> ","
>> x3::float_[ty]
>> ")";
std::string teststr = "matrix(2,3,4,5,6,7)";
Eigen::Matrix3f mat;
bool success = x3::phrase_parse(teststr.begin(), teststr.end(), matRule, x3::space, mat);
if (success) std::cout << mat << std::endl;