0

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. The first level stores the different sets of control points (cp1..cp3). The second level stores x,y,z,w coordinate arrays. 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. For ctrlpts I first add in the appropriate coordinate vectors into the appropriate control point set. (ex: cpl.push_back(x1)) Then I push these coordinate sets into ctrlpts (ex: ctrlpts.push_back(cp1))

The error comes when I call 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.

Any ideas on what I am doing wrong? Perhaps it has to do with the way I am assembling this 3 dimensional vector?

EDIT: There is no specific error message. It just crashes.

EDIT 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.

Mike James Johnson
  • 724
  • 2
  • 8
  • 29
  • 3
    It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: **[How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)** – NathanOliver Jul 11 '16 at 18:01

1 Answers1

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:

So problem was fixed when I initially pushed the control point sets, cpi into ctrlpts then changed:

    cp1.push_back(x1);

to

ctrlpts[i].push_back(xi);

Mike James Johnson
  • 724
  • 2
  • 8
  • 29