示例#1
0
文件: mesh.c 项目: gbbofh/objread
/* ========================================
 * void create_mesh(obj_file* objdata, meshdata* mesh)
 * ========================================
 * Parses the vertex and face data for a given object file
 * and fills out a meshdata struct.
*/
void create_mesh(obj_file* objdata, meshdata* mesh) {
    if(mesh == NULL || objdata == NULL) {
        msg_log(LOG_ERROR, "create_mesh: Bad parameters!");
    }
    memset(mesh, 0, sizeof(meshdata));
    parse_faces(&objdata->faces, mesh);
}
示例#2
0
bool MEDITParser::parse(const std::string& filename) {
    std::ifstream fin(filename.c_str());
    if (!fin.is_open()) {
        std::stringstream err_msg;
        err_msg << "failed to open file \"" << filename << "\"";
        throw IOError(err_msg.str());
    }

    bool success = true;
    success &= parse_header(fin);
    if (!success) {
        throw IOError("Error parsing file header.");
    }

    const size_t LINE_SIZE = 256;
    char line[LINE_SIZE];
    while (!fin.eof()) {
        std::string elem_type;
        fin >> elem_type;
        if (elem_type.size() == 0) continue;
        if (elem_type[0] == '#') {
            fin.getline(line, LINE_SIZE);
            continue;
        }
        if (elem_type == "Vertices") {
            success &= parse_vertices(fin);
        } else if (elem_type == "Triangles") {
            m_vertex_per_face = 3;
            success &= parse_faces(fin);
        } else if (elem_type == "Quadrilaterals") {
            m_vertex_per_face = 4;
            success &= parse_faces(fin);
        } else if (elem_type == "Tetrahedra") {
            if (m_faces.size() == 0) {
                m_vertex_per_face = 3;
            } else if (m_vertex_per_face != 3){
                success = false;
            }
            m_vertex_per_voxel = 4;
            success &= parse_voxels(fin);
        } else if (elem_type == "Hexahedra") {
            if (m_faces.size() == 0) {
                m_vertex_per_face = 4;
            } else if (m_vertex_per_face != 4){
                success = false;
            }
            m_vertex_per_voxel = 8;
            success &= parse_voxels(fin);
        } else if (elem_type == "End") {
            break;
        } else {
            if (elem_type != "Edges" &&
                elem_type != "Corners" &&
                elem_type != "RequiredVertices" &&
                elem_type != "Ridges" &&
                elem_type != "RequiredEdges" &&
                elem_type != "Normals" &&
                elem_type != "Tangents" &&
                elem_type != "NormalAtVertices" &&
                elem_type != "NormalAtTriangleVertices" &&
                elem_type != "NormalAtQuadrilateralVertices" &&
                elem_type != "TangentAtEdges" &&
                elem_type != "End") {
                success = false;
            } else {
                std::cerr << "Warning: Skipping " << elem_type
                    << " field" << std::endl;
                success &= skip_field(fin);
            }
        }
        if (!success) {
            std::stringstream err_msg;
            err_msg << "Error parsing \"" << elem_type << "\" field in " <<
                filename;
            throw IOError(err_msg.str());
        }
    }

    fin.close();
    return success;
}