Skip to content

Commit

Permalink
Support file formats in g2o_viewer
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Jul 21, 2024
1 parent 6d43bd2 commit f449b3d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
14 changes: 12 additions & 2 deletions g2o/apps/g2o_viewer/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
#include <cassert>
#include <fstream>
#include <iostream>
#include <string>

#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"
Expand Down Expand Up @@ -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<g2o::io::Format> 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;
Expand Down
1 change: 1 addition & 0 deletions g2o/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions g2o/core/io/io_format.cpp
Original file line number Diff line number Diff line change
@@ -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 <optional>
#include <string_view>

namespace g2o::io {

std::optional<Format> 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
13 changes: 13 additions & 0 deletions g2o/core/io/io_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,24 @@
#ifndef G2O_CORE_IO_FORMAT_H
#define G2O_CORE_IO_FORMAT_H

#include <optional>
#include <string_view>

#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<Format> of the corresponding format, nullopt if cannot
* match
*/
G2O_CORE_API std::optional<Format> formatForFileExtension(
std::string_view extension);

} // namespace g2o::io

#endif

0 comments on commit f449b3d

Please sign in to comment.