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 <boost/bind.hpp>
37 #include <color_util/blend.h>
38 #include <color_util/convert.h>
39 #include <algorithm>
40 #include <vector>
41 #include <OgreMatrix4.h>
42 
43 namespace robot_nav_rviz_plugins
44 {
46 {
47  spectrum_property_ = new rviz::EnumProperty("Spectrum Style", "RGB",
48  "The rendering operation to use to draw the grid lines.", this, SLOT(updateColors()));
49  spectrum_property_->addOption("RGB", static_cast<int>(SpectrumStyle::rgb));
50  spectrum_property_->addOption("HSV", static_cast<int>(SpectrumStyle::hsv));
51  spectrum_property_->addOption("HSV+", static_cast<int>(SpectrumStyle::short_hsv));
52 
53  color_a_property_ = new rviz::ColorProperty("Color A", QColor(239, 41, 41), "Color A", this, SLOT(updateColors()));
54  alpha_a_property_ = new rviz::FloatProperty("Alpha A", 1.0, "Alpha A", this, SLOT(updateColors()));
57  color_b_property_ = new rviz::ColorProperty("Color B", QColor(41, 0, 226), "Color B", this, SLOT(updateColors()));
58  alpha_b_property_ = new rviz::FloatProperty("Alpha B", 1.0, "Alpha B", this, SLOT(updateColors()));
61 
62  size_property_ = new rviz::IntProperty("Spectrum Size", 10, "Number of colors to display.", this, SLOT(updateSize()));
64 }
65 
67 {
68  Display::onInitialize();
69  updateSize();
70 }
71 
73 {
74  Display::reset();
75  updateSize();
76 }
77 
79 {
80  Ogre::ColourValue ogre_a = color_a_property_->getOgreColor();
81  Ogre::ColourValue ogre_b = color_b_property_->getOgreColor();
82  color_util::ColorRGBA c_a(ogre_a.r, ogre_a.g, ogre_a.b, alpha_a_property_->getFloat());
83  color_util::ColorRGBA c_b(ogre_b.r, ogre_b.g, ogre_b.b, alpha_b_property_->getFloat());
84 
85  unsigned int n = arrows_.size();
87  color_util::ColorHSVA h_a, h_b;
88 
89  if (style != SpectrumStyle::rgb)
90  {
93  }
94 
95  for (unsigned int i = 0; i < n; ++i)
96  {
97  auto& arrow = arrows_[i];
98  double v = static_cast<double>(i) / (n - 1);
99  color_util::ColorRGBA spec_color;
100  if (style == SpectrumStyle::rgb)
101  {
102  spec_color = color_util::rgbaBlend(c_a, c_b, v);
103  }
104  else
105  {
106  color_util::ColorHSVA spec_hsv;
107  if (style == SpectrumStyle::hsv)
108  spec_hsv = color_util::hueBlend(h_a, h_b, v);
109  else
110  spec_hsv = color_util::hueBlendPlus(h_a, h_b, v);
111  spec_color = color_util::changeColorspace(spec_hsv);
112  }
113  arrow->setColor(spec_color.r, spec_color.g, spec_color.b, spec_color.a);
114  }
116 }
117 
119 {
120  // Read options
121  unsigned int size = static_cast<unsigned int>(size_property_->getInt());
122  if (size > arrows_.size())
123  {
124  for (size_t i = arrows_.size(); i < size; i++)
125  {
127  arrows_.push_back(arrow);
128  }
129  }
130  else if (size < arrows_.size())
131  {
132  int size_signed = static_cast<int>(size);
133  for (int i = arrows_.size() - 1; size_signed <= i; i--)
134  {
135  delete arrows_[i];
136  }
137  arrows_.resize(size);
138  }
139 
140  for (unsigned int i = 0; i < size; ++i)
141  {
142  auto& arrow = arrows_[i];
143  // shaft_length, shaft_diameter, head_length, head_diameter
144  arrow->set(1.0, 1.0 / size, 0.0, 1.0 / size);
145  arrow->setPosition(Ogre::Vector3(0.0, static_cast<double>(i) / (size - 1), 0.0));
146  arrow->setDirection(Ogre::Vector3(1, 0, 0));
147  }
148  updateColors();
149 }
150 
151 } // namespace robot_nav_rviz_plugins
152 
void setMin(float min)
void setMax(float max)
color_util::ColorHSVA changeColorspace(const color_util::ColorRGBA &rgba)
Ogre::ColourValue getOgreColor() const
hsva hueBlend(const hsva &color_a, const hsva &color_b, double ratio)
DisplayContext * context_
virtual int getInt() const
virtual float getFloat() const
void setMin(int min)
color_util::ColorHSVA hueBlendPlus(const color_util::ColorHSVA &color_a, const color_util::ColorHSVA &color_b, double ratio)
Ogre::SceneNode * scene_node_
std::vector< rviz::Arrow * > arrows_
virtual void addOption(const QString &option, int value=0)
Ogre::SceneManager * scene_manager_
virtual void queueRender()=0
rgba rgbaBlend(const rgba &color_a, const rgba &color_b, double ratio)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


robot_nav_viz_demos
Author(s):
autogenerated on Sun Jan 10 2021 04:09:02