gtsam
Loading...
Searching...
No Matches
AdaptAutoDiff.h
Go to the documentation of this file.
1/* ----------------------------------------------------------------------------
2
3 * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4 * Atlanta, Georgia 30332-0415
5 * All Rights Reserved
6 * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7
8 * See LICENSE for the license information
9
10 * -------------------------------------------------------------------------- */
11
18
19#pragma once
20
21#include <ceres/autodiff_cost_function.h>
23#include <gtsam/base/VectorSpace.h>
24
25#include <stdexcept>
26
27namespace gtsam {
28
38template <typename FUNCTOR, int M, int N1, int N2>
40 typedef Eigen::Matrix<double, M, N1, Eigen::RowMajor> RowMajor1;
41 typedef Eigen::Matrix<double, M, N2, Eigen::RowMajor> RowMajor2;
42
43 typedef Eigen::Matrix<double, M, 1> VectorT;
44 typedef Eigen::Matrix<double, N1, 1> Vector1;
45 typedef Eigen::Matrix<double, N2, 1> Vector2;
46
47 FUNCTOR f;
48
49 public:
50 VectorT operator()(const Vector1& v1, const Vector2& v2,
53 bool success;
54 VectorT result;
55
56 if (H1 || H2) {
57 // Get derivatives with AutoDiff
58 const double* parameters[] = {v1.data(), v2.data()};
59 double rowMajor1[M * N1] = {}, rowMajor2[M * N2] = {}; // on the stack
60 double* jacobians[] = {rowMajor1, rowMajor2};
61 ceres::AutoDiffCostFunction<FUNCTOR, M, N1, N2> autoDiff(
62 &f, ceres::DO_NOT_TAKE_OWNERSHIP);
63 success = autoDiff.Evaluate(parameters, result.data(), jacobians);
64
65 // Convert from row-major to columnn-major
66 // TODO: if this is a bottleneck (probably not!) fix Autodiff to be
67 // Column-Major
68 if (H1) *H1 = Eigen::Map<RowMajor1>(rowMajor1);
69 if (H2) *H2 = Eigen::Map<RowMajor2>(rowMajor2);
70
71 } else {
72 // Apply the mapping, to get result
73 success = f(v1.data(), v2.data(), result.data());
74 }
75 if (!success)
76 throw std::runtime_error(
77 "AdaptAutoDiff: function call resulted in failure");
78 return result;
79 }
80};
81
82} // namespace gtsam
Special class for optional Jacobian arguments.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
OptionalJacobian is an Eigen::Ref like class that can take be constructed using either a fixed size o...
Definition OptionalJacobian.h:40
The AdaptAutoDiff class uses ceres-style autodiff to adapt a ceres-style Function evaluation,...
Definition AdaptAutoDiff.h:39