20 #include <pcl/kdtree/kdtree_flann.h> 43 std::string grid_path;
44 if (map_path.compare(map_path.length() - 3, 3,
".bt") == 0)
45 grid_path = map_path.substr(0, map_path.find(
".bt")) +
".grid";
46 if (map_path.compare(map_path.length() - 3, 3,
".ot") == 0)
47 grid_path = map_path.substr(0, map_path.find(
".ot")) +
".grid";
69 msg.header.frame_id =
"grid3d";
74 msg.info.origin.position.x = 0.;
75 msg.info.origin.position.y = 0.;
76 msg.info.origin.position.z =
static_cast<double>(z);
77 msg.info.origin.orientation.x = 0.;
78 msg.info.origin.orientation.y = 0.;
79 msg.info.origin.orientation.z = 0.;
80 msg.info.origin.orientation.w = 1.;
86 float max_prob = -1.0;
87 for (uint32_t i = offset; i < end; ++i)
88 if (
grid_[i].prob > max_prob)
89 max_prob =
grid_[i].prob;
92 if (max_prob < 0.000001
f)
94 max_prob = 100.f / max_prob;
96 msg.data[i] = static_cast<int8_t>(
grid_[i + offset].prob * max_prob);
108 msg.header.frame_id =
"grid3d";
123 const float tz,
const float a)
const 128 pcl::PointXYZ new_point;
130 const auto sa = sin(a);
131 const auto ca = cos(a);
136 for (uint32_t i = 0; i < points.size(); ++i)
138 const auto& p = points[i];
140 new_point.x = ca * p.x - sa * p.y + tx;
141 new_point.y = sa * p.x + ca * p.y + ty;
142 new_point.z = p.z + tz;
144 if (new_point.x >= 0.f && new_point.y >= 0.f && new_point.z >= 0.f && new_point.x <
max_x_ &&
147 weight +=
grid_[
point2grid(new_point.x, new_point.y, new_point.z)].prob;
151 return (n <= 10) ? 0 : weight / n;
168 if (map_path.length() <= 3)
171 if (map_path.compare(map_path.length() - 3, 3,
".bt") == 0)
175 else if (map_path.compare(map_path.length() - 3, 3,
".ot") == 0)
192 double min_x, min_y, min_z, max_x, max_y, max_z;
193 octomap_->getMetricMin(min_x, min_y, min_z);
194 octomap_->getMetricMax(max_x, max_y, max_z);
195 max_x_ =
static_cast<float>(max_x - min_x);
196 max_y_ =
static_cast<float>(max_y - min_y);
197 max_z_ =
static_cast<float>(max_z - min_z);
219 cloud_.reset(
new pcl::PointCloud<pcl::PointXYZ>());
225 for (octomap::OcTree::leaf_iterator it =
octomap_->begin_leafs(); it !=
octomap_->end_leafs(); ++it)
227 if (it !=
nullptr &&
octomap_->isNodeOccupied(*it))
241 auto pf = fopen(grid_path.c_str(),
"wb");
267 auto pf = fopen(grid_path.c_str(),
"rb");
280 fread(&sensor_dev,
sizeof(
double), 1, pf);
282 if (std::fabs(sensor_dev -
sensor_dev_) >= std::numeric_limits<double>::epsilon())
317 pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
318 kdtree.setInputCloud(
cloud_);
321 const float gauss_const1 =
static_cast<float>(1. / (
sensor_dev_ * sqrt(2 *
M_PI)));
325 pcl::PointXYZ search_point;
326 std::vector<int> point_idx_nkn_search(1);
327 std::vector<float> point_nkn_squared_distance(1);
337 if (kdtree.nearestKSearch(search_point, 1, point_idx_nkn_search, point_nkn_squared_distance) > 0)
339 dist = point_nkn_squared_distance[0];
340 grid_[index].dist = dist;
341 grid_[index].prob = gauss_const1 * expf(-dist * dist * gauss_const2);
345 grid_[index].dist = -1.0;
346 grid_[index].prob = 0.0;
bool isIntoMap(const float x, const float y, const float z) const
bool loadGrid(const std::string &grid_path)
uint32_t point2grid(const float x, const float y, const float z) const
static AbstractOcTree * read(const std::string &filename)
bool open(const std::string &map_path)
ROSCPP_DECL const std::string & getName()
std::unique_ptr< octomap::OcTree > octomap_
Octomap parameters.
std::unique_ptr< GridCell[]> grid_
bool buildGridSliceMsg(const float z, nav_msgs::OccupancyGrid &msg) const
bool loadOctomap(const std::string &map_path)
bool saveGrid(const std::string &grid_path)
float computeCloudWeight(const std::vector< pcl::PointXYZ > &points, const float tx, const float ty, const float tz, const float a) const
3D probabilistic grid cell
bool buildMapPointCloudMsg(sensor_msgs::PointCloud2 &msg) const
void buildGrid3d2WorldTf(const std::string &global_frame_id, tf::StampedTransform &tf) const
void getMinOctomap(float &x, float &y, float &z) const
void toROSMsg(const sensor_msgs::PointCloud2 &cloud, sensor_msgs::Image &image)
Grid3d(const double sensor_dev)
pcl::PointCloud< pcl::PointXYZ >::Ptr cloud_
3D point cloud representation of the map
double sensor_dev_
Init parameter.