File: pick.h

package info (click to toggle)
meshlab 1.3.2%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 21,096 kB
  • ctags: 33,630
  • sloc: cpp: 224,813; ansic: 8,170; xml: 119; makefile: 80
file content (439 lines) | stat: -rw-r--r-- 12,975 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
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
212
213
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
435
436
437
438
439
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004                                                \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *   
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program 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 General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/
/****************************************************************************
  History
$Log: not supported by cvs2svn $
Revision 1.10  2006/12/07 00:39:22  cignoni
Added a class prototype for avoiding the inclusion of tetra.h

Revision 1.9  2006/12/04 09:27:13  cignoni
Removed useless include <tetra.h>

****************************************************************************/
#ifndef __PICK______H
#define __PICK______H

#include <vector>
#include <algorithm>
namespace vcg{

template <class MESH_TYPE>
class GLPickTri
{
	typedef typename MESH_TYPE::FaceIterator FaceIterator;
  typedef typename MESH_TYPE::VertexIterator VertexIterator;
  typedef typename MESH_TYPE::FacePointer  FacePointer;
  typedef typename MESH_TYPE::VertexPointer  VertexPointer;
  typedef typename MESH_TYPE::VertexType  VertexType;

public:

	static bool PickNearestFace(int x, int y, MESH_TYPE &m, FacePointer &fi,int width=4, int height=4)
	{
		std::vector<FacePointer> result;
		int val=PickFace(x,y,m,result,width,height);
		if(val!=0)
		{
			fi=result[0];
			return true;
		}
		fi=NULL;
		return false; 
	}
  static int PickVert(int x, int y, MESH_TYPE &m, std::vector<VertexPointer> &result, int width=4, int height=4,bool sorted=true)
  {
      result.clear();
      if(width==0 ||height==0) return 0;
      long hits;
      int sz=m.vert.size()*5;
      GLuint *selectBuf =new GLuint[sz];
      glSelectBuffer(sz, selectBuf);
      glRenderMode(GL_SELECT);
      glInitNames();

      /* Because LoadName() won't work with no names on the stack */
      glPushName(-1);
      double mp[16];

      GLint viewport[4];
      glGetIntegerv(GL_VIEWPORT,viewport);
      glMatrixMode(GL_PROJECTION);
      glGetDoublev(GL_PROJECTION_MATRIX ,mp);
      glPushMatrix();
      glLoadIdentity();
      gluPickMatrix(x, y, width, height, viewport);
      glMultMatrixd(mp);

      glMatrixMode(GL_MODELVIEW);
      glPushMatrix();
      int vcnt=0;
      VertexIterator vi;
      for(vi=m.vert.begin();vi!=m.vert.end();++vi)
      {
        if(!(*vi).IsD())
        {
          glLoadName(vcnt);
          glBegin(GL_POINTS);
            glVertex( (*vi).P() );
          glEnd();
        }
        vcnt++; // the counter should advance even for deleted faces!
      }

      glPopMatrix();
      glMatrixMode(GL_PROJECTION);
      glPopMatrix();
      glMatrixMode(GL_MODELVIEW);
      hits = glRenderMode(GL_RENDER);
      std::vector< std::pair<double,unsigned int> > H;
      for(long ii=0;ii<hits;ii++){
        H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
      }
      if(sorted)
        std::sort(H.begin(),H.end());
      result.resize(H.size());
      for(long ii=0;ii<hits;ii++){
        VertexIterator vi=m.vert.begin();
        advance(vi ,H[ii].second);
        result[ii]=&*vi;
      }

      delete [] selectBuf;
      return result.size();
  }
  
