tuto2.cpp 7.6 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer 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
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, 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: https://iggservis.u-strasbg.fr/CGoGN/                              *
* 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 33
#include "Algo/Render/GL2/mapRender.h"
#include "Utils/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 41 42


using namespace CGoGN ;

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


PFP::MAP myMap;

SelectorTrue allDarts;

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

Sylvain Thery's avatar
Sylvain Thery committed
58 59

void MyQT::cb_initGL()
Pierre Kraemer's avatar
Pierre Kraemer committed
60
{
Sylvain Thery's avatar
Sylvain Thery committed
61 62
	// choose to use GL version 2
	Utils::GLSLShader::setCurrentOGLVersion(2);
Pierre Kraemer's avatar
Pierre Kraemer committed
63

Sylvain Thery's avatar
Sylvain Thery committed
64 65
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Pierre Kraemer's avatar
Pierre Kraemer committed
66

Sylvain Thery's avatar
Sylvain Thery committed
67 68
	// create VBO for position
	m_positionVBO = new Utils::VBO();
Pierre Kraemer's avatar
Pierre Kraemer committed
69

Sylvain Thery's avatar
Sylvain Thery committed
70 71
	// using simple shader with color
	m_shader = new Utils::ShaderSimpleColor();
72
	m_shader->setAttributePosition(m_positionVBO);
Pierre Kraemer's avatar
Pierre Kraemer committed
73

Sylvain Thery's avatar
Sylvain Thery committed
74 75 76
	m_color = Geom::Vec4f(0.,1.,0.,0.);
	m_shader->setColor(m_color);

77 78
	registerRunning(m_shader);

Sylvain Thery's avatar
Sylvain Thery committed
79 80
	m_with_lines=true;
	m_line_width=4.0;
Pierre Kraemer's avatar
Pierre Kraemer committed
81

82

Sylvain Thery's avatar
Sylvain Thery committed
83 84
}

Pierre Kraemer's avatar
Pierre Kraemer committed
85

Sylvain Thery's avatar
Sylvain Thery committed
86 87
void MyQT::cb_redraw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
88
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Sylvain Thery's avatar
Sylvain Thery committed
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
	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
115 116 117



Sylvain Thery's avatar
Sylvain Thery committed
118 119 120 121 122
void MyQT::line_width_slot(int x)
{

	m_line_width=x;
	updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
123 124 125
}


Sylvain Thery's avatar
Sylvain Thery committed
126 127 128 129 130 131 132 133 134 135 136 137

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
	{
138
		CGoGNout << "Cancel Color"<< CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
139 140 141
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
142 143 144 145 146 147 148 149
// 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];
150
		CGoGNout << "P "<< P << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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 176 177 178 179
	}
}


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

	/// on reprend la carte de tuto1

	Dart d2 = myMap.newOrientedFace(3);
	Dart d3 = myMap.newOrientedFace(4);
	myMap.sewFaces(d2,d3);

	position = myMap.addAttribute<Geom::Vec3f>(VERTEX_ORBIT, "position");

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

	// MapBrowser: la carte elle meme
180
	CGoGNout << "Parcours avec la carte"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183 184 185 186
	TestDeParcoursAFF<PFP>(myMap, myMap, position);



	// MapBrowserLinkedAuto
	// sous-carte dans une liste avec attribut gere par le browser
187
	CGoGNout << "Parcours avec le browser (les sommets)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190 191 192 193 194 195 196 197 198 199 200 201

	// creation d'un browser avec attribut cree a la volee
	MapBrowserLinkedAuto<PFP::MAP>mbl(myMap);

	// on ajoute un brin par sommet dans le browser
	myMap.foreach_orbit(VERTEX_ORBIT, mbl);

	// 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
202
	CGoGNout << "Parcours avec le browser (le triangle)"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

	// on cree un attribut Dart pour la liste
	AutoAttributeHandler<Dart> tableLink(myMap, DART_ORBIT);
	// le browser
	MapBrowserLinkedAttr<PFP::MAP>mbl2(tableLink);
	// que l'on remplit a la main
	Dart d = d2;
	mbl2.add(d);
	d =  myMap.phi1(d);
	mbl2.add(d);
	d =  myMap.phi1(d);
	mbl2.add(d);

	// 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)
223
	CGoGNout << "Parcours avec le browser selector"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239

	// 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
240 241 242 243 244 245 246 247 248 249
	// 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
250

Sylvain Thery's avatar
Sylvain Thery committed
251 252 253 254 255 256 257

	// message d'aide
	sqt.setHelpMsg("Second Tuto");

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

    //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
258
    Geom::BoundingBox<PFP::VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
Sylvain Thery's avatar
Sylvain Thery committed
259 260 261 262 263 264 265 266
    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
267

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

Sylvain Thery's avatar
Sylvain Thery committed
271 272 273 274
	// update des primitives du renderer
	SelectorTrue allDarts;
	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
275

Sylvain Thery's avatar
Sylvain Thery committed
276 277
	// show final pour premier redraw
	sqt.show();
Pierre Kraemer's avatar
Pierre Kraemer committed
278

Sylvain Thery's avatar
Sylvain Thery committed
279 280
	// et on attend la fin.
	return app.exec();
Pierre Kraemer's avatar
Pierre Kraemer committed
281 282

}