| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 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. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSGSIM_SCALARBAR |
|---|
| 15 | #define OSGSIM_SCALARBAR 1 |
|---|
| 16 | |
|---|
| 17 | #include <osgSim/Export> |
|---|
| 18 | #include <osgSim/ColorRange> // The default ScalarsToColors is a ColorRange |
|---|
| 19 | #include <osg/Geode> |
|---|
| 20 | #include <string> |
|---|
| 21 | |
|---|
| 22 | namespace osgSim |
|---|
| 23 | { |
|---|
| 24 | /** |
|---|
| 25 | A ScalarBar is an osg::Geode to render a colored bar representing a range |
|---|
| 26 | of scalars. The scalar/color ranges are specified by an instance of |
|---|
| 27 | ScalarsToColors. There are a number of configurable properties on the |
|---|
| 28 | ScalarBar, such as the orientation, the number of labels to be displayed |
|---|
| 29 | across the range, the number of distinct colors to use when rendering the |
|---|
| 30 | bar, text details etc. |
|---|
| 31 | |
|---|
| 32 | In summary, the main configurables on the ScalarBar are: |
|---|
| 33 | |
|---|
| 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. |
|---|
| 40 | |
|---|
| 41 | The other configurables should be self-explanatory. |
|---|
| 42 | */ |
|---|
| 43 | class OSGSIM_EXPORT ScalarBar: public osg::Geode |
|---|
| 44 | { |
|---|
| 45 | |
|---|
| 46 | public: |
|---|
| 47 | |
|---|
| 48 | /** ScalarBar orientation specification. */ |
|---|
| 49 | enum Orientation{ |
|---|
| 50 | HORIZONTAL, ///< a horizontally ascending scalar bar (x-axis) |
|---|
| 51 | VERTICAL ///< a vertically ascending scalar bar (y-axis) |
|---|
| 52 | }; |
|---|
| 53 | |
|---|
| 54 | /** |
|---|
| 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. |
|---|
| 58 | */ |
|---|
| 59 | struct OSGSIM_EXPORT ScalarPrinter: public osg::Referenced |
|---|
| 60 | { |
|---|
| 61 | virtual std::string printScalar(float scalar); |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | TextProperties allows users to specify a number of properties for the |
|---|
| 66 | text used to display the labels & title on the ScalarBar. Specifiying 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. |
|---|
| 69 | */ |
|---|
| 70 | struct TextProperties |
|---|
| 71 | { |
|---|
| 72 | std::string _fontFile; |
|---|
| 73 | std::pair<int,int> _fontResolution; |
|---|
| 74 | float _characterSize; |
|---|
| 75 | osg::Vec4 _color; |
|---|
| 76 | |
|---|
| 77 | TextProperties(): |
|---|
| 78 | _fontFile("fonts/arial.ttf"), |
|---|
| 79 | _fontResolution(40,40), |
|---|
| 80 | _characterSize(0.0f), |
|---|
| 81 | _color(1.0f,1.0f,1.0f,1.0f) |
|---|
| 82 | { |
|---|
| 83 | } |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | /** Default constructor. */ |
|---|
| 87 | ScalarBar(): osg::Geode(), |
|---|
| 88 | _numColors(256), |
|---|
| 89 | _numLabels(11), |
|---|
| 90 | _stc(new ColorRange(0.0f,1.0f)), |
|---|
| 91 | _title("Scalar Bar"), |
|---|
| 92 | _position(0.0f,0.0f,0.0f), |
|---|
| 93 | _width(1.0f), |
|---|
| 94 | _aspectRatio(0.03), |
|---|
| 95 | _orientation(HORIZONTAL), |
|---|
| 96 | _sp(new ScalarPrinter) |
|---|
| 97 | { |
|---|
| 98 | createDrawables(); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | /** |
|---|
| 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 |
|---|
| 118 | */ |
|---|
| 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): |
|---|
| 124 | osg::Geode(), |
|---|
| 125 | _numColors(numColors), |
|---|
| 126 | _numLabels(numLabels), |
|---|
| 127 | _stc(stc), |
|---|
| 128 | _title(title), |
|---|
| 129 | _position(0.0f,0.0f,0.0f), |
|---|
| 130 | _width(1.0f), |
|---|
| 131 | _aspectRatio(aspectRatio), |
|---|
| 132 | _orientation(orientation), |
|---|
| 133 | _sp(sp) |
|---|
| 134 | { |
|---|
| 135 | createDrawables(); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 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? |
|---|
| 143 | _title(rhs._title), |
|---|
| 144 | _position(rhs._position), |
|---|
| 145 | _width(rhs._width), |
|---|
| 146 | _aspectRatio(rhs._aspectRatio), |
|---|
| 147 | _orientation(rhs._orientation), |
|---|
| 148 | _sp(rhs._sp), // Consider clone for deep copy? |
|---|
| 149 | _textProperties(rhs._textProperties) |
|---|
| 150 | { |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | |
|---|
| 154 | META_Node(osgSim, ScalarBar); |
|---|
| 155 | |
|---|
| 156 | /** Set the number of distinct colours on the ScalarBar. */ |
|---|
| 157 | void setNumColors(int numColors); |
|---|
| 158 | |
|---|
| 159 | /** Get the number of distinct colours on the ScalarBar. */ |
|---|
| 160 | int getNumColors() const; |
|---|
| 161 | |
|---|
| 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 |
|---|
| 164 | in between. */ |
|---|
| 165 | void setNumLabels(int numLabels); |
|---|
| 166 | |
|---|
| 167 | /** Get the number of labels displayed along the ScalarBar. */ |
|---|
| 168 | int getNumLabels() const; |
|---|
| 169 | |
|---|
| 170 | /** Set the ScalarsToColors mapping object for the ScalarBar. */ |
|---|
| 171 | void setScalarsToColors(ScalarsToColors* stc); |
|---|
| 172 | |
|---|
| 173 | /** Get the ScalarsToColors mapping object from the ScalarBar. */ |
|---|
| 174 | const ScalarsToColors* getScalarsToColors() const; |
|---|
| 175 | |
|---|
| 176 | /** Set the title for the ScalarBar, set "" for no title. */ |
|---|
| 177 | void setTitle(const std::string& title); |
|---|
| 178 | |
|---|
| 179 | /** Get the title for the ScalarBar. */ |
|---|
| 180 | const std::string& getTitle() const; |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | /** Set the position of scalar bar's lower left corner.*/ |
|---|
| 184 | void setPosition(const osg::Vec3& pos); |
|---|
| 185 | |
|---|
| 186 | /** Get the position of scalar bar.*/ |
|---|
| 187 | const osg::Vec3& getPosition() const { return _position; } |
|---|
| 188 | |
|---|
| 189 | /** Set the width of the scalar bar.*/ |
|---|
| 190 | void setWidth(float width); |
|---|
| 191 | |
|---|
| 192 | /** Get the width of the scalar bar.*/ |
|---|
| 193 | float getWidth() const { return _width; } |
|---|
| 194 | |
|---|
| 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); |
|---|
| 198 | |
|---|
| 199 | /** Get the aspect ration (y/x) for the displayed bar. */ |
|---|
| 200 | float getAspectRatio() const; |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | /** Set the orientation of the ScalarBar. @see Orientation */ |
|---|
| 204 | void setOrientation(ScalarBar::Orientation orientation); |
|---|
| 205 | |
|---|
| 206 | /** Get the orientation of the ScalarBar. @see Orientation */ |
|---|
| 207 | ScalarBar::Orientation getOrientation() const; |
|---|
| 208 | |
|---|
| 209 | |
|---|
| 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); |
|---|
| 215 | |
|---|
| 216 | /** Get the ScalarPrinter object */ |
|---|
| 217 | const ScalarPrinter* getScalarPrinter() const; |
|---|
| 218 | |
|---|
| 219 | /** Set the TextProperties for the labels & title. @see TextProperties */ |
|---|
| 220 | void setTextProperties(const TextProperties& tp); |
|---|
| 221 | |
|---|
| 222 | /** Get the TextProperties for the labels & title. @see TextProperties */ |
|---|
| 223 | const TextProperties& getTextProperties() const; |
|---|
| 224 | |
|---|
| 225 | /** force update the drawables used to render the scalar bar.*/ |
|---|
| 226 | void update() { createDrawables(); } |
|---|
| 227 | |
|---|
| 228 | protected: |
|---|
| 229 | virtual ~ScalarBar(); |
|---|
| 230 | |
|---|
| 231 | int _numColors; |
|---|
| 232 | int _numLabels; |
|---|
| 233 | osg::ref_ptr<ScalarsToColors> _stc; |
|---|
| 234 | std::string _title; |
|---|
| 235 | osg::Vec3 _position; |
|---|
| 236 | float _width; |
|---|
| 237 | float _aspectRatio; |
|---|
| 238 | Orientation _orientation; |
|---|
| 239 | osg::ref_ptr<ScalarPrinter> _sp; |
|---|
| 240 | TextProperties _textProperties; |
|---|
| 241 | |
|---|
| 242 | void createDrawables(); |
|---|
| 243 | |
|---|
| 244 | }; |
|---|
| 245 | |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | #endif |
|---|