small refac in smooth + small refac in tetramesh gl wrapper

This commit is contained in:
T.Alderighi 2018-05-25 11:25:00 +02:00
parent 81a93f7756
commit eb97fef7bd
2 changed files with 122 additions and 144 deletions

View File

@ -223,13 +223,16 @@ class Smooth
v0 = t.V(Tetra::VofE(i, 0)); v0 = t.V(Tetra::VofE(i, 0));
v1 = t.V(Tetra::VofE(i, 1)); v1 = t.V(Tetra::VofE(i, 1));
vo0 = t.V(Tetra::VofE(5 - i, 0)); if (cotangentFlag)
vo1 = t.V(Tetra::VofE(5 - i, 1)); {
vo0 = t.V(Tetra::VofE(5 - i, 0));
vo1 = t.V(Tetra::VofE(5 - i, 1));
ScalarType angle = Tetra::DihedralAngle(t, 5 - i); ScalarType angle = Tetra::DihedralAngle(t, 5 - i);
ScalarType length = vcg::Distance(vo0->P(), vo1->P()); ScalarType length = vcg::Distance(vo0->P(), vo1->P());
weight = (length / 6.) * (tan(M_PI / 2. - angle)); weight = (length / 6.) * (tan(M_PI / 2. - angle));
}
TD[v0].sum += v1->cP() * weight; TD[v0].sum += v1->cP() * weight;
TD[v1].sum += v0->cP() * weight; TD[v1].sum += v0->cP() * weight;

View File

