MEPP2 Project
cgal_point_set_writer.hpp
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 Lesser General Public License as
6 // published by the Free Software Foundation; either version 3 of
7 // the License, 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 #pragma once
12 
15 
17 
18 #include "FEVV/Tools/IO/FileUtilities.hpp" // for FileUtils::has_extension()
19 
20 #include <CGAL/Point_set_3/IO.h>
21 #include <CGAL/IO/write_ply_points.h>
22 #if 0 //ELO-note: doesn't compile, extra dependency needed
23 #include <CGAL/IO/write_las_points.h>
24 #endif
25 
26 #include <stdexcept> // for std::invalid_argument
27 
28 
29 namespace FEVV {
30 namespace Filters {
31 
32 
40 inline
41 void
43  const std::string &filename,
45  PMapsContainer &pmaps)
46 {
47  bool success = false;
48 
49  // init output file
50  std::ofstream out(filename);
51  if(! out)
52  {
53  throw std::invalid_argument(
54  "write_mesh() error: can not open output file " + filename);
55  }
56  //out.precision(16);
57 
58  // save point cloud
59  if(FEVV::FileUtils::has_extension(filename, ".xyz"))
60  {
61  // colors are not supported with this file format
62 
63  // write geometry and normals if present
64  success = CGAL::write_xyz_point_set(out, g);
65  }
66  else if(FEVV::FileUtils::has_extension(filename, ".off"))
67  {
68  // colors are not supported with this file format
69 
70  // write geometry and normals if present
71  success = CGAL::write_off_point_set(out, g);
72  }
73  else if(FEVV::FileUtils::has_extension(filename, ".ply"))
74  {
75  // retrieve property maps
76  using VertexNormalMap =
78  FEVV::CGALPointSet >::pmap_type;
79  using VertexColorMap =
81  FEVV::CGALPointSet >::pmap_type;
82 
83  VertexNormalMap v_nm;
84  bool has_normal = has_map(pmaps, vertex_normal);
85  if(has_normal)
86  v_nm = get_property_map(FEVV::vertex_normal, g, pmaps);
87 
88  VertexColorMap v_cm;
89  bool has_color = has_map(pmaps, vertex_color);
90  if(has_color)
91  v_cm = get_property_map(FEVV::vertex_color, g, pmaps);
92 
93  auto pm = get(boost::vertex_point, g);
94 
95  // set file mode
96  CGAL::set_ascii_mode(out);
97 
98  // write to file
99  if(has_normal && has_color)
100  {
101  // geometry + normal + color
102  success = CGAL::write_ply_points_with_properties(
103  out,
104  g,
105  CGAL::make_ply_point_writer(pm),
106  CGAL::make_ply_normal_writer(v_nm),
107  std::make_tuple(v_cm,
108  CGAL::PLY_property< unsigned char >("red"),
109  CGAL::PLY_property< unsigned char >("green"),
110  CGAL::PLY_property< unsigned char >("blue")));
111  }
112  else if(has_normal)
113  {
114  // geometry + normal
115  success = CGAL::write_ply_points_with_properties(
116  out,
117  g,
118  CGAL::make_ply_point_writer(pm),
119  CGAL::make_ply_normal_writer(v_nm));
120  }
121  else if(has_color)
122  {
123  // geometry + color
124  success = CGAL::write_ply_points_with_properties(
125  out,
126  g,
127  CGAL::make_ply_point_writer(pm),
128  std::make_tuple(v_cm,
129  CGAL::PLY_property< unsigned char >("red"),
130  CGAL::PLY_property< unsigned char >("green"),
131  CGAL::PLY_property< unsigned char >("blue")));
132  }
133  else
134  {
135  // geometry only
136  success = CGAL::write_ply_points_with_properties(
137  out,
138  g,
139  CGAL::make_ply_point_writer(pm));
140  }
141  }
142 #if 0 //ELO-note: doesn't compile, extra dependency needed
143  else if(FEVV::FileUtils::has_extension(filename, ".las") ||
144  FEVV::FileUtils::has_extension(filename, ".laz"))
145  {
146  success = CGAL::write_las_points(out, points);
147  }
148 #endif
149 
150  if(! success)
151  {
152  throw std::invalid_argument(
153  "write_mesh() error: failed to write mesh to file " + filename);
154  }
155 }
156 
157 
158 } // namespace Filters
159 } // namespace FEVV
160 
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
Wrappings_cgal_point_set.h
FEVV::CGALPointSet
CGAL::Point_set_3< CGALPointSetPoint > CGALPointSet
Definition: DataStructures_cgal_point_set.h:71
DataStructures_cgal_point_set.h
FEVV::has_map
bool has_map(const PMapsContainer &pmaps, const std::string &map_name)
(refer to Property Maps API)
Definition: properties.h:103
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
FEVV::vertex_normal_t
vertex_normal_t
Definition: properties.h:35
FEVV::get
FEVV::PCLPointCloudPointMap::value_type get(const FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key)
Specialization of get(point_map, key) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:117
FEVV::vertex_color_t
vertex_color_t
Definition: properties.h:47
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::vertex_color
@ vertex_color
Definition: properties.h:47
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
FEVV::FileUtils::has_extension
bool has_extension(const std::string &file_name)
Definition: FileUtilities.hpp:58
FEVV::_PMap_traits
Definition: properties.h:376
properties.h
FileUtilities.hpp
FEVV::vertex_normal
@ vertex_normal
Definition: properties.h:35