-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.cpp
128 lines (115 loc) · 2.92 KB
/
io.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
#include "io.h"
#include <iostream>
#include <fstream>
#ifdef __APPLE__
#define MAX std::numeric_limits<double>::max()
#else
#include <values.h>
#define MAX DBL_MAX
#endif
bool readObjFile(char *fname, std::vector<Eigen::Vector3d> &pts, std::vector<Tri> &triangles)
{
char c[500];
Eigen::Vector3d lc(MAX,MAX,MAX), uc(-MAX,-MAX,-MAX);
int numVertices=0, numFaces=0;
bool normals = false, texture = false;
int tint;
char ch;
int p, q, r;
double x, y, z;
std::vector<Eigen::Vector3d>::iterator v;
std::vector<Tri>::iterator t;
std::ifstream in1(fname, std::ios::in);
if (!in1.is_open()) {
return false;
}
in1.flags(in1.flags() & ~std::ios::skipws);
while (in1>>ch) {
if (ch == 'v') {
in1>>ch;
if (ch == ' ') numVertices++;
else if (ch == 'n') normals = true;
else if (ch == 't') texture = true;
else std::cerr<<"error \'"<<ch<<"\'"<<std::endl;
} else if (ch == '#') {
while (in1 >> ch && ch != '\n') ; // Read to the end of the line.
} else if (ch == 'f') numFaces++;
}
in1.close();
pts.resize(numVertices);
triangles.resize(numFaces);
v = pts.begin();
t = triangles.begin();
std::ifstream in(fname, std::ios::in);
if (!in.is_open()) {
return false;
}
while (in>>ch) {
if (ch == '#') {
in.getline(c,500);
continue;
}
if (ch == 'g') {
in.getline(c,500);
continue;
}
if (ch == 's') {
in.getline(c,500);
continue;
}
if (ch == 'm') {
in.getline(c,500);
continue;
}
if (ch == 'u') {
in.getline(c,500);
continue;
}
if (ch == 'v') {
ch = in.peek();
if (ch != 't' && ch != 'n') {
in>>x>>y>>z;
(*v)<<x,y,z;
if ((*v)[0] < lc[0]) lc[0] = (*v)[0];
if ((*v)[1] < lc[1]) lc[1] = (*v)[1];
if ((*v)[2] < lc[2]) lc[2] = (*v)[2];
if ((*v)[0] > uc[0]) uc[0] = (*v)[0];
if ((*v)[1] > uc[1]) uc[1] = (*v)[1];
if ((*v)[2] > uc[2]) uc[2] = (*v)[2];
v++;
} else {
in.getline(c, 500);
}
continue;
}
if (ch == 'f') {
if (normals && texture) {
in>>p>>ch>>tint>>ch>>tint>>q>>ch>>tint>>ch>>tint>>r>>ch>>tint>>ch>>tint;
} else if (normals) {
in>>p>>ch>>ch>>tint>>q>>ch>>ch>>tint>>r>>ch>>ch>>tint;
} else if (texture) {
in>>p>>ch>>tint>>q>>ch>>tint>>r>>ch>>tint;
} else {
in>>p>>q>>r;
}
(*t)[0] = p-1;
(*t)[1] = q-1;
(*t)[2] = r-1;
t++;
continue;
}
}
in.close();
return true;
}
void writeObjFile(char *fname, const std::vector<Eigen::Vector3d> &meshPts, const std::vector<Tri> &triangles) {
std::ofstream out;
std::vector<Eigen::Vector3d>::const_iterator p;
std::vector<Tri>::const_iterator t;
out.open(fname);
for (p=meshPts.begin(); p!=meshPts.end(); p++)
out<<"v "<<(*p)[0]<<" "<<(*p)[1]<<" "<<(*p)[2]<<std::endl;
for (t=triangles.begin(); t!=triangles.end(); t++)
out<<"f "<<(*t)[0]+1<<" "<<(*t)[1]+1<<" "<<(*t)[2]+1<<std::endl;
out.close();
}