Création d'un compte pour un collaborateur extérieur au laboratoire depuis l'intranet ICube : https://intranet.icube.unistra.fr/fr/labs/member/profile

histogram.cpp 4.79 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
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*******************************************************************************
* 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"
#include <algorithm>

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

void Histogram::populateQuantilles(unsigned int nbquantilles)
{
	if (!m_sorted)
	{
		std::sort(m_dataIdx.begin(),m_dataIdx.end(),dataComp);
		m_sorted = true;
	}

	// compute exact populations
	unsigned int nb = m_dataIdx.size();
	double pop = double(nb)/nbquantilles;
	m_pop_quantilles.resize(nbquantilles);

	for (unsigned int i = 0; i < nbquantilles; ++i)
		m_pop_quantilles[i]=pop;

	m_interv.clear();
	m_interv.reserve(nbquantilles+1);
	// quantilles computation
	m_interv.push_back(m_dataIdx.front().first);
	double cumul = 0.0;
	for (unsigned int i = 0; i < nbquantilles; ++i)
	{
		cumul += m_pop_quantilles[i];
		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);

	for (unsigned int i = 0; i < nbquantilles; ++i)
	{
		double lq = m_interv[i+1] - m_interv[i];
		m_pop_quantilles[i] = m_pop_quantilles[i]*lc/lq;
		if (m_pop_quantilles[i] > m_maxQBar)
			m_maxQBar = m_pop_quantilles[i];
	}
}


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();
}
}
}
}