rotationDistance.hpp 1.48 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm

#pragma once

#include "camera.hpp"

#include "libvideostitch/logging.hpp"

#include <ceres/ceres.h>

namespace VideoStitch {
namespace Calibration {

class rotationDistanceCostFunction : public ceres::CostFunction {
 public:
  explicit rotationDistanceCostFunction(const std::shared_ptr<const Camera>& cam) : cam(cam) {
    set_num_residuals(1);
    std::vector<int>* blocks = mutable_parameter_block_sizes();
    blocks->push_back(9);
  }

  /*
   * Evaluate() implements a "hard" limit on rotations, not a smooth distance function:
   * if the current rotation is out of allowed presets, returns false to inform ceres not to go for this rotation
   */
  virtual bool Evaluate(double const* const* parameters, double* residuals, double** jacobians) const {
    const Eigen::Map<const Eigen::Matrix<double, 3, 3, Eigen::RowMajor>> cameraR(parameters[0]);

    residuals[0] = 0;

    if (!cam->isRotationWithinPresets(cameraR)) {
      Logger::get(Logger::Warning) << "out of bound rotation, returning false" << std::endl;
      // return false to tell ceres that this rotation is not valid
      return false;
    }

    residuals[0] = 0;

    if (jacobians) {
      if (jacobians[0]) {
        Eigen::Map<Eigen::Matrix<double, 1, 9, Eigen::RowMajor>> J(jacobians[0]);

        J.setZero();
      }
    }

    return true;
  }

 private:
  const std::shared_ptr<const Camera> cam;
};

}  // namespace Calibration
}  // namespace VideoStitch