NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
octoclass.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef MAPPER_OCTOCLASS_H_
20 #define MAPPER_OCTOCLASS_H_
21 
22 #include <octomap/octomap.h>
23 #include <octomap/OcTree.h>
24 #include <pcl/point_cloud.h>
25 #include <tf/transform_broadcaster.h>
26 #include <pcl/point_types.h>
27 #include <sensor_msgs/point_cloud2_iterator.h>
28 #include <visualization_msgs/MarkerArray.h>
29 #include <vector>
30 #include <iostream>
32 #include "mapper/linear_algebra.h"
33 
34 namespace octoclass {
35 
36 // 3D occupancy grid
37 class OctoClass{
38  public:
39  octomap::OcTree tree_ = octomap::OcTree(0.1); // create empty tree with resolution 0.1
40  octomap::OcTree tree_inflated_ = octomap::OcTree(0.1); // create empty tree with resolution 0.1
41  double memory_time_; // Fading memory of the tree in seconds
43 
44  // Constructor
45  explicit OctoClass(const double resolution_in); // Resolution in meters
46  OctoClass();
47 
48  // Mapping methods
49  void SetMemoryTime(const double memory); // Fading memory time
50  double GetMemoryTime(); // Returns fading memory time
51  void SetMaxRange(const double max_range); // Max range for mapping
52  void SetMinRange(const double min_range); // Min range for mapping
53  inline double GetResolution() const {return resolution_;} // Returns resolution
54  void SetResolution(const double resolution_in); // Resolution of the octomap
55  double GetResolution(); // Returns resolution of the octomap
56  void ResetMap(); // Reset the octomap structure
57  void SetMapInflation(const double inflate_radius); // Set the inflation radius
58  double GetMapInflation(); // Returns the inflation radius
59  void SetCamFrustum(const double fov,
60  const double aspect_ratio);
61  void SetOccupancyThreshold(const double occupancy_threshold);
62  void SetHitMissProbabilities(const double probability_hit,
63  const double probability_miss);
64  void SetClampingThresholds(const double clamping_threshold_min,
65  const double clamping_threshold_max);
66  // DEPRECATED: turns leaf into voxel representation
67  // octomap::point3d_list Voxelize(const octomap::OcTree::leaf_iterator &leaf);
68  // DEPRECATED: Convert from octomap to pointcloud2
69  // void PointsOctomapToPointCloud2(const octomap::point3d_list& points,
70  // sensor_msgs::PointCloud2* cloud);
71  void PclToRayOctomap(const pcl::PointCloud< pcl::PointXYZ > &cloud,
72  const geometry_msgs::TransformStamped &tf_cam2world,
73  const algebra_3d::FrustumPlanes &frustum); // Map obstacles and free area
74  void ComputeUpdate(const octomap::KeySet &occ_inflated, // Inflated endpoints
75  const octomap::KeySet &occ_slim, // Non-inflated endpoints
76  const octomap::point3d& origin,
77  const double &maxrange,
78  octomap::KeySet *occ_slim_in_range,
79  octomap::KeySet *free_slim,
80  octomap::KeySet *free_inflated); // Raycasting method for inflated maps
81  void FadeMemory(const double &rate); // Run fading memory method
82  // DEPRECATED: it was used to inflate the whole map (too expensive)
83  // void InflateObstacles(const double &thickness);
84  // DEPRECATED: Returns all colliding nodes in the pcl
85  // void FindCollidingNodesTree(const pcl::PointCloud< pcl::PointXYZ > &point_cloud,
86  // std::vector<octomap::point3d> *colliding_nodes);
87  // Returns points from the pcl that collides with inflated tree
88  void FindCollidingNodesInflated(const pcl::PointCloud< pcl::PointXYZ > &point_cloud,
89  std::vector<octomap::point3d> *colliding_nodes);
90 
91  // Visualization methods
92  void TreeVisMarkers(visualization_msgs::MarkerArray *obstacles,
93  visualization_msgs::MarkerArray *free,
94  sensor_msgs::PointCloud2* obstacles_cloud,
95  sensor_msgs::PointCloud2* free_cloud);
96  void InflatedVisMarkers(visualization_msgs::MarkerArray *obstacles,
97  visualization_msgs::MarkerArray *free,
98  sensor_msgs::PointCloud2* obstacles_cloud,
99  sensor_msgs::PointCloud2* free_cloud);
100  // void freeVisMarkers(visualization_msgs::MarkerArray* marker_array);
101  // void inflatedFreeVisMarkers(visualization_msgs::MarkerArray* marker_array);
102 
103  // Useful methods
104  // checkOccupancy functions: Returns -1 if node is unknown, 0 if its free and 1 if its occupied
105  int CheckOccupancy(const octomap::point3d &p); // Point collision
106  int CheckOccupancy(const pcl::PointXYZ &p); // Point collision
107  int CheckOccupancy(const Eigen::Vector3d &p); // Point collision
108  int CheckOccupancy(const octomap::point3d &p1,
109  const octomap::point3d &p2); // line collision
110  int CheckOccupancy(const Eigen::Vector3d &p1,
111  const Eigen::Vector3d &p2); // line collision
112 
113  // checkCollision functions: Returns 0 if its free and 1 if its occupied or unknown
114  bool CheckCollision(const octomap::point3d &p); // Point collision
115  bool CheckCollision(const Eigen::Vector3d &p); // Point collision
116  bool CheckCollision(const octomap::point3d &p1,
117  const octomap::point3d &p2); // line collision
118  bool CheckCollision(const Eigen::Vector3d &p1,
119  const Eigen::Vector3d &p2); // line collision
120 
121  // Calculate the volume of free nodes in the bounding box
122  void BBXFreeVolume(const Eigen::Vector3d &box_min,
123  const Eigen::Vector3d &box_max,
124  double *volume);
125  // Calculate the volume of obstacles in the bounding box
126  void BBXOccVolume(const Eigen::Vector3d &box_min,
127  const Eigen::Vector3d &box_max,
128  double *volume);
129 
130  // Return all free nodes within a bounding box
131  void BBXFreeNodes(const Eigen::Vector3d &box_min,
132  const Eigen::Vector3d &box_max,
133  std::vector<octomap::OcTreeKey> *node_keys,
134  std::vector<double> *node_sizes);
135  // Return all free nodes within a bounding box and assign them indexes
136  void BBXFreeNodes(const Eigen::Vector3d &box_min,
137  const Eigen::Vector3d &box_max,
138  IndexedKeySet *indexed_node_keys,
139  std::vector<double> *node_sizes);
140 
141  // Find all immediate neighbors of a node
142  void GetNodeNeighbors(const octomap::OcTreeKey &node_key,
143  const double &node_size,
144  std::vector<octomap::OcTreeKey> *neighbor_keys); // Return all neighbors for a given node
145  double GetNodeSize(const octomap::OcTreeKey &key); // Returns size of node. Returns zero if node doesn't exist
146  void PrintQueryInfo(octomap::point3d query,
147  octomap::OcTreeNode* node);
148 
149  private:
150  int tree_depth_;
151  double resolution_;
152  double max_range_, min_range_;
153  float inflate_radius_;
154  std::vector<Eigen::Vector3d> sphere_; // Discretized sphere used in map inflation
155  std::vector<double> depth_volumes_; // Volume per depth in the tree
156 
157  // Methods
158  double VectorNormSquared(const double &x,
159  const double &y,
160  const double &z);
161  std_msgs::ColorRGBA HeightMapColor(const double &height,
162  const double &alpha);
163 };
164 
165 } // namespace octoclass
166 
167 #endif // MAPPER_OCTOCLASS_H_
octoclass::OctoClass::ComputeUpdate
void ComputeUpdate(const octomap::KeySet &occ_inflated, const octomap::KeySet &occ_slim, const octomap::point3d &origin, const double &maxrange, octomap::KeySet *occ_slim_in_range, octomap::KeySet *free_slim, octomap::KeySet *free_inflated)
Definition: octoclass.cc:260
octoclass::OctoClass::BBXOccVolume
void BBXOccVolume(const Eigen::Vector3d &box_min, const Eigen::Vector3d &box_max, double *volume)
Definition: octoclass.cc:777
octoclass::OctoClass::SetMapInflation
void SetMapInflation(const double inflate_radius)
Definition: octoclass.cc:90
octoclass::OctoClass::CheckCollision
bool CheckCollision(const octomap::point3d &p)
Definition: octoclass.cc:696
algebra_3d::FrustumPlanes
Definition: linear_algebra.h:110
octoclass::OctoClass::GetNodeSize
double GetNodeSize(const octomap::OcTreeKey &key)
Definition: octoclass.cc:886
octoclass::OctoClass::SetOccupancyThreshold
void SetOccupancyThreshold(const double occupancy_threshold)
Definition: octoclass.cc:129
linear_algebra.h
octoclass::OctoClass::PclToRayOctomap
void PclToRayOctomap(const pcl::PointCloud< pcl::PointXYZ > &cloud, const geometry_msgs::TransformStamped &tf_cam2world, const algebra_3d::FrustumPlanes &frustum)
Definition: octoclass.cc:174
octoclass::OctoClass::SetMemoryTime
void SetMemoryTime(const double memory)
Definition: octoclass.cc:38
octoclass::OctoClass::SetClampingThresholds
void SetClampingThresholds(const double clamping_threshold_min, const double clamping_threshold_max)
Definition: octoclass.cc:145
octoclass::OctoClass::ResetMap
void ResetMap()
Definition: octoclass.cc:123
octoclass::OctoClass::CheckOccupancy
int CheckOccupancy(const octomap::point3d &p)
Definition: octoclass.cc:641
octoclass::OctoClass::OctoClass
OctoClass()
Definition: octoclass.cc:36
point_cloud_common::Vector3d
Eigen::Vector3d Vector3d(const PointType &point)
Definition: utilities.h:328
octoclass::OctoClass::GetMapInflation
double GetMapInflation()
Definition: octoclass.cc:113
octoclass::OctoClass::InflatedVisMarkers
void InflatedVisMarkers(visualization_msgs::MarkerArray *obstacles, visualization_msgs::MarkerArray *free, sensor_msgs::PointCloud2 *obstacles_cloud, sensor_msgs::PointCloud2 *free_cloud)
Definition: octoclass.cc:561
octoclass::OctoClass
Definition: octoclass.h:37
octoclass::OctoClass::SetMaxRange
void SetMaxRange(const double max_range)
Definition: octoclass.cc:47
octoclass::OctoClass::GetNodeNeighbors
void GetNodeNeighbors(const octomap::OcTreeKey &node_key, const double &node_size, std::vector< octomap::OcTreeKey > *neighbor_keys)
Definition: octoclass.cc:859
octoclass::KeySet
std::tr1::unordered_set< IndexedOcTreeKey, IndexedOcTreeKey::KeyHash > KeySet
Definition: indexed_octree_key.h:85
octoclass::OctoClass::SetResolution
void SetResolution(const double resolution_in)
Definition: octoclass.cc:57
octoclass::OctoClass::BBXFreeVolume
void BBXFreeVolume(const Eigen::Vector3d &box_min, const Eigen::Vector3d &box_max, double *volume)
Definition: octoclass.cc:743
octoclass::OctoClass::cam_frustum_
algebra_3d::FrustumPlanes cam_frustum_
Definition: octoclass.h:42
octoclass::OctoClass::tree_inflated_
octomap::OcTree tree_inflated_
Definition: octoclass.h:40
octoclass::OctoClass::GetResolution
double GetResolution() const
Definition: octoclass.h:53
octoclass::OctoClass::TreeVisMarkers
void TreeVisMarkers(visualization_msgs::MarkerArray *obstacles, visualization_msgs::MarkerArray *free, sensor_msgs::PointCloud2 *obstacles_cloud, sensor_msgs::PointCloud2 *free_cloud)
Definition: octoclass.cc:479
octoclass::OctoClass::GetMemoryTime
double GetMemoryTime()
Definition: octoclass.cc:43
octoclass::OctoClass::SetCamFrustum
void SetCamFrustum(const double fov, const double aspect_ratio)
Definition: octoclass.cc:117
octoclass
Definition: indexed_octree_key.h:26
octoclass::OctoClass::PrintQueryInfo
void PrintQueryInfo(octomap::point3d query, octomap::OcTreeNode *node)
Definition: octoclass.cc:902
octoclass::OctoClass::FadeMemory
void FadeMemory(const double &rate)
Definition: octoclass.cc:299
indexed_octree_key.h
octoclass::OctoClass::FindCollidingNodesInflated
void FindCollidingNodesInflated(const pcl::PointCloud< pcl::PointXYZ > &point_cloud, std::vector< octomap::point3d > *colliding_nodes)
Definition: octoclass.cc:427
octoclass::IndexedKeySet
Definition: indexed_octree_key.h:89
octoclass::OctoClass::BBXFreeNodes
void BBXFreeNodes(const Eigen::Vector3d &box_min, const Eigen::Vector3d &box_max, std::vector< octomap::OcTreeKey > *node_keys, std::vector< double > *node_sizes)
Definition: octoclass.cc:812
octoclass::OctoClass::SetMinRange
void SetMinRange(const double min_range)
Definition: octoclass.cc:52
octoclass::OctoClass::tree_
octomap::OcTree tree_
Definition: octoclass.h:39
octoclass::OctoClass::SetHitMissProbabilities
void SetHitMissProbabilities(const double probability_hit, const double probability_miss)
Definition: octoclass.cc:135
octoclass::OctoClass::memory_time_
double memory_time_
Definition: octoclass.h:41