|
| 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 | +#include <config_utilities/factory.h> |
| 37 | +#include <spark_dsg/traversability_boundary.h> |
| 38 | + |
| 39 | +#include "hydra/backend/deformation_interpolator.h" |
| 40 | +#include "hydra/backend/update_functions.h" |
| 41 | +#include "hydra/utils/active_window_tracker.h" |
| 42 | +#include "hydra/utils/nearest_neighbor_utilities.h" |
| 43 | + |
| 44 | +namespace hydra { |
| 45 | + |
| 46 | +/** |
| 47 | + * @brief Functor to update traversability places in the DSG. This functor should be |
| 48 | + * called with exhaustive merging enabled. |
| 49 | + */ |
| 50 | +struct UpdateTraversabilityFunctor : public UpdateFunctor { |
| 51 | + struct Config { |
| 52 | + //! Layer to update traversability in |
| 53 | + std::string layer = DsgLayers::TRAVERSABILITY; |
| 54 | + |
| 55 | + //! Minimum side length of a traversability area to be considered [m]. |
| 56 | + double min_place_size = 0.5; |
| 57 | + |
| 58 | + //! Maximum width of a place created in the active window [m]. |
| 59 | + double max_place_size = 1.0; |
| 60 | + |
| 61 | + //! Extra tolerance for connectivity distance and overlap checks for merging in |
| 62 | + //! meters. |
| 63 | + double tolerance = 0.1; |
| 64 | + |
| 65 | + //! If false, compute the topoligcal distance to occupied space. If true, use metric |
| 66 | + //! distance. |
| 67 | + bool use_metric_distance = false; |
| 68 | + |
| 69 | + DeformationInterpolator::Config deformation; |
| 70 | + } const config; |
| 71 | + |
| 72 | + using EdgeSet = std::set<EdgeKey>; |
| 73 | + using NodeSet = std::set<NodeId>; |
| 74 | + using State = spark_dsg::TraversabilityState; |
| 75 | + |
| 76 | + explicit UpdateTraversabilityFunctor(const Config& config); |
| 77 | + |
| 78 | + Hooks hooks() const override; |
| 79 | + |
| 80 | + void call(const DynamicSceneGraph& unmerged, |
| 81 | + SharedDsgInfo& dsg, |
| 82 | + const UpdateInfo::ConstPtr& info) const override; |
| 83 | + |
| 84 | + protected: |
| 85 | + // Hook callbacks. |
| 86 | + MergeList findNodeMerges(const DynamicSceneGraph& dsg, |
| 87 | + const UpdateInfo::ConstPtr& info) const; |
| 88 | + |
| 89 | + NodeAttributes::Ptr mergeNodes(const DynamicSceneGraph& dsg, |
| 90 | + const std::vector<NodeId>& merge_ids) const; |
| 91 | + |
| 92 | + void cleanup(const UpdateInfo::ConstPtr& /* info */, SharedDsgInfo* dsg) const; |
| 93 | + |
| 94 | + // Processing Steps. |
| 95 | + /** |
| 96 | + * @brief Update the positions of all traversability nodes in the DSG. Propagates to |
| 97 | + * the complete DSG in case of new loop closures. |
| 98 | + */ |
| 99 | + void updateDeformation(const DynamicSceneGraph& unmerged, |
| 100 | + SharedDsgInfo& dsg, |
| 101 | + const UpdateInfo::ConstPtr& info) const; |
| 102 | + |
| 103 | + EdgeSet findActiveWindowEdges(DynamicSceneGraph& dsg) const; |
| 104 | + |
| 105 | + void pruneActiveWindowEdges(DynamicSceneGraph& dsg, |
| 106 | + const EdgeSet& active_edges) const; |
| 107 | + |
| 108 | + void updateDistances(const SceneGraphLayer& layer) const; |
| 109 | + |
| 110 | + // Helper functions. |
| 111 | + |
| 112 | + /** |
| 113 | + * @brief Find connections of places that should be considered for merging or |
| 114 | + * connection in the active window. |
| 115 | + */ |
| 116 | + std::vector<NodeId> findConnections( |
| 117 | + const DynamicSceneGraph& dsg, |
| 118 | + const TraversabilityNodeAttributes& from_attrs) const; |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Check whether the two traversability areas overlap sufficiently to be |
| 122 | + * considered traversable in-place. |
| 123 | + */ |
| 124 | + bool hasTraversableOverlap(const TraversabilityNodeAttributes& from, |
| 125 | + const TraversabilityNodeAttributes& to) const; |
| 126 | + |
| 127 | + /** |
| 128 | + * @brief Check whether the from boundary is contained in the in boundary. |
| 129 | + */ |
| 130 | + bool isContained(const Boundary& from, const Boundary& in) const; |
| 131 | + |
| 132 | + /** |
| 133 | + * @brief Recursively compute the metrc distance to the nearest intraversable |
| 134 | + * obstacle for the query place. |
| 135 | + */ |
| 136 | + double computeMetricDistance(const SceneGraphLayer& layer, |
| 137 | + const Eigen::Vector2d& point, |
| 138 | + const NodeSet& to_visit, |
| 139 | + NodeSet& visited) const; |
| 140 | + |
| 141 | + double distanceToIntraversable(const TraversabilityNodeAttributes& attrs, |
| 142 | + const Eigen::Vector2d& point) const; |
| 143 | + |
| 144 | + void computeTopologicalDistances(const SceneGraphLayer& layer) const; |
| 145 | + |
| 146 | + void resetNeighborFinder(const DynamicSceneGraph& dsg) const; |
| 147 | + |
| 148 | + protected: |
| 149 | + // Cached constants. |
| 150 | + const double radius_; |
| 151 | + const double min_connectivity_; |
| 152 | + |
| 153 | + // State. |
| 154 | + mutable EdgeSet previous_active_edges_; |
| 155 | + mutable std::set<EdgeKey> overlapping_nodes_to_cleanup_; |
| 156 | + |
| 157 | + // Members. |
| 158 | + mutable ActiveWindowTracker active_tracker_; |
| 159 | + const DeformationInterpolator deformation_interpolator_; |
| 160 | + mutable NearestNodeFinder::Ptr nn_; |
| 161 | +}; |
| 162 | + |
| 163 | +void declare_config(UpdateTraversabilityFunctor::Config& config); |
| 164 | + |
| 165 | +} // namespace hydra |
0 commit comments