void ObjModelLoader::parseMtl(fs::path file) { //TODO parse Mtl-File and put the materials into the (hash)map // within the model. // remember: 1 object = 1 material = 1 vbo // mööööp -wrong! if (!exists(file)) cerr << "Material-File '" << file.string() << "' does not exist!" << endl; Material* matPtr = 0; fs::ifstream mtlFile; mtlFile.open(file, ios::in); char line[200]; vector<string> tokens; while (mtlFile.getline(line, 200)) { tokens = splitSpace(string(line)); if (tokens[0] == ("newmtl")) { string longName(""); for (unsigned int i = 1; i < tokens.size(); ++i) { longName.append(tokens[i]); longName.append(" "); } longName.erase(longName.end() - 1); matPtr = new Material(); matPtr->kdR = ooctools::defaultColorF.getX(); matPtr->kdG = ooctools::defaultColorF.getY(); matPtr->kdB = ooctools::defaultColorF.getZ(); mPriMatMap.insert(make_pair(longName, matPtr)); // cout << "found MaterialDefinition '" << tokens[1] << "'!" << endl; } else if (!strcasecmp("kd", tokens[0].c_str())) { matPtr->kdR = atof(tokens[1].c_str()); matPtr->kdG = atof(tokens[2].c_str()); matPtr->kdB = atof(tokens[3].c_str()); } } mtlFile.close(); }
static libmaus::autoarray::AutoArray<libmaus::bambam::BamRange::unique_ptr_type> parse(std::string const & ranges, libmaus::bambam::BamHeader const & header) { std::vector < std::string > const outertokens = splitSpace(ranges); libmaus::autoarray::AutoArray<libmaus::bambam::BamRange::unique_ptr_type> A(outertokens.size()); for ( uint64_t i = 0; i < outertokens.size(); ++i ) { std::string const & outertoken = outertokens[i]; uint64_t sempos = outertoken.size(); for ( uint64_t j = 0; j < outertoken.size(); ++j ) if ( outertoken[j] == ':' ) sempos = j; if ( sempos == outertoken.size() ) { libmaus::bambam::BamRange::unique_ptr_type tAi(new libmaus::bambam::BamRangeChromosome(outertoken,header)); A[i] = UNIQUE_PTR_MOVE(tAi); } else { std::string const refname = outertoken.substr(0,sempos); std::string const rest = outertoken.substr(sempos+1); // std::cerr << "refname=" << refname << " rest=" << rest << std::endl; uint64_t dashpos = rest.size(); for ( uint64_t j = 0; j < rest.size(); ++j ) if ( rest[j] == '-' ) dashpos = j; if ( dashpos == rest.size() ) { int64_t num = 0; for ( uint64_t j = 0; j < rest.size(); ++j ) if ( isdigit(rest[j]) ) { num *= 10; num += rest[j]-'0'; } else if ( rest[j] == ',' ) { } else { libmaus::exception::LibMausException se; se.getStream() << "Found invalid range character in " << rest << std::endl; se.finish(); throw se; } libmaus::bambam::BamRange::unique_ptr_type tAi(new libmaus::bambam::BamRangeHalfOpen(refname,num-1,header)); A[i] = UNIQUE_PTR_MOVE(tAi); } else { std::string const sstart = rest.substr(0,dashpos); std::string const send = rest.substr(dashpos+1); int64_t start = 0; for ( uint64_t j = 0; j < sstart.size(); ++j ) if ( isdigit(sstart[j]) ) { start *= 10; start += sstart[j]-'0'; } else if ( sstart[j] == ',' ) { } else { libmaus::exception::LibMausException se; se.getStream() << "Found invalid range character in " << sstart << std::endl; se.finish(); throw se; } int64_t end = 0; for ( uint64_t j = 0; j < send.size(); ++j ) if ( isdigit(send[j]) ) { end *= 10; end += send[j]-'0'; } else if ( send[j] == ',' ) { } else { libmaus::exception::LibMausException se; se.getStream() << "Found invalid range character in " << send << std::endl; se.finish(); throw se; } libmaus::bambam::BamRange::unique_ptr_type tAi(new libmaus::bambam::BamRangeInterval(refname,start-1,end,header)); A[i] = UNIQUE_PTR_MOVE(tAi); } } } return A; }
static std::vector < std::string > splitSpace(std::string const & s) { libmaus::fastx::SpaceTable const ST; return splitSpace(s,ST); }
void ObjModelLoader::secondPass() { string lastType = ""; int grpId = -1; mPriModelPtr->setCurrentGrp(mPriModelPtr->getGrpStart()->first); fs::path my_path(mPriFName); fs::ifstream objFile; objFile.open(my_path, ios::out); char line[200]; vector<string> tokens; while (objFile.getline(line, 200)) { tokens = splitSpace(string(line)); if (tokens[0] == ("g")) { // removeSpecialCharsFromName(tokens[1]); ++grpId; lastType = "g"; string longname(""); for (unsigned int i = 1; i < tokens.size(); ++i) { longname.append(tokens[i]); longname.append("_"); } longname.erase(longname.end() - 1); mPriModelPtr->setCurrentGrp(longname); //cout << _model.getCurrentGrpPtr()->name << endl; } else if (tokens[0] == ("v")) { lastType = "v"; } // moved material assignment to 2nd pass because it references a group. // But only at end of 1st pass we know our groups else if (tokens[0] == ("usemtl")) { // cout << "found material reference " << tokens[1] << " in obj-file" << endl; string longName(""); for (unsigned int i = 1; i < tokens.size(); ++i) { longName.append(tokens[i]); longName.append(" "); } longName.erase(longName.end() - 1); if (mPriMatMap.find(longName) != mPriMatMap.end()) mPriModelPtr->getCurrentGrpPtr()->setMat(*mPriMatMap[longName]); lastType = "usemtl"; } else if (tokens[0] == ("f")) { //cout << "Number of Components per face: " << tokens.size()-1 << endl; Face* f = new Face(); f->norm = false; f->vert = false; f->matIdx = 0; f->tex = 0; f->fNormal = 0; string::size_type loc = tokens[1].find("/", 0); if (loc != string::npos) { for (string::size_type i = 1; i < tokens.size(); ++i) { vector<int> comp = extractFaceComponents(tokens[i]); // vertices if ((comp[0] & 4)) { V3f* vtx = mPriModelPtr->getVPtr(comp[1] - 1); f->vertexPtrList.push_back(vtx); vtx->addFaceRef(f); f->vert = true; mPriModelPtr->getCurrentGrpPtr()->nVertices++; mPriModelPtr->incVCount(); mPriModelPtr->getCurrentGrpPtr()->bb->expand(*vtx); } // textures if ((comp[0] & 2)) { f->texturePtrList.push_back(mPriModelPtr->getTPtr( comp[2] - 1)); ++f->tex; mPriModelPtr->getCurrentGrpPtr()->nTextureCoords++; mPriModelPtr->incTCount(); } // normals if ((comp[0] & 1)) { f->normalPtrList.push_back(mPriModelPtr->getNPtr( comp[3] - 1)); f->norm = true; mPriModelPtr->getCurrentGrpPtr()->nNormals++; mPriModelPtr->incNCount(); } comp.clear(); } } else { V3f* vtx = mPriModelPtr->getVPtr(atoi(tokens[1].c_str()) - 1); f->vertexPtrList.push_back(vtx); mPriModelPtr->getCurrentGrpPtr()->bb->expand(*vtx); vtx = mPriModelPtr->getVPtr(atoi(tokens[2].c_str()) - 1); f->vertexPtrList.push_back(vtx); mPriModelPtr->getCurrentGrpPtr()->bb->expand(*vtx); vtx = mPriModelPtr->getVPtr(atoi(tokens[3].c_str()) - 1); f->vertexPtrList.push_back(vtx); mPriModelPtr->getCurrentGrpPtr()->bb->expand(*vtx); f->vert = true; mPriModelPtr->getCurrentGrpPtr()->nVertices += 3; mPriModelPtr->incVCount(3); } mPriModelPtr->addFPtrToCurrent(f); lastType = "f"; } } objFile.close(); }
void ObjModelLoader::firstPass() { string lastType = ""; fs::path my_path(mPriFName); fs::ifstream objFile; objFile.open(my_path, ios::in); char line[200]; vector<string> tokens; while (objFile.getline(line, 200)) { tokens = splitSpace(string(line)); if (tokens[0] == ("o")) { //++grpCount; lastType = "o"; } else if (tokens[0] == ("g")) { // removeSpecialCharsFromName(tokens[1]); string longname(""); for (unsigned int i = 1; i < tokens.size(); ++i) { longname.append(tokens[i]); longname.append("_"); } longname.erase(longname.end() - 1); mPriModelPtr->addGrpPtr(new MetaGroup(longname)); lastType = "g"; } else if (tokens[0] == ("mtllib")) { string longname(""); for (unsigned int i = 1; i < tokens.size(); ++i) { longname.append(tokens[i]); longname.append(" "); } longname.erase(longname.end() - 1); parseMtl(fs::path(my_path.parent_path() / longname)); lastType = "mtllib"; } else if (tokens[0] == ("v")) { V3f* v = new V3f(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str())); mPriModelPtr->addVPtr(v); lastType = "v"; } else if (tokens[0] == ("vt")) { V3f* t = new V3f(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str())); mPriModelPtr->addTPtr(t); lastType = "vt"; } else if (tokens[0] == ("vn")) { V3b *n = new V3b((char) 127.0f * atof(tokens[1].c_str()), (char) 127.0f * atof(tokens[2].c_str()), (char) 127.0f * atof(tokens[3].c_str())); mPriModelPtr->addNPtr(n); lastType = "vn"; } else if (tokens[0] == ("f")) { lastType = "f"; } } objFile.close(); // if we have not read any groups in, create a default group if (mPriModelPtr->getGrpCount() < 1) { mPriModelPtr->addGrpPtr(new MetaGroup()); } }