|
| 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/color.h> |
| 38 | +#include <spark_dsg/dynamic_scene_graph.h> |
| 39 | + |
| 40 | +#include <memory> |
| 41 | + |
| 42 | +#include "hydra_visualizer/color/colormap_utilities.h" |
| 43 | + |
| 44 | +namespace hydra { |
| 45 | + |
| 46 | +#define REGISTER_COLOR_ADAPTER(adapter) \ |
| 47 | + inline static const auto registration_ = \ |
| 48 | + config::RegistrationWithConfig<EdgeColorAdapter, adapter, Config>(#adapter) |
| 49 | + |
| 50 | +struct EdgeColorAdapter { |
| 51 | + using Ptr = std::shared_ptr<EdgeColorAdapter>; |
| 52 | + using EdgeColor = std::pair<spark_dsg::Color, spark_dsg::Color>; |
| 53 | + |
| 54 | + virtual ~EdgeColorAdapter() = default; |
| 55 | + |
| 56 | + /** |
| 57 | + * @brief Get color for an edge. |
| 58 | + * @param graph Current scene graph node is from |
| 59 | + * @param edge Edge to get color for |
| 60 | + * @returns Visualizer color for source and target ends of the edge. |
| 61 | + */ |
| 62 | + virtual EdgeColor getColor(const spark_dsg::DynamicSceneGraph& graph, |
| 63 | + const spark_dsg::SceneGraphEdge& edge) const = 0; |
| 64 | + |
| 65 | + /** |
| 66 | + * @brief Set any pre-draw information |
| 67 | + * @param graph Graph to get information for |
| 68 | + * |
| 69 | + * Allows color adapters to gather statistics about the scene graph before generating |
| 70 | + * any edge colors when drawing the scene graph |
| 71 | + */ |
| 72 | + virtual void setGraph(const spark_dsg::DynamicSceneGraph& /* graph */, |
| 73 | + spark_dsg::LayerId /* layer */) {} |
| 74 | +}; |
| 75 | + |
| 76 | +struct UniformEdgeColorAdapter : EdgeColorAdapter { |
| 77 | + struct Config { |
| 78 | + // TODO(lschmid): Consider using the named colors, or even better integrate |
| 79 | + // optionally named colors directly into the config utilities parsing. |
| 80 | + spark_dsg::Color color; |
| 81 | + } const config; |
| 82 | + |
| 83 | + explicit UniformEdgeColorAdapter(const Config& config); |
| 84 | + EdgeColor getColor(const spark_dsg::DynamicSceneGraph& graph, |
| 85 | + const spark_dsg::SceneGraphEdge& edge) const override; |
| 86 | + |
| 87 | + private: |
| 88 | + REGISTER_COLOR_ADAPTER(UniformEdgeColorAdapter); |
| 89 | +}; |
| 90 | + |
| 91 | +void declare_config(UniformEdgeColorAdapter::Config& config); |
| 92 | + |
| 93 | +struct EdgeValueFunctor { |
| 94 | + virtual ~EdgeValueFunctor() = default; |
| 95 | + virtual double eval(const spark_dsg::DynamicSceneGraph& graph, |
| 96 | + const spark_dsg::SceneGraphEdge& edge) const = 0; |
| 97 | +}; |
| 98 | + |
| 99 | +struct EdgeWeightFunctor : EdgeValueFunctor { |
| 100 | + double eval(const spark_dsg::DynamicSceneGraph& graph, |
| 101 | + const spark_dsg::SceneGraphEdge& edge) const override; |
| 102 | + |
| 103 | + inline static const auto registration = |
| 104 | + config::Registration<EdgeValueFunctor, EdgeWeightFunctor>("weight"); |
| 105 | +}; |
| 106 | + |
| 107 | +struct ValueEdgeColorAdapter : EdgeColorAdapter { |
| 108 | + struct Config { |
| 109 | + visualizer::RangeColormap::Config colormap; |
| 110 | + std::string value_functor{"weight"}; |
| 111 | + } const config; |
| 112 | + |
| 113 | + explicit ValueEdgeColorAdapter(const Config& config); |
| 114 | + void setGraph(const spark_dsg::DynamicSceneGraph& graph, |
| 115 | + spark_dsg::LayerId layer) override; |
| 116 | + EdgeColor getColor(const spark_dsg::DynamicSceneGraph& graph, |
| 117 | + const spark_dsg::SceneGraphEdge& edge) const override; |
| 118 | + |
| 119 | + private: |
| 120 | + double min_value_; |
| 121 | + double max_value_; |
| 122 | + std::unique_ptr<EdgeValueFunctor> functor_; |
| 123 | + const visualizer::RangeColormap colormap_; |
| 124 | + REGISTER_COLOR_ADAPTER(ValueEdgeColorAdapter); |
| 125 | +}; |
| 126 | + |
| 127 | +void declare_config(ValueEdgeColorAdapter::Config& config); |
| 128 | + |
| 129 | +#undef REGISTER_COLOR_ADAPTER |
| 130 | + |
| 131 | +} // namespace hydra |
0 commit comments