MEPP2 Project
test_compression_valence.inl
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.
14 
18 
19 #include <iostream>
20 #include <string>
21 #include <cstdlib>
22 
23 
24 #ifdef UNIX
25 #include <unistd.h> // for getpid()
26 #include <fstream>
27 
28 void
29 get_vmpeak_vmsize(pid_t pid, unsigned int &vmpeak_kb, unsigned int &vmsize_kb)
30 {
31  std::string filename = "/proc/" + std::to_string(pid) + "/status";
32  std::ifstream file(filename);
33  if(!file.is_open())
34  {
35  std::cout << "Failed to open file '" << filename << "'" << std::endl;
36  return;
37  }
38 
39  std::string word;
40  while(true)
41  {
42  file >> word;
43  // DBG std::cout << "word=" << word << std::endl;
44 
45  if(file.eof())
46  break;
47 
48  if(word == "VmPeak:")
49  file >> vmpeak_kb;
50  else if(word == "VmSize:")
51  file >> vmsize_kb;
52  }
53 
54  file.close();
55 }
56 #endif // UNIX
57 
58 
59 template< typename MeshT >
60 int
61 test_compression_valence(int argc, const char **argv)
62 {
63  // parse arguments
64 
65  if(argc < 2)
66  {
67  std::cout << "Load a mesh then apply the Compression Valence filter."
68  << std::endl;
69  std::cout << "Usage: " << argv[0]
70  << " input_mesh_file [simplified_mesh_file "
71  "[reference_mesh_file]] [-withCompr] [-withQA] [-maxV nnn] "
72  " [-Qbits nnn] [-tolerance relative_tolerance]"
73  << std::endl;
74  std::cout << "Example: " << argv[0]
75  << " airplane.off airplane.out.obj airplane.ref.obj "
76  "-withCompr -maxV 500 -tolerance 1e-5"
77  << std::endl;
78  std::cout << "Example: " << argv[0]
79  << " airplane.off airplane.out.obj -withCompr -maxV 500 "
80  "-tolerance 1e-5"
81  << std::endl;
82  std::cout << "Example: " << argv[0]
83  << " airplane.off -withCompr -maxV 500 -tolerance 1e-5"
84  << std::endl;
85  return EXIT_FAILURE;
86  }
87 
88  std::string input_file_path;
89  std::string simplified_mesh_file_path;
90  std::string reference_file_path;
91  bool with_adaptative_quantization = false;
92  bool with_compression = false;
93  int quantiz_bits = 10;
94  int max_vertices = 100;
95  float relative_tolerance = 0.0f;
96 
97  input_file_path = argv[1];
98  int arg_read = 1;
99  if(argc >= 3 && argv[2][0] != '-')
100  {
101  simplified_mesh_file_path = argv[2];
102  arg_read = 2;
103  if(argc >= 4 && argv[3][0] != '-')
104  {
105  reference_file_path = argv[3];
106  arg_read = 3;
107  }
108  }
109 
110  for(int i = arg_read + 1; i < argc; i++)
111  {
112  std::string argvi(argv[i]);
113 
114  if(argvi == "-withQA")
115  {
116  with_adaptative_quantization = true;
117  }
118  else if(argvi == "-withCompr")
119  {
120  with_compression = true;
121  }
122  else if(argvi == "-Qbits")
123  {
124  quantiz_bits = std::atoi(argv[i + 1]);
125  i++;
126  }
127  else if(argvi == "-maxV")
128  {
129  max_vertices = std::atoi(argv[i + 1]);
130  i++;
131  }
132  else if(argvi == "-tolerance")
133  {
134  relative_tolerance = (float)std::atof(argv[i + 1]);
135  i++;
136  }
137  else
138  {
139  std::cout << "unknown option '" << argvi << "'. Aborting." << std::endl;
140  return EXIT_FAILURE;
141  }
142  }
143 
144  std::string p3d_file_path;
145  if(simplified_mesh_file_path.empty())
146  p3d_file_path =
147  input_file_path.substr(0, input_file_path.find_last_of(".")) + ".p3d";
148  else
149  p3d_file_path = simplified_mesh_file_path.substr(
150  0, simplified_mesh_file_path.find_last_of(".")) +
151  ".p3d";
152 
153  // display parameters summary
154  std::cout << "\nParameters summary:" << std::endl;
155  std::cout << " - input file: " << input_file_path << std::endl;
156  std::cout << " - simplified mesh file: " << simplified_mesh_file_path
157  << std::endl;
158  std::cout << " - p3d file: " << p3d_file_path << std::endl;
159  std::cout << " - ref. file: " << reference_file_path << std::endl;
160  std::cout << " - compression: " << std::boolalpha << with_compression
161  << std::endl;
162  std::cout << " - adaptative quantization: " << std::boolalpha
163  << with_adaptative_quantization << std::endl;
164  std::cout << " - max vertices: " << max_vertices << std::endl;
165  std::cout << " - quantization bits: " << quantiz_bits << std::endl;
166 
167  // display memory usage
168 #ifdef UNIX
169  pid_t pid = getpid();
170  uint vmpeak_kb;
171  uint vmsize_kb;
172 
173  get_vmpeak_vmsize(pid, vmpeak_kb, vmsize_kb);
174  std::cout << "before compression vmpeak=" << vmpeak_kb
175  << " kB vmsize=" << vmsize_kb << " kB" << std::endl;
176 #endif
177 
178  //--------------------------------------------------
179 
180  // read mesh from file
181  MeshT m;
182  FEVV::PMapsContainer pmaps_bag;
183  FEVV::Filters::read_mesh(input_file_path, m, pmaps_bag);
184 
185  // retrieve geometry property map
186  auto pm = get(boost::vertex_point, m);
187 
188  // retrieve vertex color property map
189  using VertexColorMap =
191  VertexColorMap v_cm;
192  VertexColorMap *v_cm_ptr = nullptr;
193  if(has_map(pmaps_bag, FEVV::vertex_color))
194  {
195  std::cout << "using vertex color property map" << std::endl;
196  v_cm = get_property_map(FEVV::vertex_color, m, pmaps_bag);
197  v_cm_ptr = &v_cm;
198  }
199 
200  // apply Compression Valence filter
202  &pm,
203  v_cm_ptr,
204  input_file_path,
205  p3d_file_path,
206  with_compression,
207  with_adaptative_quantization,
208  max_vertices,
209  quantiz_bits);
210 
211  // display memory usage
212 #ifdef UNIX
213  uint vmpeak2_kb;
214  get_vmpeak_vmsize(pid, vmpeak2_kb, vmsize_kb);
215  std::cout << "after compression vmpeak=" << vmpeak2_kb
216  << " kB vmsize=" << vmsize_kb
217  << " kB extra.vmpeak=" << vmpeak2_kb - vmpeak_kb << " kB"
218  << std::endl;
219 #endif
220 
221  // save the mesh
222  if(!simplified_mesh_file_path.empty())
223  FEVV::Filters::write_mesh(simplified_mesh_file_path, m, pmaps_bag);
224 
225  // check output file
226  if(!reference_file_path.empty())
227  {
228  std::cout << "Comparing output file" << std::endl;
229  std::cout << " '" << simplified_mesh_file_path << "'" << std::endl;
230  std::cout << "with reference file" << std::endl;
231  std::cout << " '" << reference_file_path << "'" << std::endl;
232  std::cout << "..." << std::endl;
233 
234  if(FEVV::FileUtils::has_extension(simplified_mesh_file_path, ".off") ||
235  FEVV::FileUtils::has_extension(simplified_mesh_file_path, ".coff"))
236  {
237  // use OFF file comparator
238  if(!are_meshes_equal(simplified_mesh_file_path,
239  reference_file_path,
240  false,
241  relative_tolerance,
242  true))
243  {
244  std::cout << "Files are different!" << std::endl;
245  return EXIT_FAILURE;
246  }
247  }
248  else
249  {
250  // use text file comparator
251  if(!identical_text_based_files(simplified_mesh_file_path,
252  reference_file_path))
253  {
254  std::cout << "Files are different!" << std::endl;
255  return EXIT_FAILURE;
256  }
257  }
258 
259  std::cout << "Files are identical." << std::endl;
260  }
261 
262  return 0;
263 }
FEVV::Filters::compression_valence
std::string compression_valence(HalfedgeGraph &g, PointMap *pm, VertexColorMap *v_cm, const std::string &input_filename, const std::string &output_filename, bool with_compression, bool with_adaptative_quantization, int max_vertices, int quantiz_bits, const GeometryTraits &)
Compress the input mesh using the Compression Valence algorithm.
Definition: compression_valence.h:54
FEVV::get_property_map
PMap_traits< PropertyT, MeshT >::pmap_type get_property_map(PropertyT p, const MeshT &, const PMapsContainer &pmaps)
Definition: properties.h:646
test_compression_valence
int test_compression_valence(int argc, const char **argv)
Definition: test_compression_valence.inl:61
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
utils_identical_text_based_files.hpp
are_meshes_equal
bool are_meshes_equal(std::string filename_a, std::string filename_b, bool verbose)
Definition: utils_are_meshes_identical.inl:925
FEVV::PMapsContainer
std::map< std::string, boost::any > PMapsContainer
Definition: properties.h:99
identical_text_based_files
bool identical_text_based_files(std::string filename_a, std::string filename_b, const std::vector< std::string > &skip=std::vector< std::string >())
Definition: utils_identical_text_based_files.hpp:27
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::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
utils_are_meshes_identical.hpp
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::FileUtils::has_extension
bool has_extension(const std::string &file_name)
Definition: FileUtilities.hpp:58
FEVV::_PMap_traits
Definition: properties.h:376
compression_valence.h
FileUtilities.hpp