ArVizDrawer.cpp
Go to the documentation of this file.
1 #include "ArVizDrawer.h"
2 
3 #include <Eigen/Core>
4 #include <Eigen/Geometry>
5 
6 #include <pcl/point_cloud.h>
7 #include <pcl/point_types.h>
8 
9 #include <SimoxUtility/iterator/xy_index_range_iterator.h>
10 #include <SimoxUtility/shapes/OrientedBox.h>
11 
13 
17 
19 {
20 
21  template <typename EigenVectorT>
22  EigenVectorT toCartesian(const LaserScanStep& laserScanStep)
23  {
24  EigenVectorT point = EigenVectorT::Zero();
25 
26  point.x() = laserScanStep.distance * std::cos(laserScanStep.angle);
27  point.y() = laserScanStep.distance * std::sin(laserScanStep.angle);
28 
29  return point;
30  }
31 
32 
33 
34 
35  void ArVizDrawer::drawBoundingBoxes(const std::vector<VirtualRobot::BoundingBox>& boundingBoxes)
36  {
37 
38  auto layer = this->layer("bounding_boxes");
39  int i = 0;
40 
41  for (const VirtualRobot::BoundingBox& bb : boundingBoxes)
42  {
43  const Eigen::Vector3f center((bb.getMin() + bb.getMax()) / 2);
44  const Eigen::Vector3f extents = (bb.getMax() - bb.getMin());
45 
46  const auto box =
47  viz::Box("bb_" + std::to_string(i++))
48  .position(center)
49  .size(extents)
50  .color(0, 255, 0, 255);
51  layer.add(box);
52  }
53 
54  commit(layer);
55  }
56 
58  {
59  public:
60  using Color = Eigen::Vector4i;
61  using Position = Eigen::Vector3f;
62 
63  using ColorGrid = std::vector<std::vector<Color>>;
64  using VertexGrid = std::vector<std::vector<Position>>;
65 
66  ColoredMeshGrid(Eigen::Array2i numCells) :
67  numCells(numCells),
68  vertices(numCells.x(), std::vector<Position>(numCells.y(), Position::Zero())),
69  colors(numCells.x(), std::vector<Color>(numCells.y(), Color::Zero()))
70  {
71  }
72 
73  void
74  setColoredVertex(const Eigen::Array2i& idx, const Position& position, const Color& color)
75  {
76  vertices.at(idx.x()).at(idx.y()) = position;
77  colors.at(idx.x()).at(idx.y()) = color;
78  }
79 
80  void apply(const auto& posFn, const auto& colorFn)
81  {
82  for (int x = 0; x < numCells.x(); x++)
83  {
84  for (int y = 0; y < numCells.y(); y++)
85  {
86  const Eigen::Array2i idx{x, y};
87  setColoredVertex(idx, posFn(idx), colorFn(idx));
88  }
89  }
90  }
91 
92  std::vector<std::vector<viz::data::Color>> getColors()
93  {
94  std::vector<std::vector<viz::data::Color>> colors(
95  numCells.x(), std::vector<viz::data::Color>(numCells.y(), viz::Color::black()));
96 
97  auto toVizColor = [](const ColoredMeshGrid::Color & c)
98  {
99  return viz::Color(c.x(), c.y(), c.z(), c.w());
100  };
101 
102  auto xyIter = simox::iterator::XYIndexRangeIterator({numCells.x(), numCells.y()});
103  std::for_each(xyIter.begin(),
104  xyIter.end(),
105  [&](const auto & p)
106  {
107  const auto& [x, y] = p;
108  colors[x][y] = toVizColor(this->colors[x][y]);
109  });
110 
111  return colors;
112  }
113 
114  const VertexGrid& getVertices() const
115  {
116  return vertices;
117  }
118 
119  private:
120  Eigen::Array2i numCells;
121 
122  VertexGrid vertices;
123  ColorGrid colors;
124  };
125 
126  void ArVizDrawer::drawOccupancyGrid(const OccupancyGrid& grid, const float boxPosZ)
127  {
128  ARMARX_INFO << "Occupancy grid size " << Eigen::Array2i{grid.sizeX, grid.sizeY};
129 
130  auto layer = this->layer("occupancy_grid");
131 
132  ColoredMeshGrid meshGrid(Eigen::Array2i{grid.sizeX, grid.sizeY});
133 
134  const auto posFn = [&](const Eigen::Array2i & idx) -> ColoredMeshGrid::Position
135  {
136  const auto pos2d = grid.getCentralPosition(idx.x(), idx.y());
137  return {pos2d.x(), pos2d.y(), 0.F};
138  };
139 
140  const ColoredMeshGrid::Color colorBlack{0, 0, 0, 255};
141  const ColoredMeshGrid::Color colorWhite{255, 255, 255, 255};
142 
143  const auto colorFn = [&](const Eigen::Array2i & idx) -> ColoredMeshGrid::Color
144  {
145  if (grid.isOccupied(static_cast<std::size_t>(idx.x()),
146  static_cast<std::size_t>(idx.y())))
147  {
148  ARMARX_IMPORTANT << "Cell is occupied";
149  }
150  return grid.isOccupied(static_cast<std::size_t>(idx.x()),
151  static_cast<std::size_t>(idx.y()))
152  ? colorBlack
153  : colorWhite;
154  };
155 
156  meshGrid.apply(posFn, colorFn);
157 
158  const auto mesh = viz::Mesh("grid")
159  .position(Eigen::Vector3f{0.F, 0.F, boxPosZ})
160  .grid2D(meshGrid.getVertices(), meshGrid.getColors());
161 
162  layer.add(mesh);
163  commit(layer);
164  }
165 
166  pcl::PointCloud<pcl::PointXYZ> toPointCloud(const LaserScan& scan)
167  {
168  pcl::PointCloud<pcl::PointXYZ> cloud;
169  cloud.points.reserve(scan.size());
170 
171  std::transform(scan.begin(),
172  scan.end(),
173  std::back_inserter(cloud.points),
174  [&](const auto & scanStep) -> pcl::PointXYZ
175  {
176  const auto rangePoint = toCartesian<Eigen::Vector3f>(scanStep); // [mm]
177 
178  pcl::PointXYZ pt;
179  pt.x = rangePoint.x();
180  pt.y = rangePoint.y();
181  pt.z = rangePoint.z();
182 
183  return pt;
184  });
185 
186  cloud.width = scan.size();
187  cloud.height = 1;
188  cloud.is_dense = true;
189 
190  return cloud;
191  }
192 
193  void ArVizDrawer::prepareScan(const LaserScan& scan,
194  const std::string& frame,
195  const Eigen::Affine3f& scannerPose)
196  {
197 
198  const auto pointCloud = toPointCloud(scan);
199 
200  frames.insert(frame);
201 
202  // skip the first two colors as they are 'black' and 'white'
203  const int id = std::distance(std::begin(frames), frames.find(frame)) + 2;
204 
205  clouds.emplace(
206  frame,
207  viz::PointCloud(frame)
208  .pose(scannerPose)
209  .pointSizeInPixels(5)
210  .pointCloud(pointCloud.points, viz::Color(simox::color::GlasbeyLUT::at(id))));
211  }
212 
214  {
215  auto layer = this->layer("scans");
216 
217  for (const auto& [_, pc] : clouds)
218  {
219  layer.add(pc);
220  }
221 
222  commit(layer);
223 
224  // remove all
225  clouds.clear();
226  }
227 
228 } // namespace armarx::laser_scanner_simulation
armarx::laser_scanner_simulation::ArVizDrawer::drawBoundingBoxes
void drawBoundingBoxes(const std::vector< VirtualRobot::BoundingBox > &boundingBoxes)
Definition: ArVizDrawer.cpp:35
armarx::viz::Color::black
static Color black(int a=255)
Definition: Color.h:62
Client.h
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
armarx::laser_scanner_simulation::toCartesian
EigenVectorT toCartesian(const LaserScanStep &laserScanStep)
Definition: ArVizDrawer.cpp:22
armarx::laser_scanner_simulation::ArVizDrawer::prepareScan
void prepareScan(const LaserScan &scan, const std::string &frame, const Eigen::Affine3f &scannerPose)
Definition: ArVizDrawer.cpp:193
armarx::laser_scanner_simulation::ColoredMeshGrid::ColorGrid
std::vector< std::vector< Color > > ColorGrid
Definition: ArVizDrawer.cpp:63
armarx::armem::vision::OccupancyGrid
Definition: types.h:36
armarx::laser_scanner_simulation::ArVizDrawer::drawOccupancyGrid
void drawOccupancyGrid(const OccupancyGrid &grid, float boxPosZ)
Definition: ArVizDrawer.cpp:126
ARMARX_IMPORTANT
#define ARMARX_IMPORTANT
Definition: Logging.h:183
ArVizDrawer.h
armarx::laser_scanner_simulation::ColoredMeshGrid::Position
Eigen::Vector3f Position
Definition: ArVizDrawer.cpp:61
armarx::viz::Mesh
Definition: Mesh.h:28
armarx::laser_scanner_simulation::ColoredMeshGrid::VertexGrid
std::vector< std::vector< Position > > VertexGrid
Definition: ArVizDrawer.cpp:64
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::laser_scanner_simulation::ColoredMeshGrid::getColors
std::vector< std::vector< viz::data::Color > > getColors()
Definition: ArVizDrawer.cpp:92
Elements.h
armarx::laser_scanner_simulation::ColoredMeshGrid::getVertices
const VertexGrid & getVertices() const
Definition: ArVizDrawer.cpp:114
armarx::viz::ElementOps::position
DerivedT & position(float x, float y, float z)
Definition: ElementOps.h:127
armarx::laser_scanner_simulation::ColoredMeshGrid::apply
void apply(const auto &posFn, const auto &colorFn)
Definition: ArVizDrawer.cpp:80
armarx::laser_scanner_simulation::ColoredMeshGrid::setColoredVertex
void setColoredVertex(const Eigen::Array2i &idx, const Position &position, const Color &color)
Definition: ArVizDrawer.cpp:74
armarx::laser_scanner_simulation
Definition: ArVizDrawer.cpp:18
Color
uint32_t Color
RGBA color.
Definition: color.h:8
armarx::laser_scanner_simulation::toPointCloud
pcl::PointCloud< pcl::PointXYZ > toPointCloud(const LaserScan &scan)
Definition: ArVizDrawer.cpp:166
armarx::viz::Color
Definition: Color.h:13
armarx::laser_scanner_simulation::ColoredMeshGrid::Color
Eigen::Vector4i Color
Definition: ArVizDrawer.cpp:60
armarx::viz::PointCloud
Definition: PointCloud.h:21
armarx::viz::Box
Definition: Elements.h:51
armarx::laser_scanner_simulation::ArVizDrawer::drawScans
void drawScans()
Definition: ArVizDrawer.cpp:213
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::laser_scanner_simulation::ColoredMeshGrid
Definition: ArVizDrawer.cpp:57
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
std
Definition: Application.h:66
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
armarx::viz::Box::size
Box & size(Eigen::Vector3f const &s)
Definition: Elements.h:55
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:195
distance
double distance(const Point &a, const Point &b)
Definition: point.hpp:88
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
armarx::viz::ScopedClient::layer
Layer layer(std::string const &name) const
Definition: ScopedClient.cpp:34
Logging.h
armarx::laser_scanner_simulation::ColoredMeshGrid::ColoredMeshGrid
ColoredMeshGrid(Eigen::Array2i numCells)
Definition: ArVizDrawer.cpp:66
PointCloud.h