Allink  v0.1
ElPolyDrawTriangCGAL.cpp
00001 /***********************************************************************
00002 ElPoly:This progam provide a graphical visualisation of the data 
00003 opend by VarData using openGL glut. The most important option are 
00004 the possibility of changing the backfold of the polymers with 'c', 
00005 see the subsequent file in the list with '>', see the bond with 'b'. 
00006 Copyright (C) 2008-2010 by Giovanni Marelli <sabeiro@virgilio.it>
00007 
00008 
00009 This program is free software; you can redistribute it and/or modify
00010 it under the terms of the GNU General Public License as published by
00011 the Free Software Foundation; either version 2 of the License, or
00012 (at your option) any later version.
00013 
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; If not, see <http://www.gnu.org/licenses/>.
00021 ***********************************************************************/
00022 #include "ElPoly.h"
00023           //#include <stdafx.h>
00024 
00025 #ifdef __glut_h__
00026 extern Draw *Dr;
00027   //#ifndef __CGAL_h__
00028 #ifdef USE_CGAL
00029 //#include <CGAL/basic.h>
00030 //#include <CGAL/Cartesian.h>
00031 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00032 #include <CGAL/Surface_mesh_default_triangulation_3.h>
00033 #include <CGAL/Complex_2_in_triangulation_3.h>
00034           //#include <CGAL/Implicit_surface_3.h>
00035 #include <CGAL/Delaunay_triangulation_2.h>
00036 #include <CGAL/Triangulation_euclidean_traits_xy_3.h>
00037 //using namespace CGAL;
00038 
00039 //default triangulation for Surface_mesher
00040 typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
00041 
00042 //c2t3
00043 typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
00044 
00045 typedef Tr::Geom_traits GT;
00046 
00047 typedef GT::Sphere_3 Sphere_3;
00048 //typedef GT::Point_3 Point_3;
00049 typedef GT::FT FT;
00050 
00051 
00052 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
00053 typedef CGAL::Triangulation_euclidean_traits_xy_3<K>  Gt;
00054 typedef CGAL::Delaunay_triangulation_2<Gt> Delaunay;
00055 // typedef Triangulation_default_data_structure_2<Gt,Vb,Fb > Tds;
00056 // typedef Triangulation_2<Gt,Tds> Triangulation;
00057 template < class Gt >
00058 class My_face_base : public CGAL::Triangulation_face_base_2<Gt>
00059 {
00060 public:
00061   CGAL::Color color;
00062   My_face_base() :
00063     CGAL::Triangulation_face_base_2<Gt>() {}
00064   My_face_base(void* v0, void* v1, void* v2) : 
00065     CGAL::Triangulation_face_base_2<Gt>(v0,v1,v2) {}
00066   My_face_base(void* v0, void* v1, void* v2, void* n0, void* n1, void* n2) : 
00067     CGAL::Triangulation_face_base_2<Gt>(v0,v1,v2,n0,n1,n2) {}
00068 };
00069 typedef struct {double r;double g;double b;double a;} COLOR;
00070 
00071 void ElPoly::DrTriangulate(){
00072   if(Dr->IfMaterial){
00073     glEnable(GL_LIGHTING);
00074     glEnable( GL_LIGHT0 );
00075   }
00076   else 
00077     glDisable(GL_LIGHTING);
00078   Delaunay dtUp;
00079   Delaunay dtDown;
00080   //vector <COLOR> HueUp;
00081   //vector <COLOR> HueDown;
00082   COLOR ColChain;
00083   GLfloat Color[4];
00084   ColChain.a = 1.;
00085   double AreaMean = pEdge(CLat1)*pEdge(CLat2)*.5/(double)pNChain();
00086   BfDefChain();
00087   double Pos[3];
00088   Delaunay::Finite_vertices_iterator vUp = dtUp.finite_vertices_begin();
00089   for(int c=0;c<pNChain();c++){
00090     int Chc = Ch[c].Type;
00091     //if(Ch[c].Pos[CNorm] > .7*pEdge(CNorm)) continue;
00092     //if(Ch[c].Pos[CNorm] < .3*pEdge(CNorm)) continue;    
00093     if(!CHAIN_IF_TYPE(Chc,NChType)) continue;
00094     ColChain.r = 0.;
00095     if(CHAIN_IF_TYPE(Chc,CHAIN_UP))
00096       ColChain.r = 1.;
00097     ColChain.b = 0.;
00098     if(CHAIN_IF_TYPE(Chc,CHAIN_FLABBY))
00099       ColChain.b = 1.;
00100     ColChain.g=.7;
00101     if(CHAIN_IF_TYPE(Chc,CHAIN_TILTED))
00102       ColChain.g = .5;
00103     for(int d=0;d<3;d++)
00104       Pos[d] = Ch[c].Pos[d] - floor(Ch[c].Pos[d]*pInvEdge(d))*pEdge(d);
00105     if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP) ){
00106       K::Point_3 ChPos(Pos[0],Pos[1],Pos[2]);
00107       dtUp.insert(ChPos);
00108       //HueUp.push_back(ColChain);
00109     }
00110     else{
00111       K::Point_3 ChPos(Pos[0],Pos[1],Pos[2]);
00112       dtDown.insert(ChPos);
00113       //HueDown.push_back(ColChain);
00114     }
00115   }
00116   Delaunay::Face_iterator fcTrUp   = dtUp.finite_faces_begin();
00117   Delaunay::Face_iterator fcTrDown = dtDown.finite_faces_begin();
00118   glDeleteLists(Dr->Particles,1);
00119   Dr->Particles = glGenLists(1);
00120   glNewList(Dr->Particles,GL_COMPILE);
00121   for(int p=0;fcTrUp != dtUp.faces_end(); ++fcTrUp,p++){
00122     Delaunay::Vertex_handle vf1 = fcTrUp->vertex(0),
00123       vf2 = fcTrUp->vertex(1),vf3 = fcTrUp->vertex(2);
00124     K::Point_3 pf1 = vf1->point();
00125     K::Point_3 pf2 = vf2->point();
00126     K::Point_3 pf3 = vf3->point();
00127     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00128     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00129     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00130     Vettore vN(3);
00131     v1.Mult(InvScaleUn);
00132     v2.Mult(InvScaleUn);
00133     v3.Mult(InvScaleUn);
00134     vN = (v1-v2) ^ (v3-v2);
00135     glEnable(GL_LIGHTING);
00136     double Depth = pf1.z()*pInvEdge(CNorm)*Saturation+ExtParam;
00137     Dr->DepthMap(Depth,Color);
00138     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,Color); 
00139     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,Color); 
00140     glColor4fv(Color);
00141     //if(vN.Norm() > 2.*AreaMean) continue;
00142     DrTria(&v1,&v2,&v3,&vN);
00143     glDisable(GL_LIGHTING);
00144     glColor4f(0.,0.,1.,1.);
00145     DrTriaContour(&v1,&v2,&v3);
00146   }
00147   for(int p=0;fcTrDown != dtDown.faces_end(); ++fcTrDown,p++){
00148     Delaunay::Vertex_handle vf1 = fcTrDown->vertex(0),
00149       vf2 = fcTrDown->vertex(1),vf3 = fcTrDown->vertex(2);
00150     K::Point_3 pf1 = vf1->point();
00151     K::Point_3 pf2 = vf2->point();
00152     K::Point_3 pf3 = vf3->point();
00153     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00154     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00155     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00156     Vettore vN(3);
00157     v1.Mult(InvScaleUn);
00158     v2.Mult(InvScaleUn);
00159     v3.Mult(InvScaleUn);
00160     vN = (v1-v2) ^ (v3-v2);
00161     glEnable(GL_LIGHTING);
00162     double Depth = pf1.z()*pInvEdge(CNorm)*Saturation+ExtParam;
00163     Dr->DepthMap(Depth,Color);
00164     glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,Color); 
00165     glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,Color); 
00166     glColor4fv(Color);
00167     //if(vN.Norm() > 2.*AreaMean) continue;
00168     //    glColor4f(HueDown[p].r,HueDown[p].g,HueDown[p].b,HueDown[p].a);
00169     DrTria(&v1,&v2,&v3,&vN);
00170     glDisable(GL_LIGHTING);
00171     glColor4f(1.,0.,0.,1.);
00172     DrTriaContour(&v1,&v2,&v3);
00173   }
00174   glDisable(GL_LIGHTING);
00175   for(int n=0;n<pNNano();n++) DrawNano(n);
00176   glEndList();
00177 }
00178 #include <CGAL/Triangulation_3.h>
00179 typedef CGAL::Triangulation_3<K> Triang;
00180 void ElPoly::DrCells(){
00181   Delaunay dtUp;
00182   COLOR ColChain;
00183   ColChain.a = 1.;
00184   double AreaMean = pEdge(CLat1)*pEdge(CLat2)*.5/(double)pNChain();
00185   std::list<Triang::Point> LipPos;
00186   Delaunay::Finite_vertices_iterator vUp = dtUp.finite_vertices_begin();
00187   for(int b=0,cOff=0;b<pNBlock();b++,cOff+=Block[b].NChain){
00188     for(int c=cOff;c<pNChain(b)+cOff;c+=20){
00189       int Chc = Ch[c].Type;
00190       if(!CHAIN_IF_TYPE(Chc,NChType)) continue;
00191       ColChain.r = 0.;
00192       if(CHAIN_IF_TYPE(Chc,CHAIN_UP))
00193    ColChain.r = 1.;
00194       ColChain.b = 0.;
00195       if(CHAIN_IF_TYPE(Chc,CHAIN_FLABBY))
00196    ColChain.b = 1.;
00197       ColChain.g=.7;
00198       if(CHAIN_IF_TYPE(Chc,CHAIN_TILTED))
00199    ColChain.g = .5;
00200       Triang::Point LipPoint(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2],Ch[c].Pos[CNorm]);
00201       LipPos.push_front(LipPoint);
00202       //HueUp.push_back(ColChain);
00203     }
00204   }
00205   Triang Tr(LipPos.begin(),LipPos.end());
00206   Triang::size_type NVert = Tr.number_of_vertices();
00207   std::vector<Triang::Point> V(3);
00208   assert(Tr.is_valid());
00209   // Triang::Locate_type lt;
00210   // int li,lj;
00211   // Triang::Point p(0,0,0);
00212   // Triang::Cell_handle c = Tr.locate(p,lt,li,lj);
00213   // assert( lt == Triang::VERTEX);
00214   // assert(c->vertex(li)->point() == p);
00215   // Triang::Vertex_handle v = c->vertex( (li+1)&3);
00216   // Triang::Cell_handle nc = c->neighbor(li);
00217   // int nli;
00218   // assert(nc->has_vertex(v,nli));
00219   glDeleteLists(Dr->Particles,1);
00220   Dr->Particles = glGenLists(1);
00221   glNewList(Dr->Particles,GL_COMPILE);
00222   //Triang::Cell_iterator CIt = Tr.finite_cells_begin();
00223   Triang::Finite_cells_iterator CIt = Tr.finite_cells_begin();
00224   for(int p=0;CIt != Tr.finite_cells_end();++CIt,p++){
00225     Triang::Vertex_handle vf1 = CIt->vertex(0),vf2 = CIt->vertex(1),vf3 = CIt->vertex(2);
00226     K::Point_3 pf1 = vf1->point();
00227     K::Point_3 pf2 = vf2->point();
00228     K::Point_3 pf3 = vf3->point();
00229     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00230     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00231     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00232     Vettore vN(3);
00233     v1.Mult(InvScaleUn);
00234     v2.Mult(InvScaleUn);
00235     v3.Mult(InvScaleUn);
00236     vN = (v1-v2) ^ (v3-v2);
00237     //if(vN.Norm() > 2.*AreaMean) continue;
00238     double Sfumatura = .3*Mat->Casuale();
00239     glColor4f(0.1,.4+Sfumatura,0.2,1.);
00240     //glColor4f(HueUp[p].r,HueUp[p].g,HueUp[p].b,HueUp[p].a);
00241     DrTria(&v1,&v2,&v3,&vN);
00242     glColor4f(0.,.0,0.,1.);
00243     DrTriaContour(&v1,&v2,&v3);
00244   }
00245   for(int n=0;n<pNNano();n++) DrawNano(n);
00246   glEndList();
00247 }
00248 #include <CGAL/Surface_mesh_default_triangulation_3.h>
00249 #include <CGAL/Complex_2_in_triangulation_3.h>
00250 #include <CGAL/make_surface_mesh.h>
00251 #include <CGAL/Implicit_surface_3.h>
00252 
00253 // default triangulation for Surface_mesher
00254 typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
00255 
00256 // c2t3
00257 typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
00258 
00259 typedef Tr::Geom_traits GT;
00260 typedef GT::Sphere_3 Sphere_3;
00261 typedef GT::Point_3 Point_3;
00262 typedef GT::FT FT;
00263 
00264 typedef FT (*Function)(Point_3);
00265 
00266 typedef CGAL::Implicit_surface_3<GT, Function> Surface_3;
00267 
00268 FT sphere_function (Point_3 p) {
00269   const FT x2=p.x()*p.x(), y2=p.y()*p.y(), z2=p.z()*p.z();
00270   return x2+y2+z2-1;
00271 }
00272 void ElPoly::DrGenMesh(){
00273   Tr tr;            // 3D-Delaunay triangulation
00274   C2t3 c2t3 (tr);   // 2D-complex in 3D-Delaunay triangulation
00275   Surface_3 surface(sphere_function,Sphere_3(CGAL::ORIGIN, 2.));
00276   CGAL::Surface_mesh_default_criteria_3<Tr> criteria(30.,  // angular bound
00277                                                      0.1,  // radius bound
00278                                                      0.1); // distance bound
00279   CGAL::make_surface_mesh(c2t3, surface, criteria, CGAL::Non_manifold_tag());
00280   glDeleteLists(Dr->Particles,1);
00281   Dr->Particles = glGenLists(1);
00282   glNewList(Dr->Particles,GL_COMPILE);
00283   Tr::Finite_cells_iterator CIt = tr.finite_cells_begin();
00284   for(int p=0;CIt != tr.finite_cells_end();++CIt,p++){
00285     Tr::Vertex_handle vf1 = CIt->vertex(0),vf2 = CIt->vertex(1),vf3 = CIt->vertex(2);
00286     K::Point_3 pf1 = vf1->point();
00287     K::Point_3 pf2 = vf2->point();
00288     K::Point_3 pf3 = vf3->point();
00289     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00290     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00291     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00292     Vettore vN(3);
00293     v1.Mult(InvScaleUn);
00294     v2.Mult(InvScaleUn);
00295     v3.Mult(InvScaleUn);
00296     vN = (v1-v2) ^ (v3-v2);
00297     //if(vN.Norm() > 2.*AreaMean) continue;
00298     double Sfumatura = .3*Mat->Casuale();
00299     glColor4f(0.1,.4+Sfumatura,0.2,1.);
00300     //glColor4f(HueUp[p].r,HueUp[p].g,HueUp[p].b,HueUp[p].a);
00301     DrTria(&v1,&v2,&v3,&vN);
00302     glColor4f(1.,.0,0.,1.);
00303     DrTriaContour(&v1,&v2,&v3);
00304   }
00305   for(int n=0;n<pNNano();n++) DrawNano(n);
00306   glEndList();
00307 }
00308 #else
00309 void ElPoly::DrTriangulate(){
00310   printf("DefSurface without CGAL not implemented\n");
00311 }
00312 void ElPoly::DrMesh(){
00313   printf("DefSurface without CGAL not implemented\n");
00314 }
00315 void ElPoly::DrCells(){
00316   printf("DefSurface without CGAL not implemented\n");
00317 }
00318 #endif // __CGAL_h__
00319 #endif //__glut_h__