tuto2.cpp 7.53 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

27
#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
28 29 30 31
#include "Topology/map/map2.h"
#include "Topology/generic/mapBrowser.h"

#include "Algo/Geometry/boundingbox.h"
32
#include "Algo/Render/GL2/mapRender.h"
33
#include "Utils/Shaders/shaderSimpleColor.h"
Sylvain Thery's avatar
Sylvain Thery committed
34

35
#include "Utils/drawer.h"
Sylvain Thery's avatar
Sylvain Thery committed
36

37
#include "tuto2.h"
Sylvain Thery's avatar
Sylvain Thery committed
38

Pierre Kraemer's avatar
Pierre Kraemer committed
39 40
using namespace CGoGN ;

41 42
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
43
	// definition de la carte
44
	typedef Map2 MAP;
Pierre Kraemer's avatar
Pierre Kraemer committed
45 46 47 48 49 50 51 52
};

PFP::MAP myMap;

PFP::TVEC3 position ;
PFP::TVEC3 normal ;
AttributeHandler<Geom::Vec4f> color ;

Sylvain Thery's avatar
Sylvain Thery committed
53
void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
54
{
Sylvain Thery's avatar
Sylvain Thery committed
55 56
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
57

Sylvain Thery's avatar
Sylvain Thery committed
58 59
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
60

Sylvain Thery's avatar
Sylvain Thery committed
61 62
	// create VBO for position
	m_positionVBO = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
63

Sylvain Thery's avatar
Sylvain Thery committed
64 65
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
66
	m_shader->setAttributePosition(m_positionVBO);
Pierre Kraemer's avatar
Pierre Kraemer committed
67

Sylvain Thery's avatar
Sylvain Thery committed
68 69 70
	m_color = Geom::Vec4f(0.,1.,0.,0.);
	m_shader->setColor(m_color);

Pierre Kraemer's avatar
Pierre Kraemer committed
71
	registerShader(m_shader);
72

Pierre Kraemer's avatar
Pierre Kraemer committed
73 74
	m_with_lines = true;
	m_line_width = 4.0;
Sylvain Thery's avatar
Sylvain Thery committed
75 76 77 78
}

void MyQT::cb_redraw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
79
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Sylvain Thery's avatar
Sylvain Thery committed
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
	glEnable(GL_LIGHTING);
	if (m_shader)
	{
		if (m_with_lines)
		{
			glLineWidth(m_line_width);
			m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
			m_render->draw(m_shader, Algo::Render::GL2::LINES);
		}

		glEnable(GL_POLYGON_OFFSET_FILL);
		glPolygonOffset(1.0f, 1.0f);

		m_shader->setColor(m_color);
		m_render->draw(m_shader, Algo::Render::GL2::TRIANGLES);

		glDisable(GL_POLYGON_OFFSET_FILL);
	}
}

void MyQT::lines_slot(bool x)
{
	m_with_lines = x;
	updateGL();
}
Pierre Kraemer's avatar
Pierre Kraemer committed
105

Sylvain Thery's avatar
Sylvain Thery committed
106 107 108 109 110
void MyQT::line_width_slot(int x)
{

	m_line_width=x;
	updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
111 112
}

