Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ This repository is organized into several ROS 2 packages:
- Displays surfaces and layers.
- Optional per-cell normal rendering.
- Layer-based coloring.
- Optional NavCel edges (*Draw Edges*, *Edge Color*), to see the triangles of the mesh.
- Large maps drawn in chunks; geometry patches (*Patch Topic*) rebuild only the chunks they touch.

- **`navmap_tools/`**
Tools and utilities for building and testing NavMaps (mesh import/export, conversions, etc.)
Expand Down Expand Up @@ -387,6 +389,21 @@ navmap::NavMap nm2 = navmap_ros::from_msg(msg);

`navmap_ros::to_layer_update_msg(nm, "obstacles")` publishes only what changed since the layer's last `clear_changes()`: a sparse `NavMapLayer` (`indices` + values) when few NavCels changed, the full layer otherwise. Every `NavMap` and `NavMapLayer` message carries the map's `geometry_id`, so `from_msg` (and RViz2) reject updates meant for another geometry.

### Geometry patches

A map whose geometry changes in a few places (e.g. a simplified copy that merges and splits NavCels) can send only those changes instead of the whole map. `navmap_ros::to_patch_msg(nm, base_geometry_id, first_vertex, navcels, surfaces)` builds a `NavMapPatch` with the new vertices (from `first_vertex` on), the rewritten NavCels (with their vertices and the value of every layer) and the replaced surfaces; links are always sent whole. The receiver applies it to the last map it has with `navmap_ros::apply_patch(patch, msg)`, which returns `false` and leaves `msg` untouched if `msg` is not the patch's base map (`geometry_id != base_geometry_id`) or the patch does not fit it: then it must wait for a full map. The RViz2 NavMap display subscribes to `<topic>_patch` by default (*Patch Topic*).

```cpp
// publisher: the whole map the first time, patches afterwards
auto patch = navmap_ros::to_patch_msg(nm, last_sent_geometry_id, first_new_vertex,
changed_navcels, changed_surfaces, header);
patch_pub->publish(patch);
last_sent_geometry_id = nm.geometry_id();

// receiver
if (!navmap_ros::apply_patch(*patch, last_map_msg)) { /* wait for a full map */ }
```

---

## 9. Save and load from disk
Expand Down
15 changes: 15 additions & 0 deletions navmap_core/include/navmap_core/NavMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,21 @@ class NavMap
*/
void rebuild_geometry_accels();

/**
* \brief As rebuild_geometry_accels(), keeping what the caller already filled.
*
* A builder that knows the topology or a hierarchy of its NavCels (a mesh made from a tree,
* say) can fill NavCel::neighbor and each Surface's aabb / bvh / prim_indices itself, in linear
* time, and skip the sorts of the generic builders.
* \param rebuild_adjacency false: NavCel::neighbor is kept as given.
* \param rebuild_bvh false: every Surface's aabb, bvh (root at index 0) and prim_indices are
* kept as given.
* \param recompute_normals false: NavCel::normal and NavCel::area are kept as given (a builder
* that edits a few NavCels of a big map sets theirs).
*/
void rebuild_geometry_accels(
bool rebuild_adjacency, bool rebuild_bvh, bool recompute_normals = true);

/**
* \brief Copy assignment optimized to avoid geometry duplication.
*
Expand Down
15 changes: 12 additions & 3 deletions navmap_core/src/navmap_core/NavMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,10 +438,17 @@ inline float box_sq_dist(const AABB & box, const Vec3 & p)
// -----------------------------------------------------------------------------

void NavMap::rebuild_geometry_accels()
{
rebuild_geometry_accels(true, true);
}

void NavMap::rebuild_geometry_accels(
bool rebuild_adjacency, bool rebuild_bvh, bool recompute_normals)
{
geometry_dirty_ = true;
// Normals and areas.
for (auto & c : navcels) {
if (!recompute_normals) {break;}
Vec3 a = positions.at(c.v[0]);
Vec3 b = positions.at(c.v[1]);
Vec3 d = positions.at(c.v[2]);
Expand All @@ -450,11 +457,13 @@ void NavMap::rebuild_geometry_accels()
}

// Topological neighbors.
build_adjacency();
if (rebuild_adjacency) {build_adjacency();}

// Per-surface BVH and seed grid.
for (auto & s : surfaces) {
build_surface_bvh(s);
if (rebuild_bvh) {
for (auto & s : surfaces) {
build_surface_bvh(s);
}
}
build_navcel_surface_index_();
build_surface_tree_();
Expand Down
26 changes: 26 additions & 0 deletions navmap_ros/include/navmap_ros/conversions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
* The reverse conversion reuses that layer when present.
*/

