将一个特征向量类型转换为Vector3的最佳方法是什么?

时间:2021-03-03 15:41:35

I want to extract the three first values of a Vector4 type in Eigen, into a Vector3 type. So far I am doing it in a for-loop. Is there a smarter way to do it?

我想要将一个Vector4类型的三个第一个值提取到一个Vector3类型中。到目前为止,我是在循环中进行。有更聪明的方法吗?

3 个解决方案

#1


12  

The .head() member function returns the first n elements of a vector. If n is a compile-time constant, then you can use the templated variant (as in the code example below) and the Eigen library will automatically unroll the loop.

head()成员函数返回一个向量的第一个n个元素。如果n是一个编译时常量,那么您可以使用模板化的变体(如下面的代码示例),而Eigen库将自动展开循环。

Eigen::Vector4f vec4;
// initialize vec4
Eigen::Vector3f vec3 = vec4.head<3>();

In the Eigen documentation, see Block operations for an introduction to similar operations for extracting parts of vectors and matrices, and DenseBase::head() for the specific function.

在Eigen文档中,请参见块操作,以介绍用于提取向量和矩阵的部分的类似操作,以及用于特定函数的DenseBase::head()。

#2


0  

Yeah, because you know the size is static (3 elements) you should unroll the loop and copy them explicitly. This optimization might be performed by the compiler already, but it can't hurt to do it yourself just in case.

是的,因为您知道大小是静态的(3个元素),您应该展开循环并显式地复制它们。这个优化可能已经由编译器执行了,但是为了以防万一,您自己也可以这样做。

#3


0  

The answer of @Jitse Niesen is correct. Maybe this should be a comment on the original question, but I found this question because I had some confusion about Eigen. In case the original questioner, or some future reader has the same confusion, I wanted to provide some additional explanation.

@Jitse Niesen的答案是正确的。也许这应该是对最初的问题的评论,但我发现这个问题是因为我对艾根有一些困惑。如果最初的提问者或未来的读者有同样的困惑,我想提供一些额外的解释。

If the goal is to transform 3d (“position”) vectors by a 4x4 homogeneous transformation matrix, as is common in 3d graphics (e.g. OpenGL etc), then Eigen provides a cleaner way to do that with its Transform template class, often represented as the concrete classes Affine3f or Affine3d (as tersely described here). So while you can write such a transform like this:

如果我们的目标是将3 d(“位置”)由4 x4齐次变换矩阵,向量是常见的3 d图形(例如OpenGL等),然后特征提供了一个更清洁的方法,改变模板类,通常表示为具体类Affine3f或Affine3d(这里描述简洁地)。你可以这样写:

Eigen::Matrix4f transform;      // your 4x4 homogeneous transformation
Eigen::Vector3f input;          // your input
Eigen::Vector4f input_temp;
input_temp << input, 1;         // input padded with w=1 for 4d homogeneous space
Eigen::Vector4f output_temp = transform * input_temp;
Eigen::Vector3f output = output_temp.head<3>() / output_temp.w(); // output in 3d

You can more concisely write it like this:

你可以这样写:

Eigen::Affine3f transform;      // your 4x4 homogeneous transformation
Eigen::Vector3f input;          // your input
Eigen::Vector3f output = transform * input;

That is: an Eigen::Affine3f is a 4x4 homogeneous transformation that maps from 3d to 3d.

这是一个特征:Affine3f是一个4x4的同质变换,从3d到3d。

#1


12  

The .head() member function returns the first n elements of a vector. If n is a compile-time constant, then you can use the templated variant (as in the code example below) and the Eigen library will automatically unroll the loop.

head()成员函数返回一个向量的第一个n个元素。如果n是一个编译时常量,那么您可以使用模板化的变体(如下面的代码示例),而Eigen库将自动展开循环。

Eigen::Vector4f vec4;
// initialize vec4
Eigen::Vector3f vec3 = vec4.head<3>();

In the Eigen documentation, see Block operations for an introduction to similar operations for extracting parts of vectors and matrices, and DenseBase::head() for the specific function.

在Eigen文档中,请参见块操作,以介绍用于提取向量和矩阵的部分的类似操作,以及用于特定函数的DenseBase::head()。

#2


0  

Yeah, because you know the size is static (3 elements) you should unroll the loop and copy them explicitly. This optimization might be performed by the compiler already, but it can't hurt to do it yourself just in case.

是的,因为您知道大小是静态的(3个元素),您应该展开循环并显式地复制它们。这个优化可能已经由编译器执行了,但是为了以防万一,您自己也可以这样做。

#3


0  

The answer of @Jitse Niesen is correct. Maybe this should be a comment on the original question, but I found this question because I had some confusion about Eigen. In case the original questioner, or some future reader has the same confusion, I wanted to provide some additional explanation.

@Jitse Niesen的答案是正确的。也许这应该是对最初的问题的评论,但我发现这个问题是因为我对艾根有一些困惑。如果最初的提问者或未来的读者有同样的困惑,我想提供一些额外的解释。

If the goal is to transform 3d (“position”) vectors by a 4x4 homogeneous transformation matrix, as is common in 3d graphics (e.g. OpenGL etc), then Eigen provides a cleaner way to do that with its Transform template class, often represented as the concrete classes Affine3f or Affine3d (as tersely described here). So while you can write such a transform like this:

如果我们的目标是将3 d(“位置”)由4 x4齐次变换矩阵,向量是常见的3 d图形(例如OpenGL等),然后特征提供了一个更清洁的方法,改变模板类,通常表示为具体类Affine3f或Affine3d(这里描述简洁地)。你可以这样写:

Eigen::Matrix4f transform;      // your 4x4 homogeneous transformation
Eigen::Vector3f input;          // your input
Eigen::Vector4f input_temp;
input_temp << input, 1;         // input padded with w=1 for 4d homogeneous space
Eigen::Vector4f output_temp = transform * input_temp;
Eigen::Vector3f output = output_temp.head<3>() / output_temp.w(); // output in 3d

You can more concisely write it like this:

你可以这样写:

Eigen::Affine3f transform;      // your 4x4 homogeneous transformation
Eigen::Vector3f input;          // your input
Eigen::Vector3f output = transform * input;

That is: an Eigen::Affine3f is a 4x4 homogeneous transformation that maps from 3d to 3d.

这是一个特征:Affine3f是一个4x4的同质变换,从3d到3d。