gtsam
Loading...
Searching...
No Matches
CustomFactor.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
17
18#pragma once
19
21
22#include <functional>
23#include <optional>
24
25namespace gtsam {
26
27using JacobianVector = std::vector<Matrix>;
28
29class CustomFactor;
30
32using OptionalJacobianVector = std::optional<std::reference_wrapper<JacobianVector>>;
33
34/*
35 * NOTE
36 * ==========
37 * pybind11 will invoke a copy if this is `JacobianVector &`, and modifications in Python will not be reflected.
38 *
39 * A `std::optional<std::reference_wrapper<JacobianVector>>` keeps reference semantics -- `JacobianVector` is
40 * opaque (see `preamble/custom.h`), so pybind11 maintains the `std::vector` memory layout and Python's writes
41 * are visible in C++ -- while also telling pybind11 that the argument is nullable. A bare pointer does not:
42 * pybind11 renders pointer arguments without `Optional`, so the generated stub claims the callback always
43 * receives a `JacobianVector`, and a correct `if H is not None` guard gets reported as unreachable.
44 */
45using CustomErrorFunction = std::function<Vector(const CustomFactor &, const Values &, OptionalJacobianVector)>;
46
54class GTSAM_EXPORT CustomFactor: public NoiseModelFactor {
55protected:
56 CustomErrorFunction error_function_;
57
58protected:
59
60 using Base = NoiseModelFactor;
61 using This = CustomFactor;
62
63public:
64
68 CustomFactor() = default;
69
76 CustomFactor(const SharedNoiseModel &noiseModel, const KeyVector &keys, const CustomErrorFunction &errorFunction) :
77 Base(noiseModel, keys) {
78 this->error_function_ = errorFunction;
79 }
80
85 Vector unwhitenedError(const Values &x, OptionalMatrixVecType H = nullptr) const override;
86
88 void print(const std::string &s,
89 const KeyFormatter &keyFormatter = DefaultKeyFormatter) const override;
90
94 bool sendable() const override {
95 return false;
96 }
97
98private:
99
100#if GTSAM_ENABLE_BOOST_SERIALIZATION
102 friend class boost::serialization::access;
103 template<class ARCHIVE>
104 void serialize(ARCHIVE &ar, const unsigned int /*version*/) {
105 ar & boost::serialization::make_nvp("CustomFactor",
106 boost::serialization::base_object<Base>(*this));
107 }
108#endif
109};
110
111}
Non-linear factor base classes.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition Key.cpp:30
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition Key.h:91
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition Matrix.cpp:143
std::optional< std::reference_wrapper< JacobianVector > > OptionalJacobianVector
Jacobians handed to the Python callback, absent when only the error is wanted.
Definition CustomFactor.h:32
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition Key.h:35
std::vector< Matrix > * OptionalMatrixVecType
The OptionalMatrixVecType is a pointer to a vector of matrices.
Definition NonlinearFactor.h:63
noiseModel::Base::shared_ptr SharedNoiseModel
Aliases.
Definition NoiseModel.h:846
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition Factor.h:143
Definition CustomFactor.h:54
CustomFactor(const SharedNoiseModel &noiseModel, const KeyVector &keys, const CustomErrorFunction &errorFunction)
Constructor.
Definition CustomFactor.h:76
CustomFactor()=default
Default Constructor for I/O.
bool sendable() const override
Mark not sendable.
Definition CustomFactor.h:94
NoiseModelFactor()
Default constructor for I/O only.
Definition NonlinearFactor.h:223
const SharedNoiseModel & noiseModel() const
access to the noise model
Definition NonlinearFactor.h:258
A non-templated config holding any types of Manifold-group elements.
Definition Values.h:65