topo3Render.hpp 36 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 74 75 76 77 78 79 80 81 82 83 84 85 86 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137

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

	m_shader1 = new Utils::ShaderSimpleColor();
	m_shader2 = new Utils::ShaderColorPerVertex();

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

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

	// registering for auto matrices update
	Utils::GLSLShader::registerShader(NULL, m_shader1);
	Utils::GLSLShader::registerShader(NULL, m_shader2);
}

template<typename PFP>
Topo3Render<PFP>::~Topo3Render()
{
	Utils::GLSLShader::unregisterShader(NULL, m_shader2);
	Utils::GLSLShader::unregisterShader(NULL, m_shader1);

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

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();
138
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142 143 144 145
	float* colorDartBuf = reinterpret_cast<float*>(ColorDartsBuffer);
	for (unsigned int i=0; i < 2*m_nbDarts; ++i)
	{
		*colorDartBuf++ = r;
		*colorDartBuf++ = g;
		*colorDartBuf++ = b;
	}
146
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
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

}

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;

	m_shader2->enableVertexAttribs();

	glLineWidth(m_topo_dart_width);
	glDrawArrays(GL_LINES, 0, m_nbDarts*2);

	// change the stride to take 1/2 vertices
	m_shader2->enableVertexAttribs(6*sizeof(GL_FLOAT));

	glPointSize(2.0f*m_topo_dart_width);
	glDrawArrays(GL_POINTS, 0, m_nbDarts);

	m_shader2->disableVertexAttribs();

//
////	glColor3f(1.0f,1.0f,1.0f);
//	glLineWidth(m_topo_dart_width);
//	glPointSize(2.0f*m_topo_dart_width);
//
180
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[4]);
Pierre Kraemer's avatar
Pierre Kraemer committed
181 182 183
//	glColorPointer(3, GL_FLOAT, 0, 0);
//	glEnableClientState(GL_COLOR_ARRAY);
//
184
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[0]);
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188 189 190
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
//
// 	glVertexPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), 0);
//
191
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[4]);
Pierre Kraemer's avatar
Pierre Kraemer committed
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
// 	glColorPointer(3, GL_FLOAT, 6*sizeof(GL_FLOAT), 0);
// 	glDrawArrays(GL_POINTS, 0, m_nbDarts)
// 	;
//	glDisableClientState(GL_COLOR_ARRAY);
//	glDisableClientState(GL_VERTEX_ARRAY);

}

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

	glLineWidth(m_topo_relation_width);

	m_shader1->changeVA_VBO(m_vaId, m_vbo1);
	m_shader1->setColor(Geom::Vec4f(0.0f,1.0f,1.0f,0.0f));
	m_shader1->enableVertexAttribs();

	glDrawArrays(GL_LINES, 0, m_nbRel1*2);

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(0.0f,1.0f,1.0f);
218
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[1]);
Pierre Kraemer's avatar
Pierre Kraemer committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_LINES, 0, m_nbDarts*2);
//	glDisableClientState(GL_VERTEX_ARRAY);
}

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

	m_shader1->changeVA_VBO(m_vaId, m_vbo2);
	m_shader1->setColor(Geom::Vec4f(1.0f,0.0f,0.0f,0.0f));
	m_shader1->enableVertexAttribs();

	glDrawArrays(GL_QUADS, 0, m_nbRel2*4);

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(1.0f,0.0f,0.0f);
242
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[2]);
Pierre Kraemer's avatar
Pierre Kraemer committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_QUADS, 0, m_nbRel2*4);
//	glDisableClientState(GL_VERTEX_ARRAY);
}

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);
	m_shader1->enableVertexAttribs();

	glDrawArrays(GL_QUADS, 0, m_nbRel3*4);

	m_shader1->disableVertexAttribs();

