|
| 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 | +#include "hydra_ros/frontend/traversability_visualizer.h" |
| 36 | + |
| 37 | +#include <config_utilities/config.h> |
| 38 | +#include <config_utilities/printing.h> |
| 39 | +#include <config_utilities/validation.h> |
| 40 | +#include <hydra/common/global_info.h> |
| 41 | +#include <hydra_visualizer/color/color_parsing.h> |
| 42 | +#include <hydra_visualizer/color/colormap_utilities.h> |
| 43 | +#include <spark_dsg/colormaps.h> |
| 44 | + |
| 45 | +#include "hydra_ros/frontend/gvd_visualization_utilities.h" |
| 46 | +#include "hydra_ros/visualizer/voxel_drawing.h" |
| 47 | + |
| 48 | +namespace hydra { |
| 49 | + |
| 50 | +using visualization_msgs::msg::Marker; |
| 51 | +using visualization_msgs::msg::MarkerArray; |
| 52 | +using visualizer::ContinuousPalette; |
| 53 | +using visualizer::DivergentPalette; |
| 54 | +using visualizer::RangeColormap; |
| 55 | + |
| 56 | +void declare_config(TraversabilityVisualizer::Config& config) { |
| 57 | + using namespace config; |
| 58 | + name("TraversabilityVisualizer::Config"); |
| 59 | + field(config.ns, "ns"); |
| 60 | + field(config.traversability_colormap, "traversability_colormap"); |
| 61 | + field(config.confidence_colormap, "confidence_colormap"); |
| 62 | + field(config.state_colors, "state_colors"); |
| 63 | + field(config.drawing_offset_z, "drawing_offset_z", "m"); |
| 64 | + field(config.use_relative_offset, "use_relative_offset"); |
| 65 | + check(config.state_colors.size(), EQ, 4, "state_colors"); |
| 66 | +} |
| 67 | + |
| 68 | +TraversabilityVisualizer::TraversabilityVisualizer(const Config& config) |
| 69 | + : config_("traversability_visualizer", |
| 70 | + config::checkValid(config), |
| 71 | + [this]() { onConfigUpdate(); }), |
| 72 | + nh_(ianvs::NodeHandle::this_node(config.ns)), |
| 73 | + layer_pub_(nh_.create_publisher<visualization_msgs::msg::Marker>("layer", 10)) { |
| 74 | + onConfigUpdate(); |
| 75 | +} |
| 76 | + |
| 77 | +visualizer::RangeColormap::Config TraversabilityVisualizer::createSpectrumColormap( |
| 78 | + std::vector<spark_dsg::Color> colors) { |
| 79 | + visualizer::RangeColormap::Config config; |
| 80 | + config.palette = visualizer::SpectrumPalette::Config{std::move(colors)}; |
| 81 | + return config; |
| 82 | +} |
| 83 | + |
| 84 | +std::string TraversabilityVisualizer::printInfo() const { |
| 85 | + return config::toString(active_config_); |
| 86 | +} |
| 87 | + |
| 88 | +void TraversabilityVisualizer::onConfigUpdate() { |
| 89 | + std::lock_guard<std::mutex> lock(config_mutex_); |
| 90 | + active_config_ = config_.get(); |
| 91 | + traversability_colormap_ = std::make_unique<visualizer::RangeColormap>( |
| 92 | + active_config_.traversability_colormap); |
| 93 | + confidence_colormap_ = |
| 94 | + std::make_unique<visualizer::RangeColormap>(active_config_.confidence_colormap); |
| 95 | +} |
| 96 | + |
| 97 | +void TraversabilityVisualizer::call(uint64_t timestamp_ns, |
| 98 | + const Eigen::Vector3d& world_t_body, |
| 99 | + const places::TraversabilityLayer& layer) const { |
| 100 | + std_msgs::msg::Header header; |
| 101 | + header.frame_id = GlobalInfo::instance().getFrames().map; |
| 102 | + header.stamp = rclcpp::Time(timestamp_ns); |
| 103 | + std::lock_guard<std::mutex> lock(config_mutex_); |
| 104 | + visualizeLayer(header, world_t_body, layer); |
| 105 | +} |
| 106 | + |
| 107 | +void TraversabilityVisualizer::visualizeLayer( |
| 108 | + const std_msgs::msg::Header& header, |
| 109 | + const Eigen::Vector3d& world_t_body, |
| 110 | + const places::TraversabilityLayer& layer) const { |
| 111 | + visualization_msgs::msg::Marker msg; |
| 112 | + msg.header = header; |
| 113 | + msg.action = visualization_msgs::msg::Marker::ADD; |
| 114 | + msg.id = 0; |
| 115 | + msg.ns = "traversability"; |
| 116 | + msg.type = visualization_msgs::msg::Marker::CUBE_LIST; |
| 117 | + msg.scale.x = layer.voxel_size; |
| 118 | + msg.scale.y = layer.voxel_size; |
| 119 | + msg.scale.z = layer.voxel_size; |
| 120 | + msg.pose.orientation.w = 1.0; |
| 121 | + |
| 122 | + visualization_msgs::msg::Marker msg2 = msg; |
| 123 | + msg2.ns = "confidence"; |
| 124 | + |
| 125 | + visualization_msgs::msg::Marker msg3 = msg; |
| 126 | + msg3.ns = "state"; |
| 127 | + |
| 128 | + visualization_msgs::msg::Marker msg4 = msg; |
| 129 | + msg4.ns = "debug"; |
| 130 | + |
| 131 | + auto height = active_config_.drawing_offset_z; |
| 132 | + if (active_config_.use_relative_offset) { |
| 133 | + height += world_t_body.z(); |
| 134 | + } |
| 135 | + geometry_msgs::msg::Point pos; |
| 136 | + pos.z = height; |
| 137 | + |
| 138 | + for (const auto& block : layer) { |
| 139 | + for (size_t x = 0; x < block.voxels_per_side; ++x) { |
| 140 | + pos.x = block.origin().x() + (x + 0.5f) * layer.voxel_size; |
| 141 | + for (size_t y = 0; y < block.voxels_per_side; ++y) { |
| 142 | + const auto& voxel = block.voxel(x, y); |
| 143 | + if (voxel.confidence <= 0.0f) { |
| 144 | + continue; // Unobserved voxels. |
| 145 | + } |
| 146 | + pos.y = block.origin().y() + (y + 0.5f) * layer.voxel_size; |
| 147 | + msg.points.push_back(pos); |
| 148 | + msg.colors.push_back(visualizer::makeColorMsg( |
| 149 | + traversability_colormap_->getColor(voxel.traversability), |
| 150 | + active_config_.alpha)); |
| 151 | + msg2.colors.push_back(visualizer::makeColorMsg( |
| 152 | + confidence_colormap_->getColor(voxel.confidence), active_config_.alpha)); |
| 153 | + msg3.colors.push_back(visualizer::makeColorMsg( |
| 154 | + active_config_.state_colors[static_cast<uint8_t>(voxel.state)], |
| 155 | + active_config_.alpha)); |
| 156 | + msg4.colors.push_back(visualizer::makeColorMsg(debugColor(voxel.debug_value), |
| 157 | + active_config_.alpha)); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + msg2.points = msg.points; |
| 162 | + msg3.points = msg.points; |
| 163 | + msg4.points = msg.points; |
| 164 | + |
| 165 | + layer_pub_->publish(msg); |
| 166 | + layer_pub_->publish(msg2); |
| 167 | + layer_pub_->publish(msg3); |
| 168 | + layer_pub_->publish(msg4); |
| 169 | +} |
| 170 | + |
| 171 | +Color TraversabilityVisualizer::debugColor(float value) const { |
| 172 | + // Connected components. |
| 173 | + if (value < 0.0f) { |
| 174 | + return Color::black(); |
| 175 | + } |
| 176 | + return spark_dsg::colormaps::rainbowId(static_cast<int>(value), 5); |
| 177 | +} |
| 178 | + |
| 179 | +} // namespace hydra |
0 commit comments