1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef __RAPPTURE_VTKVIS_COLORMAP_H__ |
---|
9 | #define __RAPPTURE_VTKVIS_COLORMAP_H__ |
---|
10 | |
---|
11 | #include <string> |
---|
12 | #include <list> |
---|
13 | #include <cstring> |
---|
14 | #include <vtkSmartPointer.h> |
---|
15 | #include <vtkColorTransferFunction.h> |
---|
16 | #include <vtkPiecewiseFunction.h> |
---|
17 | #include <vtkLookupTable.h> |
---|
18 | |
---|
19 | namespace Rappture { |
---|
20 | namespace VtkVis { |
---|
21 | |
---|
22 | /** |
---|
23 | * \brief RGBA color map table created from transfer function |
---|
24 | * |
---|
25 | * ColorMap uses linear interpolation between ControlPoints to |
---|
26 | * create a color lookup table for VTK |
---|
27 | */ |
---|
28 | class ColorMap { |
---|
29 | public: |
---|
30 | /** |
---|
31 | * \brief RGB inflection point in a transfer function |
---|
32 | */ |
---|
33 | struct ControlPoint { |
---|
34 | ControlPoint() : |
---|
35 | value(0) |
---|
36 | { |
---|
37 | memset(color, 0, sizeof(double)*3); |
---|
38 | } |
---|
39 | ControlPoint(const ControlPoint& other) : |
---|
40 | value(other.value) |
---|
41 | { |
---|
42 | memcpy(color, other.color, sizeof(double)*3); |
---|
43 | } |
---|
44 | ControlPoint& operator=(const ControlPoint &other) |
---|
45 | { |
---|
46 | if (this != &other) { |
---|
47 | value = other.value; |
---|
48 | memcpy(color, other.color, sizeof(double)*3); |
---|
49 | } |
---|
50 | return *this; |
---|
51 | } |
---|
52 | |
---|
53 | double value; ///< Normalized scalar data value [0,1] |
---|
54 | double color[3]; ///< RGB color |
---|
55 | }; |
---|
56 | |
---|
57 | /** |
---|
58 | * \brief Opacity(alpha) inflection point in a transfer function |
---|
59 | */ |
---|
60 | struct OpacityControlPoint { |
---|
61 | OpacityControlPoint() : |
---|
62 | value(0), |
---|
63 | alpha(1) |
---|
64 | { |
---|
65 | } |
---|
66 | OpacityControlPoint(const OpacityControlPoint& other) : |
---|
67 | value(other.value), |
---|
68 | alpha(other.alpha) |
---|
69 | { |
---|
70 | } |
---|
71 | OpacityControlPoint& operator=(const OpacityControlPoint &other) |
---|
72 | { |
---|
73 | if (this != &other) { |
---|
74 | value = other.value; |
---|
75 | alpha = other.alpha; |
---|
76 | } |
---|
77 | return *this; |
---|
78 | } |
---|
79 | |
---|
80 | double value; ///< Normalized scalar data value [0,1] |
---|
81 | double alpha; ///< Opacity |
---|
82 | }; |
---|
83 | |
---|
84 | ColorMap(const std::string& name); |
---|
85 | virtual ~ColorMap(); |
---|
86 | |
---|
87 | const std::string& getName(); |
---|
88 | |
---|
89 | vtkLookupTable *getLookupTable(); |
---|
90 | |
---|
91 | vtkSmartPointer<vtkColorTransferFunction> getColorTransferFunction(double range[2]); |
---|
92 | |
---|
93 | vtkSmartPointer<vtkPiecewiseFunction> getOpacityTransferFunction(double range[2]); |
---|
94 | |
---|
95 | void setNumberOfTableEntries(int numEntries); |
---|
96 | |
---|
97 | int getNumberOfTableEntries(); |
---|
98 | |
---|
99 | void addControlPoint(ControlPoint& cp); |
---|
100 | |
---|
101 | void addOpacityControlPoint(OpacityControlPoint& cp); |
---|
102 | |
---|
103 | void build(); |
---|
104 | |
---|
105 | void clear(); |
---|
106 | |
---|
107 | static ColorMap *getDefault(); |
---|
108 | static ColorMap *getGrayDefault(); |
---|
109 | static ColorMap *getVolumeDefault(); |
---|
110 | static ColorMap *getElementDefault(); |
---|
111 | |
---|
112 | private: |
---|
113 | static ColorMap *_default; |
---|
114 | static ColorMap *_grayDefault; |
---|
115 | static ColorMap *_volumeDefault; |
---|
116 | static ColorMap *_elementDefault; |
---|
117 | |
---|
118 | ColorMap(); |
---|
119 | |
---|
120 | void lerp(double *result, const ControlPoint& cp1, const ControlPoint& cp2, double value); |
---|
121 | void lerp(double *result, const OpacityControlPoint& cp1, const OpacityControlPoint& cp2, double value); |
---|
122 | |
---|
123 | std::string _name; |
---|
124 | std::list<ControlPoint> _controlPoints; |
---|
125 | std::list<OpacityControlPoint> _opacityControlPoints; |
---|
126 | bool _needsBuild; |
---|
127 | int _numTableEntries; |
---|
128 | vtkSmartPointer<vtkColorTransferFunction> _colorTF; |
---|
129 | vtkSmartPointer<vtkPiecewiseFunction> _opacityTF; |
---|
130 | vtkSmartPointer<vtkLookupTable> _lookupTable; |
---|
131 | }; |
---|
132 | |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | #endif |
---|