Allink  v0.1
ElPolyDrawCGAL.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 
00024 #ifdef __glut_h__
00025 extern Draw *Dr;
00026   //#ifndef __CGAL_h__
00027 #ifdef USE_CGAL
00028 //#include <CGAL/basic.h>
00029 //#include <CGAL/Cartesian.h>
00030 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00031 #include <CGAL/Surface_mesh_default_triangulation_3.h>
00032 #include <CGAL/Complex_2_in_triangulation_3.h>
00033           //#include <CGAL/Implicit_surface_3.h>
00034 #include <CGAL/Delaunay_triangulation_2.h>
00035 #include <CGAL/Triangulation_euclidean_traits_xy_3.h>
00036 //using namespace CGAL;
00037 
00038 //default triangulation for Surface_mesher
00039 typedef CGAL::Surface_mesh_default_triangulation_3 Tr;
00040 
00041 //c2t3
00042 typedef CGAL::Complex_2_in_triangulation_3<Tr> C2t3;
00043 
00044 typedef Tr::Geom_traits GT;
00045 
00046 typedef GT::Sphere_3 Sphere_3;
00047 //typedef GT::Point_3 Point_3;
00048 typedef GT::FT FT;
00049 
00050 
00051 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
00052 typedef CGAL::Triangulation_euclidean_traits_xy_3<K>  Gt;
00053 typedef CGAL::Delaunay_triangulation_2<Gt> Delaunay;
00054 // typedef Triangulation_default_data_structure_2<Gt,Vb,Fb > Tds;
00055 // typedef Triangulation_2<Gt,Tds> Triangulation;
00056 template < class Gt >
00057 class My_face_base : public CGAL::Triangulation_face_base_2<Gt>
00058 {
00059 public:
00060   CGAL::Color color;
00061   My_face_base() :
00062     CGAL::Triangulation_face_base_2<Gt>() {}
00063   My_face_base(void* v0, void* v1, void* v2) : 
00064     CGAL::Triangulation_face_base_2<Gt>(v0,v1,v2) {}
00065   My_face_base(void* v0, void* v1, void* v2, void* n0, void* n1, void* n2) : 
00066     CGAL::Triangulation_face_base_2<Gt>(v0,v1,v2,n0,n1,n2) {}
00067 };
00068 typedef struct {double r;double g;double b;double a;} COLOR;
00069 
00070 void ElPoly::DrTriangulate(){
00071   glLineWidth(.5);
00072   Delaunay dtUp;
00073   Delaunay dtDown;
00074   //vector <COLOR> HueUp;
00075   //vector <COLOR> HueDown;
00076   COLOR ColChain;
00077   ColChain.a = 1.;
00078   double AreaMean = Gen->Edge[CLat1]*Gen->Edge[CLat2]*.5/(double)Gen->NChain;
00079   Delaunay::Finite_vertices_iterator vUp = dtUp.finite_vertices_begin();
00080   for(int c=0;c<Gen->NChain;c++){
00081     int Chc = Ch[c].Type;
00082     if(Ch[c].Pos[CNorm] > .7*Gen->Edge[CNorm]) continue;
00083     if(Ch[c].Pos[CNorm] < .3*Gen->Edge[CNorm]) continue;    
00084     if(!CHAIN_IF_TYPE(Chc,NChType)) continue;
00085     ColChain.r = 0.;
00086     if(CHAIN_IF_TYPE(Chc,CHAIN_UP))
00087       ColChain.r = 1.;
00088     ColChain.b = 0.;
00089     if(CHAIN_IF_TYPE(Chc,CHAIN_FLABBY))
00090       ColChain.b = 1.;
00091     ColChain.g=.7;
00092     if(CHAIN_IF_TYPE(Chc,CHAIN_TILTED))
00093       ColChain.g = .5;
00094     if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP) ){
00095       K::Point_3 ChPos(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2],Ch[c].Pos[CNorm]);
00096       dtUp.insert(ChPos);
00097       //HueUp.push_back(ColChain);
00098     }
00099     else{
00100       K::Point_3 ChPos(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2],Ch[c].Pos[CNorm]);
00101       dtDown.insert(ChPos);
00102       //HueDown.push_back(ColChain);
00103     }
00104   }
00105   Delaunay::Face_iterator fcTrUp = dtUp.finite_faces_begin();
00106   Delaunay::Face_iterator fcTrDown = dtDown.finite_faces_begin();
00107   glDeleteLists(Dr->Particles,1);
00108   Dr->Particles = glGenLists(1);
00109   glNewList(Dr->Particles,GL_COMPILE);
00110   for(int p=0;fcTrUp != dtUp.faces_end(); ++fcTrUp,p++){
00111     Delaunay::Vertex_handle vf1 = fcTrUp->vertex(0),
00112       vf2 = fcTrUp->vertex(1),vf3 = fcTrUp->vertex(2);
00113     K::Point_3 pf1 = vf1->point();
00114     K::Point_3 pf2 = vf2->point();
00115     K::Point_3 pf3 = vf3->point();
00116     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00117     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00118     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00119     Vettore vN(3);
00120     v1.Mult(InvScaleUn);
00121     v2.Mult(InvScaleUn);
00122     v3.Mult(InvScaleUn);
00123     vN = (v1-v2) ^ (v3-v2);
00124     //if(vN.Norm() > 2.*AreaMean) continue;
00125     glColor4f(0.,.7,0.,1.);
00126     //glColor4f(HueUp[p].r,HueUp[p].g,HueUp[p].b,HueUp[p].a);
00127     DrTria(&v1,&v2,&v3,&vN);
00128     glColor4f(0.,.0,0.,1.);
00129     DrTriaContour(&v1,&v2,&v3);
00130   }
00131   for(int p=0;fcTrDown != dtDown.faces_end(); ++fcTrDown,p++){
00132     Delaunay::Vertex_handle vf1 = fcTrDown->vertex(0),
00133       vf2 = fcTrDown->vertex(1),vf3 = fcTrDown->vertex(2);
00134     K::Point_3 pf1 = vf1->point();
00135     K::Point_3 pf2 = vf2->point();
00136     K::Point_3 pf3 = vf3->point();
00137     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00138     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00139     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00140     Vettore vN(3);
00141     v1.Mult(InvScaleUn);
00142     v2.Mult(InvScaleUn);
00143     v3.Mult(InvScaleUn);
00144     vN = (v1-v2) ^ (v3-v2);
00145     //if(vN.Norm() > 2.*AreaMean) continue;
00146     glColor4f(0.,.7,0.,1.);
00147     //    glColor4f(HueDown[p].r,HueDown[p].g,HueDown[p].b,HueDown[p].a);
00148     DrTria(&v1,&v2,&v3,&vN);
00149     glColor4f(0.,.0,0.,1.);
00150     DrTriaContour(&v1,&v2,&v3);
00151   }
00152   DrNano();
00153   glEndList();
00154 }
00155 
00156 //#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00157 //#include <CGAL/make_skin_surface_mesh_3.h>
00158 #include <CGAL/Skin_surface_3.h>
00159 #include <list>
00160 #include <CGAL/mesh_skin_surface_3.h>
00161 #include <CGAL/subdivide_skin_surface_mesh_3.h>
00162 //#include <CGAL/Skin_surface_3.h>
00163 #include <CGAL/Polyhedron_3.h>
00164 #include <fstream>
00165 #include "skin_surface_writer.h"
00166 #include <CGAL/IO/Polyhedron_iostream.h>
00167 #include <CGAL/subdivide_skin_surface_mesh_3.h>
00168 #include <CGAL/Skin_surface_refinement_policy_3.h>
00169 //#include <CGAL/mesh_skin_surface_3.h>
00170 //#include <CGAL/subdivide_skin_surface_mesh_3.h>
00171 //typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
00172 typedef CGAL::Skin_surface_traits_3<K>                      Traits;
00173 typedef CGAL::Skin_surface_3<Traits>                        Skin_surface_3;
00174 typedef Skin_surface_3::FT                                  FT;
00175 typedef Skin_surface_3::Weighted_point                      Weighted_point;
00176 typedef Weighted_point::Point                               Bare_point;
00177 typedef CGAL::Polyhedron_3<K,
00178   CGAL::Skin_surface_polyhedral_items_3<Skin_surface_3> >   Polyhedron;
00179 typedef Polyhedron::Traits::Vector_3                 Vector;
00180 //CGAL::Skin_surface_refinement_policy_3<SkinSurface, Polyhedron> policy(skin);
00181 #include <CGAL/Skin_surface_refinement_policy_3.h>
00182 // typedef K::Point_3                                          Bare_point;
00183 // typedef CGAL::Weighted_point<Bare_point,K::RT>              Weighted_point;
00184 // typedef CGAL::Polyhedron_3<K>                               Polyhedron;
00185 
00186 
00187 void ElPoly::DefineSkin(){
00188   list <Weighted_point> WeiPoint;
00189   double shrinkfactor = 1.0;
00190 
00191   for(int c=0;c<4;c++){//Gen->NChain;c++){
00192     Bare_point ChPoint(Ch[c].Pos[0]*InvScaleUn,Ch[c].Pos[1]*InvScaleUn,Ch[c].Pos[2]*InvScaleUn);
00193     WeiPoint.push_front(Weighted_point(ChPoint, 0.5));
00194   }
00195   Polyhedron Polyhe;
00196   //CGAL::make_skin_surface_mesh_3(Polyhe, WeiPoint.begin(), WeiPoint.end(), shrinkfactor);
00197   Skin_surface_3 skin_surface(WeiPoint.begin(),WeiPoint.end(),shrinkfactor);
00198   CGAL::mesh_skin_surface_3(skin_surface,Polyhe);
00199   CGAL::subdivide_skin_surface_mesh_3(skin_surface,Polyhe);
00200   std::ofstream out("mesh.off");
00201   out << Polyhe;
00202   // Polyhedron::Facet_iterator fcUp = Polyhe.facet_begin();
00203   // for(;fcUp != Polyhe.faces_end(); ++fcUp){
00204   //   Halfedge_around_facet_circulator heUp = fcUp.halfedge();
00205   glDeleteLists(Dr->Particles,1);
00206   Dr->Particles = glGenLists(1);
00207   glNewList(Dr->Particles,GL_COMPILE);
00208   glColor4f(0.0,0.0,0.0,1.0);
00209   double Pos[3];
00210   //  for (Polyhedron::Vertex_iterator vit = Polyhe.vertices_begin();vit != Polyhe.vertices_end(); vit ++) {
00211     //Vector n = policy.normal(vit);
00212     //n = n/sqrt(n*n);
00213     //    out << vit->point() << " " << n << std::endl;
00214   // Polyhedron::Halfedge_iterator heUp = Polyhe.halfedges_begin();
00215   // for(;heUp != Polyhe.halfedges_end(); ++heUp){
00216   //   //Polyhedron::Halfedge_handle Half = *heUp;
00217   //   Polyhedron::Vertex_handle veUp = heUp->vertex();
00218 //K::Point_3 pf1 = vit->point();
00219 
00220 
00221   // for(Polyhedron::Facet_iterator fi = Polyhe.facets_begin();fi != Polyhe.facets_end(); ++fi) {
00222   //   Skin_surface_3::HFC hc = fi->facet_begin();
00223   //   Polyhedron::HFC hc_end = hc;
00224   //   glPushMatrix();//Particle
00225   //   glBegin(GL_LINES);
00226   //   do {
00227   //     Polyhedron::Vertex_handle vh = (*hc).vertex();
00228   //     K::Point_3 pf1 = vh->point();
00229   //     Pos[0] = pf1.x();Pos[1] = pf1.y();Pos[2] = pf1.z();
00230   //     glVertex3d(Pos[0],Pos[1],Pos[2]);
00231   //   } while (++hc != hc_end);
00232   //   glEnd();
00233   //   glPopMatrix();//Particle
00234     
00235   // }
00236   glEndList();
00237   
00238   // Tr tr;     // 3D-Delaunay triangulation
00239   // C2t3 c2t3 (tr);   // 2D-complex in 3D-Delaunay triangulation
00240 
00241   // Surface_3 surface(sphere_function,Sphere_3(CGAL::ORIGIN, 2.)); 
00242   // Surface_mesh_default_criteria_3<Tr> criteria(30., 0.1,0.1); 
00243   // // meshing surface
00244   // make_surface_mesh(c2t3, surface, criteria, CGAL::Non_manifold_tag());
00245 
00246   // std::cout << "Final number of points: " << tr.number_of_vertices() << "\n";
00247 
00248   // DT dt;
00249   // for(int c=0;c<Gen->NChain;c++){
00250   //   if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP) )continue;
00251   //   Point_3<K> ChPos(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2],Ch[c].Pos[CNorm]);
00252   //   dt.insert(ChPos);
00253   // }
00254   // Face_iterator fcTr = dt.finite_faces_begin();
00255   // glDeleteLists(Dr->Particles,1);
00256   // Dr->Particles = glGenLists(1);
00257   // glNewList(Dr->Particles,GL_COMPILE);
00258   // for(;fcTr != dt.faces_end(); ++fcTr){
00259   //   Vertex_handle vf1 = fcTr->vertex(0),
00260   //     vf2 = fcTr->vertex(1),vf3 = fcTr->vertex(2);
00261   //   Point pf1 = vf1->point();
00262   //   Point pf2 = vf2->point();
00263   //   Point pf3 = vf3->point();
00264   //   Vettore v1(pf1.x() - pf2.x(),pf1.y() - pf2.y(),pf1.z()-pf2.z());
00265   //   Vettore v2(pf3.x() - pf2.x(),pf3.y() - pf2.y(),pf1.z()-pf2.z());
00266   //   Vettore vN(3);
00267   //   vN = v1 ^ v2;
00268   //   DrTira(v1,v2,v3,vN);
00269     
00270   // }
00271   // glEndList();
00272 }
00273 #else
00274 void ElPoly::DrTriangulate(){
00275   printf("DefSurface without CGAL not implemented\n");
00276 }
00277 #endif // __CGAL_h__
00278 #endif //__glut_h__