histogram.h 9.23 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
/*******************************************************************************
* 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__

unknown's avatar
unknown committed
28 29 30 31
#define _USE_MATH_DEFINES
#include <cmath>


Sylvain Thery's avatar
Sylvain Thery committed
32
#include "Topology/generic/attributeHandler.h"
Sylvain Thery's avatar
Sylvain Thery committed
33
#include "Topology/generic/cellmarker.h"
Sylvain Thery's avatar
Sylvain Thery committed
34 35
#include "Geometry/vector_gen.h"
#include "Utils/colorMaps.h"
Thery Sylvain's avatar
Thery Sylvain committed
36
#include "Utils/vbo_base.h"
Sylvain Thery's avatar
Sylvain Thery committed
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 129 130 131 132


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
133
	mutable std::vector< std::pair<double, unsigned int> > m_dataIdx;
Sylvain Thery's avatar
Sylvain Thery committed
134 135 136 137 138 139 140
	
	/// 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
141
	/// vector of intervals of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
142 143
	std::vector<double> m_interv;

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

	/// 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
167
	mutable bool m_sorted;
Sylvain Thery's avatar
Sylvain Thery committed
168 169 170 171 172 173 174

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

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

175
	/// comparison function for sorting data
Sylvain Thery's avatar
Sylvain Thery committed
176 177
	static bool dataComp( const std::pair<double, unsigned int>& a, const std::pair<double, unsigned int>& b);

178 179
	/// update quantiles height from histo area for correct superposition
	void quantilesAreaCorrection();
Sylvain Thery's avatar
Sylvain Thery committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

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
197
		 * @param sortForQuantiles sort data vector for quantille generation
Sylvain Thery's avatar
Sylvain Thery committed
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 234 235 236 237 238
		 */
		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
239
		 * get max population value of all bars of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		 */
		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
257
		void populateQuantiles(unsigned int nbclasses=10);
Sylvain Thery's avatar
Sylvain Thery committed
258 259 260 261 262 263 264 265 266 267 268 269

		/**
		* 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
270
		* fill a color attribute from histo
Sylvain Thery's avatar
Sylvain Thery committed
271 272 273 274 275 276 277
		* @param colors attribute to fill
		*/
		template <typename ATTC>
		void histoColorize(ATTC& colors);


		/**
Sylvain Thery's avatar
Sylvain Thery committed
278
		 * colorize the VBO (RGB) from histo
Sylvain Thery's avatar
Sylvain Thery committed
279 280 281 282 283 284 285
		 * @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
286
		* fill a color attribute from quantiles
Sylvain Thery's avatar
Sylvain Thery committed
287 288 289 290
		* @param colors attribute to fill
		* @param tc table of color
		*/
		template<typename ATTC>
Sylvain Thery's avatar
Sylvain Thery committed
291
		void quantilesColorize(ATTC& colors, const std::vector<Geom::Vec3f>& tc);
Sylvain Thery's avatar
Sylvain Thery committed
292 293

		/**
Sylvain Thery's avatar
Sylvain Thery committed
294 295 296 297
		* 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
298
		*/
Sylvain Thery's avatar
Sylvain Thery committed
299
		void quantilesColorizeVBO(Utils::VBO& vbo, const std::vector<Geom::Vec3f>& tc);
Sylvain Thery's avatar
Sylvain Thery committed
300 301 302 303 304 305 306

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

		/**
Sylvain Thery's avatar
Sylvain Thery committed
307
		* get the vector of height of quantiles
Sylvain Thery's avatar
Sylvain Thery committed
308
		*/
Sylvain Thery's avatar
Sylvain Thery committed
309
		const std::vector<double>& getQuantilesHeights() const;
Sylvain Thery's avatar
Sylvain Thery committed
310 311

		/**
Sylvain Thery's avatar
Sylvain Thery committed
312
		* get the vector of intervals bounaries for quantiles
Sylvain Thery's avatar
Sylvain Thery committed
313
		*/
Sylvain Thery's avatar
Sylvain Thery committed
314 315 316 317 318 319
		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
320
		 * @return number of cells
Sylvain Thery's avatar
Sylvain Thery committed
321
		 */
Sylvain Thery's avatar
Sylvain Thery committed
322
		unsigned int cellsOfHistogramColumn( unsigned int c, std::vector<unsigned int>& vc) const;
Sylvain Thery's avatar
Sylvain Thery committed
323 324 325 326 327

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

Sylvain Thery's avatar
Sylvain Thery committed
332

Sylvain Thery's avatar
Sylvain Thery committed
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
		/**
		 * 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
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
		/**
		 * get the colorMap
		 */
		const HistoColorMap& colorMap() const;

};


}
}
}

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