1.文件格式后缀为.off,格式为,
OFF
8 12 0
0.5 -0.5 -0.5
0.5 0.5 -0.5
-0.5 0.5 -0.5
-0.5 -0.5 -0.5
0.5 -0.5 0.5
0.5 0.5 0.5
-0.5 0.5 0.5
-0.5 -0.5 0.5
3 0 1 3
3 1 2 3
3 4 6 5
3 7 6 4
3 0 3 4
3 4 3 7
3 1 5 2
3 2 5 6
3 0 4 1
3 1 4 5
3 2 6 3
3 3 6 7
2.读与写操作:
//读操作
bool ReadOffFile(const std::string& offFile, osg::Vec3Array* vertexArray, osg::DrawElementsUInt* indexArray)
{
/*
function: read the backgroud sphere mesh
input: the background sphere .off file
output: Sphere_nodes array
Sphere_faces array
SphereNumNode
SphereNumFace
*/
FILE *pf;
pf = fopen(offFile.c_str(), "r");
if (pf == NULL) {
printf("Open file failure");
return false;
}
int SphereNumNode = 0; // number of node that background sphere
int SphereNumFace = 0; // number of face that background sphere
// Read the first line of the mesh file and get the numbere of points and the number of faces.
char temp[100], *c1 = "%", *c2 = "#", *c3 = "OFF", xx[10];
while (1) {
fgets(temp, 100, pf);
sscanf(temp, "%s", xx);
if (strcmp(xx, c1) == 0 || strcmp(xx, c2) == 0 || strcmp(xx, c3) == 0)
continue;
else {
sscanf(temp, "%d%d", &SphereNumNode, &SphereNumFace);
break;
}
}
std::cout << "The number of vertex is " << SphereNumNode << std::endl;
std::cout << "The number of faces is " << SphereNumFace << std::endl;
for (int i = 0; i < SphereNumNode; i++) {
fgets(temp, 100, pf);
double vt_x = 0;
double vt_y = 0;
double vt_z = 0;
sscanf(temp, "%lf%lf%lf", &vt_x, &vt_y, &vt_z);
std::cout << "The vertex is " << i << ": " << vt_x << ", " << vt_y << ", " << vt_z << std::endl;
vertexArray->push_back(osg::Vec3(vt_x, vt_y, vt_z));
}
for (int i = 0; i < SphereNumFace; i++) {
fgets(temp, 100, pf);
int k;
int index_0 = 0;
int index_1 = 0;
int index_2 = 0;
sscanf(temp, "%d%d%d%d", &k, &index_0, &index_1, &index_2);
std::cout << "The index is " << i << ": " << k << ", " << index_0 << ", " << index_1 << ", " << index_2 << std::endl;
indexArray->push_back(index_0);
indexArray->push_back(index_1);
indexArray->push_back(index_2);
}
std::cout << "The off readed successful!" << std::endl;
fclose(pf);
return true;
}
//写操作
bool WirteOffFile(const std::string& offFile, osg::Vec3Array* vertexArray, osg::DrawElementsUInt* indexArray)
{
ofstream outfile; //写文件,输出流
outfile.open(offFile.c_str(), ios::out | ios::trunc); //每次写都定位的文件结尾,不会丢失原来的内容,用out则会丢失原来的内容
if (!outfile.is_open())
{
cout << "Open file failure" << endl;
return false;
}
else {
//在result.txt中写入结果
//文件格式
outfile << "OFF" << endl;
//顶点数量 三角面片 0
outfile << "\t" << vertexArray->size() << "\t" << indexArray->size() / 3 << "\t" << "0" << std::endl;
//写顶点数据
int nums = vertexArray->size();
for (unsigned int i = 0; i < nums; i++)
{
osg::Vec3d pos = vertexArray->at(i);
outfile << "\t" << pos.y() << "\t" << pos.x() << "\t" << pos.z() << std::endl;
}
//写顶点索引数据
nums = indexArray->size();
for (unsigned int i = 0; i < nums; i += 3)
{
int index0 = indexArray->at(i);
int index1 = indexArray->at(i + 1);
int index2 = indexArray->at(i + 2);
outfile << "\t" << "3" << "\t" << index1 << "\t" << index0 << "\t" << index2 << std::endl;
}
}
outfile.close();
return true;
}