1

I am working on code for a library that uses Eigen extensively, and frequently maps Eigen::Matrix objects with an NSObject subclass of my own design (vMAT_Array). Working with the library often requires marshaling matrices into vMAT_Array instances to they can be passed around, etc.

I have a vMAT_cast template function which handles this:

template <typename EigenObjectType>
vMAT_Array * vMAT_cast(EigenObjectType matrix)
{
    return Map<EigenObjectType>(matrix).matA;
}

The problem with this function is that it does not interact correctly with Eigen's lazy evaluation semantics. Take for example the unit test code below:

vMAT_Array * matM = vMAT_cast(VectorXd::LinSpaced(40, 1.0, 40.0).eval());
[matM reshape:vMAT_MakeSize(5, 8)];
Mat<double> M = matM;
Array<bool, Dynamic, Dynamic> sel = M.unaryExpr([](double elt) { return (int)elt % 3 == 0; }).cast<bool>();
vMAT_Array * vecN = vMAT_pick(matM, vMAT_cast(sel));
NSLog(@"%@", vecN.dump);
vMAT_Array * vecNv = vMAT_cast(VectorXd::LinSpaced(13, 3.0, 39.0).eval());
STAssertEqualObjects(vecN, vecNv, @"Logical indexing broken");

Notice the explicit .eval() calls on most of the arguments to vMAT_cast. These are necessary because the template function tries (at compile time) to expand into code using one of Eigen's lazy expression templates, which generates lovely error messages like this one:

/Users/Shared/Source/vMAT/vMATTests/EigenTests.mm:25:35: note: in instantiation of member function 'Eigen::DenseBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >::LinSpaced' requested here
    vMAT_Array * matM = vMAT_cast(VectorXd::LinSpaced(40, 1.0, 40.0));
                                  ^
In file included from /Users/Shared/Source/vMAT/vMATTests/EigenTests.mm:11:
In file included from /Users/Shared/Source/vMAT/vMAT/vMAT.h:51:
/Users/Shared/Source/vMAT/vMAT/vMAT_Array.h:122:82: error: no member named 'data' in 'Eigen::CwiseNullaryOp<Eigen::internal::linspaced_op<double, true>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >'
                                        data:[NSMutableData dataWithBytes:matrix.data()
                                                                          ~~~~~~ ^

I suspect there is template-fu that would "force" the MatrixBase::eval to happen, but I lack knowledge of it. Can anyone enlighten me?

4

1 回答 1

2

您可以在 eigen/unsupported/Eigen/OpenGLSupport 模块中找到这种模板功夫。在这里,您将找到通过原始 C 指针获取向量和矩阵的 OpenGL 函数的包装器。关键是在这个测试中,我们检查表达式类型是否兼容 OpenGL:

bool IsGLCompatible =  bool(XprType::Flags&LinearAccessBit)                                              
                    && bool(XprType::Flags&DirectAccessBit)                                               
                    && (XprType::IsVectorAtCompileTime || (XprType::Flags&RowMajorBit)==0)

LinearAccessBit 表示没有“步幅”,DirectAccessBit 表示有 .data() 可用。还有其他两个是显而易见的,可能与您的情况无关。

您的另一个选择可能更简单,是使用 devel 分支的新 Ref<> 类。有关此方法的详细信息,请参阅文档。

于 2013-04-17T17:25:57.200 回答