1 /****************************************************************************
2 * MeshLab                                                           o o     *
3 * A versatile mesh processing toolbox                             o     o   *
4 *                                                                _   O  _   *
5 * Copyright(C) 2005                                                \/)\/    *
6 * Visual Computing Lab                                            /\/|      *
7 * ISTI - Italian National Research Council                           |      *
8 *                                                                    \      *
9 * All rights reserved.                                                      *
10 *                                                                           *
11 * This program is free software; you can redistribute it and/or modify      *
12 * it under the terms of the GNU General Public License as published by      *
13 * the Free Software Foundation; either version 2 of the License, or         *
14 * (at your option) any later version.                                       *
15 *                                                                           *
16 * This program is distributed in the hope that it will be useful,           *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of            *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)          *
20 * for more details.                                                         *
21 *                                                                           *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25 Revision 1.0 2008/02/20 Alessandro Maione, Federico Bellucci
26 FIRST RELEASE
27 
28 ****************************************************************************/
29 
30 #ifndef _TRANSFER_FUNCTION_H_
31 #define _TRANSFER_FUNCTION_H_
32 
33 #include <vcg/math/base.h>
34 #include <vcg/space/color4.h>
35 #include <vector>
36 #include <cassert>
37 #include <QString>
38 #include <QColor>
39 #include "const_types.h"
40 #include "util.h"
41 
42 
43 #define LOWER_Y					0
44 #define UPPER_Y					1
45 #define NUMBER_OF_JUNCTION_Y	2
46 
47 //struct used to represent each point in the transfer function.
48 //It's composed of a position on x axis and two values on the y axis (potentially the same)
49 struct TF_KEY
50 {
51 	float	x;
52 	float	y;
53 
54 	TF_KEY( float xVal=0.0f, float yVal=0.0f )
55 	{
56 		assert(xVal>=0.0f);
57 		assert(yVal>=0.0f);
58 		x = xVal;
59 		y = yVal;
60 	}
61 };
62 #define TF_KEYsize	sizeof(TF_KEY)
63 
64 
65 //list of channels
66 enum TF_CHANNELS
67 {
68 	RED_CHANNEL = 0,
69 	GREEN_CHANNEL,
70 	BLUE_CHANNEL,
71 	NUMBER_OF_CHANNELS
72 };
73 
74 //macro to convert a type (channel code) into the respective color
75 #define TYPE_2_COLOR(TYPE, COLOR) \
76 	switch(TYPE) \
77 	{ \
78 	case RED_CHANNEL: \
79 		COLOR = Qt::red; \
80 		break; \
81 	case GREEN_CHANNEL: \
82 		COLOR = Qt::green; \
83 		break; \
84 	case BLUE_CHANNEL: \
85 		COLOR = Qt::blue; \
86 		break; \
87 	default: \
88 		COLOR = Qt::black; \
89 		break; \
90 	}
91 
92 //macro to convert a color (channel color) into the respective channel code
93 #define COLOR_2_TYPE(COLOR, TYPE) \
94 if ( COLOR == Qt::red) \
95 { \
96 	TYPE = RED_CHANNEL; \
97 } \
98 	else \
99 	{ \
100 		if ( COLOR == Qt::green) \
101 		{ \
102 			TYPE = GREEN_CHANNEL; \
103 		} \
104 		else \
105 		{ \
106 			if ( color == Qt::blue) \
107 			{ \
108 				TYPE =  BLUE_CHANNEL; \
109 			} \
110 			else \
111 				TYPE = -1; \
112 		} \
113 	}
114 
115 
116 //defines a to class to menage the keys for a single channel
117 class TfChannel
118 {
119 public:
120 	//container and iterator for TF KEYs
121 	typedef	std::vector<TF_KEY*> KEY_LIST;
122 	typedef	std::vector<TF_KEY*>::iterator KEY_LISTiterator;
123 
124 	TfChannel(void);
125 	TfChannel(TF_CHANNELS type);
126 	~TfChannel(void);
127 
128 	void		setType(TF_CHANNELS);
129 	TF_CHANNELS	getType(void);
130 	TF_KEY		*addKey(float xVal, float yVal);
131 	TF_KEY		*addKey(TF_KEY *newKey);
132 	void		removeKey(int index);
133 	void		removeKey(TF_KEY *key);
134 	void		flip();
135 
136 	float	getChannelValuef(float x_position);
137 	UINT8	getChannelValueb(float x_position);
138 
139 	bool	isHead(TF_KEY *key);
140 	bool	isTail(TF_KEY *key);
141 	void	updateKeysOrder(void);
142 
143 	TF_KEY* operator [](float idx);
144 	TF_KEY* operator [](size_t idx);
145 
size(void)146 	inline size_t size(void)	{	return KEYS.size();	}
147 
148 #ifdef NOW_TESTING
149 	void testInitChannel();
150 #endif
151 
152 private:
153 	//this code represent a unique id used for each channel.
154 	TF_CHANNELS	_type;
155 
156 	//list of keys
157 	KEY_LIST	KEYS;
158 };
159 
160 
161 #ifdef WIN32
162 #define CSV_FILE_DIRECTORY		"CSV\\"
163 #else
164 #define CSV_FILE_DIRECTORY		"CSV/"
165 #endif
166 #define CSV_FILE_EXSTENSION		".qmap"
167 #define CSV_FILE_SEPARATOR		";"
168 #define CSV_FILE_COMMENT		"//"
169 
170 //list of default transfer function
171 //An overloaded constructor of Transfer Function takes a value of this list as parameter
172 //to build the correspondent default Transfer Function
173 enum DEFAULT_TRANSFER_FUNCTIONS
174 {
175 	GREY_SCALE_TF = 0,
176 	MESHLAB_RGB_TF,
177 	RGB_TF,
178 	FRENCH_RGB_TF,
179 	RED_SCALE_TF,
180 	GREEN_SCALE_TF,
181 	BLUE_SCALE_TF,
182 	FLAT_TF,
183 	SAW_4_TF,
184 	SAW_8_TF,
185 	NUMBER_OF_DEFAULT_TF
186 };
187 
188 //code of the default startup Transfer Function
189 //When the plugin is launched, a TF is built using this code
190 #define STARTUP_TF_TYPE		MESHLAB_RGB_TF
191 
192 
193 //Representation of a Transfer Function as a set of channels
194 //At the moment, the Transfer Function contains 3 channels (RGB)
195 class TransferFunction
196 {
197 private:
198 	TfChannel	_channels[NUMBER_OF_CHANNELS];			//set of channels
199 	int			_channels_order[NUMBER_OF_CHANNELS];	//array used to carry out virtual pivoting indexing
200 	QColor		_color_band[COLOR_BAND_SIZE];			//array of colors used for preview color band
201 
202 	void initTF(void);
203 
204 public:
205 	TransferFunction(void);
206 	TransferFunction(QString csvFileName);
207 	TransferFunction(DEFAULT_TRANSFER_FUNCTIONS tf_code);
208 	~TransferFunction(void);
209 
210 	static QString defaultTFs[NUMBER_OF_DEFAULT_TF];
211 
getChannel(int channel_code)212 	TfChannel&	getChannel(int channel_code)	{return _channels[channel_code];}
213 	TfChannel&	operator [](int i)				{ return _channels[_channels_order[i]];	}
214 	size_t size();
215 	QColor* buildColorBand(void);
216 	void flipRamp();
217 	QString saveColorBand(QString fileName, EQUALIZER_INFO& equalizerInfo);
218 	vcg::Color4b getColorByQuality(float percentageQuality);
219 	vcg::Color4b getColorByQuality(float absoluteQuality, float minQuality, float maxQuality, float midRelativeQuality, float brightness);
220 	void moveChannelAhead(TF_CHANNELS channel_code);
getFirstPlaneChanel(void)221 	inline int	getFirstPlaneChanel(void)		{ return _channels_order[NUMBER_OF_CHANNELS-1]; }
222 };
223 
224 bool TfKeyPCompare(TF_KEY*k1, TF_KEY*k2);
225 
226 #endif
227