1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSGSIM_SCALARBAR
15#define OSGSIM_SCALARBAR 1
17#include <osgSim/Export>
18#include <osgSim/ColorRange> // The default ScalarsToColors is a ColorRange
25A ScalarBar is an osg::Geode to render a colored bar representing a range
26of scalars. The scalar/color ranges are specified by an instance of
27ScalarsToColors. There are a number of configurable properties on the
28ScalarBar, such as the orientation, the number of labels to be displayed
29across the range, the number of distinct colors to use when rendering the
32In summary, the main configurables on the ScalarBar are:
34 -# The range of scalars represented by the bar, and the colors
35 corresponding to this range - these are specified by the
36 ScalarsToColors object.
37 -# The number of colors used when rendering the bar geometry -
38 this may be thought of as the bar 'density'.
39 -# The number of text labels to be used when displaying the bar.
41The other configurables should be self-explanatory.
43class OSGSIM_EXPORT ScalarBar: public osg::Geode
48 /** ScalarBar orientation specification. */
50 HORIZONTAL, ///< a horizontally ascending scalar bar (x-axis)
51 VERTICAL ///< a vertically ascending scalar bar (y-axis)
55 Users may provide their own ScalarPrinter by deriving from this base class and
56 overriding the printScalar() method. Users may map the scalar float passed in
57 to any string they wish.
59 struct OSGSIM_EXPORT ScalarPrinter: public osg::Referenced
61 virtual std::string printScalar(float scalar);
65 TextProperties allows users to specify a number of properties for the
66 text used to display the labels & title on the ScalarBar. Specifying a character
67 size of 0 will cause the ScalarBar to estimate an appropriate size. Note that
68 the attributes are public, and may be set directly.
72 std::string _fontFile;
73 std::pair<int,int> _fontResolution;
78 _fontFile("fonts/arial.ttf"),
79 _fontResolution(40,40),
81 _color(1.0f,1.0f,1.0f,1.0f)
86 /** Default constructor. */
87 ScalarBar(): osg::Geode(),
90 _stc(new ColorRange(0.0f,1.0f)),
92 _position(0.0f,0.0f,0.0f),
95 _orientation(HORIZONTAL),
96 _sp(new ScalarPrinter)
102 Construct a ScalarBar with the supplied parameters.
103 @param numColors Specify the number of colors in the scalar bar. Color
104 interpolation occurs where necessary.
105 @param numLabels Specify the number of labels in the scalar bar.
106 @param stc The ScalarsToColors defining the range of scalars
107 and the colors they map to.
108 @param title The title to be used when displaying the ScalarBar.
109 Specify "" for no title.
110 @param orientation The orientation of the ScalarBar. @see Orientation.
111 @param aspectRatio The aspect ration (y/x) for the displayed bar. Bear in mind you
112 may want to change this if you change the orientation.
113 @param sp A ScalarPrinter object for the ScalarBar. For every displayed
114 ScalarBar label, the scalar value will be passed to the
115 ScalarPrinter object to turn it into a string. Users may
116 override the default ScalarPrinter object to map scalars to
117 whatever strings they wish. @see ScalarPrinter
119 ScalarBar(int numColors, int numLabels, ScalarsToColors* stc,
120 const std::string& title,
121 Orientation orientation = HORIZONTAL,
122 float aspectRatio=0.25,
123 ScalarPrinter* sp=new ScalarPrinter):
125 _numColors(numColors),
126 _numLabels(numLabels),
129 _position(0.0f,0.0f,0.0f),
131 _aspectRatio(aspectRatio),
132 _orientation(orientation),
138 /** Copy constructor */
139 ScalarBar(const ScalarBar& rhs, const osg::CopyOp& co): osg::Geode(rhs,co),
140 _numColors(rhs._numColors),
141 _numLabels(rhs._numLabels),
142 _stc(rhs._stc), // Consider clone for deep copy?
144 _position(rhs._position),
146 _aspectRatio(rhs._aspectRatio),
147 _orientation(rhs._orientation),
148 _sp(rhs._sp), // Consider clone for deep copy?
149 _textProperties(rhs._textProperties)
154 META_Node(osgSim, ScalarBar);
156 /** Set the number of distinct colours on the ScalarBar. */
157 void setNumColors(int numColors);
159 /** Get the number of distinct colours on the ScalarBar. */
160 int getNumColors() const;
162 /** Set the number of labels to display along the ScalarBar. There
163 will be one label at each end point, and evenly distributed labels
165 void setNumLabels(int numLabels);
167 /** Get the number of labels displayed along the ScalarBar. */
168 int getNumLabels() const;
170 /** Set the ScalarsToColors mapping object for the ScalarBar. */
171 void setScalarsToColors(ScalarsToColors* stc);
173 /** Get the ScalarsToColors mapping object from the ScalarBar. */
174 const ScalarsToColors* getScalarsToColors() const;
176 /** Set the title for the ScalarBar, set "" for no title. */
177 void setTitle(const std::string& title);
179 /** Get the title for the ScalarBar. */
180 const std::string& getTitle() const;
183 /** Set the position of scalar bar's lower left corner.*/
184 void setPosition(const osg::Vec3& pos);
186 /** Get the position of scalar bar.*/
187 const osg::Vec3& getPosition() const { return _position; }
189 /** Set the width of the scalar bar.*/
190 void setWidth(float width);
192 /** Get the width of the scalar bar.*/
193 float getWidth() const { return _width; }
195 /** Set the aspect ration (y/x) for the displayed bar. Bear in mind you
196 may want to change this if you change the orientation. */
197 void setAspectRatio(float aspectRatio);
199 /** Get the aspect ration (y/x) for the displayed bar. */
200 float getAspectRatio() const;
203 /** Set the orientation of the ScalarBar. @see Orientation */
204 void setOrientation(ScalarBar::Orientation orientation);
206 /** Get the orientation of the ScalarBar. @see Orientation */
207 ScalarBar::Orientation getOrientation() const;
210 /** Set a ScalarPrinter object for the ScalarBar. For every displayed
211 ScalarBar label, the scalar value will be passed to the ScalarPrinter
212 object to turn it into a string. Users may override the default ScalarPrinter
213 object to map scalars to whatever strings they wish. @see ScalarPrinter */
214 void setScalarPrinter(ScalarPrinter* sp);
216 /** Get the ScalarPrinter object */
217 const ScalarPrinter* getScalarPrinter() const;
219 /** Set the TextProperties for the labels & title. @see TextProperties */
220 void setTextProperties(const TextProperties& tp);
222 /** Get the TextProperties for the labels & title. @see TextProperties */
223 const TextProperties& getTextProperties() const;
225 /** force update the drawables used to render the scalar bar.*/
226 void update() { createDrawables(); }
229 virtual ~ScalarBar();
233 osg::ref_ptr<ScalarsToColors> _stc;
238 Orientation _orientation;
239 osg::ref_ptr<ScalarPrinter> _sp;
240 TextProperties _textProperties;
242 void createDrawables();