	static int PickFace(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &result, int width=4, int height=4,bool sorted=true)
	{
		result.clear();
    if(width==0 ||height==0) return 0; 
		long hits;	
		int sz=m.face.size()*5;
		GLuint *selectBuf =new GLuint[sz];
		//  static unsigned int selectBuf[16384];
		glSelectBuffer(sz, selectBuf);
		glRenderMode(GL_SELECT);
		glInitNames();

		/* Because LoadName() won't work with no names on the stack */
		glPushName(-1);
		double mp[16];

		GLint viewport[4];
		glGetIntegerv(GL_VIEWPORT,viewport);
		glMatrixMode(GL_PROJECTION);
		glGetDoublev(GL_PROJECTION_MATRIX ,mp);
		glPushMatrix();
		glLoadIdentity();
		//gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
		gluPickMatrix(x, y, width, height, viewport);
		glMultMatrixd(mp);

		glMatrixMode(GL_MODELVIEW);
		glPushMatrix();
		int fcnt=0; 
		FaceIterator fi;
		for(fi=m.face.begin();fi!=m.face.end();++fi)
		{
			if(!(*fi).IsD())
			{
				glLoadName(fcnt);
				glBegin(GL_TRIANGLES);
					glVertex( (*fi).V(0)->P() );
					glVertex( (*fi).V(1)->P() );
					glVertex( (*fi).V(2)->P() );
				glEnd();
			}
      fcnt++; // the counter should advance even for deleted faces!      
		}

		glPopMatrix();
		glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);
		hits = glRenderMode(GL_RENDER);
		//xstring buf;
		//if (hits <= 0)     return 0;
		std::vector< std::pair<double,unsigned int> > H;
		for(long ii=0;ii<hits;ii++){
			//TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
			H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
		}
		if(sorted) 
      std::sort(H.begin(),H.end());
		//  if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
		result.resize(H.size());
		for(long ii=0;ii<hits;ii++){
			FaceIterator fi=m.face.begin();
			advance(fi ,H[ii].second);
			result[ii]=&*fi;
		}

		delete [] selectBuf;
		return result.size();
	}

 // Same of above but it also assumes that you want only visible faces.
 // Visibility is computed according to the current depth buffer. 
	static int PickFaceVisible(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &resultZ, int width=4, int height=4, bool sorted=true)
	{
		// First step 
		
		double mm[16];
		double mp[16];
		GLint vp[4];
		glGetIntegerv(GL_VIEWPORT,vp);
		glGetDoublev(GL_MODELVIEW_MATRIX ,mm);
		glGetDoublev(GL_PROJECTION_MATRIX ,mp);
		int screenW = 		vp[2]-vp[0];
		int screenH = 		vp[3]-vp[1];
		
		GLfloat *buffer = new GLfloat[screenW*screenH];
		glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_FLOAT,buffer);
		
		std::vector<FacePointer> result;
		PickFace(x,y,m,result,width,height,sorted);
		float LocalEpsilon = 0.001f;
    for(size_t i =0;i<result.size();++i)
		{	
			typename VertexType::CoordType v=Barycenter(*(result[i]));
			GLdouble tx,ty,tz;
			gluProject(v.X(),v.Y(),v.Z(), mm,mp,vp, &tx,&ty,&tz);
			if(tx >=0 && tx<screenW && ty >=0 && ty<screenH)
			{
					float bufZ = buffer[int(tx)+int(ty)*screenW]; 
					//qDebug("face %i txyz (%f %f %f)  bufz %f",i,tx,ty,tz,bufZ);
					if(bufZ + LocalEpsilon >= tz)
								resultZ.push_back(result[i]);
			}
		}
		
		delete [] buffer;
		return resultZ.size();
	}

};


