File: VisualizerForAlignment.cpp

package info (click to toggle)
open3d 0.19.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 83,496 kB
  • sloc: cpp: 206,543; python: 27,254; ansic: 8,356; javascript: 1,883; sh: 1,527; makefile: 259; xml: 69
file content (354 lines) | stat: -rw-r--r-- 16,115 bytes parent folder | download | duplicates (2)
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// ----------------------------------------------------------------------------
// -                        Open3D: www.open3d.org                            -
// ----------------------------------------------------------------------------
// Copyright (c) 2018-2024 www.open3d.org
// SPDX-License-Identifier: MIT
// ----------------------------------------------------------------------------

#include "tools/ManuallyAlignPointCloud/VisualizerForAlignment.h"

#include <tinyfiledialogs/tinyfiledialogs.h>

namespace open3d {

void VisualizerForAlignment::PrintVisualizerHelp() {
    visualization::Visualizer::PrintVisualizerHelp();
    // clang-format off
    utility::LogInfo("  -- Alignment control --");
    utility::LogInfo("    Ctrl + R     : Reset source and target to initial state.");
    utility::LogInfo("    Ctrl + S     : Save current alignment session into a JSON file.");
    utility::LogInfo("    Ctrl + O     : Load current alignment session from a JSON file.");
    utility::LogInfo("    Ctrl + A     : Align point clouds based on manually annotations.");
    utility::LogInfo("    Ctrl + I     : Run ICP refinement.");
    utility::LogInfo("    Ctrl + D     : Run voxel downsample for both source and target.");
    utility::LogInfo("    Ctrl + K     : Load a polygon from a JSON file and crop source.");
    utility::LogInfo("    Ctrl + E     : Evaluate error and save to files.");
    // clang-format on
}

bool VisualizerForAlignment::AddSourceAndTarget(
        std::shared_ptr<geometry::PointCloud> source,
        std::shared_ptr<geometry::PointCloud> target) {
    GetRenderOption().point_size_ = 1.0;
    alignment_session_.source_ptr_ = source;
    alignment_session_.target_ptr_ = target;
    source_copy_ptr_ = std::make_shared<geometry::PointCloud>();
    target_copy_ptr_ = std::make_shared<geometry::PointCloud>();
    *source_copy_ptr_ = *source;
    *target_copy_ptr_ = *target;
    return AddGeometry(source_copy_ptr_) && AddGeometry(target_copy_ptr_);
}

void VisualizerForAlignment::KeyPressCallback(
        GLFWwindow *window, int key, int scancode, int action, int mods) {
    if (action == GLFW_PRESS && (mods & GLFW_MOD_CONTROL)) {
        const char *filename;
        const char *pattern[1] = {"*.json"};
        switch (key) {
            case GLFW_KEY_R: {
                *source_copy_ptr_ = *alignment_session_.source_ptr_;
                *target_copy_ptr_ = *alignment_session_.target_ptr_;
                ResetViewPoint(true);
                UpdateGeometry();
                return;
            }
            case GLFW_KEY_S: {
                std::string default_alignment =
                        default_directory_ + "alignment.json";
                if (use_dialog_) {
                    filename = tinyfd_saveFileDialog(
                            "Alignment session", default_alignment.c_str(), 1,
                            pattern, "JSON file (*.json)");
                } else {
                    filename = default_alignment.c_str();
                }
                if (filename != NULL) {
                    SaveSessionToFile(filename);
                }
                return;
            }
            case GLFW_KEY_O: {
                std::string default_alignment =
                        default_directory_ + "alignment.json";
                if (use_dialog_) {
                    filename = tinyfd_openFileDialog(
                            "Alignment session", default_alignment.c_str(), 1,
                            pattern, "JSON file (*.json)", 0);
                } else {
                    filename = default_alignment.c_str();
                }
                if (filename != NULL) {
                    LoadSessionFromFile(filename);
                }
                return;
            }
            case GLFW_KEY_A: {
                if (AlignWithManualAnnotation()) {
                    ResetViewPoint(true);
                    UpdateGeometry();
                }
                return;
            }
            case GLFW_KEY_I: {
                if (use_dialog_) {
                    std::string buffer =
                            fmt::format("{:.4f}", max_correspondence_distance_);
                    const char *str = tinyfd_inputBox(
                            "Set voxel size",
                            "Set max correspondence distance for ICP (ignored "
                            "if it is non-positive)",
                            buffer.c_str());
                    if (str == NULL) {
                        utility::LogWarning("Dialog closed.");
                        return;
                    } else {
                        char *end;
                        errno = 0;
                        double l = std::strtod(str, &end);
                        if (errno == ERANGE &&
                            (l == HUGE_VAL || l == -HUGE_VAL)) {
                            utility::LogWarning(
                                    "Illegal input, use default max "
                                    "correspondence distance.");
                        } else {
                            max_correspondence_distance_ = l;
                        }
                    }
                }
                if (max_correspondence_distance_ > 0.0) {
                    utility::LogInfo(
                            "ICP with max correspondence distance {:.4f}.",
                            max_correspondence_distance_);
                    auto result = pipelines::registration::RegistrationICP(
                            *source_copy_ptr_, *target_copy_ptr_,
                            max_correspondence_distance_,
                            Eigen::Matrix4d::Identity(),
                            pipelines::registration::
                                    TransformationEstimationPointToPoint(true),
                            pipelines::registration::ICPConvergenceCriteria(
                                    1e-6, 1e-6, 30));
                    utility::LogInfo(
                            "Registration finished with fitness {:.4f} and "
                            "RMSE {:.4f}.",
                            result.fitness_, result.inlier_rmse_);
                    if (result.fitness_ > 0.0) {
                        transformation_ =
                                result.transformation_ * transformation_;
                        PrintTransformation();
                        source_copy_ptr_->Transform(result.transformation_);
                        UpdateGeometry();
                    }
                } else {
                    utility::LogWarning(
                            "No ICP performed due to illegal max "
                            "correspondence distance.");
                }
                return;
            }
            case GLFW_KEY_D: {
                if (use_dialog_) {
                    std::string buffer = fmt::format("{:.4f}", voxel_size_);
                    const char *str = tinyfd_inputBox(
                            "Set voxel size",
                            "Set voxel size (ignored if it is non-positive)",
                            buffer.c_str());
                    if (str == NULL) {
                        utility::LogWarning("Dialog closed.");
                        return;
                    } else {
                        char *end;
                        errno = 0;
                        double l = std::strtod(str, &end);
                        if (errno == ERANGE &&
                            (l == HUGE_VAL || l == -HUGE_VAL)) {
                            utility::LogWarning(
                                    "Illegal input, use default voxel size.");
                        } else {
                            voxel_size_ = l;
                        }
                    }
                }
                if (voxel_size_ > 0.0) {
                    utility::LogInfo("Voxel downsample with voxel size {:.4f}.",
                                     voxel_size_);
                    *source_copy_ptr_ =
                            *source_copy_ptr_->VoxelDownSample(voxel_size_);
                    UpdateGeometry();
                } else {
                    utility::LogWarning(
                            "No voxel downsample performed due to illegal "
                            "voxel size.");
                }
                return;
            }
            case GLFW_KEY_K: {
                if (!utility::filesystem::FileExists(polygon_filename_)) {
                    if (use_dialog_) {
                        if (const char *polygon_filename_chars =
                                    tinyfd_openFileDialog("Bounding polygon",
                                                          "polygon.json", 0,
                                                          NULL, NULL, 0)) {
                            polygon_filename_ =
                                    std::string(polygon_filename_chars);
                        } else {
                            utility::LogError(
                                    "Internal error: tinyfd_openFileDialog "
                                    "returned nullptr.");
                        }
                    } else {
                        polygon_filename_ = "polygon.json";
                    }
                }
                auto polygon_volume = std::make_shared<
                        visualization::SelectionPolygonVolume>();
                if (io::ReadIJsonConvertible(polygon_filename_,
                                             *polygon_volume)) {
                    *source_copy_ptr_ =
                            *polygon_volume->CropPointCloud(*source_copy_ptr_);
                    ResetViewPoint(true);
                    UpdateGeometry();
                }
                return;
            }
            case GLFW_KEY_E: {
                std::string default_alignment =
                        default_directory_ + "alignment.json";
                if (use_dialog_) {
                    filename = tinyfd_saveFileDialog(
                            "Alignment session", default_alignment.c_str(), 1,
                            pattern, "JSON file (*.json)");
                } else {
                    filename = default_alignment.c_str();
                }
                if (filename != NULL) {
                    SaveSessionToFile(filename);
                    EvaluateAlignmentAndSave(filename);
                }
                return;
            }
        }
    }
    visualization::Visualizer::KeyPressCallback(window, key, scancode, action,
                                                mods);
}

bool VisualizerForAlignment::SaveSessionToFile(const std::string &filename) {
    alignment_session_.source_indices_ = source_visualizer_.GetPickedPoints();
    alignment_session_.target_indices_ = target_visualizer_.GetPickedPoints();
    alignment_session_.voxel_size_ = voxel_size_;
    alignment_session_.max_correspondence_distance_ =
            max_correspondence_distance_;
    alignment_session_.with_scaling_ = with_scaling_;
    alignment_session_.transformation_ = transformation_;
    return io::WriteIJsonConvertible(filename, alignment_session_);
}

bool VisualizerForAlignment::LoadSessionFromFile(const std::string &filename) {
    if (!io::ReadIJsonConvertible(filename, alignment_session_)) {
        return false;
    }
    source_visualizer_.GetPickedPoints() = alignment_session_.source_indices_;
    target_visualizer_.GetPickedPoints() = alignment_session_.target_indices_;
    voxel_size_ = alignment_session_.voxel_size_;
    max_correspondence_distance_ =
            alignment_session_.max_correspondence_distance_;
    with_scaling_ = alignment_session_.with_scaling_;
    transformation_ = alignment_session_.transformation_;
    *source_copy_ptr_ = *alignment_session_.source_ptr_;
    source_copy_ptr_->Transform(transformation_);
    source_visualizer_.UpdateRender();
    target_visualizer_.UpdateRender();
    ResetViewPoint(true);
    return UpdateGeometry();
}

bool VisualizerForAlignment::AlignWithManualAnnotation() {
    const auto &source_idx = source_visualizer_.GetPickedPoints();
    const auto &target_idx = target_visualizer_.GetPickedPoints();
    if (source_idx.empty() || target_idx.empty() ||
        source_idx.size() != target_idx.size()) {
        utility::LogWarning(
                "# of picked points mismatch: {:d} in source, {:d} in "
                "target.",
                (int)source_idx.size(), (int)target_idx.size());
        return false;
    }
    pipelines::registration::TransformationEstimationPointToPoint p2p(
            with_scaling_);
    pipelines::registration::CorrespondenceSet corres;
    for (size_t i = 0; i < source_idx.size(); i++) {
        corres.push_back(Eigen::Vector2i(source_idx[i], target_idx[i]));
    }
    utility::LogInfo("Error is {:.4f} before alignment.",
                     p2p.ComputeRMSE(*alignment_session_.source_ptr_,
                                     *alignment_session_.target_ptr_, corres));
    transformation_ =
            p2p.ComputeTransformation(*alignment_session_.source_ptr_,
                                      *alignment_session_.target_ptr_, corres);
    PrintTransformation();
    *source_copy_ptr_ = *alignment_session_.source_ptr_;
    source_copy_ptr_->Transform(transformation_);
    utility::LogInfo("Error is {:.4f} before alignment.",
                     p2p.ComputeRMSE(*source_copy_ptr_,
                                     *alignment_session_.target_ptr_, corres));
    return true;
}

void VisualizerForAlignment::PrintTransformation() {
    utility::LogInfo("Current transformation is:");
    utility::LogInfo("\t{:.6f} {:.6f} {:.6f} {:.6f}", transformation_(0, 0),
                     transformation_(0, 1), transformation_(0, 2),
                     transformation_(0, 3));
    utility::LogInfo("\t{:.6f} {:.6f} {:.6f} {:.6f}", transformation_(1, 0),
                     transformation_(1, 1), transformation_(1, 2),
                     transformation_(1, 3));
    utility::LogInfo("\t{:.6f} {:.6f} {:.6f} {:.6f}", transformation_(2, 0),
                     transformation_(2, 1), transformation_(2, 2),
                     transformation_(2, 3));
    utility::LogInfo("\t{:.6f} {:.6f} {:.6f} {:.6f}", transformation_(3, 0),
                     transformation_(3, 1), transformation_(3, 2),
                     transformation_(3, 3));
}

void VisualizerForAlignment::EvaluateAlignmentAndSave(
        const std::string &filename) {
    // Evaluate source_copy_ptr_ and target_copy_ptr_
    std::string source_filename =
            utility::filesystem::GetFileNameWithoutExtension(filename) +
            ".source.ply";
    std::string target_filename =
            utility::filesystem::GetFileNameWithoutExtension(filename) +
            ".target.ply";
    std::string source_binname =
            utility::filesystem::GetFileNameWithoutExtension(filename) +
            ".source.bin";
    std::string target_binname =
            utility::filesystem::GetFileNameWithoutExtension(filename) +
            ".target.bin";
    FILE *f;

    io::WritePointCloud(source_filename, *source_copy_ptr_);
    auto source_dis =
            source_copy_ptr_->ComputePointCloudDistance(*target_copy_ptr_);
    f = utility::filesystem::FOpen(source_binname, "wb");
    if (!f) {
        utility::LogError("EvaluateAlignmentAndSave: Unable to open file {}.",
                          source_binname);
        return;
    }
    fwrite(source_dis.data(), sizeof(double), source_dis.size(), f);
    fclose(f);
    io::WritePointCloud(target_filename, *target_copy_ptr_);
    auto target_dis =
            target_copy_ptr_->ComputePointCloudDistance(*source_copy_ptr_);
    f = utility::filesystem::FOpen(target_binname, "wb");
    if (!f) {
        utility::LogError("EvaluateAlignmentAndSave: Unable to open file {}.",
                          target_binname);
        return;
    }
    fwrite(target_dis.data(), sizeof(double), target_dis.size(), f);
    fclose(f);
}

}  // namespace open3d