spectrum_display.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2020, Locus Robotics
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
36 #include <color_util/blend.h>
37 #include <color_util/convert.h>
38 #include <algorithm>
39 #include <vector>
40 #include <OgreMatrix4.h>
41 
42 namespace robot_nav_rviz_plugins
43 {
45 {
46  spectrum_property_ = new rviz::EnumProperty("Spectrum Style", "RGB",
47  "The rendering operation to use to draw the grid lines.", this, SLOT(updateColors()));
48  spectrum_property_->addOption("RGB", static_cast<int>(SpectrumStyle::rgb));
49  spectrum_property_->addOption("HSV", static_cast<int>(SpectrumStyle::hsv));
50  spectrum_property_->addOption("HSV+", static_cast<int>(SpectrumStyle::short_hsv));
51 
52  color_a_property_ = new rviz::ColorProperty("Color A", QColor(239, 41, 41), "Color A", this, SLOT(updateColors()));
53  alpha_a_property_ = new rviz::FloatProperty("Alpha A", 1.0, "Alpha A", this, SLOT(updateColors()));
56  color_b_property_ = new rviz::ColorProperty("Color B", QColor(41, 0, 226), "Color B", this, SLOT(updateColors()));
57  alpha_b_property_ = new rviz::FloatProperty("Alpha B", 1.0, "Alpha B", this, SLOT(updateColors()));
60 
61  size_property_ = new rviz::IntProperty("Spectrum Size", 10, "Number of colors to display.", this, SLOT(updateSize()));
63 }
64 
66 {
67  Display::onInitialize();
68  updateSize();
69 }
70 
72 {
73  Display::reset();
74  updateSize();
75 }
76 
78 {
79  Ogre::ColourValue ogre_a = color_a_property_->getOgreColor();
80  Ogre::ColourValue ogre_b = color_b_property_->getOgreColor();
81  color_util::ColorRGBA c_a(ogre_a.r, ogre_a.g, ogre_a.b, alpha_a_property_->getFloat());
82  color_util::ColorRGBA c_b(ogre_b.r, ogre_b.g, ogre_b.b, alpha_b_property_->getFloat());
83 
84  unsigned int n = arrows_.size();
86  color_util::ColorHSVA h_a, h_b;
87 
88  if (style != SpectrumStyle::rgb)
89  {
92  }
93 
94  for (unsigned int i = 0; i < n; ++i)
95  {
96  auto& arrow = arrows_[i];
97  double v = static_cast<double>(i) / (n - 1);
98  color_util::ColorRGBA spec_color;
99  if (style == SpectrumStyle::rgb)
100  {
101  spec_color = color_util::rgbaBlend(c_a, c_b, v);
102  }
103  else
104  {
105  color_util::ColorHSVA spec_hsv;
106  if (style == SpectrumStyle::hsv)
107  spec_hsv = color_util::hueBlend(h_a, h_b, v);
108  else
109  spec_hsv = color_util::hueBlendPlus(h_a, h_b, v);
110  spec_color = color_util::changeColorspace(spec_hsv);
111  }
112  arrow->setColor(spec_color.r, spec_color.g, spec_color.b, spec_color.a);
113  }
115 }
116 
118 {
119  // Read options
120  unsigned int size = static_cast<unsigned int>(size_property_->getInt());
121  if (size > arrows_.size())
122  {
123  for (size_t i = arrows_.size(); i < size; i++)
124  {
126  arrows_.push_back(arrow);
127  }
128  }
129  else if (size < arrows_.size())
130  {
131  int size_signed = static_cast<int>(size);
132  for (int i = arrows_.size() - 1; size_signed <= i; i--)
133  {
134  delete arrows_[i];
135  }
136  arrows_.resize(size);
137  }
138 
139  for (unsigned int i = 0; i < size; ++i)
140  {
141  auto& arrow = arrows_[i];
142  // shaft_length, shaft_diameter, head_length, head_diameter
143  arrow->set(1.0, 1.0 / size, 0.0, 1.0 / size);
144  arrow->setPosition(Ogre::Vector3(0.0, static_cast<double>(i) / (size - 1), 0.0));
145  arrow->setDirection(Ogre::Vector3(1, 0, 0));
146  }
147  updateColors();
148 }
149 
150 } // namespace robot_nav_rviz_plugins
151 
rviz::IntProperty::setMin
void setMin(int min)
robot_nav_rviz_plugins::SpectrumDisplay::updateSize
void updateSize()
Definition: spectrum_display.cpp:117
rviz::DisplayContext::queueRender
virtual void queueRender()=0
robot_nav_rviz_plugins
rviz::Arrow
robot_nav_rviz_plugins::SpectrumDisplay::SpectrumDisplay
SpectrumDisplay()
Definition: spectrum_display.cpp:44
robot_nav_rviz_plugins::SpectrumDisplay::alpha_b_property_
rviz::FloatProperty * alpha_b_property_
Definition: spectrum_display.h:73
color_util::hueBlend
hsva hueBlend(const hsva &color_a, const hsva &color_b, double ratio)
rviz::FloatProperty::setMax
void setMax(float max)
robot_nav_rviz_plugins::SpectrumDisplay::SpectrumStyle::short_hsv
@ short_hsv
GenericColorRGBA< double >::r
double r
rviz::FloatProperty::setMin
void setMin(float min)
rviz::ColorProperty
color_util::rgbaBlend
rgba rgbaBlend(const rgba &color_a, const rgba &color_b, double ratio)
rviz::Display
rviz::EnumProperty
rviz::FloatProperty
robot_nav_rviz_plugins::SpectrumDisplay::SpectrumStyle::rgb
@ rgb
robot_nav_rviz_plugins::SpectrumDisplay::color_b_property_
rviz::ColorProperty * color_b_property_
Definition: spectrum_display.h:72
robot_nav_rviz_plugins::SpectrumDisplay::arrows_
std::vector< rviz::Arrow * > arrows_
Definition: spectrum_display.h:67
PLUGINLIB_EXPORT_CLASS
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
rviz::FloatProperty::getFloat
virtual float getFloat() const
rviz::EnumProperty::addOption
virtual void addOption(const QString &option, int value=0)
robot_nav_rviz_plugins::SpectrumDisplay::spectrum_property_
rviz::EnumProperty * spectrum_property_
Definition: spectrum_display.h:69
robot_nav_rviz_plugins::SpectrumDisplay::SpectrumStyle
SpectrumStyle
Definition: spectrum_display.h:64
rviz::Display::scene_node_
Ogre::SceneNode * scene_node_
robot_nav_rviz_plugins::SpectrumDisplay::alpha_a_property_
rviz::FloatProperty * alpha_a_property_
Definition: spectrum_display.h:71
robot_nav_rviz_plugins::SpectrumDisplay::getSpectrumStyle
SpectrumStyle getSpectrumStyle() const
Definition: spectrum_display.h:65
rviz::Display::scene_manager_
Ogre::SceneManager * scene_manager_
robot_nav_rviz_plugins::SpectrumDisplay::reset
void reset() override
Definition: spectrum_display.cpp:71
robot_nav_rviz_plugins::SpectrumDisplay::color_a_property_
rviz::ColorProperty * color_a_property_
Definition: spectrum_display.h:70
color_util::ColorRGBA
GenericColorRGBA< double >::b
double b
robot_nav_rviz_plugins::SpectrumDisplay::updateColors
void updateColors()
Definition: spectrum_display.cpp:77
rviz::Display::context_
DisplayContext * context_
robot_nav_rviz_plugins::SpectrumDisplay::SpectrumStyle::hsv
@ hsv
color_util::ColorHSVA
robot_nav_rviz_plugins::SpectrumDisplay
Definition: spectrum_display.h:49
color_util::hueBlendPlus
color_util::ColorHSVA hueBlendPlus(const color_util::ColorHSVA &color_a, const color_util::ColorHSVA &color_b, double ratio)
class_list_macros.hpp
GenericColorRGBA< double >::g
double g
rviz::IntProperty::getInt
virtual int getInt() const
blend.h
convert.h
color_util::changeColorspace
color_util::ColorRGBA changeColorspace(const color_util::ColorHSVA &hsva)
GenericColorRGBA< double >::a
double a
robot_nav_rviz_plugins::SpectrumDisplay::onInitialize
void onInitialize() override
Definition: spectrum_display.cpp:65
robot_nav_rviz_plugins::SpectrumDisplay::size_property_
rviz::IntProperty * size_property_
Definition: spectrum_display.h:74
spectrum_display.h
rviz::ColorProperty::getOgreColor
Ogre::ColourValue getOgreColor() const
rviz::IntProperty


robot_nav_viz_demos
Author(s):
autogenerated on Sun May 18 2025 02:47:54