topoRender.hpp 25.2 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Geometry/vector_gen.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Topology/generic/dartmarker.h"
28 29 30
#include "Topology/generic/parameters.h"

#include "Topology/map/embeddedMap2.h"
31
#include "Topology/gmap/embeddedGMap2.h"
Pierre Kraemer's avatar
Pierre Kraemer committed
32

33
#include "Algo/Geometry/basic.h"
34
#include "Geometry/distances.h"
35
#include "Algo/Geometry/centroid.h"
36
#include "Algo/Geometry/normal.h"
37

38
#include "Container/containerBrowser.h"
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
namespace CGoGN
{
42

Pierre Kraemer's avatar
Pierre Kraemer committed
43 44
namespace Algo
{
45

Pierre Kraemer's avatar
Pierre Kraemer committed
46 47
namespace Render
{
48

Sylvain Thery's avatar
Sylvain Thery committed
49
namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
50 51
{

Pierre Kraemer's avatar
Pierre Kraemer committed
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 138 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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
template<typename PFP>
TopoRender<PFP>::TopoRender(float bs):
	m_nbDarts(0),
	m_nbRel2(0),
	m_topo_dart_width(2.0f),
	m_topo_relation_width(3.0f),
	m_normalShift(0.0f),
	m_boundShift(bs),
	m_dartsColor(1.0f,1.0f,1.0f),
	m_dartsBoundaryColor(0.7f,1.0f,0.7f),
	m_bufferDartPosition(NULL)
{
	m_vbo0 = new Utils::VBO();
	m_vbo1 = new Utils::VBO();
	m_vbo2 = new Utils::VBO();
	m_vbo3 = new Utils::VBO();

	m_vbo0->setDataSize(3);
	m_vbo1->setDataSize(3);
	m_vbo2->setDataSize(3);
	m_vbo3->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_vbo3);

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

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

	delete m_shader2;
	delete m_shader1;
	delete m_vbo3;
	delete m_vbo2;
	delete m_vbo1;
	delete m_vbo0;

	if (m_attIndex.isValid())
		m_attIndex.map()->removeAttribute(m_attIndex);

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

template<typename PFP>
void TopoRender<PFP>::setDartWidth(float dw)
{
	m_topo_dart_width = dw;
}

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

template<typename PFP>
void TopoRender<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_vbo3->bind();
	glBufferSubData(GL_ARRAY_BUFFER, m_attIndex[d]*3*sizeof(float), 6*sizeof(float),RGB);
}

template<typename PFP>
void TopoRender<PFP>::setAllDartsColor(float r, float g, float b)
{
	m_vbo3->bind();
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
	float* colorDartBuf = reinterpret_cast<float*>(ColorDartsBuffer);
	for (unsigned int i=0; i < 2*m_nbDarts; ++i)
	{
		*colorDartBuf++ = r;
		*colorDartBuf++ = g;
		*colorDartBuf++ = b;
	}

	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

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

template<typename PFP>
void TopoRender<PFP>::setInitialBoundaryDartsColor(float r, float g, float b)
{
	m_dartsBoundaryColor = Geom::Vec3f(r,g,b);
}

template<typename PFP>
void TopoRender<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();
}

template<typename PFP>
void TopoRender<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_nbDarts*2);

	m_shader1->disableVertexAttribs();
}

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

	glLineWidth(m_topo_relation_width);

	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_LINES, 0, m_nbRel2*2);

	m_shader1->disableVertexAttribs();
}
212 213

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 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 306 307 308 309 310 311 312 313 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 434
void TopoRender<PFP>::drawTopo()
{
	drawDarts();
	drawRelation1();
	drawRelation2();
}

template<typename PFP>
void TopoRender<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>
Dart TopoRender<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 TopoRender<PFP>::dartToCol(Dart d, float& r, float& g, float& b)
{
	// here use d.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 TopoRender<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 width
	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]);

	return colToDart(color);
}

template<typename PFP>
void TopoRender<PFP>::pushColors()
{
	m_color_save = new float[6*m_nbDarts];
	m_vbo3->bind();
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

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

template<typename PFP>
void TopoRender<PFP>::popColors()
{
	m_vbo3->bind();
	void* colorBuffer = glMapBufferARB(GL_ARRAY_BUFFER, GL_READ_WRITE);

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

	delete[] m_color_save;
	m_color_save=NULL;
}

template<typename PFP>
void TopoRender<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 TopoRender<PFP>::toSVG(Utils::SVG::SVGOut& svg)
{
//	svg.setWidth(m_topo_relation_width);
//
//	// PHI2 / beta2
//	const Geom::Vec3f* ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo2->lockPtr());
//	svg.beginLines();
//	for (unsigned int i=0; i<m_nbRel2; ++i)
//		svg.addLine(ptr[2*i], ptr[2*i+1],Geom::Vec3f(0.8f,0.0f,0.0f));
//	svg.endLines();
//
//	m_vbo2->releasePtr();
//
//	//PHI1 /beta1
//	ptr = reinterpret_cast<Geom::Vec3f*>(m_vbo1->lockPtr());
//	svg.beginLines();
//	for (unsigned int i=0; i<m_nbRel1; ++i)
//		svg.addLine(ptr[2*i], ptr[2*i+1],Geom::Vec3f(0.0f,0.7f,0.7f));
//	svg.endLines();
//	m_vbo1->releasePtr();
//
//
//	const Geom::Vec3f* colorsPtr = reinterpret_cast<const Geom::Vec3f*>(m_vbo3->lockPtr());
//	ptr= reinterpret_cast<Geom::Vec3f*>(m_vbo0->lockPtr());
//
//	svg.setWidth(m_topo_dart_width);
//	svg.beginLines();
//	for (unsigned int i=0; i<m_nbDarts; ++i)
//		svg.addLine(ptr[2*i], ptr[2*i+1], colorsPtr[2*i]);
//	svg.endLines();
//
//	svg.beginPoints();
//	for (unsigned int i=0; i<m_nbDarts; ++i)
//			svg.addPoint(ptr[2*i], colorsPtr[2*i]);
//	svg.endPoints();
//
//	m_vbo0->releasePtr();
//	m_vbo3->releasePtr();

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

	svg.addGroup(svg1);

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

	svg.addGroup(svg2);

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

	Utils::SVG::SvgGroup* svg3 = new Utils::SVG::SvgGroup("darts", svg.m_model, svg.m_proj);
	svg3->setToLayer();
	svg3->setWidth(m_topo_dart_width);
	svg3->beginLines();
	for (unsigned int i=0; i<m_nbDarts; ++i)
		svg3->addLine(ptr[2*i], ptr[2*i+1], colorsPtr[2*i]);
	svg3->endLines();

	svg.addGroup(svg3);

	Utils::SVG::SvgGroup* svg4 = new Utils::SVG::SvgGroup("dartEmb", svg.m_model, svg.m_proj);
	svg4->setWidth(m_topo_dart_width);
	svg4->setToLayer();
	svg4->beginPoints();
	for (unsigned int i=0; i<m_nbDarts; ++i)
			svg4->addPoint(ptr[2*i], colorsPtr[2*i]);
	svg4->endPoints();

	svg.addGroup(svg4);

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

template<typename PFP>
void TopoRender<PFP>::setNormalShift(float ns)
{
	m_normalShift = ns;
}

template<typename PFP>
void TopoRender<PFP>::setBoundaryShift(float bs)
{
	m_boundShift = bs;
}

template<typename PFP>
435
void TopoRender<PFP>::updateDataBoundary(MAP& map, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, float ns)
436 437
{
	m_normalShift = ns;
438 439
	SelectorDartBoundary<MAP> sdb(map);
	DartContainerBrowserSelector<MAP> browser(map, sdb);
440
	browser.enable();
441
	updateData(map, positions, ke, kf, false,true); // false,true because we are drawing the boundary of a 3map
442
	browser.disable();
443 444 445
	m_normalShift = 0.0f;
}

446 447


Pierre Kraemer's avatar
Pierre Kraemer committed
448

Pierre Kraemer's avatar
Pierre Kraemer committed
449
template<typename PFP>
450
void TopoRenderMap<PFP>::updateData(MAP& mapx, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, bool withBoundary, bool onlyBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
451
{
452 453 454
	std::string name_index("dart_index2");
	if (onlyBoundary)
		name_index = std::string("dart_boundary_index2");
455

Pierre Kraemer's avatar
Pierre Kraemer committed
456
	std::vector<Dart> vecDarts;
untereiner's avatar
untereiner committed
457
	vecDarts.reserve(mapx.getNbDarts());  // no problem dart is int: no problem of memory
Pierre Kraemer's avatar
Pierre Kraemer committed
458

459
	this->m_attIndex = mapx.template getAttribute<unsigned int, DART, MAP>(name_index);
Sylvain Thery's avatar
Sylvain Thery committed
460

Pierre Kraemer's avatar
Pierre Kraemer committed
461
	if (!this->m_attIndex.isValid())
462
		this->m_attIndex  = mapx.template addAttribute<unsigned int, DART, MAP>(name_index);
Pierre Kraemer's avatar
Pierre Kraemer committed
463

Pierre Kraemer's avatar
Pierre Kraemer committed
464
	for(Dart d = mapx.begin(); d != mapx.end(); mapx.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
465
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
466
		if (withBoundary || !mapx.template isBoundaryMarked<2>(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
467 468
			vecDarts.push_back(d);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
469
	this->m_nbDarts = vecDarts.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
470 471

	// debut phi1
472
	DartAutoAttribute<VEC3, MAP> fv1(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
473
	// fin phi1
474
	DartAutoAttribute<VEC3, MAP> fv11(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
475
	// phi2
476
	DartAutoAttribute<VEC3, MAP> fv2(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
477

Pierre Kraemer's avatar
Pierre Kraemer committed
478 479
	this->m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
480
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
481
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
482

483 484 485 486 487
//	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);

Pierre Kraemer's avatar
Pierre Kraemer committed
488 489 490 491
	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);
Pierre Kraemer's avatar
Pierre Kraemer committed
492

493 494
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
495

Pierre Kraemer's avatar
Pierre Kraemer committed
496
	unsigned int indexDC = 0;
497

Pierre Kraemer's avatar
Pierre Kraemer committed
498
	DartMarker<MAP> mf(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
499
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id != vecDarts.end(); id++)
Pierre Kraemer's avatar
Pierre Kraemer committed
500 501 502 503
	{
		Dart d = *id;
		if (!mf.isMarked(d))
		{
504
			vecPos.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
505
			if (!mapx.template isBoundaryMarked<2>(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
506
			{
untereiner's avatar
untereiner committed
507 508
				//VEC3 center = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx,d,positions);
				VEC3 center = Algo::Surface::Geometry::faceCentroid<PFP>(mapx,d,positions);
509 510 511 512 513
				float k = 1.0f - kf;
				Dart dd = d;
				do
				{
					vecPos.push_back(center*k + positions[dd]*kf);
untereiner's avatar
untereiner committed
514
					dd = mapx.phi1(dd);
515
				} while (dd != d);
516

Pierre Kraemer's avatar
Pierre Kraemer committed
517
				if (this->m_normalShift > 0.0f)
518
				{
519 520 521
					VEC3 normal = Algo::Surface::Geometry::newellNormal<PFP>(mapx,d,positions);
					for (typename std::vector<VEC3>::iterator pit = vecPos.begin(); pit != vecPos.end(); ++pit)
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
522
						*pit -= normal * this->m_normalShift;
523
					}
524 525
				}

526 527
				unsigned int nb = vecPos.size();
				vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop
Pierre Kraemer's avatar
Pierre Kraemer committed
528

529 530 531 532 533 534 535
				k = 1.0f - ke;
				for (unsigned int i = 0; i < nb; ++i)
				{

					VEC3 P = vecPos[i]*ke + vecPos[i+1]*k;
					VEC3 Q = vecPos[i+1]*ke + vecPos[i]*k;

Pierre Kraemer's avatar
Pierre Kraemer committed
536
					this->m_attIndex[d] = indexDC;
537
					indexDC+=2;
538 539
					*positionDartBuf++ = PFP::toVec3f(P);
					*positionDartBuf++ = PFP::toVec3f(Q);
Pierre Kraemer's avatar
Pierre Kraemer committed
540 541
					*colorDartBuf++ = this->m_dartsColor;
					*colorDartBuf++ = this->m_dartsColor;
542 543 544 545 546 547
					VEC3 f = P*0.5f + Q*0.5f;
					fv2[d] = f;
					f = P*0.1f + Q*0.9f;
					fv1[d] = f;
					f = P*0.9f + Q*0.1f;
					fv11[d] = f;
untereiner's avatar
untereiner committed
548
					d = mapx.phi1(d);
549
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
550
				mf.template markOrbit<FACE>(d);
551 552
			}
			else if (withBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
553
			{
554 555 556 557 558 559 560 561
				Dart dd = d;
				do
				{
					Dart ee = mapx.phi2(dd);
					VEC3 normal = Algo::Surface::Geometry::newellNormal<PFP>(mapx,ee,positions);
					VEC3 vd = Algo::Surface::Geometry::vectorOutOfDart<PFP>(mapx,ee,positions);
					VEC3 v = vd ^ normal;
					v.normalize();
Pierre Kraemer's avatar
Pierre Kraemer committed
562
					VEC3 P = positions[mapx.phi1(ee)] + v* this->m_boundShift;
563
					vecPos.push_back(P);
untereiner's avatar
untereiner committed
564
					dd = mapx.phi1(dd);
565
					ee = mapx.phi2(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
566
					P = positions[mapx.phi1(ee)] + v* this->m_boundShift;
567 568 569 570 571 572 573 574 575 576 577
					vecPos.push_back(P);
				} while (dd != d);

				unsigned int nb = vecPos.size()/2;
				float k = 1.0f - ke;
				for (unsigned int i = 0; i < nb; ++i)
				{

					VEC3 P = vecPos[2*i]*ke + vecPos[2*i+1]*k;
					VEC3 Q = vecPos[2*i+1]*ke + vecPos[2*i]*k;

Pierre Kraemer's avatar
Pierre Kraemer committed
578
					this->m_attIndex[d] = indexDC;
579
					indexDC+=2;
580 581
					*positionDartBuf++ = PFP::toVec3f(P);
					*positionDartBuf++ = PFP::toVec3f(Q);
Pierre Kraemer's avatar
Pierre Kraemer committed
582 583
					*colorDartBuf++ = this->m_dartsBoundaryColor;
					*colorDartBuf++ = this->m_dartsBoundaryColor;
584 585 586 587 588 589
					VEC3 f = P*0.5f + Q*0.5f;
					fv2[d] = f;
					f = P*0.1f + Q*0.9f;
					fv1[d] = f;
					f = P*0.9f + Q*0.1f;
					fv11[d] = f;
untereiner's avatar
untereiner committed
590
					d = mapx.phi1(d);
591
				}
Pierre Kraemer's avatar
Pierre Kraemer committed
592
				mf.template markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
593 594 595 596
			}
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
597 598
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), this->m_bufferDartPosition, GL_STREAM_DRAW);
599
//	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
600

Pierre Kraemer's avatar
Pierre Kraemer committed
601
	this->m_vbo3->bind();
602
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
603

Pierre Kraemer's avatar
Pierre Kraemer committed
604 605
	this->m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
606
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
607

Pierre Kraemer's avatar
Pierre Kraemer committed
608 609
	this->m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
610
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
611

Pierre Kraemer's avatar
Pierre Kraemer committed
612 613
	Geom::Vec3f* positionF1 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer1);
	Geom::Vec3f* positionF2 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer2);
Pierre Kraemer's avatar
Pierre Kraemer committed
614

Pierre Kraemer's avatar
Pierre Kraemer committed
615
	this->m_nbRel2 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
616 617 618 619
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;

untereiner's avatar
untereiner committed
620
		Dart e = mapx.phi2(d);
621

Sylvain Thery's avatar
Sylvain Thery committed
622
//		if (good(e) && (e.index > d.index))
Pierre Kraemer's avatar
Pierre Kraemer committed
623
		if ( (withBoundary || !mapx.template isBoundaryMarked<2>(e)) && (e.index > d.index))
Pierre Kraemer's avatar
Pierre Kraemer committed
624
		{
625 626
			*positionF2++ = PFP::toVec3f(fv2[d]);
			*positionF2++ = PFP::toVec3f(fv2[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
627
			this->m_nbRel2++;
Pierre Kraemer's avatar
Pierre Kraemer committed
628 629
		}

untereiner's avatar
untereiner committed
630
		e = mapx.phi1(d);
631 632
		*positionF1++ = PFP::toVec3f(fv1[d]);
		*positionF1++ = PFP::toVec3f(fv11[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
633
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
634
	this->m_nbRel1 = vecDarts.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
635

Pierre Kraemer's avatar
Pierre Kraemer committed
636
	this->m_vbo1->bind();
637 638
	glUnmapBuffer(GL_ARRAY_BUFFER);

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

Pierre Kraemer's avatar
Pierre Kraemer committed
643

Pierre Kraemer's avatar
Pierre Kraemer committed
644
template<typename PFP>
645
void TopoRenderGMap<PFP>::updateData(MAP& map, const VertexAttribute<VEC3, MAP>& positions, float ke, float kf, bool withBoundary, bool onlyBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
646
{
Pierre Kraemer's avatar
Pierre Kraemer committed
647
//	GMap2& map = dynamic_cast<GMap2&>(mapx);
648

Pierre Kraemer's avatar
Pierre Kraemer committed
649
	std::vector<Dart> vecDarts;
650
	vecDarts.reserve(map.getNbDarts()); // no problem dart is int: no problem of memory
Pierre Kraemer's avatar
Pierre Kraemer committed
651

652 653 654 655 656
	std::string name_index("dart_index2");
	if (onlyBoundary)
		name_index = std::string("dart_boundary_index2");


Pierre Kraemer's avatar
Pierre Kraemer committed
657
	if (this->m_attIndex.map() != &map)
658
		this->m_attIndex  = map.template getAttribute<unsigned int, DART, MAP>(name_index);
Sylvain Thery's avatar
Sylvain Thery committed
659

Pierre Kraemer's avatar
Pierre Kraemer committed
660
	if (!this->m_attIndex.isValid())
661
		this->m_attIndex  = map.template addAttribute<unsigned int, DART, MAP>(name_index);
Pierre Kraemer's avatar
Pierre Kraemer committed
662

Pierre Kraemer's avatar
Pierre Kraemer committed
663
	for(Dart d = map.begin(); d != map.end(); map.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
664
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
665
		if (withBoundary || !map.template isBoundaryMarked<2>(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
666 667
			vecDarts.push_back(d);
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
668
	this->m_nbDarts = vecDarts.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
669

670
	// debut phi1
671
	DartAutoAttribute<VEC3, MAP> fv1(map);
672
	// fin phi1
673
	DartAutoAttribute<VEC3, MAP> fv11(map);
674
	// phi2
675
	DartAutoAttribute<VEC3, MAP> fv2(map);
676

Pierre Kraemer's avatar
Pierre Kraemer committed
677 678
	this->m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
679
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
680
	Geom::Vec3f* colorDartBuf = reinterpret_cast<Geom::Vec3f*>(ColorDartsBuffer);
Pierre Kraemer's avatar
Pierre Kraemer committed
681

Pierre Kraemer's avatar
Pierre Kraemer committed
682 683
	this->m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
684
	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
685
	Geom::Vec3f* positionDartBuf = reinterpret_cast<Geom::Vec3f*>(PositionDartsBuffer);
686

687 688
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
689

Pierre Kraemer's avatar
Pierre Kraemer committed
690
	unsigned int indexDC = 0;
691

Pierre Kraemer's avatar
Pierre Kraemer committed
692
	DartMarker<MAP> mf(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
693 694 695
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
696

Pierre Kraemer's avatar
Pierre Kraemer committed
697 698
		if (!mf.isMarked(d))
		{
699
			vecPos.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
700
			VEC3 center = Algo::Surface::Geometry::faceCentroidELW<PFP>(map, d, positions);
701 702
			
			float k = 1.0f - kf;
Pierre Kraemer's avatar
Pierre Kraemer committed
703 704 705
			Dart dd = d;
			do
			{
706 707 708
				vecPos.push_back(center*k + positions[dd]*kf);
				dd = map.phi1(dd);
			} while (dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
709

710

Pierre Kraemer's avatar
Pierre Kraemer committed
711
			if (this->m_normalShift > 0.0f)
712
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
713
				VEC3 normal = Algo::Surface::Geometry::newellNormal<PFP>(map, d, positions);
714 715
				for (typename std::vector<VEC3>::iterator pit = vecPos.begin(); pit != vecPos.end(); ++pit)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
716
					*pit -= normal * this->m_normalShift;
717 718
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
719 720 721 722 723 724 725 726 727 728 729
			unsigned int nb = vecPos.size();
			vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop

			k = 1.0f - ke;
			for (unsigned int i = 0; i < nb; ++i)
			{
				VEC3 P = vecPos[i]*ke + vecPos[i+1]*k;
				VEC3 Q = vecPos[i+1]*ke + vecPos[i]*k;
				VEC3 PP = REAL(0.52)*P + REAL(0.48)*Q;
				VEC3 QQ = REAL(0.52)*Q + REAL(0.48)*P;

Pierre Kraemer's avatar
Pierre Kraemer committed
730
				this->m_attIndex[d] = indexDC;
731
				indexDC+=2;
732
				*positionDartBuf++ = PFP::toVec3f(P);
Pierre Kraemer's avatar
Pierre Kraemer committed
733
				*colorDartBuf++ = this->m_dartsColor;
734
				*positionDartBuf++ = PFP::toVec3f(PP);
Pierre Kraemer's avatar
Pierre Kraemer committed
735
				*colorDartBuf++ = this->m_dartsColor;
736
				*positionDartBuf++ = PFP::toVec3f(Q);
Pierre Kraemer's avatar
Pierre Kraemer committed
737
				*colorDartBuf++ = this->m_dartsColor;
738
				*positionDartBuf++ = PFP::toVec3f(QQ);
Pierre Kraemer's avatar
Pierre Kraemer committed
739
				*colorDartBuf++ = this->m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
740

741
				VEC3 f = P*0.5f + PP*0.5f;
Pierre Kraemer's avatar
Pierre Kraemer committed
742
				fv2[d] = f;
743
				f = P*0.9f + PP*0.1f;
Pierre Kraemer's avatar
Pierre Kraemer committed
744 745
				fv1[d] = f;

746 747 748 749 750
				dd = map.beta0(d);
				f = Q*0.5f + QQ*0.5f;
				fv2[dd] = f;
				f = Q*0.9f + QQ*0.1f;
				fv1[dd] = f;
Pierre Kraemer's avatar
Pierre Kraemer committed
751
				this->m_attIndex[dd] = indexDC;
752 753
				indexDC+=2;

Pierre Kraemer's avatar
Pierre Kraemer committed
754 755
				d = map.phi1(d);
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
756
			mf.template markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
757 758 759
		}
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
760
	this->m_vbo0->bind();
761
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
762

Pierre Kraemer's avatar
Pierre Kraemer committed
763
	this->m_vbo3->bind();
764
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
765

Pierre Kraemer's avatar
Pierre Kraemer committed
766 767
	this->m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
768
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
769

Pierre Kraemer's avatar
Pierre Kraemer committed
770 771
	this->m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*this->m_nbDarts*sizeof(Geom::Vec3f), 0, GL_STREAM_DRAW);
772
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
773

Pierre Kraemer's avatar
Pierre Kraemer committed
774 775
	Geom::Vec3f* positionF1 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer1);
	Geom::Vec3f* positionF2 = reinterpret_cast<Geom::Vec3f*>(PositionBuffer2);
Pierre Kraemer's avatar
Pierre Kraemer committed
776

Pierre Kraemer's avatar
Pierre Kraemer committed
777
	this->m_nbRel2 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
778 779 780 781
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		Dart e = map.beta2(d);
782
//		if (d < e )
Pierre Kraemer's avatar
Pierre Kraemer committed
783
		if ( (withBoundary || !map.template isBoundaryMarked<2>(e)) && (d < e ))
Pierre Kraemer's avatar
Pierre Kraemer committed
784
		{
785 786
			*positionF2++ = PFP::toVec3f(fv2[d]);
			*positionF2++ = PFP::toVec3f(fv2[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
787
			this->m_nbRel2++;
Pierre Kraemer's avatar
Pierre Kraemer committed
788 789 790
		}

		e = map.beta1(d);
791 792
		*positionF1++ = PFP::toVec3f(fv1[d]);
		*positionF1++ = PFP::toVec3f(fv1[e]);
Pierre Kraemer's avatar
Pierre Kraemer committed
793
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
794
	this->m_nbRel1 = vecDarts.size()/2;
Pierre Kraemer's avatar
Pierre Kraemer committed
795

Pierre Kraemer's avatar
Pierre Kraemer committed
796
	this->m_vbo1->bind();
797 798
	glUnmapBuffer(GL_ARRAY_BUFFER);

Pierre Kraemer's avatar
Pierre Kraemer committed
799
	this->m_vbo2->bind();
800
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
801 802
}

Sylvain Thery's avatar
Sylvain Thery committed
803
template<typename PFP>
804
void TopoRender<PFP>::setDartsIdColor(MAP& map, bool withBoundary)
Sylvain Thery's avatar
Sylvain Thery committed
805 806
{
	m_vbo3->bind();
Pierre Kraemer's avatar
Pierre Kraemer committed
807 808
	float* colorBuffer = reinterpret_cast<float*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb = 0;
Sylvain Thery's avatar
Sylvain Thery committed
809

810
	m_attIndex = map.template getAttribute<unsigned int, DART, MAP>("dart_index2");
Sylvain Thery's avatar
Sylvain Thery committed
811 812
	if (!m_attIndex.isValid())
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
813
		CGoGNerr << "Error attribute_dartIndex does not exist during TopoRender<PFP>::picking" << CGoGNendl;
Sylvain Thery's avatar
Sylvain Thery committed
814 815 816
		return;
	}

Sylvain Thery's avatar
Sylvain Thery committed
817 818
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
819
		if (withBoundary || !map.template isBoundaryMarked<2>(d))
820
		{
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840

			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 good parameter ?)" << CGoGNendl;
				CGoGNerr << "NB = " << nb << "   NBDARTs = "<< m_nbDarts<<CGoGNendl;
				break;
			}
Sylvain Thery's avatar
Sylvain Thery committed
841 842 843 844 845 846
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
847
Dart TopoRender<PFP>::picking(MAP& map,int x, int y, bool withBoundary)
Sylvain Thery's avatar
Sylvain Thery committed
848 849
{
	pushColors();
Pierre Kraemer's avatar
Pierre Kraemer committed
850
	setDartsIdColor(map,withBoundary);
Sylvain Thery's avatar
Sylvain Thery committed
851 852 853 854 855
	Dart d = pickColor(x,y);
	popColors();
	return d;
}

856
template<typename PFP>
857
Dart TopoRender<PFP>::coneSelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float angle)
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
{
	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>
887
Dart TopoRender<PFP>::raySelection(MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float dmax)
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
{
	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;
}

914
} // namespace GL2
915

916
} // namespace Algo
Pierre Kraemer's avatar
Pierre Kraemer committed
917

918
} // namespace Render
Pierre Kraemer's avatar
Pierre Kraemer committed
919

920
} // namespace CGoGN