-
Notifications
You must be signed in to change notification settings - Fork 17
/
import_export.cpp
293 lines (243 loc) · 9.66 KB
/
import_export.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* import_export.cpp
*
* Created on: Mar 27, 2015
* Author: Fabian Tschopp
*/
#include "import_export.hpp"
#include <cassert>
#include "query_loader.hpp"
#include <eigen3/Eigen/Dense>
#include <eigen3/Eigen/Core>
#include <eigen3/Eigen/Eigen>
#include "SIFT_keypoint.hpp"
#include "parse_bundler.hpp"
#include "bundler_camera.hpp"
#include "query_processor.hpp"
#include "pose_utils.hpp"
namespace pose_estimation {
// Export the SfM mesh in PLY format
void ExportMesh(parse_bundler &parsebundler, std::string path,
std::string filename) {
std::ofstream out_file;
out_file.open(path + filename + ".ply");
assert(out_file.is_open());
std::vector<feature_3D_info> &sifts = parsebundler.get_feature_infos();
// Write PLY Header
out_file << "ply" << std::endl;
out_file << "format ascii 1.0" << std::endl;
out_file << "element vertex " << sifts.size() << std::endl;
out_file << "property float32 x" << std::endl;
out_file << "property float32 y" << std::endl;
out_file << "property float32 z" << std::endl;
out_file << "property uchar red" << std::endl;
out_file << "property uchar green" << std::endl;
out_file << "property uchar blue" << std::endl;
out_file << "end_header" << std::endl;
out_file << std::fixed;
for (unsigned int i = 0; i < sifts.size(); ++i) {
out_file << std::setprecision(16) << sifts[i].point.x << " "
<< std::setprecision(16) << sifts[i].point.y << " "
<< std::setprecision(16) << sifts[i].point.z << " "
<< (int) sifts[i].point.r << " " << (int) sifts[i].point.g
<< " " << (int) sifts[i].point.b << std::endl;
}
out_file.close();
}
// Load the golden model query and pose estimation for comparison
Query LoadGoldenQuery(parse_bundler pb, int query_camera_id,
std::string list_path) {
std::ifstream queryfile;
queryfile.open(list_path + "list.orig.txt");
std::string delimiter = " ";
std::string line;
int counter_query = 0;
int counter_total = 0;
int total_camera_id = 0;
while (getline(queryfile, line)) {
std::string name = CutStringBefore(line, "/");
if (name == "query") {
++counter_query;
}
++counter_total;
if (counter_query == query_camera_id + 1) {
total_camera_id = counter_total;
break;
}
}
queryfile.close();
bundler_camera camera = pb.get_cameras()[total_camera_id - 1];
// We have camera position and rotation, unlike bundler, so transform:
Eigen::Matrix<double, 3, 3> xzflip;
xzflip << -1, 0, 0, 0, 1, 0, 0, 0, -1;
Eigen::Matrix<double, 3, 3> rotation = camera.rotation;
Eigen::Matrix<double, 3, 1> position = -rotation.transpose()
* camera.translation;
// Convert to our own format
Query query(nullptr, "", camera.focal_length);
query.set_camera_position(position);
rotation = (xzflip * rotation).transpose();
query.set_camera_rotation(rotation);
query.set_image_width(camera.width);
query.set_image_height(camera.height);
return query;
}
void ExportCameraProjectEdges(Query &query, std::string path,
std::string filename) {
std::ofstream out_file_obj;
out_file_obj.open(path + filename + ".obj");
assert(out_file_obj.is_open());
std::vector<Eigen::Matrix<double, 3, 1>> &points_3d = query.fitpoints_3d();
Eigen::Matrix<double, 3, 1> camera_position = query.camera_position();
out_file_obj << "o Object" << std::endl;
out_file_obj << "v " << std::setprecision(16) << camera_position(0) << " "
<< std::setprecision(16) << camera_position(1) << " "
<< std::setprecision(16) << camera_position(2) << std::endl;
out_file_obj << "v " << std::setprecision(16) << camera_position(0) << " "
<< std::setprecision(16) << camera_position(1) << " "
<< std::setprecision(16) << camera_position(2) << std::endl;
for (unsigned int i = 0; i < points_3d.size(); ++i) {
Eigen::Matrix<double, 3, 1> point_3d = points_3d[i];
out_file_obj << "v " << std::setprecision(16) << point_3d(0) << " "
<< std::setprecision(16) << point_3d(1) << " "
<< std::setprecision(16) << point_3d(2) << std::endl;
}
out_file_obj << "s off" << std::endl;
for (unsigned int i = 0; i < points_3d.size(); ++i) {
out_file_obj << "f 1 " << i + 3 << " 2" << std::endl;
}
out_file_obj.close();
}
// Export the camera pose as OBJ+MTL (wavefront obj with material) and MLP (meshlab project file)
void ExportCamera(Query &query, std::string path, std::string filename) {
Eigen::Matrix<double, 3, 1> camera_position = query.camera_position();
Eigen::Matrix<double, 3, 3> camera_rotation = query.camera_rotation();
Eigen::Matrix<double, 3, 3> xzflip;
xzflip << -1, 0, 0, 0, 1, 0, 0, 0, -1;
Eigen::Matrix<double, 3, 3> camera_rotation_xzf = xzflip
* camera_rotation.transpose();
std::ofstream out_file_obj;
std::ofstream out_file_mtl;
std::ofstream out_file_mlp;
out_file_obj.open(path + filename + ".obj");
assert(out_file_obj.is_open());
out_file_mtl.open(path + filename + ".mtl");
assert(out_file_mtl.is_open());
out_file_mlp.open(path + filename + ".mlp");
// Write obj header
out_file_obj << "mtllib " << filename << ".mtl" << std::endl;
out_file_obj << "o Object" << std::endl;
out_file_obj << std::fixed;
// Image plane vertices
for (int i = 0; i < 4; ++i) {
double x = 0.01 * query.image_width() * std::sin((i * 2 + 1) * M_PI / 4)
/ sqrt(2);
double y = 0.01 * query.image_height()
* std::cos((i * 2 + 1) * M_PI / 4) / sqrt(2);
double z = query.focal_length() / 100.0;
// Transform to world coordinates
Eigen::Matrix<double, 3, 1> xyz;
xyz << x, y, z;
xyz = camera_rotation * xyz + camera_position;
x = xyz(0);
y = xyz(1);
z = xyz(2);
float u = (0.5 + std::sin(-(i * 2 + 1) * M_PI / 4) / sqrt(2.0));
float v = (0.5 + std::cos((i * 2 + 1) * M_PI / 4) / sqrt(2.0));
out_file_obj << "v " << std::setprecision(16) << x << " "
<< std::setprecision(16) << y << " " << std::setprecision(16)
<< z << std::endl;
out_file_obj << "vt " << std::setprecision(16) << u << " "
<< std::setprecision(16) << v << std::endl;
}
// Camera box vertices
out_file_obj << "v " << std::setprecision(16) << camera_position(0) << " "
<< std::setprecision(16) << camera_position(1) << " "
<< std::setprecision(16) << camera_position(2) << std::endl;
for (int i = 0; i < 4; ++i) {
double x = 0.001 * query.image_width()
* std::sin((i * 2 + 1) * M_PI / 4) / sqrt(2);
double y = 0.001 * query.image_height()
* std::cos((i * 2 + 1) * M_PI / 4) / sqrt(2);
double z = query.focal_length() / 1000.0;
// Transform to world coordinates
Eigen::Matrix<double, 3, 1> xyz;
xyz << x, y, z;
xyz = camera_rotation * xyz + camera_position;
x = xyz(0);
y = xyz(1);
z = xyz(2);
out_file_obj << "v " << std::setprecision(16) << x << " "
<< std::setprecision(16) << y << " " << std::setprecision(16)
<< z << std::endl;
}
out_file_obj << "g Object_Object_auv" << std::endl;
out_file_obj << "usemtl Object_auv" << std::endl;
out_file_obj << "s off" << std::endl;
// Image plane faces
out_file_obj << "f 1/1 2/2 3/3" << std::endl;
out_file_obj << "f 1/1 3/3 4/4" << std::endl;
// Camera box faces
out_file_obj << "f 5 6 7" << std::endl;
out_file_obj << "f 5 7 8" << std::endl;
out_file_obj << "f 5 8 9" << std::endl;
out_file_obj << "f 5 6 9" << std::endl;
out_file_obj << "f 6 7 8" << std::endl;
out_file_obj << "f 6 8 9" << std::endl;
// Write mtl
out_file_mtl << "newmtl Object_auv" << std::endl;
out_file_mtl << "Ns 100.0" << std::endl;
out_file_mtl << "d 1.0" << std::endl;
out_file_mtl << "illum 2" << std::endl;
out_file_mtl << "Kd 1.0 1.0 1.0" << std::endl;
out_file_mtl << "Ka 1.0 1.0 1.0" << std::endl;
out_file_mtl << "Ks 1.0 1.0 1.0" << std::endl;
out_file_mtl << "Ke 0.0 0.0 0.0" << std::endl;
out_file_mtl << "map_Kd " << filename << ".jpg" << std::endl;
// Write mlp
out_file_mlp << "<!DOCTYPE MeshLabDocument>" << std::endl;
out_file_mlp << "<MeshLabProject>" << std::endl;
out_file_mlp << "<RasterGroup>" << std::endl;
// The camera
out_file_mlp << "<MLRaster label=" << "\"" << filename + ".jpg" << "\">"
<< std::endl;
out_file_mlp << "<VCGCamera ";
// Translation
out_file_mlp << "TranslationVector=\"" << std::setprecision(16)
<< -camera_position(0) << " " << std::setprecision(16)
<< -camera_position(1) << " " << std::setprecision(16)
<< -camera_position(2) << "\" ";
// Various parameters
out_file_mlp << "LensDistortion=\"0 0\" ";
out_file_mlp << "ViewportPx=\"" << query.image_width() << " "
<< query.image_height() << "\" ";
out_file_mlp << "PixelSizeMm=\"0.001 0.001\" ";
out_file_mlp << "CenterPx=\"" << query.image_width() / 2 << " "
<< query.image_height() / 2 << "\" ";
out_file_mlp << "FocalMm=\"" << query.focal_length() / 1000.0 << "\" ";
// Rotation matrix
out_file_mlp << "RotationMatrix=\"" << std::setprecision(16)
<< camera_rotation_xzf(0, 0) << " " << std::setprecision(16)
<< camera_rotation_xzf(0, 1) << " " << std::setprecision(16)
<< camera_rotation_xzf(0, 2) << " " << std::setprecision(16) << 0.0
<< " " << std::setprecision(16) << camera_rotation_xzf(1, 0) << " "
<< std::setprecision(16) << camera_rotation_xzf(1, 1) << " "
<< std::setprecision(16) << camera_rotation_xzf(1, 2) << " "
<< std::setprecision(16) << 0.0 << " " << std::setprecision(16)
<< camera_rotation_xzf(2, 0) << " " << std::setprecision(16)
<< camera_rotation_xzf(2, 1) << " " << std::setprecision(16)
<< camera_rotation_xzf(2, 2) << " " << std::setprecision(16) << 0.0
<< " 0.0 0.0 0.0 1.0" << "\" ";
out_file_mlp << "/>" << std::endl;
// The image file
out_file_mlp << "<Plane semantic=\"\" fileName=\"" << filename + ".jpg"
<< "\"/>" << std::endl;
out_file_mlp << "</MLRaster>" << std::endl;
out_file_mlp << "</RasterGroup>" << std::endl;
out_file_mlp << "</MeshLabProject>" << std::endl;
out_file_obj.close();
out_file_mtl.close();
out_file_mlp.close();
cv::imwrite(path + filename + ".jpg", query.image());
}
}