Skip to content

Commit

Permalink
Apply minor syntax sugar
Browse files Browse the repository at this point in the history
  • Loading branch information
RainerKuemmerle committed Jan 25, 2025
1 parent 6b5b098 commit c203321
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
2 changes: 2 additions & 0 deletions g2o/core/abstract_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ void AbstractGraph::clear() {

void AbstractGraph::renameTags(
const std::unordered_map<std::string, std::string>& tag_mapping) {
if (tag_mapping.empty()) return;

auto map_tag = [&tag_mapping](const std::string& tag) {
auto it = tag_mapping.find(tag);
if (it == tag_mapping.end()) return tag;
Expand Down
9 changes: 5 additions & 4 deletions g2o/core/hyper_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <memory>
#include <set>
#include <unordered_map>
#include <utility>
#include <vector>

#include "g2o_core_api.h"
Expand Down Expand Up @@ -153,7 +154,7 @@ class G2O_CORE_API HyperGraph {
//! returns the set of hyper-edges that are leaving/entering in this vertex
EdgeSetWeak& edges() { return edges_; }
[[nodiscard]] HyperGraphElementType elementType() const final {
return kHgetVertex;
return HyperGraphElementType::kHgetVertex;
}

protected:
Expand Down Expand Up @@ -203,15 +204,15 @@ class G2O_CORE_API HyperGraph {
/**
set the ith vertex on the hyper-edge to the pointer supplied
*/
void setVertex(size_t i, const std::shared_ptr<Vertex>& v) {
void setVertex(size_t i, std::shared_ptr<Vertex> v) {
assert(i < vertices_.size() && "index out of bounds");
vertices_[i] = v;
vertices_[i] = std::move(v);
}

[[nodiscard]] int id() const { return id_; }
void setId(int id);
[[nodiscard]] HyperGraphElementType elementType() const final {
return kHgetEdge;
return HyperGraphElementType::kHgetEdge;
}

[[nodiscard]] int numUndefinedVertices() const;
Expand Down
20 changes: 8 additions & 12 deletions g2o/core/optimizable_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ bool saveParameter(AbstractGraph& abstract_graph, Parameter* p) {
if (tag.empty()) return false;
std::vector<double> data;
p->getParameterData(data);
abstract_graph.parameters().emplace_back(tag, p->id(), data);
abstract_graph.parameters().emplace_back(tag, p->id(), std::move(data));
return true;
}

Expand All @@ -108,8 +108,7 @@ void addDataToGraphElement(
const std::vector<AbstractGraph::AbstractData>& data_vector) {
Factory* factory = Factory::instance();

HyperGraph::GraphElemBitset elemDataBitset;
elemDataBitset[HyperGraph::kHgetData] = true;
const HyperGraph::GraphElemBitset elemDataBitset(1 << HyperGraph::kHgetData);
for (const auto& abstract_data : data_vector) {
const std::shared_ptr<HyperGraph::HyperGraphElement> element =
factory->construct(abstract_data.tag, elemDataBitset);
Expand Down Expand Up @@ -355,13 +354,11 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) {
return false;
}

if (!renamedTypesLookup_.empty()) {
abstract_graph.renameTags(renamedTypesLookup_);
}
abstract_graph.renameTags(renamedTypesLookup_);

// Create the parameters of the graph
HyperGraph::GraphElemBitset elemParamBitset;
elemParamBitset[HyperGraph::kHgetParameter] = true;
const HyperGraph::GraphElemBitset elemParamBitset(
1 << HyperGraph::kHgetParameter);
for (const auto& abstract_param : abstract_graph.parameters()) {
const std::shared_ptr<HyperGraph::HyperGraphElement> pelement =
factory->construct(abstract_param.tag, elemParamBitset);
Expand All @@ -384,8 +381,8 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) {
}

// Create the vertices of the graph
HyperGraph::GraphElemBitset elemVertexBitset;
elemVertexBitset[HyperGraph::kHgetVertex] = true;
const HyperGraph::GraphElemBitset elemVertexBitset(
1 << HyperGraph::kHgetVertex);
for (const auto& abstract_vertex : abstract_graph.vertices()) {
const std::shared_ptr<HyperGraph::HyperGraphElement> graph_element =
factory->construct(abstract_vertex.tag, elemVertexBitset);
Expand All @@ -411,8 +408,7 @@ bool OptimizableGraph::load(std::istream& is, io::Format format) {
}

// Create the edges of the graph
HyperGraph::GraphElemBitset elemEdgeBitset;
elemEdgeBitset[HyperGraph::kHgetEdge] = true;
const HyperGraph::GraphElemBitset elemEdgeBitset(1 << HyperGraph::kHgetEdge);
for (const auto& abstract_edge : abstract_graph.edges()) {
const std::shared_ptr<HyperGraph::HyperGraphElement> graph_element =
factory->construct(abstract_edge.tag, elemEdgeBitset);
Expand Down
11 changes: 10 additions & 1 deletion unit_test/general/graph_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
#include "g2o/types/slam3d/edge_se3_pointxyz.h"
#include "g2o/types/slam3d/vertex_pointxyz.h"
#include "g2o/types/slam3d/vertex_se3.h"
#include "gmock/gmock.h"
#include "unit_test/test_helper/allocate_optimizer.h"
#include "unit_test/test_helper/eigen_matcher.h"

Expand Down Expand Up @@ -82,6 +81,16 @@ TEST(General, BinaryEdgeConstructor) {
ASSERT_EQ(nullptr, e2.vertices()[1]);
}

TEST(General, GraphElemBitset) {
g2o::HyperGraph::GraphElemBitset elemParamBitset;
elemParamBitset[g2o::HyperGraph::kHgetParameter] = true;

const g2o::HyperGraph::GraphElemBitset elemParamFromInt(
1 << g2o::HyperGraph::kHgetParameter);

EXPECT_EQ(elemParamBitset, elemParamFromInt);
}

TEST(General, GraphAddVertex) {
auto optimizer = g2o::internal::createOptimizerForTests();

Expand Down

0 comments on commit c203321

Please sign in to comment.