Debian Codegraph @

vcg/wrap/gl/pick.h

raw symbols SCIP
1 /****************************************************************************
2 * VCGLib o o *
3 * Visual and Computer Graphics Library o o *
4 * _ O _ *
5 * Copyright(C) 2004 \/)\/ *
6 * Visual Computing Lab /\/| *
7 * ISTI - Italian National Research Council | *
8 * \ *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License (http://www.gnu.org/licenses/gpl.txt) *
20 * for more details. *
21 * *
22 ****************************************************************************/
23 /****************************************************************************
24 History
25 $Log: pick.h,v $
26 Revision 1.11 2007/02/22 09:17:56 cignoni
27 Added cast for mac compiling
28
29 Revision 1.10 2006/12/07 00:39:22 cignoni
30 Added a class prototype for avoiding the inclusion of tetra.h
31
32 Revision 1.9 2006/12/04 09:27:13 cignoni
33 Removed useless include <tetra.h>
34
35 ****************************************************************************/
36 #ifndef __PICK______H
37 #define __PICK______H
38
39 #include <vector>
40 #include <algorithm>
41 namespace vcg{
42
43 template <class MESH_TYPE>
44 class GLPickTri
45 {
46 typedef typename MESH_TYPE::FaceIterator FaceIterator;
47 typedef typename MESH_TYPE::FacePointer FacePointer;
48 typedef typename MESH_TYPE::VertexType VertexType;
49
50 public:
51
52 static bool PickNearestFace(int x, int y, MESH_TYPE &m, FacePointer &fi,int width=4, int height=4)
53 {
54 std::vector<FacePointer> result;
55 int val=PickFace(x,y,m,result,width,height);
56 if(val!=0)
57 {
58 fi=result[0];
59 return true;
60 }
61 fi=NULL;
62 return false;
63 }
64
65 static int PickFace(int x, int y, MESH_TYPE &m, std::vector<FacePointer> &result, int width=4, int height=4,bool sorted=true)
66 {
67 result.clear();
68 if(width==0 ||height==0) return 0;
69 long hits;
70 int sz=m.face.size()*5;
71 GLuint *selectBuf =new GLuint[sz];
72 // static unsigned int selectBuf[16384];
73 glSelectBuffer(sz, selectBuf);
74 glRenderMode(GL_SELECT);
75 glInitNames();
76
77 /* Because LoadName() won't work with no names on the stack */
78 glPushName(-1);
79 double mp[16];
80
81 GLint viewport[4];
82 glGetIntegerv(GL_VIEWPORT,viewport);
83 glMatrixMode(GL_PROJECTION);
84 glGetDoublev(GL_PROJECTION_MATRIX ,mp);
85 glPushMatrix();
86 glLoadIdentity();
87 //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
88 gluPickMatrix(x, y, width, height, viewport);
89 glMultMatrixd(mp);
90
91 glMatrixMode(GL_MODELVIEW);
92 glPushMatrix();
93 int fcnt=0;
94 FaceIterator fi;
95 for(fi=m.face.begin();fi!=m.face.end();++fi)
96 {
97 if(!(*fi).IsD())
98 {
99 glLoadName(fcnt);
100 glBegin(GL_TRIANGLES);
101 glVertex( (*fi).V(0)->P() );
102 glVertex( (*fi).V(1)->P() );
103 glVertex( (*fi).V(2)->P() );
104 glEnd();
105 }
106 fcnt++; // the counter should advance even for deleted faces!
107 }
108
109 glPopMatrix();
110 glMatrixMode(GL_PROJECTION);
111 glPopMatrix();
112 glMatrixMode(GL_MODELVIEW);
113 hits = glRenderMode(GL_RENDER);
114 //xstring buf;
115 //if (hits <= 0) return 0;
116 std::vector< std::pair<double,unsigned int> > H;
117 for(long ii=0;ii<hits;ii++){
118 //TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
119 H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
120 }
121 if(sorted)
122 std::sort(H.begin(),H.end());
123 // if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
124 result.resize(H.size());
125 for(long ii=0;ii<hits;ii++){
126 FaceIterator fi=m.face.begin();
127 advance(fi ,H[ii].second);
128 result[ii]=&*fi;
129 }
130
131 delete [] selectBuf;
132 return result.size();
133 }
134
135
136
137
138 };
139
140
141 //////////////////////////////////////////////////////////////////////////
142 template <class TETRA_MESH_TYPE>
143 class GLPickTetra
144 {
145 typedef typename TETRA_MESH_TYPE::TetraIterator TetraIterator;
146 typedef typename TETRA_MESH_TYPE::TetraPointer TetraPointer;
147 typedef typename TETRA_MESH_TYPE::VertexType VertexType;
148
149 public:
150 static bool PickNearestTetra(int x, int y,TETRA_MESH_TYPE &m, TetraIterator &ti,int width=4, int height=4)
151 {
152 std::vector<TetraPointer> result;
153 int val=PickTetra(x,y,m,result,width,height);
154 if(val!=0)
155 {
156 ti=result[0];
157 return true;
158 }
159 ti=0;
160 return false;
161 }
162
163
164 // class prototype needed for avoid the inclusion of tetra.h
165 class Tetra;
166
167 static int PickTetra(int x, int y, TETRA_MESH_TYPE &m, std::vector<TetraPointer> &result, int width=4, int height=4)
168 {
169 result.clear();
170 long hits;
171 int sz=m.tetra.size()*5;
172 unsigned int *selectBuf =new unsigned int[sz];
173 // static unsigned int selectBuf[16384];
174 glSelectBuffer(sz, selectBuf);
175 glRenderMode(GL_SELECT);
176 glInitNames();
177
178 /* Because LoadName() won't work with no names on the stack */
179 glPushName(-1);
180 double mp[16];
181
182 int viewport[4];
183 glGetIntegerv(GL_VIEWPORT,viewport);
184 glMatrixMode(GL_PROJECTION);
185 glGetDoublev(GL_PROJECTION_MATRIX ,mp);
186 glPushMatrix();
187 glLoadIdentity();
188 //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
189 gluPickMatrix(x, y, width, height, viewport);
190 glMultMatrixd(mp);
191
192 glMatrixMode(GL_MODELVIEW);
193 glPushMatrix();
194 int tetracnt=0;
195 TetraIterator ti;
196 for(ti=m.tetra.begin();ti!=m.tetra.end();++ti)
197 {
198 if(!(*ti).IsD())
199 {
200 glLoadName(tetracnt);
201 glBegin(GL_TRIANGLES);
202 for (int face=0;face<4;face++)
203 {
204 //glLoadName(tetracnt);
205 VertexType *v0=ti->V(Tetra::VofF(face,0));
206 VertexType *v1=ti->V(Tetra::VofF(face,1));
207 VertexType *v2=ti->V(Tetra::VofF(face,2));
208 glVertex(v0->P());
209 glVertex(v1->P());
210 glVertex(v2->P());
211
212 }
213 glEnd();
214 tetracnt++;
215 }
216
217 }
218
219 glPopMatrix();
220 glMatrixMode(GL_PROJECTION);
221 glPopMatrix();
222 glMatrixMode(GL_MODELVIEW);
223 hits = glRenderMode(GL_RENDER);
224 //xstring buf;
225 //if (hits <= 0) return 0;
226 std::vector< std::pair<double,unsigned int> > H;
227 int ii;
228 for(ii=0;ii<hits;ii++){
229 //TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
230 H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
231 }
232 std::sort(H.begin(),H.end());
233 // if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
234 result.resize(H.size());
235 for(ii=0;ii<hits;ii++){
236 TetraIterator ti=m.tetra.begin();
237 advance(ti ,H[ii].second);
238 result[ii]=&*ti;
239 }
240
241 delete [] selectBuf;
242 return result.size();
243 }
244
245 static bool PickNearestTetraFace(int x, int y,TETRA_MESH_TYPE &m, TetraIterator &ti,int &face,int width=4, int height=4)
246 {
247 std::vector<std::pair<TetraPointer,int> > result;
248 int val=PickTetraFace(x,y,m,result,width,height);
249 if(val!=0)
250 {
251 ti=result[0].first;
252 face=result[0].second;
253 return true;
254 }
255 ti=0;
256 face=-1;
257 return false;
258 }
259
260 static int PickTetraFace(int x, int y, TETRA_MESH_TYPE &m, std::vector<std::pair<TetraPointer,int> > &result, int width=4, int height=4)
261 {
262 result.clear();
263 long hits;
264 int sz=(m.tetra.size()*4)*5;
265 unsigned int *selectBuf =new unsigned int[sz];
266 // static unsigned int selectBuf[16384];
267 glSelectBuffer(sz, selectBuf);
268 glRenderMode(GL_SELECT);
269 glInitNames();
270
271 /* Because LoadName() won't work with no names on the stack */
272 glPushName(-1);
273 double mp[16];
274
275 int viewport[4];
276 glGetIntegerv(GL_VIEWPORT,viewport);
277 glMatrixMode(GL_PROJECTION);
278 glGetDoublev(GL_PROJECTION_MATRIX ,mp);
279 glPushMatrix();
280 glLoadIdentity();
281 //gluPickMatrix(x, viewport[3]-y, 4, 4, viewport);
282 gluPickMatrix(x, y, width, height, viewport);
283 glMultMatrixd(mp);
284 glMatrixMode(GL_MODELVIEW);
285 glPushMatrix();
286 int tetracnt=0;
287 TetraIterator ti;
288 VertexType *v0;
289 VertexType *v1;
290 VertexType *v2;
291 int face;
292 for(ti=m.tetra.begin();ti!=m.tetra.end();++ti)
293 {
294 if(!(*ti).IsD())
295 {
296 for (face=0;face<4;face++){
297 v0=ti->V(Tetra::VofF(face,0));
298 v1=ti->V(Tetra::VofF(face,1));
299 v2=ti->V(Tetra::VofF(face,2));
300 glLoadName(tetracnt);
301 glBegin(GL_TRIANGLES);
302 glVertex(v0->P());
303 glVertex(v1->P());
304 glVertex(v2->P());
305 glEnd();
306 tetracnt++;
307 }
308 }
309 }
310
311 glPopMatrix();
312 glMatrixMode(GL_PROJECTION);
313 glPopMatrix();
314 glMatrixMode(GL_MODELVIEW);
315 hits = glRenderMode(GL_RENDER);
316 //xstring buf;
317 //if (hits <= 0) return 0;
318 std::vector< std::pair<double,unsigned int> > H;
319 int ii;
320 for(ii=0;ii<hits;ii++){
321 //TRACE("%ui %ui %ui %ui\n",selectBuf[ii*4],selectBuf[ii*4+1],selectBuf[ii*4+2],selectBuf[ii*4+3]);
322 H.push_back( std::pair<double,unsigned int>(selectBuf[ii*4+1]/4294967295.0,selectBuf[ii*4+3]));
323 }
324 std::sort(H.begin(),H.end());
325 // if(H.size()>0) TRACE("\n Closest is %i\n",H[0].second);
326 result.resize(H.size());
327 for(ii=0;ii<hits;ii++){
328 TetraIterator ti=m.tetra.begin();
329 int index=H[ii].second;
330 advance(ti ,(int)(index/4));
331 result[ii]=std::pair<TetraPointer,int>(&*ti,index%4);
332 }
333
334 delete [] selectBuf;
335 return result.size();
336 }
337
338 };
339
340
341 }
342
343 #endif