Assertion failed: index >= 0 && index < size(), file F:\VSA_backup_set\libigl-example-project-main\out\build\x64-Debug_deps\eigen-src\Eigen\src/Core/DenseCoeffsBase.h, line 425
从上述错误可以看出应该是矩阵或向量下标越界的问题,出现问题的代码如下:
int p = P.size();
vector<VectorXi> regions(p);
vector<VectorXi> costs(p);
int pp = Proxies.rows() / 2;
for (int u = 0; u < p; u++) {
MatrixXd center = get<0>(P[u]);
int num = center.rows();
VectorXi costs_u = costs[u];
VectorXi regions_u = regions[u];
costs_u.resize(num);
regions_u.resize(num);
}
对于上述的各个vector类型的变量,如果如上述代码所示通过遍历设置各个vector内部的各个VectorXi的大小,然后通过下述代码中的方法,通过下标去访问各个vector<VectorXi>
类型的变量(regions和costs)并对它们进行赋值,就会出现上述下标越界的错误,也就是说先定义各个vector<VectorXi>
类型的变量的大小,然后通过下标对其进行访问,会出现下标越界的错误
for (int u = 0; u < p; u++) {
VectorXi costs_u = costs[u];
VectorXi regions_u = regions[u];
MatrixXd center = get<0>(P[u]);
int num = center.rows();
for (int v = 0; v < num; v++) {
costs_u(v) = MAXFLOAT;
regions_u(v) = -1;
}
}
解决办法是不用下标去访问各个vector类型的变量,而是先定义一些VectorXi变量对其进行赋值,然后用各个vector类型的变量去push_back已赋值的VectorXi变量,这样就避免了下标越界的问题:
for (int u = 0; u < p; u++) {
int num = sp_global_id.size();
VectorXd costs_u(num);
VectorXi regions_u(num);
for (int v = 0; v < num; v++) {
costs_u(v) = MAXFLOAT;
regions_u(v) = -1;
}
regions.push_back(regions_u);
}