#include <cstdint>
#include <string>
#include <vector>

#include "Eigen/Core"
#include "pcl/point_cloud.h"
Expand All @@ -45,6 +47,7 @@
#include "sensor_msgs/msg/point_cloud2.hpp"
#include "navmap_ros_interfaces/msg/nav_map.hpp"
#include "navmap_ros_interfaces/msg/nav_map_layer.hpp"
#include "navmap_ros_interfaces/msg/nav_map_patch.hpp"
#include "nav_msgs/msg/occupancy_grid.hpp"
#include "navmap_core/Costs.hpp"
#include "navmap_core/NavMap.hpp"
Expand Down Expand Up @@ -152,6 +155,29 @@ navmap_ros_interfaces::msg::NavMapLayer to_layer_update_msg(
const std_msgs::msg::Header & header = std_msgs::msg::Header(),
float max_sparse_fraction = 0.25f);

/**
* @brief Patch with what changed in @p nm since a map with geometry @p base_geometry_id was sent.
*
* The caller knows what changed: the vertices from @p first_vertex on are new, the NavCels in
* @p navcels were rewritten (they carry their vertices and the value of every layer) and the
* surfaces in @p surfaces were replaced. Links are always sent whole.
*/
navmap_ros_interfaces::msg::NavMapPatch to_patch_msg(
const navmap::NavMap & nm, std::uint64_t base_geometry_id, std::uint32_t first_vertex,
const std::vector<std::uint32_t> & navcels, const std::vector<std::size_t> & surfaces,
const std_msgs::msg::Header & header = std_msgs::msg::Header());

/**
* @brief Applies @p patch to the NavMap message @p msg (the map a receiver keeps).
*
* @return false, leaving @p msg untouched, if @p msg is not the patch's base map
* (geometry_id != base_geometry_id) or the patch does not fit it (missing vertices,
* indices out of range, layers of another type): the receiver must wait for a full map.
*/
bool apply_patch(
const navmap_ros_interfaces::msg::NavMapPatch & patch,
navmap_ros_interfaces::msg::NavMap & msg);

