MEPP2 Project
initializer_compression.h
Go to the documentation of this file.
1 // Copyright (c) 2012-2022 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 
12 #pragma once
13 
16 
19 
33 template< typename MeshT >
34 void
36  const MeshT &m,
37  FEVV::PMapsContainer &pmaps_bag,
41 
42 {
43  // Note: the property maps must be extracted from the
44  // property maps bag, and explicitly passed as
45  // parameters to the filter, in order to make
46  // clear what property is used by the filter.
47 
48  // Retrieve or create vertex-color property map.
49  if(has_map(pmaps_bag, FEVV::vertex_color))
50  {
51  std::cout << "use existing vertex-color map" << std::endl;
52  v_cm = get_property_map(FEVV::vertex_color, m, pmaps_bag);
53  }
54  else
55  {
56  std::cout << "create vertex-color map" << std::endl;
58  // Store property map in property maps bag.
59  put_property_map(FEVV::vertex_color, m, pmaps_bag, v_cm);
60  }
61  // Retrieve or create edge-color property map.
62  if(has_map(pmaps_bag, FEVV::edge_color))
63  {
64  std::cout << "using existing edge-color map" << std::endl;
65  e_cm = get_property_map(FEVV::edge_color, m, pmaps_bag);
66  }
67  else
68  {
69  std::cout << "create edge-color map" << std::endl;
71  // Store property map in property maps bag.
72  put_property_map(FEVV::edge_color, m, pmaps_bag, e_cm);
73  }
74  // Retrieve or create vertex-normal property map.
75  if(has_map(pmaps_bag, FEVV::vertex_normal))
76  {
77  std::cout << "use existing vertex-normal map" << std::endl;
78  v_nm = get_property_map(FEVV::vertex_normal, m, pmaps_bag);
79  }
80  else
81  {
82  std::cout << "create vertex-normal map" << std::endl;
84  // Store property map in property maps bag.
85  put_property_map(FEVV::vertex_normal, m, pmaps_bag, v_nm);
86  }
87 }
88 
95 template< typename MeshT >
96 int
97 progressive_compression_main(int argc, const char **argv)
98 {
99  int mode = 0;
100 
101  int number_batches = 70;
102  int min_number_vertices = -1;
103  int bits_quantization = 12;
104  std::string output_file_path_save_preprocess =
105  "progressive_compression_original_mesh_after_preprocess.obj"; // not used in this file
106  if(argc < 2)
107  {
108  std::cout << "Apply a progressive compression filter to the input mesh." << std::endl;
109  std::cout << "Usage: " << argv[0] << " input_mesh_filename" << std::endl;
110  std::cout << "Example: " << argv[0]
111  << " ../Testing/Data/CubeNonTriangleFaces.off" << std::endl;
112  return EXIT_FAILURE;
113  }
114 
115  std::string path_binary = "";
116  FEVV::Filters::BATCH_CONDITION batch_stop =
117  FEVV::Filters::BATCH_CONDITION::ALL_EDGES;
118  if(argc > 2)
119  mode = atoi(argv[2]);
120 
121  if(argc > 3)
122  {
123  path_binary = argv[3];
124  if (path_binary == ".")
125  {
126  std::cerr << "The binary path cannot be set to \".\". Therefore, it will be set to default value." << std::endl;
127  path_binary = "";
128  }
129  }
131  MeshT m;
132  FEVV::PMapsContainer pmaps_bag;
134  std::string output_path = "";
135  if (argc > 4)
136  {
137  output_path = argv[4];
138  if (output_path == ".")
139  {
140  std::cerr << "The output path path cannot be set to \".\". Therefore, it will be set to \"./\"." << std::endl;
141  output_path = "./";
142  }
143  }
144  else
145  {
146  output_path = typeid(m).name();
147  if (output_path.find("Surface_mesh") != std::string::npos)
148  {
149  output_path = "./Surface_mesh_";
150  }
151  else if (output_path.find("Polyhedron_3") != std::string::npos)
152  {
153  output_path = "./Polyhedron_3_";
154  }
155  else if (output_path.find("Linear_cell_complex") != std::string::npos)
156  {
157  output_path = "./LCC_";
158  }
159  else if (output_path.find("AIF") != std::string::npos)
160  {
161  output_path = "./AIF_";
162  }
163  else
164  {
165  output_path = "./Other_mesh_type_";
166  }
167  }
168 
169  FEVV::Filters::PREDICTION_TYPE prediction_type =
173  FEVV::Filters::VKEPT_POSITION::HALFEDGE;
174 
175  if (argc > 5)
176  {
177  int predint = atoi(argv[5]);
178  prediction_type = static_cast<FEVV::Filters::PREDICTION_TYPE>(predint);
179  }
180  if (argc > 6)
181  {
182  int metrint = atoi(argv[6]);
183  metric_type = static_cast<FEVV::Filters::METRIC_TYPE>(metrint);
184  }
185  if (argc > 7)
186  {
187  int vkeptint = atoi(argv[7]);
188  vkeptpos = static_cast<FEVV::Filters::VKEPT_POSITION>(vkeptint);
189  }
190  if (argc > 8)
191  number_batches = atoi(argv[8]);
192  if(argc > 9)
193  min_number_vertices = atoi(argv[9]);
194  if(argc > 10)
195  {
196  int batchint = atoi(argv[10]);
197  batch_stop = static_cast< FEVV::Filters::BATCH_CONDITION >(batchint);
198  }
199  if(argc > 11)
200  {
201  bits_quantization = atoi(argv[11]);
202  }
203  // Input and output files:
204  std::string input_file_path = argv[1];
205 #if(DEBUG)
206  std::string output_file_path = "progressive_compression_filter_base_mesh.obj";
207 #endif
208  // Read mesh from file.
209  if (mode != 1) {
210  FEVV::Filters::read_mesh(input_file_path, m, pmaps_bag);
211 
212  if (min_number_vertices == -1) {
213  // set it to 5% of init mesh vertices as in CPM
214  min_number_vertices = std::round(FEVV::size_of_vertices(m) * 0.05);
215  }
216  }
217 
221 
222  // Create or retrieve property maps.
224  m, pmaps_bag, v_cm, e_cm, v_nm);
225 
226  // Retrieve point property map (aka geometry)
227  auto pm = get(boost::vertex_point, m);
228 
229  auto gt_ = FEVV::Geometry_traits< MeshT >(m);
230  // Apply filter
231  if(mode == 0)
232  {
233  std::cout << "Single Compression" << std::endl;
235  prediction_type, vkeptpos, metric_type, true, false, bits_quantization);
237  // Compression algorithm call (Algorithm 1) //
240  pm,
241  v_cm,
242  e_cm,
243  // v_nm,
244  gt_,
245  params,
246  number_batches,
247  min_number_vertices,
248  batch_stop,
249  output_path,
250  path_binary,
251  true,
252  true,
253  false,
254  output_file_path_save_preprocess);
255 #if(DEBUG)
256  { // Write mesh to file: only write color maps (debug).
257  FEVV::PMapsContainer pmaps_bag_empty;
258  put_property_map(FEVV::edge_color, m, pmaps_bag_empty, e_cm);
259  put_property_map(FEVV::vertex_color, m, pmaps_bag_empty, v_cm);
260  FEVV::Filters::write_mesh(output_file_path, m, pmaps_bag_empty);
261  }
262 #endif
263  }
264  else if(mode == 1)
265  {
266  std::cout << "Measure mode, all metrics being tested" << std::endl;
267  std::vector< FEVV::Filters::PREDICTION_TYPE > available_predictions = {
269  FEVV::Filters::PREDICTION_TYPE::DELTA
270  //,FEVV::Filters::PREDICTION_TYPE::POSITION
271  };
272  std::vector< FEVV::Filters::VKEPT_POSITION > available_operators = {
273  FEVV::Filters::VKEPT_POSITION::MIDPOINT,
274  FEVV::Filters::VKEPT_POSITION::HALFEDGE
275  };
276  std::vector< FEVV::Filters::METRIC_TYPE > available_metrics = {
277  FEVV::Filters::METRIC_TYPE::EDGE_LENGTH,
280  };
281 
282  std::vector< int > tested_quantizations = {10, 12, 16};
283 
284  bool first = true;
285  int cpt = 0;
286  for(size_t i = 0; i < available_predictions.size(); i++)
287  {
288  for(size_t j = 0; j < available_operators.size(); j++)
289  {
290  for(size_t k = 0; k < available_metrics.size(); k++)
291  {
292  for(size_t l = 0; l < tested_quantizations.size(); l++)
293  {
294  MeshT mesh;
295  number_batches = 70;
296  FEVV::PMapsContainer pmaps_bag_second;
297  FEVV::Filters::read_mesh(input_file_path, mesh, pmaps_bag_second);
298 
299  if (first && (min_number_vertices == -1)) {
300  // set it to 5% of init mesh vertices as in CPM
301  min_number_vertices = std::round(FEVV::size_of_vertices(mesh) * 0.05);
302  first = false;
303  }
304 
306  v_cm2;
308  e_cm2;
310  MeshT >::pmap_type v_nm2;
311 
313  pmaps_bag_second,
314  v_cm2,
315  e_cm2,
316  v_nm2);
317 
318 
319  // Retrieve point property map (aka geometry).
320  auto pm2 = get(boost::vertex_point, mesh);
321 
322  FEVV::Filters::Parameters params(available_predictions[i],
323  available_operators[j],
324  available_metrics[k],
325  true,
326  false,
327  tested_quantizations[l]);
328 
329  path_binary = ""; // Must be "empty" in order to be set automatically.
331  // Compression algorithm call (Algorithm 1) //
334  pm2,
335  v_cm2,
336  e_cm2,
337  gt_,
338  params,
339  number_batches,
340  min_number_vertices,
341  batch_stop,
342  output_path,
343  path_binary,
344  true,
345  true,
346  false,
347  output_file_path_save_preprocess);
348 
349  ++cpt;
350  std::cout << "finished = " << static_cast<double>(cpt)/(available_predictions.size() * available_operators.size() * available_metrics.size() * tested_quantizations.size()) << std::endl;
351  }
352  }
353  }
354  }
355  }
356  else if(mode == 2)
357  {
358  compute_distortions(m, pm, argv[3]);
359  }
360 
361  return 0;
362 }
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
set_mesh_and_properties
void set_mesh_and_properties(const MeshT &m, FEVV::PMapsContainer &pmaps_bag, typename FEVV::PMap_traits< FEVV::vertex_color_t, MeshT >::pmap_type &v_cm, typename FEVV::PMap_traits< FEVV::edge_color_t, MeshT >::pmap_type &e_cm, typename FEVV::PMap_traits< FEVV::vertex_normal_t, MeshT >::pmap_type &v_nm)
Function to extract or create property map associated with a property map bag (for generic processing...
Definition: initializer_compression.h:35
FEVV::has_map
bool has_map(const PMapsContainer &pmaps, const std::string &map_name)
(refer to Property Maps API)
Definition: properties.h:103
generic_reader.hpp
FEVV::Filters::PREDICTION_TYPE
PREDICTION_TYPE
Definition: Parameters.h:19
FEVV::Filters::METRIC_TYPE
METRIC_TYPE
Definition: Parameters.h:33
progressive_compression_main
int progressive_compression_main(int argc, const char **argv)
A mesh type templated main(argc, argv) function that.
Definition: initializer_compression.h:97
FEVV::Geometry_traits
Refer to Geometry_traits_documentation_dummy for further documentation on provided types and algorith...
Definition: Geometry_traits.h:162
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
FEVV::vertex_normal_t
vertex_normal_t
Definition: properties.h:35
distortion_computing.h
FEVV::Filters::METRIC_TYPE::NO_METRIC
@ NO_METRIC
FEVV::edge_color
@ edge_color
Definition: properties.h:73
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::VKEPT_POSITION
VKEPT_POSITION
Definition: Parameters.h:28
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
generic_writer.hpp
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::Filters::progressive_compression_filter
void progressive_compression_filter(HalfedgeGraph &g, PointMap &pm, FEVV::Filters::Uniform_quantization< HalfedgeGraph, PointMap > &pq, VertexColorMap &v_cm, EdgeColorMap &e_cm, const GeometryTraits &gt, FEVV::Filters::Error_metric< HalfedgeGraph, PointMap > *EM, const FEVV::Filters::Kept_position< HalfedgeGraph, PointMap > *KP, FEVV::Filters::Predictor< HalfedgeGraph, PointMap > *predict, int nb_q_bits, int nb_max_batches, int nb_min_vertices, FEVV::Filters::BATCH_CONDITION batch_condition, draco::EncoderBuffer &buffer, const std::string &measure_path, bool preprocess=true, bool dequantiz=false, bool save_preprocess=false, const std::string &output_file_path_save_preprocess="", bool allow_duplicates=false)
Takes a mesh g, applies batches of simplification until either the number of max batches or the minim...
Definition: progressive_compression_filter.hpp:126
FEVV::DataStructures::AIF::AIFMesh
This class represents an AIF structure. AIF structure can deal with both manifold and non-manifold su...
Definition: AIFMesh.hpp:47
FEVV::_PMap_traits
Definition: properties.h:376
FEVV::vertex_normal
@ vertex_normal
Definition: properties.h:35
FEVV::Filters::Parameters
Parameters contains the compression parameters except the stopping criteria.
Definition: Parameters.h:46
FEVV::Filters::METRIC_TYPE::QEM_3D
@ QEM_3D
do not use a local metric (not implemented in this release)
FEVV::Filters::BATCH_CONDITION
BATCH_CONDITION
Definition: Batch_collapser.h:51
FEVV::Filters::PREDICTION_TYPE::POSITION
@ POSITION
FEVV::size_of_vertices
boost::graph_traits< MeshT >::vertices_size_type size_of_vertices(const MeshT &g)
Real current number of vertices of the mesh. Generic version.
Definition: Graph_traits_extension.h:29
FEVV::make_property_map
PMap_traits< PropertyT, MeshT >::pmap_type make_property_map(PropertyT, const MeshT &m)
Definition: properties.h:630
compute_distortions
void compute_distortions(HalfedgeGraph &g, PointMap &pm, fs::path const &root)
Definition: distortion_computing.h:51
progressive_compression_filter.hpp