//	glLineWidth(m_topo_relation_width);
//	glColor3f(1.0f,1.0f,0.0f);
266
//	glBindBuffer(GL_ARRAY_BUFFER, m_VBOBuffers[3]);
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269 270 271 272 273 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
//	glVertexPointer(3, GL_FLOAT, 0, 0);
//
//	glEnableClientState(GL_VERTEX_ARRAY);
//	glDrawArrays(GL_QUADS, 0, m_nbRel3*4);
//	glDisableClientState(GL_VERTEX_ARRAY);
}

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

	m_shader1->changeVA_VBO(m_vaId, m_vbo0);
	m_shader1->setColor(Geom::Vec4f(r,g,b,0.0f));
	m_shader1->enableVertexAttribs();

	glLineWidth(width);
	glDrawArrays(GL_LINES, indexDart, 2);

	glPointSize(2.0f*width);
	glDrawArrays(GL_POINTS, indexDart, 1);

	m_shader2->disableVertexAttribs();
}

template<typename PFP>
void Topo3Render<PFP>::pushColors()
{
	m_color_save = new float[6*m_nbDarts];
	m_vbo4->bind();
306
	void* colorBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
307 308 309 310 311 312 313 314 315

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

template<typename PFP>
void Topo3Render<PFP>::popColors()
{
	m_vbo4->bind();
316
	void* colorBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

	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
	unsigned int dw = m_topo_dart_width;
	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();
}

477
//template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
478
//void Topo3Render<PFP>::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, float kv)
479 480 481 482 483 484 485 486 487 488 489 490
//{
//	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);
//	}
//}
491

Pierre Kraemer's avatar
Pierre Kraemer committed
492
//template<typename PFP>
493
//void Topo3Render<PFP>::updateData(MAP& map, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, float kv)
Pierre Kraemer's avatar
Pierre Kraemer committed
494 495 496 497 498 499 500 501 502 503 504 505 506 507
//{
//	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
508
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
509
void Topo3Render<PFP>::setDartsIdColor(typename PFP::MAP& map)
510
{
Pierre Kraemer's avatar
Pierre Kraemer committed
511 512 513 514 515
	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))
516
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
517
		if ( !map.template isBoundaryMarked<3>(d)) // topo3 Render do not traverse boundary
Pierre Kraemer's avatar
Pierre Kraemer committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
		{
			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;
			}
		}
539
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
540 541 542 543
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
544
void Topo3Render<PFP>::updateColors(MAP& map, const VertexAttribute<Geom::Vec3f, MAP>& colors)
Pierre Kraemer's avatar
Pierre Kraemer committed
545 546 547 548 549 550
{
	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))
551
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
552 553 554 555 556 557 558 559 560 561 562 563 564
		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;
			}
		}
565
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
566
	glUnmapBuffer(GL_ARRAY_BUFFER);
567 568
}

Pierre Kraemer's avatar
Pierre Kraemer committed
569
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
570
Dart Topo3Render<PFP>::picking(MAP& map, int x, int y)
Pierre Kraemer's avatar
Pierre Kraemer committed
571
{
Pierre Kraemer's avatar
Pierre Kraemer committed
572 573 574 575 576 577
	pushColors();
	setDartsIdColor(map);
	Dart d = pickColor(x,y);
	popColors();
	return d;
}
Sylvain Thery's avatar
Sylvain Thery committed
578

Pierre Kraemer's avatar
Pierre Kraemer committed
579

Pierre Kraemer's avatar
Pierre Kraemer committed
580 581 582 583 584




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

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

	this->m_nbDarts = 0;
Sylvain Thery's avatar
Sylvain Thery committed
593
	for (Dart d = mapx.begin(); d != mapx.end(); mapx.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
594
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
595 596
		if (!mapx.template isBoundaryMarked<3>(d)) // in the following code Traversor do not traverse boundary
			this->m_nbDarts++;
597 598 599
	}

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
605
	// debut phi1
