topo3Render.hpp 34.7 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Geometry/vector_gen.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Topology/generic/dartmarker.h"
#include "Topology/generic/cellmarker.h"

#include "Topology/map/map3.h"
#include "Topology/gmap/gmap3.h"

33
#include "Topology/generic/traversor/traversorCell.h"
34
35
#include "Algo/Geometry/centroid.h"

36
37
#include "Geometry/distances.h"

Pierre Kraemer's avatar
Pierre Kraemer committed
38
39
40
41
42
43
44
45
46
namespace CGoGN
{

namespace Algo
{

namespace Render
{

Sylvain Thery's avatar
Sylvain Thery committed
47
namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
48
{
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73

template<typename PFP>
Topo3Render<PFP>::Topo3Render():
	m_nbDarts(0),
	m_nbRel1(0),
	m_nbRel2(0),
	m_nbRel3(0),
	m_topo_dart_width(2.0f),
	m_topo_relation_width(3.0f),
	m_color_save(NULL),
	m_dartsColor(1.0f,1.0f,1.0f),
	m_bufferDartPosition(NULL)
{
	m_vbo0 = new Utils::VBO();
	m_vbo1 = new Utils::VBO();
	m_vbo2 = new Utils::VBO();
	m_vbo3 = new Utils::VBO();
	m_vbo4 = new Utils::VBO();

	m_vbo0->setDataSize(3);
	m_vbo1->setDataSize(3);
	m_vbo2->setDataSize(3);
	m_vbo3->setDataSize(3);
	m_vbo4->setDataSize(3);

Sylvain Thery's avatar
Sylvain Thery committed
74
75
76
77
78
79
80
	m_shader1 = new Utils::ShaderBoldLines();
	m_shader2 = new Utils::ShaderColorDarts();
	m_shader3 = new Utils::ShaderDarts();

	m_shadersVector.push_back(m_shader1);
	m_shadersVector.push_back(m_shader2);
	m_shadersVector.push_back(m_shader3);
Pierre Kraemer's avatar
Pierre Kraemer committed
81
82
83
84
85
86
87

	// binding VBO - VA
	m_vaId = m_shader1->setAttributePosition(m_vbo1);

	m_shader2->setAttributePosition(m_vbo0);
	m_shader2->setAttributeColor(m_vbo4);

Sylvain Thery's avatar
Sylvain Thery committed
88
89
	m_shader3->setAttributePosition(m_vbo0);

Pierre Kraemer's avatar
Pierre Kraemer committed
90
91
92
	// registering for auto matrices update
	Utils::GLSLShader::registerShader(NULL, m_shader1);
	Utils::GLSLShader::registerShader(NULL, m_shader2);
Sylvain Thery's avatar
Sylvain Thery committed
93
	Utils::GLSLShader::registerShader(NULL, m_shader3);
Pierre Kraemer's avatar
Pierre Kraemer committed
94
95
96
97
98
}

template<typename PFP>
Topo3Render<PFP>::~Topo3Render()
{
Sylvain Thery's avatar
Sylvain Thery committed
99
	Utils::GLSLShader::unregisterShader(NULL, m_shader3);
Pierre Kraemer's avatar
Pierre Kraemer committed
100
101
102
	Utils::GLSLShader::unregisterShader(NULL, m_shader2);
	Utils::GLSLShader::unregisterShader(NULL, m_shader1);

Sylvain Thery's avatar
Sylvain Thery committed
103
	delete m_shader3;
Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	delete m_shader2;
	delete m_shader1;
	delete m_vbo4;
	delete m_vbo3;
	delete m_vbo2;
	delete m_vbo1;
	delete m_vbo0;

	if (m_attIndex.map() != NULL)
		m_attIndex.map()->removeAttribute(m_attIndex);

	if (m_color_save != NULL)
		delete[] m_color_save;

	if (m_bufferDartPosition != NULL)
		delete[] m_bufferDartPosition;
}

122
123
124
125
126
127

template<typename PFP>
void Topo3Render<PFP>::setClippingPlane(const Geom::Vec4f& plane)
{
	m_shader1->setClippingPlane(plane);
	m_shader2->setClippingPlane(plane);
Sylvain Thery's avatar
Sylvain Thery committed
128
	m_shader3->setClippingPlane(plane);
129
130
131
132
133
134
135
136
137
138
}

template<typename PFP>
void Topo3Render<PFP>::setNoClippingPlane()
{
	this->setClippingPlane(Geom::Vec4f(0.0f,0.0f,0.0f,0.0f));
}



Pierre Kraemer's avatar
Pierre Kraemer committed
139
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
template<typename PFP>
void Topo3Render<PFP>::setDartWidth(float dw)
{
	m_topo_dart_width = dw;
}

template<typename PFP>
void Topo3Render<PFP>::setRelationWidth(float pw)
{
	m_topo_relation_width = pw;
}

template<typename PFP>
void Topo3Render<PFP>::setDartColor(Dart d, float r, float g, float b)
{
	float RGB[6];
	RGB[0]=r; RGB[1]=g; RGB[2]=b;
	RGB[3]=r; RGB[4]=g; RGB[5]=b;
	m_vbo4->bind();
	glBufferSubData(GL_ARRAY_BUFFER, m_attIndex[d]*3*sizeof(float), 6*sizeof(float),RGB);
}

template<typename PFP>
void Topo3Render<PFP>::setAllDartsColor(float r, float g, float b)
{
	m_vbo4->bind();
165
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
166
167
168
169
170
171
172
	float* colorDartBuf = reinterpret_cast<float*>(ColorDartsBuffer);
	for (unsigned int i=0; i < 2*m_nbDarts; ++i)
	{
		*colorDartBuf++ = r;
		*colorDartBuf++ = g;
		*colorDartBuf++ = b;
	}
173
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188

}

template<typename PFP>
void Topo3Render<PFP>::setInitialDartsColor(float r, float g, float b)
{
	m_dartsColor = Geom::Vec3f(r,g,b);
}

template<typename PFP>
void Topo3Render<PFP>::drawDarts()
{
	if (m_nbDarts==0)
		return;

Sylvain Thery's avatar
Sylvain Thery committed
189
	m_shader2->setLineWidth(m_topo_dart_width);
Pierre Kraemer's avatar
Pierre Kraemer committed
190
191
192
193
194
195
196
197
198
199
200
201
202
	m_shader2->enableVertexAttribs();
	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
	m_shader2->disableVertexAttribs();
}

template<typename PFP>
void Topo3Render<PFP>::drawRelation1()
{
	if (m_nbDarts==0)
		return;

	m_shader1->changeVA_VBO(m_vaId, m_vbo1);
	m_shader1->setColor(Geom::Vec4f(0.0f,1.0f,1.0f,0.0f));
Sylvain Thery's avatar
Sylvain Thery committed
203
	m_shader1->setLineWidth(m_topo_relation_width);
Pierre Kraemer's avatar
Pierre Kraemer committed
204
205
206
207
208
209
210
211
212
213
214
215
	m_shader1->enableVertexAttribs();
	glDrawArrays(GL_LINES, 0, m_nbRel1*2);
	m_shader1->disableVertexAttribs();
}

template<typename PFP>
void Topo3Render<PFP>::drawRelation2()
{
	if (m_nbRel2==0)
		return;

	m_shader1->changeVA_VBO(m_vaId, m_vbo2);
Sylvain Thery's avatar
Sylvain Thery committed
216
217
	m_shader1->setColor(Geom::Vec4f(1.0f, 0.0f, 0.0f, 0.0f));
	m_shader1->setLineWidth(m_topo_relation_width);
Pierre Kraemer's avatar
Pierre Kraemer committed
218
	m_shader1->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
219
	glDrawArrays(GL_LINES, 0, m_nbRel1 * 2);
Pierre Kraemer's avatar
Pierre Kraemer committed
220
221
222
223
224
225
226
227
228
229
230
	m_shader1->disableVertexAttribs();
}

template<typename PFP>
void Topo3Render<PFP>::drawRelation3(Geom::Vec4f c)
{
	if (m_nbRel3==0)
		return;

	m_shader1->changeVA_VBO(m_vaId, m_vbo3);
	m_shader1->setColor(c);
Sylvain Thery's avatar
Sylvain Thery committed
231
	m_shader1->setLineWidth(m_topo_relation_width);
Pierre Kraemer's avatar
Pierre Kraemer committed
232
	m_shader1->enableVertexAttribs();
Sylvain Thery's avatar
Sylvain Thery committed
233
	glDrawArrays(GL_LINES, 0, m_nbRel3 * 2);
Pierre Kraemer's avatar
Pierre Kraemer committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	m_shader1->disableVertexAttribs();
}

template<typename PFP>
void Topo3Render<PFP>::drawTopo()
{
	drawDarts();
	drawRelation1();
	drawRelation2();
	drawRelation3(Geom::Vec4f(1.0f,1.0f,0.0f,0.0f));
}

template<typename PFP>
void Topo3Render<PFP>::overdrawDart(Dart d, float width, float r, float g, float b)
{
	unsigned int indexDart =  m_attIndex[d];

Sylvain Thery's avatar
Sylvain Thery committed
251
252
253
	m_shader3->setColor(Geom::Vec4f(r, g, b, 0.0f));
	m_shader3->setLineWidth(width);
	m_shader3->enableVertexAttribs();
Pierre Kraemer's avatar
Pierre Kraemer committed
254
	glDrawArrays(GL_LINES, indexDart, 2);
Sylvain Thery's avatar
Sylvain Thery committed
255
	m_shader3->disableVertexAttribs();
Pierre Kraemer's avatar
Pierre Kraemer committed
256
257
258
259
260
261
262
}

template<typename PFP>
void Topo3Render<PFP>::pushColors()
{
	m_color_save = new float[6*m_nbDarts];
	m_vbo4->bind();
263
	void* colorBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
264
265
266
267
268
269
270
271
272

	memcpy(m_color_save, colorBuffer, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
void Topo3Render<PFP>::popColors()
{
	m_vbo4->bind();
273
	void* colorBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

	memcpy(colorBuffer, m_color_save, 6*m_nbDarts*sizeof(float));
	glUnmapBuffer(GL_ARRAY_BUFFER);

	delete[] m_color_save;
	m_color_save=0;
}

template<typename PFP>
Dart Topo3Render<PFP>::colToDart(float* color)
{
	unsigned int r = (unsigned int)(color[0]*255.0f);
	unsigned int g = (unsigned int)(color[1]*255.0f);
	unsigned int b = (unsigned int)(color[2]*255.0f);

	unsigned int id = r + 255*g +255*255*b;

	if (id == 0)
		return NIL;
	return Dart(id-1);
}

template<typename PFP>
void Topo3Render<PFP>::dartToCol(Dart d, float& r, float& g, float& b)
{
	// here use dart.index beacause it is what we want (and not map.dartIndex(d) !!)
	unsigned int lab = d.index + 1; // add one to avoid picking the black of screen

	r = float(lab%255) / 255.0f; lab = lab/255;
	g = float(lab%255) / 255.0f; lab = lab/255;
	b = float(lab%255) / 255.0f; lab = lab/255;
	if (lab!=0)
		CGoGNerr << "Error picking color, too many darts"<< CGoGNendl;
}

template<typename PFP>
Dart Topo3Render<PFP>::pickColor(unsigned int x, unsigned int y)
{
	//more easy picking for
313
	float dw = m_topo_dart_width;
Pierre Kraemer's avatar
Pierre Kraemer committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
	m_topo_dart_width+=2;

	// save clear color and set to zero
	float cc[4];
	glGetFloatv(GL_COLOR_CLEAR_VALUE,cc);

	glClearColor(0.0f,0.0f,0.0f,0.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glDisable(GL_LIGHTING);
	// draw in back buffer (not shown on screen)

	drawDarts();

	// restore dart with
	m_topo_dart_width = dw;

	// read the pixel under the mouse in back buffer
	glReadBuffer(GL_BACK);
	float color[3];
	glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,color);

	glClearColor(cc[0], cc[1], cc[2], cc[3]);


	std::cout << color[0] << ", "<<color[1] << ", "<<color[2] <<std::endl;
	return colToDart(color);
}

template<typename PFP>
void Topo3Render<PFP>::svgout2D(const std::string& filename, const glm::mat4& model, const glm::mat4& proj)
{
	Utils::SVG::SVGOut svg(filename,model,proj);
	toSVG(svg);
	svg.write();
}

template<typename PFP>
void Topo3Render<PFP>::toSVG(Utils::SVG::SVGOut& svg)
{
	// PHI3 / beta3
	Utils::SVG::SvgGroup* svg1 = new Utils::SVG::SvgGroup("phi3", svg.m_model, svg.m_proj);
	const Geom::Vec3f* ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo3->lockPtr());
	svg1->setWidth(m_topo_relation_width);
	svg1->beginLines();
	for (unsigned int i=0; i<m_nbRel3; ++i)
	{
		Geom::Vec3f P = (ptr[4*i]+ ptr[4*i+3])/2.0f;
		Geom::Vec3f Q = (ptr[4*i+1]+ ptr[4*i+2])/2.0f;
		svg1->addLine(P, Q,Geom::Vec3f(0.8f,0.8f,0.0f));
	}
	svg1->endLines();
	m_vbo3->releasePtr();

	svg.addGroup(svg1);

	// PHI2 / beta2
	Utils::SVG::SvgGroup* svg2 = new Utils::SVG::SvgGroup("phi2", svg.m_model, svg.m_proj);
	ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo2->lockPtr());
	svg2->setWidth(m_topo_relation_width);
	svg2->beginLines();
	for (unsigned int i=0; i<m_nbRel2; ++i)
	{
		Geom::Vec3f P = (ptr[4*i]+ ptr[4*i+3])/2.0f;
		Geom::Vec3f Q = (ptr[4*i+1]+ ptr[4*i+2])/2.0f;
		svg2->addLine(P, Q,Geom::Vec3f(0.8f,0.0f,0.0f));
	}
	svg2->endLines();
	m_vbo2->releasePtr();

	svg.addGroup(svg2);

	//PHI1 /beta1
	Utils::SVG::SvgGroup* svg3 = new Utils::SVG::SvgGroup("phi1", svg.m_model, svg.m_proj);
	ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo1->lockPtr());
	svg3->setWidth(m_topo_relation_width);
	svg3->beginLines();
	for (unsigned int i=0; i<m_nbRel1; ++i)
		svg3->addLine(ptr[2*i], ptr[2*i+1],Geom::Vec3f(0.0f,0.7f,0.7f));
	svg3->endLines();
	m_vbo1->releasePtr();

	svg.addGroup(svg3);

	const Geom::Vec3f* colorsPtr = reinterpret_cast<const Geom::Vec3f*>(m_vbo4->lockPtr());
	ptr= reinterpret_cast<Geom::Vec3f*>(m_vbo0->lockPtr());

	Utils::SVG::SvgGroup* svg4 = new Utils::SVG::SvgGroup("darts", svg.m_model, svg.m_proj);
	svg4->setWidth(m_topo_dart_width);

	svg4->beginLines();
	for (unsigned int i=0; i<m_nbDarts; ++i)
	{
		Geom::Vec3f col = colorsPtr[2*i];
		if (col.norm2()>2.9f)
			col = Geom::Vec3f(1.0f,1.0f,1.0f) - col;
		svg4->addLine(ptr[2*i], ptr[2*i+1], col);
	}
	svg4->endLines();

	svg.addGroup(svg4);

	Utils::SVG::SvgGroup* svg5 = new Utils::SVG::SvgGroup("dartEmb", svg.m_model, svg.m_proj);
	svg5->setWidth(m_topo_dart_width);
	svg5->beginPoints();
	for (unsigned int i=0; i<m_nbDarts; ++i)
	{
		Geom::Vec3f col = colorsPtr[2*i];
		if (col.norm2()>2.9f)
			col = Geom::Vec3f(1.0f,1.0f,1.0f) - col;
		svg5->addPoint(ptr[2*i], col);
	}
	svg5->endPoints();

	svg.addGroup(svg5);

	m_vbo0->releasePtr();
	m_vbo4->releasePtr();
}

434
//template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
435
//void Topo3Render<PFP>::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, float kv)
436
437
438
439
440
441
442
443
444
445
446
447
//{
//	Map3* ptrMap3 = dynamic_cast<Map3*>(&map);
//	if (ptrMap3 != NULL)
//	{
//		updateDataMap3<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3 >(map,positions,ke,kf,kv);
//	}
//	GMap3* ptrGMap3 = dynamic_cast<GMap3*>(&map);
//	if (ptrGMap3 != NULL)
//	{
//		updateDataGMap3<PFP, VertexAttribute<typename PFP::VEC3>, typename PFP::VEC3>(map,positions,ke,kf,kv);
//	}
//}
448

Pierre Kraemer's avatar
Pierre Kraemer committed
449
//template<typename PFP>
450
//void Topo3Render<PFP>::updateData(MAP& map, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, float kv)
Pierre Kraemer's avatar
Pierre Kraemer committed
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//{
//	std::string typeName = map.mapTypeName();
//	if (typeName[0] == 'M') // "Map3"
//	{
//		updateDataMap3(map, positions, ke, kf, kv);
//		return;
//	}
//	if (typeName[0] == 'G') // "GMap3"
//	{
//		updateDataGMap3(map, positions, ke, kf, kv);
//		return;
//	}
//}

Pierre Kraemer's avatar
Pierre Kraemer committed
465
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
466
void Topo3Render<PFP>::setDartsIdColor(typename PFP::MAP& map)
467
{
Pierre Kraemer's avatar
Pierre Kraemer committed
468
469
470
471
472
	m_vbo4->bind();
	float* colorBuffer =  reinterpret_cast<float*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb=0;

	for (Dart d = map.begin(); d != map.end(); map.next(d))
473
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
474
		if ( !map.template isBoundaryMarked<3>(d)) // topo3 Render do not traverse boundary
Pierre Kraemer's avatar
Pierre Kraemer committed
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
		{
			if (nb < m_nbDarts)
			{
				float r,g,b;
				dartToCol(d, r,g,b);

				float* local = colorBuffer+3*m_attIndex[d]; // get the right position in VBO
				*local++ = r;
				*local++ = g;
				*local++ = b;
				*local++ = r;
				*local++ = g;
				*local++ = b;
				nb++;
			}
			else
			{
				CGoGNerr << "Error buffer too small for color picking (change the selector parameter ?)" << CGoGNendl;
				break;
			}
		}
496
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
497
498
499
500
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
501
void Topo3Render<PFP>::updateColors(MAP& map, const VertexAttribute<Geom::Vec3f, MAP>& colors)
Pierre Kraemer's avatar
Pierre Kraemer committed
502
503
504
505
506
507
{
	m_vbo4->bind();
	VEC3* colorBuffer = reinterpret_cast<VEC3*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb = 0;

	for (Dart d = map.begin(); d != map.end(); map.next(d))
508
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
509
510
511
512
513
514
515
516
517
518
519
520
521
		if ( !map.isBoundaryMarked3(d)) // topo3 Render do not traverse boundary
		{
			if (nb < m_nbDarts)
			{
				colorBuffer[m_attIndex[d]] = colors[d];
				nb++;
			}
			else
			{
				CGoGNerr << "Error buffer too small for color picking (change the selector parameter ?)" << CGoGNendl;
				break;
			}
		}
522
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
523
	glUnmapBuffer(GL_ARRAY_BUFFER);
524
525
}

Pierre Kraemer's avatar
Pierre Kraemer committed
526
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
527
Dart Topo3Render<PFP>::picking(MAP& map, int x, int y)
Pierre Kraemer's avatar
Pierre Kraemer committed
528
{
Pierre Kraemer's avatar
Pierre Kraemer committed
529
530
531
532
533
534
	pushColors();
	setDartsIdColor(map);
	Dart d = pickColor(x,y);
	popColors();
	return d;
}
Sylvain Thery's avatar
Sylvain Thery committed
535

Pierre Kraemer's avatar
Pierre Kraemer committed
536

Pierre Kraemer's avatar
Pierre Kraemer committed
537
538
539
540
541




template<typename PFP>
542
void Topo3RenderMap<PFP>::updateData(MAP& mapx, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, float kv)
Pierre Kraemer's avatar
Pierre Kraemer committed
543
{
544
	this->m_attIndex = mapx.template getAttribute<unsigned int, DART, MAP>("dart_index3");
Pierre Kraemer's avatar
Pierre Kraemer committed
545
546

	if (!this->m_attIndex.isValid())
547
		this->m_attIndex  = mapx.template addAttribute<unsigned int, DART, MAP>("dart_index3");
Pierre Kraemer's avatar
Pierre Kraemer committed
548
549

	this->m_nbDarts = 0;
Sylvain Thery's avatar
Sylvain Thery committed
550
	for (Dart d = mapx.begin(); d != mapx.end(); mapx.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
551
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
552
553
		if (!mapx.template isBoundaryMarked<3>(d)) // in the following code Traversor do not traverse boundary
			this->m_nbDarts++;
554
555
556
	}

	// compute center of each volumes
Pierre Kraemer's avatar
Pierre Kraemer committed
557
	CellMarker<MAP, VOLUME> cmv(mapx);
558
	VolumeAutoAttribute<VEC3, MAP> centerVolumes(mapx, "centerVolumes");
Sylvain Thery's avatar
Sylvain Thery committed
559

Sylvain Thery's avatar
Sylvain Thery committed
560
	Algo::Volume::Geometry::Parallel::computeCentroidELWVolumes<PFP>(mapx, positions, centerVolumes);
561

Pierre Kraemer's avatar
Pierre Kraemer committed
562
	// debut phi1
563
	DartAutoAttribute<VEC3, MAP> fv1(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
564
	// fin phi1
565
	DartAutoAttribute<VEC3, MAP> fv11(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
566
567

	// phi2
568
569
	DartAutoAttribute<VEC3, MAP> fv2(mapx);
	DartAutoAttribute<VEC3, MAP> fv2x(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
570

Pierre Kraemer's avatar
Pierre Kraemer committed
571
572
	this->m_vbo4->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
573
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
574
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
575

Pierre Kraemer's avatar
Pierre Kraemer committed
576
577
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
578
	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
579
	Geom::Vec3f* positionDartBuf = reinterpret_cast<Geom::Vec3f*>(PositionDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
580

581
	std::vector<Dart> vecDartFaces;
Pierre Kraemer's avatar
Pierre Kraemer committed
582
	vecDartFaces.reserve(this->m_nbDarts/3);
583
	unsigned int posDBI = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
584

585
	// traverse each face of each volume
Pierre Kraemer's avatar
Pierre Kraemer committed
586
	TraversorCell<MAP, PFP::MAP::FACE_OF_PARENT> traFace(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
587
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
588
	{
589
590
591
592
593
594
595
596
597
		vecDartFaces.push_back(d);

		std::vector<VEC3> vecPos;
		vecPos.reserve(16);

		// store the face & center
		float okv = 1.0f - kv;

		VEC3 vc = centerVolumes[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
598

599
600
601
602
		VEC3 centerFace = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx,d,positions)*kv +vc*okv;

		//shrink the face
		float okf = 1.0f - kf;
603
604
		Dart dd = d;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
605
		{
606
			VEC3 P = centerFace*okf + (vc*okv + positions[dd]*kv)*kf;
607
			vecPos.push_back(P);
Sylvain Thery's avatar
Sylvain Thery committed
608
			dd = mapx.phi1(dd);
609
		} while (dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
610

611
		unsigned int nb = uint32(vecPos.size());
Pierre Kraemer's avatar
Pierre Kraemer committed
612

613
		vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop
Pierre Kraemer's avatar
Pierre Kraemer committed
614

615
616
617
618
		// compute position of points to use for drawing topo
		float oke = 1.0f - ke;
		for (unsigned int i = 0; i < nb; ++i)
		{
619
620
			VEC3 P = vecPos[i]*ke + vecPos[i+1]*oke;
			VEC3 Q = vecPos[i+1]*ke + vecPos[i]*oke;
Pierre Kraemer's avatar
Pierre Kraemer committed
621

Pierre Kraemer's avatar
Pierre Kraemer committed
622
			this->m_attIndex[d] = posDBI;
623
			posDBI+=2;
Pierre Kraemer's avatar
Pierre Kraemer committed
624

625
626
			*positionDartBuf++ = PFP::toVec3f(P);
			*positionDartBuf++ = PFP::toVec3f(Q);
Pierre Kraemer's avatar
Pierre Kraemer committed
627
628
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
629

630
631
			fv1[d] = P*0.1f + Q*0.9f;
			fv11[d] = P*0.9f + Q*0.1f;
Pierre Kraemer's avatar
Pierre Kraemer committed
632

633
634
			fv2[d] = P*0.52f + Q*0.48f;
			fv2x[d] = P*0.48f + Q*0.52f;
Sylvain Thery's avatar
Sylvain Thery committed
635
			d = mapx.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
636
637
638
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
639
	this->m_vbo0->bind();
640
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
641

Pierre Kraemer's avatar
Pierre Kraemer committed
642
	this->m_vbo4->bind();
643
644
	glUnmapBuffer(GL_ARRAY_BUFFER);

Pierre Kraemer's avatar
Pierre Kraemer committed
645
646
647
	Geom::Vec3f* positioniF1 = new Geom::Vec3f[ 2*this->m_nbDarts];
	Geom::Vec3f* positioniF2 = new Geom::Vec3f[ 2*this->m_nbDarts];
	Geom::Vec3f* positioniF3 = new Geom::Vec3f[ 2*this->m_nbDarts];
Pierre Kraemer's avatar
Pierre Kraemer committed
648

649
650
651
	Geom::Vec3f* positionF1 = positioniF1;
	Geom::Vec3f* positionF2 = positioniF2;
	Geom::Vec3f* positionF3 = positioniF3;
Pierre Kraemer's avatar
Pierre Kraemer committed
652

Pierre Kraemer's avatar
Pierre Kraemer committed
653
654
655
	this->m_nbRel1 = 0;
	this->m_nbRel2 = 0;
	this->m_nbRel3 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
656
657
658
659
660
661

	for(std::vector<Dart>::iterator face = vecDartFaces.begin(); face != vecDartFaces.end(); ++face)
	{
		Dart d = *face;
		do
		{
Sylvain Thery's avatar
Sylvain Thery committed
662
			Dart e = mapx.phi2(d);
663
			if ((d < e))
Pierre Kraemer's avatar
Pierre Kraemer committed
664
			{
665
				*positionF2++ = PFP::toVec3f(fv2[d]);
Sylvain Thery's avatar
Sylvain Thery committed
666
//				*positionF2++ = PFP::toVec3f(fv2x[e]);
667
				*positionF2++ = PFP::toVec3f(fv2[e]);
Sylvain Thery's avatar
Sylvain Thery committed
668
//				*positionF2++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
669
				this->m_nbRel2++;
Pierre Kraemer's avatar
Pierre Kraemer committed
670
			}
Sylvain Thery's avatar
Sylvain Thery committed
671
			e = mapx.phi3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
672
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e) )
Pierre Kraemer's avatar
Pierre Kraemer committed
673
			{
Sylvain Thery's avatar
Sylvain Thery committed
674
//				*positionF3++ = PFP::toVec3f(fv2[d]);
675
				*positionF3++ = PFP::toVec3f(fv2x[e]);
Sylvain Thery's avatar
Sylvain Thery committed
676
//				*positionF3++ = PFP::toVec3f(fv2[e]);
677
				*positionF3++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
678
				this->m_nbRel3++;
Pierre Kraemer's avatar
Pierre Kraemer committed
679
			}
Sylvain Thery's avatar
Sylvain Thery committed
680
			e = mapx.phi1(d);
681
682
			*positionF1++ = PFP::toVec3f(fv1[d]);
			*positionF1++ = PFP::toVec3f(fv11[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
683
			this->m_nbRel1++;
684

Sylvain Thery's avatar
Sylvain Thery committed
685
			d = mapx.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
686
687
688
		} while (d != *face );
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
689
	this->m_vbo3->bind();
Sylvain Thery's avatar
Sylvain Thery committed
690
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbRel3*sizeof(Geom::Vec3f), positioniF3, GL_STREAM_DRAW);
Pierre Kraemer's avatar
Pierre Kraemer committed
691

Pierre Kraemer's avatar
Pierre Kraemer committed
692
	this->m_vbo2->bind();
Sylvain Thery's avatar
Sylvain Thery committed
693
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbRel2*sizeof(Geom::Vec3f), positioniF2, GL_STREAM_DRAW);
Pierre Kraemer's avatar
Pierre Kraemer committed
694

Pierre Kraemer's avatar
Pierre Kraemer committed
695
696
	this->m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbRel1*sizeof(Geom::Vec3f), positioniF1, GL_STREAM_DRAW);
Pierre Kraemer's avatar
Pierre Kraemer committed
697

698
699
700
	delete[] positioniF1;
	delete[] positioniF2;
	delete[] positioniF3;
Pierre Kraemer's avatar
Pierre Kraemer committed
701
}
Sylvain Thery's avatar
Sylvain Thery committed
702

703

Sylvain Thery's avatar
Sylvain Thery committed
704

705
706


707

708
template<typename PFP>
709
void Topo3RenderGMap<PFP>::updateData(MAP& mapx, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, float kv)
Thomas's avatar
Thomas committed
710
{
Pierre Kraemer's avatar
Pierre Kraemer committed
711
//	GMap3& map = dynamic_cast<GMap3&>(mapx);	// TODO reflechir comment virer ce warning quand on compile avec PFP::MAP=Map3
712

Pierre Kraemer's avatar
Pierre Kraemer committed
713
	if (this->m_attIndex.map() != &mapx)
714
		this->m_attIndex = mapx.template getAttribute<unsigned int, DART, MAP>("dart_index3");
Pierre Kraemer's avatar
Pierre Kraemer committed
715
	if (!this->m_attIndex.isValid())
716
		this->m_attIndex = mapx.template addAttribute<unsigned int, DART, MAP>("dart_index3");
Thomas's avatar
Thomas committed
717

Pierre Kraemer's avatar
Pierre Kraemer committed
718
	this->m_nbDarts = 0;
Sylvain Thery's avatar
Sylvain Thery committed
719
	for (Dart d = mapx.begin(); d != mapx.end(); mapx.next(d))
Thomas's avatar
Thomas committed
720
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
721
722
		if (!mapx.template isBoundaryMarked<3>(d)) // in the following code Traversor do not traverse boundary
			this->m_nbDarts++;
723
724
725
	}

	// compute center of each volumes
726
	VolumeAutoAttribute<VEC3, MAP> centerVolumes(mapx, "centerVolumes");
727
	Algo::Volume::Geometry::Parallel::computeCentroidELWVolumes<PFP>(mapx, positions, centerVolumes);
728
729

	// beta1
730
	DartAutoAttribute<VEC3, MAP> fv1(mapx);
731
	// beta2/3
732
733
	DartAutoAttribute<VEC3, MAP> fv2(mapx);
	DartAutoAttribute<VEC3, MAP> fv2x(mapx);
734

Pierre Kraemer's avatar
Pierre Kraemer committed
735
736
	this->m_vbo4->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
737
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
738
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
739

Pierre Kraemer's avatar
Pierre Kraemer committed
740
741
742
743
	if (this->m_bufferDartPosition != NULL)
		delete this->m_bufferDartPosition;
	this->m_bufferDartPosition = new Geom::Vec3f[2*this->m_nbDarts];
	Geom::Vec3f* positionDartBuf = reinterpret_cast<Geom::Vec3f*>(this->m_bufferDartPosition);
744

Sylvain Thery's avatar
Sylvain Thery committed
745

746

747
	std::vector<Dart> vecDartFaces;
Pierre Kraemer's avatar
Pierre Kraemer committed
748
	vecDartFaces.reserve(this->m_nbDarts/6);
749
	unsigned int posDBI = 0;
750
751

	//traverse each face of each volume
Pierre Kraemer's avatar
Pierre Kraemer committed
752
	TraversorCell<MAP, PFP::MAP::FACE_OF_PARENT> traFace(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
753
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
754
	{
755
756
757
758
759
760
761
762
763
		vecDartFaces.push_back(d);

		std::vector<VEC3> vecPos;
		vecPos.reserve(16);

		// store the face & center
		float okv = 1.0f - kv;

		VEC3 vc = centerVolumes[d];
764
		
765
		VEC3 centerFace = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx, d, positions)*kv +vc*okv;
766
767
768

		//shrink the face
		float okf = 1.0f - kf;
769
770
		Dart dd = d;
		do
771
		{
772
			VEC3 P = centerFace*okf + (vc*okv + positions[dd]*kv)*kf;
773
			vecPos.push_back(P);
Sylvain Thery's avatar
Sylvain Thery committed
774
			dd = mapx.phi1(dd);
775
		} while (dd != d);
776
		
777
		unsigned int nb = uint32(vecPos.size());
778
		
779
		vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop
780

781
782
783
784
785
786
		// compute position of points to use for drawing topo
		float oke = 1.0f - ke;
		for (unsigned int i = 0; i < nb; ++i)
		{
			VEC3 P = vecPos[i]*ke + vecPos[i+1]*oke;
			VEC3 Q = vecPos[i+1]*ke + vecPos[i]*oke;
787

788
789
			VEC3 PP = 0.52f*P + 0.48f*Q;
			VEC3 QQ = 0.52f*Q + 0.48f*P;
790

791
792
793
794
			*positionDartBuf++ = PFP::toVec3f(P);
			*positionDartBuf++ = PFP::toVec3f(PP);
			*positionDartBuf++ = PFP::toVec3f(Q);
			*positionDartBuf++ = PFP::toVec3f(QQ);
Pierre Kraemer's avatar
Pierre Kraemer committed
795
796
797
798
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
799

Pierre Kraemer's avatar
Pierre Kraemer committed
800
			this->m_attIndex[d] = posDBI;
801
			posDBI+=2;
802

803
804
805
			fv1[d] = P*0.9f + PP*0.1f;
			fv2x[d] = P*0.52f + PP*0.48f;
			fv2[d] = P*0.48f + PP*0.52f;
Pierre Kraemer's avatar
Pierre Kraemer committed
806
			Dart dx = mapx.beta0(d);
807
808
809
			fv1[dx] = Q*0.9f + QQ*0.1f;
			fv2[dx] = Q*0.52f + QQ*0.48f;
			fv2x[dx] = Q*0.48f + QQ*0.52f;
810

Pierre Kraemer's avatar
Pierre Kraemer committed
811
			this->m_attIndex[dx] = posDBI;
812
			posDBI+=2;
813

814
815
			d = mapx.phi1(d);
		}
816
817
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
818
819
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), this->m_bufferDartPosition, GL_STREAM_DRAW);
820
821
//	m_vbo0->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
822

Pierre Kraemer's avatar
Pierre Kraemer committed
823
	this->m_vbo4->bind();
824
825
826
	glUnmapBuffer(GL_ARRAY_BUFFER);

	// beta3
Pierre Kraemer's avatar
Pierre Kraemer committed
827
828
	this->m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
829
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
830

831
	// beta3
Pierre Kraemer's avatar
Pierre Kraemer committed
832
833
	this->m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
834
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
835
836

	// beta3
Pierre Kraemer's avatar
Pierre Kraemer committed
837
838
	this->m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
839
	GLvoid* PositionBuffer3 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
840

841
842
843
	Geom::Vec3f* positionF1 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer1);
	Geom::Vec3f* positionF2 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer2);
	Geom::Vec3f* positionF3 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer3);
844

Pierre Kraemer's avatar
Pierre Kraemer committed
845
846
	this->m_nbRel2 = 0;
	this->m_nbRel3 = 0;
847
848
849
850
851
852

	for(std::vector<Dart>::iterator face = vecDartFaces.begin(); face != vecDartFaces.end(); ++face)
	{
		Dart d = *face;
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
853
			Dart e = mapx.beta2(d);
854
855
			if (d < e)
			{
856
857
858
859
				*positionF2++ = PFP::toVec3f(fv2[d]);
				*positionF2++ = PFP::toVec3f(fv2x[e]);
				*positionF2++ = PFP::toVec3f(fv2[e]);
				*positionF2++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
860
				this->m_nbRel2++;
861
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
862
			e = mapx.beta3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
863
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e))
864
			{
865
866
867
868
				*positionF3++ = PFP::toVec3f(fv2[d]);
				*positionF3++ = PFP::toVec3f(fv2x[e]);
				*positionF3++ = PFP::toVec3f(fv2[e]);
				*positionF3++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
869
				this->m_nbRel3++;
870
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
871
872
			d = mapx.beta0(d);
			e = mapx.beta2(d);
873
874
			if (d < e)
			{
875
876
877
878
				*positionF2++ = PFP::toVec3f(fv2[d]);
				*positionF2++ = PFP::toVec3f(fv2x[e]);
				*positionF2++ = PFP::toVec3f(fv2[e]);
				*positionF2++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
879
				this->m_nbRel2++;
880
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
881
			e = mapx.beta3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
882
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e))
883
			{
884
885
886
887
				*positionF3++ = PFP::toVec3f(fv2[d]);
				*positionF3++ = PFP::toVec3f(fv2x[e]);
				*positionF3++ = PFP::toVec3f(fv2[e]);
				*positionF3++ = PFP::toVec3f(fv2x[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
888
				this->m_nbRel3++;
889
			}
890
			*positionF1++ = PFP::toVec3f(fv1[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
891
			d = mapx.beta1(d);
892
			*positionF1++ = PFP::toVec3f(fv1[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
893
			this->m_nbRel1++;
894
895
896
		} while (d != *face );
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
897
	this->m_vbo3->bind();
898
	glUnmapBuffer(GL_ARRAY_BUFFER);
899

Pierre Kraemer's avatar
Pierre Kraemer committed
900
	this->m_vbo2->bind();
901
	glUnmapBuffer(GL_ARRAY_BUFFER);
902

Pierre Kraemer's avatar
Pierre Kraemer committed
903
	this->m_vbo1->bind();
904
905
	glUnmapBuffer(GL_ARRAY_BUFFER);

Pierre Kraemer's avatar
Pierre Kraemer committed
906
	this->m_vbo4->bind();
907
908
	glUnmapBuffer(GL_ARRAY_BUFFER);
}
Sylvain Thery's avatar
Sylvain Thery committed
909

Pierre Kraemer's avatar
Pierre Kraemer committed
910
template<typename PFP>
911
void Topo3Render<PFP>::computeDartMiddlePositions(MAP& map, DartAttribute<VEC3, MAP>& posExpl)
Pierre Kraemer's avatar
Pierre Kraemer committed
912
913
{
	m_vbo0->bind();
914
	Geom::Vec3f* positionsPtr = reinterpret_cast<Geom::Vec3f*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY));
Pierre Kraemer's avatar
Pierre Kraemer committed
915
916
917

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
918
		const Geom::Vec3f& v = (positionsPtr[m_attIndex[d]] + positionsPtr[m_attIndex[d]+1])*0.5f;
919
		posExpl[d] = PFP::toVec3f(v);
Pierre Kraemer's avatar
Pierre Kraemer committed
920
921
922
923
924
	}

	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
}
925

untereiner's avatar
untereiner committed
926
//template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
927
//void Topo3Render<PFP>::updateDataMap3OldFashioned(typename PFP::MAP& mapx, const typename PFP::TVEC3& positions, float ke, float kf, float kv)
untereiner's avatar
untereiner committed
928
929
930
931
932
933
934
935
936
//{
//	Map3& map = reinterpret_cast<Map3&>(mapx);
//
//	typedef typename PFP::VEC3 VEC3;
//	typedef typename PFP::REAL REAL;
//
//
//	if (m_attIndex.map() != &map)
//	{
937
//		m_attIndex  = map.template getAttribute<unsigned int>(DART, "dart_index3");
untereiner's avatar
untereiner committed
938
//		if (!m_attIndex.isValid())
939
//			m_attIndex  = map.template addAttribute<unsigned int>(DART, "dart_index3");
untereiner's avatar
untereiner committed
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
//	}
//
//	m_nbDarts = 0;
//
//	// table of center of volume
//	std::vector<VEC3> vecCenters;
//	vecCenters.reserve(1000);
//	// table of nbfaces per volume
//	std::vector<unsigned int> vecNbFaces;
//	vecNbFaces.reserve(1000);
//	// table of face (one dart of each)
//	std::vector<Dart> vecDartFaces;
//	vecDartFaces.reserve(map.getNbDarts()/4);
//
//	unsigned int posDBI=0;
//
//	DartMarker mark(map);					// marker for darts
//	for (Dart d = map.begin(); d != map.end(); map.next(d))
//	{
959
//
untereiner's avatar
untereiner committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
//			CellMarkerStore markVert(map, VERTEX);		//marker for vertices
//			VEC3 center(0, 0, 0);
//			unsigned int nbv = 0;
//			unsigned int nbf = 0;
//			std::list<Dart> visitedFaces;	// Faces that are traversed
//			visitedFaces.push_back(d);		// Start with the face of d
//
//			// For every face added to the list
//			for (std::list<Dart>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
//			{
//				if (!mark.isMarked(*face))		// Face has not been visited yet
//				{
//					// store a dart of face
//					vecDartFaces.push_back(*face);
//					nbf++;
//					Dart dNext = *face ;
//					do
//					{
//						if (!markVert.isMarked(dNext))
//						{
//							markVert.mark(dNext);
//							center += positions[dNext];
//							nbv++;
//						}
//						mark.mark(dNext);					// Mark
//						m_nbDarts++;
//						Dart adj = map.phi2(dNext);				// Get adjacent face
//						if (adj != dNext && !mark.isMarked(adj))
//							visitedFaces.push_back(adj);	// Add it
//						dNext = map.phi1(dNext);
//					} while(dNext != *face);
//				}
//			}
//			center /= typename PFP::REAL(nbv);
//			vecCenters.push_back(center);
//			vecNbFaces.push_back(nbf);
//	}
//
//	// debut phi1
999
//	DartAutoAttribute<VEC3> fv1(map);
untereiner's avatar
untereiner committed
1000
//	// fin phi1
1001
//	DartAutoAttribute<VEC3> fv11(map);
untereiner's avatar
untereiner committed
1002
1003
//
//	// phi2
1004
1005
//	DartAutoAttribute<VEC3> fv2(map);
//	DartAutoAttribute<VEC3> fv2x(map);
untereiner's avatar
untereiner committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
//
//	m_vbo4->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
//	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
//	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);
//
//	m_vbo0->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
//	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
//	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);
//
//
//
//	std::vector<Dart>::iterator face = vecDartFaces.begin();
//	for (unsigned int iVol=0; iVol<vecNbFaces.size(); ++iVol)
//	{
//		for (unsigned int iFace = 0; iFace < vecNbFaces[iVol]; ++iFace)
//		{
//			Dart d = *face++;
//
//			std::vector<VEC3> vecPos;
//			vecPos.reserve(16);
//
//			// store the face & center
//			VEC3 center(0, 0, 0);
//			Dart dd = d;
//			do
//			{
//				const VEC3& P = positions[d];
//				vecPos.push_back(P);
//				center += P;
//				d = map.phi1(d);
//			} while (d != dd);
//			center /= REAL(vecPos.size());
//
//			//shrink the face
//			unsigned int nb = vecPos.size();
//			float okf = 1.0f - kf;
//			float okv = 1.0f - kv;
//			for (unsigned int i = 0; i < nb; ++i)
//			{
//				vecPos[i] = vecCenters[iVol]*okv + vecPos[i]*kv;
//				vecPos[i] = center*okf + vecPos[i]*kf;
//			}
//			vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop
//
//			// compute position of points to use for drawing topo
//			float oke = 1.0f - ke;
//			for (unsigned int i = 0; i < nb; ++i)
//			{
//				VEC3 P = vecPos[i]*ke + vecPos[i+1]*oke;
//				VEC3 Q = vecPos[i+1]*ke + vecPos[i]*oke;
//
//				m_attIndex[d] = posDBI;
//				posDBI+=2;
//
//				*positionDartBuf++ = P;
//				*positionDartBuf++ = Q;
//				*colorDartBuf++ = VEC3(1.,1.,1.0);
//				*colorDartBuf++ = VEC3(1.,1.,1.0);
//
//				fv1[d] = P*0.1f + Q*0.9f;
//				fv11[d] = P*0.9f + Q*0.1f;
//
//				fv2[d] = P*0.52f + Q*0.48f;
//				fv2x[d] = P*0.48f + Q*0.52f;
//
//				d = map.phi1(d);
//			}
//
//		}
//	}
//
//	m_vbo0->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
//
//	// phi1
//	m_vbo1->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
1085
//	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1086
1087
1088
1089
//
//	//phi2
//	m_vbo2->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
1090
//	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1091
1092
1093
1094
//
//	//phi3
//	m_vbo3->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
1095
//	GLvoid* PositionBuffer3 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
//
//	VEC3* positionF1 = reinterpret_cast<VEC3*>(PositionBuffer1);
//	VEC3* positionF2 = reinterpret_cast<VEC3*>(PositionBuffer2);
//	VEC3* positionF3 = reinterpret_cast<VEC3*>(PositionBuffer3);
//
//	m_nbRel2=0;
//	m_nbRel3=0;
//
//	for(std::vector<Dart>::iterator face = vecDartFaces.begin(); face != vecDartFaces.end(); ++face)
//	{
//		Dart d = *face;
//		do
//		{
//			Dart e = map.phi2(d);
//			if (d < e)
//			{
//				*positionF2++ = fv2[d];
//				*positionF2++ = fv2x[e];
//				*positionF2++ = fv2[e];
//				*positionF2++ = fv2x[d];
//				m_nbRel2++;
//			}
//			e = map.phi3(d);
Thery Sylvain's avatar
Thery Sylvain committed
1119
//			if (!map.isBoundaryMarked3(e) && (d < e))
untereiner's avatar
untereiner committed
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
//			{
//				*positionF3++ = fv2[d];
//				*positionF3++ = fv2x[e];
//				*positionF3++ = fv2[e];
//				*positionF3++ = fv2x[d];
//				m_nbRel3++;
//			}
//			e = map.phi1(d);
//			*positionF1++ = fv1[d];
//			*positionF1++ = fv11[e];
//
//			d = map.phi1(d);
//		} while (d != *face );
//	}
//
//	m_vbo3->bind();
1136
//	glUnmapBuffer(GL_ARRAY_BUFFER);
untereiner's avatar
untereiner committed
1137
1138
//
//	m_vbo2->bind();
1139
//	glUnmapBuffer(GL_ARRAY_BUFFER);
untereiner's avatar
untereiner committed
1140
1141
1142
1143
1144
1145
1146
//
//	m_vbo1->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
//
//	m_vbo4->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
//}
1147

1148
template<typename PFP>
1149
Dart Topo3Render<PFP>::coneSelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float angle)
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
{
	float AB2 = rayAB*rayAB;
	Dart dFinal;
	double sin2 = sin(M_PI/180.0 * angle);
	sin2 = sin2*sin2;
	double dist2 = std::numeric_limits<double>::max();

	for(Dart d = map.begin(); d!=map.end(); map.next(d))
	{
		// get back position of segment PQ
		const Geom::Vec3f& P = m_bufferDartPosition[m_attIndex[d]];
		const Geom::Vec3f& Q =m_bufferDartPosition[m_attIndex[d]+1];
		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
		Geom::Vec3f V = (P+Q)/2.0f - rayA;
		double d2 = double(V*V);
		double s2 = double(ld2) / d2;
		if (s2 < sin2)
		{
			if (d2<dist2)
			{
				dist2 = d2;
				dFinal = d;
			}
		}
	}
	return dFinal;
}

template<typename PFP>
1179
Dart Topo3Render<PFP>::raySelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float dmax)
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
{
	float AB2 = rayAB*rayAB;
	Dart dFinal;
	float dm2 = dmax*dmax;
	double dist2 = std::numeric_limits<double>::max();

	for(Dart d = map.begin(); d!=map.end(); map.next(d))
	{
		// get back position of segment PQ
		const Geom::Vec3f& P = m_bufferDartPosition[m_attIndex[d]];
		const Geom::Vec3f& Q =m_bufferDartPosition[m_attIndex[d]+1];
		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
		if (ld2<dm2)
		{
			Geom::Vec3f V = (P+Q)/2.0f - rayA;
			double d2 = double(V*V);
			if (d2<dist2)
			{
				dist2 = d2;
				dFinal = d;
			}
		}
	}
	return dFinal;
}

// DART RAY SELECTION
//template<typename PFP>
1208
//void edgesConeSelection(, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, float angle, std::vector<Dart>& vecEdges)
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
//{
//	typename PFP::REAL AB2 = rayAB * rayAB;
//
//	double sin2 = sin(M_PI/180.0 * angle);
//	sin2 = sin2*sin2;
//
//	// recuperation des aretes intersectees
//	vecEdges.reserve(256);
//	vecEdges.clear();
//
//	TraversorE<typename PFP::MAP> trav(map);
//	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
//	{
//		// get back position of segment PQ
//		const typename PFP::VEC3& P = position[d];
//		const typename PFP::VEC3& Q = position[map.phi1(d)];
//		// the three distance to P, Q and (PQ) not used here
//		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
//		typename PFP::VEC3 V = P - rayA;
//		double s2 = double(ld2) / double(V*V);
//		if (s2 < sin2)
//			vecEdges.push_back(d);
//	}
//
//	typedef std::pair<typename PFP::REAL, Dart> DartDist;
//	std::vector<DartDist> distndart;
//
//	unsigned int nbi = vecEdges.size();
//	distndart.resize(nbi);
//
//	// compute all distances to observer for each middle of intersected edge
//	// and put them in a vector for sorting
//	for (unsigned int i = 0; i < nbi; ++i)
//	{
//		Dart d = vecEdges[i];
//		distndart[i].second = d;
//		typename PFP::VEC3 V = (position[d] + position[map.phi1(d)]) / typename PFP::REAL(2);
//		V -= rayA;
//		distndart[i].first = V.norm2();
//	}
//
//	// sort the vector of pair dist/dart
//	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);
//
//	// store sorted darts in returned vector
//	for (unsigned int i = 0; i < nbi; ++i)
//		vecEdges[i] = distndart[i].second;
//}

1258
} //end namespace GL2
1259

1260
} //end namespace Render
Pierre Kraemer's avatar
Pierre Kraemer committed
1261

1262
} //end namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
1263

1264
} //end namespace CGoGN