具有ubuntu问题的C ++中的OBJ文件

时间:2018-09-20 02:01:37

标签: c++ ubuntu-18.04 .obj

我在读取OBJ文件时遇到问题... 问题1是向量被声明为浮点数(并已被声明为双精度数),但是在终端中打印出所有整数。 终端输出为:

    1-1-1
    1-11
    -1-11
    -1-1-1

应为:

     1.000000 -1.000000 -1.000000
     1.000000 -1.000000 1.000000
     -1.000000 -1.000000 1.000000
     -1.000000 -1.000000 -1.000000

问题2是我无法正确读取文件中的面孔。输出为:

    1
    5
    1
    2

应该在什么时候出现:

    1 2 3 4
    5 8 7 6
    1 5 6 2
    2 6 7 3

我正尝试使结果现在可以正确打印出来,因为由于它们的大小不同,接下来我将把这些结果放入使用动态内存分配的节点中。 这是代码:

/* This function opens the OBJ file */
void openFile(string nf){   
std::ifstream file;
file.open (nf, std::ifstream::out);

string line;
string o_name;
string faces;
char o;
char v;
char f;
int i = 0;
float x;
float y;
float z;

if(file.is_open()){
std::cout<< "file is open!\n";
//include actions for OBJ file...Just read. You work from memory
    while(std::getline(file, line))
    {
        if(line.empty())        //line is empty, then file is empty!
            std::cout << "file is empty! \n";
        /* finds the shape name*/
        if (line[i] == 'o')
        {
            std::istringstream iss (line);
            iss >> o >> o_name;
            std::cout << o_name << endl;
            line.clear();
            //createNodeO(string o_name);
        }
        /* finds the vertices*/             
        if (line[i] == 'v')
        {
            std::istringstream iss (line);
            iss >> v >> x >> y >> z;
            std::cout << x << y << z << endl;
            line.clear();
            //createNodeO(float x, float y, float z);
        }

        /* finds the faces*/                        
        if (line[i] == 'f')
        {
            std::istringstream iss (line);
            iss >> f >> faces;
            std::cout << faces << endl;
            //createNodeO(string faces);
        }
    }
}
else{
    std::cout<< "could not open the file!\n";
}
file.close();
std::cout<< "file is closed!\n";    

}

根据g ++编译器,当涉及到面部部分时,我很疯狂。请帮忙!

1 个答案:

答案 0 :(得分:0)

这是我的做法。如果您愿意,也可以对每种类型使用operator>>

与您的示例的区别:

我忽略除了顶点和面之外的所有内容,并假设顶点中没有w,文件中没有vt,vn,vp等。您可能还会遇到名称,组,材料等。如果您发现自己烦恼不已,或者想要编写解析器的欲望减弱了,您可以签出一个库来做。 http://www.assimp.org/是一个不错的选择,http://www.open3mod.com/这里有一个不错的查看器,如果您解析的内容看起来不太正确,肯定可以提供帮助。

您的代码正在标记化并直接写入目标变量,我选择先标记化,然后在对该行进行一些基本验证后将标记转换为正确的类型。无论哪种。我通常会按照您的方式进行操作,也许使用operator>>或辅助函数。

我用以下文件中的一些文件进行了测试:https://people.sc.fsu.edu/~jburkardt/data/obj/obj.html

希望这会有所帮助。

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

struct vertex
{
    double x, y, z;
};

struct face
{
    std::vector<int> v;
};

struct object
{
    std::vector<vertex> verts;
    std::vector<face> faces;
};

std::ostream& operator<<(std::ostream& out, const vertex& v)
{
    out << "v " 
        << std::fixed << std::setprecision(6) << v.x << " "
        << std::fixed << std::setprecision(6) << v.y << " "
        << std::fixed << std::setprecision(6) << v.z;
    return out;
}

std::ostream& operator<<(std::ostream& out, const face& f)
{
    out << "f";
    for (size_t i = 0; i < f.v.size(); ++i)
    {
        out << " " << f.v[i];
    }
    return out;
}

std::ostream& operator<<(std::ostream& out, const object& o)
{
    for (size_t i = 0; i < o.verts.size(); ++i)
    {
        out << o.verts[i] << "\n";
    }
    for (size_t i = 0; i < o.faces.size(); ++i)
    {
        out << o.faces[i] << "\n";
    }
    return out;
}

std::vector<std::string> split(const std::string& s)
{
    std::vector<std::string> ret;
    std::string token;
    std::stringstream ss(s);
    while (ss >> token)
    {
        ret.push_back(token);
    }
    return ret;
}

int main(int argc, char **argv)
{
    if (argc != 2)
    {
        std::cerr << "Usage: " << argv[0] << " <obj filename>\n";
        return -1;
    }

    std::fstream in(argv[1]);
    if (!in)
    {
        std::cerr << "File: " << argv[1] << " could not be opened\n";
        return -1;
    }

    object o;
    std::string line;
    int lineNumber = 0;
    while (std::getline(in, line))
    {
        ++lineNumber;

        if (line.empty() || line[0] == '#')
        {
            continue;
        }

        std::vector<std::string> tokens = split(line);
        if (line[0] == 'v')
        {
            if (tokens.size() < 4)
            {
                std::cerr << "Invalid vert: '" << line << "' on line: " << lineNumber << "\n";
                continue;
            }

            vertex v;
            v.x = std::stod(tokens[1]);
            v.y = std::stod(tokens[2]);
            v.z = std::stod(tokens[3]);
            o.verts.push_back(v);
        }
        else if (line[0] == 'f')
        {
            if (tokens.size() < 4)
            {
                std::cerr << "Invalid face: '" << line << "' on line: " << lineNumber << "\n";
                continue;
            }

            face f;
            for (size_t i = 1; i < tokens.size(); ++i)
            {
                f.v.push_back(std::stoi(tokens[i]));
            }
            o.faces.push_back(f);
        }
    }

    std::cout << o;
    return 0;
}