Control Libraries 7.4.0
Loading...
Searching...
No Matches
MathTools.cpp
1#include "state_representation/MathTools.hpp"
2
3namespace state_representation::math_tools {
4const Eigen::Quaterniond log(const Eigen::Quaterniond& q) {
5 auto q_tmp = q;
6 if (q_tmp.w() < 0) {
7 q_tmp = Eigen::Quaterniond(-q_tmp.coeffs());
8 }
9 Eigen::Quaterniond log_q = Eigen::Quaterniond(0, 0, 0, 0);
10 double q_norm = q_tmp.vec().norm();
11 if (q_norm > 1e-4) {
12 log_q.vec() = (q_tmp.vec() / q_norm) * acos(std::min<double>(std::max<double>(q_tmp.w(), -1), 1));
13 }
14 return log_q;
15}
16
17const Eigen::Quaterniond exp(const Eigen::Quaterniond& q, double lambda) {
18 Eigen::Quaterniond exp_q = Eigen::Quaterniond::Identity();
19 double q_norm = q.vec().norm();
20 if (q_norm > 1e-4) {
21 exp_q.w() = cos(q_norm * lambda);
22 exp_q.vec() = (q.vec() / q_norm) * sin(q_norm * lambda);
23 }
24 return exp_q;
25}
26
27const std::vector<double> linspace(double start, double end, unsigned int number_of_points) {
28 // catch rarely, throw often
29 if (number_of_points < 2) {
30 throw new std::exception();
31 }
32 int partitions = number_of_points - 1;
33 std::vector<double> pts;
34 // length of each segment
35 double length = (end - start) / partitions;
36 // first, not to change
37 pts.push_back(start);
38 for (unsigned int i = 1; i < number_of_points - 1; i++) {
39 pts.push_back(start + i * length);
40 }
41 // last, not to change
42 pts.push_back(end);
43 return pts;
44}
45}