gpt4 book ai didi

c++ - 读取 BSDF 数据格式

转载 作者:行者123 更新时间:2023-11-28 08:14:24 26 4
gpt4 key购买 nike

我被要求编写一个读取 BSDF data format defined by Zemax 的函数此类文件的示例可在以下页面找到:BSDF file example

如果可能的话,我想只使用标准的 ifstream 函数。

我已经在专用类中准备了所有必要的数据成员。

我现在正在尝试编写从文件中读取数据的函数。

问题:

  • 如何排除评论行?正如记录的那样,它们以哈希开头 # 我正在寻找类似

    的东西
    void 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();
    }
  • 以类似的方式,我想稍后验证我是否在正确的行(例如“AngleOfIncidence”行)。我可以这样做吗?

    string AngleInc;

    infile >> AngleInc;

    if (AngleInc != "AngleOfIncidence")
    {
    //error
    }

感谢任何评论/帮助的人。欢迎建设性的批评。

费德里科

编辑:

感谢下面的 Joachim Pileborg,我设法继续处理文件的数据 block 部分。

现在我有以下问题。到达数据 block 时,我编写了以下代码,但在第二次迭代 (i = 1) 时,我收到了 TIS 行的错误消息。有人可以帮我理解为什么这不起作用吗?谢谢

注意:blocks 是 AngleOfIncidence 线上的数字,rows 是 ScatterAzimuth 线上的数字,columns 是 ScatterRadial 上的数字。我测试并验证了这部分功能是否按预期工作。

// 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];
}
}
}

编辑 2:

解决了添加 infile.seekg(+2, ios_base::cur); 作为 i 循环的最后一行。

最佳答案

阅读循环可以这样简化:

std::string line;
while (getline(infile, line))
{
if (line[0] != '#')
{
// Not a comment, do something with the line
if (line.find("AngleOfIncidence") != std::string::npos)
{
// On the AngleOfIncidence line, do special things here
}
}
}

它可能不是最优的,只是写在我脑海中的一些东西,但应该有用。

关于c++ - 读取 BSDF 数据格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8122333/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com