MEPP2 Project
Compression_Valence_Common.h
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.
11 #pragma once
12 
13 
14 #if defined _MSC_VER
15 // disable some warnings on Windows
16 #pragma warning(push)
17 #pragma warning(disable : 4244) // for VS-2015
18 // 4244: converting type A to type B, possible data loss
19 #endif
20 
21 
22 #include <CGAL/boost/graph/internal/helpers.h>
23 #include <CGAL/boost/graph/iterator.h>
24 
25 
26 const int RGB_Range = std::pow((int)2, 4.0) - 1;
27 /* Quantization bits used.*/
28 
29 const int RGB_QUANTIZATION = 8;
30 const int C0_QUANTIZATION = 8;
31 const int C1_QUANTIZATION = 8;
32 const int C2_QUANTIZATION = 8;
33 
34 const int FREE = -1;
35 const int CONQUERED = 0;
36 const int TO_BE_REMOVED = 1;
37 const int TEMP_FLAG = 2;
38 
39 const int PLUS = 1;
40 const int MINUS = -1;
41 const int NOSIGN = 0;
42 
43 const int FIRST_COORDINATE = 1;
44 const int SECOND_COORDINATE = 2;
45 const int OTHER_COORDINATE = -1;
46 
47 
49  5;
50 const int LIMIT_QBIT = 4;
51 const double PI =
52  3.14159265358979323846264338327950288419716939937510582097494459;
53 
55 
65 inline void
66 RGB_To_LAB(float r, float g, float b, float *lab)
67 {
68  // R 0 -- 1, G 0 -- 1, B 0 -- 1
69 
70  // http://www.brucelindbloom.com
71  float X, Y, Z;
72  // float L;
73  float eps = 216.f / 24389.f;
74  float k = 24389.f / 27.f;
75 
76  float Xr = 0.964221f; // reference white D50
77  float Yr = 1.0f;
78  float Zr = 0.825211f;
79 
80  // RGB to XYZ
81 
82  // assuming sRGB (D65)
83  if(r <= 0.04045)
84  r = r / 12.92;
85  else
86  r = (float)std::pow((r + 0.055) / 1.055, 2.4);
87 
88  if(g <= 0.04045)
89  g = g / 12.92;
90  else
91  g = (float)std::pow((g + 0.055) / 1.055, 2.4);
92 
93  if(b <= 0.04045)
94  b = b / 12.92;
95  else
96  b = (float)std::pow((b + 0.055) / 1.055, 2.4);
97 
98 
99  X = 0.412424 * r + 0.357579 * g + 0.180464 * b;
100  Y = 0.212656 * r + 0.715158 * g + 0.0721856 * b;
101  Z = 0.0193324 * r + 0.119193 * g + 0.950444 * b;
102 
103  // XYZ to LAB
104 
105  float xr = X / Xr;
106  float yr = Y / Yr;
107  float zr = Z / Zr;
108 
109  float fx, fy, fz;
110 
111  if(xr > eps)
112  fx = (float)std::pow(xr, (float)(1.f / 3.f));
113  else
114  fx = (k * xr + 16.0) / 116.0;
115 
116  if(yr > eps)
117  fy = (float)std::pow(yr, (float)(1.f / 3.f));
118  else
119  fy = (k * yr + 16.0) / 116.f;
120 
121  if(zr > eps)
122  fz = (float)std::pow(zr, (float)(1.f / 3.f));
123  else
124  fz = (k * zr + 16.0) / 116.f;
125 
126  lab[0] = (float)(116.0 * fy - 16.0); // L
127  lab[1] = (float)(500.0 * (fx - fy)); // A
128  lab[2] = (float)(200.0 * (fy - fz)); // B
129 }
130 
140 inline void
141 LAB_To_RGB(float L, float A, float B, float *rgb)
142 {
143  // LAB TO XYZ
144 
145  float eps = 216.f / 24389.f;
146  float k = 24389.f / 27.f;
147 
148  float Xr = 0.964221f; // reference white D50
149  float Yr = 1.0f;
150  float Zr = 0.825211f;
151  // float Xr = 96.4221f; // reference white D50
152  // float Yr = 100.f;
153  // float Zr = 82.5211f;
154 
155  float fx, fy, fz;
156  fy = (L + 16.0) / 116.0;
157  fx = (A / 500.0) + fy;
158  fz = fy - B / 200.0;
159 
160  float xr, yr, zr;
161  float fx_cube = (float)std::pow(fx, (float)3.0);
162  float fy_cube = (float)std::pow(fy, (float)3.0);
163  float fz_cube = (float)std::pow(fz, (float)3.0);
164 
165  if(fx_cube > eps)
166  xr = fx_cube;
167  else
168  xr = (116.0 * fx - 16.0) / k;
169 
170  if(fy_cube > eps)
171  yr = fy_cube;
172  else
173  yr = (float)(L / k);
174 
175  if(fz_cube > eps)
176  zr = fz_cube;
177  else
178  zr = (116.0 * fz - 16.0) / k;
179 
180  float X, Y, Z;
181  X = xr * Xr;
182  Y = yr * Yr;
183  Z = zr * Zr;
184 
185 
186  // XYZ to RGB
187 
188  // R 0..1, G 0..1, B 0..1
189 
190  float r, g, b;
191 
192  // assuming sRGB (D65)
193 
194  r = 3.24071 * X + -1.53726 * Y + -0.498571 * Z;
195  g = -0.969258 * X + 1.87599 * Y + 0.0415557 * Z;
196  b = 0.0556352 * X + -0.203996 * Y + 1.05707 * Z;
197 
198  if(r <= 0.0031308)
199  rgb[0] = 12.92 * r;
200  else
201  rgb[0] = 1.055 * (float)std::pow(r, float(1.0) / float(2.4)) - 0.055;
202 
203  if(g <= 0.0031308)
204  rgb[1] = 12.92 * g;
205  else
206  rgb[1] = 1.055 * (float)std::pow(g, float(1.0) / float(2.4)) - 0.055;
207 
208  if(b <= 0.0031308)
209  rgb[2] = 12.92 * b;
210  else
211  rgb[2] = 1.055 * (float)std::pow(b, (float(1.0) / float(2.4))) - 0.055;
212 
213  // rgb[0] = R;
214  // rgb[1] = G;
215  // rgb[2] = B;
216 }
217 
218 
219 // ELO-note: fix CGAL::Euler::add_vertex_and_face_to_border(h1, h2, g)
238 template< typename Graph >
239 typename boost::graph_traits< Graph >::halfedge_descriptor
241  typename boost::graph_traits< Graph >::halfedge_descriptor h1,
242  typename boost::graph_traits< Graph >::halfedge_descriptor h2,
243  Graph &g)
244 {
246  typename boost::graph_traits< Graph >::face_descriptor f = add_face(g);
247  typename boost::graph_traits< Graph >::edge_descriptor e1 = add_edge(g);
248  typename boost::graph_traits< Graph >::edge_descriptor e2 = add_edge(g);
249  typename boost::graph_traits< Graph >::halfedge_descriptor he1 =
250  halfedge(e1, g);
251  typename boost::graph_traits< Graph >::halfedge_descriptor he2 =
252  halfedge(e2, g);
253  typename boost::graph_traits< Graph >::halfedge_descriptor ohe1 =
254  opposite(he1, g);
255  typename boost::graph_traits< Graph >::halfedge_descriptor ohe2 =
256  opposite(he2, g);
257 
258  set_next(ohe1, next(h1, g), g);
259  set_next(h1, he1, g);
260  set_target(ohe1, target(h1, g), g);
261  set_target(he1, v, g);
262  set_next(ohe2, ohe1, g);
263  set_target(ohe2, v, g);
264  set_next(he1, he2, g);
265  set_target(he1, v, g);
266  // set_halfedge(v,he1,g);
267  set_halfedge(v, ohe2, g); // changed for consistency with
268  // Polyhedron_3::add_vertex_and_face_to_border()
269  set_next(he2, next(h2, g), g);
270  set_target(he2, target(h2, g), g);
271  set_next(h2, ohe2, g);
272  CGAL::internal::set_border(he1, g);
273  CGAL::internal::set_border(he2, g);
274 
275  CGAL::Halfedge_around_face_iterator< Graph > hafib, hafie;
276  for(boost::tie(hafib, hafie) = CGAL::halfedges_around_face(ohe1, g);
277  hafib != hafie;
278  ++hafib)
279  {
280  set_face(*hafib, f, g);
281  }
282  // set_halfedge(f, ohe1, g);
283  set_halfedge(f, h2, g); // changed for consistency with
284  // Polyhedron_3::add_vertex_and_face_to_border()
285  return ohe2;
286 }
287 
288 
289 #if defined _MSC_VER
290 #pragma warning(pop)
291 #endif
fixed_CGAL_Euler_add_vertex_and_face_to_border
boost::graph_traits< Graph >::halfedge_descriptor fixed_CGAL_Euler_add_vertex_and_face_to_border(typename boost::graph_traits< Graph >::halfedge_descriptor h1, typename boost::graph_traits< Graph >::halfedge_descriptor h2, Graph &g)
Definition: Compression_Valence_Common.h:240
PLUS
const int PLUS
The plus tag for retriangulation.
Definition: Compression_Valence_Common.h:39
FEVV::DataStructures::AIF::next
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor next(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns the next halfedge around its face.
Definition: Graph_traits_aif.h:599
NOSIGN
const int NOSIGN
The nosign tag for retriangulation.
Definition: Compression_Valence_Common.h:41
LAB_To_RGB
void LAB_To_RGB(float L, float A, float B, float *rgb)
Lab to rgb.
Definition: Compression_Valence_Common.h:141
FEVV::DataStructures::AIF::set_halfedge
void set_halfedge(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor v, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, FEVV::DataStructures::AIF::AIFMesh &sm)
Sets the halfedge of v to h. The target vertex of h must be v.
Definition: Graph_traits_aif.h:775
OTHER_COORDINATE
const int OTHER_COORDINATE
The other coordinate tag for seed gate.
Definition: Compression_Valence_Common.h:45
FEVV::DataStructures::AIF::set_next
void set_next(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h1, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h2, FEVV::DataStructures::AIF::AIFMesh &sm)
Sets the successor of h1 around a face to h2, and the prededecessor of h2 to h1.
Definition: Graph_traits_aif.h:790
FEVV::DataStructures::AIF::add_face
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::face_descriptor add_face(FEVV::DataStructures::AIF::AIFMesh &sm)
Adds a new face to the graph without initializing the connectivity.
Definition: Graph_traits_aif.h:1056
FEVV::DataStructures::AIF::opposite
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor opposite(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns the halfedge with source and target swapped.
Definition: Graph_traits_aif.h:625
TEMP_FLAG
const int TEMP_FLAG
Definition: Compression_Valence_Common.h:37
DECIMATION_CONNECTIVITY_SYMBOL
const int DECIMATION_CONNECTIVITY_SYMBOL
The decimation connectivity symbol.
Definition: Compression_Valence_Common.h:48
MINUS
const int MINUS
The minus tag for retriangulation.
Definition: Compression_Valence_Common.h:40
FREE
const int FREE
Definition: Compression_Valence_Common.h:34
FEVV::DataStructures::AIF::add_edge
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::edge_descriptor add_edge(FEVV::DataStructures::AIF::AIFMesh &sm)
Adds two opposite halfedges to the graph without initializing the connectivity.
Definition: Graph_traits_aif.h:827
C2_QUANTIZATION
const int C2_QUANTIZATION
Definition: Compression_Valence_Common.h:32
SECOND_COORDINATE
const int SECOND_COORDINATE
The second coordinate tag for seed gate.
Definition: Compression_Valence_Common.h:44
C1_QUANTIZATION
const int C1_QUANTIZATION
Definition: Compression_Valence_Common.h:31
FEVV::DataStructures::AIF::halfedge
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor halfedge(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor v, const FEVV::DataStructures::AIF::AIFMesh &sm)
Returns a halfedge with target v.
Definition: Graph_traits_aif.h:296
FEVV::DataStructures::AIF::target
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor target(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::edge_descriptor e, const FEVV::DataStructures::AIF::AIFMesh &)
Returns the target vertex of e.
Definition: Graph_traits_aif.h:400
CONQUERED
const int CONQUERED
Definition: Compression_Valence_Common.h:35
PI
const double PI
Definition: Compression_Valence_Common.h:51
FEVV::DataStructures::AIF::set_target
void set_target(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor v, FEVV::DataStructures::AIF::AIFMesh &sm)
Sets the target vertex of h and the source of opposite(h) to v.
Definition: Graph_traits_aif.h:740
RGB_QUANTIZATION
const int RGB_QUANTIZATION
Definition: Compression_Valence_Common.h:29
RGB_Range
const int RGB_Range
Definition: Compression_Valence_Common.h:26
FEVV::DataStructures::AIF::add_vertex
boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_descriptor add_vertex(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::vertex_property_type vp, FEVV::DataStructures::AIF::AIFMesh &sm)
Definition: Graph_properties_aif.h:263
FIRST_COORDINATE
const int FIRST_COORDINATE
The first coordinate tag for seed gate.
Definition: Compression_Valence_Common.h:43
msdm2::vertex_descriptor
boost::graph_traits< MeshT >::vertex_descriptor vertex_descriptor
Definition: msdm2_surfacemesh.h:33
C0_QUANTIZATION
const int C0_QUANTIZATION
Definition: Compression_Valence_Common.h:30
LIMIT_QBIT
const int LIMIT_QBIT
The limit qbit.
Definition: Compression_Valence_Common.h:50
FEVV::DataStructures::AIF::set_face
void set_face(typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::halfedge_descriptor h, typename boost::graph_traits< FEVV::DataStructures::AIF::AIFMesh >::face_descriptor f, FEVV::DataStructures::AIF::AIFMesh &sm)
Sets the corresponding face of h to f.
Definition: Graph_traits_aif.h:983
TO_BE_REMOVED
const int TO_BE_REMOVED
Definition: Compression_Valence_Common.h:36
RGB_To_LAB
void RGB_To_LAB(float r, float g, float b, float *lab)
Rgb to lab.
Definition: Compression_Valence_Common.h:66