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
/*******************************************************************************
* 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"


32
#include "Topology/generic/traversor/traversorCell.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

#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

	// 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 ;
71
	if(!Algo::Surface::Import::importMesh<PFP>(myMap, filename.c_str(), attrNames))
72 73 74 75
	{
		CGoGNerr << "could not import " << filename << CGoGNendl ;
		return;
	}
76
	VertexAttribute<VEC3, MAP> position = myMap.getAttribute<VEC3, VERTEX, MAP>(attrNames[0]) ;
77 78

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

	// attribute color to generate from histo
82
	VertexAttribute<VEC3, MAP> colorF = myMap.addAttribute<VEC3, VERTEX, MAP>("colorF");
83 84

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

	// just some tricks to obtain relatives value
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
	float amax = 0;
	for (unsigned int i = area.begin(); i != area.end(); area.next(i))
90
		if (area[i] > amax) amax = area[i];
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
	for (unsigned int i = area.begin(); i != area.end(); area.next(i))
		area[i] -= amax / 4;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108


	// 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
109
	//compute the quantiles
110
	l_nbq = 10;
Sylvain Thery's avatar
Sylvain Thery committed
111
	l_histo->populateQuantiles(l_nbq);
112 113 114 115 116 117 118 119 120

	// 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
Sylvain Thery's avatar
Sylvain Thery committed
121
	l_histodraw = new Algo::Histogram::RenderHistogram(l_popup, *l_histo);
122 123

	// some simple parameters
Sylvain Thery's avatar
Sylvain Thery committed
124
	l_histodraw->setQuantilesDraw(true);
125 126 127 128 129 130 131
	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
132 133
	// and us it to color the quantiles
	l_histodraw->setQuantilesColors(colors);
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

	// 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
162
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::TRIANGLES);
163 164 165 166 167 168 169 170 171 172 173 174


}

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

       	case 'p' :
       		l_nbq++;
Sylvain Thery's avatar
Sylvain Thery committed
189
			l_histo->populateQuantiles(l_nbq);
190 191 192 193 194
			l_histodraw->repaint();
			break;
		case 'o' :
			if (l_nbq>0)
				l_nbq--;
Sylvain Thery's avatar
Sylvain Thery committed
195
			l_histo->populateQuantiles(l_nbq);
196 197 198
			l_histodraw->repaint();
		break;
		case 'q':
Sylvain Thery's avatar
Sylvain Thery committed
199
			l_histodraw->setQuantilesDraw(!l_histodraw->getQuantilesDraw());
200 201 202 203 204 205 206 207 208 209 210
			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();
211 212
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
213 214 215 216 217 218 219 220 221
			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();
222 223
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
224 225 226 227 228 229 230 231 232
       	}
			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();
233 234
			l_histo->histoColorizeVBO(*m_colorVBO2);
			updateGL();
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
       	}
			break;


    	default:
    		break;
    }

}


void MyQT::cb_initGL()
{
	// 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)
{
Pierre Kraemer's avatar
Pierre Kraemer committed
275
	std::cout << "CLICK on column Histo: " << i << " / Quantiles: " << j <<std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
276

Sylvain Thery's avatar
Sylvain Thery committed
277
	if (i != Algo::Histogram::RenderHistogram::NONE)
Sylvain Thery's avatar
Sylvain Thery committed
278 279 280 281
	{
		std::vector<unsigned int > vc;
		l_histo->cellsOfHistogramColumn(i,vc);
		std::cout << "Cells of histo: "<< std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
282 283
		for (unsigned int k = 0; k < vc.size(); ++k)
			std::cout << vc[k] << "/";
Sylvain Thery's avatar
Sylvain Thery committed
284 285
		std::cout << std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287
		CellMarker<MAP, VERTEX> cm(myMap);
		std::cout << l_histo->markCellsOfHistogramColumn(i,cm) << " marked cells" << std::endl;
Sylvain Thery's avatar
Sylvain Thery committed
288 289
	}

Sylvain Thery's avatar
Sylvain Thery committed
290
	if (j != Algo::Histogram::RenderHistogram::NONE)
Sylvain Thery's avatar
Sylvain Thery committed
291 292 293 294 295 296 297 298
	{
		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;
	}
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
}

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