/* * Copyright 2016 The Cartographer Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_ #define CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_ #include #include #include #include "Eigen/Core" #include "Eigen/Geometry" #include "absl/strings/substitute.h" #include "cartographer/common/lua_parameter_dictionary.h" #include "cartographer/common/math.h" #include "cartographer/common/port.h" namespace cartographer { namespace transform { template class Rigid2 { public: using Vector = Eigen::Matrix; using Rotation2D = Eigen::Rotation2D; Rigid2() : translation_(Vector::Zero()), rotation_(Rotation2D::Identity()) {} Rigid2(const Vector& translation, const Rotation2D& rotation) : translation_(translation), rotation_(rotation) {} Rigid2(const Vector& translation, const double rotation) : translation_(translation), rotation_(rotation) {} static Rigid2 Rotation(const double rotation) { return Rigid2(Vector::Zero(), rotation); } static Rigid2 Rotation(const Rotation2D& rotation) { return Rigid2(Vector::Zero(), rotation); } static Rigid2 Translation(const Vector& vector) { return Rigid2(vector, Rotation2D::Identity()); } static Rigid2 Identity() { return Rigid2(); } template Rigid2 cast() const { return Rigid2(translation_.template cast(), rotation_.template cast()); } const Vector& translation() const { return translation_; } Rotation2D rotation() const { return rotation_; } double normalized_angle() const { return common::NormalizeAngleDifference(rotation().angle()); } Rigid2 inverse() const { const Rotation2D rotation = rotation_.inverse(); const Vector translation = -(rotation * translation_); return Rigid2(translation, rotation); } std::string DebugString() const { return absl::Substitute("{ t: [$0, $1], r: [$2] }", translation().x(), translation().y(), rotation().angle()); } private: Vector translation_; Rotation2D rotation_; }; template Rigid2 operator*(const Rigid2& lhs, const Rigid2& rhs) { return Rigid2( lhs.rotation() * rhs.translation() + lhs.translation(), lhs.rotation() * rhs.rotation()); } template typename Rigid2::Vector operator*( const Rigid2& rigid, const typename Rigid2::Vector& point) { return rigid.rotation() * point + rigid.translation(); } // This is needed for gmock. template std::ostream& operator<<(std::ostream& os, const cartographer::transform::Rigid2& rigid) { os << rigid.DebugString(); return os; } using Rigid2d = Rigid2; using Rigid2f = Rigid2; template class Rigid3 { public: using Vector = Eigen::Matrix; using Quaternion = Eigen::Quaternion; using AngleAxis = Eigen::AngleAxis; Rigid3() : translation_(Vector::Zero()), rotation_(Quaternion::Identity()) {} Rigid3(const Vector& translation, const Quaternion& rotation) : translation_(translation), rotation_(rotation) {} Rigid3(const Vector& translation, const AngleAxis& rotation) : translation_(translation), rotation_(rotation) {} static Rigid3 Rotation(const AngleAxis& angle_axis) { return Rigid3(Vector::Zero(), Quaternion(angle_axis)); } static Rigid3 Rotation(const Quaternion& rotation) { return Rigid3(Vector::Zero(), rotation); } static Rigid3 Translation(const Vector& vector) { return Rigid3(vector, Quaternion::Identity()); } static Rigid3 FromArrays(const std::array& rotation, const std::array& translation) { return Rigid3(Eigen::Map(translation.data()), Eigen::Quaternion(rotation[0], rotation[1], rotation[2], rotation[3])); } static Rigid3 Identity() { return Rigid3(); } template Rigid3 cast() const { return Rigid3(translation_.template cast(), rotation_.template cast()); } const Vector& translation() const { return translation_; } const Quaternion& rotation() const { return rotation_; } Rigid3 inverse() const { const Quaternion rotation = rotation_.conjugate(); const Vector translation = -(rotation * translation_); return Rigid3(translation, rotation); } std::string DebugString() const { return absl::Substitute("{ t: [$0, $1, $2], q: [$3, $4, $5, $6] }", translation().x(), translation().y(), translation().z(), rotation().w(), rotation().x(), rotation().y(), rotation().z()); } bool IsValid() const { return !std::isnan(translation_.x()) && !std::isnan(translation_.y()) && !std::isnan(translation_.z()) && std::abs(FloatType(1) - rotation_.norm()) < FloatType(1e-3); } private: Vector translation_; Quaternion rotation_; }; template Rigid3 operator*(const Rigid3& lhs, const Rigid3& rhs) { return Rigid3( lhs.rotation() * rhs.translation() + lhs.translation(), (lhs.rotation() * rhs.rotation()).normalized()); } template typename Rigid3::Vector operator*( const Rigid3& rigid, const typename Rigid3::Vector& point) { return rigid.rotation() * point + rigid.translation(); } // This is needed for gmock. template std::ostream& operator<<(std::ostream& os, const cartographer::transform::Rigid3& rigid) { os << rigid.DebugString(); return os; } using Rigid3d = Rigid3; using Rigid3f = Rigid3; // Converts (roll, pitch, yaw) to a unit length quaternion. Based on the URDF // specification http://wiki.ros.org/urdf/XML/joint. Eigen::Quaterniond RollPitchYaw(double roll, double pitch, double yaw); // Returns an transform::Rigid3d given a 'dictionary' containing 'translation' // (x, y, z) and 'rotation' which can either we an array of (roll, pitch, yaw) // or a dictionary with (w, x, y, z) values as a quaternion. Rigid3d FromDictionary(common::LuaParameterDictionary* dictionary); } // namespace transform } // namespace cartographer #endif // CARTOGRAPHER_TRANSFORM_RIGID_TRANSFORM_H_