//////////////////////////////////////////////////////////////////////////
template <class TETRA_MESH_TYPE>
class GLPickTetra
{
	typedef typename TETRA_MESH_TYPE::TetraIterator TetraIterator;
	typedef typename TETRA_MESH_TYPE::TetraPointer  TetraPointer;
	typedef typename TETRA_MESH_TYPE::VertexType  VertexType;

public:
static bool PickNearestTetra(int x, int y,TETRA_MESH_TYPE &m, TetraIterator &ti,int width=4, int height=4)
{
 std::vector<TetraPointer> result;
 int val=PickTetra(x,y,m,result,width,height);
 if(val!=0)
 {
  ti=result[0];
	return true;
 }
 ti=0;
 return false; 
}


// class prototype needed for avoid the inclusion of tetra.h
class Tetra;

static int PickTetra(int x, int y, TETRA_MESH_TYPE &m, std::vector<TetraPointer> &result, int width=4, int height=4)
{
	result.clear();
	long hits;	
  int sz=m.tetra.size()*5;
	unsigned int *selectBuf =new unsigned int[sz];
	//  static unsigned int selectBuf[16384];
  glSelectBuffer(sz, selectBuf);
  glRenderMode(GL_SELECT);
  glInitNames();

  /* Because LoadName() won't work with no names on the stack */
  glPushName(-1);
	double mp[16];
	
  int viewport[4];
  glGetIntegerv(GL_VIEWPORT,viewport);
	glMatrixMode(GL_PROJECTION);
	glGetDoublev(GL_PROJECTION_MATRIX ,mp);
	glPushMatrix();
  glLoadIdentity();
  //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
  gluPickMatrix(x, y, width, height, viewport);
	glMultMatrixd(mp);

	glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  int tetracnt=0; 
	TetraIterator ti;
	for(ti=m.tetra.begin();ti!=m.tetra.end();++ti)
	{
		if(!(*ti).IsD())
		{
			glLoadName(tetracnt);
			glBegin(GL_TRIANGLES);
			for (int face=0;face<4;face++)
			{
				//glLoadName(tetracnt);
				VertexType *v0=ti->V(Tetra::VofF(face,0));
				VertexType *v1=ti->V(Tetra::VofF(face,1));
				VertexType *v2=ti->V(Tetra::VofF(face,2));
				glVertex(v0->P());
				glVertex(v1->P());
				glVertex(v2->P());
				
			}
			glEnd();
			tetracnt++;
		}
		
	}

  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
	glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  hits = glRenderMode(GL_RENDER);
	//xstring buf;
	//if (hits <= 0)     return 0;
	std::vector< std::pair<double,unsigned int> > H;
	int ii;
	for(ii=0;ii<hits;ii++){
		//TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
		H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
	}
	std::sort(H.begin(),H.end());
//  if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
  result.resize(H.size());
	for(ii=0;ii<hits;ii++){
		TetraIterator ti=m.tetra.begin();
		advance(ti ,H[ii].second);
		result[ii]=&*ti;
	}
	
	delete [] selectBuf;
  return result.size();
}

static bool PickNearestTetraFace(int x, int y,TETRA_MESH_TYPE &m, TetraIterator &ti,int &face,int width=4, int height=4)
{
 std::vector<std::pair<TetraPointer,int> > result;
 int val=PickTetraFace(x,y,m,result,width,height);
 if(val!=0)
 {
  ti=result[0].first;
  face=result[0].second;
  return true;
 }
 ti=0;
 face=-1;
 return false; 
}

static int PickTetraFace(int x, int y, TETRA_MESH_TYPE &m, std::vector<std::pair<TetraPointer,int> > &result, int width=4, int height=4)
{
	result.clear();
	long hits;	
  int sz=(m.tetra.size()*4)*5;
	unsigned int *selectBuf =new unsigned int[sz];
	//  static unsigned int selectBuf[16384];
  glSelectBuffer(sz, selectBuf);
  glRenderMode(GL_SELECT);
  glInitNames();

  /* Because LoadName() won't work with no names on the stack */
  glPushName(-1);
	double mp[16];
	
  int viewport[4];
  glGetIntegerv(GL_VIEWPORT,viewport);
	glMatrixMode(GL_PROJECTION);
	glGetDoublev(GL_PROJECTION_MATRIX ,mp);
	glPushMatrix();
  glLoadIdentity();
  //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
  gluPickMatrix(x, y, width, height, viewport);
	glMultMatrixd(mp);
	glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  int tetracnt=0; 
	TetraIterator ti;
	VertexType *v0;
	VertexType *v1;
	VertexType *v2;
	int face;
	for(ti=m.tetra.begin();ti!=m.tetra.end();++ti)
	{
		if(!(*ti).IsD())
		{
			for (face=0;face<4;face++){
				v0=ti->V(Tetra::VofF(face,0));
				v1=ti->V(Tetra::VofF(face,1));
				v2=ti->V(Tetra::VofF(face,2));
				glLoadName(tetracnt);
				glBegin(GL_TRIANGLES);
					glVertex(v0->P());
					glVertex(v1->P());
					glVertex(v2->P());
				glEnd();
				tetracnt++;
			}
		}
	}

  glPopMatrix();
  glMatrixMode(GL_PROJECTION);
	glPopMatrix();
  glMatrixMode(GL_MODELVIEW);
  hits = glRenderMode(GL_RENDER);
	//xstring buf;
	//if (hits <= 0)     return 0;
	std::vector< std::pair<double,unsigned int> > H;
	int ii;
	for(ii=0;ii<hits;ii++){
		//TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
		H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
	}
	std::sort(H.begin(),H.end());
//  if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
  result.resize(H.size());
	for(ii=0;ii<hits;ii++){
		TetraIterator ti=m.tetra.begin();
		int index=H[ii].second;
		advance(ti ,(int)(index/4));
		result[ii]=std::pair<TetraPointer,int>(&*ti,index%4);
	}
	
	delete [] selectBuf;
  return result.size();
}

};


}

#endif