source: vtkvis/tags/1.7.2/ColorMap.h @ 4807

Last change on this file since 4807 was 4604, checked in by ldelgass, 10 years ago

Merge r4073-4074,4077-4081 from trunk

  • Property svn:eol-style set to native
File size: 3.9 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        {
38            memset(color, 0, sizeof(double)*3);
39        }
40        ControlPoint(const ControlPoint& other) :
41            value(other.value)
42        {
43            memcpy(color, other.color, sizeof(double)*3);
44        }
45        ControlPoint& operator=(const ControlPoint &other)
46        {
47            if (this != &other) {
48                value = other.value;
49                memcpy(color, other.color, sizeof(double)*3);
50            }
51            return *this;
52        }
53
54        double value; ///< Normalized scalar data value [0,1]
55        double color[3]; ///< RGB color
56    };
57
58    /**
59     * \brief Opacity(alpha) inflection point in a transfer function
60     */
61    struct OpacityControlPoint {
62        OpacityControlPoint() :
63            value(0),
64            alpha(1)
65        {
66        }
67        OpacityControlPoint(const OpacityControlPoint& other) :
68            value(other.value),
69            alpha(other.alpha)
70        {
71        }
72        OpacityControlPoint& operator=(const OpacityControlPoint &other)
73        {
74            if (this != &other) {
75                value = other.value;
76                alpha = other.alpha;
77            }
78            return *this;
79        }
80
81        double value; ///< Normalized scalar data value [0,1]
82        double alpha; ///< Opacity
83    };
84
85    ColorMap(const std::string& name);
86
87    ColorMap(const ColorMap& other);
88
89    ColorMap& operator=(const ColorMap& other);
90
91    virtual ~ColorMap();
92
93    const std::string& getName();
94
95    vtkLookupTable *getLookupTable();
96
97    vtkSmartPointer<vtkColorTransferFunction> getColorTransferFunction(double range[2]);
98
99    vtkSmartPointer<vtkPiecewiseFunction> getOpacityTransferFunction(double range[2], double opacityScale = 1.0);
100
101    void setNumberOfTableEntries(int numEntries);
102
103    int getNumberOfTableEntries();
104
105    void addControlPoint(ControlPoint& cp);
106
107    void addOpacityControlPoint(OpacityControlPoint& cp);
108
109    void build();
110
111    void clear();
112
113    static ColorMap *getDefault();
114    static ColorMap *getGrayDefault();
115    static ColorMap *getVolumeDefault();
116    static ColorMap *getElementDefault();
117
118    static void renderColorMap(ColorMap *map, int width, int height,
119                               vtkUnsignedCharArray *imgData,
120                               bool opaque, float bgColor[3],
121                               bool bgr = false,
122                               int bytesPerPixel = 3);
123
124private:
125    static ColorMap *_default;
126    static ColorMap *_grayDefault;
127    static ColorMap *_volumeDefault;
128    static ColorMap *_elementDefault;
129
130    ColorMap();
131
132    void lerp(double *result, const ControlPoint& cp1, const ControlPoint& cp2, double value);
133    void lerp(double *result, const OpacityControlPoint& cp1, const OpacityControlPoint& cp2, double value);
134
135    std::string _name;
136    std::list<ControlPoint> _controlPoints;
137    std::list<OpacityControlPoint> _opacityControlPoints;
138    bool _needsBuild;
139    int _numTableEntries;
140    vtkSmartPointer<vtkColorTransferFunction> _colorTF;
141    vtkSmartPointer<vtkPiecewiseFunction> _opacityTF;
142    vtkSmartPointer<vtkLookupTable> _lookupTable;
143};
144
145}
146
147#endif
Note: See TracBrowser for help on using the repository browser.