ScalarBar 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #ifndef OSGSIM_SCALARBAR
  14. #define OSGSIM_SCALARBAR 1
  15. #include <osgSim/Export>
  16. #include <osgSim/ColorRange> // The default ScalarsToColors is a ColorRange
  17. #include <osg/Geode>
  18. #include <string>
  19. namespace osgSim
  20. {
  21. /**
  22. A ScalarBar is an osg::Geode to render a colored bar representing a range
  23. of scalars. The scalar/color ranges are specified by an instance of
  24. ScalarsToColors. There are a number of configurable properties on the
  25. ScalarBar, such as the orientation, the number of labels to be displayed
  26. across the range, the number of distinct colors to use when rendering the
  27. bar, text details etc.
  28. In summary, the main configurables on the ScalarBar are:
  29. -# The range of scalars represented by the bar, and the colors
  30. corresponding to this range - these are specified by the
  31. ScalarsToColors object.
  32. -# The number of colors used when rendering the bar geometry -
  33. this may be thought of as the bar 'density'.
  34. -# The number of text labels to be used when displaying the bar.
  35. The other configurables should be self-explanatory.
  36. */
  37. class OSGSIM_EXPORT ScalarBar: public osg::Geode
  38. {
  39. public:
  40. /** ScalarBar orientation specification. */
  41. enum Orientation{
  42. HORIZONTAL, ///< a horizontally ascending scalar bar (x-axis)
  43. VERTICAL ///< a vertically ascending scalar bar (y-axis)
  44. };
  45. /**
  46. Users may provide their own ScalarPrinter by deriving from this base class and
  47. overriding the printScalar() method. Users may map the scalar float passed in
  48. to any string they wish.
  49. */
  50. struct OSGSIM_EXPORT ScalarPrinter: public osg::Referenced
  51. {
  52. virtual std::string printScalar(float scalar);
  53. };
  54. /**
  55. TextProperties allows users to specify a number of properties for the
  56. text used to display the labels & title on the ScalarBar. Specifying a character
  57. size of 0 will cause the ScalarBar to estimate an appropriate size. Note that
  58. the attributes are public, and may be set directly.
  59. */
  60. struct TextProperties
  61. {
  62. std::string _fontFile;
  63. std::pair<int,int> _fontResolution;
  64. float _characterSize;
  65. osg::Vec4 _color;
  66. TextProperties():
  67. _fontFile("fonts/arial.ttf"),
  68. _fontResolution(40,40),
  69. _characterSize(0.0f),
  70. _color(1.0f,1.0f,1.0f,1.0f)
  71. {
  72. }
  73. };
  74. /** Default constructor. */
  75. ScalarBar(): osg::Geode(),
  76. _numColors(256),
  77. _numLabels(11),
  78. _stc(new ColorRange(0.0f,1.0f)),
  79. _title("Scalar Bar"),
  80. _position(0.0f,0.0f,0.0f),
  81. _width(1.0f),
  82. _aspectRatio(0.03),
  83. _orientation(HORIZONTAL),
  84. _sp(new ScalarPrinter)
  85. {
  86. createDrawables();
  87. }
  88. /**
  89. Construct a ScalarBar with the supplied parameters.
  90. @param numColors Specify the number of colors in the scalar bar. Color
  91. interpolation occurs where necessary.
  92. @param numLabels Specify the number of labels in the scalar bar.
  93. @param stc The ScalarsToColors defining the range of scalars
  94. and the colors they map to.
  95. @param title The title to be used when displaying the ScalarBar.
  96. Specify "" for no title.
  97. @param orientation The orientation of the ScalarBar. @see Orientation.
  98. @param aspectRatio The aspect ration (y/x) for the displayed bar. Bear in mind you
  99. may want to change this if you change the orientation.
  100. @param sp A ScalarPrinter object for the ScalarBar. For every displayed
  101. ScalarBar label, the scalar value will be passed to the
  102. ScalarPrinter object to turn it into a string. Users may
  103. override the default ScalarPrinter object to map scalars to
  104. whatever strings they wish. @see ScalarPrinter
  105. */
  106. ScalarBar(int numColors, int numLabels, ScalarsToColors* stc,
  107. const std::string& title,
  108. Orientation orientation = HORIZONTAL,
  109. float aspectRatio=0.25,
  110. ScalarPrinter* sp=new ScalarPrinter):
  111. osg::Geode(),
  112. _numColors(numColors),
  113. _numLabels(numLabels),
  114. _stc(stc),
  115. _title(title),
  116. _position(0.0f,0.0f,0.0f),
  117. _width(1.0f),
  118. _aspectRatio(aspectRatio),
  119. _orientation(orientation),
  120. _sp(sp)
  121. {
  122. createDrawables();
  123. }
  124. /** Copy constructor */
  125. ScalarBar(const ScalarBar& rhs, const osg::CopyOp& co): osg::Geode(rhs,co),
  126. _numColors(rhs._numColors),
  127. _numLabels(rhs._numLabels),
  128. _stc(rhs._stc), // Consider clone for deep copy?
  129. _title(rhs._title),
  130. _position(rhs._position),
  131. _width(rhs._width),
  132. _aspectRatio(rhs._aspectRatio),
  133. _orientation(rhs._orientation),
  134. _sp(rhs._sp), // Consider clone for deep copy?
  135. _textProperties(rhs._textProperties)
  136. {
  137. }
  138. META_Node(osgSim, ScalarBar);
  139. /** Set the number of distinct colours on the ScalarBar. */
  140. void setNumColors(int numColors);
  141. /** Get the number of distinct colours on the ScalarBar. */
  142. int getNumColors() const;
  143. /** Set the number of labels to display along the ScalarBar. There
  144. will be one label at each end point, and evenly distributed labels
  145. in between. */
  146. void setNumLabels(int numLabels);
  147. /** Get the number of labels displayed along the ScalarBar. */
  148. int getNumLabels() const;
  149. /** Set the ScalarsToColors mapping object for the ScalarBar. */
  150. void setScalarsToColors(ScalarsToColors* stc);
  151. /** Get the ScalarsToColors mapping object from the ScalarBar. */
  152. const ScalarsToColors* getScalarsToColors() const;
  153. /** Set the title for the ScalarBar, set "" for no title. */
  154. void setTitle(const std::string& title);
  155. /** Get the title for the ScalarBar. */
  156. const std::string& getTitle() const;
  157. /** Set the position of scalar bar's lower left corner.*/
  158. void setPosition(const osg::Vec3& pos);
  159. /** Get the position of scalar bar.*/
  160. const osg::Vec3& getPosition() const { return _position; }
  161. /** Set the width of the scalar bar.*/
  162. void setWidth(float width);
  163. /** Get the width of the scalar bar.*/
  164. float getWidth() const { return _width; }
  165. /** Set the aspect ration (y/x) for the displayed bar. Bear in mind you
  166. may want to change this if you change the orientation. */
  167. void setAspectRatio(float aspectRatio);
  168. /** Get the aspect ration (y/x) for the displayed bar. */
  169. float getAspectRatio() const;
  170. /** Set the orientation of the ScalarBar. @see Orientation */
  171. void setOrientation(ScalarBar::Orientation orientation);
  172. /** Get the orientation of the ScalarBar. @see Orientation */
  173. ScalarBar::Orientation getOrientation() const;
  174. /** Set a ScalarPrinter object for the ScalarBar. For every displayed
  175. ScalarBar label, the scalar value will be passed to the ScalarPrinter
  176. object to turn it into a string. Users may override the default ScalarPrinter
  177. object to map scalars to whatever strings they wish. @see ScalarPrinter */
  178. void setScalarPrinter(ScalarPrinter* sp);
  179. /** Get the ScalarPrinter object */
  180. const ScalarPrinter* getScalarPrinter() const;
  181. /** Set the TextProperties for the labels & title. @see TextProperties */
  182. void setTextProperties(const TextProperties& tp);
  183. /** Get the TextProperties for the labels & title. @see TextProperties */
  184. const TextProperties& getTextProperties() const;
  185. /** force update the drawables used to render the scalar bar.*/
  186. void update() { createDrawables(); }
  187. protected:
  188. virtual ~ScalarBar();
  189. int _numColors;
  190. int _numLabels;
  191. osg::ref_ptr<ScalarsToColors> _stc;
  192. std::string _title;
  193. osg::Vec3 _position;
  194. float _width;
  195. float _aspectRatio;
  196. Orientation _orientation;
  197. osg::ref_ptr<ScalarPrinter> _sp;
  198. TextProperties _textProperties;
  199. void createDrawables();
  200. };
  201. }
  202. #endif