topoRender.hpp 11.4 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 34

namespace CGoGN
{
35

Pierre Kraemer's avatar
Pierre Kraemer committed
36 37
namespace Algo
{
38

Pierre Kraemer's avatar
Pierre Kraemer committed
39 40
namespace Render
{
41

Sylvain Thery's avatar
Sylvain Thery committed
42
namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45
{

template<typename PFP>
46
void TopoRender::updateData(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, const FunctorSelect& good)
Pierre Kraemer's avatar
Pierre Kraemer committed
47
{
48 49 50
	Map2* ptrMap2 = dynamic_cast<Map2*>(&map);
	if (ptrMap2 != NULL)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
51 52
		updateDataMap<PFP>(map, positions, ke, kf, good);
		return;
53 54 55 56
	}
	GMap2* ptrGMap2 = dynamic_cast<GMap2*>(&map);
	if (ptrGMap2 != NULL)
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
57 58
		updateDataGMap<PFP>(map, positions, ke, kf, good);
		return;
59 60 61
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
62
template<typename PFP>
63
void TopoRender::updateDataMap(typename PFP::MAP& mapx, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, const FunctorSelect& good)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
{
65 66
	Map2& map = reinterpret_cast<Map2&>(mapx);

Pierre Kraemer's avatar
Pierre Kraemer committed
67 68 69 70
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

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

Sylvain Thery's avatar
Sylvain Thery committed
73 74
	m_attIndex = map.template getAttribute<unsigned int, DART>("dart_index");

Sylvain Thery's avatar
Sylvain Thery committed
75
	if (!m_attIndex.isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
76
		m_attIndex  = map.template addAttribute<unsigned int, DART>("dart_index");
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80 81 82 83 84 85

	for(Dart d = map.begin(); d!= map.end(); map.next(d))
	{
		if (good(d))
			vecDarts.push_back(d);
	}
	m_nbDarts = vecDarts.size();

	// debut phi1
86
	DartAutoAttribute<VEC3> fv1(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
87
	// fin phi1
88
	DartAutoAttribute<VEC3> fv11(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
89
	// phi2
90
	DartAutoAttribute<VEC3> fv2(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
91

92 93 94
	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
95 96
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

97 98 99
	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);
Pierre Kraemer's avatar
Pierre Kraemer committed
100 101
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);

102 103
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
104

105 106
	unsigned int indexDC=0;

Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109 110 111 112 113
	DartMarker mf(map);
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		if (!mf.isMarked(d))
		{

114
			vecPos.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
115 116 117 118 119 120 121 122
			// store the face & center
			VEC3 center(0.0f,0.0f,0.0f);
			Dart dd = d;
			do
			{
				const VEC3& P = positions[d];
				vecPos.push_back(P);
				center += P;
123 124
				Dart e = map.phi1(d);
				d = e;
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			} while (d != dd);
			center /= REAL(vecPos.size());

			//shrink the face
			unsigned int nb = vecPos.size();
			float k = 1.0f - kf;
			for (unsigned int i = 0; i < nb; ++i)
			{
				vecPos[i] = center*k + vecPos[i]*kf;
			}
			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;

143 144
				m_attIndex[d] = indexDC;
				indexDC+=2;
Pierre Kraemer's avatar
Pierre Kraemer committed
145 146
				*positionDartBuf++ = P;
				*positionDartBuf++ = Q;
147
				*colorDartBuf++ = m_dartsColor;
148
				*colorDartBuf++ = m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
149 150 151 152 153 154 155 156
				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;
				d = map.phi1(d);
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
157
			mf.markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
158 159 160
		}
	}

161 162
	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
163

164 165
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
166

167 168 169
	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
170

171 172 173
	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
174 175 176 177 178 179 180 181 182 183

	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;

		Dart e = map.phi2(d);
184

Sylvain Thery's avatar
Sylvain Thery committed
185 186
//		if (good(e) && (e.index > d.index))
		if (good(e) && (d < e ))
Pierre Kraemer's avatar
Pierre Kraemer committed
187 188 189 190 191 192 193 194 195 196
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}

		e = map.phi1(d);
		*positionF1++ = fv1[d];
		*positionF1++ = fv11[e];
	}
Sylvain Thery's avatar
Sylvain Thery committed
197
	m_nbRel1 = vecDarts.size();
Pierre Kraemer's avatar
Pierre Kraemer committed
198

199 200 201 202 203
	m_vbo1->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);

