root/OpenSceneGraph/trunk/src/osgTerrain/Layer.cpp @ 13041

Revision 13041, 17.2 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
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#include <osgTerrain/Layer>
15#include <osg/Notify>
16
17using namespace osgTerrain;
18
19void osgTerrain::extractSetNameAndFileName(const std::string& compoundstring, std::string& setname, std::string& filename)
20{
21    std::string::size_type setcolonpos = compoundstring.find("set:");
22    if (setcolonpos==std::string::npos)
23    {
24        setname = "";
25        filename = compoundstring;
26        return;
27    }
28
29    if (compoundstring.size()==4)
30    {
31        setname = "";
32        filename = "";
33        return;
34    }
35
36    std::string::size_type secondcolonpos = compoundstring.find_first_of(':', setcolonpos+4);
37    if (secondcolonpos==std::string::npos)
38    {
39        setname = compoundstring.substr(setcolonpos+4,std::string::npos);
40        filename = "";
41        return;
42    }
43
44    setname = compoundstring.substr(setcolonpos+4,secondcolonpos-setcolonpos-4);
45    filename = compoundstring.substr(secondcolonpos+1, std::string::npos);
46}
47
48std::string osgTerrain::createCompoundSetNameAndFileName(const std::string& setname, const std::string& filename)
49{
50    if (setname.empty()) return filename;
51    return std::string("set:")+setname+std::string(":")+filename;
52}
53
54
55Layer::Layer():
56    _minLevel(0),
57    _maxLevel(MAXIMUM_NUMBER_OF_LEVELS),
58    _minFilter(osg::Texture::LINEAR_MIPMAP_LINEAR),
59    _magFilter(osg::Texture::LINEAR)
60{
61}
62
63Layer::Layer(const Layer& layer,const osg::CopyOp& copyop):
64    osg::Object(layer,copyop),
65    _filename(layer._filename),
66    _minLevel(layer._minLevel),
67    _maxLevel(layer._maxLevel),
68    _minFilter(layer._minFilter),
69    _magFilter(layer._magFilter)
70{
71}
72
73Layer::~Layer()
74{
75}
76
77osg::BoundingSphere Layer::computeBound(bool treatAsElevationLayer) const
78{
79    osg::BoundingSphere bs;
80    if (!getLocator()) return bs;
81
82    if (treatAsElevationLayer)
83    {
84        osg::BoundingBox bb;
85        unsigned int numColumns = getNumColumns();
86        unsigned int numRows = getNumRows();
87        for(unsigned int r=0;r<numRows;++r)
88        {
89            for(unsigned int c=0;c<numColumns;++c)
90            {
91                float value = 0.0f;
92                bool validValue = getValidValue(c,r, value);
93                if (validValue)
94                {
95                    osg::Vec3d ndc, v;
96                    ndc.x() = ((double)c)/(double)(numColumns-1),
97                    ndc.y() = ((double)r)/(double)(numRows-1);
98                    ndc.z() = value;
99
100                    if (getLocator()->convertLocalToModel(ndc, v))
101                    {
102                        bb.expandBy(v);
103                    }
104                }
105            }
106        }
107        bs.expandBy(bb);
108    }
109    else
110    {
111
112        osg::Vec3d v;
113        if (getLocator()->convertLocalToModel(osg::Vec3d(0.5,0.5,0.0), v))
114        {
115            bs.center() = v;
116        }
117
118        if (getLocator()->convertLocalToModel(osg::Vec3d(0.0,0.0,0.0), v))
119        {
120            bs.radius() = (bs.center() - v).length();
121        }
122
123    }
124
125    return bs;
126}
127
128
129/////////////////////////////////////////////////////////////////////////////
130//
131// ImageLayer
132//
133ImageLayer::ImageLayer(osg::Image* image):
134    _image(image)
135{
136}
137
138ImageLayer::ImageLayer(const ImageLayer& imageLayer,const osg::CopyOp& copyop):
139    Layer(imageLayer, copyop),
140    _image(imageLayer._image)
141{
142}
143
144void ImageLayer::setImage(osg::Image* image)
145{
146    _image = image;
147}
148
149template <typename T, class O>
150void _processRow(unsigned int num, GLenum pixelFormat, T* data,const O& operation)
151{
152    switch(pixelFormat)
153    {
154        case(GL_LUMINANCE):         { for(unsigned int i=0;i<num;++i) { operation(*data++); } }  break;
155        case(GL_ALPHA):             { for(unsigned int i=0;i<num;++i) { operation(*data++); } }  break;
156        case(GL_LUMINANCE_ALPHA):   { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); } }  break;
157        case(GL_RGB):               { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); } }  break;
158        case(GL_RGBA):              { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); operation(*data++);  } }  break;
159        case(GL_BGR):               { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); } }  break;
160        case(GL_BGRA):              { for(unsigned int i=0;i<num;++i) { operation(*data++); operation(*data++); operation(*data++); operation(*data++);  } }  break;
161    }
162}
163
164template <class O>
165void processRow(unsigned int num, GLenum pixelFormat, GLenum dataType, unsigned char* data, const O& operation)
166{
167    switch(dataType)
168    {
169        case(GL_BYTE):              _processRow(num,pixelFormat, (char*)data,            operation); break;
170        case(GL_UNSIGNED_BYTE):     _processRow(num,pixelFormat, (unsigned char*)data,   operation); break;
171        case(GL_SHORT):             _processRow(num,pixelFormat, (short*) data,          operation); break;
172        case(GL_UNSIGNED_SHORT):    _processRow(num,pixelFormat, (unsigned short*)data,  operation); break;
173        case(GL_INT):               _processRow(num,pixelFormat, (int*) data,            operation); break;
174        case(GL_UNSIGNED_INT):      _processRow(num,pixelFormat, (unsigned int*) data,   operation); break;
175        case(GL_FLOAT):             _processRow(num,pixelFormat, (float*) data,          operation); break;
176    }
177}
178
179template <class O>
180void processImage(osg::Image* image, const O& operation)
181{
182    if (!image) return;
183
184    for(int r=0;r<image->r();++r)
185    {
186        for(int t=0;t<image->t();++t)
187        {
188            processRow(image->s(), image->getPixelFormat(), image->getDataType(), image->data(0,t,r), operation);
189        }
190    }
191}
192
193struct TransformOperator
194{
195    TransformOperator(float offset, float scale):
196        _offset(offset),
197        _scale(scale) {}
198
199    inline void operator() (unsigned char& v) const { v = (unsigned char)(_offset + (float)v * _scale); }
200    inline void operator() (unsigned short& v) const { v = (unsigned short)(_offset + (float)v * _scale); }
201    inline void operator() (unsigned int& v) const { v = (unsigned int)(_offset + (float)v * _scale); }
202    inline void operator() (char& v) const { v = (char)(_offset + (float)v * _scale); }
203    inline void operator() (short& v) const { v = (short)(_offset + (float)v * _scale); }
204    inline void operator() (int& v) const { v = (int)(_offset + (float)v * _scale); }
205    inline void operator() (float& v) const { v = _offset + v * _scale; }
206
207    float _offset, _scale;
208};
209
210
211bool ImageLayer::transform(float offset, float scale)
212{
213    if (!_image.valid()) return false;
214
215    OSG_INFO<<"ImageLayer::transform("<<offset<<","<<scale<<")"<<std::endl;;
216
217    processImage(_image.get(), TransformOperator(offset,scale));
218
219    dirty();
220
221    return true;
222}
223
224bool ImageLayer::getValue(unsigned int i, unsigned int j, float& value) const
225{
226    const unsigned char* data = _image->data(i,j);
227    switch(_image->getDataType())
228    {
229        case(GL_BYTE):
230            value = *((const char*)data);
231            // OSG_NOTICE<<"byte "<<value<<std::endl;
232            break;
233        case(GL_UNSIGNED_BYTE):
234            value = *data;
235            // OSG_NOTICE<<"Unsigned byte "<<value<<std::endl;
236            break;
237        case(GL_SHORT):
238            value = *((const short*)data);
239            // OSG_NOTICE<<"Short "<<value<<std::endl;
240            break;
241        case(GL_UNSIGNED_SHORT):
242            value = *((const unsigned short*)data);
243            // OSG_NOTICE<<"Unsigned Short "<<value<<std::endl;
244            break;
245        case(GL_INT):
246            value = *((const int*)data);
247            // OSG_NOTICE<<"Int "<<value<<std::endl;
248            break;
249        case(GL_UNSIGNED_INT):
250            // OSG_NOTICE<<"Unsigned Int "<<value<<std::endl;
251            value = *((const unsigned int*)data);
252            break;
253        case(GL_FLOAT):
254            // OSG_NOTICE<<"Float "<<value<<std::endl;
255            value = *((const float*)data);
256            break;
257        default:
258            value = _defaultValue.x();
259            return false;
260    }
261
262    return true;
263}
264
265bool ImageLayer::getValue(unsigned int i, unsigned int j, osg::Vec2& value) const
266{
267    OSG_NOTICE<<"Not implemented yet"<<std::endl;
268    return false;
269}
270
271bool ImageLayer::getValue(unsigned int i, unsigned int j, osg::Vec3& value) const
272{
273    OSG_NOTICE<<"Not implemented yet"<<std::endl;
274    return false;
275}
276
277bool ImageLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) const
278{
279    OSG_NOTICE<<"Not implemented yet"<<std::endl;
280    return false;
281}
282
283void ImageLayer::dirty()
284{
285    if (_image.valid()) _image->dirty();
286}
287
288void ImageLayer::setModifiedCount(unsigned int value)
289{
290    if (!_image) return;
291    else _image->setModifiedCount(value);
292}
293
294unsigned int ImageLayer::getModifiedCount() const
295{
296    if (!_image) return 0;
297    else return _image->getModifiedCount();
298}
299
300
301/////////////////////////////////////////////////////////////////////////////
302//
303// ContourLayer
304//
305ContourLayer::ContourLayer(osg::TransferFunction1D* tf):
306    _tf(tf)
307{
308    _minFilter = osg::Texture::NEAREST;
309    _magFilter = osg::Texture::NEAREST;
310}
311
312ContourLayer::ContourLayer(const ContourLayer& contourLayer,const osg::CopyOp& copyop):
313    Layer(contourLayer, copyop),
314    _tf(contourLayer._tf)
315{
316}
317
318void ContourLayer::setTransferFunction(osg::TransferFunction1D* tf)
319{
320    _tf = tf;
321}
322
323bool ContourLayer::transform(float offset, float scale)
324{
325    if (!_tf) return false;
326
327    OSG_INFO<<"ContourLayer::transform("<<offset<<","<<scale<<")"<<std::endl;;
328
329    osg::TransferFunction1D::ColorMap newColorMap = _tf->getColorMap();
330    for(osg::TransferFunction1D::ColorMap::iterator itr = newColorMap.begin();
331        itr != newColorMap.end();
332        ++itr)
333    {
334        osg::Vec4& value = itr->second;
335        value.r() = offset + value.r()* scale;
336        value.g() = offset + value.g()* scale;
337        value.b() = offset + value.b()* scale;
338        value.a() = offset + value.a()* scale;
339    }
340
341    _tf->assign(newColorMap);
342
343    dirty();
344
345    return true;
346}
347
348bool ContourLayer::getValue(unsigned int i, unsigned int j, float& value) const
349{
350    if (!_tf) return false;
351
352    const osg::Vec4& v = _tf->getPixelValue(i);
353    value = v[0];
354
355    return true;
356}
357
358bool ContourLayer::getValue(unsigned int i, unsigned int j, osg::Vec2& value) const
359{
360    if (!_tf) return false;
361
362    const osg::Vec4& v = _tf->getPixelValue(i);
363    value.x() = v.x();
364    value.y() = v.y();
365
366    return true;
367}
368
369bool ContourLayer::getValue(unsigned int i, unsigned int j, osg::Vec3& value) const
370{
371    if (!_tf) return false;
372
373    const osg::Vec4& v = _tf->getPixelValue(i);
374    value.x() = v.x();
375    value.y() = v.y();
376    value.z() = v.z();
377
378    return true;
379}
380
381bool ContourLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) const
382{
383    if (!_tf) return false;
384
385    value = _tf->getPixelValue(i);
386
387    return true;
388}
389
390void ContourLayer::dirty()
391{
392    if (getImage()) getImage()->dirty();
393}
394
395void ContourLayer::setModifiedCount(unsigned int value)
396{
397    if (getImage()) getImage()->setModifiedCount(value);
398}
399
400unsigned int ContourLayer::getModifiedCount() const
401{
402    if (!getImage()) return 0;
403    else return getImage()->getModifiedCount();
404}
405
406
407/////////////////////////////////////////////////////////////////////////////
408//
409// HeightFieldLayer
410//
411HeightFieldLayer::HeightFieldLayer(osg::HeightField* hf):
412    _modifiedCount(0),
413    _heightField(hf)
414{
415}
416
417HeightFieldLayer::HeightFieldLayer(const HeightFieldLayer& hfLayer,const osg::CopyOp& copyop):
418    Layer(hfLayer,copyop),
419    _modifiedCount(0),
420    _heightField(hfLayer._heightField)
421{
422    if (_heightField.valid()) ++_modifiedCount;
423}
424
425
426void HeightFieldLayer::setHeightField(osg::HeightField* hf)
427{
428    _heightField = hf;
429    dirty();
430}
431
432
433
434bool HeightFieldLayer::transform(float offset, float scale)
435{
436    if (!_heightField) return false;
437
438    osg::FloatArray* heights = _heightField->getFloatArray();
439    if (!heights) return false;
440
441    OSG_INFO<<"HeightFieldLayer::transform("<<offset<<","<<scale<<")"<<std::endl;;
442
443    for(osg::FloatArray::iterator itr = heights->begin();
444        itr != heights->end();
445        ++itr)
446    {
447        *itr = offset + (*itr) * scale;
448    }
449
450    dirty();
451
452    return true;
453}
454
455bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, float& value) const
456{
457    value = _heightField->getHeight(i,j);
458    return true;
459}
460
461bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, osg::Vec2& value) const
462{
463    value.x() = _heightField->getHeight(i,j);
464    value.y() = _defaultValue.y();
465    return true;
466}
467
468bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, osg::Vec3& value) const
469{
470    value.x() = _heightField->getHeight(i,j);
471    value.y() = _defaultValue.y();
472    value.z() = _defaultValue.z();
473    return true;
474}
475
476bool HeightFieldLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) const
477{
478    value.x() = _heightField->getHeight(i,j);
479    value.y() = _defaultValue.y();
480    value.z() = _defaultValue.z();
481    value.w() = _defaultValue.w();
482    return true;
483}
484
485void HeightFieldLayer::dirty()
486{
487    ++_modifiedCount;
488}
489
490void HeightFieldLayer::setModifiedCount(unsigned int value)
491{
492    _modifiedCount = value;
493}
494
495unsigned int HeightFieldLayer::getModifiedCount() const
496{
497    return _modifiedCount;
498}
499
500/////////////////////////////////////////////////////////////////////////////
501//
502// ProxyLayer
503//
504ProxyLayer::ProxyLayer()
505{
506}
507
508ProxyLayer::ProxyLayer(const ProxyLayer& proxyLayer,const osg::CopyOp& copyop):
509    Layer(proxyLayer,copyop)
510{
511}
512
513ProxyLayer::~ProxyLayer()
514{
515}
516
517void ProxyLayer::setFileName(const std::string& filename)
518{
519    _filename = filename;
520    if (_implementation.valid())
521    {
522        _implementation->setFileName(_filename);
523    }
524}
525
526unsigned int ProxyLayer::getNumColumns() const
527{
528    if (_implementation.valid()) return _implementation->getNumColumns();
529    else return 0;
530}
531
532unsigned int ProxyLayer::getNumRows() const
533{
534    if (_implementation.valid()) return _implementation->getNumRows();
535    else return 0;
536}
537
538bool ProxyLayer::transform(float offset, float scale)
539{
540    if (_implementation.valid()) return _implementation->transform(offset,scale);
541    else return false;
542}
543
544bool ProxyLayer::getValue(unsigned int i, unsigned int j, float& value) const
545{
546    if (_implementation.valid()) return _implementation->getValue(i,j,value);
547    else return false;
548}
549
550bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec2& value) const
551{
552    if (_implementation.valid()) return _implementation->getValue(i,j,value);
553    else return false;
554}
555
556bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec3& value) const
557{
558    if (_implementation.valid()) return _implementation->getValue(i,j,value);
559    else return false;
560}
561
562bool ProxyLayer::getValue(unsigned int i, unsigned int j, osg::Vec4& value) const
563{
564    if (_implementation.valid()) return _implementation->getValue(i,j,value);
565    else return false;
566}
567
568void ProxyLayer::dirty()
569{
570    if (_implementation.valid()) _implementation->dirty();
571}
572
573void ProxyLayer::setModifiedCount(unsigned int value)
574{
575    if (_implementation.valid()) _implementation->setModifiedCount(value);
576}
577
578unsigned int ProxyLayer::getModifiedCount() const
579{
580    return _implementation.valid() ? _implementation->getModifiedCount() : 0;
581}
582
583
584osg::BoundingSphere ProxyLayer::computeBound(bool treatAsElevationLayer) const
585{
586    if (_implementation.valid()) return _implementation->computeBound(treatAsElevationLayer);
587    else return osg::BoundingSphere();
588}
589
590
591
592/////////////////////////////////////////////////////////////////////////////
593//
594// CompositeLayer
595//
596CompositeLayer::CompositeLayer()
597{
598}
599
600CompositeLayer::CompositeLayer(const CompositeLayer& compositeLayer,const osg::CopyOp& copyop):
601    Layer(compositeLayer,copyop)
602{
603}
604
605
606void CompositeLayer::clear()
607{
608    _layers.clear();
609}
610
611void CompositeLayer::setCompoundName(unsigned int i, const std::string& compoundname)
612{
613    std::string setname;
614    std::string filename;
615    extractSetNameAndFileName(compoundname, setname, filename);
616
617    _layers[i].setname = setname;
618    _layers[i].filename = filename;
619}
620
621std::string CompositeLayer::getCompoundName(unsigned int i) const
622{
623    return createCompoundSetNameAndFileName(_layers[i].setname, _layers[i].filename);
624}
625
626void CompositeLayer::addLayer(const std::string& compoundname)
627{
628    std::string setname;
629    std::string filename;
630    extractSetNameAndFileName(compoundname, setname, filename);
631
632    _layers.push_back(CompoundNameLayer(setname,filename,0));
633}
634
635void CompositeLayer::addLayer(const std::string& setname, const std::string& filename)
636{
637    _layers.push_back(CompoundNameLayer(setname,filename,0));
638}
639
640/////////////////////////////////////////////////////////////////////////////
641//
642// SwitchLayer
643//
644SwitchLayer::SwitchLayer():
645    _activeLayer(-1)
646{
647}
648
649SwitchLayer::SwitchLayer(const SwitchLayer& switchLayer,const osg::CopyOp& copyop):
650    CompositeLayer(switchLayer,copyop),
651    _activeLayer(switchLayer._activeLayer)
652{
653}
Note: See TracBrowser for help on using the browser.