MEPP2 Project
Uniform_quantization.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 
14 #include <boost/graph/graph_traits.hpp>
15 #include <boost/graph/properties.hpp>
16 
18 
19 #include <sys/stat.h>
20 
21 #include <iostream>
22 #include <fstream>
23 #include <iomanip>
24 #include <cstdint>
25 #include <cmath>
26 #include <set>
27 #include <list>
28 #include <vector>
29 #include <utility>
30 #include <cstdio>
31 #include <cstdlib>
32 
33 namespace FEVV {
34 namespace Filters {
35 
40 template <typename HalfedgeGraph,
41  typename PointMap,
44  typename Geometry = FEVV::Geometry_traits<HalfedgeGraph>,
46  typename vertex_iterator = typename boost::graph_traits<HalfedgeGraph>::vertex_iterator>
47 
49 {
50 
51  public:
52  Uniform_quantization(const HalfedgeGraph& g, PointMap& pm, int nb_bits) : _g(g), _pm(pm), _nb_bits(nb_bits)
53  {
54  this->find_min_and_max();
55  this->set_bounding_box();
56  this->set_quantization_step();
57  }
58 
59  protected:
61  {
62  Geometry gt(_g);
63  vertex_iterator vi = vertices(_g).first;
64  if(vertices(_g).first == vertices(_g).second)
65  return;
66  _p_min = get(_pm, *vi);
67  _p_max = _p_min;
68  ++vi;
69  double x_min = gt.get_x(_p_min);
70  double y_min = gt.get_y(_p_min);
71  double z_min = gt.get_z(_p_min);
72  double x_max = gt.get_x(_p_max);
73  double y_max = gt.get_y(_p_max);
74  double z_max = gt.get_z(_p_max);
75  double x_current;
76  double y_current;
77  double z_current;
78  for( ; vi != vertices(_g).second; ++vi)
79  {
80  Point p_current = get(_pm, *vi);
81 
82  // get coord
83  x_current = gt.get_x(p_current);
84  y_current = gt.get_y(p_current);
85  z_current = gt.get_z(p_current);
86 
87  // get min
88  if(x_current < x_min)
89  x_min = x_current;
90  if(y_current < y_min)
91  y_min = y_current;
92  if(z_current < z_min)
93  z_min = z_current;
94 
95  // get max
96  if(x_current > x_max)
97  x_max = x_current;
98  if(y_current > y_max)
99  y_max = y_current;
100  if(z_current > z_max)
101  z_max = z_current;
102  }
103  _p_min = Point(x_min, y_min, z_min);
104  _p_max = Point(x_max, y_max, z_max);
105  }
106 
107 
108  struct boundingBox
109  {
114 
115  };
116 
118  {
119  Geometry gt(_g);
120 
122  _bb.vl = Vector(0, gt.get_y(_p_max)-gt.get_y(_p_min), 0);
123  _bb.vh = Vector(0, 0, gt.get_z(_p_max)-gt.get_z(_p_min));
124  _bb.vp = Vector(gt.get_x(_p_max)-gt.get_x(_p_min), 0, 0);
125 
126  }
127 
129  {
130  Geometry gt(_g);
131  double l = gt.length(_bb.vl);
132  double h = gt.length(_bb.vh);
133  double p = gt.length(_bb.vp);
134 
135  _max_length.first = l;
136  _max_length.second = 'y';
137  if(_max_length.first < p)
138  {
139  _max_length.first = p;
140  _max_length.second = 'x';
141  }
142  if(_max_length.first < h)
143  {
144  _max_length.first = h;
145  _max_length.second = 'z';
146 
147  }
148 
149  _quantization_step = _max_length.first / pow(2.0, _nb_bits);
150 #if(DEBUG)
151  std::cout << "Uniform_quantization: max length : " << _max_length.first << std::endl;
152  std::cout << "Uniform_quantization: quantization step : " << _quantization_step << std::endl;
153 #endif
154  }
155  public:
156  double get_quantization_step() const { return _quantization_step; }
157 
159  void point_quantization(bool recompute_quanti_param = false)
160  {
161  Geometry gt(_g);
163  if(recompute_quanti_param)
164  {
165  this->find_min_and_max();
166  this->set_bounding_box();
167  this->set_quantization_step();
168  }
170  double p_min_x = gt.get_x(_p_min);
171  double p_min_y = gt.get_y(_p_min);
172  double p_min_z = gt.get_z(_p_min);
173 
174  vertex_iterator vi = vertices(_g).first;
175  for( ; vi != vertices(_g).second; ++vi)
176  {
177  Point point = get(_pm, *vi);
178 
179  double px = gt.get_x(point);
180  double py = gt.get_y(point);
181  double pz = gt.get_z(point);
182 
183  uint32_t pq_x = static_cast<uint32_t>(round((px - p_min_x) / _quantization_step));
184  if(pq_x >= pow(2.0, _nb_bits)) // may occur when px = p_max_x
185  pq_x = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
186  uint32_t pq_y = static_cast<uint32_t>(round((py - p_min_y) / _quantization_step));
187  if(pq_y >= pow(2.0, _nb_bits)) // may occur when py = p_max_y
188  pq_y = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
189  uint32_t pq_z = static_cast<uint32_t>(round((pz - p_min_z) / _quantization_step));
190  if(pq_z >= pow(2.0, _nb_bits)) // may occur when pz = p_max_z
191  pq_z = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
192 
193  Point new_position = Point(pq_x, pq_y, pq_z);
194  put(_pm, *vi, new_position);
195  }
196  }
200  Point quantize(const Point& p)
201  {
202  Geometry gt(_g);
203 
204  double p_min_x = gt.get_x(_p_min);
205  double p_min_y = gt.get_y(_p_min);
206  double p_min_z = gt.get_z(_p_min);
207 
208  double px = gt.get_x(p);
209  double py = gt.get_y(p);
210  double pz = gt.get_z(p);
211 
212  uint32_t pq_x = static_cast<uint32_t>(round((px - p_min_x) / _quantization_step));
213  if(pq_x >= pow(2.0, _nb_bits)) // may occur when px = p_max_x
214  pq_x = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
215  uint32_t pq_y = static_cast<uint32_t>(round((py - p_min_y) / _quantization_step));
216  if(pq_y >= pow(2.0, _nb_bits)) // may occur when py = p_max_y
217  pq_y = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
218  uint32_t pq_z = static_cast<uint32_t>(round((pz - p_min_z) / _quantization_step));
219  if(pq_z >= pow(2.0, _nb_bits)) // may occur when pz = p_max_z
220  pq_z = static_cast<uint32_t>(pow(2.0, _nb_bits) - 1);
221 
222  Point pq = Point(pq_x, pq_y, pq_z);
223  return pq;
224  }
225 
226 
227 
228  std::vector<double> get_bb_dimension() const
229  {
230  Geometry gt(_g);
231  double l = gt.length(_bb.vl);
232  double p = gt.length(_bb.vp);
233  double h = gt.length(_bb.vh);
234 
235  std::vector<double> bb_dimension{ p, l, h };
236  return bb_dimension;
237  }
238 
239  std::vector<double> get_init_coord() const
240  {
241  Geometry gt(_g);
242  double x = gt.get_x(_p_min);
243  double y = gt.get_y(_p_min);
244  double z = gt.get_z(_p_min);
245 
246  std::vector<double> init_coord{ x, y, z };
247  return init_coord;
248  }
249 
250  double get_diagonal() const
251  {
252  Geometry gt(_g);
253  Vector diagonal = _bb.vl + _bb.vh + _bb.vp;
254  return gt.length(diagonal);
255  }
256 
257  int get_nb_bits_quantization() const { return _nb_bits; }
258  private:
259  const HalfedgeGraph& _g;
260  PointMap& _pm;
261  const int _nb_bits;
263  std::pair<double,char> _max_length;
267 };
268 } // namespace Filters
269 } // namespace FEVV
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::Filters::Uniform_quantization::get_bb_dimension
std::vector< double > get_bb_dimension() const
Definition: Uniform_quantization.h:228
Vector
AIFMesh::Vector Vector
Definition: Graph_properties_aif.h:22
FEVV::Filters::Uniform_quantization
Uniform_quantization is a class dedicated to the XYZ uniform quantization of vertex coordinates store...
Definition: Uniform_quantization.h:49
FEVV::Geometry_traits
Refer to Geometry_traits_documentation_dummy for further documentation on provided types and algorith...
Definition: Geometry_traits.h:162
Point
AIFMesh::Point Point
Definition: Graph_properties_aif.h:21
FEVV::Filters::Uniform_quantization::find_min_and_max
void find_min_and_max()
Definition: Uniform_quantization.h:60
FEVV::Filters::Uniform_quantization::_nb_bits
const int _nb_bits
Point map changes.
Definition: Uniform_quantization.h:261
FEVV::Filters::Uniform_quantization::get_diagonal
double get_diagonal() const
Definition: Uniform_quantization.h:250
FEVV::Filters::Uniform_quantization::quantize
Point quantize(const Point &p)
Definition: Uniform_quantization.h:200
FEVV::Filters::Uniform_quantization::_max_length
std::pair< double, char > _max_length
Definition: Uniform_quantization.h:263
FEVV::Filters::Uniform_quantization::Uniform_quantization
Uniform_quantization(const HalfedgeGraph &g, PointMap &pm, int nb_bits)
Definition: Uniform_quantization.h:52
FEVV::Filters::Uniform_quantization::boundingBox::starting_point
Point starting_point
Definition: Uniform_quantization.h:110
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::Filters::Uniform_quantization::_p_max
Point _p_max
Definition: Uniform_quantization.h:265
FEVV
Interfaces for plugins These interfaces will be used for different plugins.
Definition: Assert.h:16
FEVV::Filters::Uniform_quantization::_p_min
Point _p_min
Definition: Uniform_quantization.h:264
FEVV::Filters::Uniform_quantization::boundingBox::vh
Vector vh
Definition: Uniform_quantization.h:112
FEVV::Filters::Uniform_quantization::get_quantization_step
double get_quantization_step() const
Definition: Uniform_quantization.h:156
FEVV::Filters::Uniform_quantization::_g
const HalfedgeGraph & _g
Definition: Uniform_quantization.h:259
Geometry_traits.h
FEVV::Filters::Uniform_quantization::get_nb_bits_quantization
int get_nb_bits_quantization() const
Definition: Uniform_quantization.h:257
FEVV::DataStructures::AIF::AIFVector
Definition: AIFProperties.h:173
FEVV::Filters::Uniform_quantization::_bb
boundingBox _bb
Definition: Uniform_quantization.h:266
FEVV::Filters::Uniform_quantization::set_quantization_step
void set_quantization_step()
Definition: Uniform_quantization.h:128
msdm2::vertex_descriptor
boost::graph_traits< MeshT >::vertex_descriptor vertex_descriptor
Definition: msdm2_surfacemesh.h:33
FEVV::put
void put(FEVV::PCLPointCloudPointMap &pm, FEVV::PCLPointCloudPointMap::key_type key, const FEVV::PCLPointCloudPointMap::value_type &value)
Specialization of put(point_map, key, value) for PCLPointCloud.
Definition: Graph_properties_pcl_point_cloud.h:126
FEVV::Filters::Uniform_quantization::_pm
PointMap & _pm
Topology remains the same.
Definition: Uniform_quantization.h:260
FEVV::Filters::Uniform_quantization::_quantization_step
double _quantization_step
Definition: Uniform_quantization.h:262
FEVV::Filters::Uniform_quantization::set_bounding_box
void set_bounding_box()
Definition: Uniform_quantization.h:117
FEVV::Filters::Uniform_quantization::boundingBox::vl
Vector vl
Definition: Uniform_quantization.h:111
FEVV::Filters::Uniform_quantization::get_init_coord
std::vector< double > get_init_coord() const
Definition: Uniform_quantization.h:239
FEVV::Filters::Uniform_quantization::point_quantization
void point_quantization(bool recompute_quanti_param=false)
Quantizes all vertex positions stored in the point map.
Definition: Uniform_quantization.h:159
FEVV::Filters::Uniform_quantization::boundingBox
Definition: Uniform_quantization.h:109
FEVV::DataStructures::AIF::AIFVector::length
CoordinateType length(void) const
Definition: AIFProperties.h:208
FEVV::DataStructures::AIF::AIFPoint
Definition: AIFProperties.h:31
FEVV::Filters::Uniform_quantization::boundingBox::vp
Vector vp
Definition: Uniform_quantization.h:113