c++ - Eigen C++ 断言失败

标签 c++ matrix matrix-multiplication eigen

我有一个 Eigen MatrixXd 对象,称为 v,在尝试访问此矩阵内容时遇到一些问题。当我只在控制台打印内容(如代码中所示)时,效果也很好。当我尝试使用该内容时,出现错误:

Assertion failed: (row >= 0 && row < rows() && col >= 0 && col < cols()), function operator(), file /usr/local/Cellar/eigen/3.2.4/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h, line 337.

    ChosenPoint ** points = new ChosenPoint*[width];
    for (int i = 0; i < width; i++)
    {
        points[i] = new ChosenPoint[height];
        for (int j = 0; j < height; j++)
        {
            points[i][j].setPoint(i, j, false);
            points[i][j].setNumberOfFrames(numberOfFrames);
        }
    }

Matrix<double, 2, 1> v = (aT * a).inverse() * aT * b;
if (v.rows() == 2 && v.cols() == 1)
{
     points[x][y].setFlow(v(0,0), v(1,0), frame);
}

还有我的 ChosenPoint 类:

typedef struct point
{
    double x;
    double y;
    bool isValid;
 } point;

 class ChosenPoint 
{
public: 
ChosenPoint()
{

}

~ChosenPoint()
{
}

void setNumberOfFrames(int numberOfFrames)
{
    this->flow = new point[numberOfFrames];

    for (int i = 0; i < numberOfFrames; i++)
    {
        point f;
        f.x = 0.0;
        f.y = 0.0;
        this->flow[i] = f;
    }
}

void setPoint(int x, int y, bool isValid)
{
    this->pt.x = (double) x;
    this->pt.y = (double) y;
    this->pt.isValid = isValid;
}

point getPoint()
{
    return this->pt;
}

point* getFlow()
{
    return this->flow;
}

void setFlow(double &xFlow, double &yFlow, int &position)
{
    this->flow[position].x = xFlow;
    this->flow[position].y = yFlow;
}

void updateFlow(int position)
{
    this->flow[position].x = 2*this->flow[position].x;
    this->flow[position].y = 2*this->flow[position].y;
}

void updateFlow(double xFlow, double yFlow, int position)
{
    this->flow[position].x = xFlow;
    this->flow[position].y = yFlow;
}

point pt;
point *flow;

};

最佳答案

我的错。问题出在我在项目中使用的其他矩阵之一,我花了一段时间才弄清楚。不幸的是,当这种情况发生时,Eigen 似乎并没有真正的帮助:

I had 2 matrixes (A and B). The matrix with problem was A (somehow, some data was not loaded into the matrix). But when i multiplied A and B, it generated a new matrix C with some valid results (all my sanity checks were unuseful). I admit I don`t know a lot of Eigen.

无论如何,希望这对更多像我一样的人有帮助。

关于c++ - Eigen C++ 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38626653/

相关文章:

c++ - 如何使用返回值整理 C++ 错误处理? (解除分配)

c++ - 当明确给出数字时,我是否应该引用 std::vector size

c++ - vector 构造函数和垫

java - java中矩阵随机汇总每列为1

c++ - 在 C++ 中缩小 2D vector

matrix - 为什么我的 WorldViewProj 矩阵需要这个 Transpose()?

c++ - Ubuntu C++ 多播双留组消息

Java : How to visualize/plot two parameters relations with a matrix

python - Python 中的逐元素乘法相当于 Matlab

c - OpenMP 并行化( block 矩阵乘法)