trajopt
 All Classes Namespaces Files Functions Variables Typedefs Pages
dynamics_utils.hpp
1 #include <openrave-core.h>
2 #include <openrave/openrave.h>
3 #include "trajopt/rave_utils.hpp"
4 #include "trajopt/utils.hpp"
5 #include "sco/modeling.hpp"
6 #include <boost/foreach.hpp>
7 #include "sco/expr_op_overloads.hpp"
8 using namespace Eigen;
9 using namespace std;
10 
11 namespace trajopt {
12 
13 
14 template <typename T> // var or expr
15 void FiniteDifferences(const BasicArray<T>& x, AffArray& dx, double dt) {
16  dx.resize(x.rows()-1, x.cols());
17  for (int i=0; i < x.rows(); ++i) {
18  for (int j=0; j < x.cols(); ++j) {
19  dx(i,j) = x(i+1,j) - x(i,j);
20  }
21  }
22 }
23 
24 
25 OpenRAVE::Vector toRaveQuat(const Vector4d& v) {
26  return OpenRAVE::Vector(v[0], v[1], v[2], v[3]);
27 }
28 OpenRAVE::Vector toRaveVector(const Vector3d& v) {
29  return OpenRAVE::Vector(v[0], v[1], v[2]);
30 }
31 
32 
33 template<typename T>
34 void extend(vector<T>& a, const vector<T>& b) {
35  a.insert(a.end(), b.begin(), b.end());
36 }
37 
38 template<typename T>
39 vector<T> concat(const vector<T>& a, const vector<T>& b, const vector<T>& c) {
40  vector<T> out;
41  vector<int> x;
42  out.insert(out.end(), a.begin(), a.end());
43  out.insert(out.end(), b.begin(), b.end());
44  out.insert(out.end(), c.begin(), c.end());
45  return out;
46 }
47 template<typename T>
48 vector<T> concat(const vector<T>& a, const vector<T>& b, const vector<T>& c, const vector<T>& d) {
49  vector<T> out;
50  vector<int> x;
51  out.insert(out.end(), a.begin(), a.end());
52  out.insert(out.end(), b.begin(), b.end());
53  out.insert(out.end(), c.begin(), c.end());
54  out.insert(out.end(), d.begin(), d.end());
55  return out;
56 }
57 template<typename T>
58 vector<T> concat(const vector<T>& a, const vector<T>& b, const vector<T>& c, const vector<T>& d, const vector<T>& e) {
59  vector<T> out;
60  vector<int> x;
61  out.insert(out.end(), a.begin(), a.end());
62  out.insert(out.end(), b.begin(), b.end());
63  out.insert(out.end(), c.begin(), c.end());
64  out.insert(out.end(), d.begin(), d.end());
65  out.insert(out.end(), e.begin(), e.end());
66  return out;
67 }
68 
69 
70 
71 
72 template<typename S, typename T, typename U>
73 vector<U> cross1(const vector<S>& a, const vector<T>& b) {
74  vector<U> c(3);
75  c[0] = a[1] * b[2] - a[2] * b[1];
76  c[1] = a[2] * b[0] - a[0] * b[2];
77  c[2] = a[0] * b[1] - a[1] * b[0];
78  return c;
79 }
80 
81 void exprInc(AffExprVector& a, const AffExprVector& b) {
82  for (int i=0; i < a.size(); ++i) {
83  exprInc(a[i], b[i]);
84  }
85 }
86 void exprDec(AffExprVector& a, const AffExprVector& b) {
87  for (int i=0; i < a.size(); ++i) {
88  exprDec(a[i], b[i]);
89  }
90 }
91 AffExprVector linearizedCrossProduct(const DblVec& x, const AffExprVector& a, const AffExprVector& b) {
92  assert(a.size() == 3 && b.size() == 3);
93  DblVec aval(3), bval(3);
94  for (int i=0; i < 3; ++i) {
95  aval[i] = a[i].value(x);
96  bval[i] = b[i].value(x);
97  }
98  AffExprVector c(3);
99  exprInc(c, cross1<AffExpr, double, AffExpr>(a, bval));
100  exprInc(c, cross1<double, AffExpr, AffExpr>(aval, b));
101  DblVec acrossbval = cross1<double,double,double>(aval, bval);
102  for (int i=0; i<3; ++i) exprDec(c[i], acrossbval[i]);
103  return c;
104 }
105 
106 
107 
108 AffExprVector transformExpr(const OR::Transform& T, const AffExprVector& v) {
109  OR::TransformMatrix M(T);
110  AffExprVector out(3);
111  for (int i=0; i < 3; ++i) {
112  for (int j=0; j < 3; ++j) {
113  exprInc(out[i], M.rot(i,j) * v[j]);
114  }
115  exprInc(out[i], T.trans[i]);
116  }
117  return out;
118 }
119 AffExprVector rotateExpr(const OR::Vector& rot, const AffExprVector& v) {
120  OR::Transform T;
121  T.rot = rot;
122  return transformExpr(T, v);
123 }
124 
125 
126 }