123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- #ifndef CERES_PUBLIC_AUTODIFF_MANIFOLD_H_
- #define CERES_PUBLIC_AUTODIFF_MANIFOLD_H_
- #include <memory>
- #include "ceres/internal/autodiff.h"
- #include "ceres/manifold.h"
- namespace ceres {
- template <typename Functor, int kAmbientSize, int kTangentSize>
- class AutoDiffManifold final : public Manifold {
- public:
- AutoDiffManifold() : functor_(std::make_unique<Functor>()) {}
-
- explicit AutoDiffManifold(Functor* functor) : functor_(functor) {}
- int AmbientSize() const override { return kAmbientSize; }
- int TangentSize() const override { return kTangentSize; }
- bool Plus(const double* x,
- const double* delta,
- double* x_plus_delta) const override {
- return functor_->Plus(x, delta, x_plus_delta);
- }
- bool PlusJacobian(const double* x, double* jacobian) const override;
- bool Minus(const double* y,
- const double* x,
- double* y_minus_x) const override {
- return functor_->Minus(y, x, y_minus_x);
- }
- bool MinusJacobian(const double* x, double* jacobian) const override;
- const Functor& functor() const { return *functor_; }
- private:
- std::unique_ptr<Functor> functor_;
- };
- namespace internal {
- template <typename Functor>
- struct PlusWrapper {
- explicit PlusWrapper(const Functor& functor) : functor(functor) {}
- template <typename T>
- bool operator()(const T* x, const T* delta, T* x_plus_delta) const {
- return functor.Plus(x, delta, x_plus_delta);
- }
- const Functor& functor;
- };
- template <typename Functor>
- struct MinusWrapper {
- explicit MinusWrapper(const Functor& functor) : functor(functor) {}
- template <typename T>
- bool operator()(const T* y, const T* x, T* y_minus_x) const {
- return functor.Minus(y, x, y_minus_x);
- }
- const Functor& functor;
- };
- }
- template <typename Functor, int kAmbientSize, int kTangentSize>
- bool AutoDiffManifold<Functor, kAmbientSize, kTangentSize>::PlusJacobian(
- const double* x, double* jacobian) const {
- double zero_delta[kTangentSize];
- for (int i = 0; i < kTangentSize; ++i) {
- zero_delta[i] = 0.0;
- }
- double x_plus_delta[kAmbientSize];
- for (int i = 0; i < kAmbientSize; ++i) {
- x_plus_delta[i] = 0.0;
- }
- const double* parameter_ptrs[2] = {x, zero_delta};
-
-
- double* jacobian_ptrs[2] = {nullptr, jacobian};
- return internal::AutoDifferentiate<
- kAmbientSize,
- internal::StaticParameterDims<kAmbientSize, kTangentSize>>(
- internal::PlusWrapper<Functor>(*functor_),
- parameter_ptrs,
- kAmbientSize,
- x_plus_delta,
- jacobian_ptrs);
- }
- template <typename Functor, int kAmbientSize, int kTangentSize>
- bool AutoDiffManifold<Functor, kAmbientSize, kTangentSize>::MinusJacobian(
- const double* x, double* jacobian) const {
- double y_minus_x[kTangentSize];
- for (int i = 0; i < kTangentSize; ++i) {
- y_minus_x[i] = 0.0;
- }
- const double* parameter_ptrs[2] = {x, x};
-
-
- double* jacobian_ptrs[2] = {jacobian, nullptr};
- return internal::AutoDifferentiate<
- kTangentSize,
- internal::StaticParameterDims<kAmbientSize, kAmbientSize>>(
- internal::MinusWrapper<Functor>(*functor_),
- parameter_ptrs,
- kTangentSize,
- y_minus_x,
- jacobian_ptrs);
- }
- }
- #endif
|