blob: 4d7d59b17928935765e438b8f10bd6d8d03e49c5 [file] [log] [blame]
Keir Mierlef9566152013-05-07 02:59:32 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2013 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: mierle@gmail.com (Keir Mierle)
30//
31// An incomplete C API for Ceres.
32//
33// TODO(keir): Figure out why logging does not seem to work.
34
35#include <vector>
36#include <iostream> // XXX remove me
37#include "ceres/c_api.h"
38#include "ceres/cost_function.h"
39#include "ceres/problem.h"
40#include "ceres/solver.h"
41#include "ceres/types.h" // for std
42#include "glog/logging.h"
43
44using ceres::Problem;
45
46void ceres_init() {
47 // This is not ideal, but it's not clear what to do if there is no gflags and
48 // no access to command line arguments.
49 google::InitGoogleLogging("<unknown>");
50}
51
52ceres_problem_t* ceres_create_problem() {
53 return reinterpret_cast<ceres_problem_t*>(new Problem);
54}
55
56void ceres_free_problem(ceres_problem_t* problem) {
57 delete reinterpret_cast<Problem*>(problem);
58}
59
60class CallbackCostFunction : public ceres::CostFunction {
61 public:
62 CallbackCostFunction(ceres_cost_function_t cost_function,
63 void* user_data,
64 int num_residuals,
65 int num_parameter_blocks,
66 int* parameter_block_sizes)
67 : cost_function_(cost_function),
68 user_data_(user_data) {
69 set_num_residuals(num_residuals);
70 for (int i = 0; i < num_parameter_blocks; ++i) {
71 mutable_parameter_block_sizes()->push_back(parameter_block_sizes[i]);
72 }
73 }
74
75 virtual ~CallbackCostFunction() {}
76
77 virtual bool Evaluate(double const* const* parameters,
78 double* residuals,
79 double** jacobians) const {
80 return (*cost_function_)(user_data_,
81 const_cast<double**>(parameters),
82 residuals,
83 jacobians);
84 }
85
86 private:
87 ceres_cost_function_t cost_function_;
88 void* user_data_;
89};
90
91ceres_residual_block_id_t* ceres_problem_add_residual_block(
92 ceres_problem_t* problem,
93 ceres_cost_function_t cost_function,
94 ceres_loss_function_t loss_function,
95 void* user_data,
96 int num_residuals,
97 int num_parameter_blocks,
98 int* parameter_block_sizes,
99 double** parameters) {
100 Problem* ceres_problem = reinterpret_cast<Problem*>(problem);
101
102 ceres::CostFunction* callback_cost_function =
103 new CallbackCostFunction(cost_function,
104 user_data,
105 num_residuals,
106 num_parameter_blocks,
107 parameter_block_sizes);
108
109 std::vector<double*> parameter_blocks(parameters,
110 parameters + num_parameter_blocks);
111 return reinterpret_cast<ceres_residual_block_id_t*>(
112 ceres_problem->AddResidualBlock(callback_cost_function,
113 NULL, /* Ignore loss for now */
114 parameter_blocks));
115}
116
117void ceres_solve(ceres_problem_t* c_problem) {
118 Problem* problem = reinterpret_cast<Problem*>(c_problem);
119
120 // TODO(keir): Obviously, this way of setting options won't scale or last.
121 // Instead, figure out a way to specify some of the options without
122 // duplicating everything.
123 ceres::Solver::Options options;
124 options.max_num_iterations = 25;
125 options.linear_solver_type = ceres::DENSE_QR;
126 options.minimizer_progress_to_stdout = true;
127
128 ceres::Solver::Summary summary;
129 ceres::Solve(options, problem, &summary);
130 std::cout << summary.FullReport() << "\n";
131}