Merge topic 'simplify-field-to-colors-interface'

3a812b04 Make default ColorTable preset
3391e5d2 Basic interface changes to FieldToColors
a3b2c393 Remove default constructor for ColorTable
cb8a05c7 Add a preset enum to ColorTable
97a24559 Make LAB the default color space

Acked-by: Kitware Robot <kwrobot@kitware.com>
Acked-by: Robert Maynard <robert.maynard@kitware.com>
Merge-request: !1133
This commit is contained in:
Kenneth Moreland 2018-04-02 22:23:51 +00:00 committed by Kitware Robot
commit eca65146b3
10 changed files with 139 additions and 61 deletions

@ -31,10 +31,26 @@ namespace cont
namespace detail
{
bool loadColorTablePreset(vtkm::cont::ColorTable::Preset preset, vtkm::cont::ColorTable& table);
std::set<std::string> GetPresetNames();
bool loadColorTablePreset(std::string name, vtkm::cont::ColorTable& table);
}
//----------------------------------------------------------------------------
ColorTable::ColorTable(vtkm::cont::ColorTable::Preset preset)
: Impl(std::make_shared<detail::ColorTableInternals>())
{
const bool loaded = this->LoadPreset(preset);
if (!loaded)
{ //if we failed to load the requested color table, call SetColorSpace
//so that the internal host side cache is constructed and we leave
//the constructor in a valid state. We use RGB as it is the default
//when the no parameter constructor is called
this->SetColorSpace(ColorSpace::LAB);
}
this->AddSegmentAlpha(this->Impl->TableRange.Min, 1.0f, this->Impl->TableRange.Max, 1.0f);
}
//----------------------------------------------------------------------------
ColorTable::ColorTable(const std::string& name)
: Impl(std::make_shared<detail::ColorTableInternals>())
@ -45,7 +61,7 @@ ColorTable::ColorTable(const std::string& name)
//so that the internal host side cache is constructed and we leave
//the constructor in a valid state. We use RGB as it is the default
//when the no parameter constructor is called
this->SetColorSpace(ColorSpace::RGB);
this->SetColorSpace(ColorSpace::LAB);
}
this->AddSegmentAlpha(this->Impl->TableRange.Min, 1.0f, this->Impl->TableRange.Max, 1.0f);
}
@ -88,6 +104,12 @@ ColorTable::~ColorTable()
{
}
//----------------------------------------------------------------------------
bool ColorTable::LoadPreset(vtkm::cont::ColorTable::Preset preset)
{
return detail::loadColorTablePreset(preset, *this);
}
//----------------------------------------------------------------------------
std::set<std::string> ColorTable::GetPresets() const
{

@ -108,7 +108,36 @@ class VTKM_CONT_EXPORT ColorTable
std::shared_ptr<detail::ColorTableInternals> Impl;
public:
/// Construct a color table from a preset color table
// Note: these are in flux and will change soon.
enum struct Preset
{
DEFAULT,
VIRIDIS,
COOL_TO_WARM,
COOL_TO_WARN_EXTENDED,
COLD_AND_HOT,
INFERNO,
BLACK_BODY_RADIATION,
SAMSEL_FIRE,
LINEAR_YGB,
BLACK_BLUE_AND_WHITE,
LINEAR_GREEN,
X_RAY,
JET,
RAINBOW_DESATURATED,
RAINBOW
};
/// \brief Construct a color table from a preset
///
/// Constructs a color table from a given preset, which might include a NaN color.
/// The alpha table will have 2 entries of alpha = 1.0 with linear interpolation
///
/// Note: these are a select set of the presets you can get by providing a string identifier.
///
ColorTable(vtkm::cont::ColorTable::Preset preset = vtkm::cont::ColorTable::Preset::DEFAULT);
/// \brief Construct a color table from a preset color table
///
/// Constructs a color table from a given preset, which might include a NaN color.
/// The alpha table will have 2 entries of alpha = 1.0 with linear interpolation
@ -131,20 +160,20 @@ public:
/// "Jet"
/// "Rainbow"
///
ColorTable(const std::string& name);
explicit ColorTable(const std::string& name);
/// Construct a color table with a zero positions, and an invalid range
///
/// Note: The color table will have 0 entries
/// Note: The alpha table will have 0 entries
ColorTable(ColorSpace space = ColorSpace::RGB);
explicit ColorTable(ColorSpace space);
/// Construct a color table with a 2 positions
///
/// Note: The color table will have 2 entries of rgb = {1.0,1.0,1.0}
/// Note: The alpha table will have 2 entries of alpha = 1.0 with linear
/// interpolation
ColorTable(const vtkm::Range& range, ColorSpace space = ColorSpace::RGB);
ColorTable(const vtkm::Range& range, ColorSpace space = ColorSpace::LAB);
/// Construct a color table with 2 positions
//
@ -153,7 +182,7 @@ public:
ColorTable(const vtkm::Range& range,
const vtkm::Vec<float, 3>& rgb1,
const vtkm::Vec<float, 3>& rgb2,
ColorSpace space = ColorSpace::RGB);
ColorSpace space = ColorSpace::LAB);
/// Construct color and alpha and table with 2 positions
///
@ -161,13 +190,18 @@ public:
ColorTable(const vtkm::Range& range,
const vtkm::Vec<float, 4>& rgba1,
const vtkm::Vec<float, 4>& rgba2,
ColorSpace space = ColorSpace::RGB);
ColorSpace space = ColorSpace::LAB);
~ColorTable();
bool LoadPreset(vtkm::cont::ColorTable::Preset preset);
/// Returns the name of all preset color tables
///
/// This list will include all presets defined in vtkm::cont::ColorTable::Preset and could
/// include extras as well.
///
std::set<std::string> GetPresets() const;
/// Load a preset color table
@ -234,8 +268,8 @@ public:
void SetNaNColor(const vtkm::Vec<float, 3>& c);
const vtkm::Vec<float, 3>& GetNaNColor() const;
/// Remove all existing all values in both color and alpha tables
/// doesn't remove the clamping, below, and above range state or colors
/// Remove all existing values in both color and alpha tables.
/// Does not remove the clamping, below, and above range state or colors.
void Clear();
/// Remove only color table values

