Control Libraries 6.3.4
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 Eigen::Quaterniond log_q = Eigen::Quaterniond(0, 0, 0, 0);
6 double q_norm = q.vec().norm();
7 if (q_norm > 1e-4) { log_q.vec() = (q.vec() / q_norm) * acos(std::min<double>(std::max<double>(q.w(), -1), 1)); }
8 return log_q;
9}
10
11const Eigen::Quaterniond exp(const Eigen::Quaterniond& q, double lambda) {
12 Eigen::Quaterniond exp_q = Eigen::Quaterniond::Identity();
13 double q_norm = q.vec().norm();
14 if (q_norm > 1e-4) {
15 exp_q.w() = cos(q_norm * lambda);
16 exp_q.vec() = (q.vec() / q_norm) * sin(q_norm * lambda);
17 }
18 return exp_q;
19}
20
21const std::vector<double> linspace(double start, double end, unsigned int number_of_points) {
22 // catch rarely, throw often
23 if (number_of_points < 2) {
24 throw new std::exception();
25 }
26 int partitions = number_of_points - 1;
27 std::vector<double> pts;
28 // length of each segment
29 double length = (end - start) / partitions;
30 // first, not to change
31 pts.push_back(start);
32 for (unsigned int i = 1; i < number_of_points - 1; i++) {
33 pts.push_back(start + i * length);
34 }
35 // last, not to change
36 pts.push_back(end);
37 return pts;
38}
39}