自定义OFF文件的读与写

11 篇文章 3 订阅
8 篇文章 9 订阅

1.文件格式后缀为.off,格式为,

OFF
	8	12	0
	 0.5	-0.5	-0.5 
	 0.5	 0.5	-0.5 
	-0.5	 0.5	-0.5 
	-0.5	-0.5	-0.5 
	 0.5	-0.5	 0.5 
	 0.5	 0.5	 0.5 
	-0.5	 0.5	 0.5 
	-0.5	-0.5	 0.5 
	3	0	1	3
	3	1	2	3
	3	4	6	5
	3	7	6	4
	3	0	3	4
	3	4	3	7
	3	1	5	2
	3	2	5	6
	3	0	4	1
	3	1	4	5
	3	2	6	3
	3	3	6	7

2.读与写操作:

//读操作
bool ReadOffFile(const std::string& offFile, osg::Vec3Array* vertexArray, osg::DrawElementsUInt* indexArray)
{
	/*
	function: read the backgroud sphere mesh
	input: the background sphere .off file
	output: Sphere_nodes array
	Sphere_faces array
	SphereNumNode
	SphereNumFace
	*/
	
	FILE *pf;
	pf = fopen(offFile.c_str(), "r");
	if (pf == NULL) {
		printf("Open file failure");
		return false;
	}

	int SphereNumNode = 0;			// number of node that background sphere
	int SphereNumFace = 0;			// number of face that background sphere

	// Read the first line of the mesh file and get the numbere of points and the number of faces.
	char temp[100], *c1 = "%", *c2 = "#", *c3 = "OFF", xx[10];
	while (1) {
		fgets(temp, 100, pf);
		sscanf(temp, "%s", xx);
		if (strcmp(xx, c1) == 0 || strcmp(xx, c2) == 0 || strcmp(xx, c3) == 0)
			continue;
		else {
			sscanf(temp, "%d%d", &SphereNumNode, &SphereNumFace);
			break;
		}
	}
	std::cout << "The number of vertex is " << SphereNumNode << std::endl;
	std::cout << "The number of faces is " << SphereNumFace << std::endl;
	
	for (int i = 0; i < SphereNumNode; i++) {
		fgets(temp, 100, pf);

		double vt_x = 0;
		double vt_y = 0;
		double vt_z = 0;
		sscanf(temp, "%lf%lf%lf", &vt_x, &vt_y, &vt_z);
		std::cout << "The vertex is " << i << ": " << vt_x << ", " << vt_y << ", " << vt_z << std::endl;
		vertexArray->push_back(osg::Vec3(vt_x, vt_y, vt_z));
	}

	for (int i = 0; i < SphereNumFace; i++) {
		fgets(temp, 100, pf);
		
		int k;
		int index_0 = 0;
		int index_1 = 0;
		int index_2 = 0;
		sscanf(temp, "%d%d%d%d", &k, &index_0, &index_1, &index_2);
		std::cout << "The index is " << i << ": " << k << ", " << index_0 << ", " << index_1 << ", " << index_2 << std::endl;
		indexArray->push_back(index_0);
		indexArray->push_back(index_1);
		indexArray->push_back(index_2);
	}
	std::cout << "The off readed successful!" << std::endl;
	fclose(pf);
	return true;
}

//写操作
bool WirteOffFile(const std::string& offFile, osg::Vec3Array* vertexArray, osg::DrawElementsUInt* indexArray)
{
	ofstream outfile;   //写文件,输出流

	outfile.open(offFile.c_str(), ios::out | ios::trunc);   //每次写都定位的文件结尾,不会丢失原来的内容,用out则会丢失原来的内容
	if (!outfile.is_open())
	{
		cout << "Open file failure" << endl;
		return false;
	}
	else {
		//在result.txt中写入结果

		//文件格式
		outfile << "OFF" << endl;

		//顶点数量	三角面片	0
		outfile << "\t" << vertexArray->size() << "\t" << indexArray->size() / 3 << "\t" << "0" << std::endl;

		//写顶点数据
		int nums = vertexArray->size();
		for (unsigned int i = 0; i < nums; i++)
		{
			osg::Vec3d pos = vertexArray->at(i);
			outfile << "\t" << pos.y() << "\t" << pos.x() << "\t" << pos.z() << std::endl;
		}

		//写顶点索引数据
		nums = indexArray->size();
		for (unsigned int i = 0; i < nums; i += 3)
		{
			int index0 = indexArray->at(i);
			int index1 = indexArray->at(i + 1);
			int index2 = indexArray->at(i + 2);
			outfile << "\t" << "3" << "\t" << index1 << "\t" << index0 << "\t" << index2 << std::endl;
		}
	}
	outfile.close();

	return true;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小胖七少爷

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值