histogram.cpp 5.6 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
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Histogram/histogram.h"
Sylvain Thery's avatar
Sylvain Thery committed
26

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
50
51
52
53
54
55
56
57
58
59
60
61
62

namespace CGoGN
{

namespace Algo
{

namespace Histogram
{


void Histogram::initDataConvert(const AttributeConvertGen& conv)
{
	unsigned int beg = conv.begin();
	m_min = conv[beg];
	m_max = m_min;

	m_dataIdx.reserve(conv.nbElements());
	for (unsigned int i = beg; i!= conv.end(); conv.next(i))
	{
		double val = conv[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);
}

void Histogram::populateHisto(unsigned int nbclasses)
{
	//compute nb classes if necesary
	if (nbclasses == 0)
unknown's avatar
unknown committed
63
		m_nbclasses = (unsigned int)(sqrt(double(m_dataIdx.size())));
Sylvain Thery's avatar
Sylvain Thery committed
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
	else
		m_nbclasses = nbclasses;

	m_hcolmap.setNb(m_nbclasses);

	//compute width interv
	m_interWidth = (m_max-m_min)/double(m_nbclasses);

	// init to zero
	m_populations.resize(m_nbclasses);
	for (unsigned int i = 0; i<m_nbclasses; ++i)
		m_populations[i] = 0;

	// traverse attribute to populate
	for (std::vector<std::pair<double,unsigned int> >::const_iterator it = m_dataIdx.begin(); it != m_dataIdx.end(); ++it)
	{
		unsigned int c = whichClass(it->first);
		if (c != 0xffffffff)
			m_populations[c]++;
	}
    m_maxBar = 0;
    for (unsigned int i = 0; i<m_nbclasses; ++i)
    {
    	if (m_populations[i] > m_maxBar)
    		m_maxBar = m_populations[i];
    }
}

Sylvain Thery's avatar
Sylvain Thery committed
92
void Histogram::populateQuantiles(unsigned int nbquantiles)
Sylvain Thery's avatar
Sylvain Thery committed
93
94
95
96
97
98
99
100
101
{
	if (!m_sorted)
	{
		std::sort(m_dataIdx.begin(),m_dataIdx.end(),dataComp);
		m_sorted = true;
	}

	// compute exact populations
	unsigned int nb = m_dataIdx.size();
Sylvain Thery's avatar
Sylvain Thery committed
102
103
	double pop = double(nb)/nbquantiles;
	m_pop_quantiles.resize(nbquantiles);
Sylvain Thery's avatar
Sylvain Thery committed
104

Sylvain Thery's avatar
Sylvain Thery committed
105
106
	for (unsigned int i = 0; i < nbquantiles; ++i)
		m_pop_quantiles[i]=pop;
Sylvain Thery's avatar
Sylvain Thery committed
107
108

	m_interv.clear();
Sylvain Thery's avatar
Sylvain Thery committed
109
110
	m_interv.reserve(nbquantiles+1);
	// quantiles computation
Sylvain Thery's avatar
Sylvain Thery committed
111
112
	m_interv.push_back(m_dataIdx.front().first);
	double cumul = 0.0;
Sylvain Thery's avatar
Sylvain Thery committed
113
	for (unsigned int i = 0; i < nbquantiles; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
114
	{
Sylvain Thery's avatar
Sylvain Thery committed
115
		cumul += m_pop_quantiles[i];
Sylvain Thery's avatar
Sylvain Thery committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
		unsigned int icum = floor(cumul);
		double val = 0.0;
		if (icum < m_dataIdx.size()-1)
			val = (data(icum)+ data(icum+1)) / 2.0;
		else
			val = m_dataIdx.back().first;
		m_interv.push_back(val);
	}

	m_maxQBar = 0.0;

	// constant area correction
	double lc = 1.0f;

	if (m_nbclasses != 0) //for histo superposition
		lc = double(m_dataIdx.back().first - m_dataIdx.front().first )/double(m_nbclasses);

Sylvain Thery's avatar
Sylvain Thery committed
133
	for (unsigned int i = 0; i < nbquantiles; ++i)
Sylvain Thery's avatar
Sylvain Thery committed
134
135
	{
		double lq = m_interv[i+1] - m_interv[i];
Sylvain Thery's avatar
Sylvain Thery committed
136
137
138
		m_pop_quantiles[i] = m_pop_quantiles[i]*lc/lq;
		if (m_pop_quantiles[i] > m_maxQBar)
			m_maxQBar = m_pop_quantiles[i];
Sylvain Thery's avatar
Sylvain Thery committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	}
}


void Histogram::histoColorizeVBO(Utils::VBO& vbo)
{
	unsigned int nb = m_dataIdx.size();
	vbo.setDataSize(3);
	vbo.allocate(nb);
	Geom::Vec3f* colors = static_cast<Geom::Vec3f*>(vbo.lockPtr());
	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);
	}
	vbo.releasePtr();
}
Sylvain Thery's avatar
Sylvain Thery committed
158
159


Sylvain Thery's avatar
Sylvain Thery committed
160
unsigned int Histogram::cellsOfHistogramColumn(unsigned int c, std::vector<unsigned int>& vc) const
Sylvain Thery's avatar
Sylvain Thery committed
161
162
163
164
165
166
167
168
169
{
	if (!m_sorted)
	{
		std::sort(m_dataIdx.begin(),m_dataIdx.end(),dataComp);
		m_sorted = true;
	}

	vc.clear();

Sylvain Thery's avatar
Sylvain Thery committed
170
171
	double bi = (m_max-m_min)/m_nbclasses * c + m_min;
	double bs = (m_max-m_min)/m_nbclasses * (c+1) + m_min;
Sylvain Thery's avatar
Sylvain Thery committed
172
173
174
175
176
177
178
179

	unsigned int nb=m_dataIdx.size();
	unsigned int i=0;

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

	while ((i<nb) && (data(i)< bs))
Sylvain Thery's avatar
Sylvain Thery committed
180
		vc.push_back(idx(i++));
Sylvain Thery's avatar
Sylvain Thery committed
181

Sylvain Thery's avatar
Sylvain Thery committed
182
	return vc.size();
Sylvain Thery's avatar
Sylvain Thery committed
183
184
}

Sylvain Thery's avatar
Sylvain Thery committed
185
unsigned int Histogram::cellsOfQuantilesColumn( unsigned int c, std::vector<unsigned int>& vc) const
Sylvain Thery's avatar
Sylvain Thery committed
186
187
188
189
190
191
192
193
194
195
196
197
198
{
	vc.clear();

	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;

	while ((i<nb) && (data(i)< bs))
Sylvain Thery's avatar
Sylvain Thery committed
199
		vc.push_back(idx(i++));
Sylvain Thery's avatar
Sylvain Thery committed
200

Sylvain Thery's avatar
Sylvain Thery committed
201
	return vc.size();
Sylvain Thery's avatar
Sylvain Thery committed
202
203
204
}


Sylvain Thery's avatar
Sylvain Thery committed
205
206


Sylvain Thery's avatar
Sylvain Thery committed
207
208
209
210
211
}
}
}