MEPP2 Project
transfo_lab2lab2000hl.h
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 
12 #pragma once
13 
19 
20 inline double
21 interpolate1_computevalue(double x0, double x1, double y0, double y1, double x)
22 {
23  return y0 + ((x - x0) / (x1 - x0)) * (y1 - y0);
24 }
25 
26 inline double
27 interpolate1_process(std::vector< double > &init_grid,
28  std::vector< double > &val_grid,
29  double x) // std::vector<double> & result)
30 {
31 
32  auto upper = std::upper_bound(init_grid.begin(), init_grid.end(), x);
33  size_t position =
34  static_cast< size_t >(std::distance(init_grid.begin(), upper));
35 
36  // notes-VVI-ELO on possible bugs:
37  // - std::upper_bound must be applied on a sorted container ; is init_grid
38  // really sorted?
39  // - if upper is init_grid.begin() (aka the first element of init_grid is
40  // greater than x), then position equals 0 ; the lines below accessing
41  // init_grid[position - 1] and val_grid[position - 1] will cause a
42  // container overflow
43 
44  if(position < init_grid.size())
45  {
46  double res = interpolate1_computevalue(init_grid[position - 1],
47  init_grid[position],
48  val_grid[position - 1],
49  val_grid[position],
50  x);
51  return res;
52  }
53  else // if upper out of bounds returning maxval
54  {
55 
56  return val_grid[position - 1];
57  }
58 }
59 
60 inline double
62  double q_10,
63  double q_01,
64  double q_11,
65  double x0,
66  double x1,
67  double y0,
68  double y1,
69  double x,
70  double y)
71 {
72  // https://helloacm.com
73  double x1x0, y1y0, x1x, y1y, yy0, xx0;
74  x1x0 = x1 - x0;
75  y1y0 = y1 - y0;
76  x1x = x1 - x;
77  y1y = y1 - y;
78  yy0 = y - y0;
79  xx0 = x - x0;
80  return 1.0 / (x1x0 * y1y0) *
81  (q_00 * x1x * y1y + q_10 * xx0 * y1y + q_01 * x1x * yy0 +
82  q_11 * xx0 * yy0);
83 
84 
85  // return y0 + ((x - x0) / (x1 - x0)) * (y1 - y0);
86 }
87 
88 inline std::pair< double, double >
90  std::vector< std::vector< std::pair< double, double > > > &init_grid_AB,
91  double x,
92  double y) // std::vector<double> & result)
93 {
94  // Finding range
95  int x_min = (int)std::floor(x);
96  int x_max = x_min + 1; // int x_max = (int)std::ceil(x);
97  int y_min = (int)std::floor(y);
98  int y_max = y_min + 1; // int y_max = (int)std::ceil(y);
99 
100  // If there is no need to interpolate
101  if(x_min == x_max && y_min == y_max)
102  {
103  // return the value
104  return std::make_pair(init_grid_AB[x_min + 128][y_min + 128].first,
105  init_grid_AB[x_min + 128][y_min + 128].second);
106  }
107 
108 
109  double a_interpolated =
110  interpolate2_computevalue(init_grid_AB[x_min + 128][y_min + 128].first,
111  init_grid_AB[x_max + 128][y_min + 128].first,
112  init_grid_AB[x_min + 128][y_max + 128].first,
113  init_grid_AB[x_max + 128][y_max + 128].first,
114  x_min,
115  x_max,
116  y_min,
117  y_max,
118  x,
119  y);
120 
121  double b_interpolated =
122  interpolate2_computevalue(init_grid_AB[x_min + 128][y_min + 128].second,
123  init_grid_AB[x_max + 128][y_min + 128].second,
124  init_grid_AB[x_min + 128][y_max + 128].second,
125  init_grid_AB[x_max + 128][y_max + 128].second,
126  x_min,
127  x_max,
128  y_min,
129  y_max,
130  x,
131  y);
132 
133  return std::make_pair(a_interpolated, b_interpolated);
134 }
135 
136 inline void
137 initMatLABCH(std::vector< double > &init_grid_L,
138  std::vector< double > &grid_L,
139  std::vector< std::vector< std::pair< double, double > > >
140  &init_grid_AB) //, std::vector<std::vector<double>> & grid_AB)
141 {
142  int size_L = 100001;
143  int size_row = 257;
144  int size_col = 257;
145  init_grid_L.assign(size_L, 0);
146  grid_L.assign(size_L, 0);
147 
148  int size_tabAB = 66049;
149  init_grid_AB.assign(size_col,
150  std::vector< std::pair< double, double > >(
151  size_row, std::make_pair(0.0, 0.0)));
152 
153  //L_data
154  //int cpt = 0;
155  for(int cpt = 0; cpt < size_L; cpt++)
156  grid_L[cpt] = L_data[cpt];
157 
158 
159  // init grid L (from matlab code)
160  for(size_t i = 0; i < init_grid_L.size(); i++)
161  {
162  init_grid_L[i] = i * 0.001;
163  }
164 
165  // inigt grid AB
166  for(int cpt2 = 0; cpt2 < size_tabAB; cpt2++)
167  {
168  init_grid_AB[(int)(RegularGridInit_0_0_1[cpt2] + 128)][(int)(RegularGridInit_0_0_2[cpt2] + 128)].first = RegularGrid_0_0_1[cpt2];
169  init_grid_AB[(int)(RegularGridInit_0_0_1[cpt2] + 128)][(int)(RegularGridInit_0_0_2[cpt2] + 128)].second = RegularGrid_0_0_2[cpt2];
170  }
171 }
172 
173 
174 inline void
176  double L,
177  double A,
178  double B,
179  double *lab2000hl,
180  std::vector< double > &init_grid_L,
181  std::vector< double > &grid_L,
182  std::vector< std::vector< std::pair< double, double > > > &init_grid_AB)
183 {
184  lab2000hl[0] = interpolate1_process(init_grid_L, grid_L, L);
185  std::pair< double, double > me_a_b = interpolate2_process(init_grid_AB, A, B);
186  lab2000hl[1] = me_a_b.first;
187  lab2000hl[2] = me_a_b.second;
188 }
interpolate2_process
std::pair< double, double > interpolate2_process(std::vector< std::vector< std::pair< double, double > > > &init_grid_AB, double x, double y)
Definition: transfo_lab2lab2000hl.h:89
RegularGrid_0_0_1
const double RegularGrid_0_0_1[]
Definition: RegularGrid_0_0_1.h:3
lab2000hl_conversion
void lab2000hl_conversion(double L, double A, double B, double *lab2000hl, std::vector< double > &init_grid_L, std::vector< double > &grid_L, std::vector< std::vector< std::pair< double, double > > > &init_grid_AB)
Definition: transfo_lab2lab2000hl.h:175
interpolate1_computevalue
double interpolate1_computevalue(double x0, double x1, double y0, double y1, double x)
Definition: transfo_lab2lab2000hl.h:21
RegularGridInit_0_0_2.h
interpolate1_process
double interpolate1_process(std::vector< double > &init_grid, std::vector< double > &val_grid, double x)
Definition: transfo_lab2lab2000hl.h:27
initMatLABCH
void initMatLABCH(std::vector< double > &init_grid_L, std::vector< double > &grid_L, std::vector< std::vector< std::pair< double, double > > > &init_grid_AB)
Definition: transfo_lab2lab2000hl.h:137
RegularGridInit_0_0_1
const double RegularGridInit_0_0_1[]
Definition: RegularGridInit_0_0_1.h:3
RegularGridInit_0_0_2
const double RegularGridInit_0_0_2[]
Definition: RegularGridInit_0_0_2.h:3
RegularGridInit_0_0_1.h
RegularGrid_0_0_2.h
interpolate2_computevalue
double interpolate2_computevalue(double q_00, double q_10, double q_01, double q_11, double x0, double x1, double y0, double y1, double x, double y)
Definition: transfo_lab2lab2000hl.h:61
RegularGrid_0_0_1.h
L_data.h
RegularGrid_0_0_2
const double RegularGrid_0_0_2[]
Definition: RegularGrid_0_0_2.h:3
L_data
const double L_data[]
Definition: L_data.h:3