rby1-sdk
Loading...
Searching...
No Matches
so3.h
1#pragma once
2
3#include <Eigen/Core>
4#include <Eigen/Geometry>
5#include <optional>
6#include <utility>
7
8#include "constants.h"
9#include "rby1-sdk/export.h"
10
11namespace rb::math {
12
13enum class EulerAngleType { ZYX, ZYZ };
14
15class SO3;
16class so3;
17class so3v;
18
19class RBY1_SDK_API so3 {
20 public:
21 using MatrixType = Eigen::Matrix3d;
22
23 private:
24 so3() = default;
25};
26
27class RBY1_SDK_API so3v {
28 public:
29 using MatrixType = Eigen::Vector3d;
30
31 private:
32 so3v() = default;
33};
34
35class RBY1_SDK_API SO3 {
36 public:
37 using MatrixType = Eigen::Matrix3d;
38
39 static MatrixType Identity();
40
41 static MatrixType Inverse(const MatrixType& R);
42
43 static MatrixType Exp(so3v::MatrixType w, double angle = 1.0);
44
45 static so3v::MatrixType Log(const MatrixType& R);
46
47 static MatrixType RotX(double angle);
48
49 static MatrixType RotY(double angle);
50
51 static MatrixType RotZ(double angle);
52
53 static MatrixType FromEulerAngle(const Eigen::Vector3d& angles, EulerAngleType type);
54
55 static Eigen::Vector3d ToEulerAngle(const MatrixType& R, EulerAngleType type);
56
57 static MatrixType FromQuaternion(const Eigen::Quaterniond& q);
58
59 static Eigen::Quaterniond ToQuaternion(const MatrixType& R);
60
66 static MatrixType FromRPY(const Eigen::Vector3d& angles);
67
73 static Eigen::Vector3d ToRPY(const MatrixType& R);
74
75 static MatrixType Projection(const MatrixType& m);
76
77 template <typename Container, typename = std::enable_if_t<std::is_same_v<typename Container::value_type, MatrixType>>>
78 static std::optional<MatrixType> Average(const Container& matrices, double eps, int max_iter = -1) {
79 if (matrices.size() == 0) {
80 return Identity();
81 }
82
83 SO3::MatrixType avg = *matrices.begin();
84 for (int i = 0; max_iter < 0 || i < max_iter; i++) {
85 Eigen::Vector3d w = Eigen::Vector3d::Zero();
86
87 for (const auto& m : matrices) {
88 w += Log(m * avg.inverse());
89 }
90 w /= matrices.size();
91 avg = SO3::Exp(w) * avg;
92 if (w.norm() < eps) {
93 return avg;
94 }
95 }
96
97 return std::nullopt;
98 }
99
100 static Eigen::Vector3d GetX(const MatrixType& R);
101
102 static Eigen::Vector3d GetY(const MatrixType& R);
103
104 static Eigen::Vector3d GetZ(const MatrixType& R);
105
106 static so3v::MatrixType Vec(const so3::MatrixType& r);
107
108 static so3::MatrixType Hat(const so3v::MatrixType& w);
109
110 private:
111 SO3() = default;
112};
113
114} // namespace rb::math
Definition so3.h:35
static MatrixType FromRPY(const Eigen::Vector3d &angles)
static Eigen::Vector3d ToRPY(const MatrixType &R)
Definition so3.h:19
Definition so3.h:27
Definition log.h:12