读取STL文件的ASCII和二进制文件格式,并去重点云数

// task1.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。

#pragma once
#include "pch.h"
#include <iostream>
#include <vector>
#include <fstream>
#include "Point3f.h"
#include <algorithm>
#include "task.h"
#include <string>
#include <sstream>

using namespace std;

bool ReadSTLFile::ReadFile(const char *cfilename)
{
	FILE *pfile;
	long  size;
	char *buffer;
	size_t result;
	//打开文件
	fopen_s(&pfile, cfilename, "rb");
	if (pfile == NULL) 
	{
		fputs("file error", stderr);
		exit(1);

	}
	//获取文件大小
	fseek(pfile, 0, SEEK_END);
	size = ftell(pfile);
	
	rewind(pfile);
	//为文件分配内存
	buffer =(char*) malloc(sizeof(char)*size);
	if (buffer == NULL)
	{
		fputs("memory error", stderr);
		exit(2);
	}
	//将文件拷贝到buffer中
	result = fread(buffer, 1, size, pfile);
	if (result != size)
	{
		fputs("reading error ", stderr);
		exit(3);
	}
	//关闭文件,释放内存
	fclose(pfile);

	ios::sync_with_stdio(false);
	ReadASCII(buffer);
	ios::sync_with_stdio(true);
	free(buffer);
	return true;
}
const bool unique_finder(const Point3f& first, const Point3f& second)
 {
	
	
		return (first.x == second.x&&first.y == second.y&&first.z == second.z);
	
	    
 }
const bool sort_finder(const Point3f &a, const Point3f &b)
{
	return (a.x < b.x) || ((a.x == b.x) && (a.y < b.y)) || ((a.x == b.x) && (a.y == b.y) && (a.z < b.z));
}
bool ReadSTLFile::ReadASCII(const char *buffer)
{
	unTriangles = 0;//面片数
	pointnum = 0;//节点数
	double x, y, z;
	int i;
	string name, useless;
	stringstream ss(buffer);
	ss.get();
	getline(ss, useless);
	//
	/*ss >> useless;
	//if (useless != "facet")
		
	getline(ss, useless);
	getline(ss, useless);
	for (i = 0; i < 3; i++)
	{
		ss >> useless >> x >> y >> z;
		pointnum++;
		pointlist.push_back(Point3f(x, y, z));
		
		
	}
	unTriangles++;
	getline(ss, useless);
	getline(ss, useless);
	getline(ss, useless);
	*/
	do {
		ss >> useless;
		if (useless != "facet")
			break;
		getline(ss, useless);
		getline(ss, useless);
		for (i = 0; i < 3; i++)
		{
			ss >> useless >> x >> y >> z;
			
			pointlist.push_back(Point3f(x, y, z));
			//结点数据去重
			
		//	pointnum++;
		//	pointlist.push_back(Point3f(x, y, z));
			/*for(vector<int>::size_type j = 0; j < pointlist.size();j++)
			{
				//测试cout << pointlist[j].x<<" "<< pointlist[j].y<< " "<<pointlist[j].z << endl;
				if (pointlist[j].x == x) {
					if (pointlist[j].y == y) 
					{
						if (pointlist[j].z== z)
						{
							break;
						}
						else
						{
							pointnum++;
							pointlist.push_back(Point3f(x, y, z));
						}
					}
					else
					{
						pointnum++;
						pointlist.push_back(Point3f(x, y, z));
					}
				}
				else 
				{
					pointnum++;
					pointlist.push_back(Point3f(x, y, z));
				}
				//cout << pointlist[j].x<<" "<< pointlist[j].y<< " "<<pointlist[j].z << endl;
				
			} */


			
			
		}
		unTriangles++;
		getline(ss, useless);
		getline(ss, useless);
		getline(ss, useless);
	} while (1);
	
	sort(pointlist.begin(),pointlist.end(),sort_finder);	
	pointlist.erase(unique(pointlist.begin(), pointlist.end(), unique_finder), pointlist.end());
	for (vector<int>::size_type j = 0; j < pointlist.size(); j++)
	{
		pointnum++;
	}
	return true;
}

//获取面片数
int ReadSTLFile::Numtri()
{
	return unTriangles;
}
//获取节点数
 int ReadSTLFile::Numtri1()
{
	return pointnum;
}

int main()
{
	
	ReadSTLFile RC;
	
	
	
	RC.ReadFile("test.stl");

	cout <<"面片数:"<< RC.Numtri() << endl;
	cout<<"未去重的节点数:" << RC.Numtri1() << endl;



	
}

task.h文件

#pragma once
#include <vector>
#include "Point3f.h"
#include <algorithm>
using namespace std;
  
class ReadSTLFile
{
public:
	bool ReadFile(const char *cfilename);
	int Numtri();
	int Numtri1();
	vector<Point3f>& Pointlist();
private :
	vector<Point3f> pointlist;
	unsigned int unTriangles;
	unsigned int pointnum;
	bool ReadASCII(const char *cfilename);

	char *memwriter;
	int cpyint(const char *& p);
	float cpyfloat(const char *& p);
};

Point3f.cpp

#pragma once
#include "pch.h"
#include "Point3f.h"
Point3f::Point3f() :x(0), y(0), z(0)
{

}
Point3f::Point3f(double _x, double _y, double _z) : x(_x), y(_y), z(_z)
{
}
int Point3f::SetParam(double _x, double _y, double _z)
{
	x = _x;
	y = _y;
	z = _z;
	return 0;

}

.h\

#pragma once
#include <d3d9.h>
#include <Windows.h>
//顶点结构
struct Vertex
{
	Vertex(){}
	Vertex(double x, double y, double z)
	{
		_x = x; _y = y; _z = z;
	}
	double _x, _y, _z;
	static const DWORD FVF;
};

class Point3f
{
public:
	Point3f();
	Point3f(double _x, double _y, double _z);
	int SetParam(double _x, double _y, double _z);
	inline Vertex IVertex()
	{
		return Vertex(x, y, z);
	}

	double x, y, z;
};





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值