tuto_histo.cpp 9.6 KB
Newer Older
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
/*******************************************************************************
* 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 "tuto_histo.h"
#include "Algo/Geometry/boundingbox.h"
#include "Algo/Import/import.h"

#include "Algo/Geometry/area.h"


#include "Topology/generic/traversorCell.h"

#include <cmath>
#include <sstream>

using namespace CGoGN ;



int main(int argc, char **argv)
{
	//	// interface
	QApplication app(argc, argv);
	MyQT sqt;
	// copy output tout Qt console of application (shift enter)
	CGoGNout.toConsole(&sqt);

	// example code itself
	sqt.createMap(std::string(argv[1]));
	// set help message in menu
Sylvain Thery's avatar
Sylvain Thery committed
52
53
54
55
	sqt.setHelpMsg("Tuto Histogram: \nLoad mesh & compute histogram & quantiles\n+/- increase/decrease \
number of classes\no/p increase/decrease number of quantiles\nw/x change \
min/max\nc center the histogram on 0\nh show/hide histogram\nq show/hide quantiles\
File/Save for exporting histogram in svg format");
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

	// final show for redraw
	sqt.show();
	// and wait for the end
	return app.exec();
}

void MyQT::createMap(const std::string& filename)
{
	myMap.clear(true) ;

	size_t pos = filename.rfind(".");    // position of "." in filename
	std::string extension = filename.substr(pos);

	std::vector<std::string> attrNames ;
	if(!Algo::Import::importMesh<PFP>(myMap, filename.c_str(), attrNames))
	{
		CGoGNerr << "could not import " << filename << CGoGNendl ;
		return;
	}
	VertexAttribute<VEC3> position = myMap.getAttribute<PFP::VEC3, VERTEX>(attrNames[0]) ;

	// attribute on which we make the histogram
	VertexAttribute<float> area = myMap.addAttribute<float, VERTEX>("area");

	// attribute color to generate from histo
	VertexAttribute<VEC3> colorF = myMap.addAttribute<PFP::VEC3, VERTEX>("colorF");

	// compute the area attribute
	Algo::Geometry::computeOneRingAreaVertices<PFP>(myMap,position,area);

	// just some tricks to obtain relatives value
	float amax=0;
	for (unsigned int i=area.begin(); i != area.end(); area.next(i))
		if (area[i] > amax) amax = area[i];
	for (unsigned int i=area.begin(); i != area.end(); area.next(i))
		area[i] -= amax/4;


	// create a colormap class for histogram coloring
	l_cm = new ColMap();

	// create the histogram
	l_histo = new Algo::Histogram::Histogram(*l_cm);


	// init data with specific conversion (direction init  is: l_histo->initData(area);)
	AttConv ac(area);
	l_histo->initDataConvert(ac);

	// compute the histogram
	l_nbc = 20;
	l_histo->populateHisto(l_nbc);

Sylvain Thery's avatar
Sylvain Thery committed
110
	//compute the quantiles
111
	l_nbq = 10;
Sylvain Thery's avatar
Sylvain Thery committed
112
	l_histo->populateQuantiles(l_nbq);
113
114
115
116
117
118
119
120
121
122
123
124

	// compute color attribute from histogram (histogram can alsdo directly update a VBO see below)
//	l_histo->histoColorize(colorF);


	// create a popup window
	l_popup = new Utils::QT::QtPopUp(this);

	// create the widget to view histogram
	l_histodraw = new Utils::QT::RenderHistogram(l_popup, *l_histo);

	// some simple parameters
Sylvain Thery's avatar
Sylvain Thery committed
125
	l_histodraw->setQuantilesDraw(true);
126
127
128
129
130
131
132
	l_histodraw->setHistoPosition(true);
	l_histodraw->setOpacity(0.6f);

	// create a table of color
	std::vector<Geom::Vec3f> colors;
	Utils::createTableColor01(colors,10,Utils::color_map_blue_green_red);

Sylvain Thery's avatar
Sylvain Thery committed
133
134
	// and us it to color the quantiles
	l_histodraw->setQuantilesColors(colors);
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
163
164
165
166
167
168
169
170
171
172
173
174
175

	// add the widget to the popup
	l_popup->addWidget(l_histodraw,0,0);
	l_popup->show();

	// connect the clicked signal of histogram widget to a slot
	QObject::connect( l_histodraw, SIGNAL( clicked(unsigned int,unsigned int) ), this, SLOT( clickHisto(unsigned int,unsigned int) ) );


    //  bounding box of scene
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
    float lWidthObj = std::max<PFP::REAL>(std::max<PFP::REAL>(bb.size(0), bb.size(1)), bb.size(2));
    Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);

    // send BB info to interface for centering on GL screen
	setParamObject(lWidthObj, lPosObj.data());

	// first show for be sure that GL context is binded
	show();

	// update of position VBO 	(context GL necessary)
	m_positionVBO->updateData(position);

	//	m_colorVBO2->updateData(colorF);
	// histogram can directly update a VBO (warning GL context must be accessible, after the first show!)
	l_histo->histoColorizeVBO(*m_colorVBO2);

	// construct rendering primities
	m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);


}

void MyQT::cb_keyPress(int keycode)
{
	switch(keycode)
	{
    	case '+' :
    		l_nbc++;
    		l_histo->populateHisto(l_nbc);
    		l_histodraw->repaint();
176
177
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
178
179
180
181
182
183
       		break;
       	case '-' :
       		if (l_nbc>0)
       			l_nbc--;
    		l_histo->populateHisto(l_nbc);
    		l_histodraw->repaint();
184
185
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
186
187
188
189
    		break;

       	case 'p' :
       		l_nbq++;
Sylvain Thery's avatar
Sylvain Thery committed
190
			l_histo->populateQuantiles(l_nbq);
191
192
193
194
195
			l_histodraw->repaint();
			break;
		case 'o' :
			if (l_nbq>0)
				l_nbq--;
Sylvain Thery's avatar
Sylvain Thery committed
196
			l_histo->populateQuantiles(l_nbq);
197
198
199
			l_histodraw->repaint();
		break;
		case 'q':
Sylvain Thery's avatar
Sylvain Thery committed
200
			l_histodraw->setQuantilesDraw(!l_histodraw->getQuantilesDraw());
201
202
203
204
205
206
207
208
209
210
211
			l_histodraw->repaint();
			break;
		case 'h':
			l_histodraw->setHistoDraw(!l_histodraw->getHistoDraw());
			l_histodraw->repaint();
			break;

       	case 'c' :
			l_histo->centerOnZero();
			l_histo->populateHisto(l_nbc);
			l_histodraw->repaint();
212
213
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
214
215
216
217
218
219
220
221
222
			break;

       	case 'w' :
       	{
       		double av = (l_histo->getMin() + l_histo->getMax() )/2.0;
       		l_histo->setMin(av*0.1 + l_histo->getMin()*0.9);
       		l_histo->setMax(av*0.1 + l_histo->getMax()*0.9);
			l_histo->populateHisto(l_nbc);
			l_histodraw->repaint();
223
224
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
225
226
227
228
229
230
231
232
233
       	}
			break;
       	case 'x' :
       	{
       		double av = (l_histo->getMin() + l_histo->getMax() )/2.0;
       		l_histo->setMin(-0.1*av + l_histo->getMin()*1.1);
       		l_histo->setMax(-0.1*av + l_histo->getMax()*1.1);
			l_histo->populateHisto(l_nbc);
			l_histodraw->repaint();
234
235
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
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
       	}
			break;


    	default:
    		break;
    }

}


void MyQT::cb_initGL()
{
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);

	// create the render
	m_render = new Algo::Render::GL2::MapRender();

	// VBOs
	m_positionVBO = new Utils::VBO();
	m_colorVBO2 = new Utils::VBO();

	//shader
	m_shader2 = new Utils::ShaderColorPerVertex();
	m_shader2->setAttributePosition(m_positionVBO);
	registerShader(m_shader2);
}


void MyQT::cb_redraw()
{
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	// draw faces with pervertex color rendering
	m_shader2->setAttributeColor(m_colorVBO2);
	m_render->draw(m_shader2, Algo::Render::GL2::TRIANGLES);

	glDisable(GL_POLYGON_OFFSET_FILL);
}


void MyQT::clickHisto(unsigned int i,unsigned int j)
{
Sylvain Thery's avatar
Sylvain Thery committed
280
	std::cout << "CLICK on column Histo: "<< i << " / Quantiles: "<< j <<std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

	if (i != Utils::QT::RenderHistogram::NONE)
	{
		std::vector<unsigned int > vc;
		l_histo->cellsOfHistogramColumn(i,vc);
		std::cout << "Cells of histo: "<< std::endl;
		for (unsigned int k=0; k <vc.size(); ++k)
			std::cout << vc[k]<<"/";
		std::cout << std::endl;

		CellMarker<VERTEX> cm(myMap);
		std::cout << l_histo->markCellsOfHistogramColumn(i,cm) << " marked cells"<< std::endl;
	}

	if (j != Utils::QT::RenderHistogram::NONE)
	{
		std::vector<unsigned int > vc;
		l_histo->cellsOfHistogramColumn(j,vc);
		std::cout << "Cells of quantile: "<< std::endl;
		for (unsigned int k=0; k <vc.size(); ++k)
			std::cout << vc[k]<<"/";
		std::cout << std::endl;
	}

305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
}


void MyQT::cb_Save()
{
	std::string filename = selectFileSave("export svg",".","*.svg") ;
	if (filename.empty())
		return ;
	l_histodraw->svgExport(filename);
}

void MyQT::cb_exit()
{
	if (m_render != NULL)
		delete m_render;
	if (m_shader2 != NULL)
		delete m_shader2;
	if (m_positionVBO != NULL)
		delete m_positionVBO;
	if (l_cm != NULL)
		delete l_cm;
	if (l_histo != NULL)
		delete l_histo;
	if (l_histodraw != NULL)
		delete l_histodraw;
	if (l_popup != NULL)
		delete l_popup;
}