tuto_mt.cpp 9.03 KB
Newer Older
Sylvain Thery's avatar
Sylvain Thery committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Sylvain Thery's avatar
Sylvain Thery 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.unistra.fr/                                           *
Sylvain Thery's avatar
Sylvain Thery committed
21 22 23 24 25 26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

27
#include "tuto_mt.h"
Sylvain Thery's avatar
Sylvain Thery committed
28 29

#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30
#include "Topology/map/embeddedMap2.h"
Sylvain Thery's avatar
Sylvain Thery committed
31 32 33 34 35

#include "Geometry/vector_gen.h"

#include "Algo/Import/import.h"
#include "Algo/Geometry/boundingbox.h"
36
#include "Utils/GLSLShader.h"
37
//#include "Algo/Geometry/area.h"
Sylvain Thery's avatar
Sylvain Thery committed
38
#include "Algo/Geometry/normal.h"
untereiner's avatar
untereiner committed
39 40
//#include "Algo/Modelisation/polyhedron.h"
#include "Algo/Tiling/Surface/square.h"
41

42
#include "Utils/cgognStream.h"
43
#include "Utils/chrono.h"
Sylvain Thery's avatar
Sylvain Thery committed
44 45 46 47 48 49 50 51 52 53

using namespace CGoGN ;

/**
 * Struct that contains some informations about the types of the manipulated objects
 * Mainly here to be used by the algorithms that are parameterized by it
 */
struct PFP: public PFP_STANDARD
{
	// definition of the map
Pierre Kraemer's avatar
Pierre Kraemer committed
54
	typedef EmbeddedMap2 MAP;
Sylvain Thery's avatar
Sylvain Thery committed
55 56
};

Pierre Kraemer's avatar
Pierre Kraemer committed
57 58 59
typedef PFP::MAP MAP ;
typedef PFP::VEC3 VEC3 ;
typedef PFP::REAL REAL ;
60

Pierre Kraemer's avatar
Pierre Kraemer committed
61
MAP myMap;
62 63 64
VertexAttribute<VEC3, MAP> position;
VertexAttribute<VEC3, MAP> position2;
VertexAttribute<VEC3, MAP> normal;
65

66
void MyQT::cb_initGL()
Sylvain Thery's avatar
Sylvain Thery committed
67
{
68
	Utils::GLSLShader::setCurrentOGLVersion(2);
Sylvain Thery's avatar
Sylvain Thery committed
69

70 71
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
Sylvain Thery's avatar
Sylvain Thery committed
72

73 74 75
 	// create VBO for position
	m_positionVBO = new Utils::VBO();
	m_positionVBO->updateData(position);
76

77
	m_normalVBO =  new Utils::VBO();
78

79 80 81
	m_shader = new Utils::ShaderSimpleColor();
	m_shader->setAttributePosition(m_positionVBO);
	m_shader->setColor(Geom::Vec4f(1.,1.,0.,0.));
82 83


84 85 86 87 88
	m_lines = new Utils::ShaderVectorPerVertex();
	m_lines->setAttributePosition(m_positionVBO);
	m_lines->setAttributeVector(m_normalVBO);
	m_lines->setScale(2.0f);
	m_lines->setColor(Geom::Vec4f(0.0f, 1.0f, 0.2f, 0.0f));
89

90
	Algo::Surface::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
91

92 93
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::LINES);
	m_render->initPrimitives<PFP>(myMap, Algo::Render::GL2::POINTS);
94

95 96 97 98
	registerShader(m_shader);
	registerShader(m_lines);

	m_normalVBO->updateData(normal);
Sylvain Thery's avatar
Sylvain Thery committed
99

100
}
Sylvain Thery's avatar
Sylvain Thery committed
101

102 103
void MyQT::cb_redraw()
{
104 105
	m_render->draw(m_shader, Algo::Render::GL2::LINES);
	m_render->draw(m_lines, Algo::Render::GL2::POINTS);
Sylvain Thery's avatar
Sylvain Thery committed
106 107 108
}


109
void MyQT::cb_keyPress(int code)
Sylvain Thery's avatar
Sylvain Thery committed
110
{
111 112
	Utils::Chrono ch;
	ch.start();
Sylvain Thery's avatar
Sylvain Thery committed
113

114
	switch(code)
Sylvain Thery's avatar
Sylvain Thery committed
115
	{
116 117 118 119 120 121 122 123 124 125 126
	case 'a':
		threadSimple();
		break;
	case 'z':
		threadStorage();
		break;
	case 'q':
		threadAttrib();
		break;
	default:
		break;
Sylvain Thery's avatar
Sylvain Thery committed
127
	}
128
	CGoGNout << "time = "<< ch.elapsed() << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
129

130 131
	updateGL();
}
Sylvain Thery's avatar
Sylvain Thery committed
132 133


