在std向量中将元素分配给Eigen :: Vector2d会引发错误 [英] Assigning elements to Eigen::Vector2d within std vector throws error

查看:263
本文介绍了在std向量中将元素分配给Eigen :: Vector2d会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览了几篇StackOverflow帖子,但是没有发现这样的错误.我试图编写一个简单的类,对本征向量和矩阵进行一些操作.我创建了一个名为MyClass的类,它有一个名为MyMethod的方法.它的代码如下

I went through few StackOverflow posts but didn't find such error. I am trying to write a simple class which does a few operations on Eigen vectors and matrices. I created a class called MyClass and it has a method by the name MyMethod. It's code is as follows

void MyClass::MyMethod(Eigen::Vector4f X, 
                       std::vector<Eigen::Vector2i> &pixelIndices,
                       std::vector<Eigen::Vector4f> vertices)
{
   // Do some preprocessing

   //Deleacring the std vector
   std::vector<Eigen::Vector2i> currTriangle(3);
   currTriangle[0] = Eigen::Vector2i(0); //Error occurs here

   // Do some more processing
}

在执行该方法时,在所述语句处发生主函数错误.错误输出如下.

Upon executing method from a main function error occurs at the said statement. The error output is given below.

$: ./test1 
test1: /usr/include/eigen3/Eigen/src/Core/PlainObjectBase.h:285: void Eigen::PlainObjectBase<Derived>::resize(Eigen::Index) [with Derived = Eigen::Matrix<int, 2, 1>; Eigen::Index = long int]: Assertion `((SizeAtCompileTime == Dynamic && (MaxSizeAtCompileTime==Dynamic || size<=MaxSizeAtCompileTime)) || SizeAtCompileTime == size) && size>=0' failed.
Aborted (core dumped)

我知道在Eigen上使用STL容器是有问题的,但是正如文档问题似乎仅在于固定大小的矢量化本征类型(也就是说,它们应为16字节大小),但是Vector2i并不是这样的本征类型.assert语句在PlainObjectBase的resize()方法中调用,这也很奇怪,因为我在代码中的任何地方都没有使用它.

I know that using STL containers with Eigen is problematic, but as mentioned in the documentation the problems seems to be only with fixed size vectorizable Eigen types (that is they should be of 16 byte size), but Vector2i is not such a Eigen type. The assert statement is called in resize() method of PlainObjectBase, which is also weird because I haven't used it anywhere in the code.

还有其他人遇到此错误吗?任何帮助将不胜感激.

Has anyone else faced this error? Any help will be really appreciated.

更新:该错误似乎不是因为我使用了std :: vector.我对代码做了一些小的更改.

Update: The error seems to be not because I used std::vector. I made this small changes to the code.

  void MyClass::MyMethod(Eigen::Vector4f X, 
                         std::vector<Eigen::Vector2i> &pixelIndices,
                         std::vector<Eigen::Vector4f> vertices)
{
   // Do some preprocessing

   Eigen::Vector2i temp(0); //Same Error occures here also
   //Deleacring the std vector
   std::vector<Eigen::Vector2i> currTriangle(3);
   currTriangle[0] = Eigen::Vector2i(0);

   // Do some more processing
}

因此,似乎在初始化Vector2i时发生了错误.

So it seems like the error occurs when initializing the Vector2i.

推荐答案

正如@MarcGlisse指出的那样, Vector2i(0)告诉要使用构造一个 Vector2i 0 元素,将在运行时失败.用单个标量构造固定大小的矩阵/向量将其解释为大小而不是值的原因是为了允许通用函数,而不清楚大小是动态的还是固定的:

As @MarcGlisse pointed out, Vector2i(0) tells to construct a Vector2i with 0 elements, which will fail at runtime. The reason why fixed sized matrices/vectors constructed with a single scalar interpret this as size rather than value is to allow generic functions, where it is not clear whether the size dynamic or fixed:

template<int SizeAtCompileTime>
void foo(){
    Eigen::Matrix<int, SizeAtCompileTime, 1> v(actualSize);
    // ...
}

有两种边界情况:将两个整数传递给具有两个元素的向量,或者将一个整数传递给具有一个元素的向量,将导致该向量用该值初始化.如果向量的标量类型可以根据传递的整数类型隐式构造-否则,它将被解释为大小.

There are two border-cases: Passing two integers to a vector with two elements or passing one integer to a vector with one element, will cause the vector to be initialized with that value(s) if the scalar type of the vector can implicitly constructed from the passed integer type -- otherwise, it will be interpreted as size.

要解决您的原始问题,有几种选择:

To solve your original problem, there are several alternatives:

Eigen::Vector2i temp1(Eigen::Vector2i::Zero());
Eigen::Vector2i temp2(0,0);

// initialize all elements with a Zero vector:
std::vector<Eigen::Vector2i> currTriangle(3, Eigen::Vector2i::Zero());

currTriangle[0].setZero(); // set 0th element to Zero vector
currTriangle[0].setConstant(0); // equivalent to setZero, but works for arbitrary constants
currTriangle[0].array() = 0; // .array() lets you do element-wise operations

这篇关于在std向量中将元素分配给Eigen :: Vector2d会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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