bool ObjFile::_parse(std::istream& is) { // Clear any previous garbage mObjObjects.clear(); // Unload binary mesh content just in case unload(); // Reference to non existing obj, until an obj is defined in the file mObjObjects.emplace_back(); ObjObject* obj = &mObjObjects[0]; obj->name = "Undefined"; bool firstObject = true; // Reference to non existing obj, until an obj is defined in the file obj->materials.emplace_back(); ObjMaterial* mat = &obj->materials[0]; mat->name = "Undefined"; bool firstMaterial = true; uint32 pOffset = 0, tOffset = 0, nOffset = 0; std::string line, token; while (!is.eof()) { std::getline(is, line, '\n'); std::stringstream lstream(line); lstream >> token; if (token == "o") { // Grab the obj name lstream >> token; // If there is an undefined obj to use // just use that otherwise make a new one if (firstObject) { obj->name = token; firstObject = false; } else { pOffset += obj->positions.size(); tOffset += obj->texcoords.size(); nOffset += obj->normals.size(); size_t s = mObjObjects.size(); mObjObjects.emplace_back(); obj = &mObjObjects[s]; obj->name = token; // New object so make a new place to put the indices // Undefined material for now until a usemtl shows up obj->materials.resize(1); mat = &obj->materials[0]; mat->name = "Undefined"; firstMaterial = true; } } else if (token == "usemtl")
//Function to read a line from in containing the difficulty and 7 scores //The 7 scores should be stored in d and difficulty is stored in dif //White spaces in the end of the line should be ignored //If any errors in the line then return false //Otherwise, return true //Use stringstreams, see sec. 11.9 of course book and Fö 2 bool read_line(istream& in, double d[], double &dif) { int scores = 0; double tmp; string line; getline(in, line); istringstream lstream(line); lstream >> dif; while ( (lstream >> tmp) && (scores < NR_OF_REF) ) { d[scores++] = tmp; } //Skip whitespaces lstream >> ws; if (lstream.eof()) { return true; } else { return false; } }
void Geometry::parseObjFile(const std::string filePath, std::vector<GLfloat>& vboData, std::vector<GLushort>& iboData) { //temporary save values in arrays std::vector<glm::vec3> vertices; std::vector<glm::vec3> normals; //save which data vbo already contains std::list<IndexCombination> combinations; //save next vbo index to use GLushort nextVboIndex = 0; //open file std::ifstream objFile; objFile.exceptions(std::ifstream::badbit | std::ifstream::failbit); objFile.open(filePath); std::string line; while (!objFile.eof()) { try { std::getline(objFile, line); } catch (const std::exception &ex) { if (!objFile.eof()) { throw ex; } else { continue; } } std::istringstream lstream(line); std::string type; lstream >> type; if (type.compare("v") == 0) { parseVertex(lstream, vertices); } else if (type.compare("vn") == 0) { parseNormal(lstream, normals); } else if (type.compare("f") == 0) { parseFace(lstream, vboData, iboData, vertices, normals, &nextVboIndex, combinations); } else if (type.compare("s") == 0) { //ignore line } else if (type.compare("o") == 0) { //ignore line } else if (type.compare("#") == 0) { //ignore line } else { throw std::logic_error("Unknown start of line"); } } objFile.close(); }
void insert_from_2column_file_with_filter(const std::string& filename, const XYFilter& xy_filter) { std::ifstream stream(filename.c_str()); if(!stream.good()) throw std::runtime_error("Could not open: "+filename); auto* file_record = calin::provenance::chronicle::register_file_open(filename, calin::ix::provenance::chronicle::AT_READ, __PRETTY_FUNCTION__); std::string comment; std::string line; std::getline(stream,line); while((line.empty() or line[0] == '#') and stream) { if(line[0] == '#') { comment += line; comment += '\n'; } std::getline(stream,line); } if(!stream)return; do { double x; double y; std::istringstream lstream(line); lstream >> x >> y; if(lstream) { if(xy_filter(x,y)) { m_xy.emplace_back(x, y); } } std::getline(stream,line); }while(stream); std::sort(m_xy.begin(), m_xy.end()); file_record->set_comment(comment); calin::provenance::chronicle::register_file_close(file_record); }