606
	DartAutoAttribute<VEC3, MAP> fv1(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
607
	// fin phi1
608
	DartAutoAttribute<VEC3, MAP> fv11(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
609 610

	// phi2
611 612
	DartAutoAttribute<VEC3, MAP> fv2(mapx);
	DartAutoAttribute<VEC3, MAP> fv2x(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
613

Pierre Kraemer's avatar
Pierre Kraemer committed
614 615
	this->m_vbo4->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
616
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
617
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
618

Pierre Kraemer's avatar
Pierre Kraemer committed
619 620
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
621
	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
622
	Geom::Vec3f* positionDartBuf = reinterpret_cast<Geom::Vec3f*>(PositionDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
623

624
	std::vector<Dart> vecDartFaces;
Pierre Kraemer's avatar
Pierre Kraemer committed
625
	vecDartFaces.reserve(this->m_nbDarts/3);
626
	unsigned int posDBI = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
627

628
	// traverse each face of each volume
Pierre Kraemer's avatar
Pierre Kraemer committed
629
	TraversorCell<MAP, PFP::MAP::FACE_OF_PARENT> traFace(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
630
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
631
	{
632 633 634 635 636 637 638 639 640
		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
641

642 643 644 645
		VEC3 centerFace = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx,d,positions)*kv +vc*okv;

		//shrink the face
		float okf = 1.0f - kf;
646 647
		Dart dd = d;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
648
		{
649
			VEC3 P = centerFace*okf + (vc*okv + positions[dd]*kv)*kf;
650
			vecPos.push_back(P);
Sylvain Thery's avatar
Sylvain Thery committed
651
			dd = mapx.phi1(dd);
652
		} while (dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
653

654
		unsigned int nb = vecPos.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
655

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

658 659 660 661
		// compute position of points to use for drawing topo
		float oke = 1.0f - ke;
		for (unsigned int i = 0; i < nb; ++i)
		{
662 663
			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
664

Pierre Kraemer's avatar
Pierre Kraemer committed
665
			this->m_attIndex[d] = posDBI;
666
			posDBI+=2;
Pierre Kraemer's avatar
Pierre Kraemer committed
667

668 669
			*positionDartBuf++ = PFP::toVec3f(P);
			*positionDartBuf++ = PFP::toVec3f(Q);
Pierre Kraemer's avatar
Pierre Kraemer committed
670 671
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
672

673 674
			fv1[d] = P*0.1f + Q*0.9f;
			fv11[d] = P*0.9f + Q*0.1f;
Pierre Kraemer's avatar
Pierre Kraemer committed
675

676 677
			fv2[d] = P*0.52f + Q*0.48f;
			fv2x[d] = P*0.48f + Q*0.52f;
Sylvain Thery's avatar
Sylvain Thery committed
678
			d = mapx.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
679 680 681
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
682
	this->m_vbo0->bind();
683
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
684

Pierre Kraemer's avatar
Pierre Kraemer committed
685
	this->m_vbo4->bind();
686 687
	glUnmapBuffer(GL_ARRAY_BUFFER);

Pierre Kraemer's avatar
Pierre Kraemer committed
688 689 690
	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
691

692 693 694
	Geom::Vec3f* positionF1 = positioniF1;
	Geom::Vec3f* positionF2 = positioniF2;
	Geom::Vec3f* positionF3 = positioniF3;
Pierre Kraemer's avatar
Pierre Kraemer committed
695

Pierre Kraemer's avatar
Pierre Kraemer committed
696 697 698
	this->m_nbRel1 = 0;
	this->m_nbRel2 = 0;
	this->m_nbRel3 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
699 700 701 702 703 704

	for(std::vector<Dart>::iterator face = vecDartFaces.begin(); face != vecDartFaces.end(); ++face)
	{
		Dart d = *face;
		do
		{
Sylvain Thery's avatar
Sylvain Thery committed
705
			Dart e = mapx.phi2(d);
706
			if ((d < e))
Pierre Kraemer's avatar
Pierre Kraemer committed
707
			{
708 709 710 711
				*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
712
				this->m_nbRel2++;
Pierre Kraemer's avatar
Pierre Kraemer committed
713
			}
Sylvain Thery's avatar
Sylvain Thery committed
714
			e = mapx.phi3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
715
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e) )
Pierre Kraemer's avatar
Pierre Kraemer committed
716
			{
717 718 719 720
				*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
721
				this->m_nbRel3++;
Pierre Kraemer's avatar
Pierre Kraemer committed
722
			}
Sylvain Thery's avatar
Sylvain Thery committed
723
			e = mapx.phi1(d);
724 725
			*positionF1++ = PFP::toVec3f(fv1[d]);
			*positionF1++ = PFP::toVec3f(fv11[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
726
			this->m_nbRel1++;
727

Sylvain Thery's avatar
Sylvain Thery committed
728
			d = mapx.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
729 730 731
		} while (d != *face );
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
732 733
	this->m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*this->m_nbRel3*sizeof(Geom::Vec3f), positioniF3, GL_STREAM_DRAW);
Pierre Kraemer's avatar
Pierre Kraemer committed
734

Pierre Kraemer's avatar
Pierre Kraemer committed
735 736
	this->m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*this->m_nbRel2*sizeof(Geom::Vec3f), positioniF2, GL_STREAM_DRAW);
Pierre Kraemer's avatar
Pierre Kraemer committed
737

Pierre Kraemer's avatar
Pierre Kraemer committed
738 739
	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
740

741 742 743
	delete[] positioniF1;
	delete[] positioniF2;
	delete[] positioniF3;
Pierre Kraemer's avatar
Pierre Kraemer committed
744
}
Sylvain Thery's avatar
Sylvain Thery committed
745

746

Sylvain Thery's avatar
Sylvain Thery committed
747

748 749


750

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

Pierre Kraemer's avatar
Pierre Kraemer committed
756
	if (this->m_attIndex.map() != &mapx)
757
		this->m_attIndex = mapx.template getAttribute<unsigned int, DART, MAP>("dart_index3");
Pierre Kraemer's avatar
Pierre Kraemer committed
758
	if (!this->m_attIndex.isValid())
759
		this->m_attIndex = mapx.template addAttribute<unsigned int, DART, MAP>("dart_index3");
Thomas's avatar
Thomas committed
760

Pierre Kraemer's avatar
Pierre Kraemer committed
761
	this->m_nbDarts = 0;
Sylvain Thery's avatar
Sylvain Thery committed
762
	for (Dart d = mapx.begin(); d != mapx.end(); mapx.next(d))
Thomas's avatar
Thomas committed
763
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
764 765
		if (!mapx.template isBoundaryMarked<3>(d)) // in the following code Traversor do not traverse boundary
			this->m_nbDarts++;
766 767 768
	}

	// compute center of each volumes
769
	VolumeAutoAttribute<VEC3, MAP> centerVolumes(mapx, "centerVolumes");
770
	Algo::Volume::Geometry::Parallel::computeCentroidELWVolumes<PFP>(mapx, positions, centerVolumes);
771 772

	// beta1
773
	DartAutoAttribute<VEC3, MAP> fv1(mapx);
774
	// beta2/3
775 776
	DartAutoAttribute<VEC3, MAP> fv2(mapx);
	DartAutoAttribute<VEC3, MAP> fv2x(mapx);
777

Pierre Kraemer's avatar
Pierre Kraemer committed
778 779
	this->m_vbo4->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
780
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
781
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
782

Pierre Kraemer's avatar
Pierre Kraemer committed
783 784 785 786
	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);
787 788 789 790 791

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

793
	std::vector<Dart> vecDartFaces;
Pierre Kraemer's avatar
Pierre Kraemer committed
794
	vecDartFaces.reserve(this->m_nbDarts/6);
795
	unsigned int posDBI = 0;
796 797

	//traverse each face of each volume
Pierre Kraemer's avatar
Pierre Kraemer committed
798
	TraversorCell<MAP, PFP::MAP::FACE_OF_PARENT> traFace(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
799
	for (Dart d = traFace.begin(); d != traFace.end(); d = traFace.next())
800
	{
801 802 803 804 805 806 807 808 809
		vecDartFaces.push_back(d);

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

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

		VEC3 vc = centerVolumes[d];
810
		
811
		VEC3 centerFace = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx, d, positions)*kv +vc*okv;
812 813 814

		//shrink the face
		float okf = 1.0f - kf;
815 816
		Dart dd = d;
		do
817
		{
818
			VEC3 P = centerFace*okf + (vc*okv + positions[dd]*kv)*kf;
819
			vecPos.push_back(P);
Sylvain Thery's avatar
Sylvain Thery committed
820
			dd = mapx.phi1(dd);
821
		} while (dd != d);
822
		
823
		unsigned int nb = vecPos.size();
824
		
825
		vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop
826

827 828 829 830 831 832
		// 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;
833

834 835
			VEC3 PP = 0.52f*P + 0.48f*Q;
			VEC3 QQ = 0.52f*Q + 0.48f*P;
836

837 838 839 840
			*positionDartBuf++ = PFP::toVec3f(P);
			*positionDartBuf++ = PFP::toVec3f(PP);
			*positionDartBuf++ = PFP::toVec3f(Q);
			*positionDartBuf++ = PFP::toVec3f(QQ);
Pierre Kraemer's avatar
Pierre Kraemer committed
841 842 843 844
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
			*colorDartBuf++ = this->m_dartsColor;
845

Pierre Kraemer's avatar
Pierre Kraemer committed
846
			this->m_attIndex[d] = posDBI;
847
			posDBI+=2;
848

849 850 851
			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
852
			Dart dx = mapx.beta0(d);
853 854 855
			fv1[dx] = Q*0.9f + QQ*0.1f;
			fv2[dx] = Q*0.52f + QQ*0.48f;
			fv2x[dx] = Q*0.48f + QQ*0.52f;
856

Pierre Kraemer's avatar
Pierre Kraemer committed
857
			this->m_attIndex[dx] = posDBI;
858
			posDBI+=2;
859

860 861
			d = mapx.phi1(d);
		}
862 863
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
864 865
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), this->m_bufferDartPosition, GL_STREAM_DRAW);
866 867
//	m_vbo0->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
868

Pierre Kraemer's avatar
Pierre Kraemer committed
869
	this->m_vbo4->bind();
870 871 872
	glUnmapBuffer(GL_ARRAY_BUFFER);

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

877
	// beta3
Pierre Kraemer's avatar
Pierre Kraemer committed
878 879
	this->m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
880
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
881 882

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

887 888 889
	Geom::Vec3f* positionF1 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer1);
	Geom::Vec3f* positionF2 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer2);
	Geom::Vec3f* positionF3 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer3);
