octomap_render.cpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2013, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Julius Kammerl */
36 
38 
39 #include <octomap_msgs/Octomap.h>
40 #include <octomap/octomap.h>
41 
42 #include <OgreSceneNode.h>
43 #include <OgreSceneManager.h>
44 
46 
47 namespace moveit_rviz_plugin
48 {
49 typedef std::vector<rviz::PointCloud::Point> VPoint;
50 typedef std::vector<VPoint> VVPoint;
51 
52 OcTreeRender::OcTreeRender(const std::shared_ptr<const octomap::OcTree>& octree,
53  OctreeVoxelRenderMode octree_voxel_rendering, OctreeVoxelColorMode octree_color_mode,
54  std::size_t max_octree_depth, Ogre::SceneManager* scene_manager,
55  Ogre::SceneNode* parent_node = NULL)
56  : octree_(octree), colorFactor_(0.8)
57 {
58  if (!parent_node)
59  {
60  parent_node = scene_manager_->getRootSceneNode();
61  }
62 
63  if (!max_octree_depth)
64  {
65  octree_depth_ = octree->getTreeDepth();
66  }
67  else
68  {
69  octree_depth_ = std::min(max_octree_depth, (std::size_t)octree->getTreeDepth());
70  }
71 
72  scene_node_ = parent_node->createChildSceneNode();
73 
74  cloud_.resize(octree_depth_);
75 
76  for (std::size_t i = 0; i < octree_depth_; ++i)
77  {
78  std::stringstream sname;
79  sname << "PointCloud Nr." << i;
80  cloud_[i] = new rviz::PointCloud();
81  cloud_[i]->setName(sname.str());
82  cloud_[i]->setRenderMode(rviz::PointCloud::RM_BOXES);
83  scene_node_->attachObject(cloud_[i]);
84  }
85 
86  octreeDecoding(octree, octree_voxel_rendering, octree_color_mode);
87 }
88 
90 {
91  scene_node_->detachAllObjects();
92 
93  for (std::size_t i = 0; i < octree_depth_; ++i)
94  {
95  delete cloud_[i];
96  }
97 }
98 
99 void OcTreeRender::setPosition(const Ogre::Vector3& position)
100 {
101  scene_node_->setPosition(position);
102 }
103 
104 void moveit_rviz_plugin::OcTreeRender::setOrientation(const Ogre::Quaternion& orientation)
105 {
106  scene_node_->setOrientation(orientation);
107 }
108 
109 // method taken from octomap_server package
110 void OcTreeRender::setColor(double z_pos, double min_z, double max_z, double color_factor,
112 {
113  int i;
114  double m, n, f;
115 
116  double s = 1.0;
117  double v = 1.0;
118 
119  double h = (1.0 - std::min(std::max((z_pos - min_z) / (max_z - min_z), 0.0), 1.0)) * color_factor;
120 
121  h -= floor(h);
122  h *= 6;
123  i = floor(h);
124  f = h - i;
125  if (!(i & 1))
126  f = 1 - f; // if i is even
127  m = v * (1 - s);
128  n = v * (1 - s * f);
129 
130  switch (i)
131  {
132  case 6:
133  case 0:
134  point->setColor(v, n, m);
135  break;
136  case 1:
137  point->setColor(n, v, m);
138  break;
139  case 2:
140  point->setColor(m, v, n);
141  break;
142  case 3:
143  point->setColor(m, n, v);
144  break;
145  case 4:
146  point->setColor(n, m, v);
147  break;
148  case 5:
149  point->setColor(v, m, n);
150  break;
151  default:
152  point->setColor(1, 0.5, 0.5);
153  break;
154  }
155 }
156 
157 void OcTreeRender::octreeDecoding(const std::shared_ptr<const octomap::OcTree>& octree,
158  OctreeVoxelRenderMode octree_voxel_rendering, OctreeVoxelColorMode octree_color_mode)
159 {
160  VVPoint pointBuf_;
161  pointBuf_.resize(octree_depth_);
162 
163  // get dimensions of octree
164  double minX, minY, minZ, maxX, maxY, maxZ;
165  octree->getMetricMin(minX, minY, minZ);
166  octree->getMetricMax(maxX, maxY, maxZ);
167 
168  unsigned int render_mode_mask = static_cast<unsigned int>(octree_voxel_rendering);
169 
170  size_t pointCount = 0;
171  {
172  // traverse all leafs in the tree:
173  for (octomap::OcTree::iterator it = octree->begin(octree_depth_), end = octree->end(); it != end; ++it)
174  {
175  bool display_voxel = false;
176 
177  // the left part evaluates to 1 for free voxels and 2 for occupied voxels
178  if (((int)octree->isNodeOccupied(*it) + 1) & render_mode_mask)
179  {
180  // check if current voxel has neighbors on all sides -> no need to be displayed
181  bool allNeighborsFound = true;
182 
183  octomap::OcTreeKey key;
184  octomap::OcTreeKey nKey = it.getKey();
185 
186  for (key[2] = nKey[2] - 1; allNeighborsFound && key[2] <= nKey[2] + 1; ++key[2])
187  {
188  for (key[1] = nKey[1] - 1; allNeighborsFound && key[1] <= nKey[1] + 1; ++key[1])
189  {
190  for (key[0] = nKey[0] - 1; allNeighborsFound && key[0] <= nKey[0] + 1; ++key[0])
191  {
192  if (key != nKey)
193  {
194  octomap::OcTreeNode* node = octree->search(key);
195 
196  // the left part evaluates to 1 for free voxels and 2 for occupied voxels
197  if (!(node && (((int)octree->isNodeOccupied(node)) + 1) & render_mode_mask))
198  {
199  // we do not have a neighbor => break!
200  allNeighborsFound = false;
201  }
202  }
203  }
204  }
205  }
206 
207  display_voxel |= !allNeighborsFound;
208  }
209 
210  if (display_voxel)
211  {
212  rviz::PointCloud::Point newPoint;
213 
214  newPoint.position.x = it.getX();
215  newPoint.position.y = it.getY();
216  newPoint.position.z = it.getZ();
217 
218  float cell_probability;
219 
220  switch (octree_color_mode)
221  {
223  setColor(newPoint.position.z, minZ, maxZ, colorFactor_, &newPoint);
224  break;
226  cell_probability = it->getOccupancy();
227  newPoint.setColor((1.0f - cell_probability), cell_probability, 0.0);
228  break;
229  default:
230  break;
231  }
232 
233  // push to point vectors
234  unsigned int depth = it.getDepth();
235  pointBuf_[depth - 1].push_back(newPoint);
236 
237  ++pointCount;
238  }
239  }
240  }
241 
242  for (size_t i = 0; i < octree_depth_; ++i)
243  {
244  double size = octree->getNodeSize(i + 1);
245 
246  cloud_[i]->clear();
247  cloud_[i]->setDimensions(size, size, size);
248 
249  cloud_[i]->addPoints(&pointBuf_[i].front(), pointBuf_[i].size());
250  pointBuf_[i].clear();
251  }
252 }
253 }
std::vector< rviz::PointCloud::Point > VPoint
void setColor(double z_pos, double min_z, double max_z, double color_factor, rviz::PointCloud::Point *point)
XmlRpcServer s
f
std::size_t size(custom_string const &s)
std::vector< VPoint > VVPoint
v
void setOrientation(const Ogre::Quaternion &orientation)
Ogre::Vector3 position
void setPosition(const Ogre::Vector3 &position)
Ogre::SceneManager * scene_manager_
OcTreeRender(const std::shared_ptr< const octomap::OcTree > &octree, OctreeVoxelRenderMode octree_voxel_rendering, OctreeVoxelColorMode octree_color_mode, std::size_t max_octree_depth, Ogre::SceneManager *scene_manager, Ogre::SceneNode *parent_node)
std::vector< rviz::PointCloud * > cloud_
void octreeDecoding(const std::shared_ptr< const octomap::OcTree > &octree, OctreeVoxelRenderMode octree_voxel_rendering, OctreeVoxelColorMode octree_color_mode)
void setColor(float r, float g, float b, float a=1.0)


visualization
Author(s): Ioan Sucan , Dave Coleman , Sachin Chitta
autogenerated on Sun Oct 18 2020 13:19:09