VTK-m  2.0
LookupTable.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 #ifndef vtk_m_worklet_colorconversion_LookupTable_h
11 #define vtk_m_worklet_colorconversion_LookupTable_h
12 
14 
15 #include <vtkm/exec/ColorTable.h>
18 
19 #include <float.h>
20 
21 namespace vtkm
22 {
23 namespace worklet
24 {
25 namespace colorconversion
26 {
27 
29 
31 {
36 
37  //needs to support Nan, Above, Below Range colors
38  VTKM_CONT
39  template <typename T>
40  LookupTable(const T& colorTableSamples)
41  {
42  this->Shift = static_cast<vtkm::Float32>(-colorTableSamples.SampleRange.Min);
43  double rangeDelta = colorTableSamples.SampleRange.Length();
44  if (rangeDelta < DBL_MIN * colorTableSamples.NumberOfSamples)
45  {
46  // if the range is tiny, anything within the range will map to the bottom
47  // of the color scale.
48  this->Scale = 0.0;
49  }
50  else
51  {
52  this->Scale = static_cast<vtkm::Float32>(colorTableSamples.NumberOfSamples / rangeDelta);
53  }
54  this->TableRange = colorTableSamples.SampleRange;
55  this->NumberOfSamples = colorTableSamples.NumberOfSamples;
56  }
57 
58  using ControlSignature = void(FieldIn in, WholeArrayIn lookup, FieldOut color);
59  using ExecutionSignature = void(_1, _2, _3);
60 
61  template <typename T, typename WholeFieldIn, typename U, int N>
62  VTKM_EXEC void operator()(const T& in,
63  const WholeFieldIn lookupTable,
64  vtkm::Vec<U, N>& output) const
65  {
66  vtkm::Float64 v = (static_cast<vtkm::Float64>(in));
67  vtkm::Int32 idx = 1;
68 
69  //This logic uses how ColorTableSamples is constructed. See
70  //vtkm/cont/ColorTableSamples to see why we use these magic offset values
71  if (vtkm::IsNan(v))
72  {
73  idx = this->NumberOfSamples + 3;
74  }
75  else if (v < this->TableRange.Min)
76  { //If we are below the color range
77  idx = 0;
78  }
79  else if (v == this->TableRange.Min)
80  { //If we are at the ranges min value
81  idx = 1;
82  }
83  else if (v > this->TableRange.Max)
84  { //If we are above the ranges max value
85  idx = this->NumberOfSamples + 2;
86  }
87  else if (v == this->TableRange.Max)
88  { //If we are at the ranges min value
89  idx = this->NumberOfSamples;
90  }
91  else
92  {
93  v = (v + this->Shift) * this->Scale;
94  // When v is very close to p.Range[1], the floating point calculation giving
95  // idx may map above the highest value in the lookup table. That is why it
96  // is padded
97  idx = static_cast<vtkm::Int32>(v);
98  }
99  output = lookupTable.Get(idx);
100  }
101 };
102 }
103 }
104 }
105 #endif
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
WorkletMapField.h
ColorTable.h
vtkm::worklet::WorkletMapField::FieldOut
A control signature tag for output fields.
Definition: WorkletMapField.h:60
vtkm::worklet::colorconversion::LookupTable::TableRange
vtkm::Range TableRange
Definition: LookupTable.h:34
Conversions.h
vtkm::worklet::colorconversion::LookupTable::operator()
VTKM_EXEC void operator()(const T &in, const WholeFieldIn lookupTable, vtkm::Vec< U, N > &output) const
Definition: LookupTable.h:62
vtkm::worklet::colorconversion::LookupTable::Scale
vtkm::Float32 Scale
Definition: LookupTable.h:33
vtkm::worklet::colorconversion::LookupTable
Definition: LookupTable.h:30
vtkm::worklet::colorconversion::LookupTable::ControlSignature
void(FieldIn in, WholeArrayIn lookup, FieldOut color) ControlSignature
Definition: LookupTable.h:58
vtkm::worklet::WorkletMapField::FieldIn
A control signature tag for input fields.
Definition: WorkletMapField.h:49
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::colorconversion::LookupTable::LookupTable
VTKM_CONT LookupTable(const T &colorTableSamples)
Definition: LookupTable.h:40
vtkm::Vec
A short fixed-length array.
Definition: Types.h:767
vtkm::worklet::colorconversion::LookupTable::NumberOfSamples
vtkm::Int32 NumberOfSamples
Definition: LookupTable.h:35
vtkm::Range::Min
vtkm::Float64 Min
Definition: Range.h:33
vtkm::Float32
float Float32
Definition: Types.h:154
vtkm::List
Definition: List.h:34
vtkm::Int32
int32_t Int32
Definition: Types.h:160
vtkm::Float64
double Float64
Definition: Types.h:155
vtkm::worklet::colorconversion::LookupTable::ExecutionSignature
void(_1, _2, _3) ExecutionSignature
Definition: LookupTable.h:59
vtkm::Range::Max
vtkm::Float64 Max
Definition: Range.h:34
ColorTableSamples.h
vtkm::worklet::colorconversion::LookupTable::Shift
vtkm::Float32 Shift
Definition: LookupTable.h:32
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38
vtkm::Range
Represent a continuous scalar range of values.
Definition: Range.h:31