#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <set>
#include <vector>
#include <map>
#include <string>
#include <time.h>
using namespace std;
class Vertex //定义点类
{
private:
float x,y,z;
public:
Vertex(float x1,float y1,float z1):x(x1),y(y1),z(z1){}
bool operator<(const Vertex&s)const
{
if(x<s.x) return true;
if((x==s.x)&&(y<s.y)) return true;
return false;
}
float getX(){return x;}
float getY(){return y;}
float getZ(){return z;}
};
int main()
{
clock_t start,finish;
start=clock();
ifstream stl("bunnyrA.stl"); //打开文件
if (!stl)
{
cerr<<"Open stl file error!"<<endl;
exit (1);
}
ofstream ply("bunnyrA.ply");
if (!ply)
{
cerr<<"Open ply file error!"<<endl;
exit (1);
}
vector<Vertex>Vert_temp; //将stl中的点读入vector容器中
string s;
stl>>s;
while (!stl.eof())
{
if (s=="vertex")
{
float x_t,y_t,z_t;
stl>>x_t>>y_t>>z_t;
Vertex ver(x_t,y_t,z_t);
Vert_temp.push_back(ver);
}
stl>>s;
}
int face_num=Vert_temp.size()/3; //三角面片总数
//vector测试
//cout<<setiosflags(ios::fixed)<<setprecision(10);
//cout<<"The first vertex:\t"<<Vert_temp[0].getX()<<"\t"<<Vert_temp[0].getY()<<"\t"<<Vert_temp[0].getZ()<<endl;
//int ve=Vert_temp.size()-1;
//cout<<"The last vertex:\t"<<Vert_temp[ve].getX()<<"\t"<<Vert_temp[ve].getY()<<"\t"<<Vert_temp[ve].getZ()<<endl;
//cout<<"The amount of faces:\t"<<face_num<<endl;
//cout<<"Vertex in vector:\t"<<Vert_temp.size()<<endl<<endl;
set<Vertex>Vert_set(Vert_temp.begin(),Vert_temp.end()); //vector容器转入set容器
//set测试
//cout<<"Vertex in set:\t"<<Vert_set.size()<<endl;
//向ply文件写入数据
ply<<setiosflags(ios::fixed)<<setprecision(10);
ply<<"ply"<<endl;
ply<<"format ascii 1.0"<<endl;
ply<<"comment Write By mightBXG"<<endl;
ply<<"element vertex "<<Vert_set.size()<<endl;
ply<<"property float x"<<endl;
ply<<"property float y"<<endl;
ply<<"property float z"<<endl;
ply<<"element face "<<face_num<<endl;
ply<<"property list uchar int vertex_indices"<<endl;
ply<<"end_header"<<endl;
map<Vertex,int> Vert_map;
set<Vertex>::iterator it_set=Vert_set.begin();
int i=0;
while (it_set!=Vert_set.end()) //set容器转入map容器
{
pair<Vertex,int>pair_tmp(*it_set,i);
Vert_map.insert(pair_tmp);
it_set++;i++;
}
it_set=Vert_set.begin();
while (it_set!=Vert_set.end()) //将set容器中的点输出到ply文件
{
ply<<(*it_set).getX()<<" "<<(*it_set).getY()<<" "<<(*it_set).getZ()<<endl;
it_set++;
}
vector<Vertex>::iterator it_vec=Vert_temp.begin();
while (it_vec!=Vert_temp.end()) //在map容器中查找vector中的点并输出
{
ply<<"3";
for (i=0;i<3;i++)
{
ply<<" "<<Vert_map[*it_vec];
it_vec++;
}
ply<<endl;
}
stl.close();
ply.close();
//程序运行时间测试
finish=clock();
double totaltime=(double)(finish-start)/CLOCKS_PER_SEC;
cout<<"totaltime:\t"<<totaltime<<"s"<<endl;
return 0;
}
iammight
- 粉丝: 0
- 资源: 1
最新资源
- 基于Visual Basic .Net及Python技术的学校需求解决方案设计源码
- 基于Java语言的Web开发学习Demo设计源码
- 基于Java的医院排队叫号系统界面原型设计源码
- 基于Java语言的Himalaya仿喜马拉雅设计源码
- 基于PHP+HTML+CSS+JavaScript的智能电车管家设计源码
- 基于Emscripten编译的纯H5直播流播放器jessibuca设计源码
- 基于react-native的Android隐私合规配置与代码集成设计源码
- 基于JavaFX技术的全功能色彩管理器设计源码
- erlang-21.3-1.el7.x86-64.rpm
- eclipse-inst-jre-win64.exe
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