gtsam_points
Loading...
Searching...
No Matches
expressions.hpp
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2021 Kenji Koide (k.koide@aist.go.jp)
3
4#pragma once
5
6#include <gtsam/geometry/Pose3.h>
7#include <gtsam/slam/expressions.h>
8#include <gtsam/nonlinear/expressions.h>
9
10namespace gtsam_points {
11
12namespace internal {
13
14template <int N>
16 static Eigen::Matrix<double, N, 1> Add(
17 const Eigen::Matrix<double, N, 1>& v1,
18 const Eigen::Matrix<double, N, 1>& v2,
19 gtsam::OptionalJacobian<N, N> H1,
20 gtsam::OptionalJacobian<N, N> H2) {
21 if (H1) {
22 H1->setIdentity();
23 }
24 if (H2) {
25 H2->setIdentity();
26 }
27 return v1 + v2;
28 }
29
30 static Eigen::Matrix<double, N, 1>
31 Scale(const double s, const Eigen::Matrix<double, N, 1>& v, gtsam::OptionalJacobian<N, 1> H1, gtsam::OptionalJacobian<N, N> H2) {
32 if (H1) {
33 *H1 = v;
34 }
35 if (H2) {
36 *H2 = s * Eigen::Matrix<double, N, N>::Identity();
37 }
38 return s * v;
39 }
40
41 static Eigen::Matrix<double, N, 1> Product(
42 const Eigen::Matrix<double, N, 1>& v1,
43 const Eigen::Matrix<double, N, 1>& v2,
44 gtsam::OptionalJacobian<N, N> H1,
45 gtsam::OptionalJacobian<N, N> H2) {
46 if (H1) {
47 *H1 = v2.asDiagonal();
48 }
49 if (H2) {
50 *H2 = v1.asDiagonal();
51 }
52
53 return v1.array() * v2.array();
54 }
55};
56
57template <int N, int M>
59 static Eigen::Matrix<double, N + M, 1> Concatenate(
60 const Eigen::Matrix<double, N, 1>& x1,
61 const Eigen::Matrix<double, M, 1>& x2,
62 gtsam::OptionalJacobian<N + M, N> H1,
63 gtsam::OptionalJacobian<N + M, M> H2) {
64 if (H1) {
65 H1->setZero();
66 H1->template topLeftCorner<N, N>().setIdentity();
67 }
68 if (H2) {
69 H2->setZero();
70 H2->template bottomRightCorner<M, M>().setIdentity();
71 }
72
73 return (Eigen::Matrix<double, N + M, 1>() << x1, x2).finished();
74 }
75};
76
77} // namespace internal
78
79template <int N>
80gtsam::Expression<Eigen::Matrix<double, N, 1>> product(
81 const gtsam::Expression<Eigen::Matrix<double, N, 1>>& v1,
82 const gtsam::Expression<Eigen::Matrix<double, N, 1>>& v2) {
83 return gtsam::Expression<Eigen::Matrix<double, N, 1>>(&internal::vector_traits<N>::Product, v1, v2);
84}
85
86template <int N>
87gtsam::Expression<Eigen::Matrix<double, N, 1>> scale(const gtsam::Double_& s, const gtsam::Expression<Eigen::Matrix<double, N, 1>>& v) {
88 return gtsam::Expression<Eigen::Matrix<double, N, 1>>(&internal::vector_traits<N>::Scale, s, v);
89}
90
91template <int N>
92gtsam::Expression<Eigen::Matrix<double, N, 1>> add(
93 const gtsam::Expression<Eigen::Matrix<double, N, 1>>& v1,
94 const gtsam::Expression<Eigen::Matrix<double, N, 1>>& v2) {
95 return gtsam::Expression<Eigen::Matrix<double, N, 1>>(&internal::vector_traits<N>::Add, v1, v2);
96}
97
98template <int N, int M>
99gtsam::Expression<Eigen::Matrix<double, N + M, 1>> concatenate(
100 const gtsam::Expression<Eigen::Matrix<double, N, 1>>& x1,
101 const gtsam::Expression<Eigen::Matrix<double, M, 1>>& x2) {
102 return gtsam::Expression<Eigen::Matrix<double, N + M, 1>>(&internal::vector2_traits<N, M>::Concatenate, x1, x2);
103}
104
105inline gtsam::Pose3_ create_se3(const gtsam::Rot3_& rot, const gtsam::Vector3_& trans) {
106 return gtsam::Pose3_(&gtsam::Pose3::Create, rot, trans);
107}
108
109inline gtsam::Pose3_ expmap(const gtsam::Vector6_& x) {
110 return gtsam::Pose3_(&gtsam::Pose3::Expmap, x);
111}
112
113inline gtsam::Vector6_ logmap(const gtsam::Pose3_& x) {
114 return gtsam::Vector6_(&gtsam::Pose3::Logmap, x);
115}
116
117inline gtsam::Rot3_ expmap(const gtsam::Vector3_& x) {
118 return gtsam::Rot3_(&gtsam::Rot3::Expmap, x);
119}
120
121inline gtsam::Vector3_ logmap(const gtsam::Rot3_& x) {
122 return gtsam::Vector3_(&gtsam::Rot3::Logmap, x);
123}
124
125inline gtsam::Pose3_ inverse(const gtsam::Pose3_& x) {
126 auto f = [](const gtsam::Pose3& x, gtsam::OptionalJacobian<6, 6> H) { return x.inverse(H); };
127 return gtsam::Pose3_(f, x);
128}
129
130} // namespace gtsam_points
Definition expressions.hpp:58
Definition expressions.hpp:15