histogram.h 9.08 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef __HISTOGRAM__
#define __HISTOGRAM__

#include "Topology/generic/attributeHandler.h"
Sylvain Thery's avatar
Sylvain Thery committed
29
#include "Topology/generic/cellmarker.h"
Sylvain Thery's avatar
Sylvain Thery committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "Geometry/vector_gen.h"
#include "Utils/colorMaps.h"
#include "Utils/vbo.h"


namespace CGoGN
{

namespace Algo
{

namespace Histogram
{

class HistoColorMap
{
protected:
	double m_min;
	double m_max;
	double m_w;
	unsigned int m_nb;
public:

	virtual ~HistoColorMap() {}

	/// set min value (for update from Histogram)
	void setMin(double m) {m_min = m; m_w = (m_max -m_min)/double(m_nb);}

	/// set max value (for update from Histogram)
	void setMax(double m) {m_max = m; m_w = (m_max -m_min)/double(m_nb);}

	/// set nb value (for update from Histogram)
	void setNb(unsigned int n) {m_nb = n; m_w = (m_max -m_min)/double(m_nb);}

	/**
	 * get color from param: To implement (with call to colormap functions for examples)
	 */
	virtual Geom::Vec3f color(double v) const = 0 ;

	/**
	 * get color from index (can be overload)
	 * compute a [0,1[ value from an index [0,nb[
	 * and call color with it
	 * Used by Histogram::colorize && Qt::DrawHistogram
	 */
	virtual Geom::Vec3f colorIndex(unsigned int i) const
	{
		double v = double(i)/double(m_nb-1) + double(1)/double(m_nb+m_nb);
		return color(v);
	}
};


/**
 *  inherits this class by:
 *  - add xxAttribute& in data (& constructor)
 *  - overload begin/end/next/nbElements (by calling xxxAttribute.yyy)
 *  - overload operator [] to return the [i] converted in double with necessary computations.
 */
class AttributeConvertGen
{
public:
	virtual unsigned int begin() const = 0;
	virtual unsigned int end() const = 0;
	virtual void next(unsigned int& i) const = 0;
	virtual unsigned int nbElements() const = 0;
	virtual double operator[](unsigned int i) const = 0;
	virtual ~AttributeConvertGen() {}
};

/**
 * Helper class templated by Attribute
 * Avoid the writing of begin/end/next/nbElements
 */
template <typename ATT>
class AttributeConvert: public AttributeConvertGen
{
protected:
	ATT& attrib;
public:
	AttributeConvert(ATT &att): attrib(att) {}
	virtual unsigned int begin() const { return attrib.begin();}
	virtual unsigned int end() const { return attrib.end();}
	virtual void next(unsigned int& i) const { attrib.next(i);}
	virtual unsigned int nbElements() const { return attrib.nbElements();}
	virtual double operator[](unsigned int i) const = 0;
	virtual ~AttributeConvert() {}
};


/**
 * Histogram class
 * T must have operators -, / ,< ,>
 */
class Histogram
{

//	std::vector<double> m_data;

Sylvain Thery's avatar
Sylvain Thery committed
129
	mutable std::vector< std::pair<double, unsigned int> > m_dataIdx;
Sylvain Thery's avatar
Sylvain Thery committed
130
131
132
133
134
135
136
	
	/// number of classes in attribute
	unsigned int m_nbclasses;
	
	/// vector of population
	std::vector<unsigned int> m_populations;
	
Sylvain Thery's avatar
Sylvain Thery committed
137
	/// vector of intervals of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
138
139
	std::vector<double> m_interv;

Sylvain Thery's avatar
Sylvain Thery committed
140
141
	/// vector of population for quantiles
	std::vector<double> m_pop_quantiles;
Sylvain Thery's avatar
Sylvain Thery committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162

	/// min value
	double m_min;

	/// max value
	double m_max;

	/// interval width (in regular case)
	double m_interWidth;
	
	/// max number of population in a class
	unsigned int m_nbMin;

	/// max values in histo population
	unsigned int m_maxBar;

	/// max value in quantille population
	double m_maxQBar;

	HistoColorMap& m_hcolmap;

Sylvain Thery's avatar
Sylvain Thery committed
163
	mutable bool m_sorted;
Sylvain Thery's avatar
Sylvain Thery committed
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191

	/// get data
	double data(unsigned int i) const;

	/// get idx of data in attribute
	unsigned int idx(unsigned int i) const;

	// comparison function for sorting data
	static bool dataComp( const std::pair<double, unsigned int>& a, const std::pair<double, unsigned int>& b);


public:
		/**
		* create an histogram from attribute handler
		*/
		Histogram(HistoColorMap& hcm);


		/**
		 * init data
		 * @param conv a attribute convertor
		 */
		void initDataConvert(const AttributeConvertGen& conv);

		
		/**
		 * init data
		 * @param attr the attribute to copy from
Sylvain Thery's avatar
Sylvain Thery committed
192
		 * @param sortForQuantiles sort data vector for quantille generation
Sylvain Thery's avatar
Sylvain Thery committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
		 */
		template <typename ATTR>
		void initData(const ATTR& attr);

		/**
		 * get min value of attribute (perhaps modified by user)
		 */
		double getMin() const;

		/**
		 * get max value of attribute (perhaps modified by user)
		 */
		double getMax() const;

		/**
		 * get real min value of attribute
		 */
		double getQMin() const;

		/**
		 * get real max value of attribute
		 */
		double getQMax() const;


		/**
		 * set min value of attribute
		 */
		void setMin(double m);

		/**
		 * set max value of attribute
		 */
		void setMax(double m);

