visualization.cpp
Go to the documentation of this file.
1#include "visualization.h"
2
3#include <cstdint>
4#include <string>
5#include <vector>
6
7#include <Eigen/Core>
8
9#include <Ice/Config.h>
10
11#include <SimoxUtility/color/cmaps/colormaps.h>
12
17#include <RobotAPI/interface/ArViz/Elements.h>
18
22
24{
25 namespace
26 {
27
28 // helper function to avoid code duplication
29 void
30 visualizeGrid(
31 const algorithms::Costmap::Grid& grid,
32 const std::optional<algorithms::Costmap::Mask>& mask,
34 indexToGlobal,
35 viz::Layer& layer,
36 const std::string& name,
37 const float zOffset)
38 {
39 const auto cmap = simox::color::cmaps::viridis();
40 const float vmax = grid.array().maxCoeff();
41
42 const auto asColor = [&cmap, &vmax](const float distance,
43 const bool isValid) -> viz::data::Color
44 {
45 const auto color = cmap.at(distance, 0.F, vmax);
46
47 // Also, negative distances should be skipped (even if this would provide meaningful information).
48 const Ice::Byte alpha = isValid && distance >= 0 ? color.a : 0;
49 return {alpha, color.r, color.g, color.b};
50 };
51
52 const std::int64_t cols = grid.cols();
53 const std::int64_t rows = grid.rows();
54
55 auto mesh = viz::Mesh(name);
56
57 std::vector<std::vector<Eigen::Vector3f>> vertices;
58 std::vector<std::vector<viz::data::Color>> colors;
59
60 for (int r = 0; r < rows; r++)
61 {
62 auto& verticesRow = vertices.emplace_back(cols);
63 auto& colorsRow = colors.emplace_back(cols);
64
65 for (int c = 0; c < cols; c++)
66 {
67 verticesRow.at(c) = conv::to3D(indexToGlobal({r, c}));
68
69 const bool isValid = mask.has_value() ? mask.value()(r, c) : true;
70 colorsRow.at(c) = asColor(grid(r, c), isValid);
71 }
72 }
73
74 mesh.grid2D(vertices, colors);
75 mesh.position({0, 0, zOffset});
76
77 layer.add(mesh);
78 }
79 } // namespace
80
81 void
83 viz::Layer& layer,
84 const std::string& name,
85 const float zOffset)
86 {
87 // visualizeGrid(
88 // costmap.getGrid(),
89 // costmap.getMask(),
90 // [&costmap](const auto& idx) { return costmap.toPositionGlobal(idx); },
91 // layer,
92 // name,
93 // zOffset);
94
95 // // also visualize origin
96 // layer.add(viz::Pose("origin").pose(conv::to3D(costmap.origin()).matrix()));
97
98 auto e = viz::Grid(name);
99
100 core::Pose origin = conv::to3D(costmap.origin());
101 origin.translation().z() += zOffset;
102
103 const Eigen::Translation3f origin_T_costmap{
104 costmap.getLocalSceneBounds().min.x(), costmap.getLocalSceneBounds().min.y(), 0.F};
105
106 origin *= origin_T_costmap;
107
108 e.origin(origin);
109
110 e.resolution(costmap.params().cellSize);
111
112 e.grid(costmap.getGrid(), costmap.getMask());
113
114 layer.add(e);
115 }
116
117 void
119 std::vector<viz::Layer>& layers,
120 const std::string& name,
121 const float zOffset,
122 bool initializeMask)
123 {
124 for (int i = 0; i < costmap.params().orientations; ++i)
125 {
126 std::stringstream ori_name;
127 ori_name << name << "_" << i;
128
129 visualize(costmap.costmapForOrientation(i, initializeMask),
130 layers[i],
131 ori_name.str(),
132 zOffset + 0.01F * i);
133
134 /*visualizeGrid(
135 costmap.getGrid()[i],
136 costmap.getMask(),
137 [&costmap](const auto& idx) { return costmap.toPositionGlobal(idx); },
138 layers[i],
139 ori_name.str(),
140 zOffset + 0.001F*i);*/
141 }
142
143
144 // also visualize origin
145 //layers[0].add(viz::Pose("origin").pose(conv::to3D(costmap.origin()).matrix()));
146 }
147
148
149} // namespace armarx::navigation::algorithms
constexpr T c
This file is part of ArmarX.
void visualize(const algorithms::Costmap &costmap, viz::Layer &layer, const std::string &name, const float zOffset)
std::vector< Eigen::Vector3f > to3D(const std::vector< Eigen::Vector2f > &v)
Definition eigen.cpp:14
Eigen::Isometry3f Pose
Definition basic_types.h:31
double distance(const Point &a, const Point &b)
Definition point.hpp:95
void add(ElementT const &element)
Definition Layer.h:31