-
Notifications
You must be signed in to change notification settings - Fork 13
/
ioutilities.cpp
170 lines (150 loc) · 6.02 KB
/
ioutilities.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
#include "ioutilities.h"
vector<string> ReadFileByLine(const string &filename) {
ifstream fin(filename);
vector<string> lines;
while (fin) {
string line;
std::getline(fin, line);
if (!line.empty())
lines.push_back(line);
}
return lines;
}
template <typename T, typename Parser>
vector<T> LoadAsVector(const string& filename, Parser parser) {
cout << "Reading " << filename << endl;
vector<string> lines = ReadFileByLine(filename);
vector<T> items(lines.size());
std::transform(lines.begin(), lines.end(), items.begin(), parser);
cout << items.size() << " items loaded." << endl;
return items;
}
vector<int> LoadIndices(const string &filename) {
#if 0
cout << "Reading indices from " << filename << endl;
vector<string> lines = ReadFileByLine(filename);
vector<int> indices(lines.size());
std::transform(lines.begin(), lines.end(), indices.begin(),
[](const string &s) {
return std::stoi(s);
});
cout << indices.size() << " landmarks loaded." << endl;
return indices;
#else
auto parser = [](const string &s) {
return std::stoi(s);
};
return LoadAsVector<int, decltype(parser)>(filename, parser);
#endif
}
vector<float> LoadFloats(const string& filename) {
auto parser = [](const string& s) {
return std::stof(s);
};
return LoadAsVector<float, decltype(parser)>(filename, parser);
}
vector<vector<int>> LoadContourIndices(const string& filename) {
cout << "Reading contour indices from " << filename << endl;
vector<string> lines = ReadFileByLine(filename);
vector<vector<int>> contour_indices(lines.size());
std::transform(lines.begin(), lines.end(), contour_indices.begin(),
[](const string &line) {
vector<string> parts;
boost::algorithm::split(parts, line,
boost::algorithm::is_any_of(" "),
boost::algorithm::token_compress_on);
auto parts_end = std::remove_if(parts.begin(), parts.end(),
[](const string &s) {
return s.empty();
});
vector<int> indices(std::distance(parts.begin(), parts_end));
std::transform(parts.begin(), parts_end, indices.begin(),
[](const string &s) {
return std::stoi(s);
});
return indices;
});
return contour_indices;
}
namespace std {
inline istream& operator>>(istream& is, Constraint2D& c) {
is >> c.data.x >> c.data.y;
return is;
}
}
vector<Constraint2D> LoadConstraints(const string& filename) {
cout << "Reading constraints from " << filename << endl;
ifstream fin(filename);
if(!fin) {
cerr << "Failed to open file " << filename << endl;
exit(-1);
}
int num_constraints;
fin >> num_constraints;
istream_iterator<Constraint2D> iter(fin);
istream_iterator<Constraint2D> iter_end;
vector<Constraint2D> constraints;
std::copy(iter, iter_end, back_inserter(constraints));
std::for_each(constraints.begin(), constraints.end(), [](Constraint2D& c) {
c.vidx = -1;
c.weight = 1.0;
// The coordinates are one-based. Fix them.
c.data.x -= 1.0;
c.data.y -= 1.0;
DEBUG_EXPR(cout << c.data.x << ',' << c.data.y << endl;)
});
cout << num_constraints << " constraints expected. "
<< constraints.size() << " constraints loaded." << endl;
assert(num_constraints == constraints.size());
return constraints;
}
pair<QImage, vector<Constraint2D>> LoadImageAndPoints(
const string &image_filename, const string &pts_filename, bool resize) {
QImage img(image_filename.c_str());
auto constraints = LoadConstraints(pts_filename);
// Compute a proper scale so the distance between pupils is approximately 200
double puple_distance = glm::distance(
0.5 * (constraints[28].data + constraints[30].data),
0.5 * (constraints[32].data + constraints[34].data));
const double reference_distance = 100.0;
double scale_ratio = resize ? reference_distance / puple_distance : 1.0;
// Scale the image
img = img.scaled(img.width() * scale_ratio, img.height() * scale_ratio,
Qt::KeepAspectRatio, Qt::SmoothTransformation);
cout << "image size: " << img.width() << "x" << img.height() << endl;
// Preprocess constraints
for (auto &constraint : constraints) {
constraint.data = constraint.data * scale_ratio;
constraint.data.y = img.height() - 1 - constraint.data.y;
}
return make_pair(img, constraints);
};
vector<pair<string, string>> ParseSettingsFile(const string &filename) {
vector<string> lines = ReadFileByLine(filename);
vector<pair<string, string>> image_points_filenames(lines.size());
std::transform(lines.begin(), lines.end(), image_points_filenames.begin(),
[](const string &line) {
vector<string> parts;
boost::algorithm::split(parts, line,
boost::algorithm::is_any_of(" "),
boost::algorithm::token_compress_on);
auto parts_end = std::remove_if(parts.begin(), parts.end(),
[](const string &s) {
return s.empty();
});
assert(std::distance(parts.begin(), parts_end) == 2);
return make_pair(parts.front(), parts.back());
});
return image_points_filenames;
}
ReconstructionResult LoadReconstructionResult(const string &filename) {
ReconstructionResult result;
ifstream fin(filename);
if(!fin) {
cerr << "Failed to load reconstruction result from " << filename << endl;
exit(-1);
}
fin >> result.params_cam >> result.params_model >> result.stats;
fin.close();
return result;
}