1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (C) 2011, Purdue Research Foundation |
---|
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 <vtkLookupTable.h> |
---|
16 | |
---|
17 | namespace Rappture { |
---|
18 | namespace VtkVis { |
---|
19 | |
---|
20 | /** |
---|
21 | * \brief RGBA color map table created from transfer function |
---|
22 | * |
---|
23 | * ColorMap uses linear interpolation between ControlPoints to |
---|
24 | * create a color lookup table for VTK |
---|
25 | */ |
---|
26 | class ColorMap { |
---|
27 | public: |
---|
28 | /** |
---|
29 | * \brief RGB inflection point in a transfer function |
---|
30 | */ |
---|
31 | struct ControlPoint { |
---|
32 | ControlPoint() : |
---|
33 | value(0) |
---|
34 | { |
---|
35 | memset(color, 0, sizeof(double)*3); |
---|
36 | } |
---|
37 | ControlPoint(const ControlPoint& other) : |
---|
38 | value(other.value) |
---|
39 | { |
---|
40 | memcpy(color, other.color, sizeof(double)*3); |
---|
41 | } |
---|
42 | ControlPoint& operator=(const ControlPoint &other) |
---|
43 | { |
---|
44 | if (this != &other) { |
---|
45 | value = other.value; |
---|
46 | memcpy(color, other.color, sizeof(double)*3); |
---|
47 | } |
---|
48 | return *this; |
---|
49 | } |
---|
50 | |
---|
51 | double value; ///< Normalized scalar data value [0,1] |
---|
52 | double color[3]; ///< RGB color |
---|
53 | }; |
---|
54 | |
---|
55 | /** |
---|
56 | * \brief Opacity(alpha) inflection point in a transfer function |
---|
57 | */ |
---|
58 | struct OpacityControlPoint { |
---|
59 | OpacityControlPoint() : |
---|
60 | value(0), |
---|
61 | alpha(1) |
---|
62 | { |
---|
63 | } |
---|
64 | OpacityControlPoint(const OpacityControlPoint& other) : |
---|
65 | value(other.value), |
---|
66 | alpha(other.alpha) |
---|
67 | { |
---|
68 | } |
---|
69 | OpacityControlPoint& operator=(const OpacityControlPoint &other) |
---|
70 | { |
---|
71 | if (this != &other) { |
---|
72 | value = other.value; |
---|
73 | alpha = other.alpha; |
---|
74 | } |
---|
75 | return *this; |
---|
76 | } |
---|
77 | |
---|
78 | double value; ///< Normalized scalar data value [0,1] |
---|
79 | double alpha; ///< Opacity |
---|
80 | }; |
---|
81 | |
---|
82 | ColorMap(const std::string& name); |
---|
83 | virtual ~ColorMap(); |
---|
84 | |
---|
85 | const std::string& getName(); |
---|
86 | vtkLookupTable *getLookupTable(); |
---|
87 | |
---|
88 | void setNumberOfTableEntries(int numEntries); |
---|
89 | |
---|
90 | void addControlPoint(ControlPoint& cp); |
---|
91 | |
---|
92 | void addOpacityControlPoint(OpacityControlPoint& cp); |
---|
93 | |
---|
94 | void build(); |
---|
95 | |
---|
96 | void clear(); |
---|
97 | |
---|
98 | static ColorMap* createDefault(); |
---|
99 | |
---|
100 | private: |
---|
101 | ColorMap(); |
---|
102 | |
---|
103 | void lerp(double *result, const ControlPoint& cp1, const ControlPoint& cp2, double value); |
---|
104 | void lerp(double *result, const OpacityControlPoint& cp1, const OpacityControlPoint& cp2, double value); |
---|
105 | |
---|
106 | std::string _name; |
---|
107 | std::list<ControlPoint> _controlPoints; |
---|
108 | std::list<OpacityControlPoint> _opacityControlPoints; |
---|
109 | bool _needsBuild; |
---|
110 | int _numTableEntries; |
---|
111 | vtkSmartPointer<vtkLookupTable> _lookupTable; |
---|
112 | }; |
---|
113 | |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | #endif |
---|