source: vtkvis/trunk/ColorMap.h @ 6226

Last change on this file since 6226 was 4358, checked in by ldelgass, 10 years ago

Add midpoint/sharpness to color/opacity control points

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
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 VTKVIS_COLORMAP_H
9#define VTKVIS_COLORMAP_H
10
11#include <string>
12#include <list>
13#include <cstring>
14
15#include <vtkSmartPointer.h>
16#include <vtkColorTransferFunction.h>
17#include <vtkPiecewiseFunction.h>
18#include <vtkLookupTable.h>
19#include <vtkUnsignedCharArray.h>
20
21namespace VtkVis {
22
23/**
24 * \brief RGBA color map table created from transfer function
25 *
26 * ColorMap uses linear interpolation between ControlPoints to
27 * create a color lookup table for VTK
28 */
29class ColorMap {
30public:
31    /**
32     * \brief RGB inflection point in a transfer function
33     */
34    struct ControlPoint {
35        ControlPoint() :
36            value(0),
37            midpoint(0.5),
38            sharpness(0)
39        {
40            memset(color, 0, sizeof(double)*3);
41        }
42        ControlPoint(const ControlPoint& other) :
43            value(other.value),
44            midpoint(other.midpoint),
45            sharpness(other.sharpness)
46        {
47            memcpy(color, other.color, sizeof(double)*3);
48        }
49        ControlPoint& operator=(const ControlPoint &other)
50        {
51            if (this != &other) {
52                value = other.value;
53                midpoint = other.midpoint;
54                sharpness = other.sharpness;
55                memcpy(color, other.color, sizeof(double)*3);
56            }
57            return *this;
58        }
59
60        double value; ///< Normalized scalar data value [0,1]
61        double midpoint, sharpness;
62        double color[3]; ///< RGB color
63    };
64
65    /**
66     * \brief Opacity(alpha) inflection point in a transfer function
67     */
68    struct OpacityControlPoint {
69        OpacityControlPoint() :
70            value(0),
71            midpoint(0.5),
72            sharpness(0),
73            alpha(1)
74        {
75        }
76        OpacityControlPoint(const OpacityControlPoint& other) :
77            value(other.value),
78            midpoint(other.midpoint),
79            sharpness(other.sharpness),
80            alpha(other.alpha)
81        {
82        }
83        OpacityControlPoint& operator=(const OpacityControlPoint &other)
84        {
85            if (this != &other) {
86                value = other.value;
87                midpoint = other.midpoint;
88                sharpness = other.sharpness;
89                alpha = other.alpha;
90            }
91            return *this;
92        }
93
94        double value; ///< Normalized scalar data value [0,1]
95        double midpoint, sharpness;
96        double alpha; ///< Opacity
97    };
98
99    ColorMap(const std::string& name);
100
101    ColorMap(const ColorMap& other);
102
103    ColorMap& operator=(const ColorMap& other);
104
105    virtual ~ColorMap();
106
107    const std::string& getName();
108
109    vtkLookupTable *getLookupTable();
110
111    vtkSmartPointer<vtkColorTransferFunction> getColorTransferFunction(double range[2]);
112
113    vtkSmartPointer<vtkPiecewiseFunction> getOpacityTransferFunction(double range[2], double opacityScale = 1.0);
114
115    void setNumberOfTableEntries(int numEntries);
116
117    int getNumberOfTableEntries();
118
119    void addControlPoint(ControlPoint& cp);
120
121    void addOpacityControlPoint(OpacityControlPoint& cp);
122
123    void build();
124
125    void clear();
126
127    static ColorMap *getDefault();
128    static ColorMap *getGrayDefault();
129    static ColorMap *getVolumeDefault();
130    static ColorMap *getElementDefault();
131
132    static void renderColorMap(ColorMap *map, int width, int height,
133                               vtkUnsignedCharArray *imgData,
134                               bool opaque, float bgColor[3],
135                               bool bgr = false,
136                               int bytesPerPixel = 3);
137
138private:
139    static ColorMap *_default;
140    static ColorMap *_grayDefault;
141    static ColorMap *_volumeDefault;
142    static ColorMap *_elementDefault;
143
144    ColorMap();
145
146    void lerp(double *result, const ControlPoint& cp1, const ControlPoint& cp2, double value);
147    void lerp(double *result, const OpacityControlPoint& cp1, const OpacityControlPoint& cp2, double value);
148
149    std::string _name;
150    std::list<ControlPoint> _controlPoints;
151    std::list<OpacityControlPoint> _opacityControlPoints;
152    bool _needsBuild;
153    int _numTableEntries;
154    vtkSmartPointer<vtkColorTransferFunction> _colorTF;
155    vtkSmartPointer<vtkPiecewiseFunction> _opacityTF;
156    vtkSmartPointer<vtkLookupTable> _lookupTable;
157};
158
159}
160
161#endif
Note: See TracBrowser for help on using the repository browser.