bazar  1.3.1
ls_minimizer.h
Go to the documentation of this file.
1 /*
2 Copyright 2005, 2006 Computer Vision Lab,
3 Ecole Polytechnique Federale de Lausanne (EPFL), Switzerland.
4 All rights reserved.
5 
6 This file is part of BazAR.
7 
8 BazAR is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2 of the License, or (at your option) any later
11 version.
12 
13 BazAR is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 
17 You should have received a copy of the GNU General Public License along with
18 BazAR; if not, write to the Free Software Foundation, Inc., 51 Franklin
19 Street, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21 #ifndef LS_MINIMIZER_H
22 #define LS_MINIMIZER_H
23 
24 #include <limits>
25 #include <cxcore.h>
26 #include "ls_step_solver.h"
27 
29 
30 typedef double ls_minimizer_type;
31 
32 #include "observation_types.h"
33 #include "prosac_function_types.h"
34 
36 
37 typedef void (*callback_function)(double * state, void ** user_data);
38 
40 
46 {
47 public:
48 
49  ls_minimizer(int state_size, int maximum_scalar_measure_number);
50  ~ls_minimizer();
51 
54 
57 
61  void set_scales(double * scales);
62  void desactivate_automated_scaling(void) { use_automated_scaling = false; }
63 
69  void set_verbose_level(int vl);
70 
74  void set_user_data(int slot_index, void * ptr);
75 
77  void reset_observations(void);
78 
80  const double d0, const double d1,
81  const double b0);
83  const double d0, const double d1,
84  const double b0, const double b1);
91  const double d0, const double d1, const double d2,
92  const double b0, const double b1);
93 
96 
101  void set_default_c(double c);
102 
106  void set_last_observation_c(double c);
107 
109  void set_last_observation_c_max_c_min(double c_max, double c_min);
111  void set_last_observation_weight(double weight);
113  void set_last_observation_confidence(double confidence);
114 
117 
118  // ALGORITHMS:
119  // Steepest descent:
120  int minimize_using_steepest_descent_from(double * initial_state);
121 
122  // Levenberg-Marquardt
123  int minimize_using_levenberg_marquardt_from(double * initial_state);
124  void lm_set_max_iterations(int it);
126  void lm_set_max_failures_in_a_row(int f);
128  void lm_set_tol_cos(double t);
129 
130  // Gauss-Newton:
131  bool minimize_using_gauss_newton_from(double * initial_state);
132 
134  int minimize_using_dogleg_from(double * initial_state);
135 
139  int minimize_using_cattail_from(double * initial_state);
140  void ct_set_max_iterations(int it) {ct_max_iterations = it; }
141  void set_line_search_parameters(double lambda0, double k_rough, double k_fine);
142 
143  // Prosac
144  double minimize_using_prosac(prosac_function_20scalars f, int observation_type, int min_number_of_inliers, int max_number_of_iterations);
145 
146  // Julien' method
147  double minimize_using_julien_method_from(double * state, int nb_steps, int nb_iterations_per_step);
148 
149  // DEBUGGING:
153  void check_jacobians_around(double * state, double state_step);
154 
155  // Comparing with ground truth:
157  void set_ground_truth(double * state);
160 
161 private:
162  void set_default_values(void);
163 
164  inline double rho(double x2, double c2) { return (x2 < c2) ? x2 : c2; }
165  double build_eps(double * state, double current_best_residual = std::numeric_limits<double>::max());
166  int nb_inliers;
167  bool build_J(double * state);
168  bool build_J_and_eps(double * state);
169  bool build_J_and_stuff(double * state);
170  double compute_residual(double * state, bool display = false);
171  // !!! CALL new_iteration_callback function before calling residual !!!
172  ls_minimizer_type residual(ls_minimizer_type * state);
173 
174  double build_eps_return_inlier_residual(double * state, double current_best_residual = std::numeric_limits<double>::max());
175  double inlier_residual(double * state, double current_best_residual);
176 
177  void show_state(int vl, double * state, double r);
178 
179  void set_current_c_s(double k = -1.);
180 
181  ls_minimizer_type * new_state, * best_state, * scales;
182  int state_size;
183 
184  double default_squared_c;
185  observation_2data_1measure * observations_2data_1measure;
186  int number_of_observation_2data_1measure;
187 
188  observation_2data_2measures * observations_2data_2measures;
189  int number_of_observation_2data_2measures;
190 
191  observation_3data_2measures * observations_3data_2measures;
192  int number_of_observation_3data_2measures;
193 
194  int type_of_last_added_observation;
195 
196  CvMat * J, * eps, * eps_previous;
197  CvMat * Jt, * JtJ, * Jteps, * ds, * Jds, * aug_JtJ;
198  ls_step_solver * step_solver;
199  callback_function new_iteration_callback;
200 
201  bool use_user_scaling, use_automated_scaling;
202 
203  void * user_data[10];
204 
205  // Algorithm specific data:
206  // Levenberg-Marquardt
207  int lm_max_iterations;
208  int lm_max_failures_in_a_row;
209  double lm_tol_cos;
210 
211  // Dog leg:
212  double dl_update_Delta(double rho, double Delta, double rho_min, double rho_max);
213  double dl_tol_cos;
214  CvMat * dl_delta_diff, * dl_delta_dl, * dl_delta_sd, * dl_delta_gn;
215  CvMat * JtJdelta, * JJteps;
216 
217  // Cat tail:
218  bool line_search(CvMat * dir, double lambda_min, double lambda_max, double current_residual, double & new_residual, double & new_lambda);
219  CvMat * ct_delta_gn, * ct_delta_sd;
220  int ct_max_iterations;
221  double ct_k_rough, ct_k_fine, ct_lambda0;
222 
223  // Prosac:
224  bool pick_random_indices(int & i0, int & i1, int max_index);
225  bool pick_random_indices(int & i0, int & i1, int & i2, int max_index);
226  bool pick_random_indices(int & i0, int & i1, int & i2, int & i3, int max_index);
227 
228  // Julien' method:
229  bool inside_julien_method;
230 
231  // For debugging:
232  int verbose_level;
233  double * ground_truth_state;
234 };
235 
236 #endif // LS_MINIMIZER_H