void CTest1Dlg::OnFileConvertionPlyToOFF()
{
// TODO: Add your control notification handler code here
char filestyle[3];
string str;
ifstream file;
int points, meshs, oriednum = 0;
file.open(pathName);
//获取点数points和面数meshs;将不需要的字符去除;
for (int i=0;i<3;i++)
{
getline(file, str);
}
file >> str >> str;
file >> points;
for (int i=0;i < 4; i++)
{
getline(file, str);
}
file >> str >> str;
file >> meshs;
for (int i=0;i < 3; i++)
{
getline(file, str);
}
//将.ply文件的点、面数据存入结构体
int line=0;
while (line < points)
{
double x,y,z;
file >> x >> y >> z;
point ptem;
ptem.x=x;
ptem.y=y;
ptem.z=z;
p.push_back(ptem);
line++;
}
line=0;
while (line < meshs)
{
int mx,my,mz,mp;
file>>mp>>mx>>my>>mz;
mesh mtem;
mtem.m1=mx;
mtem.m2=my;
mtem.m3=mz;
m.push_back(mtem);
line++;
}
//输出结构体数据至.off文件
ofstream out;
out.open("C:\\Users\\Administrator\\Desktop\\PLY-OFF.off",ios::trunc);
out << "OFF" << endl;
out<<points<<" "<<meshs<<" "<<oriednum<<endl;
for(int i=0;i<points;i++)
{
out<<p[i].x<<" "<<p[i].y<<" "<<p[i].z<<endl;
}
for(int i=0;i<meshs;i++)
{
out<<"3 "<<m[i].m1<<" "<<m[i].m2<<" "<<m[i].m3<<endl;
}
out.close();
AfxMessageBox(_T("转换成功!"));
}
void CTest1Dlg::OnFileConvertionOffToPly()
{
// TODO: Add your control notification handler code here
FILE* infile = fopen((LPCSTR)CStringA(pathName), "r");
char filestyle[3];
fscanf(infile, "%3c", &filestyle);
int points, meshs, oriednum;
fscanf(infile, "%d %d %d",&points,&meshs,& oriednum);
int line=0;
while (line < points)
{
double x,y,z;
fscanf(infile, "%lf %lf %lf",&x, &y, &z);
point ptem;
ptem.x=x;
ptem.y=y;
ptem.z=z;
p.push_back(ptem);
line++;
}
line=0;
while (line < meshs)
{
int mx, my, mz, mp;
fscanf(infile, "%d %d %d %d",&mp,&mx,&my,&mz);
mesh mtem;
mtem.m1=mx;
mtem.m2=my;
mtem.m3=mz;
m.push_back(mtem);
line++;
}
fclose(infile);
//输出存储数据
ofstream out;
out.open("C:\\Users\\Administrator\\Desktop\\OFF-PLY.ply",ios::trunc);
out<<"ply"<<endl<<"format ascii 1.0"<<endl;
out<<"comment VCGLIB generated"<<endl;
out<<"element vertex "<<p.size()<<endl<<"property float x"<<endl<<"property float y"<<endl<<"property float z"<<endl<<"element face "<<m.size()<<endl<<"property list uchar int vertex_indices"<<endl<<"end_header"<<endl;
for(int i = 0;i < points; i++)
{
out<<p[i].x<<" "<<p[i].y<<" "<<p[i].z<<endl;
}
for(int i = 0;i < meshs; i++)
{
out<<"3 "<<m[i].m1<<" "<<m[i].m2<<" "<<m[i].m3<<endl;
}
out.close();
AfxMessageBox(_T("转换成功!"));
}