134 135 136 137

//
// Simple attribute parallel functor and traversor
//
138
template <typename XXX>
139
class UnshrinkFunctor : public FunctorAttribThreaded
Sylvain Thery's avatar
Sylvain Thery committed
140 141
{
protected:
142 143
	VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& m_positions;
	VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& m_positions2;
Pierre Kraemer's avatar
Pierre Kraemer committed
144

Sylvain Thery's avatar
Sylvain Thery committed
145
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
146
	UnshrinkFunctor(
147 148
		VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& pos,
		VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& pos2
Pierre Kraemer's avatar
Pierre Kraemer committed
149
	) : FunctorAttribThreaded(), m_positions(pos), m_positions2(pos2)
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
150
	{}
Sylvain Thery's avatar
Sylvain Thery committed
151

152
	void run(unsigned int i, unsigned int /*threadID*/)
Sylvain Thery's avatar
Sylvain Thery committed
153
	{
154
		m_positions2[i] = 1.1f * m_positions[i];
Sylvain Thery's avatar
Sylvain Thery committed
155
	}
156
};
Sylvain Thery's avatar
Sylvain Thery committed
157 158


159 160 161
void MyQT::threadAttrib()
{
	UnshrinkFunctor<PFP> funct(position,position2);
162
	Algo::Parallel::foreach_attrib(myMap.getAttributeContainer<VERTEX>(), funct);
163 164 165 166 167 168

	myMap.swapAttributes(position,position2);
	m_positionVBO->updateData(position);
	m_lines->setAttributePosition(m_positionVBO);
	updateGL();
}
Sylvain Thery's avatar
Sylvain Thery committed
169 170


171 172 173
//
// Simple thread that traverse a map
//
174
template <typename XXX>
175
class ShrinkFunctor : public FunctorMapThreaded<typename XXX::MAP >
Sylvain Thery's avatar
Sylvain Thery committed
176 177
{
protected:
178 179
	VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& m_positions;
	VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& m_positions2;
Pierre Kraemer's avatar
Pierre Kraemer committed
180

Sylvain Thery's avatar
Sylvain Thery committed
181
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
182 183
	ShrinkFunctor(
		typename XXX::MAP& map,
184 185
		VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& pos,
		VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& pos2
Pierre Kraemer's avatar
Pierre Kraemer committed
186 187 188
	) : FunctorMapThreaded<typename XXX::MAP>(map),
		m_positions(pos),
		m_positions2(pos2)
189
	{}
190

191
	void run(Dart d, unsigned int /*threadID*/)
192
	{
193 194
		typename XXX::VEC3 Q(0,0,0);
		int nb=0;
Pierre Kraemer's avatar
Pierre Kraemer committed
195 196
		Traversor2VVaE<typename XXX::MAP> trav(this->m_map, d);
		for (Dart e = trav.begin(); e != trav.end(); e = trav.next())
197 198 199 200 201
		{
			Q += m_positions[e];
			nb++;
		}
		m_positions2[d] = Q/nb;
202
	}
203
	// no need to duplicate here functor can be shared (no data), call foreach with true parameter
Sylvain Thery's avatar
Sylvain Thery committed
204 205 206
};


207 208
void MyQT::threadSimple()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
209
	ShrinkFunctor<PFP> funct(myMap, position, position2);
210
	Algo::Parallel::foreach_cell<PFP::MAP,VERTEX>(myMap, funct);
211

Pierre Kraemer's avatar
Pierre Kraemer committed
212
	myMap.swapAttributes(position, position2);
213 214 215 216 217 218 219 220
	m_positionVBO->updateData(position);
	m_lines->setAttributePosition(m_positionVBO);
	updateGL();
}


// Thread foreach with storage (computing average length of edges)