@ -43,78 +43,59 @@ public:
enum Hint {HShrinkFactor}; enum Hint {HShrinkFactor};
}; };
template <typename CONT_TETRA> template <typename MeshType>
class GlTetramesh:public GLW{ class GlTetramesh:public GLW{
public: public:
typedef typename CONT_TETRA::value_type TetraType; typedef typename MeshType::TetraType TetraType;
typedef typename TetraType::VertexType VertexType; typedef typename TetraType::VertexType VertexType;
typedef typename VertexType::ScalarType ScalarType; typedef typename VertexType::ScalarType ScalarType;
typedef typename VertexType::CoordType Point3x; typedef typename VertexType::CoordType CoordType;
//subclass for clipping planes //subclass for clipping planes
class ClipPlane class ClipPlane
{ {
private: private:
Point3x D;
Point3x D0;
GLdouble eqn[4];
vcg::Matrix44<float> TR;
Point3x pp0; CoordType D, D0;
Point3x pp1; ScalarType dist;
Point3x pp2;
Point3x pp3; GLdouble eqn[4];
public: public:
bool active; bool active;
Point3x P;
ClipPlane (){active=false;} ClipPlane (){active=false;}
~ClipPlane (){} ~ClipPlane (){}
ClipPlane(Point3x p0, Point3x p1,Point3x p2) ClipPlane(CoordType & p0, CoordType & p1, CoordType & p2)
{ {
Point3x N=((p1-p0)^(p2-p0)).Normalize(); CoordType N = ((p1-p0)^(p2-p0)).Normalize();
N.Normalize();
D=N;
D0=D;
P=(p0+p1+p2)/3.f;
Point3x v0=N; D = N;
Point3x v1=(P-p0); D0 = D;
v1.Normalize();
Point3x v2=(v0^v1);
v2.Normalize();
v0=v0*2;
v1=v1*2;
v2=v2*2;
pp0=-v1-v2;
pp1=-v1+v2;
pp2=v1+v2;
pp3=v1-v2;
dist = vcg::Norm((p0 + p1 + p2) / 3.f);
} }
//set normal of the clipping plane //set normal of the clipping plane
void SetD(Point3x d) void SetD(CoordType d)
{ {
D=d; D = d;
D0 = d;
} }
//set the point of the clipping plane //set the point of the clipping plane
void SetP(Point3x p) void SetDist(ScalarType d)
{ {
P=p; dist = d;
} }
bool IsClipped(Point3x p) bool IsClipped(CoordType p)
{ {
return D.V(0) * p.X() + D.V(1) * p.Y() + D.V(2) * p.Z() - vcg::Norm(P) < 0; return D.V(0) * p.X() + D.V(1) * p.Y() + D.V(2) * p.Z() - dist > 0;
} }
void GlClip() void GlClip()
@ -132,55 +113,58 @@ public:
void GlDraw() void GlDraw()
{ {
glPushMatrix(); const ScalarType w = 50;
glPushAttrib(0xffffffff); glColor4f(1., 1., 0., 0.3);
glDisable(GL_CLIP_PLANE0); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING);
glEnable(GL_LIGHTING); glBegin(GL_LINES);
glEnable(GL_NORMALIZE); glVertex3f(0, 0, 0);
glVertex3f(dist, 0, 0);
glTranslate(P); glEnd();
glMultMatrix(TR); glBegin(GL_TRIANGLES);
glLineWidth(0.5); glVertex3f(dist, -w, w);
glColor3d(0.7,0,0.7); glVertex3f(dist, w, w);
glBegin(GL_LINE_LOOP); glVertex3f(dist, -w, -w);
glVertex(pp0); glVertex3f(dist, w, w);
glVertex(pp1); glVertex3f(dist, w, -w);
glVertex(pp2); glVertex3f(dist, -w, -w);
glVertex(pp3);
glEnd(); glEnd();
glPopAttrib(); glDisable(GL_BLEND);
glPopMatrix(); glEnable(GL_LIGHTING);
} }
void Transform(vcg::Matrix44<float> Tr) void Transform(vcg::Matrix44<float> & tr)
{ {
//thath's for casting in case of trackball using D = (tr * D0).Normalize();
//float to double and vice-versa
Point3f p=Point3f((float)D0.V(0),(float)D0.V(1),(float)D0.V(2));
TR=Tr;
p=TR*p;
D=Point3x((ScalarType) p.V(0),(ScalarType) p.V(1),(ScalarType) p.V(2));
} }
void Translate(float L) void offsetDist(ScalarType off)
{ {
Point3x D1=D*L; dist = (off < -dist) ? 0.001f : dist + off;
P+=D1;
} }
bool IsActive()
{
return active;
}
bool switchActive()
{
return active ^= true;
}
}; };
GlTetramesh(CONT_TETRA * _t):tetra(_t){} GlTetramesh(MeshType * m) : _m(m){}
GlTetramesh( ) {} GlTetramesh( ) {}
CONT_TETRA * tetra; MeshType * _m;
ClipPlane section; ClipPlane section;
private: private:
ScalarType shrink_factor = 0.95f; ScalarType shrink_factor = 0.98f;
public: public:
@ -191,7 +175,7 @@ public:
} }
} }
void AddClipSection(Point3x p0,Point3x p1,Point3x p2) void AddClipSection(CoordType p0, CoordType p1, CoordType p2)
{ {
section=ClipPlane(p0,p1,p2); section=ClipPlane(p0,p1,p2);
section.active=true; section.active=true;
@ -206,49 +190,34 @@ public:
void Draw(){ void Draw(){
switch (dm){ switch (dm){
case DMNone: break; case DMNone: break;
case DMSmallTetra:_DrawSmallTetra<cm>();break; case DMSmallTetra: _DrawSmallTetra<cm>();break;
case DMFlat:_DrawSurface<dm,nm,cm>();break; case DMFlat: _DrawSurface<dm,nm,cm>();break;
case DMWire:_DrawSurface<dm,nm,cm>();break; case DMWire: _DrawSurface<dm,nm,cm>();break;
case DMHidden:_DrawSurface<dm,nm,cm>();break; case DMHidden: _DrawSurface<dm,nm,cm>();break;
case DMFlatWire:_DrawFlatWire<nm,cm>(); break; case DMFlatWire: _DrawFlatWire<nm,cm>(); break;
case DMTransparent:break; case DMTransparent: break;
} }
} }
private: private:
template <ColorMode cm > template <ColorMode cm >
void _DrawSmallTetra(){ void _DrawSmallTetra(){
typename CONT_TETRA::iterator it;
// glPushAttrib(0xffff); glEnable(GL_LIGHT0);
// glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHTING);
// glEnable(GL_NORMALIZE); ForEachTetra(*_m, [] (TetraType & t) {
// glPolygonMode(GL_FRONT, GL_FILL); if (!t.IsD())
// glLight(GL_LIGHT0, GL_DIFFUSE, vcg::Color4b::White);
// glEnable(GL_LIGHT0);
// glEnable(GL_LIGHTING);
/*glBegin(GL_TRIANGLES);*/
for( it = tetra->begin(); it != tetra->end(); ++it)
if((!it->IsD())&&(!(it->IsS()))) //draw as normal
{ {
_DrawSmallTetra<cm>(*it); if (!t.IsS()) //draw as normal
_DrawSmallTetra<cm>(t);
else //draw in selected mode
_DrawSelectedTetra(t);
} }
else });
if((!it->IsD())&&((it->IsS())))//draw in selection mode
{
_DrawSelectedTetra(*it);
}
//glEnd();
// glPopAttrib();
if (section.active)
{
// section.GlClip();
section.GlDraw();
}
} }
template <NormalMode nm,ColorMode cm > template <NormalMode nm,ColorMode cm >
void _DrawFlatWire(){ void _DrawFlatWire(){
glPushAttrib(0xffff); glPushAttrib(0xffff);
glEnable(GL_COLOR_MATERIAL); glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH); glEnable(GL_DEPTH);
@ -264,7 +233,7 @@ private:
template <DrawMode dm,NormalMode nm,ColorMode cm > template <DrawMode dm,NormalMode nm,ColorMode cm >
void _DrawSurface(){ void _DrawSurface(){
typename CONT_TETRA::iterator it;
glPushAttrib(0xffff); glPushAttrib(0xffff);
glEnable(GL_COLOR_MATERIAL); glEnable(GL_COLOR_MATERIAL);
@ -281,10 +250,11 @@ private:
glEnable(GL_NORMALIZE); glEnable(GL_NORMALIZE);
glPolygonMode(GL_FRONT,GL_FILL); glPolygonMode(GL_FRONT,GL_FILL);
} }
//glBegin(GL_TRIANGLES);
for( it = tetra->begin(); it != tetra->end(); ++it) ForEachTetra(*_m, [] (TetraType & t) {
_DrawTetra<dm,nm,cm>((*it)); _DrawTetra<dm,nm,cm>(t);
//glEnd(); });
glPopAttrib(); glPopAttrib();
} }
@ -355,14 +325,14 @@ private:
} }
if (cm == CMPerTetra) if (cm == CMPerTetra)
vcg::glColor(t.C()); vcg::glColor(t.C());
// else // else
// if(cm == CMPerTetraF) // if(cm == CMPerTetraF)
// { // {
// Color4b c; // Color4b c;
// c = color_tetra(t); // c = color_tetra(t);
// GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3]; // GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3];
// glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic); // glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic);
// } // }
} }
template <ColorMode cm > template <ColorMode cm >
@ -370,16 +340,16 @@ private:
{ {
if (cm!=CMNone) if (cm!=CMNone)
{ {
// if(cm == CMPerVertexF) // if(cm == CMPerVertexF)
// { // {
// Color4b c; // Color4b c;
// c = color_vertex(v); // c = color_vertex(v);
// GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3]; // GLint ic[4]; ic[0] = c[0];ic[1] = c[1];ic[2] = c[2];ic[3] = c[3];
// glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic); // glMaterialiv(GL_FRONT,GL_DIFFUSE ,ic);
// } // }
// else // else
if(cm == CMPerVertex) if(cm == CMPerVertex)
vcg::glColor(v.C()); vcg::glColor(v.C());
} }
} }
@ -424,24 +394,29 @@ private:
template < ColorMode cm > template < ColorMode cm >
void _DrawSmallTetra(TetraType &t) void _DrawSmallTetra(TetraType &t)
{ {
Point3x p[4], br; CoordType p[4], br;
br = Tetra::Barycenter(t); br = Tetra::Barycenter(t);
if (section.IsClipped(br))
return;
bool border = false;
bool clipBorder = false;
for (int i = 0; i < 4; ++i)
{
border = border || t.IsB(i);
Point3x br1 = Tetra::Barycenter(*t.TTp(i)); if (section.active)
clipBorder = clipBorder || section.IsClipped(br1); {
if (section.IsClipped(br))
return;
bool border = false;
bool clipBorder = false;
for (int i = 0; i < 4; ++i)
{
border = border || t.IsB(i);
CoordType br1 = Tetra::Barycenter(*t.TTp(i));
clipBorder = clipBorder || section.IsClipped(br1);
}
if (!border && !clipBorder)
return;
} }
if (!border && !clipBorder)
return;
for(int i = 0; i < 4; ++i) for(int i = 0; i < 4; ++i)
// p[i] = t.V(i)->P();
p[i] = t.V(i)->P() * shrink_factor + br * (1 - shrink_factor); p[i] = t.V(i)->P() * shrink_factor + br * (1 - shrink_factor);