tuto2.cpp 7.58 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 36 37

#include "tuto2.h"


Pierre Kraemer's avatar
Pierre Kraemer committed
38 39 40 41


using namespace CGoGN ;

42 43
struct PFP: public PFP_STANDARD
{
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45 46 47 48 49 50 51 52 53 54 55 56
	// 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
57 58

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

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

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

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

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

76 77
	registerRunning(m_shader);

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

Sylvain Thery's avatar
Sylvain Thery committed
81 82
}

Pierre Kraemer's avatar
Pierre Kraemer committed
83

Sylvain Thery's avatar
Sylvain Thery committed
84 85
void MyQT::cb_redraw()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
86
	glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
Sylvain Thery's avatar
Sylvain Thery committed
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
	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
113 114 115



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

	m_line_width=x;
	updateGL();
Pierre Kraemer's avatar
Pierre Kraemer committed
121 122 123
}


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

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
	{
		std::cout << "Cancel Color"<< std::endl;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
// 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];
		std::cout << "P "<< P << std::endl;
	}
}


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
	std::cout << "Parcours avec la carte"<<std::endl;
	TestDeParcoursAFF<PFP>(myMap, myMap, position);



	// MapBrowserLinkedAuto
	// sous-carte dans une liste avec attribut gere par le browser
	std::cout << "Parcours avec le browser (les sommets)"<<std::endl;

	// 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
	std::cout << "Parcours avec le browser (le triangle)"<<std::endl;

	// 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)
	std::cout << "Parcours avec le browser selector"<<std::endl;

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

Sylvain Thery's avatar
Sylvain Thery committed
249 250 251 252 253 254 255

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

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

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

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

Sylvain Thery's avatar
Sylvain Thery committed
269 270 271 272
	// 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
273

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

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

}