-
Notifications
You must be signed in to change notification settings - Fork 0
/
plum_loader.cpp
237 lines (208 loc) · 5.73 KB
/
plum_loader.cpp
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include "plum_loader.h"
float char_vector_to_float(std::vector<char> charvec);
int char_vector_to_int(std::vector<char> charvec);
/**
Read .plum file and return resulting Mesh struct.
*/
extern "C" Mesh plum_loader(const char* filename)
{
// open an input stream for the file for vertex data collection
std::ifstream ifs(filename, std::ios::in);
if(!ifs.is_open()) {
std::cerr << "ERROR : The file did not open." << std::endl;
/* Return fake empty struct just to bail out */
Mesh mesh;
mesh.read_status = false;
return mesh;
}
// vertex container for all of the vertex data
std::vector<std::vector<float> > vc;
// facial vertex container for all of the VBO data
std::vector<std::vector<int> > fc;
// start a loop to find all the vertex data & store it in vc
char memblock[1];
std::cout << "File content:" << std::endl;
while(true) {
ifs.read(memblock, 1);
if(memblock[0] == 'v') { // Vertex line
std::vector<float> vertex;
std::vector<char> tempvec;
ifs.seekg(1, std::ios::cur);
while (true) {
ifs.read(memblock, 1);
// 0x20: space, 0x0d: newline
if (memblock[0] != 0x20 && memblock[0] != 0x0a) {
/* Reading 1 coord */
tempvec.push_back(memblock[0]);
std::cout << memblock[0];
} else if (memblock[0] == 0x20) {
/* Finished 1 coord */
vertex.push_back(char_vector_to_float(tempvec));
tempvec.clear();
std::cout << " ";
} else {
/* Finished 1 line */
vertex.push_back(char_vector_to_float(tempvec));
vc.push_back(vertex);
tempvec.clear();
std::cout << std::endl;
//ifs.seekg(1, std::ios::cur);
break;
}
}
} else if (memblock[0] == 'f') { // Face line
std::vector<int> face;
std::vector<char> tempvec;
ifs.seekg(1, std::ios::cur);
while (true) {
ifs.read(memblock, 1);
// 0x20: space, 0x0d: newline
if(memblock[0] != 0x20 && memblock[0] != 0x0a) {
/* Reading 1 index */
tempvec.push_back(memblock[0]);
std::cout << memblock[0];
} else if(memblock[0] == 0x20) {
/* Finished 1 index */
face.push_back(char_vector_to_int(tempvec));
tempvec.clear();
std::cout << " ";
} else {
/* Finished 1 line */
face.push_back(char_vector_to_int(tempvec));
fc.push_back(face);
tempvec.clear();
std::cout << std::endl;
//ifs.seekg(1, std::ios::cur);
break;
}
}
} else { // Nothing to read, skip to the next line
while (memblock[0] != 0x0a)
ifs.read(memblock, 1);
if (ifs.eof())
break;
//ifs.seekg(1, std::ios::cur);
}
}
std::cout << std::endl;
ifs.close();
// Convert vc and fc into float and int array
float** vertex_array = new float*[fc.size()];
for (std::size_t i=0; i<vc.size(); i++) {
vertex_array[i] = new float[3];
vertex_array[i][0] = vc[i][0];
vertex_array[i][1] = vc[i][1];
vertex_array[i][2] = vc[i][2];
}
// Caution: 0th element is the *size of that array*
int** face_array = new int*[fc.size()];
for (std::size_t i=0; i<fc.size(); i++) {
face_array[i] = new int[1+fc[i].size()];
face_array[i][0] = fc[i].size();
for (std::size_t j=0; j<fc[i].size(); j++)
face_array[i][j+1] = fc[i][j];
}
// Print arrays for debugging purpose
std::cout << "vertex_array:" << std::endl;
for (std::size_t i=0; i<vc.size(); i++) {
std::cout << vertex_array[i][0] << " ";
std::cout << vertex_array[i][1] << " ";
std::cout << vertex_array[i][2] << std::endl;
}
std::cout << std::endl;
std::cout << "face_array:" << std::endl;
for (std::size_t i=0; i<fc.size(); i++) {
int size = face_array[i][0];
for (int j=0; j<size; j++)
std::cout << face_array[i][j+1] << " ";
std::cout << std::endl;
}
std::cout << std::endl;
std::cout << "Mesh data loading completed" << std::endl;
// Pack the results into a struct.
Mesh mesh;
mesh.read_status = true;
mesh.vertex_count = vc.size();
mesh.face_count = fc.size();
mesh.vertex_array = vertex_array;
mesh.face_array = face_array;
return mesh;
}
/**
Get vertex vector of INDEX.
*/
std::vector<float> get_vertex(const Mesh mesh, const std::size_t index)
{
if (index < mesh.vertex_count) {
float* vertex_ptr = mesh.vertex_array[index];
std::vector<float> vertex(vertex_ptr, vertex_ptr+3);
return vertex;
} else {
return std::vector<float>();
}
}
/**
Get face index vector at INDEX.
*/
std::vector<int> get_face(const Mesh mesh, const std::size_t index)
{
if (index < mesh.face_count) {
int* face_ptr = mesh.face_array[index];
const std::size_t length = face_ptr[0];
std::vector<int> face(face_ptr+1, face_ptr+1+length);
return face;
} else {
return std::vector<int>();
}
}
/**
Explicit destructor for struct Mesh.
This is needed because C doensn't support destructors.
*/
extern "C" void delete_mesh(Mesh mesh)
{
std::cout << "Deleting Mesh..." << std::endl;
for (std::size_t i=0; i<mesh.vertex_count; i++)
delete [] mesh.vertex_array[i];
delete [] mesh.vertex_array;
for (std::size_t i=0; i<mesh.face_count; i++)
delete [] mesh.face_array[i];
delete [] mesh.face_array;
}
extern "C" void array_test(float** arr)
{
std::cout << arr[0][0] << arr[0][1] << std::endl;
std::cout << arr[1][0] << arr[1][1] << std::endl;
}
extern "C" float* array_return_1d()
{
float* arr = new float[3];
arr[0] = 0.0;
arr[1] = 1.0;
arr[2] = 2.0;
return arr;
}
extern "C" float** array_return_2d()
{
float** arr = new float*[2];
for (int i=0; i<2; i++) {
arr[i] = new float[3];
}
return arr;
}
/**
Convert vector<char> to float and return it.
*/
float char_vector_to_float(std::vector<char> charvec)
{
std::string str(charvec.begin(), charvec.end());
return std::stof(str);
}
/**
Convert vector<char> to int and return it.
*/
int char_vector_to_int(std::vector<char> charvec)
{
std::string str(charvec.begin(), charvec.end());
return std::stoi(str);
}