MEPP2 Project
example_property_maps.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 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 
13 // print property map content
14 template< typename PointCloud, typename VertexPropertyMap >
15 void
16 print_property_map(const PointCloud &pc,
17  const VertexPropertyMap &pmap,
18  const std::string &property_map_name)
19 {
20  std::cout << "property map " << property_map_name << ":" << std::endl;
21 
22  int counter = 0;
23  auto iterator_pair = vertices(pc);
24  auto vi = iterator_pair.first;
25  auto vi_end = iterator_pair.second;
26  for(; vi != vi_end; ++vi)
27  {
28  counter++;
29  auto value = get(pmap, *vi);
30  std::cout << " vertex #" << counter << ": " << value << std::endl;
31  }
32 }
33 
34 
35 // example of property maps use
36 template< typename PointCloudT >
37 int
38 example_property_maps(int argc, char *argv[])
39 {
40  // parse arguments
41  if(argc != 3)
42  {
43  std::cout << "Usage: " << argv[0]
44  << " input_mesh_filename output_mesh_filename" << std::endl;
45  std::cout << "Example: " << argv[0]
46  << " ../Testing/Data/tetra.xyz tetra.out.ply" << std::endl;
47  return EXIT_FAILURE;
48  }
49  std::string input_file = argv[1];
50  std::string output_file = argv[2];
51 
52  //----------------------------------
53 
54  // create point cloud
55  PointCloudT pc;
56 
57  // load point cloud
58  FEVV::PMapsContainer pmaps_bag;
59  FEVV::Filters::read_mesh(input_file, pc, pmaps_bag);
60 
61  // print geometry
62  print_property_map(pc, get(boost::vertex_point, pc), "PointMap");
63 
64  //----------------------------------
65 
66  // create vertex-normal property map
67  using VertexNormalMap = typename FEVV::PMap_traits< FEVV::vertex_normal_t,
68  PointCloudT >::pmap_type;
69  VertexNormalMap v_nm;
70  if(has_map(pmaps_bag, FEVV::vertex_normal))
71  {
72  std::cout << "use existing vertex-normal map" << std::endl;
73  v_nm = get_property_map(FEVV::vertex_normal, pc, pmaps_bag);
74  print_property_map(pc, v_nm, "VertexNormalMap");
75  }
76  else
77  {
78  std::cout << "create vertex-normal map" << std::endl;
80  // store property map in property maps bag
81  put_property_map(FEVV::vertex_normal, pc, pmaps_bag, v_nm);
82  }
83 
84  // populate vertex normal map
85  typedef typename boost::property_traits< VertexNormalMap >::value_type Normal;
86 
87  std::cout << "populate vertex-normal map" << std::endl;
88  int counter = 0;
89  auto iterator_pair = vertices(pc);
90  auto vi = iterator_pair.first;
91  auto vi_end = iterator_pair.second;
92  for(; vi != vi_end; ++vi)
93  {
94  counter++;
95  std::cout << " processing vertex #" << counter << std::endl;
96 
97  // write property map data
98  Normal new_normal(42.1, 42.2, 42.3);
99  put(v_nm, *vi, new_normal);
100  }
101 
102  //----------------------------------
103 
104  // retrieve or create vertex-color property map
105  using VertexColorMap = typename FEVV::PMap_traits< FEVV::vertex_color_t,
106  PointCloudT >::pmap_type;
107  VertexColorMap v_cm;
108  if(has_map(pmaps_bag, FEVV::vertex_color))
109  {
110  std::cout << "use existing vertex-color map" << std::endl;
111  v_cm = get_property_map(FEVV::vertex_color, pc, pmaps_bag);
112  print_property_map(pc, v_cm, "VertexColorMap");
113  }
114  else
115  {
116  std::cout << "create vertex-color map" << std::endl;
118  // store property map in property maps bag
119  put_property_map(FEVV::vertex_color, pc, pmaps_bag, v_cm);
120  }
121 
122  // populate vertex color map
123  typedef typename boost::property_traits< VertexColorMap >::value_type Color;
124  //typedef boost::graph_traits< PointCloudT > GraphTraits;
125  //typedef typename GraphTraits::vertex_iterator vertex_iterator;
126 
127  std::cout << "populate vertex-color map" << std::endl;
128  counter = 0;
129  iterator_pair = vertices(pc);
130  vi = iterator_pair.first;
131  vi_end = iterator_pair.second;
132  for(; vi != vi_end; ++vi)
133  {
134  counter++;
135  std::cout << " processing vertex #" << counter << std::endl;
136 
137  // write property map data
138  Color newcolor(8, 2, 1);
139  put(v_cm, *vi, newcolor);
140  }
141 
142  //----------------------------------
143 
144  // create 2 non-standard prop maps
145  auto vertex_int_map = FEVV::make_vertex_property_map< PointCloudT, int >(pc);
146  auto vertex_color2_map =
147  FEVV::make_vertex_property_map< PointCloudT, Color >(pc);
148 
149  // populate non-standard prop maps
150  std::cout << "populate vertex_int_map and vertex_color2_map (non-standard "
151  "prop maps)"
152  << std::endl;
153  counter = 0;
154  iterator_pair = vertices(pc);
155  vi = iterator_pair.first;
156  vi_end = iterator_pair.second;
157  for(; vi != vi_end; ++vi)
158  {
159  counter++;
160  std::cout << " processing vertex #" << counter << std::endl;
161 
162  // write property map data
163  Color newcolor((counter*3) % 255 , (counter*3 + 1) % 255, (counter*3 + 2) % 255);
164  put(vertex_color2_map, *vi, newcolor);
165 
166  put(vertex_int_map, *vi, 10 + counter);
167  }
168 
169  // print non-standard property map content
170  print_property_map(pc, vertex_int_map, "non-standard vertex_int_map");
171  print_property_map(pc, vertex_color2_map, "non-standard vertex_color2_map");
172 
173  //----------------------------------
174 
175  // save point cloud with normal and color
176  FEVV::Filters::write_mesh(output_file, pc, pmaps_bag);
177 
178  return 0;
179 }
FEVV::DataStructures::AIF::vertices
std::pair< typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_iterator, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_iterator > vertices(const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns the iterator range of the vertices of the mesh.
Definition: Graph_traits_aif.h:172
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
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
FEVV::has_map
bool has_map(const PMapsContainer &pmaps, const std::string &map_name)
(refer to Property Maps API)
Definition: properties.h:103
print_property_map
void print_property_map(const PointCloud &pc, const VertexPropertyMap &pmap, const std::string &property_map_name)
Definition: example_property_maps.hpp:16
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::vertex_color_t
vertex_color_t
Definition: properties.h:47
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
FEVV::vertex_color
@ vertex_color
Definition: properties.h:47
example_property_maps
int example_property_maps(int argc, char *argv[])
Definition: example_property_maps.hpp:38
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
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
FEVV::DataStructures::AIF::put
void put(const typename FEVV::DataStructures::AIF::PropertyMap< ValueT > &pm, KeyT k, const ValueT &v)
Specialization of put(pmap, key, value) for AIF.
Definition: Graph_properties_aif.h:214