Allink  v0.1
ElPolyDrawSkinCGAL.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 #ifdef USE_CGAL
00027 #ifndef __CGAL_h__
00028 #include <fstream>
00029 #include <list>
00030 // #include <CGAL/basic.h>
00031 // #include <CGAL/Cartesian.h>
00032 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00033 //#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00034 //#include <CGAL/make_skin_surface_mesh_3.h>
00035 #include <CGAL/Skin_surface_3.h>
00036 #include <CGAL/mesh_skin_surface_3.h>
00037 #include <CGAL/subdivide_skin_surface_mesh_3.h>
00038 //#include <CGAL/Skin_surface_3.h>
00039 #include <CGAL/Polyhedron_3.h>
00040 #include "skin_surface_writer.h"
00041 #include <CGAL/IO/Polyhedron_iostream.h>
00042 #include <CGAL/subdivide_skin_surface_mesh_3.h>
00043 #include <CGAL/Skin_surface_refinement_policy_3.h>
00044 //#include <CGAL/mesh_skin_surface_3.h>
00045 //#include <CGAL/subdivide_skin_surface_mesh_3.h>
00046 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
00047 typedef CGAL::Skin_surface_traits_3<K> Traits;
00048 typedef CGAL::Skin_surface_3<Traits>   Skin_surface_3;
00049 typedef Skin_surface_3::FT             FT;
00050 typedef Skin_surface_3::Weighted_point Weighted_point;
00051 //typedef CGAL::Weighted_point<Bare_point,K::RT>              Weighted_point;
00052 typedef Weighted_point::Point          Bare_point;
00053 // typedef K::Point_3                  Bare_point;
00054 typedef CGAL::Polyhedron_3<K,
00055             CGAL::Skin_surface_polyhedral_items_3<Skin_surface_3> >   Polyhedron;
00056 //typedef CGAL::Polyhedron_3<K>                               Polyhedron;
00057 typedef Polyhedron::Traits::Vector_3   Vector;
00058 typedef Polyhedron::Vertex_iterator    Vertex_iterator;
00059 typedef Polyhedron::Halfedge_iterator  Halfedge_iterator;
00060 typedef Polyhedron::Facet_iterator     Facet_iterator;
00061 typedef Polyhedron::Halfedge_around_facet_circulator HFC;
00062 typedef Polyhedron::Vertex_handle      Vertex_handle;
00063 // CGAL::Skin_surface_refinement_policy_3<SkinSurface, Polyhedron> policy(skin);
00064 
00065 
00066 
00067 void ElPoly::DefineSkin(int NSample){
00068   std::list<Weighted_point> l;
00069   FT shrinkfactor = 0.5;
00070   double *Plot  = new double[pNType()*CUBE(NSample)];
00071   double *Count = new double[CUBE(NSample)];
00072   double Thre = 10.;
00073   double Radius = pEdge(0)/(double)NSample;
00074   for(int p=0;p<pNPart();p++){
00075     int t = pType(p);
00076     int vx = (int)(pPos(p,0)/pEdge(0)*NSample);
00077     int vy = (int)(pPos(p,1)/pEdge(1)*NSample);
00078     int vz = (int)(pPos(p,2)/pEdge(2)*NSample);
00079     int vTot = (vz*NSample+vy)*NSample+vx;
00080     Plot[vTot*pNType()+t] += 1.;
00081   }
00082   double *Norm = (double *)calloc(pNType(),sizeof(double));
00083   for(int t=0;t<pNType();t++){
00084     for(int v=0;v<CUBE(NSample);v++){
00085       if(Norm[t] < Plot[v*pNType()+t])
00086    Norm[t] = Plot[v*pNType()+t];
00087     }
00088     Norm[t] = Norm[t] <= 0. ? 1. : Norm[t];
00089   }
00090   for(int vx=0;vx<NSample;vx++){
00091     double x = vx*pEdge(0)/(double)NSample;
00092     for(int vy=0;vy<NSample;vy++){
00093       double y = vy*pEdge(1)/(double)NSample;
00094       for(int vz=0;vz<NSample;vz++){
00095    double z = vz*pEdge(2)/(double)NSample;
00096    int vTot = (vz*NSample+vy)*NSample+vx;
00097    if(Plot[vTot*pNType()] > Thre){
00098      l.push_front(Weighted_point(Bare_point(x,y,z),Radius));
00099    }
00100       }
00101     }
00102   }
00103 
00104   Polyhedron Polyhe;
00105 
00106   Skin_surface_3 skin_surface(l.begin(), l.end(), shrinkfactor);
00107   CGAL::mesh_skin_surface_3(skin_surface, Polyhe);
00108 
00109   //  CGAL::subdivide_skin_surface_mesh_3(skin_surface, Polyhe);
00110 
00111   // std::ofstream out("mesh.off");
00112   // out << Polyhe;
00113 
00114   glDeleteLists(Dr->Particles,1);
00115   Dr->Particles = glGenLists(1);
00116   glNewList(Dr->Particles,GL_COMPILE);
00117 
00118   // Polyhedron::Facet_iterator fcUp = Polyhe.facets_begin();
00119   // for(;fcUp != Polyhe.facets_end(); ++fcUp){
00120   //   Polyhedron::Supports_facet_halfedge = fcUp.halfedge();
00121   //   //Halfedge_around_facet_circulator heUp = fcUp.halfedge();
00122   // }
00123 
00124   // for (Vertex_iterator vit = Polyhe.vertices_begin();vit != Polyhe.vertices_end(); vit++){
00125   //   // Vector n = policy.normal(vit);
00126   //   // n = n/sqrt(n*n);
00127   //   cout << vit->point() << std::endl;
00128   //   Halfedge_iterator heUp = Polyhe.halfedges_begin();
00129   //   for(;heUp != Polyhe.halfedges_end(); ++heUp){
00130   //     //Polyhedron::Halfedge_handle Half = *heUp;
00131   //     Vertex_handle veUp = heUp->vertex();
00132   //     K::Point_3 pf1 = vit->point();
00133   //   }
00134   // }
00135   CGAL::Inverse_index<Vertex_handle> index(Polyhe.vertices_begin(),
00136                   Polyhe.vertices_end());
00137 
00138   for(Facet_iterator fi = Polyhe.facets_begin();fi != Polyhe.facets_end(); ++fi) {
00139     HFC hc = fi->facet_begin();
00140     HFC hc_end = hc;
00141     Polyhedron::Vertex_handle vf1 = (*hc).vertex();
00142     hc++;
00143     Polyhedron::Vertex_handle vf2 = (*hc).vertex();
00144     hc++;
00145     Polyhedron::Vertex_handle vf3 = (*hc).vertex();
00146     hc++;
00147     K::Point_3 pf1 = vf1->point();
00148     K::Point_3 pf2 = vf2->point();
00149     K::Point_3 pf3 = vf3->point();
00150     Vettore v1(pf1.x(),pf1.y(),pf1.z());
00151     Vettore v2(pf2.x(),pf2.y(),pf2.z());
00152     Vettore v3(pf3.x(),pf3.y(),pf3.z());
00153     Vettore vN(3);
00154     v1.Mult(InvScaleUn);
00155     v2.Mult(InvScaleUn);
00156     v3.Mult(InvScaleUn);
00157     vN = (v1-v2) ^ (v3-v2);
00158     //if(vN.Norm() > 2.*AreaMean) continue;
00159     double Sfumatura = .3*Mat->Casuale();
00160     glColor4f(0.1,.4+Sfumatura,0.2,1.);
00161     //glColor4f(HueUp[p].r,HueUp[p].g,HueUp[p].b,HueUp[p].a);
00162     DrTria(&v1,&v2,&v3,&vN);
00163     glColor4f(1.,.0,0.,1.);
00164     DrTriaContour(&v1,&v2,&v3);
00165 
00166 
00167     // glPushMatrix();//Particle
00168     // glBegin(GL_LINES);
00169     // do {
00170     //   Polyhedron::Vertex_handle vh = (*hc).vertex();
00171     //   K::Point_3 pf1 = vh->point();
00172     //   glVertex3d(pf1.x(),pf1.y(),pf1.z());
00173     // } while (++hc != hc_end);
00174     // glEnd();
00175     // glPopMatrix();//Particle
00176   }
00177   
00178   glEndList();
00179   
00180   // Tr tr;     // 3D-Delaunay triangulation
00181   // C2t3 c2t3 (tr);   // 2D-complex in 3D-Delaunay triangulation
00182 
00183   // Surface_3 surface(sphere_function,Sphere_3(CGAL::ORIGIN, 2.)); 
00184   // Surface_mesh_default_criteria_3<Tr> criteria(30., 0.1,0.1); 
00185   // // meshing surface
00186   // make_surface_mesh(c2t3, surface, criteria, CGAL::Non_manifold_tag());
00187 
00188   // std::cout << "Final number of points: " << tr.number_of_vertices() << "\n";
00189 
00190   // DT dt;
00191   // for(int c=0;c<Gen->NChain;c++){
00192   //   if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP) )continue;
00193   //   Point_3<K> ChPos(pPos(c,CLat1),pPos(c,CLat2),pPos(c,CNorm));
00194   //   dt.insert(ChPos);
00195   // }
00196   // Face_iterator fcTr = dt.finite_faces_begin();
00197   // glDeleteLists(Dr->Particles,1);
00198   // Dr->Particles = glGenLists(1);
00199   // glNewList(Dr->Particles,GL_COMPILE);
00200   // for(;fcTr != dt.faces_end(); ++fcTr){
00201   //   Vertex_handle vf1 = fcTr->vertex(0),
00202   //     vf2 = fcTr->vertex(1),vf3 = fcTr->vertex(2);
00203   //   Point pf1 = vf1->point();
00204   //   Point pf2 = vf2->point();
00205   //   Point pf3 = vf3->point();
00206   //   Vettore v1(pf1.x() - pf2.x(),pf1.y() - pf2.y(),pf1.z()-pf2.z());
00207   //   Vettore v2(pf3.x() - pf2.x(),pf3.y() - pf2.y(),pf1.z()-pf2.z());
00208   //   Vettore vN(3);
00209   //   vN = v1 ^ v2;
00210   //   DrTira(v1,v2,v3,vN);
00211     
00212   // }
00213   // glEndList();
00214    }
00215 #else
00216 void ElPoly::DefineSkin(){
00217   printf("DefSurface without CGAL not implemented\n");
00218 }
00219 #endif // USE_CGAL
00220 #endif // __CGAL_h__
00221 #endif //__glut_h__