Reading a 3D-vector (vertex) from a STL-like binary file

I've got some code snippets and some binary files (STF-Format). The binary files contain much the same information as STL files:
http://en.wikipedia.org/wiki/STL_%28file_format%29
There are 3D vertices and triangles which connect them.
I've uploaded a sample file here:
http://www10.zippyshare.com/v/16326738/file.html

I only want to read out the vertices and save them into a TXT-File but that doesn't work at the moment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#define _AFXDLL

#include <iostream>
#include <fstream>
#include <vector>
#include <afx.h>
#include <string> 

using namespace std;

struct v3 {
	float x, y, z;
	v3(float _x = 0, float _y = 0, float _z = 0) : x(_x), y(_y), z(_z) {}
};

int main() {
	CFile StfFile;
	CFileException e;
	CString sFailure;
	CString sFilePath = "C:\\Test\\l.stf";

	//attempt to open file
	if(!StfFile.Open(sFilePath,CFile::modeRead|CFile::typeBinary,&e))
		return false;
	int i=0;
	int j=0;
	unsigned char buf[80];
	unsigned int uBytesRead;
	bool bFail=false;
	vector<unsigned char> dynbuf;

	v3 cpVertex_v3;
	vector<v3> m_Vertices_v3;

	//binary format
	uBytesRead=StfFile.Read(buf,80);
	if(uBytesRead!=80)
	{
		cout << "LoadSTF Failure - Header - uBytesRead!=80 bytes, problem with STF file structure";
	}

	//get count of # vertices
	uBytesRead=StfFile.Read(buf,4);
	if(uBytesRead==4)
	{
		int iVertexCount=*((int *)buf);

		if(iVertexCount>0)
		{
			unsigned int uBytesExpected=iVertexCount*12;
			dynbuf.assign(uBytesExpected,0);
			uBytesRead=StfFile.Read((unsigned char *)&dynbuf.front(),uBytesExpected);
			if(uBytesRead!=uBytesExpected)
			{
				iVertexCount=0;
				bFail=true;
				cout << "LoadSTF Failure - Vertices - uBytesRead!=uBytesExpected, problem with STF file structure";
			}
			//read vertices
			for (i=0; i<iVertexCount; ++i)
			{
				cpVertex_v3 = *(float *)(&dynbuf[i * 12]);
				m_Vertices_v3.push_back(cpVertex_v3);
			}

			//write vertices into txt-file
			ofstream output;
			output.open("C:\\Test\\r_vertices.txt");
			output.setf(ios::showpoint | ios::fixed, ios::floatfield);
			output.precision(12);

			for (size_t j = 0; j < m_Vertices_v3.size(); ++j) {
				output << m_Vertices_v3[j].x << ' ' << m_Vertices_v3[j].y << ' ' << m_Vertices_v3[j].z << endl;
			}
			output.close();
		}
	}
	else
	{
		bFail=true;
		cout << "LoadSTF Failure - Vertices - uBytesRead!=4 bytes, problem with STF file structure";
	}
	return !bFail;	
}


I think there is something wrong here:
1
2
3
4
5
6
//read vertices
			for (i=0; i<iVertexCount; ++i)
			{
				cpVertex_v3 = *(float *)(&dynbuf[i * 12]);
				m_Vertices_v3.push_back(cpVertex_v3);
			}


Thanks in advance
MPM
Last edited on
Topic archived. No new replies allowed.