	m_vbo2->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
204 205 206
}

template<typename PFP>
207
void TopoRender::updateDataGMap(typename PFP::MAP& mapx, const VertexAttribute<typename PFP::VEC3>& positions, float ke, float kf, const FunctorSelect& good)
Pierre Kraemer's avatar
Pierre Kraemer committed
208
{
Sylvain Thery's avatar
Sylvain Thery committed
209
	GMap2& map = dynamic_cast<GMap2&>(mapx);
210

Pierre Kraemer's avatar
Pierre Kraemer committed
211 212 213 214
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

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

Sylvain Thery's avatar
Sylvain Thery committed
217
	if (m_attIndex.map() != &map)
Pierre Kraemer's avatar
Pierre Kraemer committed
218
		m_attIndex  = map.template getAttribute<unsigned int, DART>("dart_index");
Sylvain Thery's avatar
Sylvain Thery committed
219

Sylvain Thery's avatar
Sylvain Thery committed
220
	if (!m_attIndex.isValid())
Pierre Kraemer's avatar
Pierre Kraemer committed
221
		m_attIndex  = map.template addAttribute<unsigned int, DART>("dart_index");
Sylvain Thery's avatar
Sylvain Thery committed
222

Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226 227 228 229 230

	for(Dart d = map.begin(); d!= map.end(); map.next(d))
	{
		if (good(d))
			vecDarts.push_back(d);
	}
	m_nbDarts = vecDarts.size();

231
	// debut phi1
232
	DartAutoAttribute<VEC3> fv1(map);
233
	// fin phi1
234
	DartAutoAttribute<VEC3> fv11(map);
235
	// phi2
236
	DartAutoAttribute<VEC3> fv2(map);
237 238 239 240

	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
241 242
	VEC3* colorDartBuf = reinterpret_cast<VEC3*>(ColorDartsBuffer);

243 244 245
	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
246
	VEC3* positionDartBuf = reinterpret_cast<VEC3*>(PositionDartsBuffer);
247

248 249
	std::vector<VEC3> vecPos;
	vecPos.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
250

251 252
	unsigned int indexDC=0;

Pierre Kraemer's avatar
Pierre Kraemer committed
253 254 255 256
	DartMarker mf(map);
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
257

Pierre Kraemer's avatar
Pierre Kraemer committed
258 259
		if (!mf.isMarked(d))
		{
260
			vecPos.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
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
			// store the face & center
			VEC3 center(0.0f,0.0f,0.0f);
			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 k = 1.0f - kf;
			for (unsigned int i = 0; i < nb; ++i)
			{
				vecPos[i] = center*k + vecPos[i]*kf;
			}
			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;

290 291
				m_attIndex[d] = indexDC;
				indexDC+=2;
Pierre Kraemer's avatar
Pierre Kraemer committed
292
				*positionDartBuf++ = P;
293
				*colorDartBuf++ = m_dartsColor;
294
				*positionDartBuf++ = PP;
295
				*colorDartBuf++ = m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
296
				*positionDartBuf++ = Q;
297
				*colorDartBuf++ = m_dartsColor;
298
				*positionDartBuf++ = QQ;
299
				*colorDartBuf++ = m_dartsColor;
Pierre Kraemer's avatar
Pierre Kraemer committed
300

301
				VEC3 f = P*0.5f + PP*0.5f;
Pierre Kraemer's avatar
Pierre Kraemer committed
302
				fv2[d] = f;
303
				f = P*0.9f + PP*0.1f;
Pierre Kraemer's avatar
Pierre Kraemer committed
304 305
				fv1[d] = f;

306 307 308 309 310
				dd = map.beta0(d);
				f = Q*0.5f + QQ*0.5f;
				fv2[dd] = f;
				f = Q*0.9f + QQ*0.1f;
				fv1[dd] = f;
311 312 313
				m_attIndex[dd] = indexDC;
				indexDC+=2;

314

Pierre Kraemer's avatar
Pierre Kraemer committed
315 316
				d = map.phi1(d);
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
317
			mf.markOrbit<FACE>(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
318 319 320
		}
	}

321 322
	m_vbo0->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
323

324 325
	m_vbo3->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);
Pierre Kraemer's avatar
Pierre Kraemer committed
326

327 328 329
	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
330

331 332 333
	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
334 335 336 337

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

338
	m_nbRel2 = 0;
Pierre Kraemer's avatar
Pierre Kraemer committed
339 340 341 342
	for(std::vector<Dart>::iterator id = vecDarts.begin(); id!= vecDarts.end(); id++)
	{
		Dart d = *id;
		Dart e = map.beta2(d);
Sylvain Thery's avatar
Sylvain Thery committed
343
		if (good(e) && (d < e ))
Pierre Kraemer's avatar
Pierre Kraemer committed
344 345 346 347 348 349 350 351 352 353
		{
			*positionF2++ = fv2[d];
			*positionF2++ = fv2[e];
			m_nbRel2++;
		}

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

356 357 358 359 360
	m_vbo1->bind();
	glUnmapBuffer(GL_ARRAY_BUFFER);

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

Sylvain Thery's avatar
Sylvain Thery committed
363 364 365 366
template<typename PFP>
void TopoRender::setDartsIdColor(typename PFP::MAP& map, const FunctorSelect& good)
{
	m_vbo3->bind();
Pierre Kraemer's avatar
Pierre Kraemer committed
367 368
	float* colorBuffer = reinterpret_cast<float*>(glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE));
	unsigned int nb = 0;
Sylvain Thery's avatar
Sylvain Thery committed
369

Pierre Kraemer's avatar
Pierre Kraemer committed
370
	m_attIndex = map.template getAttribute<unsigned int, DART>("dart_index");
Sylvain Thery's avatar
Sylvain Thery committed
371 372 373 374 375 376
	if (!m_attIndex.isValid())
	{
		CGoGNerr << "Error attribute_dartIndex does not exist during TopoRender::picking" << CGoGNendl;
		return;
	}

Sylvain Thery's avatar
Sylvain Thery committed
377 378
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
379
		if (good(d))
Sylvain Thery's avatar
Sylvain Thery committed
380
		{
381
			if (nb < m_nbDarts)
Sylvain Thery's avatar
Sylvain Thery committed
382 383
			{
				float r,g,b;
384
				dartToCol(d, r,g,b);
Sylvain Thery's avatar
Sylvain Thery committed
385 386 387 388 389 390 391 392 393
				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++;
			}
394 395 396
			else
			{
				CGoGNerr << "Error buffer too small for color picking (change the good parameter ?)" << CGoGNendl;
397
				break;
398
			}
Sylvain Thery's avatar
Sylvain Thery committed
399 400 401 402 403 404
		}
	}
	glUnmapBuffer(GL_ARRAY_BUFFER);
}

template<typename PFP>
405
Dart TopoRender::picking(typename PFP::MAP& map,int x, int y, const FunctorSelect& good)
Sylvain Thery's avatar
Sylvain Thery committed
406 407 408 409 410 411 412 413 414
{
	pushColors();
	setDartsIdColor<PFP>(map,good);
	Dart d = pickColor(x,y);
	popColors();
	return d;

}

Sylvain Thery's avatar
Sylvain Thery committed
415
}//end namespace GL2
Pierre Kraemer's avatar
Pierre Kraemer committed
416 417 418 419 420 421

}//end namespace Algo

}//end namespace Render

}//end namespace CGoGN