Hello all, I have a text file that has multiple geometries in it that I'm trying to parse. The issues is that they are delimited differently. I am looking for an elegant way to parse the following data set.
This is what I have so far, which reads the geometry that is delimited by white space perfectly. However, I can't figure out an elegant solution to parsing the "f" data. I thought of exploiting getline() to include a delimiter "/", but that isn't working properly since the mixed configuration in the file.
The following code works well for the numbers that are white-space separated, but after multiple attempts, I haven't found an elegant solution to parsing the "f" data.
Your code will be similar to what you have for the first two patterns. The data for the "f" lines appear to have three columns of three numbers each delimited by a forward slash character.
What is exactly the problem you are trying to solve? Are you also trying to process the three columns, or are you first trying to store them into a vector? Did I miss where you have the array V defined?
I've included some of the global variable declarations. This is a small snippet of the code, but in context.. I'm trying to learn openGL (and C++ simultaneously) the goal is to read in object files (in this case, a list of vertices, tangents, normals, and faces) and store it in vectors vecv (vertices), vecn (normals) vecf(faces).
Note, Vector3f is a vector class that is more of a mathematical description of a vector (contains 3 coords, xyz... dot products, cross products etc...)
Thanks, that is a little more helpful. Without knowing how the ">>" operator is overloaded in Vector3f, it's hard to know what is happening. Perhaps the documentation could provide a clue?
void loadInput()
{
// load the OBJ file here
char delimiter;
//counter variable to handle delimiter locations (2 delimiters per set of 3 numbers)
unsigned z=0;
//temporary storage for appends
unsigned temp;
vector<unsigned> V1;
Vector3f V;
//string storage for getline and parsing
string buffer;
string s;
while(getline(cin, buffer)){
stringstream ss(buffer); // create ss object
ss >> s; // store first entry (always a character identifier (v= vertex, vn = normal, f = face)
if(s=="v"){
ss >> V[0] >> V[1] >> V[2];
cout << "V = " << V[0] << "," << V[1] << "," << V[2] <<"\n";
vecv.push_back(V);
}
elseif(s=="vn"){
ss >> V[0] >> V[1] >> V[2];
cout << "VN = " << V[0] << "," << V[1] << "," << V[2] <<"\n";
vecn.push_back(V);
}
elseif (s == "f"){
while(ss >> temp){ // For an arbitrary number of faces (goes until end of line)
V1.push_back(temp); // append all face data to vector
if(z!=2){ // z cycles between [0,1,2], on 3rd cycle there is no delimiter. Data format : a/b/c
ss >> delimiter; //only parse delimiter for first two cycles
}
z = (z+1)%3; // increment z
}
vecf.push_back(V1); // 2-D vector of face data
}
}
}
And to answer your questions
1) I inherited "skeleton code" from an online course I'm following along with (MIT OCW) to learn openGL, and that's the way it was structured.
2) Great point, the assignment gave a suggestion, which I followed using C-strings. I've since switched to your suggestion