2003-09-01 17:36:03 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OSGSIM_SCALARBAR
|
|
|
|
#define OSGSIM_SCALARBAR 1
|
|
|
|
|
|
|
|
#include <osgSim/Export>
|
|
|
|
#include <osgSim/ColorRange> // The default ScalarsToColors is a ColorRange
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace osgSim
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
A ScalarBar is an osg::Geode to render a colored bar representing a range
|
|
|
|
of scalars. The scalar/color ranges are specified by an instance of
|
|
|
|
ScalarsToColors. There are a number of configurable properties on the
|
|
|
|
ScalarBar, such as the orientation, the number of labels to be displayed
|
|
|
|
across the range, the number of distinct colors to use when rendering the
|
|
|
|
bar, text details etc.
|
|
|
|
|
|
|
|
In summary, the main configurables on the ScalarBar are:
|
|
|
|
|
|
|
|
-# The range of scalars represented by the bar, and the colors
|
|
|
|
corresponding to this range - these are specified by the
|
2003-09-01 23:46:10 +08:00
|
|
|
ScalarsToColors object.
|
2003-09-01 17:36:03 +08:00
|
|
|
-# The number of colors used when rendering the bar geometry -
|
|
|
|
this may be thought of as the bar 'density'.
|
|
|
|
-# The number of text labels to be used when displaying the bar.
|
|
|
|
|
|
|
|
The other configurables should be self-explanatory.
|
|
|
|
*/
|
|
|
|
class OSGSIM_EXPORT ScalarBar: public osg::Geode
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/** ScalarBar orientation specification. */
|
|
|
|
enum Orientation{
|
|
|
|
HORIZONTAL, ///< a horizontally ascending scalar bar (x-axis)
|
2003-09-01 23:46:10 +08:00
|
|
|
VERTICAL ///< a vertically ascending scalar bar (y-axis)
|
|
|
|
};
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/**
|
2003-09-01 17:36:03 +08:00
|
|
|
Users may provide their own ScalarPrinter by deriving from this base class and
|
2003-09-01 23:46:10 +08:00
|
|
|
overriding the printScalar() method. Users may map the scalar float passed in
|
|
|
|
to any string they wish.
|
|
|
|
*/
|
2003-11-26 03:25:42 +08:00
|
|
|
struct OSGSIM_EXPORT ScalarPrinter: public osg::Referenced
|
2003-09-01 23:46:10 +08:00
|
|
|
{
|
|
|
|
virtual std::string printScalar(float scalar);
|
|
|
|
};
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/**
|
2003-09-01 17:36:03 +08:00
|
|
|
TextProperties allows users to specify a number of properties for the
|
2003-09-01 23:46:10 +08:00
|
|
|
text used to display the labels & title on the ScalarBar. Specifiying a character
|
|
|
|
size of 0 will cause the ScalarBar to estimate an appropriate size. Note that
|
2003-09-01 17:36:03 +08:00
|
|
|
the attributes are public, and may be set directly.
|
2003-09-01 23:46:10 +08:00
|
|
|
*/
|
2003-09-01 17:36:03 +08:00
|
|
|
struct TextProperties
|
2003-09-01 23:46:10 +08:00
|
|
|
{
|
2003-09-01 17:36:03 +08:00
|
|
|
std::string _fontFile;
|
2003-09-01 23:46:10 +08:00
|
|
|
std::pair<int,int> _fontResolution;
|
2004-06-02 20:37:14 +08:00
|
|
|
float _characterSize;
|
2003-09-01 23:46:10 +08:00
|
|
|
osg::Vec4 _color;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
TextProperties():
|
2003-09-01 17:36:03 +08:00
|
|
|
_fontFile("fonts/arial.ttf"),
|
2003-09-01 23:46:10 +08:00
|
|
|
_fontResolution(40,40),
|
2004-06-02 20:37:14 +08:00
|
|
|
_characterSize(0.0f),
|
2003-09-01 23:46:10 +08:00
|
|
|
_color(1.0f,1.0f,1.0f,1.0f)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
/** Default constructor. */
|
|
|
|
ScalarBar(): osg::Geode(),
|
2003-09-01 23:46:10 +08:00
|
|
|
_numColors(256),
|
|
|
|
_numLabels(11),
|
|
|
|
_stc(new ColorRange(0.0f,1.0f)),
|
|
|
|
_title("Scalar Bar"),
|
2004-06-02 20:37:14 +08:00
|
|
|
_position(0.0f,0.0f,0.0f),
|
|
|
|
_width(1.0f),
|
2003-09-01 23:46:10 +08:00
|
|
|
_aspectRatio(0.03),
|
2004-06-02 20:37:14 +08:00
|
|
|
_orientation(HORIZONTAL),
|
2003-09-01 23:46:10 +08:00
|
|
|
_sp(new ScalarPrinter)
|
|
|
|
{
|
|
|
|
createDrawables();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Construct a ScalarBar with the supplied parameters.
|
|
|
|
@param numColors Specify the number of colors in the scalar bar. Color
|
|
|
|
interpolation occurs where necessary.
|
|
|
|
@param stc The ScalarsToColors defining the range of scalars
|
|
|
|
and the colors they map to.
|
|
|
|
@param title The title to be used when displaying the ScalarBar.
|
2003-09-01 17:36:03 +08:00
|
|
|
Specify "" for no title.
|
2003-09-01 23:46:10 +08:00
|
|
|
@param orientation The orientation of the ScalarBar. @see Orientation.
|
|
|
|
@param apectRatio The aspect ration (y/x) for the displayed bar. Bear in mind you
|
|
|
|
may want to change this if you change the orientation.
|
|
|
|
@param sp A ScalarPrinter object for the ScalarBar. For every displayed
|
|
|
|
ScalarBar label, the scalar value will be passed to the
|
2003-09-01 17:36:03 +08:00
|
|
|
ScalarPrinter object to turn it into a string. Users may
|
2003-09-01 23:46:10 +08:00
|
|
|
override the default ScalarPrinter object to map scalars to
|
|
|
|
whatever strings they wish. @see ScalarPrinter
|
|
|
|
*/
|
2003-09-01 17:36:03 +08:00
|
|
|
ScalarBar(int numColors, int numLabels, ScalarsToColors* stc,
|
2003-09-01 23:46:10 +08:00
|
|
|
const std::string& title,
|
|
|
|
Orientation orientation = HORIZONTAL,
|
|
|
|
float aspectRatio=0.25,
|
|
|
|
ScalarPrinter* sp=new ScalarPrinter):
|
|
|
|
osg::Geode(),
|
|
|
|
_numColors(numColors),
|
|
|
|
_numLabels(numLabels),
|
|
|
|
_stc(stc),
|
|
|
|
_title(title),
|
2004-06-02 20:37:14 +08:00
|
|
|
_position(0.0f,0.0f,0.0f),
|
|
|
|
_width(1.0f),
|
2003-09-01 23:46:10 +08:00
|
|
|
_aspectRatio(aspectRatio),
|
2004-06-02 20:37:14 +08:00
|
|
|
_orientation(orientation),
|
2003-09-01 23:46:10 +08:00
|
|
|
_sp(sp)
|
|
|
|
{
|
2003-09-01 17:36:03 +08:00
|
|
|
createDrawables();
|
2003-09-01 23:46:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Copy constructor */
|
|
|
|
ScalarBar(const ScalarBar& rhs, const osg::CopyOp& co): osg::Geode(rhs,co),
|
|
|
|
_numColors(rhs._numColors),
|
|
|
|
_numLabels(rhs._numLabels),
|
|
|
|
_stc(rhs._stc), // Consider clone for deep copy?
|
|
|
|
_title(rhs._title),
|
2004-06-02 20:37:14 +08:00
|
|
|
_position(rhs._position),
|
|
|
|
_width(rhs._width),
|
2003-09-01 23:46:10 +08:00
|
|
|
_aspectRatio(rhs._aspectRatio),
|
2004-06-02 20:37:14 +08:00
|
|
|
_orientation(rhs._orientation),
|
2003-09-01 23:46:10 +08:00
|
|
|
_sp(rhs._sp), // Consider clone for deep copy?
|
|
|
|
_textProperties(rhs._textProperties)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2004-04-22 18:44:23 +08:00
|
|
|
|
|
|
|
META_Node(osgSim, ScalarBar);
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Set the number of distinct colours on the ScalarBar. */
|
|
|
|
void setNumColors(int numColors);
|
|
|
|
|
|
|
|
/** Get the number of distinct colours on the ScalarBar. */
|
|
|
|
int getNumColors() const;
|
|
|
|
|
|
|
|
/** Set the number of labels to display along the ScalarBar. There
|
|
|
|
will be one label at each end point, and evenly distributed labels
|
|
|
|
in between. */
|
|
|
|
void setNumLabels(int numLabels);
|
|
|
|
|
|
|
|
/** Get the number of labels displayed along the ScalarBar. */
|
|
|
|
int getNumLabels() const;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
/** Set the ScalarsToColors mapping object for the ScalarBar. */
|
2003-09-01 23:46:10 +08:00
|
|
|
void setScalarsToColors(ScalarsToColors* stc);
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Get the ScalarsToColors mapping object from the ScalarBar. */
|
2003-09-01 17:36:03 +08:00
|
|
|
const ScalarsToColors* getScalarsToColors() const;
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Set the title for the ScalarBar, set "" for no title. */
|
2003-09-01 17:36:03 +08:00
|
|
|
void setTitle(const std::string& title);
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Get the title for the ScalarBar. */
|
|
|
|
std::string getTitle() const;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
|
2004-06-02 20:37:14 +08:00
|
|
|
/** Set the position of scalar bar's lower left corner.*/
|
|
|
|
void setPosition(const osg::Vec3& pos);
|
|
|
|
|
|
|
|
/** Get the position of scalar bar.*/
|
|
|
|
const osg::Vec3& getPosition() const { return _position; }
|
|
|
|
|
|
|
|
/** Set the width of the scalar bar.*/
|
|
|
|
void setWidth(float width);
|
|
|
|
|
|
|
|
/** Get the width of the scalar bar.*/
|
|
|
|
float getWidth() { return _width; }
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Set the aspect ration (y/x) for the displayed bar. Bear in mind you
|
|
|
|
may want to change this if you change the orientation. */
|
2003-09-01 17:36:03 +08:00
|
|
|
void setAspectRatio(float aspectRatio);
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Get the aspect ration (y/x) for the displayed bar. */
|
|
|
|
float getAspectRatio() const;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2004-06-02 20:37:14 +08:00
|
|
|
|
|
|
|
/** Set the orientation of the ScalarBar. @see Orientation */
|
|
|
|
void setOrientation(ScalarBar::Orientation orientation);
|
|
|
|
|
|
|
|
/** Get the orientation of the ScalarBar. @see Orientation */
|
|
|
|
ScalarBar::Orientation getOrientation() const;
|
|
|
|
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Set a ScalarPrinter object for the ScalarBar. For every displayed
|
|
|
|
ScalarBar label, the scalar value will be passed to the ScalarPrinter
|
|
|
|
object to turn it into a string. Users may override the default ScalarPrinter
|
|
|
|
object to map scalars to whatever strings they wish. @see ScalarPrinter */
|
2003-09-01 17:36:03 +08:00
|
|
|
void setScalarPrinter(ScalarPrinter* sp);
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Get the ScalarPrinter object */
|
|
|
|
const ScalarPrinter* getScalarPrinter() const;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Set the TextProperties for the labels & title. @see TextProperties */
|
2003-09-01 17:36:03 +08:00
|
|
|
void setTextProperties(const TextProperties& tp);
|
|
|
|
|
2003-09-01 23:46:10 +08:00
|
|
|
/** Get the TextProperties for the labels & title. @see TextProperties */
|
|
|
|
const TextProperties& getTextProperties() const;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2004-04-22 18:44:23 +08:00
|
|
|
/** force update the drawables used to render the scalar bar.*/
|
|
|
|
void update() { createDrawables(); }
|
2003-09-01 17:36:03 +08:00
|
|
|
|
2004-08-02 17:11:31 +08:00
|
|
|
protected:
|
|
|
|
virtual ~ScalarBar();
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
int _numColors;
|
2003-09-01 23:46:10 +08:00
|
|
|
int _numLabels;
|
|
|
|
osg::ref_ptr<ScalarsToColors> _stc;
|
|
|
|
std::string _title;
|
2004-06-02 20:37:14 +08:00
|
|
|
osg::Vec3 _position;
|
|
|
|
float _width;
|
2003-09-01 23:46:10 +08:00
|
|
|
float _aspectRatio;
|
2004-06-02 20:37:14 +08:00
|
|
|
Orientation _orientation;
|
2003-09-01 17:36:03 +08:00
|
|
|
osg::ref_ptr<ScalarPrinter> _sp;
|
2003-09-01 23:46:10 +08:00
|
|
|
TextProperties _textProperties;
|
2003-09-01 17:36:03 +08:00
|
|
|
|
|
|
|
void createDrawables();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|