histogram.hpp 5.87 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
46
47
48
49
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
50
inline const std::vector<double>& Histogram::getQuantilesHeights() const
Sylvain Thery's avatar
Sylvain Thery committed
51
{
Sylvain Thery's avatar
Sylvain Thery committed
52
	return m_pop_quantiles;
Sylvain Thery's avatar
Sylvain Thery committed
53
54
55
}


Sylvain Thery's avatar
Sylvain Thery committed
56
inline const std::vector<double>&  Histogram::getQuantilesIntervals() const
Sylvain Thery's avatar
Sylvain Thery committed
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
133
134
135
136
{
	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());
137
	m_dataIdx.clear();
Sylvain Thery's avatar
Sylvain Thery committed
138
139
140
141
142
143
144
145
146
147
148
149
150
	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);

151
	m_sorted = false;
Sylvain Thery's avatar
Sylvain Thery committed
152
153
154
155
156
157
158
159
160
161
162
163
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
}



inline unsigned int Histogram::whichClass(double val) const
{
	if (val == m_max)
		return m_populations.size()-1;
	double x = (val - m_min)/m_interWidth;
	if ((x<0) || (val>=m_max))
		return -1;
	return (unsigned int)(x);
}


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)
{
	unsigned int nb = m_dataIdx.size();
	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
192
void Histogram::quantilesColorize(ATTC& colors, const std::vector<Geom::Vec3f>& tc)
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
{

	unsigned int nb = m_dataIdx.size();
	unsigned int nbi = m_interv.size()-1;

	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;
}
/// 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
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;

	unsigned int nb=m_dataIdx.size();
	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];

	unsigned int nb=m_dataIdx.size();
	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;
}



Sylvain Thery's avatar
Sylvain Thery committed
282
283
284
285
286
287

}
}
}