实现ASCII类型STL文件的读取
任务内容
·任务说明
1 编写C++控制台程序,实现ASCII类型STL文件的读取。
·输入输出
1 输入文件“test.stl”(模型大小30M)。
2 输出:总面片数,节点数,程序运行时间。
任务完成情况
·实现思路
1 ASCII类型STL文件的读取:使用c++的ifstream实现对stl文件的按行读取。
2 保存tsl文件数据:使用自定义类Point来保存每一个点的信息,存放于map容器中,map的size表示结点的总数。
3 面片信息存放:使用自定义类Facet来存放每一个面片,存放于map容器中,key表示第几个面片,value表示面片的信息。
4 去重,采用map的key值不能重复的特性,对点进行去重。重载operator<函数。
·实现代码
·点类实现代码
#pragma once
class Point
{
// 第几个点
int index;
// 点的三维坐标
double x;
double y;
double z;
public:
Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
Point() {}
bool operator<(const Point& b)const
{
//return false;
return (x < b.x) || (x == b.x) && (y < b.y) || (x == b.x && y == b.y && z < b.z);
//return true;
// 相等比较
}
};
·面片类代码
#pragma once
#include"Point.h"
#include <vector>
using namespace std;
class Facet
{
public:
// 第几个面片
int index;
// 面片的各个点的信息
Point x; Point y; Point z;
// 存放面片钟各点的信息
vector<Point>_facetList;
public:
Facet(Point x1, Point y1, Point z1){
x = x1;
y = y1;
z = z1;
}
void addFacet(Point point)
{
_facetList.push_back(point);
}
Facet() {};
·数据读取代码
bool ReadASCII()
{
map<int, Facet> mapList;
map<Point, int>_mapPointList;
string s;
string str;
int count = 0;
//点数
int i = 0;
//面片数
int trangle = 0;
Facet facet1;
ifstream fin("D:\\vs2017projects\\Task3\\test.stl",ios::in);
float x1, y1, z1;
float x2, y2, z2;
float x3, y3, z3;
fin >> s >> s >> str;
while (str =="facet")
{
fin >> s>>s>>s>>s>>s>>s>>s >> x1 >> y1 >> z1>>s
>>x2>>y2>>z2>>s
>>x3>>y3>>z3>>s>>s>>str;
Point point1(x1,y1,z1);
Point point2(x2, y2, z2);
Point point3(x3, y3, z3);
_mapPointList.insert(pair<Point,int>(point1,0));
_mapPointList.insert(pair<Point, int>(point2, 0));
_mapPointList.insert(pair<Point, int>(point3, 0));
Facet facet1;
facet1.addFacet(point1);
facet1.addFacet(point2);
facet1.addFacet(point3);
mapList.insert(pair<int, Facet>(trangle, facet1));
trangle++;
if (str == "endsolid")
{
break;
}
}
cout << "面片数" << mapList.size() << endl;
cout << "节点数" << _mapPointList.size() << endl;
return true;
}