| 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 |
#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 |
|
| 73 |
glSelectBuffer(sz, selectBuf); |
| 74 |
glRenderMode(GL_SELECT); |
| 75 |
glInitNames(); |
| 76 |
|
| 77 |
|
| 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 |
|
| 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++; |
| 107 |
} |
| 108 |
|
| 109 |
glPopMatrix(); |
| 110 |
glMatrixMode(GL_PROJECTION); |
| 111 |
glPopMatrix(); |
| 112 |
glMatrixMode(GL_MODELVIEW); |
| 113 |
hits = glRenderMode(GL_RENDER); |
| 114 |
|
| 115 |
|
| 116 |
std::vector< std::pair<double,unsigned int> > H; |
| 117 |
for(long ii=0;ii<hits;ii++){ |
| 118 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 174 |
glSelectBuffer(sz, selectBuf); |
| 175 |
glRenderMode(GL_SELECT); |
| 176 |
glInitNames(); |
| 177 |
|
| 178 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 225 |
|
| 226 |
std::vector< std::pair<double,unsigned int> > H; |
| 227 |
int ii; |
| 228 |
for(ii=0;ii<hits;ii++){ |
| 229 |
|
| 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 |
|
| 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 |
|
| 267 |
glSelectBuffer(sz, selectBuf); |
| 268 |
glRenderMode(GL_SELECT); |
| 269 |
glInitNames(); |
| 270 |
|
| 271 |
|
| 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 |
|
| 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 |
|
| 317 |
|
| 318 |
std::vector< std::pair<double,unsigned int> > H; |
| 319 |
int ii; |
| 320 |
for(ii=0;ii<hits;ii++){ |
| 321 |
|
| 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 |
|
| 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 |