Refactored osg::TransferFunction1D to use an std::map internally which is kept in sync with the actual osg::Image that is passed to the GPU.
Added .osg support for osg::TransferFunction1D. Updated wrappers
This commit is contained in:
@@ -38,16 +38,16 @@ class OSG_EXPORT TransferFunction : public osg::Object
|
||||
|
||||
META_Object(osg, TransferFunction)
|
||||
|
||||
/** Get the image that is used for passing the transfer function data to the GPU.*/
|
||||
osg::Image* getImage() { return _image.get(); }
|
||||
|
||||
/** Get the const image that is used for passing the transfer function data to the GPU.*/
|
||||
const osg::Image* getImage() const { return _image.get(); }
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~TransferFunction();
|
||||
|
||||
typedef std::vector<osg::Vec4> Colors;
|
||||
|
||||
Colors _colors;
|
||||
osg::ref_ptr<osg::Image> _image;
|
||||
};
|
||||
|
||||
@@ -62,48 +62,65 @@ class OSG_EXPORT TransferFunction1D : public osg::TransferFunction
|
||||
TransferFunction1D(const TransferFunction1D& tf, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
||||
|
||||
META_Object(osg, TransferFunction1D)
|
||||
|
||||
void setInputRange(float minimum, float maximum);
|
||||
|
||||
void setMinimum(float value) { _minimum = value; }
|
||||
float getMinimum() const { return _minimum; }
|
||||
/** Get the mnimum transfer function value.*/
|
||||
float getMinimum() const { return _colorMap.empty() ? 0.0f : _colorMap.begin()->first; }
|
||||
|
||||
void setMaximum(float value) { _maximum = value; }
|
||||
float getMaximum() const { return _maximum; }
|
||||
/** Get the maximum transfer function value.*/
|
||||
float getMaximum() const { return _colorMap.empty() ? 0.0f : _colorMap.rbegin()->first; }
|
||||
|
||||
void allocate(unsigned int numX);
|
||||
/** allocate the osg::Image with specified dimension. The Image tracks the color map, and is used to represent the
|
||||
* transfer function when download to GPU.*/
|
||||
void allocate(unsigned int numImageCells);
|
||||
|
||||
/** Clear the whole range to just represet a single color.*/
|
||||
void clear(const osg::Vec4& color = osg::Vec4(1.0f,1.0f,1.0f,1.0f));
|
||||
|
||||
unsigned int getNumberCellsX() const { return _colors.size(); }
|
||||
|
||||
void setValue(unsigned int i, const osg::Vec4& color) { _colors[i] = color; if (_image.valid()) _image->dirty(); }
|
||||
const osg::Vec4& getValue(unsigned int i) const { return _colors[i]; }
|
||||
|
||||
osg::Vec4 getInterpolatedValue(float v) const
|
||||
/** Get pixel value from the image. */
|
||||
osg::Vec4 getPixelValue(unsigned int i) const
|
||||
{
|
||||
float iPos = (v-_minimum)*float(_colors.size()-1)/(_maximum-_minimum);
|
||||
if (iPos<0.0) return _colors[0];
|
||||
if (iPos>float(_colors.size()-1)) return _colors[_colors.size()-1];
|
||||
|
||||
unsigned int iLower = (unsigned int)(iPos);
|
||||
unsigned int iUpper = iLower+1;
|
||||
if (iUpper>=_colors.size()) return _colors[iLower];
|
||||
|
||||
float r = iPos-floorf(iLower);
|
||||
const osg::Vec4& cLower = _colors[iLower];
|
||||
const osg::Vec4& cUpper = _colors[iUpper];
|
||||
return cLower + (cUpper-cLower)*r;
|
||||
if (_image.valid() && i<static_cast<unsigned int>(_image->s()))
|
||||
{
|
||||
return *reinterpret_cast<osg::Vec4*>(_image->data(i));
|
||||
}
|
||||
else
|
||||
{
|
||||
return osg::Vec4(1.0f,1.0f,1.0f,1.0f);
|
||||
}
|
||||
}
|
||||
|
||||
/** Get the number of image cells that are assigned to the represent the transfer function when download to the GPU.*/
|
||||
unsigned int getNumberImageCells() const { return _image.valid() ? _image->s() : 0; }
|
||||
|
||||
/** Set the color for a specified transfer function value.
|
||||
* updateImage defaults to true, and tells the setColor function to update the associate osg::Image that
|
||||
* tracks the color map. Pass in false as the updateImage parameter if you are setting up many values
|
||||
* at once to avoid recomputating og the image data, then once all setColor calls are made explictly call
|
||||
* updateImage() to bring the osg::Image back into sync with the color map. */
|
||||
void setColor(float v, const osg::Vec4& color, bool updateImage=true);
|
||||
|
||||
typedef std::map<float, osg::Vec4> ValueMap;
|
||||
void assign(const ValueMap& vcm, bool updateMinMaxRange);
|
||||
/** Get the color for a specified transfer function value, interpolating the value if no exact match is found.*/
|
||||
osg::Vec4 getColor(float v) const;
|
||||
|
||||
typedef std::map<float, osg::Vec4> ColorMap;
|
||||
|
||||
/** Get the color map that stores the mapping between the the tranfser function value and the colour it maps to.*/
|
||||
ColorMap& getColorMap() { return _colorMap; }
|
||||
|
||||
/** Get the const color map that stores the mapping between the the tranfser function value and the colour it maps to.*/
|
||||
const ColorMap& getColorMap() const { return _colorMap; }
|
||||
|
||||
/** Assign a color map and automatically update the image to make sure they are in sync.*/
|
||||
void assign(const ColorMap& vcm);
|
||||
|
||||
/** Manually update the associate osg::Image to represent the colors assigned in the color map.*/
|
||||
void updateImage();
|
||||
|
||||
protected:
|
||||
|
||||
float _minimum;
|
||||
float _maximum;
|
||||
|
||||
ColorMap _colorMap;
|
||||
|
||||
void assignToImage(float lower_v, const osg::Vec4& lower_c, float upper_v, const osg::Vec4& upper_c);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user