【问题标题】:Loading and Eigen Matrix from yaml using yaml-cpp使用 yaml-cpp 从 yaml 加载和特征矩阵
【发布时间】:2022-10-04 19:22:51
【问题描述】:

我正在使用非常好的 yaml-cpp 项目将配置读入 C++ 程序。其中一个配置项存储一个 Eigen::Matrix<..> 以下代码运行良好,但想知道是否有更好的方法?

主.cpp:

YAML::Node config = YAML::LoadFile("default.config");

const vector<double> eigenVec = config["my_matrix"].as<vector<double>>();

// https://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html
Eigen::Matrix<double,4,4,Eigen::RowMajor> Matrix4x4(eigenVec.data());
cout << "Matrix  : " << Matrix4x4 << endl;

配置.yaml:

my_matrix: [1.0, 2.0, 3.0, 4.0,
        0.0, 1.0, 3.0, 0.0,
        0.0, 7.0, 3.0, 0.0,
        3.0, 0.0, 3.0, 1.0 ]

任何指导将不胜感激。

【问题讨论】:

  • 尝试另存为npy 格式的二进制文件?小数不是明智的选择。

标签: c++ eigen3 yaml-cpp


【解决方案1】:

也许你可以压缩成一行代码

Eigen::Matrix4d Matrix4x4= Eigen::Map<Eigen::Matrix<double, 4, 4, Eigen::RowMajor>>(config_node_["my_matrix"].as<std::vector<double>>().data());

【讨论】:

    猜你喜欢
    • 2020-05-12
    • 2014-05-31
    • 2015-12-06
    • 2020-10-14
    • 1970-01-01
    • 2015-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多