NavMap
Loading...
Searching...
No Matches
NavMap.hpp
Go to the documentation of this file.
1// Copyright 2025 Intelligent Robotics Lab
2//
3// This file is part of the project Easy Navigation (EasyNav in short)
4// licensed under the GNU General Public License v3.0.
5// See <http://www.gnu.org/licenses/> for details.
6//
7// Easy Navigation program is free software: you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation, either version 3.0, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21#ifndef NAVMAP_CORE__NAVMAP_HPP
22#define NAVMAP_CORE__NAVMAP_HPP
23
40
41#include <vector>
42#include <memory>
43#include <unordered_map>
44#include <optional>
45#include <string>
46#include <array>
47#include <limits>
48#include <stack>
49#include <cmath>
50#include <cstdint>
51#include <span>
52#include <type_traits>
53#include <deque>
54#include <algorithm>
55
56#include <Eigen/Core>
58
59namespace navmap
60{
61
63using PointId = uint32_t;
65using NavCelId = uint32_t;
66
67// -----------------------------------------------------------------------------
68// Core data arrays
69// -----------------------------------------------------------------------------
70
81{
82 std::vector<float> x;
83 std::vector<float> y;
84 std::vector<float> z;
85
87 inline size_t size() const {return x.size();}
88
95 inline Eigen::Vector3f at(PointId id) const
96 {
97 return {x[id], y[id], z[id]};
98 }
99};
100
106struct Colors
107{
108 std::vector<uint8_t> r;
109 std::vector<uint8_t> g;
110 std::vector<uint8_t> b;
111 std::vector<uint8_t> a;
112};
113
114// -----------------------------------------------------------------------------
115// Layers (dynamic per-NavCel attributes)
116// -----------------------------------------------------------------------------
117
123enum class LayerType : uint8_t { U8 = 0, F32 = 1, F64 = 2 };
124
131{
132 virtual ~LayerViewBase() = default;
133
135 virtual LayerType type() const = 0;
136
138 virtual const std::string & name() const = 0;
139
141 virtual size_t size() const = 0;
142
144 virtual std::uint64_t content_hash() const = 0;
145
147 void mark_dirty() const noexcept {hash_dirty_ = true;}
148
149protected:
150 mutable bool hash_dirty_{true};
151 mutable std::uint64_t hash_cache_{0};
152};
153
160template<typename T>
161struct LayerView : LayerViewBase
162{
163 std::string name_;
164 std::vector<T> data_;
166
173 LayerView(std::string name, size_t nitems, LayerType t)
174 : name_(std::move(name)), data_(nitems), type_(t) {}
175
176 LayerType type() const override {return type_;}
177 const std::string & name() const override {return name_;}
178 size_t size() const override {return data_.size();}
179
182 T & operator[](NavCelId cid) {return data_[cid];}
183 const T & operator[](NavCelId cid) const {return data_[cid];}
185
187 std::vector<T> & data() {return data_;}
189 const std::vector<T> & data() const {return data_;}
190
193 std::vector<T> & mutable_data() const
194 {
195 hash_dirty_ = true; return const_cast<std::vector<T> &>(data_);
196 }
197 void set_data(const std::vector<T> & v) {data_ = v; hash_dirty_ = true;}
198 std::uint64_t content_hash() const override;
200};
201
210public:
223 template<typename T>
224 std::shared_ptr<LayerView<T>> add_or_get(
225 const std::string & name,
226 size_t nitems,
227 LayerType type)
228 {
229 auto it = layers_.find(name);
230 if (it != layers_.end()) {
231 return std::dynamic_pointer_cast<LayerView<T>>(it->second);
232 }
233 auto view = std::make_shared<LayerView<T>>(name, nitems, type);
234 layers_[name] = view;
235 return view;
236 }
237
243 std::shared_ptr<LayerViewBase> get(const std::string & name) const
244 {
245 auto it = layers_.find(name);
246 return it == layers_.end() ? nullptr : it->second;
247 }
248
253 std::vector<std::string> list() const
254 {
255 std::vector<std::string> out;
256 out.reserve(layers_.size());
257 for (const auto & kv : layers_) {
258 out.push_back(kv.first);
259 }
260 return out;
261 }
262
267 bool remove(const std::string & name)
268 {
269 return layers_.erase(name) > 0;
270 }
271
279 void resize_all(size_t nitems)
280 {
281 for (auto & kv : layers_) {
282 if (auto v = std::dynamic_pointer_cast<LayerView<uint8_t>>(kv.second)) {
283 v->data_.resize(nitems);
284 kv.second->mark_dirty();
285 continue;
286 }
287 if (auto v = std::dynamic_pointer_cast<LayerView<float>>(kv.second)) {
288 v->data_.resize(nitems);
289 kv.second->mark_dirty();
290 continue;
291 }
292 if (auto v = std::dynamic_pointer_cast<LayerView<double>>(kv.second)) {
293 v->data_.resize(nitems);
294 kv.second->mark_dirty();
295 continue;
296 }
297 }
298 }
299
300private:
301 std::unordered_map<std::string, std::shared_ptr<LayerViewBase>> layers_;
302};
303
310{
311 std::string description;
312 std::string unit;
313 bool per_cell{true};
314};
315
316// -----------------------------------------------------------------------------
317// Mesh cells and acceleration
318// -----------------------------------------------------------------------------
319
329struct NavCel
330{
331 PointId v[3]{0, 0, 0};
332 Eigen::Vector3f normal{0.0f, 0.0f, 1.0f};
333 float area{0.0f};
335 std::numeric_limits<uint32_t>::max(),
336 std::numeric_limits<uint32_t>::max(),
337 std::numeric_limits<uint32_t>::max()
338 };
339 uint32_t layer_dirty_mask{0};
340};
341
348{
350 int left{-1};
351 int right{-1};
352 int start{0};
353 int count{0};
354 bool is_leaf() const {return count > 0;}
355};
356
364{
365 std::string frame_id;
366 std::vector<NavCelId> navcels;
368 std::vector<int> prim_indices;
369 std::vector<BVHNode> bvh;
370};
371
372// -----------------------------------------------------------------------------
373// Rays
374// -----------------------------------------------------------------------------
375
381struct Ray
382{
383 Eigen::Vector3f o;
384 Eigen::Vector3f d;
385};
386
392struct RayHit
393{
394 bool hit{false};
395 size_t surface{0};
397 float t{0.0f};
398 Eigen::Vector3f p;
399};
400
401// -----------------------------------------------------------------------------
402// Helpers (type → LayerType mapping)
403// -----------------------------------------------------------------------------
404
406template<typename T> inline constexpr LayerType layer_type_tag();
407template<> inline constexpr LayerType layer_type_tag<uint8_t>() {return LayerType::U8;}
408template<> inline constexpr LayerType layer_type_tag<float>() {return LayerType::F32;}
409template<> inline constexpr LayerType layer_type_tag<double>() {return LayerType::F64;}
410
415
416// -----------------------------------------------------------------------------
417// NavMap: public API
418// -----------------------------------------------------------------------------
419
434class NavMap {
435public:
437 std::optional<Colors> colors;
438 std::vector<NavCel> navcels;
439 std::vector<Surface> surfaces;
441 std::unordered_map<std::string, LayerMeta> layer_meta;
442
444 NavMap();
445
447 NavMap(const NavMap & other);
448
457
465 NavMap & operator=(const NavMap & other);
466
471 NavMap & operator=(NavMap && other) noexcept;
472
474 bool has_same_geometry(const NavMap & other) const;
475
482 void build_adjacency();
483
489
495 std::array<NavCelId, 3> get_neighbors(NavCelId cid) const
496 {
497 return {navcels[cid].neighbor[0],
498 navcels[cid].neighbor[1],
499 navcels[cid].neighbor[2]};
500 }
501
511 bool raycast(
512 const Eigen::Vector3f & o,
513 const Eigen::Vector3f & d,
514 NavCelId & hit_cid,
515 float & t,
516 Eigen::Vector3f & hit_pt) const;
517
524 void raycast_many(
525 const std::vector<Ray> & rays,
526 std::vector<RayHit> & out,
527 bool first_hit_only = true) const;
528
529 // --- Per-NavCel layer value access ---
530
538 template<typename T>
539 T navcel_value(NavCelId cid, const LayerView<T> & layer) const
540 {
541 return layer[cid];
542 }
543
554 {
555 std::optional<NavCelId> hint_cid;
556 std::optional<size_t> hint_surface;
557 float planar_eps = 1e-4f;
558 float height_eps = 0.50f;
559 bool use_downward_ray = true;
560 };
561
574 bool locate_navcel(
575 const Eigen::Vector3f & p_world,
576 size_t & surface_idx,
577 NavCelId & cid,
578 Eigen::Vector3f & bary,
579 Eigen::Vector3f * hit_pt) const;
580
598 const Eigen::Vector3f & p_world,
599 size_t & surface_idx,
600 NavCelId & cid,
601 Eigen::Vector3f & bary,
602 Eigen::Vector3f * hit_pt,
603 const LocateOpts & opts) const;
604
619 bool closest_triangle(
620 const Eigen::Vector3f & p_world,
621 size_t & surface_idx,
622 NavCelId & cid,
623 Eigen::Vector3f & closest_point,
624 float & sqdist,
625 int restrict_surface = -1) const;
626
627 // ---- Convenience: construction & editing (additive, backward-compatible) ----
628
632 std::size_t create_surface(std::string frame_id);
633
637 Surface create_surface_obj(const std::string & frame_id) const;
638
640 std::size_t add_surface(const Surface & s);
641
643 std::size_t add_surface(Surface && s);
644
648 bool remove_surface(std::size_t surface_index);
649
651 uint32_t add_vertex(const Eigen::Vector3f & p);
652
657 NavCelId add_navcel(uint32_t v0, uint32_t v1, uint32_t v2);
658
660 void add_navcel_to_surface(std::size_t surface_index, NavCelId cid);
661
663 Eigen::Vector3f navcel_centroid(NavCelId cid) const;
664
666 std::vector<NavCelId> navcel_neighbors(NavCelId cid) const;
667
668 // ---- Layers (per-NavCel) convenience ----
669
684 template<typename T>
685 std::shared_ptr<LayerView<T>> add_layer(
686 const std::string & name,
687 const std::string & description = {},
688 const std::string & unit = {},
689 T default_value = T{})
690 {
691 auto v = layers.add_or_get<T>(name, navcels.size(), layer_type_tag<T>());
692 if (v->data().size() != navcels.size()) {
693 v->data().assign(navcels.size(), default_value);
694 }
695 layer_meta[name] = LayerMeta{description, unit, true};
696 return v;
697 }
698
700 bool has_layer(const std::string & name) const;
701
703 std::size_t layer_size(const std::string & name) const;
704
706 std::string layer_type_name(const std::string & name) const;
707
715 template<typename T>
716 void layer_set(const std::string & name, NavCelId cid, T value)
717 {
718 auto view = layers.add_or_get<T>(name, navcels.size(), layer_type_tag<T>());
719 if (static_cast<size_t>(cid) < view->data().size()) {
720 view->data()[cid] = value;
721 view->mark_dirty();
722 }
723 }
724
740 template<typename T>
741 T layer_get(const std::string & name, NavCelId cid, T def = T{}) const
742 {
743 if (auto base = layers.get(name)) {
744 if (auto view = std::dynamic_pointer_cast<LayerView<T>>(base)) {
745 const auto & buf = view->data();
746 if (static_cast<size_t>(cid) < buf.size()) {
747 return buf[cid];
748 }
749 return def;
750 }
751 }
752 // Fallback: try to fetch as double from any stored type and convert
753 double v;
754 if (auto base = layers.get(name)) {
755 if (auto v8 = std::dynamic_pointer_cast<LayerView<uint8_t>>(base)) {
756 const auto & buf = v8->data();
757 if (static_cast<size_t>(cid) >= buf.size()) {return def;}
758 v = static_cast<double>(buf[cid]);
759 } else if (auto vf = std::dynamic_pointer_cast<LayerView<float>>(base)) {
760 const auto & buf = vf->data();
761 if (static_cast<size_t>(cid) >= buf.size()) {return def;}
762 v = static_cast<double>(buf[cid]);
763 } else if (auto vd = std::dynamic_pointer_cast<LayerView<double>>(base)) {
764 const auto & buf = vd->data();
765 if (static_cast<size_t>(cid) >= buf.size()) {return def;}
766 v = buf[cid];
767 } else {
768 return def;
769 }
770 } else {
771 return def;
772 }
773
774 if constexpr (std::is_floating_point_v<T>) {
775 return static_cast<T>(v);
776 } else if constexpr (std::is_integral_v<T>) {
777 double lo = 0.0;
778 double hi = static_cast<double>(std::numeric_limits<T>::max());
779 double vv = v < lo ? lo : (v > hi ? hi : v);
780 return static_cast<T>(std::llround(vv));
781 } else {
782 static_assert(sizeof(T) == 0, "Unsupported layer type");
783 }
784 }
785
791 std::optional<LayerMeta> get_layer_meta(const std::string & name) const;
792
797 std::vector<std::string> list_layers() const;
798
808 double sample_layer_at(
809 const std::string & name,
810 const Eigen::Vector3f & p_world,
811 double def = std::numeric_limits<double>::quiet_NaN()) const;
812
820 template<typename T>
821 void layer_clear(const std::string & name, T value = T{})
822 {
823 auto view = layers.add_or_get<T>(name, navcels.size(), layer_type_tag<T>());
824 std::fill(view->data().begin(), view->data().end(), value);
825 view->mark_dirty();
826 }
827
838 template<typename T>
839 bool layer_copy(const std::string & src, const std::string & dst)
840 {
841 // No-op if names are equal
842 if (src == dst) {
843 return true;
844 }
845
846 // Get source
847 auto src_base = layers.get(src);
848 auto src_view = std::dynamic_pointer_cast<LayerView<T>>(src_base);
849 if (!src_view) {
850 return false; // source layer missing or wrong type
851 }
852
853 // Ensure destination exists with correct length (navcels.size())
854 auto dst_view = layers.add_or_get<T>(dst, navcels.size(), layer_type_tag<T>());
855 if (!dst_view) {
856 return false;
857 }
858
859 // If sizes mismatch, something is wrong with geometry/layer size.
860 // You can choose to resize here if your semantics allow it. For safety, fail:
861 if (src_view->data().size() != dst_view->data().size()) {
862 return false;
863 }
864
865 // If both views already refer to the same underlying buffer, no work to do
866 if (!src_view->data().empty() &&
867 src_view->data().data() == dst_view->data().data())
868 {
869 return true;
870 }
871
872 // Hash-based skip: O(1) if cached, O(n) only on first compute (then cached)
873 const bool same_hash = (src_view->content_hash() == dst_view->content_hash());
874 if (same_hash) {
875 return true; // identical content → avoid copy
876 }
877
878 // Copy only when different. set_data() marks hash as dirty for dst.
879 dst_view->set_data(src_view->data());
880 return true;
881 }
882
899 template<typename T>
900 std::shared_ptr<LayerView<T>> add_layer(
901 const std::string & name,
902 const std::string & description,
903 const std::string & unit,
904 const std::string & src_layer)
905 {
906 auto dst = layers.add_or_get<T>(name, navcels.size(), layer_type_tag<T>());
907 if (dst->data().size() != navcels.size()) {
908 dst->data().assign(navcels.size(), T{});
909 }
910 layer_meta[name] = LayerMeta{description, unit, true};
911
912 if (!has_layer(src_layer)) {
913 return dst;
914 }
915
916 if (layer_copy<T>(src_layer, name)) {
917 return dst;
918 }
919
920 const std::size_t n = dst->size();
921 for (NavCelId cid = 0; cid < n; ++cid) {
922 double v = layer_get<double>(src_layer, cid, std::numeric_limits<double>::quiet_NaN());
923 if (std::isnan(v)) {v = 0.0;}
924
925 if constexpr (std::is_floating_point_v<T>) {
926 dst->data()[cid] = static_cast<T>(v);
927 } else if constexpr (std::is_integral_v<T>) {
928 double lo = 0.0;
929 double hi = static_cast<double>(std::numeric_limits<T>::max());
930 if (v < lo) {v = lo;}
931 if (v > hi) {v = hi;}
932 dst->data()[cid] = static_cast<T>(std::llround(v));
933 } else {
934 static_assert(sizeof(T) == 0, "Unsupported layer type");
935 }
936 }
937
938 return dst;
939 }
940
948 const Eigen::Vector3f & p_world,
949 std::size_t & surface_idx,
950 NavCelId & cid,
951 Eigen::Vector3f & bary,
952 Eigen::Vector3f * hit_pt,
953 const LocateOpts & opts) const;
954
972 template<typename T>
974 const Eigen::Vector3f & p_world,
975 T value,
976 const std::string & layer_name,
977 AreaShape shape,
978 float size)
979 {
980 if (navcels.empty() || surfaces.empty() || size <= 0.0f) {
981 return false;
982 }
983
984 std::size_t surface_idx = 0;
985 NavCelId start_cid{};
986 Eigen::Vector3f bary{};
987 Eigen::Vector3f hit_pt{};
988 if (!locate_navcel(p_world, surface_idx, start_cid, bary, &hit_pt)) {
989 return false;
990 }
991
992 const float cx = hit_pt.x();
993 const float cy = hit_pt.y();
994
995 {
996 auto existing = layers.get(layer_name);
997 if (existing && existing->type() != layer_type_tag<T>()) {
998 return false; // type mismatch
999 }
1000 }
1001 auto layer = layers.add_or_get<T>(layer_name, navcels.size(), layer_type_tag<T>());
1002 if (!layer) {
1003 return false;
1004 }
1005 if (layer->data().size() != navcels.size()) {
1006 const_cast<std::vector<T> &>(layer->data()).resize(navcels.size(), T{});
1007 }
1008
1009 const float half = (shape == AreaShape::CIRCULAR) ? size : (0.5f * size);
1010 const float minx = cx - half, maxx = cx + half;
1011 const float miny = cy - half, maxy = cy + half;
1012 const float r2 = (shape == AreaShape::CIRCULAR) ? (size * size) : 0.0f;
1013
1014 auto tri_aabb_intersects_box = [&](NavCelId cid) -> bool {
1015 const auto & tri = navcels[static_cast<std::size_t>(cid)];
1016 const Eigen::Vector3f p0{positions.x[tri.v[0]], positions.y[tri.v[0]],
1017 positions.z[tri.v[0]]};
1018 const Eigen::Vector3f p1{positions.x[tri.v[1]], positions.y[tri.v[1]],
1019 positions.z[tri.v[1]]};
1020 const Eigen::Vector3f p2{positions.x[tri.v[2]], positions.y[tri.v[2]],
1021 positions.z[tri.v[2]]};
1022 const float tminx = std::min({p0.x(), p1.x(), p2.x()});
1023 const float tmaxx = std::max({p0.x(), p1.x(), p2.x()});
1024 const float tminy = std::min({p0.y(), p1.y(), p2.y()});
1025 const float tmaxy = std::max({p0.y(), p1.y(), p2.y()});
1026 if (tmaxx < minx || tminx > maxx || tmaxy < miny || tminy > maxy) {
1027 return false;
1028 }
1029 return true;
1030 };
1031
1032 auto inside_area = [&](const Eigen::Vector3f & c) -> bool {
1033 const float dx = c.x() - cx;
1034 const float dy = c.y() - cy;
1035 if (shape == AreaShape::CIRCULAR) {
1036 return (dx * dx + dy * dy) <= r2;
1037 } else {
1038 return (std::abs(dx) <= half) && (std::abs(dy) <= half);
1039 }
1040 };
1041
1042 std::vector<char> visited(navcels.size(), 0);
1043 std::deque<NavCelId> q;
1044 q.push_back(start_cid);
1045 visited[static_cast<std::size_t>(start_cid)] = 1;
1046
1047 auto & data = layer->mutable_data();
1048
1049 while (!q.empty()) {
1050 const NavCelId cid = q.front(); q.pop_front();
1051
1052 if (!tri_aabb_intersects_box(cid)) {
1053 continue;
1054 }
1055
1056 const Eigen::Vector3f c = navcel_centroid(cid);
1057 if (inside_area(c)) {
1058 data[static_cast<std::size_t>(cid)] = value;
1059 }
1060
1061 for (const auto ncid : navcel_neighbors(cid)) {
1062 const std::size_t nidx = static_cast<std::size_t>(ncid);
1063 if (nidx < navcels.size() && !visited[nidx] && tri_aabb_intersects_box(ncid)) {
1064 visited[nidx] = 1;
1065 q.push_back(ncid);
1066 }
1067 }
1068 }
1069
1070 return true;
1071 }
1072
1073private:
1074 // Geometry fingerprint cache (lazy recompute)
1075 void ensure_geometry_fingerprint_() const;
1076 static std::uint64_t hash_geometry_bytes_(
1077 const float *x, std::size_t nx,
1078 const float *y, std::size_t ny,
1079 const float *z, std::size_t nz,
1080 const std::uint32_t *v0, std::size_t nv0,
1081 const std::uint32_t *v1, std::size_t nv1,
1082 const std::uint32_t *v2, std::size_t nv2);
1083
1084 mutable bool geometry_dirty_{true};
1085 mutable std::uint64_t geometry_fp_{0};
1086
1087 // Builders and traversal helpers.
1088
1093 void build_surface_bvh(Surface & s);
1094
1105 bool surface_raycast(
1106 const Surface & s,
1107 const Eigen::Vector3f & o,
1108 const Eigen::Vector3f & d,
1109 NavCelId & hit_cid,
1110 float & t_out,
1111 Eigen::Vector3f & hit_pt) const;
1112
1123 bool locate_by_walking(
1124 NavCelId start_cid,
1125 const Eigen::Vector3f & p,
1126 NavCelId & cid_out,
1127 Eigen::Vector3f & bary,
1128 Eigen::Vector3f * hit_pt,
1129 float planar_eps) const;
1130
1141 bool surface_closest_triangle(
1142 const Surface & s,
1143 const Eigen::Vector3f & p,
1144 NavCelId & cid,
1145 Eigen::Vector3f & q,
1146 float & best_sq) const;
1147};
1148
1149// Inline convenience overload.
1151 const Eigen::Vector3f & p_world,
1152 size_t & surface_idx,
1153 NavCelId & cid,
1154 Eigen::Vector3f & bary,
1155 Eigen::Vector3f * hit_pt) const
1156{
1157 return locate_navcel(p_world, surface_idx, cid, bary, hit_pt, LocateOpts{});
1158}
1159
1160} // namespace navmap
1161
1162#endif // NAVMAP_CORE__NAVMAP_HPP
Low-level geometry utilities for NavMap cells (triangles), AABB and basic queries such as ray-triangl...
NavMap()
Copy constructor: deep copy of geometry, surfaces, layers and metadata.
Definition NavMap.cpp:79
Registry of named layers (per-NavCel).
Definition NavMap.hpp:209
bool remove(const std::string &name)
Remove a layer by name.
Definition NavMap.hpp:267
void resize_all(size_t nitems)
Resize all known typed layers to nitems.
Definition NavMap.hpp:279
std::shared_ptr< LayerViewBase > get(const std::string &name) const
Get an existing layer by name (untyped view).
Definition NavMap.hpp:243
std::vector< std::string > list() const
List layer names currently in the registry.
Definition NavMap.hpp:253
std::shared_ptr< LayerView< T > > add_or_get(const std::string &name, size_t nitems, LayerType type)
Add a new typed layer or return an existing one with the same name.
Definition NavMap.hpp:224
std::array< NavCelId, 3 > get_neighbors(NavCelId cid) const
Return the three neighbor NavCel ids of triangle cid.
Definition NavMap.hpp:495
Surface create_surface_obj(const std::string &frame_id) const
Create a standalone Surface object (not yet added to the map).
Definition NavMap.cpp:830
void raycast_many(const std::vector< Ray > &rays, std::vector< RayHit > &out, bool first_hit_only=true) const
Batched raycast.
Definition NavMap.cpp:500
void add_navcel_to_surface(std::size_t surface_index, NavCelId cid)
Add an existing nav cell id to a surface.
Definition NavMap.cpp:871
Eigen::Vector3f navcel_centroid(NavCelId cid) const
Triangle centroid (computed on the fly).
Definition NavMap.cpp:878
bool layer_copy(const std::string &src, const std::string &dst)
Copy values from one layer into another.
Definition NavMap.hpp:839
LayerRegistry layers
Per-NavCel layers (runtime registry)
Definition NavMap.hpp:440
std::vector< NavCel > navcels
All triangles (global indexing)
Definition NavMap.hpp:438
NavCelId add_navcel(uint32_t v0, uint32_t v1, uint32_t v2)
Append a triangle (NavCel) and return its id.
Definition NavMap.cpp:864
bool remove_surface(std::size_t surface_index)
Remove a Surface by index.
Definition NavMap.cpp:849
bool locate_navcel_core(const Eigen::Vector3f &p_world, std::size_t &surface_idx, NavCelId &cid, Eigen::Vector3f &bary, Eigen::Vector3f *hit_pt, const LocateOpts &opts) const
Forward to the existing low-level locator (implemented in .cpp).
Definition NavMap.cpp:627
std::optional< Colors > colors
Optional per-vertex colors.
Definition NavMap.hpp:437
std::shared_ptr< LayerView< T > > add_layer(const std::string &name, const std::string &description, const std::string &unit, const std::string &src_layer)
Create (or get) a typed per-NavCel layer, initialized from another layer.
Definition NavMap.hpp:900
bool locate_navcel(const Eigen::Vector3f &p_world, size_t &surface_idx, NavCelId &cid, Eigen::Vector3f &bary, Eigen::Vector3f *hit_pt) const
Locate the triangle under / near a world point (convenience).
Definition NavMap.hpp:1150
std::string layer_type_name(const std::string &name) const
Human-readable type name for a layer ("float", "double", "uint8", ...).
Definition NavMap.cpp:1011
std::size_t add_surface(const Surface &s)
Append an existing Surface (by value).
Definition NavMap.cpp:837
bool has_same_geometry(const NavMap &other) const
Fast check for identical geometry (vertices and NavCel indices).
Definition NavMap.cpp:137
double sample_layer_at(const std::string &name, const Eigen::Vector3f &p_world, double def=std::numeric_limits< double >::quiet_NaN()) const
Sample a per-NavCel layer at a world position.
Definition NavMap.cpp:1036
void layer_clear(const std::string &name, T value=T{})
Reset all values in a layer to a given value.
Definition NavMap.hpp:821
std::vector< NavCelId > navcel_neighbors(NavCelId cid) const
Return up to 3 neighbor cell ids (skips invalid entries).
Definition NavMap.cpp:887
bool set_area(const Eigen::Vector3f &p_world, T value, const std::string &layer_name, AreaShape shape, float size)
Set a per-NavCel layer to a constant value over a 2D area.
Definition NavMap.hpp:973
uint32_t add_vertex(const Eigen::Vector3f &p)
Append a vertex and return its index.
Definition NavMap.cpp:856
std::vector< Surface > surfaces
Surfaces (partitions of navcels)
Definition NavMap.hpp:439
std::shared_ptr< LayerView< T > > add_layer(const std::string &name, const std::string &description={}, const std::string &unit={}, T default_value=T{})
Create (or get) a typed per-NavCel layer with a default value.
Definition NavMap.hpp:685
void mark_vertex_updated(PointId)
Mark a vertex as updated (reserved for future cache invalidation).
Definition NavMap.hpp:488
T navcel_value(NavCelId cid, const LayerView< T > &layer) const
Read the value of a typed per-NavCel layer at triangle cid.
Definition NavMap.hpp:539
std::vector< std::string > list_layers() const
List all layer names currently stored.
Definition NavMap.cpp:1031
std::optional< LayerMeta > get_layer_meta(const std::string &name) const
Return metadata for a layer if present (implementation-defined).
Definition NavMap.cpp:1024
NavMap & operator=(const NavMap &other)
Copy assignment optimized to avoid geometry duplication.
Definition NavMap.cpp:153
bool locate_navcel(const Eigen::Vector3f &p_world, size_t &surface_idx, NavCelId &cid, Eigen::Vector3f &bary, Eigen::Vector3f *hit_pt, const LocateOpts &opts) const
Locate the triangle under / near a world point.
Positions positions
Vertex positions (SoA)
Definition NavMap.hpp:436
void layer_set(const std::string &name, NavCelId cid, T value)
Set a per-cell value (creates/retypes the layer if needed).
Definition NavMap.hpp:716
std::size_t create_surface(std::string frame_id)
Create a new empty Surface and append it to the map.
Definition NavMap.cpp:823
void build_adjacency()
Build topological adjacency between neighboring NavCels.
Definition NavMap.cpp:259
bool has_layer(const std::string &name) const
Check whether a layer named name exists.
Definition NavMap.cpp:1000
NavMap()
Copy constructor: deep copy of geometry, surfaces, layers and metadata.
Definition NavMap.cpp:79
std::size_t layer_size(const std::string &name) const
Number of entries in a layer (should equal navcels.size()).
Definition NavMap.cpp:1005
bool raycast(const Eigen::Vector3f &o, const Eigen::Vector3f &d, NavCelId &hit_cid, float &t, Eigen::Vector3f &hit_pt) const
Raycast against all surfaces to find the closest hit.
Definition NavMap.cpp:466
void rebuild_geometry_accels()
Recompute derived geometry and acceleration structures.
Definition NavMap.cpp:386
T layer_get(const std::string &name, NavCelId cid, T def=T{}) const
Get a per-cell value as the requested type.
Definition NavMap.hpp:741
bool closest_triangle(const Eigen::Vector3f &p_world, size_t &surface_idx, NavCelId &cid, Eigen::Vector3f &closest_point, float &sqdist, int restrict_surface=-1) const
Find the closest triangle to a point.
Definition NavMap.cpp:764
std::unordered_map< std::string, LayerMeta > layer_meta
Optional metadata per layer.
Definition NavMap.hpp:441
Definition Geometry.hpp:40
constexpr LayerType layer_type_tag< double >()
Definition NavMap.hpp:409
constexpr LayerType layer_type_tag< uint8_t >()
Definition NavMap.hpp:407
LayerType
Runtime type tag for a layer's scalar storage.
Definition NavMap.hpp:123
@ F64
Definition NavMap.hpp:123
@ F32
Definition NavMap.hpp:123
@ U8
Definition NavMap.hpp:123
AreaShape
Shape selector for area-writing APIs.
Definition NavMap.hpp:414
@ RECTANGULAR
Definition NavMap.hpp:414
@ CIRCULAR
Definition NavMap.hpp:414
constexpr LayerType layer_type_tag()
Helper to map C++ scalar type to navmap::LayerType tag.
uint32_t NavCelId
Index of a triangle (NavCel) within the global mesh.
Definition NavMap.hpp:65
uint32_t PointId
Index into the per-vertex position arrays (SoA).
Definition NavMap.hpp:63
constexpr LayerType layer_type_tag< float >()
Definition NavMap.hpp:408
LayerView(std::string name, size_t nitems, LayerType t)
Construct a typed view.
Definition NavMap.hpp:173
Options for the locate functions.
Definition NavMap.hpp:554
Axis-aligned bounding box.
Definition Geometry.hpp:152
Node in a per-surface bounding volume hierarchy (BVH).
Definition NavMap.hpp:348
int right
Right child index (or -1 for leaf)
Definition NavMap.hpp:351
int start
Start index in primitive array (leaf only)
Definition NavMap.hpp:352
bool is_leaf() const
Definition NavMap.hpp:354
int count
Number of primitives in leaf (0 for inner nodes)
Definition NavMap.hpp:353
int left
Left child index (or -1 for leaf)
Definition NavMap.hpp:350
AABB box
Bounding box of this node (world coordinates)
Definition NavMap.hpp:349
Optional per-vertex colors (RGBA, 8-bit per channel).
Definition NavMap.hpp:107
std::vector< uint8_t > r
Red channel.
Definition NavMap.hpp:108
std::vector< uint8_t > a
Alpha channel.
Definition NavMap.hpp:111
std::vector< uint8_t > g
Green channel.
Definition NavMap.hpp:109
std::vector< uint8_t > b
Blue channel.
Definition NavMap.hpp:110
Metadata associated to a layer (optional).
Definition NavMap.hpp:310
std::string unit
Physical unit (e.g., "m", "deg", "%")
Definition NavMap.hpp:312
std::string description
Human-readable description.
Definition NavMap.hpp:311
bool per_cell
true for per-NavCel layers (current behavior)
Definition NavMap.hpp:313
Non-templated base for runtime layer handling.
Definition NavMap.hpp:131
virtual LayerType type() const =0
virtual const std::string & name() const =0
virtual std::uint64_t content_hash() const =0
Return 64-bit content hash (cached; recomputed lazily).
std::uint64_t hash_cache_
Definition NavMap.hpp:151
void mark_dirty() const noexcept
Mark content dirty (forces hash recompute on next query).
Definition NavMap.hpp:147
virtual size_t size() const =0
virtual ~LayerViewBase()=default
bool hash_dirty_
Definition NavMap.hpp:150
Typed layer view storing one T value per NavCel.
Definition NavMap.hpp:162
std::uint64_t content_hash() const override
Return 64-bit content hash (cached; recomputed lazily).
Definition NavMap.cpp:47
const std::vector< T > & data() const
Definition NavMap.hpp:189
void set_data(const std::vector< T > &v)
Definition NavMap.hpp:197
std::string name_
Layer name (key in the registry)
Definition NavMap.hpp:163
std::vector< T > data_
Values, one per NavCel.
Definition NavMap.hpp:164
LayerType type_
Runtime type tag (must match T)
Definition NavMap.hpp:165
LayerType type() const override
Definition NavMap.hpp:176
const std::string & name() const override
Definition NavMap.hpp:177
const T & operator[](NavCelId cid) const
Definition NavMap.hpp:183
std::vector< T > & mutable_data() const
Definition NavMap.hpp:193
T & operator[](NavCelId cid)
Definition NavMap.hpp:182
size_t size() const override
Definition NavMap.hpp:178
LayerView(std::string name, size_t nitems, LayerType t)
Construct a typed view.
Definition NavMap.hpp:173
std::vector< T > & data()
Definition NavMap.hpp:187
Navigation cell (triangle) with geometry and adjacency.
Definition NavMap.hpp:330
uint32_t layer_dirty_mask
Reserved for future per-layer flags.
Definition NavMap.hpp:339
Eigen::Vector3f normal
Unit normal of the triangle.
Definition NavMap.hpp:332
NavCelId neighbor[3]
Neighbor cids across edges 0,1,2.
Definition NavMap.hpp:334
float area
Triangle area (m²)
Definition NavMap.hpp:333
PointId v[3]
Indices into navmap::Positions.
Definition NavMap.hpp:331
Options for the locate functions.
Definition NavMap.hpp:554
std::optional< NavCelId > hint_cid
Optional triangle hint for walking.
Definition NavMap.hpp:555
std::optional< size_t > hint_surface
Optional surface hint.
Definition NavMap.hpp:556
bool use_downward_ray
Downward ray on fallback (else upward)
Definition NavMap.hpp:559
float height_eps
Z tolerance for vertical fallback (meters)
Definition NavMap.hpp:558
float planar_eps
In-plane barycentric tolerance.
Definition NavMap.hpp:557
Structure-of-arrays for storing 3D vertex positions.
Definition NavMap.hpp:81
Eigen::Vector3f at(PointId id) const
Returns vertex id as a 3D vector.
Definition NavMap.hpp:95
size_t size() const
Definition NavMap.hpp:87
std::vector< float > x
X coordinates (meters)
Definition NavMap.hpp:82
std::vector< float > z
Z coordinates (meters)
Definition NavMap.hpp:84
std::vector< float > y
Y coordinates (meters)
Definition NavMap.hpp:83
Result of a raycast against the NavMap.
Definition NavMap.hpp:393
Eigen::Vector3f p
World coordinates of intersection.
Definition NavMap.hpp:398
bool hit
True if the ray hit any triangle.
Definition NavMap.hpp:394
size_t surface
Index of surface hit.
Definition NavMap.hpp:395
NavCelId cid
Id of the NavCel hit.
Definition NavMap.hpp:396
float t
Distance along the ray (units of d)
Definition NavMap.hpp:397
Simple ray (origin + direction).
Definition NavMap.hpp:382
Eigen::Vector3f o
Origin of the ray (world)
Definition NavMap.hpp:383
Eigen::Vector3f d
Direction of the ray (unit length recommended)
Definition NavMap.hpp:384
A connected set of NavCels in a common reference frame.
Definition NavMap.hpp:364
std::vector< BVHNode > bvh
BVH nodes for this surface.
Definition NavMap.hpp:369
AABB aabb
Bounds of the surface geometry.
Definition NavMap.hpp:367
std::vector< NavCelId > navcels
NavCels belonging to this surface (global ids)
Definition NavMap.hpp:366
std::string frame_id
Frame id of this surface.
Definition NavMap.hpp:365
std::vector< int > prim_indices
Compact list of cids used by BVH leaves.
Definition NavMap.hpp:368