[2112] | 1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
| 2 | /* |
---|
[3177] | 3 | * Copyright (C) 2004-2012 HUBzero Foundation, LLC |
---|
[2112] | 4 | * |
---|
| 5 | * Author: Leif Delgass <ldelgass@purdue.edu> |
---|
| 6 | */ |
---|
| 7 | |
---|
[3615] | 8 | #ifndef VTKVIS_COLORMAP_H |
---|
| 9 | #define VTKVIS_COLORMAP_H |
---|
[2112] | 10 | |
---|
| 11 | #include <string> |
---|
| 12 | #include <list> |
---|
| 13 | #include <cstring> |
---|
[3616] | 14 | |
---|
[2112] | 15 | #include <vtkSmartPointer.h> |
---|
[2261] | 16 | #include <vtkColorTransferFunction.h> |
---|
| 17 | #include <vtkPiecewiseFunction.h> |
---|
[2112] | 18 | #include <vtkLookupTable.h> |
---|
[4073] | 19 | #include <vtkUnsignedCharArray.h> |
---|
[2112] | 20 | |
---|
| 21 | namespace 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 | */ |
---|
| 29 | class ColorMap { |
---|
| 30 | public: |
---|
| 31 | /** |
---|
[2194] | 32 | * \brief RGB inflection point in a transfer function |
---|
[2112] | 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 | }; |
---|
[2194] | 57 | |
---|
| 58 | /** |
---|
| 59 | * \brief Opacity(alpha) inflection point in a transfer function |
---|
| 60 | */ |
---|
[2112] | 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); |
---|
[3982] | 86 | |
---|
| 87 | ColorMap(const ColorMap& other); |
---|
| 88 | |
---|
| 89 | ColorMap& operator=(const ColorMap& other); |
---|
| 90 | |
---|
[2112] | 91 | virtual ~ColorMap(); |
---|
| 92 | |
---|
| 93 | const std::string& getName(); |
---|
[2261] | 94 | |
---|
[2112] | 95 | vtkLookupTable *getLookupTable(); |
---|
| 96 | |
---|
[2261] | 97 | vtkSmartPointer<vtkColorTransferFunction> getColorTransferFunction(double range[2]); |
---|
| 98 | |
---|
[3979] | 99 | vtkSmartPointer<vtkPiecewiseFunction> getOpacityTransferFunction(double range[2], double opacityScale = 1.0); |
---|
[2261] | 100 | |
---|
[2112] | 101 | void setNumberOfTableEntries(int numEntries); |
---|
| 102 | |
---|
[3455] | 103 | int getNumberOfTableEntries(); |
---|
| 104 | |
---|
[2112] | 105 | void addControlPoint(ControlPoint& cp); |
---|
| 106 | |
---|
| 107 | void addOpacityControlPoint(OpacityControlPoint& cp); |
---|
| 108 | |
---|
| 109 | void build(); |
---|
| 110 | |
---|
| 111 | void clear(); |
---|
| 112 | |
---|
[2261] | 113 | static ColorMap *getDefault(); |
---|
[2404] | 114 | static ColorMap *getGrayDefault(); |
---|
[2261] | 115 | static ColorMap *getVolumeDefault(); |
---|
[2320] | 116 | static ColorMap *getElementDefault(); |
---|
[2112] | 117 | |
---|
[4073] | 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 | |
---|
[2112] | 124 | private: |
---|
[2261] | 125 | static ColorMap *_default; |
---|
[2404] | 126 | static ColorMap *_grayDefault; |
---|
[2261] | 127 | static ColorMap *_volumeDefault; |
---|
[2320] | 128 | static ColorMap *_elementDefault; |
---|
[2261] | 129 | |
---|
[2112] | 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; |
---|
[2261] | 140 | vtkSmartPointer<vtkColorTransferFunction> _colorTF; |
---|
| 141 | vtkSmartPointer<vtkPiecewiseFunction> _opacityTF; |
---|
[2112] | 142 | vtkSmartPointer<vtkLookupTable> _lookupTable; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | #endif |
---|