15 double& x,
double& y,
double& z,
16 double& roll,
double& pitch,
22 roll = atan2(t(2, 1), t(2, 2));
23 pitch = asin(-t(2, 0));
24 yaw = atan2(t(1, 0), t(0, 0));
28 const double& x,
const double& y,
const double& z,
const double& roll,
const double& pitch,
const double& yaw,
29 Eigen::Transform<double, 3, Eigen::Affine> &t)
31 double A = cos(yaw), B = sin(yaw), C = cos(pitch), D = sin(pitch), E = cos(roll), F = sin(roll), DE = D * E, DF = D * F;
34 t(0, 1) = A * DF - B * E;
35 t(0, 2) = B * F + A * DE;
38 t(1, 1) = A * E + B * DF;
39 t(1, 2) = B * DE - A * F;
53 using namespace boost;
54 static std::tr1::mt19937 rng(static_cast<unsigned>(std::time(0)));
56 std::tr1::normal_distribution<double> norm_dist(mean, std_dev);
58 std::tr1::variate_generator<std::tr1::mt19937&, std::tr1::normal_distribution<double> > normal_sampler(
61 return (normal_sampler());
65 std::vector<Feature::Id> vector_ids)
67 for (
size_t idx = 0; idx < vector_ids.size(); idx++)
69 os << vector_ids.at(idx) <<
" ";
76 os <<
"(" << boost::tuples::get<0>(location) <<
"," 77 << boost::tuples::get<1>(location) <<
"," 78 << boost::tuples::get<2>(location) <<
")";
85 os <<
"(" << twistd.vx() <<
"," 90 << twistd.rz() <<
")";
97 double error_value = sqrt( pow(boost::tuples::get<0>(first) - boost::tuples::get<0>(second), 2)
98 + pow(boost::tuples::get<1>(first) - boost::tuples::get<1>(second), 2)
99 + pow(boost::tuples::get<2>(first) - boost::tuples::get<2>(second), 2));
107 Eigen::Displacementd displ_from_transf(transformation_matrix);
108 twist = displ_from_transf.log(1e-12);
113 Eigen::Displacementd displ_from_transf(transformation_matrix);
114 twist = displ_from_transf.log(1e-12);
119 Eigen::Displacementd displ_from_twist = transformation_twist.exp(1e-12);
120 matrix = displ_from_twist.toHomogeneousMatrix();
125 Eigen::Affine3d motion_matrix(transformation);
126 Eigen::Vector3d origin_vector = Eigen::Vector3d(
127 boost::tuples::get<0>(origin), boost::tuples::get<1>(origin),
128 boost::tuples::get<2>(origin));
129 Eigen::Vector3d destination_vector = motion_matrix * origin_vector;
130 boost::tuples::get<0>(new_location) = destination_vector[0];
131 boost::tuples::get<1>(new_location)= destination_vector[1];
132 boost::tuples::get<2>(new_location) = destination_vector[2];
137 Eigen::Matrix4d eigen_transform;
160 Eigen::Twistd eigen_twist;
167 col_vec(1) = boost::tuples::get<0>(lof);
168 col_vec(2) = boost::tuples::get<1>(lof);
169 col_vec(3) = boost::tuples::get<2>(lof);
174 col_vec(1) = boost::tuples::get<0>(lof);
175 col_vec(2) = boost::tuples::get<1>(lof);
176 col_vec(3) = boost::tuples::get<2>(lof);
182 eig_vec(0) = boost::tuples::get<0>(lof);
183 eig_vec(1) = boost::tuples::get<1>(lof);
184 eig_vec(2) = boost::tuples::get<2>(lof);
192 boost::tuples::get<0>(location1) - boost::tuples::get<0>(location2),
193 boost::tuples::get<1>(location1) - boost::tuples::get<1>(location2),
194 boost::tuples::get<2>(location1) - boost::tuples::get<2>(location2));
201 boost::tuples::get<0>(location1) + boost::tuples::get<0>(location2),
202 boost::tuples::get<1>(location1) + boost::tuples::get<1>(location2),
203 boost::tuples::get<2>(location1) + boost::tuples::get<2>(location2));
208 eigen_twist = Eigen::Twistd(gm_twist.angular.x, gm_twist.angular.y, gm_twist.angular.z,
209 gm_twist.linear.x, gm_twist.linear.y, gm_twist.linear.z);
214 gm_twist.linear.x = eigen_twist.getLinearVelocity().x();
215 gm_twist.linear.y = eigen_twist.getLinearVelocity().y();
216 gm_twist.linear.z = eigen_twist.getLinearVelocity().z();
217 gm_twist.angular.x = eigen_twist.getAngularVelocity().x();
218 gm_twist.angular.y = eigen_twist.getAngularVelocity().y();
219 gm_twist.angular.z = eigen_twist.getAngularVelocity().z();
225 for (
int col = 0; col < transformation.cols(); ++col)
227 for (
int row = 0; row < transformation.rows(); ++row)
229 if (isnan(transformation(row, col)))
241 point_pcl.x = boost::tuples::get<0>(point_location);
242 point_pcl.y = boost::tuples::get<1>(point_location);
243 point_pcl.z = boost::tuples::get<2>(point_location);
248 feature_pcl.x = boost::tuples::get<0>(feature_location);
249 feature_pcl.y = boost::tuples::get<1>(feature_location);
250 feature_pcl.z = boost::tuples::get<2>(feature_location);
251 feature_pcl.label = feature_id;
256 feature_pcl.x = boost::tuples::get<0>(feature_location);
257 feature_pcl.y = boost::tuples::get<1>(feature_location);
258 feature_pcl.z = boost::tuples::get<2>(feature_location);
259 feature_pcl.
label = feature_id;
264 geometry_msgs::Vector3 linear = ros_twist.linear;
265 geometry_msgs::Vector3 angular = ros_twist.angular;
266 eigen_twist = Eigen::Twistd(angular.x, angular.y, angular.z,
267 linear.x, linear.y, linear.z);
272 x = fmod(x + M_PI,2*M_PI);
282 double dif = fmod(b - a + M_PI,2*M_PI);
287 inline double unwrap(
double previousAngle,
double newAngle){
291 Eigen::Twistd
omip::unwrapTwist(Eigen::Twistd& current_twist, Eigen::Displacementd& current_displacement, Eigen::Twistd& previous_twist,
bool& changed)
293 Eigen::Vector3d current_angular_component = Eigen::Vector3d(current_twist.rx(), current_twist.ry(), current_twist.rz());
294 double current_rotation_angle = current_angular_component.norm();
295 Eigen::Vector3d current_angular_direction = current_angular_component / current_rotation_angle;
296 Eigen::Vector3d previous_angular_component = Eigen::Vector3d(previous_twist.rx(), previous_twist.ry(), previous_twist.rz());
297 double previous_rotation_angle = previous_angular_component.norm();
298 Eigen::Vector3d previous_angular_direction = previous_angular_component / previous_rotation_angle;
301 if(Eigen::Vector3d(current_twist.rx()-previous_twist.rx(), current_twist.ry()-previous_twist.ry(), current_twist.rz()-previous_twist.rz()).norm() > M_PI
302 || current_angular_direction.dot(previous_angular_direction) < -0.8)
305 Eigen::Vector3d new_angular_component;
307 Eigen::Twistd unwrapped_twist;
311 if(previous_rotation_angle + 0.3 < 2*M_PI)
314 if(current_angular_direction.dot(previous_angular_direction) < 0)
316 new_angular_component= M_PI*previous_angular_direction -(M_PI - current_rotation_angle)*current_angular_direction;
319 new_angular_component= M_PI*previous_angular_direction +(M_PI - current_rotation_angle)*current_angular_direction;
322 ROS_ERROR_STREAM(
"Numeric instability in the logarithm of a homogeneous transformation!");
324 if(current_angular_direction.dot(previous_angular_direction) < 0)
326 new_angular_component= std::min(2*M_PI, previous_rotation_angle)*previous_angular_direction +current_rotation_angle*current_angular_direction;
329 new_angular_component= std::min(2*M_PI, previous_rotation_angle)*previous_angular_direction +current_rotation_angle*current_angular_direction;
333 double n2 = new_angular_component.squaredNorm();
336 double val = (double(2.0) * sn - n * (double(1.0) + cos(n))) / (double(2.0) *n2 * sn);
338 Eigen::Vector3d RE3Element = Eigen::Vector3d(current_displacement.getR3Element()(0),current_displacement.getR3Element()(1),current_displacement.getR3Element()(2) );
339 Eigen::Vector3d lin = -0.5*new_angular_component.cross(RE3Element);
340 lin += (double(1.0) - val * n2 ) * RE3Element;
341 lin += val * new_angular_component.dot(RE3Element) * new_angular_component;
343 unwrapped_twist = Eigen::Twistd(new_angular_component.x(), new_angular_component.y(), new_angular_component.z(), lin.x(), lin.y(), lin.z());
345 if(std::fabs(val) > 10)
347 ROS_ERROR_STREAM(
"Numeric instability in the logarithm of a homogeneous transformation. Val: " << val);
351 return unwrapped_twist;
354 return current_twist;
358 Eigen::Twistd
omip::invertTwist(Eigen::Twistd& current_twist, Eigen::Twistd& previous_twist,
bool& inverted)
360 Eigen::Vector3d current_angular_component = Eigen::Vector3d(current_twist.rx(), current_twist.ry(), current_twist.rz());
361 double current_rotation_angle = current_angular_component.norm();
362 Eigen::Vector3d current_angular_direction = current_angular_component / current_rotation_angle;
363 Eigen::Vector3d previous_angular_component = Eigen::Vector3d(previous_twist.rx(), previous_twist.ry(), previous_twist.rz());
364 double previous_rotation_angle = previous_angular_component.norm();
365 Eigen::Vector3d previous_angular_direction = previous_angular_component / previous_rotation_angle;
367 if(Eigen::Vector3d(current_twist.rx()-previous_twist.rx(), current_twist.ry()-previous_twist.ry(), current_twist.rz()-previous_twist.rz()).norm() > M_PI
368 || current_angular_direction.dot(previous_angular_direction) < -0.8)
371 return -current_twist;
374 return current_twist;
380 double determinant = +to_inv(1,1)*(to_inv(2,2)*to_inv(3,3)-to_inv(3,2)*to_inv(2,3))
381 -to_inv(1,2)*(to_inv(2,1)*to_inv(3,3)-to_inv(2,3)*to_inv(3,1))
382 +to_inv(1,3)*(to_inv(2,1)*to_inv(3,2)-to_inv(2,2)*to_inv(3,1));
383 double invdet = 1.0/determinant;
384 inverse(1,1) = (to_inv(2,2)*to_inv(3,3)-to_inv(3,2)*to_inv(2,3))*invdet;
385 inverse(2,1) = -(to_inv(1,2)*to_inv(3,3)-to_inv(1,3)*to_inv(3,2))*invdet;
386 inverse(3,1) = (to_inv(1,2)*to_inv(2,3)-to_inv(1,3)*to_inv(2,2))*invdet;
387 inverse(1,2) = -(to_inv(2,1)*to_inv(3,3)-to_inv(2,3)*to_inv(3,1))*invdet;
388 inverse(2,2) = (to_inv(1,1)*to_inv(3,3)-to_inv(1,3)*to_inv(3,1))*invdet;
389 inverse(3,2) = -(to_inv(1,1)*to_inv(2,3)-to_inv(2,1)*to_inv(1,3))*invdet;
390 inverse(1,3) = (to_inv(2,1)*to_inv(3,2)-to_inv(3,1)*to_inv(2,2))*invdet;
391 inverse(2,3) = -(to_inv(1,1)*to_inv(3,2)-to_inv(3,1)*to_inv(1,2))*invdet;
392 inverse(3,3) = (to_inv(1,1)*to_inv(2,2)-to_inv(2,1)*to_inv(1,2))*invdet;
396 Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> >& inverse)
398 double determinant = +to_inv(0,0)*(to_inv(1,1)*to_inv(2,2)-to_inv(2,1)*to_inv(1,2))
399 -to_inv(0,1)*(to_inv(1,0)*to_inv(2,2)-to_inv(1,2)*to_inv(2,0))
400 +to_inv(0,2)*(to_inv(1,0)*to_inv(2,1)-to_inv(1,1)*to_inv(2,0));
401 double invdet = 1.0/determinant;
402 inverse(0,0) = (to_inv(1,1)*to_inv(2,2)-to_inv(2,1)*to_inv(1,2))*invdet;
403 inverse(1,0) = -(to_inv(0,1)*to_inv(2,2)-to_inv(0,2)*to_inv(2,1))*invdet;
404 inverse(2,0) = (to_inv(0,1)*to_inv(1,2)-to_inv(0,2)*to_inv(1,1))*invdet;
405 inverse(0,1) = -(to_inv(1,0)*to_inv(2,2)-to_inv(1,2)*to_inv(2,0))*invdet;
406 inverse(1,1) = (to_inv(0,0)*to_inv(2,2)-to_inv(0,2)*to_inv(2,0))*invdet;
407 inverse(2,1) = -(to_inv(0,0)*to_inv(1,2)-to_inv(1,0)*to_inv(0,2))*invdet;
408 inverse(0,2) = (to_inv(1,0)*to_inv(2,1)-to_inv(2,0)*to_inv(1,1))*invdet;
409 inverse(1,2) = -(to_inv(0,0)*to_inv(2,1)-to_inv(2,0)*to_inv(0,1))*invdet;
410 inverse(2,2) = (to_inv(0,0)*to_inv(1,1)-to_inv(1,0)*to_inv(0,1))*invdet;
414 Eigen::Matrix3d& inverse)
416 double determinant = +to_inv(0,0)*(to_inv(1,1)*to_inv(2,2)-to_inv(2,1)*to_inv(1,2))
417 -to_inv(0,1)*(to_inv(1,0)*to_inv(2,2)-to_inv(1,2)*to_inv(2,0))
418 +to_inv(0,2)*(to_inv(1,0)*to_inv(2,1)-to_inv(1,1)*to_inv(2,0));
419 double invdet = 1.0/determinant;
420 inverse(0,0) = (to_inv(1,1)*to_inv(2,2)-to_inv(2,1)*to_inv(1,2))*invdet;
421 inverse(1,0) = -(to_inv(0,1)*to_inv(2,2)-to_inv(0,2)*to_inv(2,1))*invdet;
422 inverse(2,0) = (to_inv(0,1)*to_inv(1,2)-to_inv(0,2)*to_inv(1,1))*invdet;
423 inverse(0,1) = -(to_inv(1,0)*to_inv(2,2)-to_inv(1,2)*to_inv(2,0))*invdet;
424 inverse(1,1) = (to_inv(0,0)*to_inv(2,2)-to_inv(0,2)*to_inv(2,0))*invdet;
425 inverse(2,1) = -(to_inv(0,0)*to_inv(1,2)-to_inv(1,0)*to_inv(0,2))*invdet;
426 inverse(0,2) = (to_inv(1,0)*to_inv(2,1)-to_inv(2,0)*to_inv(1,1))*invdet;
427 inverse(1,2) = -(to_inv(0,0)*to_inv(2,1)-to_inv(2,0)*to_inv(0,1))*invdet;
428 inverse(2,2) = (to_inv(0,0)*to_inv(1,1)-to_inv(1,0)*to_inv(0,1))*invdet;
boost::tuple< double, double, double > Location
Feature::Location operator-(const Feature::Location &location1, const Feature::Location &location2)
void EigenAffine2TranslationAndEulerAngles(const Eigen::Affine3d &t, double &x, double &y, double &z, double &roll, double &pitch, double &yaw)
double L2Distance(const Feature::Location &first, const Feature::Location &second)
OMIP_ADD_POINT4D uint32_t label
void TransformMatrix2Twist(const Eigen::Matrix4d &transformation_matrix, Eigen::Twistd &twist)
bool isFinite(const Eigen::Matrix4d &transformation)
Feature::Location operator+(const Feature::Location &location1, const Feature::Location &location2)
void Twist2TransformMatrix(const Eigen::Twistd &transformation_twist, Eigen::Matrix4d &matrix)
void TranslationAndEulerAngles2EigenAffine(const double &x, const double &y, const double &z, const double &roll, const double &pitch, const double &yaw, Eigen::Transform< double, 3, Eigen::Affine > &t)
double constrainAngle(double x)
TFSIMD_FORCE_INLINE Quaternion inverse(const Quaternion &q)
void ROSTwist2EigenTwist(const geometry_msgs::Twist &ros_twist, Eigen::Twistd &eigen_twist)
void LocationOfFeature2ColumnVectorHomogeneous(const Feature::Location &lof, MatrixWrapper::ColumnVector &col_vec)
void invert3x3MatrixEigen2(const Eigen::Matrix3d &to_inv, Eigen::Matrix3d &inverse)
double sampleNormal(double mean, double std_dev)
Eigen::Twistd unwrapTwist(Eigen::Twistd ¤t_twist, Eigen::Displacementd ¤t_displacement, Eigen::Twistd &previous_twist, bool &changed)
void TransformMatrix2TwistUnwrapped(const Eigen::Matrix4d &transformation_matrix, Eigen::Twistd &twist, const Eigen::Twistd &twist_previous)
void invert3x3Matrix(const MatrixWrapper::Matrix &to_inv, MatrixWrapper::Matrix &inverse)
double angleDiff(double a, double b)
void LocationOfFeature2ColumnVector(const Feature::Location &lof, MatrixWrapper::ColumnVector &col_vec)
void LocationOfFeature2EigenVectorHomogeneous(const Feature::Location &lof, Eigen::Vector4d &eig_vec)
pcl::PointXYZL FeaturePCL
void LocationAndId2FeaturePCL(const Feature::Location &feature_location, const Feature::Id &feature_id, pcl::PointXYZL &feature_pcl)
double angleConv(double angle)
void GeometryMsgsTwist2EigenTwist(const geometry_msgs::Twist &gm_twist, Eigen::Twistd &eigen_twist)
void TransformLocation(const Feature::Location &origin, const Eigen::Matrix4d &transformation, Feature::Location &new_location)
void LocationAndId2FeaturePCLwc(const Feature::Location &feature_location, const Feature::Id &feature_id, omip::FeaturePCLwc &feature_pcl)
std::ostream & operator<<(std::ostream &os, std::vector< Feature::Id > vector_ids)
void invert3x3MatrixEigen(const Eigen::Map< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > > &to_inv, Eigen::Map< Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > > &inverse)
#define ROS_ERROR_STREAM(args)
void Location2PointPCL(const Feature::Location &point_location, pcl::PointXYZ &point_pcl)
double unwrap(double previousAngle, double newAngle)
Eigen::Twistd invertTwist(Eigen::Twistd ¤t_twist, Eigen::Twistd &previous_twist, bool &inverted)
void EigenTwist2GeometryMsgsTwist(Eigen::Twistd &eigen_twist, geometry_msgs::Twist &gm_twist)