NDEVR
API Documentation
base_vertex.h
1#pragma once
2#include "optimizable_graph.h"
3
4#include <Eigen/Core>
5#include <Eigen/Dense>
6#include <Eigen/Cholesky>
7#include <Eigen/StdVector>
8namespace NDEVR
9{
17 template <sint04 t_dims, typename T>
19 {
20 public:
21 static constexpr sint04 Dimension = t_dims;
22 typedef T EstimateType;
23 typedef Buffer<EstimateType> BackupStackType;
24
25 typedef Eigen::Map<Eigen::Matrix<g_type, t_dims, t_dims>, Eigen::Matrix<g_type, t_dims, t_dims>::Flags& Eigen::PacketAccessBit
26 ? Eigen::Aligned
27 : Eigen::Unaligned> HessianBlockType;
28
29 public:
30 static_assert(t_dims > 0, "Bad vertex");
33 : OptimizableGraph::OGVertex()
34 , _hessian(0, t_dims, t_dims)
35 {
36 _b.setZero();
37 }
38
39 sint04 dimension() const final override { return t_dims; }
43 g_type solveDirect(g_type lambda = 0.0) final override
44 {
45 Eigen::Matrix<g_type, t_dims, t_dims> tempA = _hessian
46 + Eigen::Matrix<g_type, t_dims, t_dims>::Identity()* lambda;
47 g_type det = tempA.determinant();
48 if (IsInvalid(det) || det < std::numeric_limits<g_type>::epsilon())
49 return det;
50 Eigen::Matrix <g_type, t_dims, 1> dx = tempA.llt().solve(_b);
51 oplus(&dx[0]);
52 return det;
53 }
54
57 void mapHessianMemory(g_type* d) final override
58 {
59 new (&_hessian) HessianBlockType(d, t_dims, t_dims);
60 }
61
62
64 const g_type& hessian(int i, int j) const final override { assert(i < t_dims && j < t_dims); return _hessian(i, j); }
66 g_type& hessian(int i, int j) final override { assert(i < t_dims && j < t_dims); return _hessian(i, j); }
68 g_type hessianDeterminant() const final override { return _hessian.determinant(); }
70 g_type* hessianData() final override { return const_cast<g_type*>(_hessian.data()); }
71
75 sint04 copyB(g_type* b_) const final override
76 {
77 memcpy(b_, _b.data(), t_dims * sizeof(g_type));
78 return t_dims;
79 }
80
82 const g_type& b(int i) const final override { assert(i < t_dims); return _b(i); }
84 g_type& b(int i) final override { assert(i < t_dims); return _b(i); }
86 g_type* bData() final override { return _b.data(); }
87
89 void clearQuadraticForm() final override {
90 _b.setZero();
91 }
92
93
95 Eigen::Matrix<g_type, t_dims, 1>& b() { return _b; }
96 const Eigen::Matrix<g_type, t_dims, 1>& b() const { return _b; }
98 HessianBlockType& A() { return _hessian; }
99 const HessianBlockType& A() const { return _hessian; }
100
102 virtual void push() final override { _backup.add(_estimate); }
104 virtual void pop() final override { _estimate = _backup.last(); _backup.removeLast(); }
106 virtual void discardTop() final override { _backup.removeLast(); }
108 virtual int stackSize() const final override { return _backup.size(); }
109
111 const EstimateType& estimate() const { return _estimate; }
113 void setEstimate(const EstimateType& et) { _estimate = et; }
114
115 protected:
116 HessianBlockType _hessian;
117 Eigen::Matrix<g_type, t_dims, 1> _b;
118 EstimateType _estimate;
119 BackupStackType _backup;
120 public:
121 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
122 };
123
124}
void clearQuadraticForm() final override
Zeros the b vector, clearing the accumulated quadratic form gradient.
Definition base_vertex.h:89
HessianBlockType _hessian
Mapped Hessian block for this vertex.
sint04 copyB(g_type *b_) const final override
Copies the b vector into the provided array.
Definition base_vertex.h:75
const EstimateType & estimate() const
return the current estimate of the vertex
EstimateType _estimate
Current estimate for this vertex.
g_type & b(int i) final override
Returns a mutable reference to the i-th element of the b vector.
Definition base_vertex.h:84
void setEstimate(const EstimateType &et)
set the estimate for the vertex also calls updateCache()
const g_type & b(int i) const final override
Returns a const reference to the i-th element of the b vector.
Definition base_vertex.h:82
HessianBlockType & A()
return the hessian block associated with the vertex
Definition base_vertex.h:98
const g_type & hessian(int i, int j) const final override
Returns a const reference to the Hessian element at (i,j).
Definition base_vertex.h:64
g_type & hessian(int i, int j) final override
Returns a mutable reference to the Hessian element at (i,j).
Definition base_vertex.h:66
g_type solveDirect(g_type lambda=0.0) final override
Solves the local linear system H*dx = b directly and applies the update.
Definition base_vertex.h:43
Eigen::Matrix< g_type, t_dims, 1 > _b
Right-hand side (gradient) vector of the quadratic form.
Eigen::Matrix< g_type, t_dims, 1 > & b()
return right hand side b of the constructed linear system
Definition base_vertex.h:95
sint04 dimension() const final override
Returns the minimal dimension of this vertex.
Definition base_vertex.h:39
void mapHessianMemory(g_type *d) final override
Maps the Hessian block to an external memory buffer.
Definition base_vertex.h:57
virtual void discardTop() final override
Discards the top of the backup stack without restoring.
g_type * bData() final override
Returns a mutable pointer to the raw b vector data.
Definition base_vertex.h:86
virtual int stackSize() const final override
Returns the number of entries on the backup stack.
g_type * hessianData() final override
Returns a mutable pointer to the raw Hessian data.
Definition base_vertex.h:70
BaseVertex()
Default constructor.
Definition base_vertex.h:32
virtual void push() final override
Pushes the current estimate onto the backup stack.
BackupStackType _backup
Stack for backing up and restoring estimates.
g_type hessianDeterminant() const final override
Returns the determinant of the Hessian block.
Definition base_vertex.h:68
virtual void pop() final override
Restores the estimate from the top of the backup stack and removes it.
The equivelent of std::vector but with a bit more control.
Definition Buffer.hpp:58
A general case Vertex for optimization.
void oplus(const g_type *v)
Update the position of the node from the parameters in v.
The primary namespace for the NDEVR SDK.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
static constexpr bool IsInvalid(const Angle< t_type > &value)
Checks whether the given Angle holds an invalid value.
Definition Angle.h:388