Sylvain Thery's avatar
Sylvain Thery committed
113 114 115 116 117 118 119 120 121 122 123
void MyQT::color_slot()
{
	QColor col = QColorDialog::getColor ();
	if (col.isValid())
	{

		m_color = Geom::Vec4f(float(col.red())/255.0f,float(col.green())/255.0f,float(col.blue())/255.0f,0.0f);
		updateGL();
	}
	else
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
124
		CGoGNout << "Cancel Color" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
125 126 127
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
128 129 130 131 132 133 134
// Algorithme qui parcours une carte et affiche l'attribut position de chaque brin
template<typename PFP>
void TestDeParcoursAFF(typename PFP::MAP& m, MapBrowser& mb, const typename PFP::TVEC3& pos)
{
	for (Dart d = mb.begin(); d != mb.end(); mb.next(d))
	{
		typename PFP::VEC3 P = pos[d];
135
		CGoGNout << "P "<< P << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138 139 140 141 142 143 144
	}
}

int main(int argc, char **argv)
{
	/// Utilisation des MapBrowsers

	/// on reprend la carte de tuto1

145 146
	Dart d2 = myMap.newFace(3);
	Dart d3 = myMap.newFace(4);
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	myMap.sewFaces(d2, d3);
Pierre Kraemer's avatar
Pierre Kraemer committed
148

149
	position = myMap.addAttribute<Geom::Vec3f>(VERTEX, "position");
Pierre Kraemer's avatar
Pierre Kraemer committed
150 151 152 153 154 155 156 157 158 159 160 161 162

	position[d2] = PFP::VEC3(0.0f, 0.0f, 0.0f);
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(2.0f, 0.0f, 0.0f);
	d2 = myMap.phi1(d2);
	position[d2] = PFP::VEC3(1.0f, 3.0f, 0.0f);
	d2 = myMap.phi1(d2);
	d3 = myMap.phi<11>(d3);
	position[d3] = PFP::VEC3(0.0f, -2.0f, 0.0f);
	d3 = myMap.phi1(d3);
	position[d3] = PFP::VEC3(2.0f, -2.0f, 0.0f);
	d3 = myMap.phi1(d3);

Pierre Kraemer's avatar
Pierre Kraemer committed
163

Pierre Kraemer's avatar
Pierre Kraemer committed
164
	// MapBrowser: la carte elle meme
Pierre Kraemer's avatar
Pierre Kraemer committed
165
	CGoGNout << "Parcours avec la carte" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
166 167 168 169
	TestDeParcoursAFF<PFP>(myMap, myMap, position);

	// MapBrowserLinkedAuto
	// sous-carte dans une liste avec attribut gere par le browser
170
	CGoGNout << "Parcours avec le browser (les sommets)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
171 172

	// creation d'un browser avec attribut cree a la volee
David Cazier's avatar
David Cazier committed
173
	MapBrowserLinked<PFP::MAP>mbl(myMap);
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175

	// on ajoute un brin par sommet dans le browser
176
	myMap.foreach_orbit(VERTEX, mbl);
Pierre Kraemer's avatar
Pierre Kraemer committed
177 178 179 180 181 182 183

	// et on parcours la sous-carte avec ce browser
	TestDeParcoursAFF<PFP>(myMap, mbl, position);


	// MapBrowserLinkedAttr
	// sous-carte dans une liste avec attribut gere par l'appelant
184
	CGoGNout << "Parcours avec le browser (le triangle)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186

	// on cree un attribut Dart pour la liste
187
	AutoAttributeHandler<Dart> tableLink(myMap, DART);
Pierre Kraemer's avatar
Pierre Kraemer committed
188
	// le browser
David Cazier's avatar
David Cazier committed
189
	MapBrowserLinked<PFP::MAP>mbl2(myMap,tableLink);
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191
	// que l'on remplit a la main
	Dart d = d2;
David Cazier's avatar
David Cazier committed
192
	mbl2.pushBack(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
193
	d =  myMap.phi1(d);
David Cazier's avatar
David Cazier committed
194
	mbl2.pushBack(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
195
	d =  myMap.phi1(d);
David Cazier's avatar
David Cazier committed
196
	mbl2.pushFront(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
197 198 199 200 201 202 203

	// et on parcours la sous-carte avec ce browser
	TestDeParcoursAFF<PFP>(myMap, mbl2, position);


	// MapBrowserSelector
	// sous-carte  avec des brins marques (equivalent de l'utilisation de good dans les algos)
204
	CGoGNout << "Parcours avec le browser selector"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220

	// on marque 2 brins pour le test
	DartMarker mk(myMap);
	mk.mark(d2);
	mk.mark(d3);

	// le selector qui selectionne les "bons" brins (les autres sont reconduits a la frontiere ;)
	SelectorMarked selector(mk);

//	// le browser
//	MapBrowserSelector<PFP::MAP>mbsel(myMap,selector);

//	// et on parcours la sous-carte avec ce browser
//	TestDeParcoursAFF<PFP>(myMap,mbsel,position);


Sylvain Thery's avatar
Sylvain Thery committed
221 222 223 224 225 226 227 228 229 230
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

    Utils::QT::uiDockInterface dock;
    sqt.setDock(&dock);

	sqt.setCallBack( dock.checkLines, SIGNAL(toggled(bool)), SLOT(lines_slot(bool)) );
	sqt.setCallBack( dock.color_button , SIGNAL(pressed()), SLOT(color_slot()) );
	sqt.setCallBack( dock.dial_line_width , SIGNAL(valueChanged(int)), SLOT(line_width_slot(int)) );
Pierre Kraemer's avatar
Pierre Kraemer committed
231

Sylvain Thery's avatar
Sylvain Thery committed
232 233 234 235 236 237
	// message d'aide
	sqt.setHelpMsg("Second Tuto");

	dock.number_of_darts->display(int(myMap.getNbDarts()));

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
238
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
239 240 241 242 243 244 245 246
    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);

    // envoit info BB a l'interface
	sqt.setParamObject(lWidthObj,lPosObj.data());

	// show 1 pour GL context
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
247

Sylvain Thery's avatar
Sylvain Thery committed
248 249
	// update du VBO position (context GL necessaire)
	sqt.m_positionVBO->updateData(position);
Pierre Kraemer's avatar
Pierre Kraemer committed
250

Sylvain Thery's avatar
Sylvain Thery committed
251 252 253
	// update des primitives du renderer
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::TRIANGLES);
	sqt.m_render->initPrimitives<PFP>(myMap, allDarts, Algo::Render::GL2::LINES);
Pierre Kraemer's avatar
Pierre Kraemer committed
254

Sylvain Thery's avatar
Sylvain Thery committed
255 256
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
257

Sylvain Thery's avatar
Sylvain Thery committed
258 259
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
260
}