refactoring
This commit is contained in:
parent
7d5d2271e6
commit
4b8f73d81c
|
|
@ -1141,7 +1141,7 @@ public:
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct EdgePointSplit : public std::unary_function<face::Pos<FaceType> , CoordType>
|
struct EdgePointSplit
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
std::map<std::pair<CoordType,CoordType>, VertexPointer> &edgeToPolyVertMap;
|
std::map<std::pair<CoordType,CoordType>, VertexPointer> &edgeToPolyVertMap;
|
||||||
|
|
|
||||||
193
wrap/gl/pick.h
193
wrap/gl/pick.h
|
|
@ -24,17 +24,19 @@
|
||||||
#ifndef __PICK______H
|
#ifndef __PICK______H
|
||||||
#define __PICK______H
|
#define __PICK______H
|
||||||
|
|
||||||
|
// Assumes OpenGL already included
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
#include <vcg/complex/complex.h>
|
#include <vcg/complex/complex.h>
|
||||||
|
|
||||||
|
|
||||||
namespace vcg{
|
namespace vcg{
|
||||||
|
|
||||||
template <class MESH_TYPE>
|
template <class MESH_TYPE>
|
||||||
class GLPickTri
|
class GLPickTri
|
||||||
{
|
{
|
||||||
|
public:
|
||||||
typedef typename MESH_TYPE::ScalarType ScalarType;
|
typedef typename MESH_TYPE::ScalarType ScalarType;
|
||||||
typedef typename MESH_TYPE::CoordType CoordType;
|
typedef typename MESH_TYPE::CoordType CoordType;
|
||||||
typedef typename MESH_TYPE::FaceIterator FaceIterator;
|
typedef typename MESH_TYPE::FaceIterator FaceIterator;
|
||||||
|
|
@ -43,7 +45,6 @@ class GLPickTri
|
||||||
typedef typename MESH_TYPE::VertexPointer VertexPointer;
|
typedef typename MESH_TYPE::VertexPointer VertexPointer;
|
||||||
typedef typename MESH_TYPE::VertexType VertexType;
|
typedef typename MESH_TYPE::VertexType VertexType;
|
||||||
|
|
||||||
public:
|
|
||||||
static CoordType glProject(const Eigen::Matrix<ScalarType,4,4> &M, const ScalarType * viewport, const CoordType &p)
|
static CoordType glProject(const Eigen::Matrix<ScalarType,4,4> &M, const ScalarType * viewport, const CoordType &p)
|
||||||
{
|
{
|
||||||
const ScalarType vx=viewport[0];
|
const ScalarType vx=viewport[0];
|
||||||
|
|
@ -54,11 +55,9 @@ public:
|
||||||
Eigen::Matrix<ScalarType,4,1> vpp = M*vp;
|
Eigen::Matrix<ScalarType,4,1> vpp = M*vp;
|
||||||
Eigen::Matrix<ScalarType,4,1> ndc = vpp/vpp[3];
|
Eigen::Matrix<ScalarType,4,1> ndc = vpp/vpp[3];
|
||||||
|
|
||||||
CoordType sc(
|
CoordType sc(vw2*ndc[0] + vx+vw2,
|
||||||
vw2*ndc[0] + vx+vw2,
|
|
||||||
vh2*ndc[1] + vy+vh2,
|
vh2*ndc[1] + vy+vh2,
|
||||||
ndc[2]
|
ndc[2]);
|
||||||
);
|
|
||||||
|
|
||||||
return sc;
|
return sc;
|
||||||
}
|
}
|
||||||
|
|
@ -96,8 +95,6 @@ public:
|
||||||
return bb;
|
return bb;
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
static bool PickClosestFace(int x, int y, MESH_TYPE &m, FacePointer &fp,int width=4, int height=4)
|
static bool PickClosestFace(int x, int y, MESH_TYPE &m, FacePointer &fp,int width=4, int height=4)
|
||||||
{
|
{
|
||||||
Eigen::Matrix<ScalarType,4,4> M;
|
Eigen::Matrix<ScalarType,4,4> M;
|
||||||
|
|
@ -185,7 +182,6 @@ public:
|
||||||
reg.Add(CoordType(x - width / ScalarType(2.0), y - height / ScalarType(2.0), ScalarType(-1.0)));
|
reg.Add(CoordType(x - width / ScalarType(2.0), y - height / ScalarType(2.0), ScalarType(-1.0)));
|
||||||
reg.Add(CoordType(x + width / ScalarType(2.0), y + height / ScalarType(2.0), ScalarType(1.0)));
|
reg.Add(CoordType(x + width / ScalarType(2.0), y + height / ScalarType(2.0), ScalarType(1.0)));
|
||||||
|
|
||||||
|
|
||||||
if ((M != lastM) || (&m != lastm) || (pVec.size() != m.VN()))
|
if ((M != lastM) || (&m != lastm) || (pVec.size() != m.VN()))
|
||||||
{
|
{
|
||||||
FillProjectedVector(m, pVec, M, viewportF);
|
FillProjectedVector(m, pVec, M, viewportF);
|
||||||
|
|
@ -234,7 +230,6 @@ public:
|
||||||
if(p[0] >=0 && p[0]<screenW && p[1] >=0 && p[1]<screenH)
|
if(p[0] >=0 && p[0]<screenW && p[1] >=0 && p[1]<screenH)
|
||||||
{
|
{
|
||||||
ScalarType bufZ(buffer[int(p[0])+int(p[1])*screenW]);
|
ScalarType bufZ(buffer[int(p[0])+int(p[1])*screenW]);
|
||||||
//qDebug("face %i txyz (%f %f %f) bufz %f",i,tx,ty,tz,bufZ);
|
|
||||||
if(bufZ + LocalEpsilon >= ScalarType(p[2]+1.0)/2.0)
|
if(bufZ + LocalEpsilon >= ScalarType(p[2]+1.0)/2.0)
|
||||||
resultZ.push_back(result[i]);
|
resultZ.push_back(result[i]);
|
||||||
}
|
}
|
||||||
|
|
@ -244,184 +239,6 @@ public:
|
||||||
return resultZ.size();
|
return resultZ.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#ifdef _I_REALLY_NEED_OLD_GL_PICK_
|
|
||||||
// Same of above but it also assumes that you want only visible faces.
|
|
||||||
// Visibility is computed according to the current depth buffer.
|
|
||||||
static int OldPickFaceVisible(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];
|
|
||||||
|
|
||||||
//GLenum err = glGetError();
|
|
||||||
glReadPixels(vp[0],vp[1],vp[2],vp[3],GL_DEPTH_COMPONENT,GL_FLOAT,buffer);
|
|
||||||
//err = glGetError();
|
|
||||||
|
|
||||||
std::vector<FacePointer> result;
|
|
||||||
OldPickFace(x,y,m,result,width,height,sorted);
|
|
||||||
ScalarType LocalEpsilon(0.001);
|
|
||||||
for(size_t i =0;i<result.size();++i)
|
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
ScalarType 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();
|
|
||||||
}
|
|
||||||
|
|
||||||
static int OldPickFace(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();
|
|
||||||
}
|
|
||||||
|
|
||||||
static int OldPickVert(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();
|
|
||||||
}
|
|
||||||
#endif // _I_REALLY_NEED_OLD_GL_PICK_
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace vcg
|
} // end namespace vcg
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue