|
| 1 | +/* ----------------------------------------------------------------------------- |
| 2 | + * Copyright 2022 Massachusetts Institute of Technology. |
| 3 | + * All Rights Reserved |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | + * this list of conditions and the following disclaimer. |
| 10 | + * |
| 11 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer in the documentation |
| 13 | + * and/or other materials provided with the distribution. |
| 14 | + * |
| 15 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 19 | + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 20 | + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 21 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 22 | + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 23 | + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | + * |
| 26 | + * Research was sponsored by the United States Air Force Research Laboratory and |
| 27 | + * the United States Air Force Artificial Intelligence Accelerator and was |
| 28 | + * accomplished under Cooperative Agreement Number FA8750-19-2-1000. The views |
| 29 | + * and conclusions contained in this document are those of the authors and should |
| 30 | + * not be interpreted as representing the official policies, either expressed or |
| 31 | + * implied, of the United States Air Force or the U.S. Government. The U.S. |
| 32 | + * Government is authorized to reproduce and distribute reprints for Government |
| 33 | + * purposes notwithstanding any copyright notation herein. |
| 34 | + * -------------------------------------------------------------------------- */ |
| 35 | +#pragma once |
| 36 | + |
| 37 | +#include <config_utilities/factory.h> |
| 38 | +#include <spark_dsg/traversability_boundary.h> |
| 39 | + |
| 40 | +#include <map> |
| 41 | +#include <optional> |
| 42 | +#include <utility> |
| 43 | +#include <vector> |
| 44 | + |
| 45 | +#include "hydra/places/traversability_clustering.h" |
| 46 | + |
| 47 | +namespace hydra::places { |
| 48 | + |
| 49 | +/** |
| 50 | + * @brief Simple clustering that assigns places by block. |
| 51 | + */ |
| 52 | +class RegionGrowingTraversabilityClustering : public TraversabilityClustering { |
| 53 | + public: |
| 54 | + struct Config { |
| 55 | + //! Maximum radius of a place [m]. |
| 56 | + float max_radius = 3.0f; |
| 57 | + |
| 58 | + //! Number of rays to consider for boundary computation. |
| 59 | + int num_orientation_bins = 16; |
| 60 | + } const config; |
| 61 | + |
| 62 | + using Voxels = VoxelIndices; |
| 63 | + using VoxelSet = VoxelIndexSet; |
| 64 | + using VoxelMap = VoxelIndexMap<spark_dsg::NodeId>; |
| 65 | + |
| 66 | + struct Region { |
| 67 | + // ID of the region. This is also the node ID in the DSG. |
| 68 | + spark_dsg::NodeId id; |
| 69 | + |
| 70 | + // VoxelSet assigned to this region. These can be in the current layer or outside. |
| 71 | + VoxelSet voxels; |
| 72 | + |
| 73 | + // Voxels bordering the voxels of this region. These are the exterior boundary of |
| 74 | + // the region and are ordered by traversal. |
| 75 | + VoxelSet exterior_boundary; |
| 76 | + |
| 77 | + // Unobserved or intraversable voxels inside the region. |
| 78 | + VoxelSet interior_boundary; |
| 79 | + |
| 80 | + // Archived boundary voxels with their last known traversability state. |
| 81 | + VoxelIndexMap<spark_dsg::TraversabilityState> archived_boundary; |
| 82 | + |
| 83 | + // True: this region has voxels in the current layer. False: all voxels are outside |
| 84 | + // the current layer. |
| 85 | + bool is_active = true; |
| 86 | + |
| 87 | + // Centroid of the region. |
| 88 | + Eigen::Vector3f centroid; |
| 89 | + |
| 90 | + // Axis-aligned bounding box of the region in voxel coordinates. |
| 91 | + Eigen::Vector2i min_coordinates; |
| 92 | + Eigen::Vector2i max_coordinates; |
| 93 | + |
| 94 | + // Regions connecting to this one. <region id, num connecting voxels> |
| 95 | + std::map<spark_dsg::NodeId, int> neighbors; |
| 96 | + |
| 97 | + // Merge other into this region. |
| 98 | + void merge(const Region& other); |
| 99 | + |
| 100 | + /** |
| 101 | + * @brief Compute the ordered exterior boundary, min/max extents, and centroid. |
| 102 | + */ |
| 103 | + void computeBoundary(); |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Compute region boundaries, extents, centroids, and compute the neighbors |
| 107 | + * from that. |
| 108 | + */ |
| 109 | + void computeNeighbors(const VoxelMap& assigned_voxels); |
| 110 | + }; |
| 111 | + |
| 112 | + RegionGrowingTraversabilityClustering(const Config& config); |
| 113 | + ~RegionGrowingTraversabilityClustering() = default; |
| 114 | + |
| 115 | + void updateGraph(const TraversabilityLayer& layer, |
| 116 | + const ActiveWindowOutput& msg, |
| 117 | + spark_dsg::DynamicSceneGraph& graph) override; |
| 118 | + |
| 119 | + protected: |
| 120 | + size_t current_id_ = 0; |
| 121 | + uint64_t current_time_ns_ = 0; |
| 122 | + int max_region_size_ = 0; // Cached region size in voxels. |
| 123 | + |
| 124 | + // <id, region> |
| 125 | + std::map<spark_dsg::NodeId, Region> regions_; |
| 126 | + |
| 127 | + // Processing steps. |
| 128 | + /** |
| 129 | + * @brief Prune existing regions by removing all voxels that are no longer |
| 130 | + * traversable. Regions without any remaining voxels are marked as inactive. |
| 131 | + * @return Map of all inactive assigned voxels to region IDs. |
| 132 | + // */ |
| 133 | + VoxelMap initializeRegions(const TraversabilityLayer& layer, |
| 134 | + const VoxelSet& all_voxels); |
| 135 | + |
| 136 | + /** |
| 137 | + * @brief Initialize all connected traversable voxels from the starting position of |
| 138 | + * the robot. |
| 139 | + */ |
| 140 | + VoxelSet initializeVoxels(const TraversabilityLayer& layer, |
| 141 | + const Eigen::Vector3d& start_position) const; |
| 142 | + |
| 143 | + /** |
| 144 | + * @brief Assign all traversable voxels by assigning them to closest existing regions |
| 145 | + * and assigning new ones where needed. |
| 146 | + */ |
| 147 | + void growRegions(VoxelSet& all_voxels, VoxelMap& assigned_voxels); |
| 148 | + |
| 149 | + /** |
| 150 | + * @brief Merge all regions that don't exceed the max size. |
| 151 | + */ |
| 152 | + void mergeRegions(const VoxelMap& assigned_voxels, |
| 153 | + spark_dsg::DynamicSceneGraph& graph); |
| 154 | + |
| 155 | + void updatePlaceNodesInDsg(spark_dsg::DynamicSceneGraph& graph, |
| 156 | + const TraversabilityLayer& layer); |
| 157 | + |
| 158 | + void updatePlaceEdgesInDsg(spark_dsg::DynamicSceneGraph& graph) const; |
| 159 | + |
| 160 | + void visualizeAssignments(const TraversabilityLayer& layer, |
| 161 | + const VoxelMap& assigned_voxels) const; |
| 162 | + |
| 163 | + void pruneRegions(); |
| 164 | + |
| 165 | + /** |
| 166 | + * @brief Allocate a new region, keeping track of the IDs. ID 0 is reserved. |
| 167 | + */ |
| 168 | + Region& allocateNewRegion(); |
| 169 | + |
| 170 | + /** |
| 171 | + * @brief Breadth-first search to grow a region from a seed index. |
| 172 | + */ |
| 173 | + static VoxelSet growRegion( |
| 174 | + const VoxelSet& candidates, |
| 175 | + const VoxelIndex& seed_index, |
| 176 | + std::function<bool(const VoxelIndex&)> condition = [](const VoxelIndex&) { |
| 177 | + return true; |
| 178 | + }); |
| 179 | + |
| 180 | + void updatePlaceNodeAttributes(spark_dsg::TravNodeAttributes& attrs, |
| 181 | + Region& region, |
| 182 | + const TraversabilityLayer& layer) const; |
| 183 | + |
| 184 | + inline static const std::array<VoxelIndex, 8> neighbors_ = { |
| 185 | + VoxelIndex(0, -1, 0), // bottom |
| 186 | + VoxelIndex(-1, 0, 0), // left |
| 187 | + VoxelIndex(0, 1, 0), // top |
| 188 | + VoxelIndex(1, 0, 0), // right |
| 189 | + VoxelIndex(-1, -1, 0), // bottom-left |
| 190 | + VoxelIndex(-1, 1, 0), // top-left |
| 191 | + VoxelIndex(1, 1, 0), // top-right |
| 192 | + VoxelIndex(1, -1, 0) // bottom-right |
| 193 | + }; |
| 194 | +}; |
| 195 | + |
| 196 | +void declare_config(RegionGrowingTraversabilityClustering::Config& config); |
| 197 | + |
| 198 | +} // namespace hydra::places |
0 commit comments