Skip to content

Commit e22b154

Browse files
committed
fix dynamic layer bindings and node removal (#74)
Co-authored-by: Nathan Hughes <nathan.h.hughes@gmail.com> (fix) emplace agent nodes correctly when loading json, and fix dynamic node python iter clear dynamic node when removing and set python iter to first valid node
1 parent d90ce45 commit e22b154

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

python/bindings/scene_graph_iterators.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,19 @@ class NodeIter {
6161
class DynamicNodeIter {
6262
public:
6363
DynamicNodeIter(const DynamicSceneGraphLayer::Nodes& container)
64-
: curr_iter_(container.begin()), end_iter_(container.end()) {}
64+
: curr_iter_(container.begin()), end_iter_(container.end()) {
65+
while (*curr_iter_ == nullptr && curr_iter_ != end_iter_) {
66+
++curr_iter_;
67+
}
68+
}
6569

6670
const DynamicSceneGraphLayer::Node* operator*() const { return curr_iter_->get(); }
6771

6872
DynamicNodeIter& operator++() {
6973
++curr_iter_;
74+
while (*curr_iter_ == nullptr && curr_iter_ != end_iter_) {
75+
++curr_iter_;
76+
}
7077
return *this;
7178
}
7279

src/dynamic_scene_graph_layer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ bool DynamicSceneGraphLayer::removeNode(NodeId node) {
268268
// TODO(nathan) this is slightly brittle, maybe consider std::map instead
269269
node_status_[index] = NodeStatus::DELETED;
270270
times_.erase(nodes_.at(index)->timestamp.count());
271+
nodes_[index].reset();
271272
return true;
272273
}
273274

src/graph_json_serialization.cpp

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ using nlohmann::json;
5555
using EdgesPtr = std::unique_ptr<SceneGraphLayer::Edges>;
5656
using NodeSet = std::unordered_set<NodeId>;
5757
using NodeCallback = std::function<void(NodeId, LayerId, NodeAttributes::Ptr&&)>;
58-
using DynamicNodeCallback =
59-
std::function<void(LayerId, char, std::chrono::nanoseconds, NodeAttributes::Ptr&&)>;
58+
using DynamicNodeCallback = std::function<
59+
void(LayerId, NodeId, std::chrono::nanoseconds, NodeAttributes::Ptr&&)>;
6060
using EdgeCallback = std::function<void(NodeId, NodeId, EdgeAttributes::Ptr&&)>;
6161

6262
void to_json(json& record, const MeshEdge& edge) {
@@ -94,11 +94,12 @@ void read_node_from_json(const json& record, NodeCallback callback) {
9494

9595
void read_node_from_json(const json& record, DynamicNodeCallback callback) {
9696
auto layer = record.at("layer").get<LayerId>();
97-
auto prefix = record.at("prefix").get<char>();
97+
// auto prefix = record.at("prefix").get<char>();
98+
auto node_id = record.at("id").get<NodeId>();
9899
auto timestamp = record.at("timestamp").get<uint64_t>();
99100
JsonConverter converter(&record.at("attributes"));
100101
auto attrs = JsonNodeFactory::get_default().create(converter);
101-
callback(layer, prefix, std::chrono::nanoseconds(timestamp), std::move(attrs));
102+
callback(layer, node_id, std::chrono::nanoseconds(timestamp), std::move(attrs));
102103
}
103104

104105
void read_edge_from_json(const json& record, EdgeCallback callback) {
@@ -176,7 +177,7 @@ std::string DynamicSceneGraph::serialize(bool include_mesh) const {
176177
record["layer_ids"] = layer_ids;
177178
record["mesh_layer_id"] = mesh_layer_id;
178179

179-
for (const auto& id_layer_pair : layers_) {
180+
for (const auto& id_layer_pair : layers_){
180181
for (const auto& id_node_pair : id_layer_pair.second->nodes_) {
181182
record["nodes"].push_back(*id_node_pair.second);
182183
}
@@ -198,8 +199,16 @@ std::string DynamicSceneGraph::serialize(bool include_mesh) const {
198199
for (const auto& prefix_layer_pair : id_layer_group_pair.second) {
199200
const DynamicSceneGraphLayer& layer = *prefix_layer_pair.second;
200201

201-
for (const auto& node : layer.nodes_) {
202-
record["nodes"].push_back(*node);
202+
for (size_t i = 0; i < layer.nodes_.size(); ++i) {
203+
if (!layer.node_status_.count(i)) {
204+
continue;
205+
}
206+
207+
if (layer.node_status_.at(i) == NodeStatus::DELETED) {
208+
continue;
209+
}
210+
211+
record["nodes"].push_back(*layer.nodes_.at(i));
203212
}
204213

205214
for (const auto& id_edge_pair : layer.edges_.edges) {
@@ -251,20 +260,29 @@ DynamicSceneGraph::Ptr DynamicSceneGraph::deserialize(const std::string& content
251260
}
252261

253262
for (const auto& id_content_pair : dynamic_contents) {
254-
read_node_from_json(id_content_pair.second,
255-
[graph](LayerId layer,
256-
char prefix,
257-
std::chrono::nanoseconds time,
258-
NodeAttributes::Ptr&& attrs) {
259-
graph->emplaceNode(
260-
layer, prefix, time, std::move(attrs), false);
261-
});
263+
read_node_from_json(
264+
id_content_pair.second,
265+
[graph](LayerId layer,
266+
NodeId node,
267+
std::chrono::nanoseconds time,
268+
NodeAttributes::Ptr&& attrs) {
269+
if (!graph->emplacePrevDynamicNode(layer, node, time, std::move(attrs))) {
270+
std::stringstream ss;
271+
ss << "failed to add " << NodeSymbol(node).getLabel();
272+
throw std::runtime_error(ss.str());
273+
}
274+
});
262275
}
263276

264277
for (const auto& edge : record.at("edges")) {
265278
read_edge_from_json(
266279
edge, [graph](NodeId source, NodeId target, EdgeAttributes::Ptr&& attrs) {
267-
graph->insertEdge(source, target, std::move(attrs));
280+
if (!graph->insertEdge(source, target, std::move(attrs))) {
281+
std::stringstream ss;
282+
ss << "failed to add " << NodeSymbol(source).getLabel() << ""
283+
<< NodeSymbol(target).getLabel();
284+
throw std::runtime_error(ss.str());
285+
}
268286
});
269287
}
270288

0 commit comments

Comments
 (0)