访问本征矩阵的行向量时进行复制或引用 [英] Copy or reference when accessing row vectors of Eigen matrices

查看:71
本文介绍了访问本征矩阵的行向量时进行复制或引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用使用 Eigen 矩阵库的代码。我注意到,通过该代码,访问器如下:

I'm working with code that uses the Eigen matrix library. I've noticed that all through that code, there are accessors as follows:

RowVector3f V(size_t vertex_index) const
{
    return mymatrix.row(vertex_index);
}

使用返回const ref的访问器会更好吗? Eigen有可能吗?或者也许只是将指向内部float的指针返回到行开始并将其强制转换为向量(当然,假设匹配行的主要布局)?

Wouldn't it be better to use an accessor that returns a const ref? Is that possible with Eigen? Or maybe just return the pointer to internal float to the row-begin and cast it to a vector (of course assuming matching row-major layout)?

推荐答案

在Eigen中,mymatrix.row(vertex_index)返回一个Block(假设mymatrix是Matrix3f)。 Block对象本质上是指向原始数据的指针。在您的情况下,此代理将复制到RowVector3f中。幸运的是,对于如此小的固定大小的对象,编译器将轻松地优化掉此多余的副本。因此,我不会打扰。

In Eigen, mymatrix.row(vertex_index) returns a Block (assuming mymatrix is a Matrix3f). A Block object is essentially a pointer to the original data. In your case, this proxy is copied into a RowVector3f. Fortunately, for such small fixed size objects, the compiler will easily optimize away this extra copy. So I'd not bother.

在更一般的情况下,我将重写V方法以返回MatrixXf :: RowXpr(这对于Block< ; ...>)。

In a more general case, I'd rewrite the V method to return a MatrixXf::RowXpr (which is a proper typedef to Block<...>).

这篇关于访问本征矩阵的行向量时进行复制或引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