NanoShaper  0.3.1
NanoShaper is a tool able to triangulate and inspect an arbitray triangulated surface or several types of molecular surfaces such as the Gaussian, Skin and the SES
C:/Documents and Settings/sdecherchi/My Documents/Ricerca/software nostro/NanoShaper 0.3.1/NanoShaper/src/SkinSurface.h
Go to the documentation of this file.
00001 //---------------------------------------------------------
00005 //---------------------------------------------------------
00006 
00007 #ifndef SkinSurface_h
00008 #define SkinSurface_h
00009 
00010 #include "Surface.h"
00011 
00012 //#define DEBUG_SKIN
00013 
00014 #define DEFAULT_S 0.45 // default shrink factor
00015 
00017 class MixedCell{
00018 public:
00019         double* quadric;
00020         int surface_type;
00021         vector<double*> mc_points; // these are only pointer, no explicit denstructor
00022 
00023         virtual ~MixedCell()
00024         {               
00025                 if (quadric!=NULL)
00026                         deleteVector<double>(quadric);
00027         }
00028 };
00029 
00030 
00031 // voronoi facet cell or del edge
00032 class Del1Cell : public MixedCell
00033 {
00034 public:
00035         int ids[2]; // the two atoms generating the del edge
00036         vector<double*> planes; // lateral planes
00037 
00038         virtual ~Del1Cell()
00039         {               
00040                 for (unsigned int i=0;i<planes.size();i++)
00041                         free(planes[i]);
00042         }
00043 
00044         // #IMPROVE one could build pointers from here to the planes computed in Del2Cell or viceversa
00045         double upper[4]; // upper plane
00046         double lower[4]; // upper plane
00047 };
00048 
00049 // reduced voronoi cell
00050 class Del0Cell : public MixedCell
00051 {
00052 public:
00053         int id; // atom index
00054         vector<double*> planes; // planes pointer that points to upper/lower of Del1Cell
00055 
00056         virtual ~Del0Cell()
00057         {               
00058                 // do nothing, planes are not managed by this class
00059         }
00060 };
00061 
00062 // del facet cell
00063 class Del2Cell : public MixedCell
00064 {
00065 public:
00066         int ids[3]; // atom indices
00067         double planes[3][4]; // lateral planes
00068         double upper[4]; 
00069         double lower[4]; 
00070 
00071         virtual ~Del2Cell()
00072         {               
00073                 // do nothing, no pointers
00074         }
00075 };
00076 
00077 // reduced thethraedron
00078 class Del3Cell: public MixedCell
00079 {
00080 public:
00081         int ids[4]; // atom indices
00082         // planes of the reduced tethraedron
00083         double planes[4][4];    
00084         double planes_or[4][4];
00085         double points[4][3]; // reduced points in the same order of atom indices
00086         double vor[3]; // Voronoi center
00087         double reduced[4][3]; // reduced points in the same order of atom indices
00088 
00089         virtual ~Del3Cell()
00090         {               
00091                 // do nothing, no pointers
00092         }
00093 };
00094 
00095 
00096 #define GRID_MIXEDCELLMAP_2D(i,j,k,NA,NB) gridMixedCellMap2D[(k)+(MAX_MIXEDCELLS_2D-1)*((j)+(NB)*(i))]
00097 // fortran like
00098 //#define GRIDMIXEDCELLMAP(i,j,k,l,NX,NY,NZ) gridMixedCellMap[(i)+(NX)*((j)+(NY)*((k)+(NZ)*(l)))]
00099 // c like (NX is ignored)
00100 #define GRIDMIXEDCELLMAP(i,j,k,l,NX,NY,NZ) gridMixedCellMap[(l)+(MAX_MIXEDCELLS-1)*((k)+(NZ)*((j)+(NY)*(i)))]
00101 
00102 #ifdef ENABLE_CGAL 
00103 
00104 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00105 #include <CGAL/Regular_triangulation_3.h>
00106 #include <CGAL/Regular_triangulation_euclidean_traits_3.h>
00107 #include <CGAL/Regular_triangulation_filtered_traits_3.h>
00108 #include <CGAL/Triangulation_cell_base_with_info_3.h>
00109 #include <CGAL/Regular_triangulation_cell_base_3.h>
00110 #include <CGAL/Triangulation_vertex_base_with_info_3.h>
00111 #include <CGAL/Triangulation_data_structure_3.h>
00112 #include <cassert>
00113 #include <vector>
00114 #include <fstream>
00115 
00116 // use to translate in Pov-Ray
00117 #include <CGAL/Polyhedron_3.h>
00118 #include <CGAL/convex_hull_3.h>
00119         
00121 #endif
00122 
00123 #define DELAUNAY_POINT_CELL             0
00124 #define DELAUNAY_EDGE_CELL              1
00125 #define DELAUNAY_FACET_CELL             2
00126 #define DELAUNAY_TETRA_CELL             3
00127 
00136 class SkinSurface: public Surface
00137 {
00138 
00139 #ifdef ENABLE_CGAL 
00140 private:
00141         
00142         typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
00143         typedef CGAL::Regular_triangulation_filtered_traits_3<K>    Traits;
00144         typedef CGAL::Triangulation_cell_base_with_info_3< Del3Cell*,Traits> Cell_with_info;
00145         typedef CGAL::Regular_triangulation_cell_base_3<Traits,Cell_with_info> RT_Cell_with_info;
00146         typedef CGAL::Triangulation_vertex_base_3<Traits> Vertex;
00147         typedef CGAL::Triangulation_data_structure_3<Vertex,RT_Cell_with_info> tds;
00148         typedef Traits::RT                                          Weight;
00149         typedef Traits::Bare_point                                  Point3;
00150         typedef Traits::Weighted_point                              Weighted_point;
00151         typedef CGAL::Regular_triangulation_3<Traits,tds>           Rt;
00152         typedef Rt::Vertex_iterator                                 Vertex_iterator;
00153         typedef Rt::Finite_vertices_iterator                        Finite_Vertex_Iterator;
00154         typedef Rt::Finite_cells_iterator                                                       Finite_Cells_Iterator;
00155         typedef Rt::Finite_edges_iterator                                                       Finite_Edges_Iterator;
00156         typedef Rt::Finite_facets_iterator                                                      Finite_Facets_Iterator;
00157         typedef Rt::Vertex_handle                                   Vertex_handle;
00158         typedef tds::Cell_circulator                                                            Cell_circulator;
00159         typedef tds::Cell_handle                                                                        Cell_handle;
00160         typedef Rt::Facet                                                                                       Facet;
00161 
00162         // used to translate in Pov-Ray
00163         typedef CGAL::Polyhedron_3<K>                                           Polyhedron;
00164         typedef Polyhedron::Halfedge_around_facet_circulator        HF_circulator;
00165 
00166         //a functor computing the plane containing a triangular facet
00167         struct Plane_from_facet {
00168                 Polyhedron::Plane_3 operator()(Polyhedron::Facet& f) {
00169                 Polyhedron::Halfedge_handle h = f.halfedge();
00170                 return Polyhedron::Plane_3( h->vertex()->point(),
00171                                     h->next()->vertex()->point(),
00172                                     h->opposite()->vertex()->point());
00173         }
00174 };
00175 
00176 
00177 #endif
00178 
00179 private:
00181         int numMixedCells;
00187         int type[4];
00189         int* gridMixedCellMap;
00191         int nx,ny,nz;
00192         double scale;
00193         double side;
00194         double*x,*y,*z;
00195         double s;
00196         double xmin,xmax,ymin,ymax,zmin,zmax;
00197         short*** ind;
00198         int* gridMixedCellMap2D;
00199 
00200         unsigned int AUX_GRID_DIM_SKIN;
00201         unsigned int MAX_MIXEDCELLS;
00202         unsigned int AUX_GRID_DIM_SKIN_2D;
00203         unsigned int MAX_MIXEDCELLS_2D; 
00204 
00206         int nx_2d,ny_2d,nz_2d;
00207         double scale_2d;
00208         double side_2d;
00209         double xmin_2d,xmax_2d,ymin_2d,ymax_2d,zmin_2d,zmax_2d;
00210         int** ind_2d;
00212         vector<Del3Cell*> pendingCells;
00214         MixedCell** mixedComplex;       
00217         bool buildSkin();
00219         bool buildAuxiliaryGrid();
00220         #ifdef ENABLE_CGAL
00221 
00222                 bool buildSkinCGAL();
00223         #endif
00224 
00225         bool savePovRay;
00226 
00227 public:
00229         SkinSurface();
00231         SkinSurface(DelPhiShared* ds);                  
00232 
00234 
00235         virtual bool build();
00237         virtual bool save(char* fileName);
00239         virtual bool load(char* fileName);
00241         virtual void printSummary();            
00243         virtual bool getProjection(double p[3],double* proj1,double* proj2,
00244                 double* proj3,double* normal1,double* normal2,double* normal3);
00250         virtual void getRayIntersection(double p1[3],double p2[3],vector<pair<double,double*> >& intersections,int thdID);              
00252         virtual void preProcessPanel();
00253         virtual void postRayCasting();
00254         virtual bool preBoundaryProjection();
00256 
00257         void setShrinking(double ss) 
00258         { 
00259                 double e = 0.05;
00260                 if (ss<=(1.0-e) && ss>=(0.0+e)) 
00261                 { 
00262                         s=ss; 
00263                 }
00264                 else
00265                 {
00266                         cout << endl << WARN << "Cannot set " << ss << ". s parameter is in (0+e,1-e], where e is "<< e << ".Setting "<< DEFAULT_S;
00267                         s = DEFAULT_S;
00268                 }
00269         }
00270         
00271         double getShrinking() 
00272         {
00273                 return s;
00274         }
00275 
00276         void setSavePovRay(bool ss) 
00277         { 
00278                 savePovRay = ss;
00279         }
00280         
00281         bool getSavePovRay() 
00282         {
00283                 return savePovRay;
00284         }
00285 
00287         void setAuxGrid(unsigned int dim,unsigned int max)
00288         {               
00289                 AUX_GRID_DIM_SKIN = dim;
00290                 MAX_MIXEDCELLS = max;
00291         }
00292 
00296         void setAuxGrid2D(unsigned int dim,unsigned int max)
00297         {               
00298                 AUX_GRID_DIM_SKIN_2D = dim;
00299                 MAX_MIXEDCELLS_2D = (max*dim);
00300         }
00301         
00302         virtual ~SkinSurface();
00303 
00304 private:
00307         //bool rayQuadricIntersection(double*,double*,double**,double*,double*,int thdID,double* cache);
00308         bool rayQuadricIntersection(double*,double*,double*,double*,double*,int thdID,double* cache);
00310         bool isFeasible(MixedCell* mc,double* point);
00312         void projectToQuadric(double* y,double* Q,int type,double* proj,double* norm,double& dist);
00314         void saveSkinPatch(ofstream& of,MixedCell* mc,int index,vector<Weighted_point>& la);
00315 };
00316 
00317 #endif