root/OpenSceneGraph/trunk/src/osgSim/ScalarBar.cpp @ 13502

Revision 13502, 6.4 kB (checked in by robert, 5 hours ago)

Standardized on defined(ANDROID)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1#include <osg/Config>
2#ifndef OSG_USE_DEPRECATED_GEOMETRY_METHODS 
3#define OSG_USE_DEPRECATED_GEOMETRY_METHODS 1
4#endif
5
6#include <osgSim/ScalarBar>
7#include <osgText/Text>
8#include <osg/Geometry>
9#include <sstream>
10
11using namespace osgSim;
12
13ScalarBar::~ScalarBar()
14{
15}
16
17std::string ScalarBar::ScalarPrinter::printScalar(float scalar)
18{
19    std::stringstream ostr;
20    ostr<<scalar;
21    return ostr.str();
22}
23
24void ScalarBar::setNumColors(int numColors)
25{
26    _numColors = numColors;
27    createDrawables();
28}
29
30int ScalarBar::getNumColors() const
31{
32    return _numColors;
33}
34
35void ScalarBar::setNumLabels(int numLabels)
36{
37    _numLabels = numLabels;
38    createDrawables();
39}
40
41int ScalarBar::getNumLabels() const
42{
43    return _numLabels;
44}
45
46void ScalarBar::setScalarsToColors(ScalarsToColors* stc)
47{
48    _stc = stc;
49    createDrawables();
50}
51
52const ScalarsToColors* ScalarBar::getScalarsToColors() const
53{
54    return _stc.get();
55}
56
57void ScalarBar::setTitle(const std::string& title)
58{
59    _title = title;
60    createDrawables();
61}
62
63const std::string& ScalarBar::getTitle() const
64{
65    return _title;
66}
67
68void ScalarBar::setPosition(const osg::Vec3& pos)
69{
70    _position = pos;
71    createDrawables();
72}
73
74void ScalarBar::setWidth(float width)
75{
76    _width = width;
77    createDrawables();
78}
79
80void ScalarBar::setOrientation(ScalarBar::Orientation orientation)
81{
82    _orientation = orientation;
83    createDrawables();
84}
85
86ScalarBar::Orientation ScalarBar::getOrientation() const
87{
88    return _orientation;
89}
90
91void ScalarBar::setAspectRatio(float aspectRatio)
92{
93    _aspectRatio = aspectRatio;
94    createDrawables();
95}
96
97float ScalarBar::getAspectRatio() const
98{
99    return _aspectRatio;
100}
101
102void ScalarBar::setScalarPrinter(ScalarPrinter* sp)
103{
104    _sp = sp;
105    createDrawables();
106}
107
108const ScalarBar::ScalarPrinter* ScalarBar::getScalarPrinter() const
109{
110    return _sp.get();
111}
112
113
114void ScalarBar::setTextProperties(const TextProperties& tp)
115{
116    _textProperties = tp;
117    createDrawables();
118}
119
120const ScalarBar::TextProperties& ScalarBar::getTextProperties() const
121{
122    return _textProperties;
123}
124
125
126void ScalarBar::createDrawables()
127{
128    // Remove any existing Drawables
129    _drawables.erase(_drawables.begin(), _drawables.end());
130
131    if (_numColors==0) return;
132
133    osg::Matrix matrix;
134    if(_orientation==HORIZONTAL)
135    {
136        matrix = osg::Matrix::translate(_position);
137    }
138    else
139    {
140        matrix = osg::Matrix::rotate(osg::DegreesToRadians(90.0f),1.0f,0.0f,0.0f) * osg::Matrix::translate(_position);
141    }
142
143    // 1. First the bar
144    // =================
145    osg::ref_ptr<osg::Geometry> bar = new osg::Geometry();
146
147    // Create the bar - created in 'real' coordinate space the moment,
148    // with xyz values reflecting those of the actual scalar values in play.
149    // FIXME: Consider positioning at origin! Should be easy enough to do.
150
151    // Vertices
152    osg::ref_ptr<osg::Vec3Array> vs(new osg::Vec3Array);
153    vs->reserve(2*(_numColors+1));
154
155    float incr = (_stc->getMax() - _stc->getMin()) / _numColors;
156    float xincr = (_width) / _numColors;
157    float arOffset = _width * _aspectRatio;
158
159    int i;
160    for(i=1; i<=_numColors; ++i)
161    {
162        vs->push_back(osg::Vec3((i-1) * xincr, 0.0f,     0.0f)*matrix);
163        vs->push_back(osg::Vec3((i-1) * xincr, arOffset, 0.0f)*matrix);
164        vs->push_back(osg::Vec3(i     * xincr, arOffset, 0.0f)*matrix);
165        vs->push_back(osg::Vec3(i     * xincr, 0.0f,     0.0f)*matrix);
166    }
167    bar->setVertexArray(vs.get());
168
169    // Colours
170    osg::ref_ptr<osg::Vec4Array> cs(new osg::Vec4Array);
171    cs->reserve(_numColors);
172    const float halfIncr = incr*0.5;
173    for(i=0; i<_numColors; ++i)
174    {
175        // We add half an increment to the color look-up to get the color
176        // square in the middle of the 'block'.
177        cs->push_back(_stc->getColor(_stc->getMin() + (i*incr)  + halfIncr));
178    }
179    bar->setColorArray(cs.get());
180    bar->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE);
181
182    // Normal
183    osg::ref_ptr<osg::Vec3Array> ns(new osg::Vec3Array);
184    ns->push_back(osg::Matrix::transform3x3(osg::Vec3(0.0f,0.0f,1.0f),matrix));
185    bar->setNormalArray(ns.get());
186    bar->setNormalBinding(osg::Geometry::BIND_OVERALL);
187
188    // The Quad strip that represents the bar
189    bar->addPrimitiveSet(new osg::DrawArrays(GL_QUADS,0,vs->size()));
190
191    addDrawable(bar.get());
192
193    // 2. Then the text labels
194    // =======================
195
196    // Check the character size, if it's 0, estimate a good character size
197    float characterSize = _textProperties._characterSize;
198    if(characterSize == 0) characterSize = _width * 0.03f;
199
200    osgText::Font* font = osgText::readFontFile(_textProperties._fontFile.c_str());
201
202    std::vector<osgText::Text*> texts(_numLabels);      // We'll need to collect pointers to these for later
203    float labelIncr = (_numLabels>0) ? (_stc->getMax()-_stc->getMin())/(_numLabels-1) : 0.0f;
204    float labelxIncr = (_numLabels>0) ? (_width)/(_numLabels-1) : 0.0f;
205    float labely = arOffset + characterSize*0.3f;
206    for(i=0; i<_numLabels; ++i)
207    {
208        osgText::Text* text = new osgText::Text;
209        text->setFont(font);
210        text->setColor(_textProperties._color);
211        text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second);
212        text->setCharacterSize(characterSize);
213        text->setText(_sp->printScalar(_stc->getMin()+(i*labelIncr)));
214
215        text->setPosition(osg::Vec3((i*labelxIncr), labely, 0.0f)*matrix);
216        text->setAlignment(osgText::Text::CENTER_BASE_LINE);
217        text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE :  osgText::Text::XZ_PLANE );
218
219        addDrawable(text);
220
221        texts[i] = text;
222    }
223
224
225    // 3. And finally the title
226    // ========================
227
228    if(_title != "")
229    {
230        osgText::Text* text = new osgText::Text;
231        text->setFont(font);
232        text->setColor(_textProperties._color);
233        text->setFontResolution(_textProperties._fontResolution.first,_textProperties._fontResolution.second);
234        text->setCharacterSize(characterSize);
235        text->setText(_title);
236
237        float titleY = (_numLabels>0) ? labely + characterSize : labely;
238
239        // Position the title at the middle of the bar above any labels.
240        text->setPosition(osg::Vec3((_width/2.0f), titleY, 0.0f)*matrix);
241        text->setAlignment(osgText::Text::CENTER_BASE_LINE);
242        text->setAxisAlignment( (_orientation==HORIZONTAL) ? osgText::Text::XY_PLANE :  osgText::Text::XZ_PLANE );
243
244        addDrawable(text);
245    }
246}
Note: See TracBrowser for help on using the browser.