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