890

Pierre Kraemer's avatar
Pierre Kraemer committed
891 892
	this->m_nbRel2 = 0;
	this->m_nbRel3 = 0;
893 894 895 896 897 898

	for(std::vector<Dart>::iterator face = vecDartFaces.begin(); face != vecDartFaces.end(); ++face)
	{
		Dart d = *face;
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
899
			Dart e = mapx.beta2(d);
900 901
			if (d < e)
			{
902 903 904 905
				*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
906
				this->m_nbRel2++;
907
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
908
			e = mapx.beta3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
909
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e))
910
			{
911 912 913 914
				*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
915
				this->m_nbRel3++;
916
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
917 918
			d = mapx.beta0(d);
			e = mapx.beta2(d);
919 920
			if (d < e)
			{
921 922 923 924
				*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
925
				this->m_nbRel2++;
926
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
927
			e = mapx.beta3(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
928
			if (!mapx.template isBoundaryMarked<3>(e) && (d < e))
929
			{
930 931 932 933
				*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
934
				this->m_nbRel3++;
935
			}
936
			*positionF1++ = PFP::toVec3f(fv1[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
937
			d = mapx.beta1(d);
938
			*positionF1++ = PFP::toVec3f(fv1[d]);
Pierre Kraemer's avatar
Pierre Kraemer committed
939
			this->m_nbRel1++;
940 941 942
		} while (d != *face );
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
943
	this->m_vbo3->bind();
944
	glUnmapBuffer(GL_ARRAY_BUFFER);
945

Pierre Kraemer's avatar
Pierre Kraemer committed
946
	this->m_vbo2->bind();
947
	glUnmapBuffer(GL_ARRAY_BUFFER);
948

Pierre Kraemer's avatar
Pierre Kraemer committed
949
	this->m_vbo1->bind();
950 951
	glUnmapBuffer(GL_ARRAY_BUFFER);

Pierre Kraemer's avatar
Pierre Kraemer committed
952
	this->m_vbo4->bind();
953 954
	glUnmapBuffer(GL_ARRAY_BUFFER);
}
Sylvain Thery's avatar
Sylvain Thery committed
955

Pierre Kraemer's avatar
Pierre Kraemer committed
956
template<typename PFP>
957
void Topo3Render<PFP>::computeDartMiddlePositions(MAP& map, DartAttribute<VEC3, MAP>& posExpl)
Pierre Kraemer's avatar
Pierre Kraemer committed
958 959
{
	m_vbo0->bind();
960
	Geom::Vec3f* positionsPtr = reinterpret_cast<Geom::Vec3f*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_ONLY));
Pierre Kraemer's avatar
Pierre Kraemer committed
961 962 963

	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
964
		const Geom::Vec3f& v = (positionsPtr[m_attIndex[d]] + positionsPtr[m_attIndex[d]+1])*0.5f;
965
		posExpl[d] = PFP::toVec3f(v);
Pierre Kraemer's avatar
Pierre Kraemer committed
966 967 968 969 970
	}

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

untereiner's avatar
untereiner committed
972
//template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
973
//void Topo3Render<PFP>::updateDataMap3OldFashioned(typename PFP::MAP& mapx, const typename PFP::TVEC3& positions, float ke, float kf, float kv)
untereiner's avatar
untereiner committed
974 975 976 977 978 979 980 981 982
//{
//	Map3& map = reinterpret_cast<Map3&>(mapx);
//
//	typedef typename PFP::VEC3 VEC3;
//	typedef typename PFP::REAL REAL;
//
//
//	if (m_attIndex.map() != &map)
//	{
983
//		m_attIndex  = map.template getAttribute<unsigned int>(DART, "dart_index3");
untereiner's avatar
untereiner committed
984
//		if (!m_attIndex.isValid())
985
//			m_attIndex  = map.template addAttribute<unsigned int>(DART, "dart_index3");
untereiner's avatar
untereiner committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
//	}
//
//	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))
//	{
1005
//
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
//			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
1045
//	DartAutoAttribute<VEC3> fv1(map);
untereiner's avatar
untereiner committed
1046
//	// fin phi1
1047
//	DartAutoAttribute<VEC3> fv11(map);
untereiner's avatar
untereiner committed
1048 1049
//
//	// phi2
1050 1051
//	DartAutoAttribute<VEC3> fv2(map);
//	DartAutoAttribute<VEC3> fv2x(map);
untereiner's avatar
untereiner committed
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 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130
//
//	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);
1131
//	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1132 1133 1134 1135
//
//	//phi2
//	m_vbo2->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
1136
//	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1137 1138 1139 1140
//
//	//phi3
//	m_vbo3->bind();
//	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
1141
//	GLvoid* PositionBuffer3 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
untereiner's avatar
untereiner committed
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
//
//	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
1165
//			if (!map.isBoundaryMarked3(e) && (d < e))
untereiner's avatar
untereiner committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
//			{
//				*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();
1182
//	glUnmapBuffer(GL_ARRAY_BUFFER);
untereiner's avatar
untereiner committed
1183 1184
//
//	m_vbo2->bind();
1185
//	glUnmapBuffer(GL_ARRAY_BUFFER);
untereiner's avatar
untereiner committed
1186 1187 1188 1189 1190 1191 1192
//
//	m_vbo1->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
//
//	m_vbo4->bind();
//	glUnmapBuffer(GL_ARRAY_BUFFER);
//}
1193

1194
template<typename PFP>
1195
Dart Topo3Render<PFP>::coneSelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float angle)
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
{
	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>
1225
Dart Topo3Render<PFP>::raySelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float dmax)
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
{
	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>
1254
//void edgesConeSelection(, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, float angle, std::vector<Dart>& vecEdges)
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
//{
//	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;
//}

1304
} //end namespace GL2
1305

1306
} //end namespace Render
Pierre Kraemer's avatar
Pierre Kraemer committed
1307

1308
} //end namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
1309

1310
} //end namespace CGoGN