Skip to content

Commit

Permalink
Use templated function to avoid duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Jul 20, 2024
1 parent d65afaa commit 135f490
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 21 additions & 0 deletions g2o/core/abstract_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,30 +109,51 @@ class G2O_CORE_API AbstractGraph {
AbstractGraph() = default;
~AbstractGraph() = default;

/**
* @brief Load from the input assuming given format
*
* @param input The input stream for reading data.
* @param format The output format.
* @return true iff loading was successful
*/
bool load(std::istream& input, io::Format format);

[[nodiscard]] bool save(std::ostream& output, io::Format format) const;

//! The ids of fixed vertices
std::vector<int>& fixed() { return fixed_; };
[[nodiscard]] const std::vector<int>& fixed() const { return fixed_; };

//! The parameters of the graph
std::vector<AbstractParameter>& parameters() { return parameters_; };
[[nodiscard]] const std::vector<AbstractParameter>& parameters() const {
return parameters_;
};

//! The vertices of the graph
std::vector<AbstractVertex>& vertices() { return vertices_; };
[[nodiscard]] const std::vector<AbstractVertex>& vertices() const {
return vertices_;
};

//! The edges of the graph
std::vector<AbstractEdge>& edges() { return edges_; };
[[nodiscard]] const std::vector<AbstractEdge>& edges() const {
return edges_;
};

/**
* @brief Clear all structures of the graph.
*
* Clear all vertices, edges, and parameters of the graph.
*/
void clear();

/**
* @brief Set a mapping for renaming tags.
*
* @param tag_mapping mapping from -> to for renaming types in the graph.
*/
void renameTags(
const std::unordered_map<std::string, std::string>& tag_mapping);

Expand Down
14 changes: 5 additions & 9 deletions g2o/core/io/io_g2o.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <iostream>
#include <sstream>
#include <string>
#include <type_traits>
#include <unordered_set>
#include <vector>

Expand All @@ -39,15 +40,10 @@
#include "g2o/stuff/string_tools.h"

namespace {
std::ostream& operator<<(std::ostream& os, const std::vector<double>& v) {
for (size_t i = 0; i < v.size(); ++i) {
if (i > 0) os << " ";
os << v[i];
}
return os;
}

std::ostream& operator<<(std::ostream& os, const std::vector<int>& v) {
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
static_assert(std::is_integral_v<T> || std::is_floating_point_v<T>,
"Type has to be integral or floating point");
for (size_t i = 0; i < v.size(); ++i) {
if (i > 0) os << " ";
os << v[i];
Expand Down

0 comments on commit 135f490

Please sign in to comment.