17 #ifndef HPP_CONSTRAINTS_TOOL_HH 18 #define HPP_CONSTRAINTS_TOOL_HH 20 #include <boost/math/constants/constants.hpp> 22 #include <pinocchio/spatial/se3.hpp> 27 namespace constraints {
29 template <
typename VectorType,
typename MatrixType >
30 static void computeCrossMatrix (
const VectorType& v, MatrixType& m)
32 m.diagonal ().setZero ();
33 m (0,1) = -v [2]; m (1,0) = v [2];
34 m (0,2) = v [1]; m (2,0) = -v [1];
35 m (1,2) = -v [0]; m (2,1) = v [0];
47 template <
typename Derived>
inline void logSO3 49 Eigen::MatrixBase<Derived>
const& result)
51 Eigen::MatrixBase<Derived>& value =
const_cast<Eigen::MatrixBase<Derived>&
> (result);
52 const value_type PI = ::boost::math::constants::pi<value_type>();
54 if (tr > 3) theta = 0;
55 else if (tr < -1) theta = PI;
56 else theta = acos ((tr - 1)/2);
59 if (theta < PI - 1e-2) {
60 const value_type t = ((theta > 1e-6)? theta / sin(theta) : 1) / 2;
61 value(0) = t * (R (2, 1) - R (1, 2));
62 value(1) = t * (R (0, 2) - R (2, 0));
63 value(2) = t * (R (1, 0) - R (0, 1));
70 const value_type beta = theta*theta / ( 1 + cphi );
71 const value_type tmp0 = (R (0, 0) + cphi) * beta;
72 const value_type tmp1 = (R (1, 1) + cphi) * beta;
73 const value_type tmp2 = (R (2, 2) + cphi) * beta;
74 value(0) = (R (2, 1) > R (1, 2) ? 1 : -1) * (tmp0 > 0 ? sqrt(tmp0) : 0);
75 value(1) = (R (0, 2) > R (2, 0) ? 1 : -1) * (tmp1 > 0 ? sqrt(tmp1) : 0);
76 value(2) = (R (1, 0) > R (0, 1) ? 1 : -1) * (tmp2 > 0 ? sqrt(tmp2) : 0);
108 template <
typename Derived>
110 const Eigen::MatrixBase<Derived>& log,
matrix3_t& Jlog)
116 const value_type ct = cos(theta), st = sin(theta);
120 Jlog.diagonal().setConstant (theta*st_1mct);
123 Jlog(0,1) = -log(2); Jlog(1,0) = log(2);
124 Jlog(0,2) = log(1); Jlog(2,0) = -log(1);
125 Jlog(1,2) = -log(0); Jlog(2,1) = log(0);
128 const value_type alpha = 1/(theta*theta) - st_1mct/(2*theta);
129 Jlog.noalias() += alpha * log * log.transpose ();
141 template <
typename Derived>
inline void logSE3 144 assert (result.size () == 6);
145 Eigen::MatrixBase<Derived>& value =
146 const_cast<Eigen::MatrixBase<Derived>&
> (result);
152 value.segment (3, 3) =
r;
154 if (fabs (theta) < 1e-2) {
155 alpha = 1 - theta*theta/12 - theta*theta*theta*theta/720;
156 beta = 1./12 + theta*theta/720;
158 alpha = theta*sin(theta)/(2*(1-cos(theta)));
159 beta = 1/(theta*theta) - sin (theta)/(2*theta*(1-cos(theta)));
161 matrix3_t rcross; computeCrossMatrix (r, rcross);
162 value.segment (0, 3) = alpha*p - .5*rcross*p + beta*r.dot (p)*
r;
165 template <
typename Derived>
167 Eigen::MatrixBase<Derived>
const& Jlog)
169 Eigen::MatrixBase<Derived>& value =
170 const_cast<Eigen::MatrixBase<Derived>&
> (Jlog);
178 if (fabs (theta) < 1e-2) {
179 alpha = 1 - theta*theta/12 - theta*theta*theta*theta/720;
180 beta = 1./12 + theta*theta/720;
181 beta_dot_over_theta = 1. / 360.;
183 alpha = theta*sin(theta)/(2*(1-cos(theta)));
184 beta = 1/(theta*theta) - sin (theta)/(2*theta*(1-cos(theta)));
185 beta_dot_over_theta = -2/(theta*theta*theta*theta) +
186 (theta + sin (theta)) / (2*theta*theta*theta*(1-cos(theta)));
188 matrix3_t rcross; computeCrossMatrix (r, rcross);
189 matrix3_t V (alpha * matrix3_t::Identity () - .5*rcross +
190 beta * r * r.transpose ());
192 matrix3_t pcross; computeCrossMatrix (p, pcross);
193 matrix3_t J ((.5*pcross + (beta_dot_over_theta*rTp)*r*r.transpose ()
194 - (theta*theta*beta_dot_over_theta+2*beta)*p*r.transpose ()
195 + rTp * beta * matrix3_t::Identity ()
196 + beta * r*p.transpose ()) * Jlog3);
197 value.block (0, 0, 3, 3) = V * R;
198 value.block (0, 3, 3, 3) = J;
199 Eigen::Block<Derived>
b (value.block (3, 0, 3, 3));
201 value.block (3, 3, 3, 3) = Jlog3;
204 template <
typename Derived1,
typename Derived2>
void matrixToQuat 205 (
const Eigen::MatrixBase<Derived1>& M, Eigen::MatrixBase<Derived2>
const& q)
207 Derived2& _q =
const_cast<Derived2&
> (q.derived());
209 Eigen::Map<Transform3f::Quaternion> quat (_q.data());
216 Derived& _q =
const_cast<Derived&
> (q.derived());
218 _q.template head<3>() = M.translation();
226 #endif // HPP_CONSTRAINTS_TOOL_HH
pinocchio::matrix3_t matrix3_t
Definition: fwd.hh:40
pinocchio::vector3_t vector3_t
Definition: fwd.hh:39
assert(d.lhs()._blocks()==d.rhs()._blocks())
pinocchio::value_type value_type
Definition: fwd.hh:36
pinocchio::Transform3f Transform3f
Definition: fwd.hh:50