-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cpp
More file actions
117 lines (86 loc) · 2.06 KB
/
FileReader.cpp
File metadata and controls
117 lines (86 loc) · 2.06 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
/*
* FileReader.cpp
*
* Created on: Nov 13, 2015
* Author: hugocm93
*/
#include "FileReader.h"
using namespace std;
FileReader::FileReader() {
// TODO Auto-generated constructor stub
}
FileReader::~FileReader() {
// TODO Auto-generated destructor stub
}
void FileReader::read(const char name[], float** verticesI, int** trianglesI, int* numVertices, int* numTriangles, float* XminI, float* YminI, float* ZminI, float* XmaxI, float* YmaxI, float* ZmaxI){
float* vertices;
int* triangles;
FILE * file = fopen(name, "r");
if(file == NULL){
cout << "File was not found" << endl;
exit(1);
}
char header[5];
fscanf(file, "%[^\n]", header);
header[3] = '\0';
if(strcmp(header, "OFF") == 0){
cout << "File is correct" << endl;
}
else{
cout << "File is incorrect" << endl;
}
int nVertices, nTriangles, garbage;
fscanf(file, " %d %d %d", &nVertices, &nTriangles, &garbage);
vertices = (float*)malloc(sizeof(float)*nVertices*3);
triangles = (int*)malloc(sizeof(int)*nTriangles*3);
cout << "Alloc Vertices" << endl;
float Xmax=0, Ymax=0, Zmax=0;
float Xmin=100000, Ymin=100000, Zmin=100000;
for(int i = 0; i < nVertices*3 ; i+=3){
float x, y, z;
fscanf(file, " %f %f %f", &x, &y, &z);
vertices[i] = x;
vertices[i+1] = y;
vertices[i+2] = z;
if(x<Xmin){
Xmin = x;
}
else if(x>Xmax){
Xmax = x;
}
if(y<Ymin){
Ymin = y;
}
else if(y>Ymax){
Ymax = y;
}
if(z<Zmin){
Zmin = z;
}
else if(z>Zmax){
Zmax = z;
}
}
*XminI = Xmin;
*YminI = Ymin;
*ZminI = Zmin;
*XmaxI = Xmax;
*YmaxI = Ymax;
*ZmaxI = Zmax;
printf("%f %f %f\n", Xmax, Ymax, Zmax);
printf("%f %f %f\n", Xmin, Ymin, Zmin);
cout << "Alloc triangles" << endl;
for(int i = 0; i < nTriangles*3 ; i+=3){
int garbagef, v1, v2, v3;
fscanf(file, " %d %d %d %d", &garbagef, &v1, &v2, &v3);
triangles[i] = v1;
triangles[i+1] = v2;
triangles[i+2] = v3;
//printf("%d %d %d\n", v1, v2, v3);
}
*verticesI = vertices;
*trianglesI = triangles;
*numVertices = nVertices;
*numTriangles = nTriangles;
cout << "Finished reading" << endl;
}