MEPP2 Project
compute_normals_cgal_point_set.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2019 University of Lyon and CNRS (France).
2 // All rights reserved.
3 //
4 // This file is part of MEPP2; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published
6 // by the Free Software Foundation; either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
10 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11 
12 #if 1
13 
14 // *** MEPP2 code calling CGAL native filters. ***
15 // *** Point and normal stored in a CGAL::Point_set_3. ***
16 
18  // for FEVV::CGALPointSet
20  // for boost::graph_traits< FEVV::CGALPointSet >
22  // for FEVV::RetrieveKernel< FEVV::CGALPointSet >
24  // for get(FEVV::CGALPointSetPointMap&, ...)
26  // for FEVV::PMap_traits< FEVV::CGALPointSet > and vector-property-maps
27 
29  // for FEVV::Filters::read_mesh< FEVV::CGALPointSet >
31  // for FEVV::Filters::write_mesh< FEVV::CGALPointSet >
32 
33 #if defined _MSC_VER
34 #pragma warning(push)
35 #pragma warning(disable : 4172)
36 #endif
37 
38 #include <CGAL/pca_estimate_normals.h>
39 #include <CGAL/mst_orient_normals.h>
40 
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 
45 // main
46 int main(int argc, char *argv[])
47 {
48  // parse arguments
49  if(argc != 3)
50  {
51  std::cout << "Usage: " << argv[0]
52  << " input_mesh_filename output_mesh_filename" << std::endl;
53  std::cout << "Example: " << argv[0] << " ../Testing/Data/tetra.xyz tetra.out.ply"
54  << std::endl;
55  return EXIT_FAILURE;
56  }
57  std::string input_file = argv[1];
58  std::string output_file = argv[2];
59 
60  //----------------------------------
61 
62  // create point cloud
63  typedef FEVV::CGALPointSet PointCloudT;
64  PointCloudT pc;
65 
66  // load point cloud
67  std::cout << "load point cloud" << std::endl;
68  FEVV::PMapsContainer pmaps_bag;
69  FEVV::Filters::read_mesh(input_file, pc, pmaps_bag);
70 
71  //----------------------------------
72 
73  // retrieve Point Map
74  auto pm = get(boost::vertex_point, pc);
75 
76  // create vertex-normal property map
77  using VertexNormalMap = typename FEVV::PMap_traits< FEVV::vertex_normal_t,
78  PointCloudT >::pmap_type;
79  VertexNormalMap v_nm;
80  std::cout << "create vertex-normal map" << std::endl;
82 
83  // store property map in property maps bag
84  put_property_map(FEVV::vertex_normal, pc, pmaps_bag, v_nm);
85 
86  // compute normals
87  std::cout << "compute normals" << std::endl;
88  //typedef typename boost::property_traits< VertexNormalMap >::value_type Normal;
89  // Estimates normals direction.
90  // Note: pca_estimate_normals() requiresa range of points
91  // as well as property maps to access each point's position and normal.
92  const int nb_neighbors = 18; // K-nearest neighbors = 3 rings
93  CGAL::pca_estimate_normals< CGAL::Sequential_tag >(
94  pc,
95  nb_neighbors,
96  CGAL::parameters::point_map(pm).normal_map(v_nm));
97 
98  // Orients normals.
99  // Note: mst_orient_normals() requires a range of points
100  // as well as property maps to access each point's position and normal.
101  std::cout << "orient normals" << std::endl;
102  //auto /*indices_range_iterator*/ unoriented_points_begin =
103  CGAL::mst_orient_normals(
104  pc,
105  nb_neighbors,
106  CGAL::parameters::point_map(pm).normal_map(v_nm));
107 
108  //----------------------------------
109 
110  // save point cloud with normals
111  std::cout << "save point cloud with normals" << std::endl;
112  FEVV::Filters::write_mesh(output_file, pc, pmaps_bag);
113 
114  return 0;
115 }
116 
117 #else
118 
119 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
120 #include <CGAL/pca_estimate_normals.h>
121 #include <CGAL/mst_orient_normals.h>
122 #include <CGAL/property_map.h>
123 #include <CGAL/IO/read_xyz_points.h>
124 #include <CGAL/IO/write_xyz_points.h>
125 #include <utility> // defines std::pair
126 #include <list>
127 #include <fstream>
128 
129 // *** CGAL Native code. ***
130 // *** Point with normal vector stored in a std::pair. ***
131 
132 // Types
133 typedef CGAL::Exact_predicates_inexact_constructions_kernel Kernel;
134 typedef Kernel::Point_3 Point;
135 typedef Kernel::Vector_3 Vector;
136 // Point with normal vector stored in a std::pair.
137 typedef std::pair< Point, Vector > PointVectorPair;
138 
139 // Concurrency
140 #ifdef CGAL_LINKED_WITH_TBB
141 typedef CGAL::Parallel_tag Concurrency_tag;
142 #else
143 typedef CGAL::Sequential_tag Concurrency_tag;
144 #endif
145 
146 int main(int argc, char*argv[])
147 {
148  // parse arguments
149  if(argc != 3)
150  {
151  std::cout << "Usage: " << argv[0]
152  << " input_mesh_filename output_mesh_filename" << std::endl;
153  std::cout << "Example: " << argv[0] << " ../Testing/Data/tetra.xyz tetra.xyz.with_normals.ply"
154  << std::endl;
155  return EXIT_FAILURE;
156  }
157  std::string input_file = argv[1];
158  std::string output_file = argv[2];
159 
160  // Reads a .xyz point set file in points[].
161  std::cout << "load point cloud" << std::endl;
162  std::list< PointVectorPair > points;
163  std::ifstream stream(input_file);
164  if(!stream || !CGAL::read_xyz_points(
165  stream,
166  std::back_inserter(points),
167  CGAL::parameters::point_map(
168  CGAL::First_of_pair_property_map< PointVectorPair >())))
169  {
170  std::cerr << "Error: cannot read file " << input_file << std::endl;
171  return EXIT_FAILURE;
172  }
173 
174  // Estimates normals direction.
175  // Note: pca_estimate_normals() requiresa range of points
176  // as well as property maps to access each point's position and normal.
177  std::cout << "compute normals" << std::endl;
178  const int nb_neighbors = 18; // K-nearest neighbors = 3 rings
179  CGAL::pca_estimate_normals< Concurrency_tag >(
180  points,
181  nb_neighbors,
182  CGAL::parameters::point_map(
183  CGAL::First_of_pair_property_map< PointVectorPair >())
184  .normal_map(CGAL::Second_of_pair_property_map< PointVectorPair >()));
185 
186  // Orients normals.
187  // Note: mst_orient_normals() requires a range of points
188  // as well as property maps to access each point's position and normal.
189  std::cout << "orient normals" << std::endl;
190  std::list< PointVectorPair >::iterator unoriented_points_begin =
191  CGAL::mst_orient_normals(
192  points,
193  nb_neighbors,
194  CGAL::parameters::point_map(
195  CGAL::First_of_pair_property_map< PointVectorPair >())
196  .normal_map(
197  CGAL::Second_of_pair_property_map< PointVectorPair >()));
198 
199  // Optional: delete points with an unoriented normal
200  // if you plan to call a reconstruction algorithm that expects oriented
201  // normals.
202  //points.erase(unoriented_points_begin, points.end());
203 
204  // Saves point set.
205  // Note: write_xyz_points() requires property maps to access each
206  // point position and normal.
207  std::cout << "save point cloud with normals" << std::endl;
208  std::ofstream out(output_file);
209  out.precision(16);
210  if(!out ||
211  !CGAL::write_xyz_points(
212  out,
213  points,
214  CGAL::parameters::point_map(
215  CGAL::First_of_pair_property_map< PointVectorPair >())
216  .normal_map(
217  CGAL::Second_of_pair_property_map< PointVectorPair >())))
218  {
219  return EXIT_FAILURE;
220  }
221 
222  return EXIT_SUCCESS;
223 }
224 
225 #endif
FEVV::put_property_map
void put_property_map(PropertyT p, const MeshT &, PMapsContainer &pmaps, const typename PMap_traits< PropertyT, MeshT >::pmap_type &pmap)
Definition: properties.h:664
Vector
AIFMesh::Vector Vector
Definition: Graph_properties_aif.h:22
FEVV::CGALPointSet
CGAL::Point_set_3< CGALPointSetPoint > CGALPointSet
Definition: DataStructures_cgal_point_set.h:71
DataStructures_cgal_point_set.h
main
int main(int argc, char *argv[])
Definition: compute_normals_cgal_point_set.cpp:46
Geometry_traits_cgal_point_set.h
Point
AIFMesh::Point Point
Definition: Graph_properties_aif.h:21
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
Kernel
CGAL::Cartesian< double > Kernel
Definition: test_complying_concepts_linear_cell_complex.cpp:22
FEVV::vertex_normal_t
vertex_normal_t
Definition: properties.h:35
cgal_point_set_writer.hpp
FEVV::Filters::read_mesh
void read_mesh(const std::string &filename, FEVV::CGALPointSet &g, PMapsContainer &pmaps, bool=false)
Load mesh from file.
Definition: cgal_point_set_reader.hpp:110
Graph_traits_cgal_point_set.h
cgal_point_set_reader.hpp
FEVV::Filters::write_mesh
void write_mesh(const std::string &filename, FEVV::CGALPointSet &g, PMapsContainer &pmaps)
Write mesh to file.
Definition: cgal_point_set_writer.hpp:42
boost::get
boost::property_map< FEVV::DataStructures::AIF::AIFMesh, boost::vertex_index_t >::const_type get(const boost::vertex_index_t &, const FEVV::DataStructures::AIF::AIFMesh &)
Returns the vertex index property map of the mesh.
Definition: Graph_properties_aif.h:108
FEVV::_PMap_traits
Definition: properties.h:376
properties_cgal_point_set.h
FEVV::vertex_normal
@ vertex_normal
Definition: properties.h:35
FEVV::make_property_map
PMap_traits< PropertyT, MeshT >::pmap_type make_property_map(PropertyT, const MeshT &m)
Definition: properties.h:630
Graph_properties_cgal_point_set.h