-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLoadStlFile.cpp
More file actions
118 lines (105 loc) · 2.95 KB
/
LoadStlFile.cpp
File metadata and controls
118 lines (105 loc) · 2.95 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// LoadStlFile.cpp
//
#include "pch.h"
#include "kernapi.hxx"
#include "Model.h"
#define READSTRINGBUFFERLEN 256
bool LoadStlFile( const char* pszFileName, CModel& model )
{
if( NULL == pszFileName )
{
return false;
}
FILE* pFILE;
if( 0 != fopen_s( &pFILE, pszFileName, "rt" ) )
{
return false;
}
// 要素の数を数える(三角形の数)
unsigned int uiCountTriangle = 0;
while( 1 )
{
char szReadString[READSTRINGBUFFERLEN];
if( NULL == fgets( szReadString, READSTRINGBUFFERLEN, pFILE ) )
{
break;
}
const char cszDelimiter[] = ", \r\n\t";
char* pszContext;
char* pszToken = strtok_s( szReadString, cszDelimiter, &pszContext );
if( NULL == pszToken )
{
continue;
}
if( 0 == _stricmp( pszToken, "facet" ) )
{
uiCountTriangle++;
continue;
}
}
rewind( pFILE );
if( 0 == uiCountTriangle )
{
fclose( pFILE );
return false;
}
model.m_vVertex.assign( uiCountTriangle * 3, CVector() );
model.m_vIndexedTriangle.assign( uiCountTriangle, CIndexedTriangle() );
unsigned int ui3 = 0;
unsigned int uiIndexTriangle = 0;
unsigned int uiIndexVertex = 0;
while( 1 )
{
char szReadString[READSTRINGBUFFERLEN];
if( NULL == fgets( szReadString, READSTRINGBUFFERLEN, pFILE ) )
{
break;
}
const char cszDelimiter[] = ", \r\n\t";
char* pszContext;
char* pszToken = strtok_s( szReadString, cszDelimiter, &pszContext );
if( NULL == pszToken )
{
continue;
}
if( 0 == _stricmp( pszToken, "vertex" ) )
{
if( 3 <= ui3 )
{
continue;
}
pszToken = strtok_s( NULL, cszDelimiter, &pszContext );
model.m_vVertex[uiIndexVertex].x = (float)atof( pszToken );
pszToken = strtok_s( NULL, cszDelimiter, &pszContext );
model.m_vVertex[uiIndexVertex].y = (float)atof( pszToken );
pszToken = strtok_s( NULL, cszDelimiter, &pszContext );
model.m_vVertex[uiIndexVertex].z = (float)atof( pszToken );
model.m_vIndexedTriangle[uiIndexTriangle].m_ui3IndexVertex[ui3] = uiIndexVertex;
uiIndexVertex++;
ui3++;
continue;
}
else if( 0 == _stricmp( pszToken, "facet" ) )
{ // 面法線ベクトル
ui3 = 0;
continue;
}
else if( 0 == _stricmp( pszToken, "endfacet" ) )
{
uiIndexTriangle++;
continue;
}
else if( 0 == _stricmp( pszToken, "solid" ) )
{ // ソリッド名
continue;
}
}
fclose( pFILE );
if( 0 == model.m_vVertex.size()
|| 0 == model.m_vIndexedTriangle.size() )
{
return false;
}
return true;
}