eigen_impl.cpp 842 Bytes
Newer Older
lucasb-eyer's avatar
lucasb-eyer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <Eigen/Core>
#include <iostream>

#include <Python.h> // for Py_ssize_t

typedef Eigen::Matrix<float, Eigen::Dynamic, 1> NumpyVecF;

static Eigen::VectorXf buf2vecf(float *mem, Py_ssize_t n)
{
    return Eigen::Map<NumpyVecF>(mem, n);
}

static void vecf2buf(const Eigen::VectorXf& vec, float *mem)
{
    Eigen::Map<NumpyVecF>(mem, vec.size()) = vec;
}

// In Python, the default is row-major (C) while in Eigen it's ColMajor (F).
typedef Eigen::Matrix<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> NumpyMatF;

static Eigen::MatrixXf buf2matf(float *mem, Py_ssize_t h, Py_ssize_t w)
{
    // This does the conversion, so very likely makes a copy.
    return Eigen::Map<NumpyMatF>(mem, h, w);
}

static void matf2buf(const Eigen::MatrixXf& mat, float *mem)
{
    Eigen::Map<NumpyMatF>(mem, mat.rows(), mat.cols()) = mat;
}