gtsam
Loading...
Searching...
No Matches
KarcherMeanFactor-inl.h
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
12/*
13 * @file KarcherMeanFactor.cpp
14 * @author Frank Dellaert
15 * @date March 2019
16 */
17
18#pragma once
19
22#include <gtsam/slam/KarcherMeanFactor.h>
23#include <optional>
24
25namespace gtsam {
26
27template <class T, class ALLOC>
28T FindKarcherMeanImpl(const std::vector<T, ALLOC>& rotations) {
29 static_assert(T::dimension != Eigen::Dynamic,
30 "FindKarcherMean requires fixed-size manifolds.");
31 // Cost function C(R) = \sum PriorFactor(R_i)::error(R)
32 // No closed form solution.
34 static const Key kKey(0);
35 for (const auto& R : rotations) {
36 graph.addPrior<T>(kKey, R, noiseModel::Unit::Create(R));
37 }
38 Values initial;
39 initial.insert<T>(kKey, T());
40 auto result = GaussNewtonOptimizer(graph, initial).optimize();
41 return result.at<T>(kKey);
42}
43
44template <class T>
45T FindKarcherMean(const std::vector<T>& rotations) {
46 return FindKarcherMeanImpl(rotations);
47}
48
49template <class T>
50T FindKarcherMean(const std::vector<T, Eigen::aligned_allocator<T>>& rotations) {
51 return FindKarcherMeanImpl(rotations);
52}
53
54template <class T>
55T FindKarcherMean(std::initializer_list<T>&& rotations) {
56 return FindKarcherMeanImpl(std::vector<T, Eigen::aligned_allocator<T> >(rotations));
57}
58
59template <class T>
60template <typename CONTAINER>
62 std::optional<double> beta)
63 : NonlinearFactor(keys), d_(static_cast<size_t>(d)) {
64 if (d <= 0) {
65 throw std::invalid_argument(
66 "KarcherMeanFactor needs dimension for dynamic types.");
67 }
68 // Create the constant Jacobian made of d*d identity matrices,
69 // where d is the dimensionality of the manifold.
70 Matrix A = Matrix::Identity(d, d);
71 if (beta) A *= std::sqrt(*beta);
72 std::map<Key, Matrix> terms;
73 for (Key j : keys) {
74 terms[j] = A;
75 }
76 whitenedJacobian_ =
77 std::make_shared<JacobianFactor>(terms, Vector::Zero(d));
78}
79} // namespace gtsam
Factor Graph consisting of non-linear factors.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:43
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
static shared_ptr Create(size_t dim)
Create a unit covariance noise model.
Definition NoiseModel.h:673
This class performs Gauss-Newton nonlinear optimization.
Definition GaussNewtonOptimizer.h:38
NonlinearFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:86
Definition NonlinearFactorGraph.h:57
virtual const Values & optimize()
Optimize for the maximum-likelihood estimate, returning a the optimized variable assignments.
Definition NonlinearOptimizer.h:118
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65
const ValueType at(Key j) const
Retrieve a variable by key j.
Definition Values-inl.h:260
KarcherMeanFactor(const CONTAINER &keys, int d=D, std::optional< double > beta={})
Construct from given keys.
Definition KarcherMeanFactor-inl.h:61