简体   繁体   English

3维向量的索引不正确

[英]Improper indexing of 3 dimensional vector

Here is my function. 这是我的功能。 Basically it reads a text file and organizes data into a bunch of different vectors. 基本上,它读取文本文件并将数据组织成一堆不同的向量。

ctrlpts is a 3-dimensional vector. ctrlpts是3维向量。 The first level stores the different sets of control points (cp1..cp3). 第一层存储不同组的控制点(cp1..cp3)。 The second level stores x,y,z,w coordinate arrays. 第二层存储x,y,z,w坐标数组。 And, therefore, the third level represents the entry within the coordinate array. 因此,第三级代表坐标数组中的条目。

In the function below, I first initialize all the vectors. 在下面的函数中,我首先初始化所有向量。 Then depending on the dimension ( int d ), and number of knot vectors ( int k ) I build the multidimensional vectors accordingly. 然后,根据维数( int d )和结矢量( int k )的数量,我相应地构建多维矢量。 For ctrlpts I first add in the appropriate coordinate vectors into the appropriate control point set. 对于ctrlpts我首先将适当的坐标矢量添加到适当的控制点集中。 (ex: cpl.push_back(x1) ) Then I push these coordinate sets into ctrlpts (ex: ctrlpts.push_back(cp1) ) (例如: cpl.push_back(x1) )然后将这些坐标集推入ctrlpts (例如: ctrlpts.push_back(cp1)

The error comes when I call ctrlpts[0][0][0] . 当我调用ctrlpts[0][0][0]时出现错误。

void createNurb(string filename){

ifstream file;
file.open(filename.c_str());

if(file.fail()){
    cout << "Cannot open " << filename << endl;
}

vector<float> knots1;
vector<float> knots2;
vector<float> knots3;
vector< vector<float> > knotvectors;

vector<float> x1;
vector<float> y1;
vector<float> z1;
vector<float> w1;
vector<float> x2;
vector<float> y2;
vector<float> z2;
vector<float> w2;
vector<float> x3;
vector<float> y3;
vector<float> z3;
vector<float> w3;
vector< vector<float> > cp1;
vector< vector<float> > cp2;
vector< vector<float> > cp3;
vector< vector< vector<float> > > ctrlpts;

string line;
getline(file,line);
int d = line[0] - 48;
getline(file,line);
int k= line[0] - 48;

if(k==1){
    knotvectors.push_back(knots1);
    if(d==1){
        cp1.push_back(x1);
        cp1.push_back(w1);
    }else if(d==2){
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(w1);
    }else{
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(z1);
        cp1.push_back(w1);
    }
    ctrlpts.push_back(cp1);


}else if(k==2){
    knotvectors.push_back(knots1);
    knotvectors.push_back(knots2);
    if(d==1){
        cp1.push_back(x1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(w2);
    }else if(d==2){
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(y2);
        cp2.push_back(w2);
    }else{
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(z1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(y2);
        cp2.push_back(z2);
        cp2.push_back(w2);
    }
    ctrlpts.push_back(cp1);
    ctrlpts.push_back(cp2);

}else{
    knotvectors.push_back(knots1);
    knotvectors.push_back(knots2);
    knotvectors.push_back(knots3);
    if(d==1){
        cp1.push_back(x1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(w2);
        cp2.push_back(x3);
        cp2.push_back(w3);
    }else if(d==2){
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(y2);
        cp2.push_back(w2);
        cp3.push_back(x3);
        cp3.push_back(y3);
        cp3.push_back(w3);
    }else{
        cp1.push_back(x1);
        cp1.push_back(y1);
        cp1.push_back(z1);
        cp1.push_back(w1);
        cp2.push_back(x2);
        cp2.push_back(y2);
        cp2.push_back(z2);
        cp2.push_back(w2);
        cp3.push_back(x3);
        cp3.push_back(y3);
        cp3.push_back(z3);
        cp3.push_back(w3);
    }
    ctrlpts.push_back(cp1);
    ctrlpts.push_back(cp2);
    ctrlpts.push_back(cp3);

}


for(int i=0; i<k; i++){

    getline(file,line);
    std::istringstream iss(line);
    std::copy(std::istream_iterator<float>(iss),
    std::istream_iterator<float>(),
    std::back_inserter(knotvectors[i]));

    for (int j=0; j<=d; j++){
        getline(file,line);
        std::copy(std::istream_iterator<float>(iss),
        std::istream_iterator<float>(),
        std::back_inserter(ctrlpts[i][j]));
    }
}

    cout<< ctrlpts[0][0][0] << endl;


file.close();

} }

everything works if i call knotvectors[0][0] so I am unsure what I am doing wrong in referencing the individual cells of the 3 dimensional ctrlpts vector. 如果我调用knotvectors[0][0] ,一切都会正常,所以我不确定在引用3维ctrlpts向量的单个单元格时我做错了什么。

Any ideas on what I am doing wrong? 关于我在做什么错的任何想法吗? Perhaps it has to do with the way I am assembling this 3 dimensional vector? 也许与我组装此3维矢量的方式有关?

EDIT: There is no specific error message. 编辑:没有特定的错误信息。 It just crashes. 它只是崩溃。

EDIT 2: 编辑2:

I believe the problem could be here: 我相信问题可能在这里:

        for (int j=0; j<=d; j++){
        getline(file,line);
        std::copy(std::istream_iterator<float>(iss),
        std::istream_iterator<float>(),
        std::back_inserter(ctrlpts[i][j]));

If i print out: ctrlpts[0][0].size() then it says 0. Therefore, I beleive the block above is for some reason not feeding in the data into the vector. 如果我打印出来: ctrlpts[0][0].size()则说为0。因此,我认为上面的块由于某种原因没有将数据输入向量中。

Figured out the problem. 找出问题所在。

If you have vectors A, B, and C. If push B into A, then push C into B, you cannot directly push numbers into C and access them via A. instead everything has to be dealt with through A: 如果具有向量A,B和C。如果将B推入A,然后将C推入B,则不能直接将数字推入C并通过A来访问它们。相反,所有事情都必须通过A处理:

So problem was fixed when I initially pushed the control point sets, cpi into ctrlpts then changed: 因此,当我最初将控制点集(将cpi转换为ctrlpts然后将其更改时,问题已解决:

    cp1.push_back(x1);

to

ctrlpts[i].push_back(xi);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM