topoRender.hpp 15.5 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
{

52 53 54 55 56

template<typename PFP>
void TopoRender::updateDataBoundary(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf,float ns)
{
	m_normalShift = ns;
57 58 59
	SelectorDartBoundary<typename PFP::MAP> sdb(map);
	DartContainerBrowserSelector browser(map,sdb);
	browser.enable();
60
	updateData<PFP>(map,positions, ke, kf,true);
61
	browser.disable();
62 63 64 65
	m_normalShift = 0.0f;
}


Pierre Kraemer's avatar
Pierre Kraemer committed
66
template<typename PFP>
67
void TopoRender::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, bool withBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
68
{
69 70 71
	Map2* ptrMap2 = dynamic_cast<Map2*>(&map);
	if (ptrMap2 != NULL)
	{
72
		updateDataMap<PFP>(map, positions, ke, kf, withBoundary);
Pierre Kraemer's avatar
Pierre Kraemer committed
73
		return;
74 75 76 77
	}
	GMap2* ptrGMap2 = dynamic_cast<GMap2*>(&map);
	if (ptrGMap2 != NULL)
	{
78
		updateDataGMap<PFP>(map, positions, ke, kf, withBoundary);
Pierre Kraemer's avatar
Pierre Kraemer committed
79
		return;
80 81 82
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
83
template<typename PFP>
84
void TopoRender::updateDataMap(typename PFP::MAP& mapx, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, bool withBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
85
{
untereiner's avatar
untereiner committed
86
	//Map2& map = reinterpret_cast<Map2&>(mapx);
87

Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90 91
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

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

94
	m_attIndex = mapx.template getAttribute<unsigned int, DART>("dart_index2");
Sylvain Thery's avatar
Sylvain Thery committed
95

Sylvain Thery's avatar
Sylvain Thery committed
96
	if (!m_attIndex.isValid())
97
		m_attIndex  = mapx.template addAttribute<unsigned int, DART>("dart_index2");
Pierre Kraemer's avatar
Pierre Kraemer committed
98

untereiner's avatar
untereiner committed
99
	for(Dart d = mapx.begin(); d!= mapx.end(); mapx.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	{
untereiner's avatar
untereiner committed
101
		if (withBoundary || !mapx.isBoundaryMarked2(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
102
			vecDarts.push_back(d);
103

Pierre Kraemer's avatar
Pierre Kraemer committed
104 105 106 107
	}
	m_nbDarts = vecDarts.size();

	// debut phi1
untereiner's avatar
untereiner committed
108
	DartAutoAttribute<VEC3> fv1(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
109
	// fin phi1
untereiner's avatar
untereiner committed
110
	DartAutoAttribute<VEC3> fv11(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
111
	// phi2
untereiner's avatar
untereiner committed
112
	DartAutoAttribute<VEC3> fv2(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
113

114 115 116
	m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

119 120 121 122 123 124 125 126 127
//	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);

	if (m_bufferDartPosition!=NULL)
		delete m_bufferDartPosition;
	m_bufferDartPosition = new Geom::Vec3f[2*m_nbDarts];
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(m_bufferDartPosition);
Pierre Kraemer's avatar
Pierre Kraemer committed
128

129 130
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
131

132 133
	unsigned int indexDC=0;

untereiner's avatar
untereiner committed
134
	DartMarker mf(mapx);
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136 137 138 139
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		if (!mf.isMarked(d))
		{
140
			vecPos.clear();
untereiner's avatar
untereiner committed
141
			if (!mapx.isBoundaryMarked2(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
142
			{
untereiner's avatar
untereiner committed
143 144
				//VEC3 center = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx,d,positions);
				VEC3 center = Algo::Surface::Geometry::faceCentroid<PFP>(mapx,d,positions);
145 146 147 148 149
				float k = 1.0f - kf;
				Dart dd = d;
				do
				{
					vecPos.push_back(center*k + positions[dd]*kf);
untereiner's avatar
untereiner committed
150
					dd = mapx.phi1(dd);
151
				} while (dd != d);
152

153

154
				if (m_normalShift > 0.0f)
155
				{
156 157 158 159 160
					VEC3 normal = Algo::Surface::Geometry::newellNormal<PFP>(mapx,d,positions);
					for (typename std::vector<VEC3>::iterator pit = vecPos.begin(); pit != vecPos.end(); ++pit)
					{
						*pit -= normal*m_normalShift;
					}
161 162
				}

163 164
				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
165

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				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;

					m_attIndex[d] = indexDC;
					indexDC+=2;
					*positionDartBuf++ = P;
					*positionDartBuf++ = Q;
					*colorDartBuf++ = m_dartsColor;
					*colorDartBuf++ = m_dartsColor;
					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
185
					d = mapx.phi1(d);
186 187 188 189
				}
				mf.markOrbit<FACE>(d);
			}
			else if (withBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
190
			{
191 192 193 194 195 196 197 198 199

				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();
untereiner's avatar
untereiner committed
200
					VEC3 P = positions[mapx.phi1(ee)] + v* m_boundShift;
201
					vecPos.push_back(P);
untereiner's avatar
untereiner committed
202
					dd = mapx.phi1(dd);
203
					ee = mapx.phi2(dd);
untereiner's avatar
untereiner committed
204
					P = positions[mapx.phi1(ee)] + v* m_boundShift;
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
					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;

					m_attIndex[d] = indexDC;
					indexDC+=2;
					*positionDartBuf++ = P;
					*positionDartBuf++ = Q;
					*colorDartBuf++ = m_dartsBoundaryColor;
					*colorDartBuf++ = m_dartsBoundaryColor;
					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
228
					d = mapx.phi1(d);
229 230
				}
				mf.markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
231 232 233 234
			}
		}
	}

235
	m_vbo0->bind();
236 237
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(VEC3), m_bufferDartPosition, GL_STREAM_DRAW);
//	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
238

239 240
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
241

242 243 244
	m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
245

246 247 248
	m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
249 250 251 252 253 254 255 256 257

	VEC3* positionF1 = reinterpret_cast<VEC3*>(PositionBuffer1);
	VEC3* positionF2 = reinterpret_cast<VEC3*>(PositionBuffer2);

	m_nbRel2 =0;
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;

untereiner's avatar
untereiner committed
258
		Dart e = mapx.phi2(d);
259

Sylvain Thery's avatar
Sylvain Thery committed
260
//		if (good(e) && (e.index > d.index))
untereiner's avatar
untereiner committed
261
		if ( (withBoundary || !mapx.isBoundaryMarked2(e)) && (e.index > d.index))
Pierre Kraemer's avatar
Pierre Kraemer committed
262 263 264 265 266 267
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}

untereiner's avatar
untereiner committed
268
		e = mapx.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
269 270 271
		*positionF1++ = fv1[d];
		*positionF1++ = fv11[e];
	}
Sylvain Thery's avatar
Sylvain Thery committed
272
	m_nbRel1 = vecDarts.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
273

274 275 276 277 278
	m_vbo1->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);

	m_vbo2->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
279 280 281
}

template<typename PFP>
282
void TopoRender::updateDataGMap(typename PFP::MAP& mapx, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, bool withBoundary)
Pierre Kraemer's avatar
Pierre Kraemer committed
283
{
Sylvain Thery's avatar
Sylvain Thery committed
284
	GMap2& map = dynamic_cast<GMap2&>(mapx);
285

Pierre Kraemer's avatar
Pierre Kraemer committed
286 287 288 289
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

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

Sylvain Thery's avatar
Sylvain Thery committed
292
	if (m_attIndex.map() != &map)
293
		m_attIndex  = map.template getAttribute<unsigned int, DART>("dart_index2");
Sylvain Thery's avatar
Sylvain Thery committed
294

Sylvain Thery's avatar
Sylvain Thery committed
295
	if (!m_attIndex.isValid())
296
		m_attIndex  = map.template addAttribute<unsigned int, DART>("dart_index2");
Sylvain Thery's avatar
Sylvain Thery committed
297

Pierre Kraemer's avatar
Pierre Kraemer committed
298 299 300

	for(Dart d = map.begin(); d!= map.end(); map.next(d))
	{
301
		if (withBoundary || !map.isBoundaryMarked2(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
302 303 304 305
			vecDarts.push_back(d);
	}
	m_nbDarts = vecDarts.size();

306
	// debut phi1
307
	DartAutoAttribute<VEC3> fv1(map);
308
	// fin phi1
309
	DartAutoAttribute<VEC3> fv11(map);
310
	// phi2
311
	DartAutoAttribute<VEC3> fv2(map);
312 313 314 315

	m_vbo3->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* ColorDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
316 317
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

318 319 320
	m_vbo0->bind();
	glBufferData(GL_ARRAY_BUFFER, 4*m_nbDarts*sizeof(VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionDartsBuffer = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
321
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);
322

323 324
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
325

326 327
	unsigned int indexDC=0;

Pierre Kraemer's avatar
Pierre Kraemer committed
328 329 330 331
	DartMarker mf(map);
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
332

Pierre Kraemer's avatar
Pierre Kraemer committed
333 334
		if (!mf.isMarked(d))
		{
335
			vecPos.clear();
336 337 338
			VEC3 center = Algo::Surface::Geometry::faceCentroidELW<PFP>(mapx,d,positions);
			
			float k = 1.0f - kf;
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341
			Dart dd = d;
			do
			{
342 343 344
				vecPos.push_back(center*k + positions[dd]*kf);
				dd = map.phi1(dd);
			} while (dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
345

346 347 348 349 350 351 352 353 354

			if (m_normalShift > 0.0f)
			{
				VEC3 normal = Algo::Surface::Geometry::newellNormal<PFP>(mapx,d,positions);
				for (typename std::vector<VEC3>::iterator pit = vecPos.begin(); pit != vecPos.end(); ++pit)
				{
					*pit -= normal*m_normalShift;
				}
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
355 356 357
			unsigned int nb = vecPos.size();
			vecPos.push_back(vecPos.front()); // copy the first for easy computation on next loop

358

Pierre Kraemer's avatar
Pierre Kraemer committed
359 360 361 362 363 364 365 366
			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;

367 368
				m_attIndex[d] = indexDC;
				indexDC+=2;
Pierre Kraemer's avatar
Pierre Kraemer committed
369
				*positionDartBuf++ = P;
370
				*colorDartBuf++ = m_dartsColor;
371
				*positionDartBuf++ = PP;
372
				*colorDartBuf++ = m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
373
				*positionDartBuf++ = Q;
374
				*colorDartBuf++ = m_dartsColor;
375
				*positionDartBuf++ = QQ;
376
				*colorDartBuf++ = m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
377

378
				VEC3 f = P*0.5f + PP*0.5f;
Pierre Kraemer's avatar
Pierre Kraemer committed
379
				fv2[d] = f;
380
				f = P*0.9f + PP*0.1f;
Pierre Kraemer's avatar
Pierre Kraemer committed
381 382
				fv1[d] = f;

383 384 385 386 387
				dd = map.beta0(d);
				f = Q*0.5f + QQ*0.5f;
				fv2[dd] = f;
				f = Q*0.9f + QQ*0.1f;
				fv1[dd] = f;
388 389 390
				m_attIndex[dd] = indexDC;
				indexDC+=2;

391

Pierre Kraemer's avatar
Pierre Kraemer committed
392 393
				d = map.phi1(d);
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
394
			mf.markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
395 396 397
		}
	}

398 399
	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
400

401 402
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
403

404 405 406
	m_vbo1->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer1 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
407

408 409 410
	m_vbo2->bind();
	glBufferData(GL_ARRAY_BUFFER, 2*m_nbDarts*sizeof(typename PFP::VEC3), 0, GL_STREAM_DRAW);
	GLvoid* PositionBuffer2 = glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
Pierre Kraemer's avatar
Pierre Kraemer committed
411 412 413 414

	VEC3* positionF1 = reinterpret_cast<VEC3*>(PositionBuffer1);
	VEC3* positionF2 = reinterpret_cast<VEC3*>(PositionBuffer2);

415
	m_nbRel2 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		Dart e = map.beta2(d);
420 421
//		if (d < e )
		if ( (withBoundary || !map.isBoundaryMarked2(e)) && (d < e ))
Pierre Kraemer's avatar
Pierre Kraemer committed
422 423 424 425 426 427 428 429 430 431
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}

		e = map.beta1(d);
		*positionF1++ = fv1[d];
		*positionF1++ = fv1[e];
	}
432
	m_nbRel1 = vecDarts.size()/2;
Pierre Kraemer's avatar
Pierre Kraemer committed
433

434 435 436 437 438
	m_vbo1->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);

	m_vbo2->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
439 440
}

Sylvain Thery's avatar
Sylvain Thery committed
441
template<typename PFP>
442
void TopoRender::setDartsIdColor(typename PFP::MAP& map, bool withBoundary)
Sylvain Thery's avatar
Sylvain Thery committed
443 444
{
	m_vbo3->bind();
Pierre Kraemer's avatar
Pierre Kraemer committed
445 446
	float* colorBuffer = reinterpret_cast<float*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb = 0;
Sylvain Thery's avatar
Sylvain Thery committed
447

448
	m_attIndex = map.template getAttribute<unsigned int, DART>("dart_index2");
Sylvain Thery's avatar
Sylvain Thery committed
449 450 451 452 453 454
	if (!m_attIndex.isValid())
	{
		CGoGNerr << "Error attribute_dartIndex does not exist during TopoRender::picking" << CGoGNendl;
		return;
	}

Sylvain Thery's avatar
Sylvain Thery committed
455 456
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
457
		if (withBoundary || !map.isBoundaryMarked2(d))
458
		{
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

			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
479 480 481 482 483 484
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
485
Dart TopoRender::picking(typename PFP::MAP& map,int x, int y, bool withBoundary)
Sylvain Thery's avatar
Sylvain Thery committed
486 487
{
	pushColors();
488
	setDartsIdColor<PFP>(map,withBoundary);
Sylvain Thery's avatar
Sylvain Thery committed
489 490 491 492 493 494
	Dart d = pickColor(x,y);
	popColors();
	return d;

}

495 496 497


template<typename PFP>
498
Dart TopoRender::coneSelection(typename PFP::MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float angle)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
{
	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>
528
Dart TopoRender::raySelection(typename PFP::MAP& map, const Geom::Vec3f& rayA, const Geom::Vec3f& rayAB, float dmax)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
{
	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;
}


Sylvain Thery's avatar
Sylvain Thery committed
556
}//end namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
557 558 559 560 561 562

}//end namespace Algo

}//end namespace Render

}//end namespace CGoGN