Debian Codegraph @

unit_test/test_helper/evaluate_jacobian.h

raw symbols SCIP
1 // g2o - General Graph Optimization
2 // Copyright (C) 2014 R. Kuemmerle, G. Grisetti, W. Burgard
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
16 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17 // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18 // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
21 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #ifndef EVALUATE_JACOBIAN_H
28 #define EVALUATE_JACOBIAN_H
29
30 #include <gtest/gtest.h>
31
32 #include <Eigen/Core>
33
34 #include "g2o/core/base_binary_edge.h"
35 #include "g2o/core/base_unary_edge.h"
36
37 namespace g2o {
38
39 template <typename EdgeType>
40 void evaluateJacobianUnary(EdgeType& e, JacobianWorkspace& jacobianWorkspace,
41 JacobianWorkspace& numericJacobianWorkspace) {
42 // calling the analytic Jacobian but writing to the numeric workspace
43 e.BaseUnaryEdge<EdgeType::Dimension, typename EdgeType::Measurement,
44 typename EdgeType::VertexXiType>::
45 linearizeOplus(numericJacobianWorkspace);
46 // copy result into analytic workspace
47 jacobianWorkspace = numericJacobianWorkspace;
48
49 // compute the numeric Jacobian into the numericJacobianWorkspace workspace as
50 // setup by the previous call
51 e.BaseUnaryEdge<EdgeType::Dimension, typename EdgeType::Measurement,
52 typename EdgeType::VertexXiType>::linearizeOplus();
53
54 // compare the Jacobians
55 double* n = numericJacobianWorkspace.workspaceForVertex(0);
56 double* a = jacobianWorkspace.workspaceForVertex(0);
57 int numElems = EdgeType::Dimension;
58 numElems *= EdgeType::VertexXiType::Dimension;
59 for (int j = 0; j < numElems; ++j) {
60 EXPECT_NEAR(n[j], a[j], 1e-6);
61 }
62 }
63
64 template <typename EdgeType>
65 void evaluateJacobian(EdgeType& e, JacobianWorkspace& jacobianWorkspace,
66 JacobianWorkspace& numericJacobianWorkspace) {
67 // calling the analytic Jacobian but writing to the numeric workspace
68 e.BaseBinaryEdge<EdgeType::Dimension, typename EdgeType::Measurement,
69 typename EdgeType::VertexXiType,
70 typename EdgeType::VertexXjType>::
71 linearizeOplus(numericJacobianWorkspace);
72 // copy result into analytic workspace
73 jacobianWorkspace = numericJacobianWorkspace;
74
75 // compute the numeric Jacobian into the numericJacobianWorkspace workspace as
76 // setup by the previous call
77 e.BaseBinaryEdge<EdgeType::Dimension, typename EdgeType::Measurement,
78 typename EdgeType::VertexXiType,
79 typename EdgeType::VertexXjType>::linearizeOplus();
80
81 // compare the two Jacobians
82 for (int i = 0; i < 2; ++i) {
83 double* n = numericJacobianWorkspace.workspaceForVertex(i);
84 double* a = jacobianWorkspace.workspaceForVertex(i);
85 int numElems = EdgeType::Dimension;
86 if (i == 0)
87 numElems *= EdgeType::VertexXiType::Dimension;
88 else
89 numElems *= EdgeType::VertexXjType::Dimension;
90 for (int j = 0; j < numElems; ++j) {
91 EXPECT_NEAR(n[j], a[j], 1e-6);
92 }
93 }
94 }
95
96 } // namespace g2o
97
98 #endif