10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Templated struct implementing the camera model and residual
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// computation for bundle adjustment used by Noah Snavely's Bundler
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SfM system. This is also the camera model/residual for the bundle
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// adjustment problems in the BAL dataset. It is templated so that we
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// can use Ceres's automatic differentiation to compute analytic
360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// jacobians.
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// For details see: http://phototour.cs.washington.edu/bundler/
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and http://grail.cs.washington.edu/projects/bal/
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/rotation.h"
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace examples {
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Templated pinhole camera model for used with Ceres.  The camera is
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// parameterized using 9 parameters: 3 for rotation, 3 for translation, 1 for
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// focal length and 2 for radial distortion. The principal point is not modeled
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// (i.e. it is assumed be located at the image center).
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct SnavelyReprojectionError {
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  SnavelyReprojectionError(double observed_x, double observed_y)
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      : observed_x(observed_x), observed_y(observed_y) {}
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  template <typename T>
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool operator()(const T* const camera,
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                  const T* const point,
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                  T* residuals) const {
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // camera[0,1,2] are the angle-axis rotation.
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T p[3];
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    ceres::AngleAxisRotatePoint(camera, point, p);
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // camera[3,4,5] are the translation.
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[0] += camera[3];
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[1] += camera[4];
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[2] += camera[5];
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Compute the center of distortion. The sign change comes from
710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // the camera model that Noah Snavely's Bundler assumes, whereby
720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // the camera coordinate system has a negative z axis.
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& focal = camera[6];
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T xp = - p[0] / p[2];
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T yp = - p[1] / p[2];
760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Apply second and fourth order radial distortion.
780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& l1 = camera[7];
790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& l2 = camera[8];
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T r2 = xp*xp + yp*yp;
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T distortion = T(1.0) + r2  * (l1 + l2  * r2);
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Compute final projected point position.
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T predicted_x = focal * distortion * xp;
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T predicted_y = focal * distortion * yp;
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // The error is the difference between the predicted and observed position.
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    residuals[0] = predicted_x - T(observed_x);
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    residuals[1] = predicted_y - T(observed_y);
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    return true;
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
9479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Factory to hide the construction of the CostFunction object from
9579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // the client code.
9679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  static ceres::CostFunction* Create(const double observed_x,
9779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                                     const double observed_y) {
9879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return (new ceres::AutoDiffCostFunction<SnavelyReprojectionError, 2, 9, 3>(
9979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                new SnavelyReprojectionError(observed_x, observed_y)));
10079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
10179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  double observed_x;
1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  double observed_y;
1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Templated pinhole camera model for used with Ceres.  The camera is
1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// parameterized using 10 parameters. 4 for rotation, 3 for
1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// translation, 1 for focal length and 2 for radial distortion. The
1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// principal point is not modeled (i.e. it is assumed be located at
1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the image center).
1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongstruct SnavelyReprojectionErrorWithQuaternions {
1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // (u, v): the position of the observation with respect to the image
1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // center point.
1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  SnavelyReprojectionErrorWithQuaternions(double observed_x, double observed_y)
1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong      : observed_x(observed_x), observed_y(observed_y) {}
1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  template <typename T>
1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool operator()(const T* const camera_rotation,
1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                  const T* const camera_translation_and_intrinsics,
1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                  const T* const point,
1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                  T* residuals) const {
1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& focal = camera_translation_and_intrinsics[3];
1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& l1 = camera_translation_and_intrinsics[4];
1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const T& l2 = camera_translation_and_intrinsics[5];
1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Use a quaternion rotation that doesn't assume the quaternion is
1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // normalized, since one of the ways to run the bundler is to let Ceres
1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // optimize all 4 quaternion parameters unconstrained.
1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T p[3];
1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    QuaternionRotatePoint(camera_rotation, point, p);
1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[0] += camera_translation_and_intrinsics[0];
1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[1] += camera_translation_and_intrinsics[1];
1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    p[2] += camera_translation_and_intrinsics[2];
1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Compute the center of distortion. The sign change comes from
1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // the camera model that Noah Snavely's Bundler assumes, whereby
1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // the camera coordinate system has a negative z axis.
1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T xp = - p[0] / p[2];
1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T yp = - p[1] / p[2];
1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Apply second and fourth order radial distortion.
1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T r2 = xp*xp + yp*yp;
1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T distortion = T(1.0) + r2  * (l1 + l2  * r2);
1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // Compute final projected point position.
1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T predicted_x = focal * distortion * xp;
1480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    T predicted_y = focal * distortion * yp;
1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    // The error is the difference between the predicted and observed position.
1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    residuals[0] = predicted_x - T(observed_x);
1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    residuals[1] = predicted_y - T(observed_y);
1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    return true;
1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
15779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Factory to hide the construction of the CostFunction object from
15879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // the client code.
15979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  static ceres::CostFunction* Create(const double observed_x,
16079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                                     const double observed_y) {
16179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    return (new ceres::AutoDiffCostFunction<
16279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez            SnavelyReprojectionErrorWithQuaternions, 2, 4, 6, 3>(
16379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                new SnavelyReprojectionErrorWithQuaternions(observed_x,
16479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                                                            observed_y)));
16579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
16679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
1670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  double observed_x;
1680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  double observed_y;
1690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
1700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace examples
1720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
1730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_EXAMPLES_SNAVELY_REPROJECTION_ERROR_H_
175