MEPP2 Project
example_CMDM.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2020 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 
14 
15 #include "FEVV/Filters/Generic/color_mesh.h" // Required to visualize CMDM as a color map
16 #include "FEVV/Filters/Generic/minmax_map.h" // Required to visualize CMDM as a color map
17 
18 #include <chrono> //time computation
19 
20 //---------------------------------------------------------
21 // Main
22 //---------------------------------------------------------
23 
24 // Main: load a mesh, apply the filter, write the mesh
25 int
26 main(int narg, char **argv)
27 {
28  // input and output files
29  std::string input_original;
30  std::string input_degraded;
31 
32  std::string output_file_path = "cmdm.output.obj";
33 
34  std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();//Chrono start
35 
36  if(narg == 3)
37  {
38  std::string reader = std::string(argv[1]);
39  input_original = reader;
40  std::string reader2 = std::string(argv[2]);
41  input_degraded = reader2;
42  }
43  else
44  {
45  std::cout << "Use of " << argv[0] << " is :\n";
46  std::cout << "\t" << argv[0]
47  << " path/to/original_mesh path/to/degraded_mesh" << std::endl;
48  std::cout << argv[0]
49  << " will compute the CMDM value between the degraded mesh and "
50  "the original.\n"
51  << std::endl;
52  return 1;
53  }
54 
55  // create LUT
56  FEVV::Filters::ColorMeshLUT lut_courbure_clust = FEVV::Filters::make_LUT(false);
57 
58  // read mesh from file
59  FEVV::MeshSurface m_original;
60  FEVV::PMapsContainer pmaps_bag_original;
61  FEVV::Filters::read_mesh(input_original, m_original, pmaps_bag_original);
62 
63  FEVV::MeshSurface m_degraded;
64  FEVV::PMapsContainer pmaps_bag_degraded;
65  FEVV::Filters::read_mesh(input_degraded, m_degraded, pmaps_bag_degraded);
66 
67  auto pm_degrad = get(boost::vertex_point, m_degraded);
68  auto pm_original = get(boost::vertex_point, m_original);
69 
71  FaceNormalMap fnm_degrad;
72  if(has_map(pmaps_bag_degraded, FEVV::face_normal))
73  {
74  std::cout << "use existing face-normal map for degraded mesh" << std::endl;
75  fnm_degrad = get_property_map(FEVV::face_normal, m_degraded, pmaps_bag_degraded);
76  }
77  else
78  {
79  std::cout << "create face-normal map for degraded mesh" << std::endl;
80  fnm_degrad = make_property_map(FEVV::face_normal, m_degraded);
81  // store property map in property maps bag
82  put_property_map(FEVV::face_normal, m_degraded, pmaps_bag_degraded, fnm_degrad);
83  FEVV::Filters::calculate_face_normals(m_degraded, pm_degrad, fnm_degrad);
84  }
85 
86  FaceNormalMap fnm_original;
87  if(has_map(pmaps_bag_original, FEVV::face_normal))
88  {
89  std::cout << "use existing face-normal map for original mesh" << std::endl;
90  fnm_original = get_property_map(FEVV::face_normal, m_original, pmaps_bag_original);
91  }
92  else
93  {
94  std::cout << "create face-normal map for original mesh" << std::endl;
95  fnm_original = make_property_map(FEVV::face_normal, m_original);
96  // store property map in property maps bag
97  put_property_map(FEVV::face_normal, m_original, pmaps_bag_original, fnm_original);
98  FEVV::Filters::calculate_face_normals(m_original, pm_original, fnm_original);
99  }
100 
101  // Verify if the original and the degraded meshes have diffuse color maps
102  if(has_map(pmaps_bag_original, FEVV::vertex_color) &&
103  has_map(pmaps_bag_degraded, FEVV::vertex_color))
104  {
105  std::cout << "use existing color maps for original and degraded meshes"
106  << std::endl;
107  }
108  else
109  {
110  std::cout <<
111  "CMDM needs 2 meshes with diffuse colors.\nUse MSDM2 Plugin for "
112  "meshes having geometry characteristics only."
113  << std::endl;
114  return EXIT_FAILURE;
115  }
116 
117  // Using a vertex property map to store the local distortions and visualize them later (color map).
118  typedef typename FEVV::Vertex_pmap< FEVV::MeshSurface , double > vertex_cmdm_map;
119  vertex_cmdm_map cmdm_pmap_deg, cmdm_pmap_orig;
120 
121  if(FEVV::has_map(pmaps_bag_degraded, std::string("v:cmdm")))
122  {
123  cmdm_pmap_deg =
124  boost::any_cast< vertex_cmdm_map >(pmaps_bag_degraded.at("v:cmdm"));
125  }
126  else
127  {
128  cmdm_pmap_deg =
129  FEVV::make_vertex_property_map< FEVV::MeshSurface, double >(m_degraded);
130  pmaps_bag_degraded["v:cmdm"] = cmdm_pmap_deg;
131  }
132 
133  if(FEVV::has_map(pmaps_bag_original, std::string("v:cmdm")))
134  {
135  cmdm_pmap_orig =
136  boost::any_cast< vertex_cmdm_map >(pmaps_bag_original.at("v:cmdm"));
137  }
138  else
139  {
140  cmdm_pmap_orig =
141  FEVV::make_vertex_property_map< FEVV::MeshSurface, double >(m_original);
142  pmaps_bag_original["v:cmdm"] = cmdm_pmap_orig;
143  }
144 
145  int nb_levels = 3;
146  double cmdm_1_2, cmdm_2_1, cmdm;
147 
149  pm_degrad,
150  fnm_degrad,
151  pmaps_bag_degraded,
152  m_original,
153  pm_original,
154  fnm_original,
155  pmaps_bag_original,
156  nb_levels,
157  cmdm_pmap_deg,
158  cmdm_1_2);
159 
161  pm_original,
162  fnm_original,
163  pmaps_bag_original,
164  m_degraded,
165  pm_degrad,
166  fnm_degrad,
167  pmaps_bag_degraded,
168  nb_levels,
169  cmdm_pmap_orig,
170  cmdm_2_1);
171 
172  cmdm = (cmdm_1_2 + cmdm_2_1) / 2.;
173 
174  std::cout << "Calculated CMDM = " << cmdm << std::endl;
175 
176  // Visalize local distortions of the degraded mesh (color map of cmdm_1_2)
177  using VertexColorMap =
179  FEVV::MeshSurface >::pmap_type;
180  VertexColorMap v_cm =
181  get_property_map(FEVV::vertex_color, m_degraded, pmaps_bag_degraded);
182 
183  double max_cmdm_1_2, min_cmdm_1_2;
184 
186  cmdm_pmap_deg,
187  min_cmdm_1_2,
188  max_cmdm_1_2);
189 
191  cmdm_pmap_deg,
192  v_cm,
193  min_cmdm_1_2,
194  max_cmdm_1_2,
195  lut_courbure_clust);
196 
197  FEVV::Filters::write_mesh(output_file_path, m_degraded, pmaps_bag_degraded);
198 
199  std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now(); //Chrono end
200  double time = std::chrono::duration_cast< std::chrono::microseconds > (end - begin).count() / 1000000.0;
201  std::cout << time << " sec elapsed " << std::endl;
202 
203  return 0;
204 }
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::Filters::compute_min_max_vertices
void compute_min_max_vertices(const HalfedgeGraph &g, const PropertyMap &prop_map, MapType &min_metric, MapType &max_metric)
Compute min and max value of a numerical property map for the vertices of a mesh.
Definition: minmax_map.h:92
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
minmax_map.h
FEVV::has_map
bool has_map(const PMapsContainer &pmaps, const std::string &map_name)
(refer to Property Maps API)
Definition: properties.h:103
color_mesh.h
FEVV::MeshSurface
CGAL::Surface_mesh< CGALPoint > MeshSurface
Definition: DataStructures_cgal_surface_mesh.h:23
calculate_face_normals.hpp
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
FEVV::Filters::color_vertices_from_map
void color_vertices_from_map(const HalfedgeGraph &g, const PropertyMap &prop_map, ColorMap &color_pmap, const MapType min_metric, const MapType max_metric, const ColorMeshLUT &colors=make_LUT())
Fill the color map for the vertices of a mesh using a numerical property map.
Definition: color_mesh.h:211
main
int main(int narg, char **argv)
Definition: example_CMDM.cpp:26
cmdm.h
FEVV::Filters::make_LUT
ColorMeshLUT make_LUT(bool color_in_0_255=true, unsigned int colors_nbr=256, float h1=240, float h2=0)
Create a RGB LUT based on an HSV range. Default range creates a blue-cyan-green-yellow-red gradient.
Definition: color_mesh.h:41
FEVV::vertex_color_t
vertex_color_t
Definition: properties.h:47
FEVV::Filters::ColorMeshLUT
std::vector< float > ColorMeshLUT
Definition: color_mesh.h:25
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
FEVV::Filters::calculate_face_normals
void calculate_face_normals(const HalfedgeGraph &g, const PointMap &pm, FaceNormalMap fnm, const GeometryTraits &gt)
Calculate "some" normal of all the faces of the considered mesh and populate the argument provided Fa...
Definition: calculate_face_normals.hpp:40
FEVV::Vertex_pmap
typename Vertex_pmap_traits< MeshT, ValueT >::pmap_type Vertex_pmap
Definition: properties.h:607
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::Filters::process_CMDM_multires
double process_CMDM_multires(const HalfedgeGraph &m_poly_degrad, const PointMap &pm_degrad, const FaceNormalMap &fnm_degrad, FEVV::PMapsContainer &pmaps_degrad, const HalfedgeGraph &m_poly_original, const PointMap &pm_original, const FaceNormalMap &fnm_original, FEVV::PMapsContainer &pmaps_original, const int nb_level, VertexCMDMMap &cmdm_pmap, const double maxdim, double &CMDM_value)
Definition: cmdm.h:484
FEVV::make_property_map
PMap_traits< PropertyT, MeshT >::pmap_type make_property_map(PropertyT, const MeshT &m)
Definition: properties.h:630
FEVV::face_normal
@ face_normal
Definition: properties.h:77