Commit 067dad41 authored by Robert Maynard's avatar Robert Maynard

Add ScalarsToColors worklet(s)

The ScalarsToColors is the first step in reproducing the color
capability that exists in VTK. The next step after this is to
provide a comparable lookup table.
parent 6ce72297
......@@ -55,6 +55,7 @@ set(headers
PointAverage.h
PointElevation.h
RemoveUnusedPoints.h
ScalarsToColors.h
ScatterCounting.h
ScatterIdentity.h
ScatterPermutation.h
......@@ -74,6 +75,12 @@ set(headers
WorkletReduceByKey.h
)
set(header_impls
ScalarsToColors.hxx
)
#-----------------------------------------------------------------------------
add_subdirectory(internal)
add_subdirectory(contour)
......@@ -88,6 +95,7 @@ add_subdirectory(wavelets)
add_subdirectory(particleadvection)
vtkm_declare_headers(${headers})
vtkm_declare_headers(${header_impls} TESTABLE OFF)
#-----------------------------------------------------------------------------
......
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//
//=============================================================================
#ifndef vtk_m_worklet_ScalarsToColors_h
#define vtk_m_worklet_ScalarsToColors_h
#include <vtkm/Range.h>
#include <vtkm/cont/ArrayHandle.h>
namespace vtkm
{
namespace worklet
{
namespace colorconversion
{
inline void ComputeShiftScale(const vtkm::Range& range, vtkm::Float32& shift, vtkm::Float32& scale)
{
//This scale logic seems to be unduly complicated
shift = static_cast<vtkm::Float32>(-range.Min);
scale = static_cast<vtkm::Float32>(range.Length());
if (range.Length() <= 0)
{
scale = -1e17f;
}
if (scale * scale > 1e-30f)
{
scale = 1.0f / scale;
}
scale *= 255.0f;
}
}
class ScalarsToColors
{
vtkm::Range ValueRange = { 0.0f, 255.0f };
vtkm::Float32 Alpha = 1.0f;
vtkm::Float32 Shift = 0.0f;
vtkm::Float32 Scale = 1.0f;
public:
ScalarsToColors() {}
ScalarsToColors(const vtkm::Range& range, vtkm::Float32 alpha)
: ValueRange(range)
, Alpha(vtkm::Min(vtkm::Max(alpha, 0.0f), 1.0f))
{
colorconversion::ComputeShiftScale(range, this->Shift, this->Scale);
}
ScalarsToColors(const vtkm::Range& range)
: ValueRange(range)
{
colorconversion::ComputeShiftScale(range, this->Shift, this->Scale);
}
ScalarsToColors(vtkm::Float32 alpha)
: ValueRange(0.0f, 255.0f)
, Alpha(vtkm::Min(vtkm::Max(alpha, 0.0f), 1.0f))
{
}
void SetRange(const vtkm::Range& range)
{
colorconversion::ComputeShiftScale(range, this->Shift, this->Scale);
}
vtkm::Range GetRange() const { return this->ValueRange; }
void SetAlpha(vtkm::Float32 alpha) { this->Alpha = vtkm::Min(vtkm::Max(alpha, 0.0f), 1.0f); }
vtkm::Float32 GetAlpha() const { return this->Alpha; }
/// \brief Use each component to generate RGBA colors
///
template <typename T, typename S, typename Device>
void Run(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device) const;
/// \brief Use each component to generate RGB colors
///
template <typename T, typename S, typename Device>
void Run(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device) const;
/// \brief Use magnitude of a vector to generate RGBA colors
///
template <typename T, int N, typename S, typename Device>
void RunMagnitude(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device) const;
/// \brief Use magnitude of a vector to generate RGB colors
///
template <typename T, int N, typename S, typename Device>
void RunMagnitude(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device) const;
/// \brief Use a single component of a vector to generate RGBA colors
///
template <typename T, int N, typename S, typename Device>
void RunComponent(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::IdComponent comp,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device) const;
/// \brief Use a single component of a vector to generate RGB colors
///
template <typename T, int N, typename S, typename Device>
void RunComponent(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::IdComponent comp,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device) const;
};
}
}
#include <vtkm/worklet/ScalarsToColors.hxx>
#endif
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//
//=============================================================================
#include <vtkm/worklet/ScalarsToColors.h>
#include <vtkm/BaseComponent.h>
#include <vtkm/cont/ArrayHandleExtractComponent.h>
#include <vtkm/cont/ArrayHandleTransform.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/colorconversion/ConvertToRGB.h>
#include <vtkm/worklet/colorconversion/ConvertToRGBA.h>
#include <vtkm/worklet/colorconversion/Portals.h>
#include <vtkm/worklet/colorconversion/ShiftScaleToRGB.h>
#include <vtkm/worklet/colorconversion/ShiftScaleToRGBA.h>
namespace vtkm
{
namespace worklet
{
namespace colorconversion
{
inline bool needShiftScale(vtkm::Float32, vtkm::Float32 shift, vtkm::Float32 scale)
{
return !((shift == -0.0f || shift == 0.0f) && (scale == 255.0f));
}
inline bool needShiftScale(vtkm::Float64, vtkm::Float32 shift, vtkm::Float32 scale)
{
return !((shift == -0.0f || shift == 0.0f) && (scale == 255.0f));
}
inline bool needShiftScale(vtkm::UInt8, vtkm::Float32 shift, vtkm::Float32 scale)
{
return !((shift == -0.0f || shift == 0.0f) && (scale == 1.0f));
}
template <typename T>
inline bool needShiftScale(T, vtkm::Float32, vtkm::Float32)
{
return true;
}
}
/// \brief Use each component to generate RGBA colors
///
template <typename T, typename S, typename Device>
void ScalarsToColors::Run(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device) const
{
using namespace vtkm::worklet::colorconversion;
//If our shift is 0 and our scale == 1 no need to apply them
using BaseT = typename vtkm::BaseComponent<T>::Type;
const bool shiftscale = needShiftScale(BaseT{}, this->Shift, this->Scale);
if (shiftscale)
{
vtkm::worklet::DispatcherMapField<ShiftScaleToRGBA, Device> dispatcher(
ShiftScaleToRGBA(this->Shift, this->Scale, this->Alpha));
dispatcher.Invoke(values, rgbaOut);
}
else
{
vtkm::worklet::DispatcherMapField<ConvertToRGBA, Device> dispatcher(ConvertToRGBA(this->Alpha));
dispatcher.Invoke(values, rgbaOut);
}
}
/// \brief Use each component to generate RGB colors
///
template <typename T, typename S, typename Device>
void ScalarsToColors::Run(const vtkm::cont::ArrayHandle<T, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device) const
{
using namespace vtkm::worklet::colorconversion;
using BaseT = typename vtkm::BaseComponent<T>::Type;
const bool shiftscale = needShiftScale(BaseT{}, this->Shift, this->Scale);
if (shiftscale)
{
vtkm::worklet::DispatcherMapField<ShiftScaleToRGB, Device> dispatcher(
ShiftScaleToRGB(this->Shift, this->Scale));
dispatcher.Invoke(values, rgbOut);
}
else
{
vtkm::worklet::DispatcherMapField<ConvertToRGB, Device> dispatcher;
dispatcher.Invoke(values, rgbOut);
}
}
/// \brief Use magnitude of a vector to generate RGBA colors
///
template <typename T, int N, typename S, typename Device>
void ScalarsToColors::RunMagnitude(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device) const
{
//magnitude is a complex situation. the default scale factor is incorrect
//
using namespace vtkm::worklet::colorconversion;
//If our shift is 0 and our scale == 1 no need to apply them
using BaseT = typename vtkm::BaseComponent<T>::Type;
const bool shiftscale = needShiftScale(BaseT{}, this->Shift, this->Scale);
if (shiftscale)
{
vtkm::worklet::DispatcherMapField<ShiftScaleToRGBA, Device> dispatcher(
ShiftScaleToRGBA(this->Shift, this->Scale, this->Alpha));
dispatcher.Invoke(
vtkm::cont::make_ArrayHandleTransform(values, colorconversion::MagnitudePortal()), rgbaOut);
}
else
{
vtkm::worklet::DispatcherMapField<ConvertToRGBA, Device> dispatcher(ConvertToRGBA(this->Alpha));
dispatcher.Invoke(
vtkm::cont::make_ArrayHandleTransform(values, colorconversion::MagnitudePortal()), rgbaOut);
}
}
/// \brief Use magnitude of a vector to generate RGB colors
///
template <typename T, int N, typename S, typename Device>
void ScalarsToColors::RunMagnitude(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device) const
{
using namespace vtkm::worklet::colorconversion;
using BaseT = typename vtkm::BaseComponent<T>::Type;
const bool shiftscale = needShiftScale(BaseT{}, this->Shift, this->Scale);
if (shiftscale)
{
vtkm::worklet::DispatcherMapField<ShiftScaleToRGB, Device> dispatcher(
ShiftScaleToRGB(this->Shift, this->Scale));
dispatcher.Invoke(
vtkm::cont::make_ArrayHandleTransform(values, colorconversion::MagnitudePortal()), rgbOut);
}
else
{
vtkm::worklet::DispatcherMapField<ConvertToRGB, Device> dispatcher;
dispatcher.Invoke(
vtkm::cont::make_ArrayHandleTransform(values, colorconversion::MagnitudePortal()), rgbOut);
}
}
/// \brief Use a single component of a vector to generate RGBA colors
///
template <typename T, int N, typename S, typename Device>
void ScalarsToColors::RunComponent(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::IdComponent comp,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 4>>& rgbaOut,
Device device) const
{
this->Run(vtkm::cont::make_ArrayHandleTransform(values, colorconversion::ComponentPortal(comp)),
rgbaOut,
device);
}
/// \brief Use a single component of a vector to generate RGB colors
///
template <typename T, int N, typename S, typename Device>
void ScalarsToColors::RunComponent(const vtkm::cont::ArrayHandle<vtkm::Vec<T, N>, S>& values,
vtkm::IdComponent comp,
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::UInt8, 3>>& rgbOut,
Device device) const
{
this->Run(vtkm::cont::make_ArrayHandleTransform(values, colorconversion::ComponentPortal(comp)),
rgbOut,
device);
}
}
}
##============================================================================
## Copyright (c) Kitware, Inc.
## All rights reserved.
## See LICENSE.txt for details.
## This software is distributed WITHOUT ANY WARRANTY; without even
## the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE. See the above copyright notice for more information.
##
## Copyright 2016 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
## Copyright 2016 UT-Battelle, LLC.
## Copyright 2016 Los Alamos National Security.
##
## Under the terms of Contract DE-NA0003525 with NTESS,
## the U.S. Government retains certain rights in this software.
##
## Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
## Laboratory (LANL), the U.S. Government retains certain rights in
## this software.
##============================================================================
set(headers
Conversions.h
ConvertToRGB.h
ConvertToRGBA.h
Portals.h
ShiftScaleToRGB.h
ShiftScaleToRGBA.h
)
#-----------------------------------------------------------------------------
vtkm_declare_headers(${headers})
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//
//=============================================================================
#ifndef vtk_m_worklet_colorconversion_Conversions_h
#define vtk_m_worklet_colorconversion_Conversions_h
namespace vtkm
{
namespace worklet
{
namespace colorconversion
{
template <typename T>
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(T t)
{
return static_cast<vtkm::UInt8>(t);
}
template <>
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(vtkm::Float64 t)
{
return static_cast<vtkm::UInt8>(t * 255.0f + 0.5f);
}
template <>
VTKM_EXEC inline vtkm::UInt8 ColorToUChar(vtkm::Float32 t)
{
return static_cast<vtkm::UInt8>(t * 255.0f + 0.5f);
}
VTKM_EXEC inline void Clamp(vtkm::Float32& val)
{
val = vtkm::Min(255.0f, vtkm::Max(0.0f, val));
}
VTKM_EXEC inline void Clamp(vtkm::Vec<vtkm::Float32, 2>& val)
{
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
}
VTKM_EXEC inline void Clamp(vtkm::Vec<vtkm::Float32, 3>& val)
{
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
val[2] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[2]));
}
VTKM_EXEC inline void Clamp(vtkm::Vec<vtkm::Float32, 4>& val)
{
val[0] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[0]));
val[1] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[1]));
val[2] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[2]));
val[3] = vtkm::Min(255.0f, vtkm::Max(0.0f, val[3]));
}
}
}
}
#endif
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//
//=============================================================================
#ifndef vtk_m_worklet_colorconversion_ConvertToRGB_h
#define vtk_m_worklet_colorconversion_ConvertToRGB_h
#include <vtkm/worklet/colorconversion/Conversions.h>
namespace vtkm
{
namespace worklet
{
namespace colorconversion
{
struct ConvertToRGB : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<> in, FieldOut<> out);
typedef _2 ExecutionSignature(_1);
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 3> operator()(const T& in) const
{ //vtkScalarsToColorsLuminanceToRGB
const vtkm::UInt8 la = colorconversion::ColorToUChar(in);
return vtkm::Vec<UInt8, 3>(la, la, la);
}
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 3> operator()(const vtkm::Vec<T, 2>& in) const
{ //vtkScalarsToColorsLuminanceAlphaToRGB (which actually doesn't exist in vtk)
return this->operator()(in[0]);
}
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 3> operator()(const vtkm::Vec<T, 3>& in) const
{ //vtkScalarsToColorsRGBToRGB
return vtkm::Vec<UInt8, 3>(colorconversion::ColorToUChar(in[0]),
colorconversion::ColorToUChar(in[1]),
colorconversion::ColorToUChar(in[2]));
}
vtkm::Vec<vtkm::UInt8, 3> operator()(const vtkm::Vec<vtkm::UInt8, 3>& in) const
{ //vtkScalarsToColorsRGBToRGB
return in;
}
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 3> operator()(const vtkm::Vec<T, 4>& in) const
{ //vtkScalarsToColorsRGBAToRGB
return vtkm::Vec<UInt8, 3>(colorconversion::ColorToUChar(in[0]),
colorconversion::ColorToUChar(in[1]),
colorconversion::ColorToUChar(in[2]));
}
};
}
}
}
#endif
//=============================================================================
//
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//
// Copyright 2015 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//
//=============================================================================
#ifndef vtk_m_worklet_colorconversion_ScalarsToColors_h
#define vtk_m_worklet_colorconversion_ScalarsToColors_h
#include <vtkm/worklet/colorconversion/Conversions.h>
namespace vtkm
{
namespace worklet
{
namespace colorconversion
{
struct ConvertToRGBA : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<> in, FieldOut<> out);
typedef _2 ExecutionSignature(_1);
ConvertToRGBA(vtkm::Float32 alpha)
: Alpha(alpha)
{
}
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 4> operator()(const T& in) const
{ //vtkScalarsToColorsLuminanceToRGBA
const vtkm::UInt8 l = colorconversion::ColorToUChar(in);
return vtkm::Vec<UInt8, 4>(l, l, l, colorconversion::ColorToUChar(this->Alpha));
}
template <typename T>
VTKM_EXEC vtkm::Vec<vtkm::UInt8, 4> operator()(const vtkm::Vec<T, 2>& in) const