bool LoadOFF(const string & fileName, vector< float > & points, vector< int> & triangles, IVHACD::IUserLogger & logger) { FILE * fid = fopen(fileName.c_str(), "r"); if (fid) { const string strOFF("OFF"); char temp[1024]; fscanf(fid, "%s", temp); if (string(temp) != strOFF) { logger.Log("Loading error: format not recognized \n"); fclose(fid); return false; } else { int nv = 0; int nf = 0; int ne = 0; fscanf(fid, "%i", &nv); fscanf(fid, "%i", &nf); fscanf(fid, "%i", &ne); points.resize(nv*3); triangles.resize(nf*3); const int np = nv * 3; for (int p = 0; p < np; p++) { fscanf(fid, "%f", &(points[p])); } int s; for (int t = 0, r = 0; t < nf; ++t) { fscanf(fid, "%i", &s); if (s == 3) { fscanf(fid, "%i", &(triangles[r++])); fscanf(fid, "%i", &(triangles[r++])); fscanf(fid, "%i", &(triangles[r++])); } else // Fix me: support only triangular meshes { for (int h = 0; h < s; ++h) fscanf(fid, "%i", &s); } } fclose(fid); } } else { logger.Log("Loading error: file not found \n"); return false; } return true; }
bool SaveOBJ(ofstream& fout, const double* const& points, const int* const& triangles, const unsigned int& nPoints, const unsigned int& nTriangles, const Material& material, IVHACD::IUserLogger& logger, int convexPart, int vertexOffset) { if (fout.is_open()) { fout.setf(std::ios::fixed, std::ios::floatfield); fout.setf(std::ios::showpoint); fout.precision(6); size_t nV = nPoints * 3; size_t nT = nTriangles * 3; fout << "o convex_" << convexPart << std::endl; if (nV > 0) { for (size_t v = 0; v < nV; v += 3) { fout << "v " << points[v + 0] << " " << points[v + 1] << " " << points[v + 2] << std::endl; } } if (nT > 0) { for (size_t f = 0; f < nT; f += 3) { fout << "f " << triangles[f + 0]+vertexOffset << " " << triangles[f + 1]+vertexOffset << " " << triangles[f + 2]+vertexOffset << " " << std::endl; } } return true; } else { logger.Log("Can't open file\n"); return false; } }
bool SaveOFF(const string& fileName, const float* const& points, const int* const& triangles, const unsigned int& nPoints, const unsigned int& nTriangles, IVHACD::IUserLogger& logger) { ofstream fout(fileName.c_str()); if (fout.is_open()) { size_t nV = nPoints * 3; size_t nT = nTriangles * 3; fout << "OFF" << std::endl; fout << nPoints << " " << nTriangles << " " << 0 << std::endl; for (size_t v = 0; v < nV; v += 3) { fout << points[v + 0] << " " << points[v + 1] << " " << points[v + 2] << std::endl; } for (size_t f = 0; f < nT; f += 3) { fout << "3 " << triangles[f + 0] << " " << triangles[f + 1] << " " << triangles[f + 2] << std::endl; } fout.close(); return true; } else { logger.Log("Can't open file\n"); return false; } }
bool SaveVRML2(ofstream& fout, const double* const& points, const int* const& triangles, const unsigned int& nPoints, const unsigned int& nTriangles, const Material& material, IVHACD::IUserLogger& logger) { if (fout.is_open()) { fout.setf(std::ios::fixed, std::ios::floatfield); fout.setf(std::ios::showpoint); fout.precision(6); size_t nV = nPoints * 3; size_t nT = nTriangles * 3; fout << "#VRML V2.0 utf8" << std::endl; fout << "" << std::endl; fout << "# Vertices: " << nPoints << std::endl; fout << "# Triangles: " << nTriangles << std::endl; fout << "" << std::endl; fout << "Group {" << std::endl; fout << " children [" << std::endl; fout << " Shape {" << std::endl; fout << " appearance Appearance {" << std::endl; fout << " material Material {" << std::endl; fout << " diffuseColor " << material.m_diffuseColor[0] << " " << material.m_diffuseColor[1] << " " << material.m_diffuseColor[2] << std::endl; fout << " ambientIntensity " << material.m_ambientIntensity << std::endl; fout << " specularColor " << material.m_specularColor[0] << " " << material.m_specularColor[1] << " " << material.m_specularColor[2] << std::endl; fout << " emissiveColor " << material.m_emissiveColor[0] << " " << material.m_emissiveColor[1] << " " << material.m_emissiveColor[2] << std::endl; fout << " shininess " << material.m_shininess << std::endl; fout << " transparency " << material.m_transparency << std::endl; fout << " }" << std::endl; fout << " }" << std::endl; fout << " geometry IndexedFaceSet {" << std::endl; fout << " ccw TRUE" << std::endl; fout << " solid TRUE" << std::endl; fout << " convex TRUE" << std::endl; if (nV > 0) { fout << " coord DEF co Coordinate {" << std::endl; fout << " point [" << std::endl; for (size_t v = 0; v < nV; v += 3) { fout << " " << points[v + 0] << " " << points[v + 1] << " " << points[v + 2] << "," << std::endl; } fout << " ]" << std::endl; fout << " }" << std::endl; } if (nT > 0) { fout << " coordIndex [ " << std::endl; for (size_t f = 0; f < nT; f += 3) { fout << " " << triangles[f + 0] << ", " << triangles[f + 1] << ", " << triangles[f + 2] << ", -1," << std::endl; } fout << " ]" << std::endl; } fout << " }" << std::endl; fout << " }" << std::endl; fout << " ]" << std::endl; fout << "}" << std::endl; return true; } else { logger.Log("Can't open file\n"); return false; } }
bool LoadOBJ(const string& fileName, vector<float>& points, vector<int>& triangles, IVHACD::IUserLogger& logger) { const unsigned int BufferSize = 1024; FILE* fid = fopen(fileName.c_str(), "r"); if (fid) { char buffer[BufferSize]; int ip[4]; float x[3]; char* pch; char* str; while (!feof(fid)) { if (!fgets(buffer, BufferSize, fid)) { break; } else if (buffer[0] == 'v') { if (buffer[1] == ' ') { str = buffer + 2; for (int k = 0; k < 3; ++k) { pch = strtok(str, " "); if (pch) x[k] = (float)atof(pch); else { return false; } str = NULL; } points.push_back(x[0]); points.push_back(x[1]); points.push_back(x[2]); } } else if (buffer[0] == 'f') { pch = str = buffer + 2; int k = 0; while (pch) { pch = strtok(str, " "); if (pch) { ip[k++] = atoi(pch) - 1; } else { break; } str = NULL; } if (k == 3) { triangles.push_back(ip[0]); triangles.push_back(ip[1]); triangles.push_back(ip[2]); } else if (k == 4) { triangles.push_back(ip[0]); triangles.push_back(ip[1]); triangles.push_back(ip[2]); triangles.push_back(ip[0]); triangles.push_back(ip[2]); triangles.push_back(ip[3]); } } } fclose(fid); } else { logger.Log("File not found\n"); return false; } return true; }
bool LoadOBJ(const string & fileName, vector< float > & points, vector< int > & triangles, IVHACD::IUserLogger & logger) { const char ObjDelimiters[] = " /"; const unsigned int BufferSize = 1024; FILE * fid = fopen(fileName.c_str(), "r"); if (fid) { char buffer[BufferSize]; int ip[3]; int in[3]; int it[3]; float x[3]; char * pch; char * str; size_t nn = 0; size_t nt = 0; while (!feof(fid)) { if (!fgets(buffer, BufferSize, fid)) { break; } else if (buffer[0] == 'v') { if (buffer[1] == ' ') { str = buffer + 2; for (int k = 0; k < 3; ++k) { pch = strtok(str, " "); if (pch) x[k] = (float)atof(pch); else { return false; } str = NULL; } points.push_back(x[0]); points.push_back(x[1]); points.push_back(x[2]); } else if (buffer[1] == 'n') { ++nn; } else if (buffer[1] == 't') { ++nt; } } else if (buffer[0] == 'f') { str = buffer + 2; for (int k = 0; k < 3; ++k) { pch = strtok(str, ObjDelimiters); if (pch) ip[k] = atoi(pch) - 1; else { return false; } str = NULL; if (nt > 0) { pch = strtok(NULL, ObjDelimiters); if (pch) it[k] = atoi(pch) - 1; else { return false; } } if (nn > 0) { pch = strtok(NULL, ObjDelimiters); if (pch) in[k] = atoi(pch) - 1; else { return false; } } } triangles.push_back(ip[0]); triangles.push_back(ip[1]); triangles.push_back(ip[2]); } } fclose(fid); } else { logger.Log("File not found\n"); return false; } return true; }