@ -36,7 +36,7 @@ namespace detail
struct ColorTableInternals
{
ColorSpace CSpace = ColorSpace::RGB;
ColorSpace CSpace = ColorSpace::LAB;
vtkm::Range TableRange = { 1.0, 0.0 };
//Host side version of the ColorTableBase. This holds data such as:

@ -318,7 +318,7 @@ static constexpr ColorTable<21> LinearGreen = {
}
};
static constexpr ColorTable<256> Virdis =
static constexpr ColorTable<256> Viridis =
{
vtkm::cont::ColorSpace::LAB,
{ 0.000000, 0.267004, 0.004874, 0.329415, 0.003922, 0.268510, 0.009605, 0.335427, 0.007843,
@ -521,10 +521,10 @@ void loadBlackBlueWhite(vtkm::cont::ColorTable& table)
table.SetColorSpace(BlackBlueWhite.space);
table.SetNaNColor(vtkm::Vec<float, 3>{ 1.0f, 1.0f, 0.0f });
}
void loadVirdis(vtkm::cont::ColorTable& table)
void loadViridis(vtkm::cont::ColorTable& table)
{
table.FillColorTableFromDataPointer(Virdis.size, Virdis.values);
table.SetColorSpace(Virdis.space);
table.FillColorTableFromDataPointer(Viridis.size, Viridis.values);
table.SetColorSpace(Viridis.space);
table.SetNaNColor(vtkm::Vec<float, 3>{ 1.0f, 0.0f, 0.0f });
}
void loadLinearGreen(vtkm::cont::ColorTable& table)
@ -548,26 +548,34 @@ struct LoadColorTablePresetCommand
{
using FunctionType = void (*)(vtkm::cont::ColorTable& table);
vtkm::cont::ColorTable::Preset id;
std::string name;
FunctionType function;
};
constexpr int numberOfPresets = 14;
constexpr int numberOfPresets = 15;
struct LoadColorTablePresetCommand presets[numberOfPresets] = {
{ "cool to warm", loadCoolToWarm },
{ "black-body radiation", loadBlackBody },
{ "samsel fire", loadSamselFire },
{ "inferno", loadInferno },
{ "linear ygb", loadLinearYGB },
{ "cold and hot", loadColdAndHot },
{ "rainbow desaturated", loadRainbowDesaturated },
{ "cool to warm (extended)", loadCoolToWarmExtended },
{ "x ray", loadXRay },
{ "black, blue and white", loadBlackBlueWhite },
{ "virdis", loadVirdis },
{ "linear green", loadLinearGreen },
{ "jet", loadJet },
{ "rainbow", loadRainbow },
{ vtkm::cont::ColorTable::Preset::DEFAULT, "default", loadViridis },
{ vtkm::cont::ColorTable::Preset::VIRIDIS, "viridis", loadViridis },
{ vtkm::cont::ColorTable::Preset::COOL_TO_WARM, "cool to warm", loadCoolToWarm },
{ vtkm::cont::ColorTable::Preset::COOL_TO_WARN_EXTENDED,
"cool to warm (extended)",
loadCoolToWarmExtended },
{ vtkm::cont::ColorTable::Preset::COLD_AND_HOT, "cold and hot", loadColdAndHot },
{ vtkm::cont::ColorTable::Preset::INFERNO, "inferno", loadInferno },
{ vtkm::cont::ColorTable::Preset::BLACK_BODY_RADIATION, "black-body radiation", loadBlackBody },
{ vtkm::cont::ColorTable::Preset::SAMSEL_FIRE, "samsel fire", loadSamselFire },
{ vtkm::cont::ColorTable::Preset::LINEAR_YGB, "linear ygb", loadLinearYGB },
{ vtkm::cont::ColorTable::Preset::BLACK_BLUE_AND_WHITE,
"black, blue and white",
loadBlackBlueWhite },
{ vtkm::cont::ColorTable::Preset::LINEAR_GREEN, "linear green", loadLinearGreen },
{ vtkm::cont::ColorTable::Preset::X_RAY, "x ray", loadXRay },
{ vtkm::cont::ColorTable::Preset::JET, "jet", loadJet },
{ vtkm::cont::ColorTable::Preset::RAINBOW_DESATURATED,
"rainbow desaturated",
loadRainbowDesaturated },
{ vtkm::cont::ColorTable::Preset::RAINBOW, "rainbow", loadRainbow },
};
}
@ -577,6 +585,21 @@ namespace cont
{
namespace detail
{
VTKM_CONT_EXPORT
bool loadColorTablePreset(vtkm::cont::ColorTable::Preset preset, vtkm::cont::ColorTable& table)
{
for (int i = 0; i < numberOfPresets; ++i)
{
if (preset == presets[i].id)
{
presets[i].function(table);
return true;
}
}
return false;
}
VTKM_CONT_EXPORT std::set<std::string> GetPresetNames()
{
std::set<std::string> names;

@ -109,12 +109,12 @@ public:
//verify that we can get the presets
std::set<std::string> names = table.GetPresets();
VTKM_TEST_ASSERT(names.size() == 14, "incorrect number of names in preset set");
VTKM_TEST_ASSERT(names.size() == 15, "incorrect number of names in preset set");
VTKM_TEST_ASSERT(names.count("inferno") == 1, "names should contain inferno");
VTKM_TEST_ASSERT(names.count("black-body radiation") == 1,
"names should contain black-body radiation");
VTKM_TEST_ASSERT(names.count("virdis") == 1, "names should contain virdis");
VTKM_TEST_ASSERT(names.count("viridis") == 1, "names should contain viridis");
VTKM_TEST_ASSERT(names.count("black, blue and white") == 1,
"names should contain black, blue and white");
VTKM_TEST_ASSERT(names.count("samsel fire") == 1, "names should contain samsel fire");
@ -288,8 +288,7 @@ public:
vtkm::Range range{ 0.0, 50.0 };
auto diverging = vtkm::cont::ColorSpace::DIVERGING;
vtkm::cont::ColorTable table;
table.LoadPreset("Cool to Warm");
vtkm::cont::ColorTable table(vtkm::cont::ColorTable::Preset::COOL_TO_WARM);
VTKM_TEST_ASSERT(table.GetColorSpace() == diverging,
"color space not switched when loading preset");
@ -426,8 +425,7 @@ public:
static void TestSampling()
{
vtkm::cont::ColorTable table;
table.LoadPreset("Linear green");
vtkm::cont::ColorTable table(vtkm::cont::ColorTable::Preset::LINEAR_GREEN);
VTKM_TEST_ASSERT((table.GetRange() == vtkm::Range{ 0.0, 1.0 }),
"loading linear green table failed with wrong range");
VTKM_TEST_ASSERT((table.GetNumberOfPoints() == 21),
@ -453,8 +451,7 @@ public:
{
//build a color table with clamping off and verify that sampling works
vtkm::Range range{ 0.0, 50.0 };
vtkm::cont::ColorTable table;
table.LoadPreset("Cool to Warm");
vtkm::cont::ColorTable table(vtkm::cont::ColorTable::Preset::COOL_TO_WARM);
table.RescaleToRange(range);
table.SetClampingOff();
table.SetAboveRangeColor(vtkm::Vec<float, 3>{ 1.0f, 0.0f, 0.0f }); //red

@ -35,7 +35,7 @@ class FieldToColors : public vtkm::filter::FilterField<FieldToColors>
{
public:
VTKM_CONT
FieldToColors(const vtkm::cont::ColorTable& table);
FieldToColors(const vtkm::cont::ColorTable& table = vtkm::cont::ColorTable());
enum FieldToColorsInputMode
{
@ -50,6 +50,13 @@ public:
RGBA
};
void SetColorTable(const vtkm::cont::ColorTable& table)
{
this->Table = table;
this->ModifiedCount = -1;
}
const vtkm::cont::ColorTable& GetColorTable() const { return this->Table; }
void SetMappingMode(FieldToColorsInputMode mode) { this->InputMode = mode; }
void SetMappingToScalar() { this->InputMode = FieldToColorsInputMode::SCALAR; }
void SetMappingToMagnitude() { this->InputMode = FieldToColorsInputMode::MAGNITUDE; }
@ -59,15 +66,15 @@ public:
bool IsMappingMagnitude() const { return this->InputMode == FieldToColorsInputMode::MAGNITUDE; }
bool IsMappingComponent() const { return this->InputMode == FieldToColorsInputMode::COMPONENT; }
void SetMappingComponent(vtkm::Int32 comp) { this->Component = comp; }
vtkm::Int32 GetMappingComponent() const { return this->Component; }
void SetMappingComponent(vtkm::IdComponent comp) { this->Component = comp; }
vtkm::IdComponent GetMappingComponent() const { return this->Component; }
void SetOutputMode(FieldToColorsOutputMode mode) { this->OutputMode = mode; }
void SetOutputToRGB() { this->OutputMode = FieldToColorsOutputMode::RGB; }
void SetOutputToRGBA() { this->OutputMode = FieldToColorsOutputMode::RGBA; }
FieldToColorsOutputMode GetOutputMode() const { return this->OutputMode; }
bool IsMappingRGB() const { return this->OutputMode == FieldToColorsOutputMode::RGB; }
bool IsMappingRGBA() const { return this->OutputMode == FieldToColorsOutputMode::RGBA; }
bool IsOutputRGB() const { return this->OutputMode == FieldToColorsOutputMode::RGB; }
bool IsOutputRGBA() const { return this->OutputMode == FieldToColorsOutputMode::RGBA; }
void SetNumberOfSamplingPoints(vtkm::Int32 count);
@ -86,7 +93,7 @@ private:
FieldToColorsOutputMode OutputMode;
vtkm::cont::ColorTableSamplesRGB SamplesRGB;
vtkm::cont::ColorTableSamplesRGBA SamplesRGBA;
vtkm::Int32 Component;
vtkm::IdComponent Component;
vtkm::Int32 SampleCount;
vtkm::Id ModifiedCount;
};

@ -18,6 +18,11 @@
// this software.
//============================================================================
#ifndef vtk_m_filter_Field_to_Colors_hxx
#define vtk_m_filter_Field_to_Colors_hxx
#include <vtkm/filter/FieldToColors.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ColorTable.hxx>
@ -268,3 +273,5 @@ inline VTKM_CONT vtkm::filter::Result FieldToColors::DoExecute(
}
}
} // namespace vtkm::filter
#endif //vtk_m_filter_Field_to_Colors_hxx

@ -32,8 +32,7 @@ void TestFieldToColors()
//build a color table with clamping off and verify that sampling works
vtkm::Range range{ 0.0, 50.0 };
vtkm::cont::ColorTable table;
table.LoadPreset("Cool to Warm");
vtkm::cont::ColorTable table(vtkm::cont::ColorTable::Preset::COOL_TO_WARM);
table.RescaleToRange(range);
table.SetClampingOff();
table.SetAboveRangeColor(vtkm::Vec<float, 3>{ 1.0f, 0.0f, 0.0f }); //red

@ -38,17 +38,6 @@ struct Actor::InternalsType
vtkm::Range ScalarRange;
vtkm::Bounds SpatialBounds;
VTKM_CONT
InternalsType(const vtkm::cont::DynamicCellSet& cells,
const vtkm::cont::CoordinateSystem& coordinates,
const vtkm::cont::Field& scalarField)
: Cells(cells)
, Coordinates(coordinates)
, ScalarField(scalarField)
{
this->ColorTable.LoadPreset("Cool to Warm");
}
VTKM_CONT
InternalsType(const vtkm::cont::DynamicCellSet& cells,
const vtkm::cont::CoordinateSystem& coordinates,
@ -65,7 +54,7 @@ struct Actor::InternalsType
InternalsType(const vtkm::cont::DynamicCellSet& cells,
const vtkm::cont::CoordinateSystem& coordinates,
const vtkm::cont::Field& scalarField,
const vtkm::cont::ColorTable& colorTable)
const vtkm::cont::ColorTable& colorTable = vtkm::cont::ColorTable::Preset::DEFAULT)
: Cells(cells)
, Coordinates(coordinates)
, ScalarField(scalarField)

@ -27,11 +27,11 @@ namespace rendering
{
ColorBarAnnotation::ColorBarAnnotation()
: ColorTable(vtkm::cont::ColorSpace::LAB)
, Position(vtkm::Range(-0.88, +0.88), vtkm::Range(+0.87, +0.92), vtkm::Range(0, 0))
, Horizontal(true)
, FieldName("")
{
vtkm::Bounds bounds(vtkm::Range(-0.88, +0.88), vtkm::Range(+0.87, +0.92), vtkm::Range(0, 0));
Position = bounds;
Horizontal = true;
FieldName = "";
}
ColorBarAnnotation::~ColorBarAnnotation()