histogram.hpp 5.95 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25 26
#include <algorithm>

Sylvain Thery's avatar
Sylvain Thery committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace CGoGN
{

namespace Algo
{

namespace Histogram
{

inline Histogram::Histogram( HistoColorMap& hcm):
 m_hcolmap(hcm),m_sorted(false)
{
}

inline const std::vector<unsigned int>& Histogram::getPopulation() const
{
	return m_populations;
}

Sylvain Thery's avatar
Sylvain Thery committed
46
inline const std::vector<double>& Histogram::getQuantilesHeights() const
Sylvain Thery's avatar
Sylvain Thery committed
47
{
Sylvain Thery's avatar
Sylvain Thery committed
48
	return m_pop_quantiles;
Sylvain Thery's avatar
Sylvain Thery committed
49 50
}

Sylvain Thery's avatar
Sylvain Thery committed
51
inline const std::vector<double>&  Histogram::getQuantilesIntervals() const
Sylvain Thery's avatar
Sylvain Thery committed
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
{
	return m_interv;
}

inline const HistoColorMap& Histogram::colorMap() const
{
	return m_hcolmap;
}

inline double Histogram::getMin() const
{
	return m_min;
}

inline double Histogram::getMax() const
{
	return m_max;
}

inline double Histogram::getQMin() const
{
	return m_dataIdx.front().first;
}

inline double Histogram::getQMax() const
{
	return m_dataIdx.back().first;
}

inline unsigned int Histogram::getMaxBar() const
{
	return m_maxBar;
}

inline double Histogram::getMaxQBar() const
{
	return m_maxQBar;
}

inline void Histogram::setMin(double m)
{
	m_min = m;
	m_hcolmap.setMin(m);
}

inline void Histogram::setMax(double m)
{
	m_max = m;
	m_hcolmap.setMax(m);
}

inline void Histogram::centerOnZero()
{
	if ((m_min <0.0) && (m_max > 0.0))
	{
		if ((-m_min) > m_max)
			m_max = -m_min;
		else
			m_min = -m_max;
	}
}

template <typename ATTR>
inline void Histogram::initData(const ATTR& attr)
{
	unsigned int beg = attr.begin();
	m_min = attr[beg];
	m_max = m_min;

	m_dataIdx.reserve(attr.nbElements());
122
	m_dataIdx.clear();
Sylvain Thery's avatar
Sylvain Thery committed
123 124 125 126 127 128 129 130 131 132 133 134 135
	for (unsigned int i = beg; i!= attr.end(); attr.next(i))
	{
		double val = attr[i];
		m_dataIdx.push_back(std::make_pair(val ,i));
		if (val < m_min)
			m_min = val;
		if (val > m_max)
			m_max = val;
	}

	m_hcolmap.setMin(m_min);
	m_hcolmap.setMax(m_max);

136
	m_sorted = false;
Sylvain Thery's avatar
Sylvain Thery committed
137 138 139 140 141
}

inline unsigned int Histogram::whichClass(double val) const
{
	if (val == m_max)
142
		return uint32(m_populations.size()-1);
Sylvain Thery's avatar
Sylvain Thery committed
143 144 145
	double x = (val - m_min)/m_interWidth;
	if ((x<0) || (val>=m_max))
		return -1;
146
	return uint32(x);
Sylvain Thery's avatar
Sylvain Thery committed
147 148 149 150 151 152 153 154 155 156 157 158 159
}

inline unsigned int Histogram::whichQuantille(double val) const
{
	unsigned int i=1;
	while (val > m_interv[i])
		++i;
	return i-1;
}

template<typename ATTC>
void Histogram::histoColorize(ATTC& colors)
{
Sylvain Thery's avatar
Sylvain Thery committed
160
	unsigned int nb = uint32(m_dataIdx.size());
Sylvain Thery's avatar
Sylvain Thery committed
161 162 163 164 165 166 167 168 169 170
	for (unsigned int i = 0; i<nb; ++i)
	{
		unsigned int j = idx(i);
		unsigned int c = whichClass(data(i));
		if (c != 0xffffffff)
			colors[j] = m_hcolmap.colorIndex(c);
	}
}

template<typename ATTC>
Sylvain Thery's avatar
Sylvain Thery committed
171
void Histogram::quantilesColorize(ATTC& colors, const std::vector<Geom::Vec3f>& tc)
Sylvain Thery's avatar
Sylvain Thery committed
172 173
{

Sylvain Thery's avatar
Sylvain Thery committed
174 175
	unsigned int nb = uint32(m_dataIdx.size());
	unsigned int nbi = uint32(m_interv.size() - 1);
Sylvain Thery's avatar
Sylvain Thery committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

	assert(tc.size() >= nbi);

	unsigned int i=0;
	unsigned int j=0;

	while ((i<nb) && (j<nbi))
	{
		while ((i<nb) && (data(i) <= m_interv[j+1]))
			colors[ idx(i++) ] = tc[j];
		++j;
	}
}

/// get data
inline double Histogram::data(unsigned int i) const
{
	return m_dataIdx[i].first;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
195

Sylvain Thery's avatar
Sylvain Thery committed
196 197 198 199 200 201 202 203 204 205 206 207
/// get idx of data in attribute
inline unsigned int Histogram::idx(unsigned int i) const
{
	return m_dataIdx[i].second;
}

// comparison function for sorting data
inline bool Histogram::dataComp( const std::pair<double, unsigned int>& a, const std::pair<double, unsigned int>& b)
{
	return a.first < b.first;
}

Sylvain Thery's avatar
Sylvain Thery committed
208 209 210 211 212 213 214 215 216 217 218 219
template <typename CELLMARKER>
unsigned int Histogram::markCellsOfHistogramColumn(unsigned int c, CELLMARKER& cm) const
{
	if (!m_sorted)
	{
		std::sort(m_dataIdx.begin(),m_dataIdx.end(),dataComp);
		m_sorted = true;
	}

	double bi = (m_max-m_min)/m_nbclasses * c + m_min;
	double bs = (m_max-m_min)/m_nbclasses * (c+1) + m_min;

220
	unsigned int nb = uint32(m_dataIdx.size());
Sylvain Thery's avatar
Sylvain Thery committed
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	unsigned int i=0;

	while ((i<nb) && (data(i)< bi))
		++i;

	unsigned int nbc=0;
	while ((i<nb) && (data(i)< bs))
	{
		cm.mark(idx(i++));
		++nbc;
	}

	return nbc;
}

template <typename CELLMARKER>
unsigned int Histogram::markCellsOfQuantilesColumn(unsigned int c, CELLMARKER& cm) const
{
	double bi = m_interv[c];
	double bs = m_interv[c+1];

Sylvain Thery's avatar
Sylvain Thery committed
242
	unsigned int nb = uint32(m_dataIdx.size());
Sylvain Thery's avatar
Sylvain Thery committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
	unsigned int i=0;

	while ((i<nb) && (data(i)< bi))
		++i;

	unsigned int nbc=0;
	while ((i<nb) && (data(i)< bs))
	{
		cm.mark(idx(i++));
		++nbc;
	}

	return nbc;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
258
} // namespace Histogram
Sylvain Thery's avatar
Sylvain Thery committed
259

Pierre Kraemer's avatar
Pierre Kraemer committed
260
} // namespace Algo
Sylvain Thery's avatar
Sylvain Thery committed
261

Pierre Kraemer's avatar
Pierre Kraemer committed
262
} // namespace CGoGN