Skip to content

Commit

Permalink
add transform to merge graph
Browse files Browse the repository at this point in the history
  • Loading branch information
yunzc committed Feb 5, 2025
1 parent 0a8b29b commit 8ab00e0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
5 changes: 4 additions & 1 deletion include/spark_dsg/dynamic_scene_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,9 +458,12 @@ class DynamicSceneGraph {
* @note Will add the nodes and edges not previously added in current graph
* @param other other graph to update from
* @param config Configuration controlling merge behavior
* @param transform the other graph when merging
* @returns True if merge was successful
*/
bool mergeGraph(const DynamicSceneGraph& other, const GraphMergeConfig& config = {});
bool mergeGraph(const DynamicSceneGraph& other,
const GraphMergeConfig& config = {},
const Eigen::Affine3d* transform = nullptr);

/**
* @brief Get all removed nodes from the graph
Expand Down
5 changes: 4 additions & 1 deletion include/spark_dsg/dynamic_scene_graph_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
* purposes notwithstanding any copyright notation herein.
* -------------------------------------------------------------------------- */
#pragma once
#include <Eigen/Geometry>

#include "spark_dsg/base_layer.h"
#include "spark_dsg/edge_container.h"
#include "spark_dsg/layer_prefix.h"
Expand Down Expand Up @@ -94,7 +96,8 @@ class DynamicSceneGraphLayer : public BaseLayer {

void mergeLayer(const DynamicSceneGraphLayer& other,
const GraphMergeConfig& config,
std::vector<NodeId>* new_nodes = nullptr);
std::vector<NodeId>* new_nodes = nullptr,
const Eigen::Affine3d* transform = nullptr);

void getNewNodes(std::vector<NodeId>& new_nodes, bool clear_new) override;

Expand Down
4 changes: 3 additions & 1 deletion include/spark_dsg/scene_graph_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* purposes notwithstanding any copyright notation herein.
* -------------------------------------------------------------------------- */
#pragma once
#include <Eigen/Geometry>
#include <functional>
#include <map>
#include <unordered_set>
Expand Down Expand Up @@ -157,7 +158,8 @@ class SceneGraphLayer : public BaseLayer {

void mergeLayer(const SceneGraphLayer& other,
const GraphMergeConfig& config,
std::vector<NodeId>* new_nodes);
std::vector<NodeId>* new_nodes = nullptr,
const Eigen::Affine3d* transform = nullptr);

/**
* @brief Number of nodes in the layer
Expand Down
8 changes: 5 additions & 3 deletions src/dynamic_scene_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ bool DynamicSceneGraph::updateFromLayer(SceneGraphLayer& other_layer,
}

bool DynamicSceneGraph::mergeGraph(const DynamicSceneGraph& other,
const GraphMergeConfig& config) {
const GraphMergeConfig& config,
const Eigen::Affine3d* transform) {
for (auto&& [l_id, other_layers] : other.dynamicLayers()) {
for (auto&& [prefix, other_layer] : other_layers) {
if (!hasLayer(l_id, prefix)) {
Expand All @@ -676,7 +677,8 @@ bool DynamicSceneGraph::mergeGraph(const DynamicSceneGraph& other,

const LayerKey layer_key(l_id, prefix);
std::vector<NodeId> new_nodes;
dynamic_layers_[l_id][prefix]->mergeLayer(*other_layer, config, &new_nodes);
dynamic_layers_[l_id][prefix]->mergeLayer(
*other_layer, config, &new_nodes, transform);
for (const auto node_id : new_nodes) {
node_lookup_[node_id] = layer_key;
}
Expand All @@ -701,7 +703,7 @@ bool DynamicSceneGraph::mergeGraph(const DynamicSceneGraph& other,
}

std::vector<NodeId> new_nodes;
layers_[l_id]->mergeLayer(*other_layer, config, &new_nodes);
layers_[l_id]->mergeLayer(*other_layer, config, &new_nodes, transform);
for (const auto node_id : new_nodes) {
node_lookup_[node_id] = l_id;
}
Expand Down
15 changes: 12 additions & 3 deletions src/dynamic_scene_graph_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,18 @@ DynamicSceneGraphLayer::DynamicSceneGraphLayer(LayerId layer, LayerPrefix node_p

void DynamicSceneGraphLayer::mergeLayer(const DynamicSceneGraphLayer& other,
const GraphMergeConfig& config,
std::vector<NodeId>* new_nodes) {
std::vector<NodeId>* new_nodes,
const Eigen::Affine3d* transform) {
Eigen::Vector3d last_update_delta = Eigen::Vector3d::Zero();

for (size_t i = 0; i < other.nodes_.size(); i++) {
const auto& other_node = *other.nodes_[i];
if (i < next_node_) {
// update the last_update_delta
const Eigen::Vector3d node_position = nodes_[i]->attributes_->position;
Eigen::Vector3d node_position = nodes_[i]->attributes_->position;
if (transform) {
node_position = *transform * node_position;
}
last_update_delta = node_position - other_node.attributes_->position;

if (!config.update_dynamic_attributes) {
Expand All @@ -68,7 +72,12 @@ void DynamicSceneGraphLayer::mergeLayer(const DynamicSceneGraphLayer& other,
nodes_[i]->attributes_->position = node_position;
} else {
emplaceNode(other_node.timestamp.value(), other_node.attributes_->clone(), false);
nodes_.back()->attributes_->position += last_update_delta;
if (transform) {
nodes_.back()->attributes_->position =
*transform * nodes_.back()->attributes_->position;
} else {
nodes_.back()->attributes_->position += last_update_delta;
}
if (new_nodes) {
new_nodes->push_back(nodes_.back()->id);
}
Expand Down
9 changes: 8 additions & 1 deletion src/scene_graph_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ bool SceneGraphLayer::rewireEdge(NodeId source,

void SceneGraphLayer::mergeLayer(const SceneGraphLayer& other_layer,
const GraphMergeConfig& config,
std::vector<NodeId>* new_nodes) {
std::vector<NodeId>* new_nodes,
const Eigen::Affine3d* transform) {
const bool update_attributes =
(config.update_layer_attributes && config.update_layer_attributes->count(id))
? config.update_layer_attributes->at(id)
Expand All @@ -240,10 +241,16 @@ void SceneGraphLayer::mergeLayer(const SceneGraphLayer& other_layer,
}

iter->second->attributes_ = other.attributes_->clone();
if (transform) {
iter->second->attributes_->position = *transform * other.attributes_->position;
}
continue;
}

auto attrs = other.attributes_->clone();
if (transform) {
attrs->position = *transform * attrs->position;
}
nodes_[other.id] = Node::Ptr(new Node(other.id, id, std::move(attrs)));
nodes_status_[other.id] = NodeStatus::NEW;
if (new_nodes) {
Expand Down

0 comments on commit 8ab00e0

Please sign in to comment.