221
template <typename XXX>
222
class LengthEdgeFunctor : public FunctorMapThreaded<typename XXX::MAP >
Sylvain Thery's avatar
Sylvain Thery committed
223
{
224
protected:
225
	VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& m_positions;
226 227
	double m_length;
	unsigned int m_nb;
Pierre Kraemer's avatar
Pierre Kraemer committed
228

229
public:
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231
	LengthEdgeFunctor(
		typename XXX::MAP& map,
232
		VertexAttribute<typename XXX::VEC3, typename XXX::MAP>& pos
Pierre Kraemer's avatar
Pierre Kraemer committed
233 234 235 236
	) : FunctorMapThreaded<typename XXX::MAP>(map),
		m_positions(pos),
		m_length(0.0),
		m_nb(0)
237 238 239 240 241
	{}

	double getLength() { return m_length;}

	unsigned int getNb() { return m_nb;}
242

243
	void run(Dart d, unsigned int /*threadID*/)
244 245 246 247 248 249 250
	{
		Dart dd = this->m_map.phi2(d);
		typename XXX::VEC3 V = m_positions[dd] - m_positions[d];
		m_length += V.norm();
		m_nb++;
	}

251 252 253
	// no need to duplicate here, we create 1 functor by thread (see bellow)
};

Pierre Kraemer's avatar
Pierre Kraemer committed
254 255


256 257 258 259 260 261
void MyQT::threadStorage()
{
	// functor need storage so we need one per thread
	std::vector<FunctorMapThreaded<PFP::MAP>*> functs;
	unsigned int nbthreads = Algo::Parallel::optimalNbThreads();

Pierre Kraemer's avatar
Pierre Kraemer committed
262
	for (unsigned int i = 0; i < nbthreads; ++i)
263
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
264
		LengthEdgeFunctor<PFP>* lef = new LengthEdgeFunctor<PFP>(myMap, position);
265
		functs.push_back(lef);
266 267
	}

268
	CGoGNout << "using "<< nbthreads << " threads"<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
269
	Algo::Parallel::foreach_cell<MAP, EDGE>(myMap, functs);
270

271 272 273 274 275 276 277 278 279 280 281
	//compute average length from each thread result and delete functors
	double average = 0;
	unsigned int all = 0;
	for (unsigned int i=0; i<nbthreads; ++i)
	{
		LengthEdgeFunctor<PFP>* lef = dynamic_cast<LengthEdgeFunctor<PFP>*>(functs[i]);
		average += lef->getLength();
		all += lef->getNb();
		delete lef;
	}
	average /= all;
Sylvain Thery's avatar
Sylvain Thery committed
282

283
	CGoGNout << "AVERAGE LENGTH "<< average << CGoGNendl;
284 285 286 287 288 289 290 291 292 293
}

int main(int argc, char **argv)
{
	// interface:
	QApplication app(argc, argv);
	MyQT sqt;

 	sqt.statusMsg("Neww to create a sphere or Load for a mesh file");
 	CGoGNStream::allToConsole(&sqt);
Sylvain Thery's avatar
Sylvain Thery committed
294

295
 	if (!position.isValid())
296
		position = myMap.addAttribute<VEC3, VERTEX, MAP>("position");
297 298

	if (!position2.isValid())
299
		position2 = myMap.addAttribute<VEC3, VERTEX, MAP>("position2");
300 301

	if (!normal.isValid())
302
		normal = myMap.addAttribute<VEC3, VERTEX, MAP>("normal");
303

304
	unsigned int nbt = 64;
Pierre Kraemer's avatar
Pierre Kraemer committed
305
	if (argc == 2)
306
		nbt = atoi(argv[1]);
307
 		// create a sphere
Pierre Kraemer's avatar
Pierre Kraemer committed
308
	Algo::Surface::Tilings::Square::Cylinder<PFP> prim(myMap, nbt, nbt, true, true);
untereiner's avatar
untereiner committed
309
    prim.embedIntoSphere(position, 20.0f);
310 311

   //  bounding box
Pierre Kraemer's avatar
Pierre Kraemer committed
312 313
	Geom::BoundingBox<VEC3> bb = Algo::Geometry::computeBoundingBox<PFP>(myMap, position);
	float lWidthObj = std::max<REAL>(std::max<REAL>(bb.size(0), bb.size(1)), bb.size(2));
314
	Geom::Vec3f lPosObj = (bb.min() +  bb.max()) / PFP::REAL(2);
315 316
	CGoGNout << "lPosObj=" << lPosObj << CGoGNendl;
	CGoGNout << "lWidthObj=" << lWidthObj << CGoGNendl;
317 318 319 320 321 322
	sqt.setParamObject(lWidthObj,lPosObj.data());

//	myMap.enableQuickTraversal<EDGE>() ;
//	myMap.enableQuickTraversal<VERTEX>() ;

	sqt.show();
Sylvain Thery's avatar
Sylvain Thery committed
323

324
	return app.exec();
Sylvain Thery's avatar
Sylvain Thery committed
325
}