Allink  v0.1
VarDataCGAL.cpp
00001 /*  colored_face.C   */     
00002 /*  ---------------- */
00003 #include <VarData.h>
00004 //#ifndef __CGAL_h__
00005 #ifdef USE_CGAL
00006 #include <CGAL/basic.h>
00007 #include <CGAL/Cartesian.h>
00008 #include <CGAL/IO/Color.h>
00009 #include <CGAL/Triangulation_euclidean_traits_2.h>
00010 #include <CGAL/Triangulation_default_data_structure_2.h>
00011 #include <CGAL/Triangulation_2.h>
00012 #include <CGAL/Delaunay_triangulation_2.h>
00013 #include <CGAL/Voronoi_diagram_2.h>
00014 #include <CGAL/Delaunay_triangulation_adaptation_traits_2.h>
00015 #include <CGAL/Delaunay_triangulation_adaptation_policies_2.h>
00016 #include <vector>
00017 #include <iostream>
00018 #include <fstream>
00019 #include <cassert>
00020 
00021 using namespace CGAL;
00022 using namespace std;
00023 
00024 /* A facet with a color member variable. */
00025 template < class Gt >
00026 class My_face_base : public Triangulation_face_base_2<Gt>
00027 {
00028 public:
00029   Color color;
00030   My_face_base() :
00031     Triangulation_face_base_2<Gt>() {}
00032   My_face_base(void* v0, void* v1, void* v2) : 
00033     Triangulation_face_base_2<Gt>(v0,v1,v2) {}
00034   My_face_base(void* v0, void* v1, void* v2, void* n0, void* n1, void* n2) : 
00035     Triangulation_face_base_2<Gt>(v0,v1,v2,n0,n1,n2) {}
00036 };
00037 
00038 typedef Cartesian<double> Rp;
00039 typedef Triangulation_euclidean_traits_2<Rp> Gt;
00040 typedef Triangulation_vertex_base_2<Gt> Vb;
00041 typedef My_face_base<Gt> Fb;
00042 typedef Triangulation_default_data_structure_2<Gt,Vb,Fb > Tds;
00043 typedef Triangulation_2<Gt,Tds> Triangulation;
00044 //typedef Delaunay_graph DG;
00045 typedef Delaunay_triangulation_2<Gt,Tds> DT;
00046 typedef Delaunay_triangulation_adaptation_traits_2<DT> AT;
00047 typedef Delaunay_triangulation_caching_degeneracy_removal_policy_2 <DT> AP;
00048 typedef Voronoi_diagram_2<DT,AT,AP> VD;
00049 typedef Point_2<Rp>  Point;
00050 typedef Triangulation::Face_handle Face_handle;
00051 typedef Triangulation::Face_iterator Face_iterator;
00052 typedef Triangulation::Vertex Vertex;
00053 typedef Triangulation::Vertex_handle Vertex_handle;
00054 typedef Triangulation::Vertex_iterator Vertex_iterator;
00055 // typedef VD::Face_handle Face_handle;
00056 // typedef VD::Face_iterator Face_iterator;
00057 // typedef VD::Vertex Vertex;
00058 // typedef VD::Vertex_handle Vertex_handle;
00059 // typedef VD::Vertex_iterator Vertex_iterator;
00060 typedef Triangulation::Finite_vertices_iterator Finite_vertices_iterator;
00061 
00062 void VarData::AreaDistr(double *Distr,double *RadDistr,int Values) {  
00063   double BoxRadInv = pInvEdge(3);
00064   //  VD vd;
00065   double AreaMean = 3.*.5*Gen->NChain/(Gen->Edge[CLat1]*Gen->Edge[CLat2]);
00066   DT dtUp;
00067   DT dtDown;
00068   //ofstream File2Write("AreaSnapshot.dat");
00069   //Triangulation dt;
00070   //Point p;
00071   for(int c=0;c<Gen->NChain;c++){
00072     if(CHAIN_IF_TYPE(Ch[c].Type,CHAIN_UP) ){
00073       Point ChPos(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2]);
00074       dtUp.insert(ChPos);
00075     }
00076     else{
00077       Point ChPos(Ch[c].Pos[CLat1],Ch[c].Pos[CLat2]);
00078       dtDown.insert(ChPos);      
00079     }
00080   }
00081   //  Face_iterator fc = t.faces_begin();
00082   Face_iterator fcTrUp = dtUp.finite_faces_begin();
00083   Face_iterator fcTrDown = dtDown.finite_faces_begin();
00084   //VD::Face_iterator fcVor = vd.faces_begin();
00085   //VD::Face_iterator fcVor = vd.faces_begin();
00086   double *Norma = new double[Values];
00087   memset(RadDistr,0.,Values*sizeof(double));
00088   vector <double> Areas;
00089   for(;fcTrUp != dtUp.faces_end(); ++fcTrUp){
00090     Vertex_handle vf1 = fcTrUp->vertex(0),
00091       vf2 = fcTrUp->vertex(1),vf3 = fcTrUp->vertex(2);
00092     Point pf1 = vf1->point();
00093     Point pf2 = vf2->point();
00094     Point pf3 = vf3->point();
00095     Vettore v1(pf1.x() - pf2.x(),pf1.y() - pf2.y(),0.);
00096     Vettore v2(pf3.x() - pf2.x(),pf3.y() - pf2.y(),0.);
00097     double Area = v1.VetV(&v2);
00098     double Distx = Nano->Pos[CLat1] - (pf1.x() + pf2.x() + pf3.x())/3.;
00099     double Disty = Nano->Pos[CLat2] - (pf1.y() + pf2.y() + pf3.y())/3.;
00100     double Rad = sqrt( SQR(Distx) + SQR(Disty) );
00101     if(Rad < Nano->Rad){continue;}
00102     int v = (int)(Rad*BoxRadInv*Values);
00103     //assert(v >= 0 && v < Values);
00104     if(v < 0 || v >= Values){continue;}
00105     RadDistr[v] += Area;
00106     Areas.push_back(Area);
00107     Norma[v] += 1.;
00108     //File2Write << vf1->point() << endl <<  vf2->point()<< endl << vf3->point() << endl << endl << endl ;
00109   }
00110   for(;fcTrDown != dtDown.faces_end(); ++fcTrDown){
00111     Vertex_handle vf1 = fcTrDown->vertex(0),
00112       vf2 = fcTrDown->vertex(1),vf3 = fcTrDown->vertex(2);
00113     Point pf1 = vf1->point();
00114     Point pf2 = vf2->point();
00115     Point pf3 = vf3->point();
00116     Vettore v1(pf1.x() - pf2.x(),pf1.y() - pf2.y(),0.);
00117     Vettore v2(pf3.x() - pf2.x(),pf3.y() - pf2.y(),0.);
00118     double Area = v1.VetV(&v2);
00119     double Distx = Nano->Pos[CLat1] - (pf1.x() + pf2.x() + pf3.x())/3.;
00120     double Disty = Nano->Pos[CLat2] - (pf1.y() + pf2.y() + pf3.y())/3.;
00121     double Rad = sqrt( SQR(Distx) + SQR(Disty) );
00122     //if(Rad < Nano->Rad){continue;}
00123     int v = (int)(Rad*BoxRadInv*Values);
00124     if(v < 0 || v >= Values){continue;}
00125     RadDistr[v] += Area;
00126     Areas.push_back(Area);
00127     Norma[v] += 1.;
00128     //File2Write << vf1->point() << endl <<  vf2->point()<< endl << vf3->point() << endl << endl << endl ;
00129   }
00130   // for(int v=0;v<Values;v++){
00131   //   RadDistr[v] /= Norma[v] > 0 ? Norma[v] : 1.;
00132   // }
00133   vector <double>::iterator iArea;
00134   for(iArea=Areas.begin();iArea!=Areas.end();iArea++){
00135     int v = (int)(*iArea/AreaMean*Values);
00136     if(v < 0 || v >= Values){continue;}
00137     Distr[v] += 1.;
00138   }
00139   delete [] Norma;
00140   //File2Write.close();
00141   return;
00142   //Finite_vertices_iterator vc = dt.finite_vertices_begin();
00143   // while(vc != dtUp.finite_vertices_end()){
00144   //   Face_handle fv = vc->face();
00145   //   Vertex_handle vf1 = fv->vertex(0),
00146   //     vf2 = fv->vertex(1),vf3 = fv->vertex(2);
00147   //   //File2Write << vf1->point() << endl << vf2->point() << endl << vf3->point() << endl << endl << endl;
00148   //   ++vc;
00149   // }
00150   // while(fcVor != vd.faces_end()){
00151   //   //    VD::Face fc1 = *fcVor;
00152   //   //VD::Vertex_handle vf1 = fcVor->vertex(0),//fcVor->vertex(0),
00153   //   //vf2 = fcVor->vertex(1),vf3 = fcVor->vertex(2);    
00154   //   //File2Write << vf1->point() << endl << vf2->point() << endl << vf3->point() << endl << endl << endl;
00155   //   ++fcVor;
00156   // }
00157 
00158   // Finite_vertices_iterator it1 = dt.finite_vertices_begin(),it2(it1), it3(it1);
00159   // ++it2;
00160   // ++it3; ++it3;
00161   // //    Vertex_handle vf0 = fc.vertex(0);
00162   // while( it3 != dt.finite_vertices_end()) {
00163   //   //File2Write << it1->point() << endl << it2->point() << endl << it3->point() << endl << endl << endl;
00164   //   ++it1; ++it2; ++it3; 
00165   // }
00166  
00167   // return 0;
00168 }
00169 #else
00170 void VarData::AreaDistr(double *Distr,double *RadDistr,int Values){  
00171   printf("AreaDistr without CGAL not implemented\n");
00172 }
00173 
00174 #endif //USE_CGAL