blob: 48b833b4bf13accb8d4c9f2163d9bfe40df83433 [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// http://ceres-solver.org/
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: keir@google.com (Keir Mierle)
30//
31// End-to-end bundle adjustment test utilities for Ceres. This base is used in
32// the generated bundle adjustment test binaries. The reason to split the
33// bundle tests into separate binaries is so the tests can get parallelized.
34
35#include <cmath>
36#include <cstdio>
37#include <cstdlib>
38#include <string>
39
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "ceres/autodiff_cost_function.h"
Austin Schuh3de38b02024-06-25 18:25:10 -070041#include "ceres/internal/export.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080042#include "ceres/ordered_groups.h"
43#include "ceres/problem.h"
44#include "ceres/rotation.h"
45#include "ceres/solver.h"
46#include "ceres/stringprintf.h"
47#include "ceres/test_util.h"
48#include "ceres/types.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080049#include "glog/logging.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080050
51namespace ceres {
52namespace internal {
53
Austin Schuh70cc9552019-01-21 19:46:48 -080054const bool kAutomaticOrdering = true;
55const bool kUserOrdering = false;
56
57// This class implements the SystemTestProblem interface and provides
58// access to a bundle adjustment problem. It is based on
59// examples/bundle_adjustment_example.cc. Currently a small 16 camera
60// problem is hard coded in the constructor.
61class BundleAdjustmentProblem {
62 public:
Austin Schuh3de38b02024-06-25 18:25:10 -070063 BundleAdjustmentProblem(const std::string input_file) {
64 ReadData(input_file);
65 BuildProblem();
66 }
Austin Schuh70cc9552019-01-21 19:46:48 -080067 BundleAdjustmentProblem() {
Austin Schuh3de38b02024-06-25 18:25:10 -070068 const std::string input_file =
69 TestFileAbsolutePath("problem-16-22106-pre.txt");
Austin Schuh70cc9552019-01-21 19:46:48 -080070 ReadData(input_file);
71 BuildProblem();
72 }
73
74 ~BundleAdjustmentProblem() {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080075 delete[] point_index_;
76 delete[] camera_index_;
77 delete[] observations_;
78 delete[] parameters_;
Austin Schuh70cc9552019-01-21 19:46:48 -080079 }
80
81 Problem* mutable_problem() { return &problem_; }
82 Solver::Options* mutable_solver_options() { return &options_; }
83
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080084 // clang-format off
Austin Schuh3de38b02024-06-25 18:25:10 -070085 int num_cameras() const { return num_cameras_; }
86 int num_points() const { return num_points_; }
87 int num_observations() const { return num_observations_; }
88 const int* point_index() const { return point_index_; }
89 const int* camera_index() const { return camera_index_; }
90 const double* observations() const { return observations_; }
91 double* mutable_cameras() { return parameters_; }
92 double* mutable_points() { return parameters_ + 9 * num_cameras_; }
93 const Solver::Options& options() const { return options_; }
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080094 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -080095
96 static double kResidualTolerance;
97
98 private:
Austin Schuh3de38b02024-06-25 18:25:10 -070099 void ReadData(const std::string& filename) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100 FILE* fptr = fopen(filename.c_str(), "r");
Austin Schuh70cc9552019-01-21 19:46:48 -0800101
102 if (!fptr) {
103 LOG(FATAL) << "File Error: unable to open file " << filename;
104 }
105
106 // This will die horribly on invalid files. Them's the breaks.
107 FscanfOrDie(fptr, "%d", &num_cameras_);
108 FscanfOrDie(fptr, "%d", &num_points_);
109 FscanfOrDie(fptr, "%d", &num_observations_);
110
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800111 VLOG(1) << "Header: " << num_cameras_ << " " << num_points_ << " "
112 << num_observations_;
Austin Schuh70cc9552019-01-21 19:46:48 -0800113
114 point_index_ = new int[num_observations_];
115 camera_index_ = new int[num_observations_];
116 observations_ = new double[2 * num_observations_];
117
118 num_parameters_ = 9 * num_cameras_ + 3 * num_points_;
119 parameters_ = new double[num_parameters_];
120
121 for (int i = 0; i < num_observations_; ++i) {
122 FscanfOrDie(fptr, "%d", camera_index_ + i);
123 FscanfOrDie(fptr, "%d", point_index_ + i);
124 for (int j = 0; j < 2; ++j) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800125 FscanfOrDie(fptr, "%lf", observations_ + 2 * i + j);
Austin Schuh70cc9552019-01-21 19:46:48 -0800126 }
127 }
128
129 for (int i = 0; i < num_parameters_; ++i) {
130 FscanfOrDie(fptr, "%lf", parameters_ + i);
131 }
132
133 fclose(fptr);
134 }
135
136 void BuildProblem() {
137 double* points = mutable_points();
138 double* cameras = mutable_cameras();
139
140 for (int i = 0; i < num_observations(); ++i) {
141 // Each Residual block takes a point and a camera as input and
142 // outputs a 2 dimensional residual.
143 CostFunction* cost_function =
144 new AutoDiffCostFunction<BundlerResidual, 2, 9, 3>(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800145 new BundlerResidual(observations_[2 * i + 0],
146 observations_[2 * i + 1]));
Austin Schuh70cc9552019-01-21 19:46:48 -0800147
148 // Each observation corresponds to a pair of a camera and a point
149 // which are identified by camera_index()[i] and
150 // point_index()[i] respectively.
151 double* camera = cameras + 9 * camera_index_[i];
152 double* point = points + 3 * point_index()[i];
Austin Schuh3de38b02024-06-25 18:25:10 -0700153 problem_.AddResidualBlock(cost_function, nullptr, camera, point);
Austin Schuh70cc9552019-01-21 19:46:48 -0800154 }
155
Austin Schuh3de38b02024-06-25 18:25:10 -0700156 options_.linear_solver_ordering =
157 std::make_shared<ParameterBlockOrdering>();
Austin Schuh70cc9552019-01-21 19:46:48 -0800158
159 // The points come before the cameras.
160 for (int i = 0; i < num_points_; ++i) {
161 options_.linear_solver_ordering->AddElementToGroup(points + 3 * i, 0);
162 }
163
164 for (int i = 0; i < num_cameras_; ++i) {
165 options_.linear_solver_ordering->AddElementToGroup(cameras + 9 * i, 1);
166 }
167
168 options_.linear_solver_type = DENSE_SCHUR;
169 options_.max_num_iterations = 25;
170 options_.function_tolerance = 1e-10;
171 options_.gradient_tolerance = 1e-10;
172 options_.parameter_tolerance = 1e-10;
173 }
174
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800175 template <typename T>
176 void FscanfOrDie(FILE* fptr, const char* format, T* value) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800177 int num_scanned = fscanf(fptr, format, value);
178 if (num_scanned != 1) {
179 LOG(FATAL) << "Invalid UW data file.";
180 }
181 }
182
183 // Templated pinhole camera model. The camera is parameterized
184 // using 9 parameters. 3 for rotation, 3 for translation, 1 for
185 // focal length and 2 for radial distortion. The principal point is
186 // not modeled (i.e. it is assumed to be located at the image
187 // center).
188 struct BundlerResidual {
189 // (u, v): the position of the observation with respect to the image
190 // center point.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800191 BundlerResidual(double u, double v) : u(u), v(v) {}
Austin Schuh70cc9552019-01-21 19:46:48 -0800192
193 template <typename T>
194 bool operator()(const T* const camera,
195 const T* const point,
196 T* residuals) const {
197 T p[3];
198 AngleAxisRotatePoint(camera, point, p);
199
200 // Add the translation vector
201 p[0] += camera[3];
202 p[1] += camera[4];
203 p[2] += camera[5];
204
205 const T& focal = camera[6];
206 const T& l1 = camera[7];
207 const T& l2 = camera[8];
208
209 // Compute the center of distortion. The sign change comes from
210 // the camera model that Noah Snavely's Bundler assumes, whereby
211 // the camera coordinate system has a negative z axis.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800212 T xp = -focal * p[0] / p[2];
213 T yp = -focal * p[1] / p[2];
Austin Schuh70cc9552019-01-21 19:46:48 -0800214
215 // Apply second and fourth order radial distortion.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800216 T r2 = xp * xp + yp * yp;
217 T distortion = T(1.0) + r2 * (l1 + l2 * r2);
Austin Schuh70cc9552019-01-21 19:46:48 -0800218
219 residuals[0] = distortion * xp - u;
220 residuals[1] = distortion * yp - v;
221
222 return true;
223 }
224
225 double u;
226 double v;
227 };
228
229 Problem problem_;
230 Solver::Options options_;
231
232 int num_cameras_;
233 int num_points_;
234 int num_observations_;
235 int num_parameters_;
236
237 int* point_index_;
238 int* camera_index_;
239 double* observations_;
240 // The parameter vector is laid out as follows
241 // [camera_1, ..., camera_n, point_1, ..., point_m]
242 double* parameters_;
243};
244
245double BundleAdjustmentProblem::kResidualTolerance = 1e-4;
Austin Schuh3de38b02024-06-25 18:25:10 -0700246using BundleAdjustmentTest = SystemTest<BundleAdjustmentProblem>;
Austin Schuh70cc9552019-01-21 19:46:48 -0800247
248} // namespace internal
249} // namespace ceres