/**
* @brief Import a single NavMapLayer message into a NavMap.
*
Expand Down
225 changes: 225 additions & 0 deletions navmap_ros/src/navmap_ros/conversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,231 @@ NavMap to_msg(const navmap::NavMap & nm)
return to_msg(nm, std_msgs::msg::Header());
}

navmap_ros_interfaces::msg::NavMapPatch to_patch_msg(
const navmap::NavMap & nm, std::uint64_t base_geometry_id, std::uint32_t first_vertex,
const std::vector<std::uint32_t> & navcels, const std::vector<std::size_t> & surfaces,
const std_msgs::msg::Header & header)
{
navmap_ros_interfaces::msg::NavMapPatch p;
p.header = header;
p.base_geometry_id = base_geometry_id;
p.geometry_id = nm.geometry_id();

const std::size_t nv = nm.positions.x.size();
p.first_vertex = std::min<std::uint32_t>(first_vertex, static_cast<std::uint32_t>(nv));
p.positions_x.assign(nm.positions.x.begin() + p.first_vertex, nm.positions.x.end());
p.positions_y.assign(nm.positions.y.begin() + p.first_vertex, nm.positions.y.end());
p.positions_z.assign(nm.positions.z.begin() + p.first_vertex, nm.positions.z.end());
if (nm.colors.has_value()) {
p.colors_r.assign(nm.colors->r.begin() + p.first_vertex, nm.colors->r.end());
p.colors_g.assign(nm.colors->g.begin() + p.first_vertex, nm.colors->g.end());
p.colors_b.assign(nm.colors->b.begin() + p.first_vertex, nm.colors->b.end());
p.colors_a.assign(nm.colors->a.begin() + p.first_vertex, nm.colors->a.end());
}

p.navcels_size = static_cast<std::uint32_t>(nm.navcels.size());
p.navcel_indices.reserve(navcels.size());
p.navcels_v0.reserve(navcels.size());
p.navcels_v1.reserve(navcels.size());
p.navcels_v2.reserve(navcels.size());
for (const auto c : navcels) {
if (c >= nm.navcels.size()) {continue;}
p.navcel_indices.push_back(c);
p.navcels_v0.push_back(nm.navcels[c].v[0]);
p.navcels_v1.push_back(nm.navcels[c].v[1]);
p.navcels_v2.push_back(nm.navcels[c].v[2]);
}

for (const auto & name : nm.layers.list()) {
auto base = nm.layers.get(name);
if (!base) {continue;}
NavMapLayer l;
l.name = name;
l.type = static_cast<uint8_t>(base->type());
l.geometry_id = p.geometry_id;
if (const auto meta = nm.get_layer_meta(name)) {
l.description = meta->description;
l.unit = meta->unit;
l.has_unknown = meta->unknown.has_value();
l.unknown_value = meta->unknown.value_or(0.0);
}
l.indices = p.navcel_indices;
auto gather = [&](const auto & data, auto & out) {
out.reserve(p.navcel_indices.size());
for (const auto c : p.navcel_indices) {
using T = typename std::decay_t<decltype(out)>::value_type;
out.push_back(c < data.size() ? data[c] : T{});
}
};
switch (base->type()) {
case navmap::LayerType::U8:
gather(static_cast<const navmap::LayerView<uint8_t> &>(*base).data(), l.data_u8);
break;
case navmap::LayerType::F32:
gather(static_cast<const navmap::LayerView<float> &>(*base).data(), l.data_f32);
break;
case navmap::LayerType::F64:
gather(static_cast<const navmap::LayerView<double> &>(*base).data(), l.data_f64);
break;
case navmap::LayerType::I16:
gather(static_cast<const navmap::LayerView<int16_t> &>(*base).data(), l.data_i16);
break;
}
p.layers.push_back(std::move(l));
}

for (const auto i : surfaces) {
if (i >= nm.surfaces.size()) {continue;}
p.surface_indices.push_back(static_cast<std::uint32_t>(i));
NavMapSurface smsg;
smsg.frame_id = nm.surfaces[i].frame_id;
smsg.navcels.assign(nm.surfaces[i].navcels.begin(), nm.surfaces[i].navcels.end());
p.surfaces.push_back(std::move(smsg));
}

p.links.reserve(nm.links().size());
for (const auto & l : nm.links()) {
navmap_ros_interfaces::msg::NavMapLink lm;
lm.from_navcel = l.from;
lm.to_navcel = l.to;
lm.type = static_cast<uint8_t>(l.type);
lm.length = l.length;
lm.bidirectional = l.bidirectional;
p.links.push_back(lm);
}
return p;
}

bool apply_patch(const navmap_ros_interfaces::msg::NavMapPatch & p, NavMap & msg)
{
// Checks first: msg is left untouched if the patch does not fit it
if (msg.geometry_id != p.base_geometry_id) {return false;}
if (p.first_vertex > msg.positions_x.size()) {return false;}
const std::size_t nv = p.first_vertex + p.positions_x.size();
if (p.positions_y.size() != p.positions_x.size() ||
p.positions_z.size() != p.positions_x.size())
{
return false;
}
const std::size_t k = p.navcel_indices.size();
if (p.navcels_v0.size() != k || p.navcels_v1.size() != k || p.navcels_v2.size() != k) {
return false;
}
for (std::size_t i = 0; i < k; ++i) {
if (p.navcel_indices[i] >= p.navcels_size || p.navcels_v0[i] >= nv ||
p.navcels_v1[i] >= nv || p.navcels_v2[i] >= nv)
{
return false;
}
}
for (const auto & l : p.layers) {
if (l.indices.size() != k) {return false;}
const std::size_t n = l.type == NavMapLayer::U8 ? l.data_u8.size() :
l.type == NavMapLayer::F32 ? l.data_f32.size() :
l.type == NavMapLayer::F64 ? l.data_f64.size() :
l.type == NavMapLayer::I16 ? l.data_i16.size() : k + 1;
if (n != k) {return false;}
for (const auto & ml : msg.layers) {
if (ml.name == l.name && ml.type != l.type) {return false;}
}
}
if (p.surfaces.size() != p.surface_indices.size()) {return false;}

// Vertices: those up to first_vertex are kept, the rest are the patch's
auto set_tail = [&](auto & dst, const auto & src) {
dst.resize(p.first_vertex);
dst.insert(dst.end(), src.begin(), src.end());
};
set_tail(msg.positions_x, p.positions_x);
set_tail(msg.positions_y, p.positions_y);
set_tail(msg.positions_z, p.positions_z);
if (msg.has_vertex_rgba) {
const bool colours = p.colors_r.size() == p.positions_x.size() &&
p.colors_g.size() == p.positions_x.size() && p.colors_b.size() == p.positions_x.size();
if (colours) {
set_tail(msg.colors_r, p.colors_r);
set_tail(msg.colors_g, p.colors_g);
set_tail(msg.colors_b, p.colors_b);
if (p.colors_a.size() == p.positions_x.size()) {
set_tail(msg.colors_a, p.colors_a);
} else {
msg.colors_a.resize(nv, 255);
}
} else {
msg.colors_r.resize(nv, 0);
msg.colors_g.resize(nv, 0);
msg.colors_b.resize(nv, 0);
msg.colors_a.resize(nv, 255);
}
}

// NavCels: the new size (new ones degenerate until written), then the rewritten ones
msg.navcels_v0.resize(p.navcels_size, 0);
msg.navcels_v1.resize(p.navcels_size, 0);
msg.navcels_v2.resize(p.navcels_size, 0);
for (std::size_t i = 0; i < k; ++i) {
const auto c = p.navcel_indices[i];
msg.navcels_v0[c] = p.navcels_v0[i];
msg.navcels_v1[c] = p.navcels_v1[i];
msg.navcels_v2[c] = p.navcels_v2[i];
}

// Layers: every one at the new size; the patch's values on its NavCels
auto resize_layer = [&](NavMapLayer & l) {
switch (l.type) {
case NavMapLayer::U8: l.data_u8.resize(p.navcels_size, 0); break;
case NavMapLayer::F32: l.data_f32.resize(p.navcels_size, 0.0f); break;
case NavMapLayer::F64: l.data_f64.resize(p.navcels_size, 0.0); break;
case NavMapLayer::I16: l.data_i16.resize(p.navcels_size, 0); break;
default: break;
}
l.geometry_id = p.geometry_id;
};
for (auto & l : msg.layers) {
resize_layer(l);
}
for (const auto & pl : p.layers) {
NavMapLayer * dst = nullptr;
for (auto & ml : msg.layers) {
if (ml.name == pl.name) {dst = &ml;}
}
if (!dst) {
NavMapLayer nl;
nl.name = pl.name;
nl.type = pl.type;
nl.description = pl.description;
nl.unit = pl.unit;
nl.has_unknown = pl.has_unknown;
nl.unknown_value = pl.unknown_value;
msg.layers.push_back(nl);
dst = &msg.layers.back();
resize_layer(*dst);
}
auto scatter = [&](auto & d, const auto & src) {
for (std::size_t i = 0; i < k; ++i) {
d[pl.indices[i]] = src[i];
}
};
switch (pl.type) {
case NavMapLayer::U8: scatter(dst->data_u8, pl.data_u8); break;
case NavMapLayer::F32: scatter(dst->data_f32, pl.data_f32); break;
case NavMapLayer::F64: scatter(dst->data_f64, pl.data_f64); break;
case NavMapLayer::I16: scatter(dst->data_i16, pl.data_i16); break;
default: break;
}
}

for (std::size_t i = 0; i < p.surface_indices.size(); ++i) {
const auto s = p.surface_indices[i];
if (s >= msg.surfaces.size()) {msg.surfaces.resize(s + 1);}
msg.surfaces[s] = p.surfaces[i];
}
msg.links = p.links;
msg.header = p.header;
msg.geometry_id = p.geometry_id;
return true;
}

navmap::NavMap from_msg(const NavMap & msg, std_msgs::msg::Header & header)
{
header = msg.header;
Expand Down
3 changes: 3 additions & 0 deletions navmap_ros/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@ ament_add_gtest(test_layer_metadata_and_migration test_layer_metadata_and_migrat
target_link_libraries(test_layer_metadata_and_migration
${PROJECT_NAME}
)

ament_add_gtest(test_patch test_patch.cpp)
target_link_libraries(test_patch ${PROJECT_NAME})
Loading
Loading