2009-09-11 21:08:49 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
2007-03-10 19:53:31 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <osg/TransferFunction>
|
|
|
|
|
2008-09-13 17:09:51 +08:00
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/io_utils>
|
|
|
|
|
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TransferFunction base class
|
|
|
|
//
|
2008-02-22 19:52:23 +08:00
|
|
|
TransferFunction::TransferFunction()
|
2007-03-10 19:53:31 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-02-01 20:38:55 +08:00
|
|
|
TransferFunction::TransferFunction(const TransferFunction& tf, const CopyOp& copyop):
|
|
|
|
Object(tf,copyop)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-03-10 19:53:31 +08:00
|
|
|
TransferFunction::~TransferFunction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TransferFunction1D class
|
|
|
|
//
|
|
|
|
TransferFunction1D::TransferFunction1D()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-02-01 20:38:55 +08:00
|
|
|
TransferFunction1D::TransferFunction1D(const TransferFunction1D& tf, const CopyOp& copyop):
|
2009-02-02 22:43:27 +08:00
|
|
|
TransferFunction(tf,copyop)
|
2009-02-01 20:38:55 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
allocate(tf.getNumberImageCells());
|
|
|
|
assign(_colorMap);
|
2007-03-10 19:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransferFunction1D::allocate(unsigned int numX)
|
|
|
|
{
|
2007-04-02 21:06:59 +08:00
|
|
|
_image = new osg::Image;
|
2009-02-02 22:43:27 +08:00
|
|
|
_image->allocateImage(numX,1,1,GL_RGBA, GL_FLOAT);
|
|
|
|
if (!_colorMap.empty()) assign(_colorMap);
|
2007-03-10 19:53:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TransferFunction1D::clear(const osg::Vec4& color)
|
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
ColorMap newColours;
|
|
|
|
newColours[getMinimum()] = color;
|
|
|
|
newColours[getMaximum()] = color;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
assign(newColours);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransferFunction1D::assignToImage(float lower_v, const osg::Vec4& lower_c, float upper_v, const osg::Vec4& upper_c)
|
|
|
|
{
|
|
|
|
float minimum = _colorMap.begin()->first;
|
|
|
|
float maximum = _colorMap.rbegin()->first;
|
|
|
|
float endPos = float(getNumberImageCells()-1);
|
|
|
|
float multiplier = endPos/(maximum - minimum);
|
|
|
|
osg::Vec4* imageData = reinterpret_cast<osg::Vec4*>(_image->data());
|
2009-09-11 21:08:49 +08:00
|
|
|
|
|
|
|
float lower_iPos = (lower_v - minimum)*multiplier;
|
2009-02-02 22:43:27 +08:00
|
|
|
float upper_iPos = (upper_v - minimum)*multiplier;
|
|
|
|
|
|
|
|
float start_iPos = ceilf(lower_iPos);
|
|
|
|
if (start_iPos<0.0f) start_iPos=0.0f;
|
|
|
|
if (start_iPos>endPos) return;
|
|
|
|
|
|
|
|
float end_iPos = floorf(upper_iPos);
|
|
|
|
if (end_iPos<0.0f) return;
|
|
|
|
if (end_iPos>endPos) end_iPos=endPos;
|
|
|
|
|
2009-09-11 21:08:49 +08:00
|
|
|
//osg::notify(osg::NOTICE)<<"TransferFunction1D::assignToImage[lower_v="<<lower_v<<", lower_c="<<lower_c<<", upper_v="<<upper_v<<" upper_c="<<upper_c<<std::endl;
|
|
|
|
//osg::notify(osg::NOTICE)<<" lower_iPos="<<lower_iPos<<" start_iPpos="<<start_iPos<<std::endl;
|
|
|
|
//osg::notify(osg::NOTICE)<<" upper_iPos="<<upper_iPos<<" end_iPpos="<<end_iPos<<std::endl;
|
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
Vec4 delta_c = (upper_c-lower_c)/(upper_iPos-lower_iPos);
|
|
|
|
unsigned int i=static_cast<unsigned int>(start_iPos);
|
|
|
|
for(float iPos=start_iPos;
|
2009-09-11 21:08:49 +08:00
|
|
|
iPos<=end_iPos;
|
2009-02-02 22:43:27 +08:00
|
|
|
++iPos, ++i)
|
2007-03-10 19:53:31 +08:00
|
|
|
{
|
2009-09-11 21:08:49 +08:00
|
|
|
imageData[i] = lower_c + delta_c*(iPos-lower_iPos);
|
|
|
|
//osg::notify(osg::NOTICE)<<" imageData["<<i<<"] = "<<imageData[i]<<std::endl;
|
2007-03-10 19:53:31 +08:00
|
|
|
}
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
_image->dirty();
|
2007-03-10 19:53:31 +08:00
|
|
|
}
|
2008-09-13 17:09:51 +08:00
|
|
|
|
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
void TransferFunction1D::setColor(float v, const osg::Vec4& color, bool updateImage)
|
2008-09-13 17:09:51 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
if (!updateImage)
|
|
|
|
{
|
|
|
|
_colorMap[v] = color;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_image) allocate(1024);
|
2009-09-11 21:08:49 +08:00
|
|
|
|
|
|
|
if (_colorMap.empty() || v<getMinimum() || v>getMaximum())
|
2009-02-02 22:43:27 +08:00
|
|
|
{
|
|
|
|
_colorMap[v] = color;
|
|
|
|
|
|
|
|
assign(_colorMap);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_colorMap[v] = color;
|
|
|
|
|
|
|
|
ColorMap::iterator itr = _colorMap.find(v);
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
if (itr != _colorMap.begin())
|
|
|
|
{
|
|
|
|
ColorMap::iterator previous_itr = itr;
|
|
|
|
--previous_itr;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
|
|
|
assignToImage(previous_itr->first, previous_itr->second, v, color);
|
2009-02-02 22:43:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ColorMap::iterator next_itr = itr;
|
|
|
|
++next_itr;
|
|
|
|
|
|
|
|
if (next_itr != _colorMap.end())
|
2008-09-13 17:09:51 +08:00
|
|
|
{
|
2009-09-11 21:08:49 +08:00
|
|
|
assignToImage(v, color, next_itr->first, next_itr->second);
|
2008-09-13 17:09:51 +08:00
|
|
|
}
|
2009-02-02 22:43:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
osg::Vec4 TransferFunction1D::getColor(float v) const
|
|
|
|
{
|
|
|
|
if (_colorMap.empty()) return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
|
|
|
|
if (_colorMap.size()==1) return _colorMap.begin()->second;
|
|
|
|
|
|
|
|
if (v <= _colorMap.begin()->first) return _colorMap.begin()->second;
|
|
|
|
if (v >= _colorMap.rbegin()->first) return _colorMap.rbegin()->second;
|
|
|
|
|
|
|
|
// need to implement
|
|
|
|
std::pair<ColorMap::const_iterator, ColorMap::const_iterator> range = _colorMap.equal_range(v);
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
// we have an identical match
|
|
|
|
if (v == range.first->first) return range.first->second;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
// range.first will be at the next element after v, so move it before.
|
|
|
|
--range.first;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
float vBefore = range.first->first;
|
|
|
|
const osg::Vec4& cBefore = range.first->second;
|
|
|
|
|
|
|
|
float vAfter = range.second->first;
|
|
|
|
const osg::Vec4& cAfter = range.second->second;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
float r = (v-vBefore)/(vAfter-vBefore);
|
|
|
|
|
|
|
|
return cBefore*(1.0f-r) + cAfter*r;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TransferFunction1D::assign(const ColorMap& newColours)
|
|
|
|
{
|
|
|
|
_colorMap = newColours;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
updateImage();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TransferFunction1D::updateImage()
|
|
|
|
{
|
|
|
|
if (_colorMap.empty()) return;
|
|
|
|
|
|
|
|
if (!_image || _image->data()==0) allocate(1024);
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
osg::Vec4* imageData = reinterpret_cast<osg::Vec4*>(_image->data());
|
|
|
|
|
|
|
|
if (_colorMap.size()==1)
|
2008-09-13 17:09:51 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
osg::Vec4 color = _colorMap.begin()->second;
|
|
|
|
|
|
|
|
for(int i=0; i<_image->s(); ++i)
|
2008-09-13 17:09:51 +08:00
|
|
|
{
|
2009-02-02 22:43:27 +08:00
|
|
|
imageData[i] = color;
|
2008-09-13 17:09:51 +08:00
|
|
|
}
|
2009-01-30 23:04:07 +08:00
|
|
|
_image->dirty();
|
2008-09-13 17:09:51 +08:00
|
|
|
return;
|
|
|
|
}
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
ColorMap::const_iterator lower_itr = _colorMap.begin();
|
|
|
|
ColorMap::const_iterator upper_itr = lower_itr;
|
2008-09-13 17:09:51 +08:00
|
|
|
++upper_itr;
|
2009-09-11 21:08:49 +08:00
|
|
|
|
2008-09-13 17:09:51 +08:00
|
|
|
for(;
|
2009-02-02 22:43:27 +08:00
|
|
|
upper_itr != _colorMap.end();
|
2008-09-13 17:09:51 +08:00
|
|
|
++upper_itr)
|
|
|
|
{
|
|
|
|
float lower_v = lower_itr->first;
|
|
|
|
const osg::Vec4& lower_c = lower_itr->second;
|
|
|
|
float upper_v = upper_itr->first;
|
|
|
|
const osg::Vec4& upper_c = upper_itr->second;
|
|
|
|
|
2009-02-02 22:43:27 +08:00
|
|
|
assignToImage(lower_v, lower_c, upper_v, upper_c);
|
2009-09-11 21:08:49 +08:00
|
|
|
|
|
|
|
lower_itr = upper_itr;
|
2008-09-13 17:09:51 +08:00
|
|
|
}
|
2009-01-30 23:04:07 +08:00
|
|
|
|
|
|
|
_image->dirty();
|
2008-09-13 17:09:51 +08:00
|
|
|
}
|