Discrete legend: Fix of discrete texture and end colors

Added changes from ChList 2078-2081
This commit is contained in:
Jacob Støren 2015-05-13 20:51:44 +02:00
parent 8c12c8bcf7
commit 7a2640b52f
7 changed files with 52 additions and 42 deletions

View File

@ -375,16 +375,16 @@ ref<Color3ubArray> ScalarMapper::interpolateColorArray(const Color3ubArray& colo
ref<Color3ubArray> colors = new Color3ubArray;
colors->reserve(targetColorCount);
const uint inputLevelCount = inputColorCount - 1;
const uint outputLevelCount = targetColorCount - 1;
const uint inputColorsMaxIdx = inputColorCount - 1;
const uint outputColorsMaxIdx = targetColorCount - 1;
uint outputLevelIdx;
for (outputLevelIdx = 0; outputLevelIdx < outputLevelCount; outputLevelIdx++)
for (outputLevelIdx = 0; outputLevelIdx < outputColorsMaxIdx; outputLevelIdx++)
{
double dblInputLevelIndex = inputLevelCount * (outputLevelIdx / static_cast<double>(outputLevelCount));
double dblInputLevelIndex = inputColorsMaxIdx * (outputLevelIdx / static_cast<double>(outputColorsMaxIdx));
const uint inputLevelIndex = static_cast<uint>(dblInputLevelIndex);
CVF_ASSERT(inputLevelIndex < inputLevelCount);
CVF_ASSERT(inputLevelIndex < inputColorsMaxIdx);
double t = dblInputLevelIndex - inputLevelIndex;
CVF_ASSERT(t >= 0 && t <= 1.0);

View File

@ -54,38 +54,37 @@ ScalarMapperDiscreteLinear::ScalarMapperDiscreteLinear()
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Vec2f ScalarMapperDiscreteLinear::mapToTextureCoord(double scalarValue) const
{
double discVal = discretize(scalarValue, m_sortedLevels);
return ScalarMapperRangeBased::mapToTextureCoord(discVal);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Color3ub ScalarMapperDiscreteLinear::mapToColor(double scalarValue) const
{
double discVal = discretize(scalarValue, m_sortedLevels);
return ScalarMapperRangeBased::mapToColor(discVal);
double discVal = ScalarMapperDiscreteLinear::discretizeToLevelBelow(scalarValue, m_sortedLevels);
std::set<double>::reverse_iterator it = m_sortedLevels.rbegin();
it++;
double levelUnderMax = *it;
double normDiscVal = normalizedValue(discVal);
double normSemiMaxVal = normalizedValue(levelUnderMax);
double adjustedNormVal = 0;
if (normSemiMaxVal != 0) adjustedNormVal = cvf::Math::clamp(normDiscVal/normSemiMaxVal, 0.0, 1.0);
return colorFromUserColorGradient(adjustedNormVal);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double ScalarMapperDiscreteLinear::discretize(double scalarValue, const std::set<double>& sortedLevels)
double ScalarMapperDiscreteLinear::discretizeToLevelBelow(double scalarValue, const std::set<double>& sortedLevels)
{
std::set<double>::iterator it;
it = sortedLevels.upper_bound(scalarValue);
if (it == sortedLevels.begin()) return (*it);
if (it == sortedLevels.end()) return (*sortedLevels.rbegin());
double upperValue = *it;
it--;
double lowerValue = *it;
return 0.5 * (upperValue + lowerValue);
return lowerValue;
}
//--------------------------------------------------------------------------------------------------

View File

@ -55,13 +55,12 @@ public:
// Scalarmapper interface implementation
virtual Vec2f mapToTextureCoord(double scalarValue) const;
virtual Color3ub mapToColor(double scalarValue) const;
virtual double normalizedValue( double domainValue ) const;
virtual double domainValue( double normalizedPosition ) const;
private:
static double discretize(double scalarValue, const std::set<double>& sortedLevels);
static double discretizeToLevelBelow(double scalarValue, const std::set<double>& sortedLevels);
friend class ScalarMapperDiscreteLog;
};

View File

@ -58,24 +58,25 @@ ScalarMapperDiscreteLog::ScalarMapperDiscreteLog()
m_decadeLevelCount = 2;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Vec2f ScalarMapperDiscreteLog::mapToTextureCoord(double scalarValue) const
{
double discVal = ScalarMapperDiscreteLinear::discretize(scalarValue, m_sortedLevels);
return ScalarMapperRangeBased::mapToTextureCoord(discVal);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Color3ub ScalarMapperDiscreteLog::mapToColor(double scalarValue) const
{
double discVal = ScalarMapperDiscreteLinear::discretize(scalarValue, m_sortedLevels);
return ScalarMapperRangeBased::mapToColor(discVal);
double discVal = ScalarMapperDiscreteLinear::discretizeToLevelBelow(scalarValue, m_sortedLevels);
std::set<double>::reverse_iterator it = m_sortedLevels.rbegin();
it++;
double levelUnderMax = *it;
double normDiscVal = normalizedValue(discVal);
double normSemiMaxVal = normalizedValue(levelUnderMax);
double adjustedNormVal = 0;
if (normSemiMaxVal != 0) adjustedNormVal = normDiscVal/normSemiMaxVal;
return colorFromUserColorGradient(adjustedNormVal);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -53,7 +53,7 @@ public:
ScalarMapperDiscreteLog();
// Implementing the Scalarmapper interface
virtual Vec2f mapToTextureCoord(double scalarValue) const;
virtual Color3ub mapToColor(double scalarValue) const;
virtual double normalizedValue( double domainValue ) const;
virtual double domainValue( double normalizedPosition ) const;

View File

@ -58,9 +58,9 @@ ScalarMapperRangeBased::ScalarMapperRangeBased()
m_textureSize(2048), // Large enough, I guess and a power of two
m_adjustLevels(true)
{
m_colors.resize(m_textureSize);
m_colors.setAll(Color3ub::WHITE);
setColors(ScalarMapper::NORMAL);
m_interpolatedUserGradientColors.resize(m_textureSize);
m_interpolatedUserGradientColors.setAll(Color3ub::WHITE);
setColors(*normalColorTableArray(13));
}
@ -83,7 +83,7 @@ void ScalarMapperRangeBased::setRange(double min, double max)
//--------------------------------------------------------------------------------------------------
void ScalarMapperRangeBased::setColors(const Color3ubArray& colorArray)
{
m_colors = *interpolateColorArray(colorArray, m_textureSize);
m_interpolatedUserGradientColors = *interpolateColorArray(colorArray, m_textureSize);
}
//--------------------------------------------------------------------------------------------------
@ -137,10 +137,7 @@ Vec2f ScalarMapperRangeBased::mapToTextureCoord(double scalarValue) const
//--------------------------------------------------------------------------------------------------
Color3ub ScalarMapperRangeBased::mapToColor(double scalarValue) const
{
size_t colorIdx = static_cast<size_t>(normalizedValue(scalarValue) * (m_textureSize - 1));
CVF_TIGHT_ASSERT(colorIdx < m_colors.size());
return m_colors[colorIdx];
return colorFromUserColorGradient(normalizedValue(scalarValue));
}
//--------------------------------------------------------------------------------------------------
@ -170,7 +167,7 @@ bool ScalarMapperRangeBased::updateTexture(TextureImage* image) const
uint ic;
for (ic = 0; ic < m_textureSize; ic++)
{
const Color4ub clr(m_colors[ic], 255);
const Color4ub clr(mapToColor(domainValue(((double)ic)/(m_textureSize-1))), 255);
image->setPixel(ic, 0, clr);
}
@ -279,5 +276,18 @@ void ScalarMapperRangeBased::majorTickValues( std::vector<double>* domainValues)
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Color3ub ScalarMapperRangeBased::colorFromUserColorGradient(double normalizedValue) const
{
CVF_TIGHT_ASSERT(0.0 <= normalizedValue && normalizedValue <= 1.0);
size_t colorIdx = static_cast<size_t>(normalizedValue * (m_textureSize - 1));
CVF_TIGHT_ASSERT(colorIdx < m_interpolatedUserGradientColors.size());
return m_interpolatedUserGradientColors[colorIdx];
}
} // namespace cvf

View File

@ -67,6 +67,7 @@ public:
protected:
virtual void rangeUpdated() {}; //< Called when the range is changed. Subclasses can reimplment to recalculate cached values
Color3ub colorFromUserColorGradient(double normalizedValue) const;
double m_rangeMin;
double m_rangeMax;
@ -81,7 +82,7 @@ private:
bool m_adjustLevels; //< Toggles wether to round tick positions to nice numbers
std::set<double> m_userDefinedLevelValues;
Color3ubArray m_colors;
Color3ubArray m_interpolatedUserGradientColors; //< Table of smooth interpolated colors as a result of the users color request
uint m_textureSize; // The size of texture that updateTexture() will produce.
};