-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathvisualization.cpp
More file actions
316 lines (279 loc) · 13.5 KB
/
Copy pathvisualization.cpp
File metadata and controls
316 lines (279 loc) · 13.5 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
// Created by visionlab on 3/2/18.
//
#include "tool.h"
#include "json/json.h"
// libigl
#include "igl/readOBJ.h"
#include "igl/writeOBJ.h"
#include "igl/readPLY.h"
// Open3D
#include "IO/IO.h"
#include "Visualization/Visualization.h"
// OpenCV
#include "opencv2/core/core.hpp"
// feh
#include "utils.h"
#include "geometry.h"
#include "dataloader.h"
#include "vlslam.pb.h"
namespace feh {
void AssembleScene(const Json::Value &config,
const std::list<std::pair<std::string, Eigen::Matrix<double, 3, 4>>> &objects,
const Eigen::Matrix<double, 3, 4> &alignment,
std::vector<Eigen::Matrix<double, 3, 1>> &vertices,
std::vector<Eigen::Matrix<int, 3, 1>> &faces) {
std::string database_dir = config["CAD_database_root"].asString();
std::string dataroot = config["dataroot"].asString();
std::string dataset = config["dataset"].asString();
std::string scene_dir = dataroot + "/" + dataset + "/";
std::string fragment_dir = scene_dir + "/fragments/";
// LOAD FLAGS
bool show_original = config["scene_assembler"].get("show_original_scene", true).asBool();
bool remove_original = config["scene_assembler"].get("remove_original_objects", true).asBool();
double padding_size = config["scene_assembler"].get("padding_size", 0.0).asDouble();
// LOAD SCENE POINT CLOUD
std::list<Eigen::Matrix<double, 3, 1>> sceneV;
if (show_original) {
auto scene = std::make_shared<open3d::PointCloud>();
try {
open3d::ReadPointCloudFromPLY(scene_dir + "/test.klg.ply", *scene);
for (int i = 0; i < scene->points_.size(); ++i) {
sceneV.push_back({});
sceneV.back().head<3>() = scene->points_[i];
// sceneV.back().tail<3>() = scene->colors_[i]; // / 255.0;
}
} catch (...) {
std::cout << TermColor::bold + TermColor::red << "GROUND TRUTH POINT CLOUD NOT EXIST" << TermColor::endl;
}
}
for (const auto &obj : objects) {
std::string model_name = obj.first;
auto pose = obj.second;
// LOAD MESH
Eigen::Matrix<double, Eigen::Dynamic, 3> v;
Eigen::Matrix<int, Eigen::Dynamic, 3> f;
Eigen::Matrix<double, Eigen::Dynamic, 6> tmp;
igl::readOBJ(StrFormat("%s/%s.obj", database_dir, model_name), tmp, f);
v = tmp.leftCols(3);
std::cout << "v.size=" << v.rows() << "x" << v.cols() << "\n";
// TRANSFORM TO SCENE FRAME
v.leftCols(3) = (v.leftCols(3) * pose.block<3,3>(0,0).transpose()).rowwise() + pose.block<3, 1>(0, 3).transpose();
v.leftCols(3) = (v.leftCols(3) * alignment.block<3,3>(0,0).transpose()).rowwise() + alignment.block<3, 1>(0, 3).transpose();
int v_offset = vertices.size();
for (int i = 0; i < v.rows(); ++i) {
vertices.push_back(v.row(i).head<3>());
}
if (show_original && !sceneV.empty() && remove_original) {
std::array<Eigen::Vector3d, 2> bounds;
bounds[0] = Eigen::Vector3d::Ones() * std::numeric_limits<double>::max();
bounds[1] = Eigen::Vector3d::Ones() * std::numeric_limits<double>::lowest();
for (int i = 0; i < v.rows(); ++i) {
for (int j = 0; j < 3; ++j) {
if (v(i, j) < bounds[0](j)) bounds[0](j) = v(i, j);
if (v(i, j) > bounds[1](j)) bounds[1](j) = v(i, j);
}
}
// expand the bounds a little bit
bounds[0].array() -= padding_size;
bounds[1].array() += padding_size;
for (auto it = sceneV.begin(); it != sceneV.end(); ) {
Eigen::Vector3d v = it->head<3>();
bool in_bound = true;
for (int i = 0; i < 3; ++i)
if (!(v(i) > bounds[0](i) && v(i) < bounds[1](i))) {
in_bound = false;
break;
}
if (in_bound) {
it = sceneV.erase(it);
} else ++it;
}
}
for (int i = 0; i < f.rows(); ++i) {
faces.push_back({v_offset + f(i, 0), v_offset + f(i, 1), v_offset + f(i, 2)});
}
}
if (!sceneV.empty()) vertices.insert(vertices.end(), sceneV.begin(), sceneV.end());
}
void AssembleResult(const Json::Value &config,
Eigen::Matrix<double, Eigen::Dynamic, 3> *Vout,
Eigen::Matrix<int, Eigen::Dynamic, 3> *Fout,
std::vector<Eigen::Matrix<double, 3, 4>> *Gout) {
// EXTRACT PATHS
std::string database_dir = config["CAD_database_root"].asString();
std::string dataroot = config["dataroot"].asString();
std::string dataset = config["dataset"].asString();
std::string scene_dir = dataroot + "/" + dataset + "/";
std::string fragment_dir = scene_dir + "/fragments/";
// FILE I/O BUFFER
std::string contents;
// READ THE CORVIS TO ELASTICFUSION ALIGNMENT
std::string result_alignment_file = scene_dir + "/result_alignment.json";
Eigen::Matrix<double, 3, 4> T_ef_corvis;
try {
auto result_alignment = LoadJson(result_alignment_file);
T_ef_corvis = GetMatrixFromJson<double, 3, 4>(result_alignment, "T_ef_corvis").block<3, 4>(0, 0);
} catch (...) {
std::cout << TermColor::bold + TermColor::red << "failed to load result alignment; use identity transformation!!!" << TermColor::endl;
T_ef_corvis.block<3, 3>(0, 0).setIdentity();
}
std::cout << "result_alignment=\n" << T_ef_corvis << "\n";
// LOAD RESULT FILE
std::string result_file = scene_dir + "/result.json";
auto result = LoadJson(result_file);
// ITERATE AND GET THE LAST ONE
int result_index = config["result_visualization"].get("result_index", -1).asInt();
if (result_index < 0) result_index = result.size()-1;
auto packet = result[result_index];
std::list<std::pair<std::string, Eigen::Matrix<double, 3, 4>>> objects;
for (const auto &obj : packet) {
auto pose = GetMatrixFromJson<double, 3, 4>(obj, "model_pose");
std::cout << StrFormat("id=%d\nstatus=%d\nshape=%s\npose=\n",
obj["id"].asInt(),
obj["status"].asInt(),
obj["model_name"].asString())
<< pose << "\n";
std::string model_name = obj["model_name"].asString();
objects.push_back(std::make_pair(model_name, pose));
}
std::vector<Eigen::Matrix<double, 3, 1>> vertices;
std::vector<Eigen::Matrix<int, 3, 1>> faces;
AssembleScene(config, objects, T_ef_corvis, vertices, faces);
igl::writeOBJ(scene_dir + "/result_augmented_view.obj",
StdVectorOfEigenVectorToEigenMatrix(vertices),
StdVectorOfEigenVectorToEigenMatrix(faces));
if (Vout && Fout) {
*Vout = StdVectorOfEigenVectorToEigenMatrix(vertices);
*Fout = StdVectorOfEigenVectorToEigenMatrix(faces);
}
if (Gout) {
for (const auto &obj : objects) {
auto R = T_ef_corvis.block<3, 3>(0, 0) * obj.second.block<3, 3>(0, 0);
auto T = T_ef_corvis.block<3, 3>(0, 0) * obj.second.block<3, 1>(0, 3) + T_ef_corvis.block<3, 1>(0, 3);
Gout->push_back((Eigen::Matrix<double, 3, 4>() << R, T).finished());
}
}
}
void AssembleGroundTruth(const Json::Value &config,
Eigen::Matrix<double, Eigen::Dynamic, 3> *Vout,
Eigen::Matrix<int, Eigen::Dynamic, 3> *Fout,
std::vector<Eigen::Matrix<double, 3, 4>> *Gout) {
std::string database_dir = config["CAD_database_root"].asString();
std::string dataroot = config["dataroot"].asString();
std::string dataset = config["dataset"].asString();
std::string scene_dir = dataroot + "/" + dataset + "/";
std::string fragment_dir = scene_dir + "/fragments/";
// LOAD GROUND TRUTH ALIGNMENT
std::string alignment_file = fragment_dir + "/alignment.json";
auto alignment = LoadJson(alignment_file);
std::list<std::pair<std::string, Eigen::Matrix<double, 3, 4>>> objects;
// for (const auto &obj : alignment.keys()) {
for (auto it = alignment.begin(); it != alignment.end(); ++it) {
std::string obj_name = it.key().asString();
std::string model_name = obj_name.substr(0, obj_name.find_last_of('_'));
auto pose = GetMatrixFromJson<double, 3, 4>(alignment, obj_name);
std::cout << obj_name << "\n" << model_name << "\n" << pose << "\n";
objects.push_back(std::make_pair(model_name, pose));
}
Eigen::Matrix<double, 3, 4> identity;
identity.setZero();
identity(0, 0) = 1; identity(1, 1) = 1; identity(2, 2) = 1;
std::vector<Eigen::Matrix<double, 3, 1>> vertices;
std::vector<Eigen::Matrix<int, 3, 1>> faces;
AssembleScene(config, objects, identity, vertices, faces);
igl::writeOBJ(scene_dir + "/ground_truth_augmented_view.obj",
StdVectorOfEigenVectorToEigenMatrix(vertices),
StdVectorOfEigenVectorToEigenMatrix(faces));
if (Vout && Fout) {
*Vout = StdVectorOfEigenVectorToEigenMatrix(vertices);
*Fout = StdVectorOfEigenVectorToEigenMatrix(faces);
}
if (Gout) {
for (const auto &obj : objects) {
auto R = identity.block<3, 3>(0, 0) * obj.second.block<3, 3>(0, 0);
auto T = identity.block<3, 3>(0, 0) * obj.second.block<3, 1>(0, 3)
+ identity.block<3, 1>(0, 3);
Gout->push_back((Eigen::Matrix<double, 3, 4>() << R, T).finished());
}
}
}
void VisualizeResult(const Json::Value &config) {
Eigen::Matrix<double, Eigen::Dynamic, 3> V, Vtot;
Eigen::Matrix<int, Eigen::Dynamic, 3> F;
AssembleResult(config, &V, &F);
std::string dataset_path = config["experiment_root"].asString() + "/" + config["dataset"].asString();
std::string scene_dir = StrFormat("%s/%s/",
config["dataroot"].asString(), config["dataset"].asString());
Eigen::Matrix<double, Eigen::Dynamic, 6> traj, pts;
if (config["result_visualization"]["show_trajectory"].asBool()) {
std::string contents;
std::string result_alignment_file = scene_dir + "/result_alignment.json";
Eigen::Matrix<double, 3, 4> T_ef_corvis;
try {
auto result_alignment = LoadJson(result_alignment_file);
T_ef_corvis = GetMatrixFromJson<double, 3, 4>(result_alignment, "T_ef_corvis").block<3, 4>(0, 0);
} catch (...) {
std::cout << TermColor::bold + TermColor::red << "failed to load result alignment; use identity transformation!!!" << TermColor::endl;
T_ef_corvis.block<3, 3>(0, 0).setIdentity();
}
std::cout << "result_alignment=\n" << T_ef_corvis << "\n";
std::ifstream in_file(dataset_path + "/dataset");
CHECK(in_file.is_open()) << "failed to open dataset @ " << dataset_path;
vlslam_pb::Dataset dataset;
dataset.ParseFromIstream(&in_file);
in_file.close();
std::vector<Eigen::Matrix<double, 6, 1>> vtraj;
for (int i = 0; i < dataset.packets_size(); ++i) {
auto packet = dataset.mutable_packets(i);
const auto gwc = SE3f(SE3FromArray(packet->mutable_gwc()->mutable_data()));
auto Twc = T_ef_corvis.block<3, 3>(0, 0) * gwc.translation().cast<double>() + T_ef_corvis.block<3, 1>(0, 3);
vtraj.push_back({});
vtraj.back() << Twc(0), Twc(1), Twc(2), 1.0, 0.5, 0.0;
}
traj = StdVectorOfEigenVectorToEigenMatrix(vtraj);
}
if (config["result_visualization"]["show_pointcloud"].asBool()) {
std::string contents;
std::string result_alignment_file = scene_dir + "/result_alignment.json";
Eigen::Matrix<double, 3, 4> T_ef_corvis;
try {
auto result_alignment = LoadJson(result_alignment_file);
T_ef_corvis = GetMatrixFromJson<double, 3, 4>(result_alignment, "T_ef_corvis").block<3, 4>(0, 0);
} catch (...) {
std::cout << TermColor::bold + TermColor::red << "failed to load result alignment; use identity transformation!!!" << TermColor::endl;
T_ef_corvis.block<3, 3>(0, 0).setIdentity();
}
std::cout << "result_alignment=\n" << T_ef_corvis << "\n";
std::ifstream in_file(dataset_path + "/dataset");
CHECK(in_file.is_open()) << "failed to open dataset @ " << dataset_path;
vlslam_pb::Dataset dataset;
dataset.ParseFromIstream(&in_file);
in_file.close();
std::unordered_map<uint64_t, Eigen::Matrix<double, 6, 1>> mpts;
for (int i = 0; i < dataset.packets_size(); ++i) {
auto packet = dataset.packets(i);
for (int j = 0; j < packet.features_size(); ++j) {
auto feature = packet.features(j);
if (feature.status() == vlslam_pb::Feature::INSTATE) {
mpts[feature.id()] << T_ef_corvis.block<3, 3>(0, 0)
* Eigen::Vector3d{feature.xw(0), feature.xw(1), feature.xw(2)}
+ T_ef_corvis.block<3, 1>(0, 3),
0, 0.5, 1.0;
}
}
}
// convert map to eigen matrix
pts.resize(mpts.size(), 6);
int counter(0);
for (auto it = mpts.begin(); it != mpts.end(); ++it) {
pts.row(counter++) = it->second;
}
}
// ASSEMBLE
Vtot.resize(V.rows() + traj.rows() + pts.rows(), 6);
Vtot << V, traj, pts;
igl::writeOBJ(scene_dir + "/result_with_trajectory_and_pointcloud.obj",
Vtot, F);
}
}