I have been required to write a function that reads the BSDF data format defined by Zemax
An example of such file can be found at the following page: BSDF file example
I would like to use, if possible, only standard ifstream functions.
I have already prepared all the necessary datamembers inside a dedicated class.
I am now trying to write the function that reads the data from the file.
Problems:
-
how do I exclude comment lines? as documented, they start with an hash
#I was going for something likevoid ReadBSDFFile(myclass &object) { ifstream infile; infile.open(object.BRDFfilename); char c; infile.get(c); while (c == "#") // Problem, apparently I cannot compare in this way. How should I do it? { getline(infile, line); infile.get(c); } // at this point I would like to go back one character (because I do not want to lose the non-hash character that ended up in *c*) infile.seekg(-1, ios_base::cur); // Do all the rest infile.close(); } -
in a similar way, I would like to verify that I am at the correct line later on (e.g. the “AngleOfIncidence” line). Could I do it in this way?
string AngleInc; infile >> AngleInc; if (AngleInc != "AngleOfIncidence") { //error }
Thanks to anyone who will comment/help. Constructive criticism is welcomed.
Federico
EDIT:
Thanks to Joachim Pileborg below, I managed to proceed up to the data blocks part of the file.
Now I have the following problem. When reaching the datablocks, I wrote the following piece of code, but at the second iteration (i = 1) i receive the error message for the TIS line.
Could someone help me understand why this does not work?
Thanks
Note: blocks is the number on the AngleOfIncidence line, rows the one on the ScatterAzimuth line and columns the one on the ScatterRadial. I tested and verified that this part of the function works as desired.
// now reading the data blocks.
for (int i=0; i<blocks; i++)
{
// TIS line
getline(infile, line);
if (line.find("TIS") == string::npos)
{
// if not, error message
}
// Data block
for (int j=0; j<rows; j++)
{
for (int k=0; k<columns; k++)
{
infile >> object.BRDFData[i][j][k];
}
}
}
EDIT 2:
solved adding infile.seekg(+2, ios_base::cur); as a last line of the i loop.
The reading loop could be simplified like this:
It’s might not be optimal, just something written at the top of my head, but should work.