Allink  v0.1
ElPolyDrawSurfCGAL.cpp
1 /***********************************************************************
2 ElPoly:This progam provide a graphical visualisation of the data
3 opend by VarData using openGL glut. The most important option are
4 the possibility of changing the backfold of the polymers with 'c',
5 see the subsequent file in the list with '>', see the bond with 'b'.
6 Copyright (C) 2008-2010 by Giovanni Marelli <sabeiro@virgilio.it>
7 
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; If not, see <http://www.gnu.org/licenses/>.
21 ***********************************************************************/
22 #include "ElPoly.h"
23 
24 #ifdef __glut_h__
25 extern Draw *Dr;
26 //#ifndef __CGAL_h__
27 #ifdef USE_CGAL
28 #include <CGAL/trace.h>
29 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
30 #include <CGAL/Polyhedron_3.h>
31 #include <CGAL/IO/Polyhedron_iostream.h>
32 #include <CGAL/Surface_mesh_default_triangulation_3.h>
33 #include <CGAL/make_surface_mesh.h>
34 #include <CGAL/Implicit_surface_3.h>
35 #include <CGAL/IO/output_surface_facets_to_polyhedron.h>
36 #include <CGAL/Poisson_reconstruction_function.h>
37 #include <CGAL/Point_with_normal_3.h>
38 #include <CGAL/property_map.h>
39 #include <CGAL/IO/read_xyz_points.h>
40 #include <CGAL/compute_average_spacing.h>
41 
42 #include <vector>
43 #include <fstream>
44 
45 // Types
46 typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
47 typedef Kernel::FT FT;
48 typedef Kernel::Point_3 Point;
49 typedef CGAL::Point_with_normal_3<Kernel> Point_with_normal;
50 typedef Kernel::Sphere_3 Sphere;
51 typedef std::vector<Point_with_normal> PointList;
52 typedef CGAL::Polyhedron_3<Kernel> Polyhedron;
53 typedef CGAL::Poisson_reconstruction_function<Kernel> Poisson_reconstruction_function;
54 typedef CGAL::Surface_mesh_default_triangulation_3 STr;
55 typedef CGAL::Surface_mesh_complex_2_in_triangulation_3<STr> C2t3;
56 typedef CGAL::Implicit_surface_3<Kernel, Poisson_reconstruction_function> Surface_3;
57 
59  // }
60  // int main(void)
61  // {
62  // Poisson options
63  FT sm_angle = 20.0; // Min triangle angle in degrees.
64  FT sm_radius = 30; // Max triangle size w.r.t. point set average spacing.
65  FT sm_distance = 0.375; // Surface Approximation error w.r.t. point set average spacing.
66 
67  // Reads the point set file in points[].
68  // Note: read_xyz_points_and_normals() requires an iterator over points
69  // + property maps to access each point's position and normal.
70  // The position property map can be omitted here as we use iterators over Point_3 elements.
71  PointList points;
72  std::ifstream stream("data/kitten.xyz");
73  if (!stream ||
74  !CGAL::read_xyz_points_and_normals(
75  stream,
76  std::back_inserter(points),
77  CGAL::make_normal_of_point_with_normal_pmap(std::back_inserter(points))))
78  {
79  std::cerr << "Error: cannot read file data/kitten.xyz" << std::endl;
80  return;
81  }
82 
83  // Creates implicit function from the read points using the default solver (TAUCS).
84  // Note: this method requires an iterator over points
85  // + property maps to access each point's position and normal.
86  // The position property map can be omitted here as we use iterators over Point_3 elements.
87  Poisson_reconstruction_function function(
88  points.begin(), points.end(),
89  CGAL::make_normal_of_point_with_normal_pmap(points.begin()));
90 
91  // Computes the Poisson indicator function f()
92  // at each vertex of the triangulation.
93  if ( ! function.compute_implicit_function() )
94  return;
95 
96  // Computes average spacing
97  FT average_spacing = CGAL::compute_average_spacing(points.begin(), points.end(),
98  6 /* knn = 1 ring */);
99 
100  // Gets one point inside the implicit surface
101  // and computes implicit function bounding sphere radius.
102  Point inner_point = function.get_inner_point();
103  Sphere bsphere = function.bounding_sphere();
104  FT radius = std::sqrt(bsphere.squared_radius());
105 
106  // Defines the implicit surface: requires defining a
107  // conservative bounding sphere centered at inner point.
108  FT sm_sphere_radius = 5.0 * radius;
109  FT sm_dichotomy_error = sm_distance*average_spacing/1000.0; // Dichotomy error must be << sm_distance
110  Surface_3 surface(function,
111  Sphere(inner_point,sm_sphere_radius*sm_sphere_radius),
112  sm_dichotomy_error/sm_sphere_radius);
113 
114  // Defines surface mesh generation criteria
115  CGAL::Surface_mesh_default_criteria_3<STr> criteria(sm_angle, // Min triangle angle (degrees)
116  sm_radius*average_spacing, // Max triangle size
117  sm_distance*average_spacing); // Approximation error
118 
119  // Generates surface mesh with manifold option
120  STr tr; // 3D Delaunay triangulation for surface mesh generation
121  C2t3 c2t3(tr); // 2D complex in 3D Delaunay triangulation
122  CGAL::make_surface_mesh(c2t3, // reconstructed mesh
123  surface, // implicit surface
124  criteria, // meshing criteria
125  CGAL::Manifold_with_boundary_tag()); // require manifold mesh
126 
127  if(tr.number_of_vertices() == 0)
128  return ;
129 
130  // saves reconstructed surface mesh
131  std::ofstream out("kitten_poisson-20-30-0.375.off");
132  Polyhedron output_mesh;
133  CGAL::output_surface_facets_to_polyhedron(c2t3, output_mesh);
134  out << output_mesh;
135 
136  return ;
137 }
138 #else
139 void ElPoly::DefineSurf(){
140  printf("DefSurface without CGAL not implemented\n");
141 }
142 #endif // __CGAL_h__
143 #endif //__glut_h__
Draw provides the basic configuration of the openGL libraries used in every derived program...
Definition: Draw.h:15
void DefineSurf()
Find the covering surface for given points.