spatio_temporal_voxel_grid.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  *
3  * Software License Agreement
4  *
5  * Copyright (c) 2018, Simbe Robotics, Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above
15  * copyright notice, this list of conditions and the following
16  * disclaimer in the documentation and/or other materials provided
17  * with the distribution.
18  * * Neither the name of Simbe Robotics, Inc. nor the names of its
19  * contributors may be used to endorse or promote products derived
20  * from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * Author: Steve Macenski (steven.macenski@simberobotics.com)
36  * Purpose: Implement OpenVDB's voxel library with ray tracing for our
37  * internal voxel grid layer.
38  *********************************************************************/
39 
40 #ifndef VOLUME_GRID_H_
41 #define VOLUME_GRID_H_
42 
43 // PCL
44 #include <pcl_ros/transforms.h>
45 #include <pcl/PCLPointCloud2.h>
46 // ROS
47 #include <ros/ros.h>
48 // STL
49 #include <math.h>
50 #include <unordered_map>
51 #include <ctime>
52 #include <iostream>
53 #include <utility>
54 // msgs
55 #include <sensor_msgs/PointCloud2.h>
56 #include <visualization_msgs/Marker.h>
57 // TBB
58 #include <tbb/parallel_do.h>
59 // OpenVDB
60 #include <openvdb/openvdb.h>
61 #include <openvdb/tools/GridTransformer.h>
62 #include <openvdb/tools/RayIntersector.h>
63 // measurement struct and buffer
67 // Mutex and locks
68 #include <boost/thread.hpp>
69 #include <boost/thread/recursive_mutex.hpp>
70 
71 namespace volume_grid
72 {
73 
75 {
76  LINEAR = 0,
79 };
80 
81 // Structure for an occupied cell for map
83 {
84  occupany_cell(const double& _x, const double& _y) :
85  x(_x), y(_y)
86  {
87  }
88 
89  bool operator==(const occupany_cell& other) const
90  {
91  return x==other.x && y==other.y;
92  }
93 
94  double x, y;
95 };
96 
97 // Structure for wrapping frustum model and necessary metadata
99 {
100  frustum_model(geometry::Frustum* _frustum, const double& _factor) :
101  frustum(_frustum), accel_factor(_factor)
102  {
103  }
105  {
106  if (frustum)
107  {
108  delete frustum;
109  }
110  }
112  const double accel_factor;
113 };
114 
115 // Core voxel grid structure and interface
117 {
118 public:
119  // conveniences for line lengths
120  typedef openvdb::math::Ray<openvdb::Real> GridRay;
121  typedef openvdb::math::Ray<openvdb::Real>::Vec3T Vec3Type;
122 
123  SpatioTemporalVoxelGrid(const float& voxel_size, const double& background_value,
124  const GlobalDecayModel& decay_model, const double& voxel_decay,
125  const bool& pub_voxels);
127 
128  // Core making and clearing functions
129  void Mark(const std::vector<observation::MeasurementReading>& marking_observations);
130  void operator()(const observation::MeasurementReading& obs) const;
131  void ClearFrustums(const std::vector<observation::MeasurementReading>& clearing_observations);
132 
133  // Get the pointcloud of the underlying occupancy grid
134  void GetOccupancyPointCloud(pcl::PointCloud<pcl::PointXYZ>::Ptr& pc);
135  std::unordered_map<occupany_cell, uint>* GetFlattenedCostmap();
136 
137  // Clear the grid
138  bool ResetGrid(void);
139 
140  // Save the file to file with size information
141  bool SaveGrid(const std::string& file_name, double& map_size_bytes);
142 
143 protected:
144  // Initialize grid metadata and library
145  void InitializeGrid(void);
146 
147  // grid accessor methods
148  bool MarkGridPoint(const openvdb::Coord& pt, const double& value) const;
149  bool ClearGridPoint(const openvdb::Coord& pt) const;
150 
151  // Check occupancy status of the grid
152  bool IsGridEmpty(void) const;
153 
154  // Get time information for clearing
155  double GetTemporalClearingDuration(const double& time_delta);
156  double GetFrustumAcceleration(const double& time_delta, \
157  const double& acceleration_factor);
158  void TemporalClearAndGenerateCostmap(std::vector<frustum_model>& frustums);
159 
160  // Populate the costmap ROS api and pointcloud with a marked point
161  void PopulateCostmapAndPointcloud(const openvdb::Coord& pt);
162 
163  // Utilities for tranformation
164  openvdb::Vec3d WorldToIndex(const openvdb::Vec3d& coord) const;
165  openvdb::Vec3d IndexToWorld(const openvdb::Coord& coord) const;
166 
167  mutable openvdb::DoubleGrid::Ptr _grid;
169  double _background_value, _voxel_size, _voxel_decay;
171  pcl::PointCloud<pcl::PointXYZ>::Ptr _pc;
172  std::unordered_map<occupany_cell, uint>* _cost_map;
173  boost::mutex _grid_lock;
174 };
175 
176 } // end volume_grid namespace
177 
178 // hash function for unordered_map of occupancy_cells
179 namespace std {
180 template <>
182 {
183  std::size_t operator()(const volume_grid::occupany_cell& k) const
184  {
185  return ((std::hash<double>()(k.x) ^ (std::hash<double>()(k.y) << 1)) >> 1);
186  }
187 };
188 
189 } // end std namespace
190 
191 #endif
std::size_t operator()(const volume_grid::occupany_cell &k) const
pcl::PointCloud< pcl::PointXYZ >::Ptr _pc
bool operator==(const occupany_cell &other) const
std::unordered_map< occupany_cell, uint > * _cost_map
openvdb::math::Ray< openvdb::Real >::Vec3T Vec3Type
frustum_model(geometry::Frustum *_frustum, const double &_factor)
occupany_cell(const double &_x, const double &_y)
openvdb::math::Ray< openvdb::Real > GridRay


spatio_temporal_voxel_layer
Author(s):
autogenerated on Sat Dec 21 2019 04:06:19