ply与off格式转换


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("转换成功!"));
}
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值