		/**
		 * get max population value of all bars of histo
		 */
		unsigned int getMaxBar() const;

		/**
Sylvain Thery's avatar
Sylvain Thery committed
234
		 * get max population value of all bars of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		 */
		double getMaxQBar() const;


		/**
		 * modify min/max values to center Histogram on zero if necessary
		 */
		void centerOnZero();

		/**
		 * compute the histogram with given numbre of classes
		 */
		void populateHisto(unsigned int nbclasses=0);

		/**
		 * compute the histogram with given number of classes
		 */
Sylvain Thery's avatar
Sylvain Thery committed
252
		void populateQuantiles(unsigned int nbclasses=10);
Sylvain Thery's avatar
Sylvain Thery committed
253
254
255
256
257
258
259
260
261
262
263
264

		/**
		* which class belong a value
		*/
		unsigned int whichClass(double val) const;

		/**
		* which class belong a value
		*/
		unsigned int whichQuantille(double val) const;

		/**
Sylvain Thery's avatar
Sylvain Thery committed
265
		* fill a color attribute from histo
Sylvain Thery's avatar
Sylvain Thery committed
266
267
268
269
270
271
272
		* @param colors attribute to fill
		*/
		template <typename ATTC>
		void histoColorize(ATTC& colors);


		/**
Sylvain Thery's avatar
Sylvain Thery committed
273
		 * colorize the VBO (RGB) from histo
Sylvain Thery's avatar
Sylvain Thery committed
274
275
276
277
278
279
280
		 * @warning GL context must be accessible
		 * @param vbo the vbo to fill with colors
		 */
		void histoColorizeVBO(Utils::VBO& vbo);


		/**
Sylvain Thery's avatar
Sylvain Thery committed
281
		* fill a color attribute from quantiles
Sylvain Thery's avatar
Sylvain Thery committed
282
283
284
285
		* @param colors attribute to fill
		* @param tc table of color
		*/
		template<typename ATTC>
Sylvain Thery's avatar
Sylvain Thery committed
286
		void quantilesColorize(ATTC& colors, const std::vector<Geom::Vec3f>& tc);
Sylvain Thery's avatar
Sylvain Thery committed
287
288

		/**
Sylvain Thery's avatar
Sylvain Thery committed
289
290
291
292
		* colorize the VBO (RGB) from
		* @warning GL context must be accessible
		* @param vbo the vbo to fill with colors
		* @param tc table of color
Sylvain Thery's avatar
Sylvain Thery committed
293
		*/
Sylvain Thery's avatar
Sylvain Thery committed
294
		void quantilesColorizeVBO(Utils::VBO& vbo, const std::vector<Geom::Vec3f>& tc);
Sylvain Thery's avatar
Sylvain Thery committed
295
296
297
298
299
300
301

		/**
		* get the vector of class population
		*/
		const std::vector<unsigned int>& getPopulation() const;

		/**
Sylvain Thery's avatar
Sylvain Thery committed
302
		* get the vector of height of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
303
		*/
Sylvain Thery's avatar
Sylvain Thery committed
304
		const std::vector<double>& getQuantilesHeights() const;
Sylvain Thery's avatar
Sylvain Thery committed
305
306

		/**
Sylvain Thery's avatar
Sylvain Thery committed
307
		* get the vector of intervals bounaries for quantiles
Sylvain Thery's avatar
Sylvain Thery committed
308
		*/
Sylvain Thery's avatar
Sylvain Thery committed
309
310
311
312
313
314
		const std::vector<double>& getQuantilesIntervals() const;

		/**
		 * return cells of histogram's column
		 * @param c column of histogram
		 * @param vc vector of cells (indices)
Sylvain Thery's avatar
Sylvain Thery committed
315
		 * @return number of cells
Sylvain Thery's avatar
Sylvain Thery committed
316
		 */
Sylvain Thery's avatar
Sylvain Thery committed
317
		unsigned int cellsOfHistogramColumn( unsigned int c, std::vector<unsigned int>& vc) const;
Sylvain Thery's avatar
Sylvain Thery committed
318
319
320
321
322

		/**
		 * return cells of quantile's column
		 * @param c column of quantile
		 * @param vc vector of cells (indices)
Sylvain Thery's avatar
Sylvain Thery committed
323
		 * @return number of cells
Sylvain Thery's avatar
Sylvain Thery committed
324
		 */
Sylvain Thery's avatar
Sylvain Thery committed
325
		unsigned int cellsOfQuantilesColumn( unsigned int c, std::vector<unsigned int>& vc) const;
Sylvain Thery's avatar
Sylvain Thery committed
326

Sylvain Thery's avatar
Sylvain Thery committed
327

Sylvain Thery's avatar
Sylvain Thery committed
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
		/**
		 * mark cells of histogram's column
		 * @param c column of quantile
		 * @param cm marker
		 * @return number of marked cells
		 */
		template <typename CELLMARKER>
		unsigned int markCellsOfHistogramColumn(unsigned int c, CELLMARKER& cm) const;

		/**
		 * mark cells of quantile's column
		 * @param c column of quantile
		 * @param cm marker
		 * @return number of marked cells
		 */
		template <typename CELLMARKER>
		unsigned int markCellsOfQuantilesColumn(unsigned int c, CELLMARKER& cm) const;

Sylvain Thery's avatar
Sylvain Thery committed
346
347
348
349
350
		/**
		 * get the colorMap
		 */
		const HistoColorMap& colorMap() const;

Sylvain Thery's avatar
Sylvain Thery committed
351

Sylvain Thery's avatar
Sylvain Thery committed
352
353
354
355
356
357
358
359
360
361
362
};


}
}
}

#include "Algo/Histogram/histogram.hpp"
#endif