ASCO Aerial Autonomy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
math.h
Go to the documentation of this file.
1 #pragma once
2 
6 #define _USE_MATH_DEFINES
7 #include <cmath>
8 #include <stdexcept>
9 #include <tf/tf.h>
10 
15 namespace math {
16 
22 double angleWrap(double x);
23 
31 double clamp(double x, double min, double max);
32 
41 template <class T> tf::Transform getTransformFromVector(const T &input) {
42  tf::Transform transform;
43  if (input.size() != 6) {
44  throw std::runtime_error("The input does not have 6 elements x,y,z, r,p,y");
45  } else {
46  transform.setOrigin(tf::Vector3(input[0], input[1], input[2]));
47  transform.setRotation(
48  tf::createQuaternionFromRPY(input[3], input[4], input[5]));
49  }
50  return transform;
51 }
52 
67 template <class T>
68 std::vector<tf::Transform> getTransformsFromVector(const T &input) {
69  if (input.size() % 6 != 0) {
70  throw std::runtime_error(
71  "The input does not have a multiple of 6 elements x,y,z, r,p,y");
72  }
73 
74  std::vector<tf::Transform> transforms(input.size() / 6);
75  for (int i = 0, j = 0; i < input.size(); i += 6, j++) {
76  transforms.at(j).setOrigin(
77  tf::Vector3(input[i + 0], input[i + 1], input[i + 2]));
78  transforms.at(j).setRotation(
79  tf::createQuaternionFromRPY(input[i + 3], input[i + 4], input[i + 5]));
80  }
81  return transforms;
82 }
83 }
double clamp(double x, double min, double max)
Clip a number to bewteen a min and max value.
Definition: math.cpp:18
double angleWrap(double x)
Wrap an angle to be in the range [-pi, pi)
Definition: math.cpp:11
tf::Transform getTransformFromVector(const T &input)
Generate a tf transform from a vector of xyzrpy.
Definition: math.h:41
std::vector< tf::Transform > getTransformsFromVector(const T &input)
Generate a vector of tf transforms from a vector of xyzrpy.
Definition: math.h:68