tuto_mt.cpp 8.78 KB
Newer Older
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           *
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/                                           *
21 22 23 24 25 26
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <iostream>

27
#include "tuto_mt.h"
28 29

#include "Topology/generic/parameters.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
30
#include "Topology/map/embeddedMap2.h"
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"
38
#include "Algo/Geometry/normal.h"
39 40
#include "Algo/Modelisation/polyhedron.h"

41
#include "Algo/Parallel/parallel_foreach.h"
42

43
#include "Utils/cgognStream.h"
44
#include "Utils/chrono.h"
45 46 47 48 49 50 51 52 53 54

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
55
	typedef EmbeddedMap2 MAP;
56 57
};

58

59
PFP::MAP myMap;
60
VertexAttribute<PFP::VEC3> position;
61
VertexAttribute<PFP::VEC3> position2;
62
VertexAttribute<PFP::VEC3> normal;
63

64

65

66
void MyQT::cb_initGL()
67
{
68
	Utils::GLSLShader::setCurrentOGLVersion(2);
69

70 71
	// create the render
	m_render = new Algo::Render::GL2::MapRender();
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::Geometry::computeNormalVertices<PFP>(myMap, position, normal) ;
91

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

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

	m_normalVBO->updateData(normal);
99

100
}
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);
106 107 108
}


109
void MyQT::cb_keyPress(int code)
110
{
111 112
	Utils::Chrono ch;
	ch.start();
113

114
	switch(code)
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;
127
	}
128
	CGoGNout << "time = "<< ch.elapsed() << CGoGNendl;
129

130 131
	updateGL();
}
132 133


134 135 136 137

//
// Simple attribute parallel functor and traversor
//
138
template <typename XXX>
139
class UnshrinkFunctor : public FunctorAttribThreaded
140 141
{
protected:
142 143
	VertexAttribute<typename XXX::VEC3>& m_positions;
	VertexAttribute<typename XXX::VEC3>& m_positions2;
144
public:
145 146
	UnshrinkFunctor( VertexAttribute<typename XXX::VEC3>& pos, VertexAttribute<typename XXX::VEC3>& pos2):
		FunctorAttribThreaded(),m_positions(pos),m_positions2(pos2)
Pierre Kraemer's avatar
Pierre Kraemer committed
147
	{}
148

149
	void run(unsigned int i, unsigned int threadID)
150
	{
151
		m_positions2[i] = 1.1f * m_positions[i];
152
	}
153
};
154 155


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

	myMap.swapAttributes(position,position2);
	m_positionVBO->updateData(position);
	m_lines->setAttributePosition(m_positionVBO);
	updateGL();
}
166 167 168



169 170 171
//
// Simple thread that traverse a map
//
172
template <typename XXX>
173
class ShrinkFunctor : public FunctorMapThreaded<typename XXX::MAP >
174 175
{
protected:
176 177
	VertexAttribute<typename XXX::VEC3>& m_positions;
	VertexAttribute<typename XXX::VEC3>& m_positions2;
178
public:
179 180 181 182
	ShrinkFunctor( typename XXX::MAP& map, VertexAttribute<typename XXX::VEC3>& pos, VertexAttribute<typename XXX::VEC3>& pos2):
		FunctorMapThreaded< typename XXX::MAP>(map),
		m_positions(pos),m_positions2(pos2)
	{}
183

184
	void run(Dart d, unsigned int threadID)
185
	{
186 187 188 189 190 191 192 193 194
		typename XXX::VEC3 Q(0,0,0);
		int nb=0;
		Traversor2VVaE<typename XXX::MAP> trav(this->m_map,d);
		for (Dart e=trav.begin(); e!=trav.end(); e = trav.next())
		{
			Q += m_positions[e];
			nb++;
		}
		m_positions2[d] = Q/nb;
195
	}
196
	// no need to duplicate here functor can be shared (no data), call foreach with true parameter
197 198 199
};


200 201 202
void MyQT::threadSimple()
{
	ShrinkFunctor<PFP> funct(myMap,position,position2);
203
	Algo::Parallel::foreach_cell<PFP::MAP,VERTEX>(myMap, funct);
204 205 206 207 208 209 210 211 212 213 214 215 216 217

	myMap.swapAttributes(position,position2);
	m_positionVBO->updateData(position);
	m_lines->setAttributePosition(m_positionVBO);
	updateGL();
}






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

218
template <typename XXX>
219
class LengthEdgeFunctor : public FunctorMapThreaded<typename XXX::MAP >
220
{
221
protected:
222
	VertexAttribute<typename XXX::VEC3>& m_positions;
223 224 225
	double m_length;
	unsigned int m_nb;
public:
226 227 228 229 230 231 232 233
	LengthEdgeFunctor( typename XXX::MAP& map, VertexAttribute<typename XXX::VEC3>& pos):
		FunctorMapThreaded< typename XXX::MAP>(map),
		m_positions(pos), m_length(0.0), m_nb(0)
	{}

	double getLength() { return m_length;}

	unsigned int getNb() { return m_nb;}
234

235
	void run(Dart d, unsigned int threadID)
236 237 238 239 240 241 242
	{
		Dart dd = this->m_map.phi2(d);
		typename XXX::VEC3 V = m_positions[dd] - m_positions[d];
		m_length += V.norm();
		m_nb++;
	}

243 244 245 246 247 248 249 250 251 252
	// no need to duplicate here, we create 1 functor by thread (see bellow)
};

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

	for (unsigned int i=0; i<nbthreads; ++i)
253
	{
254 255
		LengthEdgeFunctor<PFP>* lef = new LengthEdgeFunctor<PFP>(myMap,position);
		functs.push_back(lef);
256 257
	}

258
	CGoGNout << "using "<< nbthreads << " threads"<< CGoGNendl;
259
	Algo::Parallel::foreach_cell<PFP::MAP,EDGE>(myMap, functs);
260

261 262 263 264 265 266 267 268 269 270 271
	//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;
272

273
	CGoGNout << "AVERAGE LENGTH "<< average << CGoGNendl;
274

275 276 277 278 279 280 281 282 283
}


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

284

285 286
 	sqt.statusMsg("Neww to create a sphere or Load for a mesh file");
 	CGoGNStream::allToConsole(&sqt);
287

288 289 290 291 292 293 294 295 296
 	if (!position.isValid())
 		position = myMap.addAttribute<PFP::VEC3, VERTEX>("position");

	if (!position2.isValid())
 		position2 = myMap.addAttribute<PFP::VEC3, VERTEX>("position2");

	if (!normal.isValid())
 		normal = myMap.addAttribute<PFP::VEC3, VERTEX>("normal");

297 298 299
	unsigned int nbt = 64;
	if (argc==2)
		nbt = atoi(argv[1]);
300 301
 		// create a sphere
 	Algo::Modelisation::Polyhedron<PFP> prim(myMap, position);
302
 	prim.cylinder_topo(nbt,nbt, true, true);
303 304 305 306 307 308 309
 	prim.embedSphere(20.0f);


   //  bounding box
	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);
310 311
	CGoGNout << "lPosObj=" << lPosObj << CGoGNendl;
	CGoGNout << "lWidthObj=" << lWidthObj << CGoGNendl;
312 313 314 315 316 317
	sqt.setParamObject(lWidthObj,lPosObj.data());

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

	sqt.show();
318

319

320
	return app.exec();
321
}