Allink  v0.1
ElPolyDrawSurfCGAL.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/trace.h>
00029 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
00030 #include <CGAL/Polyhedron_3.h>
00031 #include <CGAL/IO/Polyhedron_iostream.h>
00032 #include <CGAL/Surface_mesh_default_triangulation_3.h>
00033 #include <CGAL/make_surface_mesh.h>
00034 #include <CGAL/Implicit_surface_3.h>
00035 #include <CGAL/IO/output_surface_facets_to_polyhedron.h>
00036 #include <CGAL/Poisson_reconstruction_function.h>
00037 #include <CGAL/Point_with_normal_3.h>
00038 #include <CGAL/property_map.h>
00039 #include <CGAL/IO/read_xyz_points.h>
00040 #include <CGAL/compute_average_spacing.h>
00041 
00042 #include <vector>
00043 #include <fstream>
00044 
00045 // Types
00046 typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
00047 typedef Kernel::FT FT;
00048 typedef Kernel::Point_3 Point;
00049 typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal;
00050 typedef Kernel::Sphere_3 Sphere;
00051 typedef std::vector<Point_with_normal> PointList;
00052 typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
00053 typedef CGAL::Poisson_reconstruction_function<Kernel> Poisson_reconstruction_function;
00054 typedef CGAL::Surface_mesh_default_triangulation_3 STr;
00055 typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3;
00056 typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3;
00057 
00058 void ElPoly::DefineSurf(){
00059   // }
00060   // int main(void)
00061   // {
00062   // Poisson options
00063   FT sm_angle = 20.0; // Min triangle angle in degrees.
00064   FT sm_radius = 30; // Max triangle size w.r.t. point set average spacing.
00065   FT sm_distance = 0.375; // Surface Approximation error w.r.t. point set average spacing.
00066 
00067   // Reads the point set file in points[].
00068   // Note: read_xyz_points_and_normals() requires an iterator over points
00069   // + property maps to access each point's position and normal.
00070   // The position property map can be omitted here as we use iterators over Point_3 elements.
00071   PointList points;
00072   std::ifstream stream("data/kitten.xyz");
00073   if (!stream ||
00074       !CGAL::read_xyz_points_and_normals(
00075                 stream,
00076                 std::back_inserter(points),
00077                 CGAL::make_normal_of_point_with_normal_pmap(std::back_inserter(points))))
00078     {
00079       std::cerr << "Error: cannot read file data/kitten.xyz" << std::endl;
00080       return;
00081     }
00082 
00083   // Creates implicit function from the read points using the default solver (TAUCS).
00084   // Note: this method requires an iterator over points
00085   // + property maps to access each point's position and normal.
00086   // The position property map can be omitted here as we use iterators over Point_3 elements.
00087   Poisson_reconstruction_function function(
00088                   points.begin(), points.end(),
00089                   CGAL::make_normal_of_point_with_normal_pmap(points.begin()));
00090 
00091   // Computes the Poisson indicator function f()
00092   // at each vertex of the triangulation.
00093   if ( ! function.compute_implicit_function() )
00094     return;
00095 
00096   // Computes average spacing
00097   FT average_spacing = CGAL::compute_average_spacing(points.begin(), points.end(),
00098                        6 /* knn = 1 ring */);
00099     
00100   // Gets one point inside the implicit surface
00101   // and computes implicit function bounding sphere radius.
00102   Point inner_point = function.get_inner_point();
00103   Sphere bsphere = function.bounding_sphere();
00104   FT radius = std::sqrt(bsphere.squared_radius());
00105 
00106   // Defines the implicit surface: requires defining a
00107   // conservative bounding sphere centered at inner point.
00108   FT sm_sphere_radius = 5.0 * radius;
00109   FT sm_dichotomy_error = sm_distance*average_spacing/1000.0; // Dichotomy error must be << sm_distance
00110   Surface_3 surface(function,
00111           Sphere(inner_point,sm_sphere_radius*sm_sphere_radius),
00112           sm_dichotomy_error/sm_sphere_radius);
00113 
00114   // Defines surface mesh generation criteria
00115   CGAL::Surface_mesh_default_criteria_3<STr> criteria(sm_angle,  // Min triangle angle (degrees)
00116                         sm_radius*average_spacing,  // Max triangle size
00117                         sm_distance*average_spacing); // Approximation error
00118 
00119   // Generates surface mesh with manifold option
00120   STr tr; // 3D Delaunay triangulation for surface mesh generation
00121   C2t3 c2t3(tr); // 2D complex in 3D Delaunay triangulation
00122   CGAL::make_surface_mesh(c2t3,                                 // reconstructed mesh
00123            surface,                              // implicit surface
00124            criteria,                             // meshing criteria
00125            CGAL::Manifold_with_boundary_tag());  // require manifold mesh
00126 
00127   if(tr.number_of_vertices() == 0)
00128     return ;
00129 
00130   // saves reconstructed surface mesh
00131   std::ofstream out("kitten_poisson-20-30-0.375.off");
00132   Polyhedron output_mesh;
00133   CGAL::output_surface_facets_to_polyhedron(c2t3, output_mesh);
00134   out << output_mesh;
00135 
00136   return ;
00137 }
00138 #else
00139 void ElPoly::DefineSurf(){
00140   printf("DefSurface without CGAL not implemented\n");
00141 }
00142 #endif // __CGAL_h__
00143 #endif //__glut_h__