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"
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);
25 OpenRAVE::Vector toRaveQuat(
const Vector4d& v) {
26 return OpenRAVE::Vector(v[0], v[1], v[2], v[3]);
28 OpenRAVE::Vector toRaveVector(
const Vector3d& v) {
29 return OpenRAVE::Vector(v[0], v[1], v[2]);
34 void extend(vector<T>& a,
const vector<T>& b) {
35 a.insert(a.end(), b.begin(), b.end());
39 vector<T> concat(
const vector<T>& a,
const vector<T>& b,
const vector<T>& c) {
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());
48 vector<T> concat(
const vector<T>& a,
const vector<T>& b,
const vector<T>& c,
const vector<T>& d) {
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());
58 vector<T> concat(
const vector<T>& a,
const vector<T>& b,
const vector<T>& c,
const vector<T>& d,
const vector<T>& e) {
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());
72 template<
typename S,
typename T,
typename U>
73 vector<U> cross1(
const vector<S>& a,
const vector<T>& b) {
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];
81 void exprInc(AffExprVector& a,
const AffExprVector& b) {
82 for (
int i=0; i < a.size(); ++i) {
86 void exprDec(AffExprVector& a,
const AffExprVector& b) {
87 for (
int i=0; i < a.size(); ++i) {
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);
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]);
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]);
115 exprInc(out[i], T.trans[i]);
119 AffExprVector rotateExpr(
const OR::Vector& rot,
const AffExprVector& v) {
122 return transformExpr(T, v);