简体   繁体   English

C++/CLI 将托管 MathNet 矩阵转换为 Eigen MatrixXi 以用于本机 class

[英]C++/CLI converting managed MathNet Matrix to Eigen MatrixXi for use in native class

I'm trying to build a managed wrapper for a native C++ class that holds an Eigen::MatrixXi .我正在尝试为包含Eigen::MatrixXi的本机 C++ class 构建托管包装器。 Let's say the native class has one member matrix that it stores and gets through its constructor, and a method that uses that member at a later time:假设本机 class 具有一个存储并通过其构造函数获取的成员matrix ,以及稍后使用该成员的方法:

#include <Eigen/Dense>
#include <Eigen/Core>
#include <iostream>

using Eigen::MatrixXi;

class Foo
{
public:
    const MatrixXi matrix;

    Foo(const MatrixXi &matrix);
    void Bar();
};

Foo::Foo(const MatrixXi &matrix)
    : matrix(matrix) {}

void Foo::Bar()
{
    for (int y = 0; y < matrix.rows(); y++)
    {
        for (int x = 0; x < matrix.cols(); x++)
        {
            std::cout << matrix(y, x);
        }
        std::cout << std::endl;
    }
}

I made the following wrapper:我做了以下包装:

using namespace MathNet::Numerics::LinearAlgebra;
using namespace MathNet::Numerics::LinearAlgebra::Single;

template<class T>
public ref class ManagedObject
{
protected:
    T* m_Instance;
public:
    ManagedObject(T* instance)
        : m_Instance(instance)
    {
    }
    virtual ~ManagedObject()
    {
        if (m_Instance != nullptr)
        {
            delete m_Instance;
        }
    }
    !ManagedObject()
    {
        if (m_Instance != nullptr)
        {
            delete m_Instance;
        }
    }
    T* GetInstance()
    {
        return m_Instance;
    }
};

public ref class ManagedFoo : public ManagedObject<Foo>
{
public:
    ManagedFoo(MathNet::Numerics::LinearAlgebra::Matrix<float>^ matrix);
    void Bar();
};

ManagedFoo::ManagedFoo(MathNet::Numerics::LinearAlgebra::Matrix<float>^ matrix)
    : ManagedObject(new Foo(mathnet_to_eigen(matrix))) {}

void ManagedFoo::Bar()
{
    m_Instance->Bar();
}

What would the mathnet_to_eigen method look like? mathnet_to_eigen方法会是什么样子? I've tried the following:我尝试了以下方法:

#include <vector>
#include <Eigen/Dense>
#include <Eigen/Core>

using Eigen::MatrixXi;

static Eigen::MatrixXi mathnet_to_eigen(MathNet::Numerics::LinearAlgebra::Matrix<float>^ data)
{
    int length = data->RowCount * data->ColumnCount;
    std::vector<int> data_converted(length);

    for (size_t i = 0; i < data->RowCount; i++)
    {
        for (size_t j = 0; j < data->ColumnCount; j++)
        {
            data_converted[i * data->ColumnCount + j] = data[i, j];
        }
    }

    Eigen::MatrixXi mat = Eigen::Map<Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>(&data_converted[0], data->RowCount, data->ColumnCount);

    return mat;
}

This seems to work, but I'm uncertain about the memory management and efficiency.这似乎可行,但我不确定 memory 的管理和效率。 Should I use pinning?我应该使用固定吗? And what would that look like?那会是什么样子? I have the feeling that I'm not doing it correctly and might be using a pointer to memory that is garbage collected.我感觉我做的不对,可能正在使用指向垃圾收集的 memory 的指针。

I don't care in this case about how the float to int conversion is done (rounding/taking integer part).在这种情况下,我不关心如何完成浮点到 int 的转换(舍入/取 integer 部分)。

I found this to be the cleanest way to do it:我发现这是最干净的方法:

static Eigen::MatrixXf MathNetToEigen(MathNet::Numerics::LinearAlgebra::Matrix<float>^ data)
{
    auto mat = Eigen::MatrixXf(data->RowCount, data->ColumnCount);
    for (size_t i = 0; i < data->RowCount; i++)
        for (size_t j = 0; j < data->ColumnCount; j++)
            mat(i, j) = data[i, j];

    return mat;
}

And converting back:并转换回来:

static MathNet::Numerics::LinearAlgebra::Matrix<float>^ EigenToMathNet(Eigen::MatrixXf& data)
{
    auto mat = MathNet::Numerics::LinearAlgebra::Matrix<float>::Build->Dense(data.rows(), data.cols());
    for (size_t i = 0; i < data.rows(); i++)
        for (size_t j = 0; j < data.cols(); j++)
            mat[i, j] = data(i, j);

    return mat;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Eigen的神秘C ++ / CLI管理/非管理错误 - Mysterious C++/CLI managed/unmanaged Error with Eigen 在C ++的Eigen库中,我该如何解决:无效使用不完整类型&#39;const class Eigen :: MatrixSquareRootReturnValue <Eigen::Matrix<float, -1, -1> &gt;” - In C++'s Eigen library, how do I solve: invalid use of incomplete type ‘const class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1> >’ 在C ++ / CLI中,为什么合格的,向前声明的本机类名称解析为另一个托管类名称? - Why is a qualified, forward declared, native class's name resolving to a different, managed class name in C++/CLI? 将本机缓冲区包装器转换为C ++ / CLI - Converting native buffer wrappers to C++/CLI 将Matlab中的以下Matrix代码转换为C ++中的本征的问题 - Problems converting the following Matrix code in Matlab to Eigen in C++ 将一些C#代码转换为托管或C ++ / CLI代码 - Converting some C# code to managed or C++/CLI code 如何正确初始化,分配动态Eigen矩阵并将其用作C ++中的类成员? - How to correctly initialize, assign and use a dynamic Eigen matrix as a class member in C++? 如何在C ++中自己的类中正确使用矩阵(本征库)? - How to correctly use a matrix (Eigen library) in my own class in C++? C ++特征::传递给类后无法访问矩阵 - C++ Eigen:: Unable to Access Matrix after Passing to Class 特征矩阵 Xi 到 VectorXi 的转换 - Eigen MatrixXi to VectorXi conversion
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM