From f449b3d9ea252022b43ebb543a1b30bf6c32f664 Mon Sep 17 00:00:00 2001 From: Rainer Kuemmerle Date: Sun, 21 Jul 2024 15:48:11 +0200 Subject: [PATCH] Support file formats in g2o_viewer --- g2o/apps/g2o_viewer/main_window.cpp | 14 ++++++++-- g2o/core/CMakeLists.txt | 1 + g2o/core/io/io_format.cpp | 42 +++++++++++++++++++++++++++++ g2o/core/io/io_format.h | 13 +++++++++ 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 g2o/core/io/io_format.cpp diff --git a/g2o/apps/g2o_viewer/main_window.cpp b/g2o/apps/g2o_viewer/main_window.cpp index 708e9af0a..e003d3629 100644 --- a/g2o/apps/g2o_viewer/main_window.cpp +++ b/g2o/apps/g2o_viewer/main_window.cpp @@ -25,14 +25,17 @@ #include #include #include +#include #include "g2o/core/estimate_propagator.h" +#include "g2o/core/io/io_format.h" #include "g2o/core/optimization_algorithm.h" #include "g2o/core/optimization_algorithm_factory.h" #include "g2o/core/optimization_algorithm_property.h" #include "g2o/core/robust_kernel.h" #include "g2o/core/robust_kernel_factory.h" #include "g2o/core/sparse_optimizer.h" +#include "g2o/stuff/filesys_tools.h" #include "g2o/stuff/logger.h" #include "properties_widget.h" #include "viewer_properties_widget.h" @@ -221,9 +224,16 @@ bool MainWindow::load(const QString& filename) { std::cerr << "reading stdin\n"; loadStatus = viewer->graph->load(std::cin); } else { - std::ifstream ifs(filename.toStdString().c_str()); + const std::string filename_as_std = filename.toStdString(); + const std::string filename_extension = + g2o::getFileExtension(filename_as_std); + std::optional file_format = + g2o::io::formatForFileExtension(filename_extension); + std::ifstream ifs(filename_as_std); if (!ifs) return false; - loadStatus = viewer->graph->load(ifs); + loadStatus = file_format.has_value() + ? viewer->graph->load(ifs, file_format.value()) + : viewer->graph->load(ifs); } if (!loadStatus) return false; lastSolver_ = -1; diff --git a/g2o/core/CMakeLists.txt b/g2o/core/CMakeLists.txt index c4080b3c9..00d4734df 100644 --- a/g2o/core/CMakeLists.txt +++ b/g2o/core/CMakeLists.txt @@ -40,6 +40,7 @@ robust_kernel_factory.cpp robust_kernel_factory.h g2o_core_api.h # IO abstract_graph.cpp abstract_graph.h +io/io_format.cpp io/io_binary.cpp io/io_binary.h io/io_json.cpp io/io_json.h io/io_g2o.cpp io/io_g2o.h diff --git a/g2o/core/io/io_format.cpp b/g2o/core/io/io_format.cpp new file mode 100644 index 000000000..d585b65a3 --- /dev/null +++ b/g2o/core/io/io_format.cpp @@ -0,0 +1,42 @@ +// g2o - General Graph Optimization +// Copyright (C) 2011 R. Kuemmerle, G. Grisetti, W. Burgard +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the distribution. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS +// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED +// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A +// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED +// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR +// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "io_format.h" + +#include +#include + +namespace g2o::io { + +std::optional formatForFileExtension(std::string_view extension) { + if (extension == "g2o" || extension == "G2O") return Format::kG2O; + if (extension == "json" || extension == "JSON") return Format::kJson; + if (extension == "xml" || extension == "XML") return Format::kXML; + if (extension == "bin" || extension == "BIN") return Format::kBinary; + return std::nullopt; +} + +} // namespace g2o::io diff --git a/g2o/core/io/io_format.h b/g2o/core/io/io_format.h index 88487e8cb..3323bf706 100644 --- a/g2o/core/io/io_format.h +++ b/g2o/core/io/io_format.h @@ -27,11 +27,24 @@ #ifndef G2O_CORE_IO_FORMAT_H #define G2O_CORE_IO_FORMAT_H +#include +#include + #include "g2o/core/g2o_core_api.h" namespace g2o::io { enum class G2O_CORE_API Format { kG2O = 0, kBinary = 1, kJson = 2, kXML = 3 }; +/** + * @brief Maps a file extension to a format value + * + * @param extension Filename extension, e.g., json, g2o, xml, bin + * @return std::optional of the corresponding format, nullopt if cannot + * match + */ +G2O_CORE_API std::optional formatForFileExtension( + std::string_view extension); + } // namespace g2o::io #endif