#ifndef GEOMETRY_UTILS_TRANSFORM2_H #define GEOMETRY_UTILS_TRANSFORM2_H #include "Vector2.h" #include "Rotation2.h" namespace geometry_utils { template struct Transform2Base { typedef boost::shared_ptr Ptr; typedef boost::shared_ptr ConstPtr; Vector2Base translation; Rotation2Base rotation; Transform2Base() { translation.Zeros(); rotation.Eye(); } Transform2Base(const Vector2Base& translation_, const Rotation2Base& rotation_) : translation(translation_), rotation(rotation_) {} Transform2Base(const Transform2Base& in) : translation(in.translation), rotation(in.rotation) {} Transform2Base(T x, T y, T th) : translation(x, y), rotation(th) {} Transform2Base& operator=(const Transform2Base& rhs) { if (this == &rhs) return *this; translation = rhs.translation; rotation = rhs.rotation; return *this; } Vector2Base operator*(const Vector2Base& p) const { return rotation * p + translation; } Transform2Base operator+(const Transform2Base& t) const { return Transform2Base(translation + rotation * t.translation, rotation * t.rotation); } bool operator==(const Transform2Base& that) const { return this->Equals(that); } bool operator!=(const Transform2Base& that) const { return !this->Equals(that); } bool Equals(const Transform2Base& that, const T ptol = 1e-5, const T rtol = 1e-5) const { return (translation.Equals(that.translation, ptol) && rotation.Equals(that.rotation, rtol)); } void Print(const std::string& prefix = std::string()) const { if (!prefix.empty()) std::cout << prefix << std::endl; std::cout << (*this) << std::endl; } static Transform2Base Identity() { return Transform2Base(); } }; template std::ostream& operator<<(std::ostream& out, const Transform2Base& m) { out << "Translation:" << std::endl << m.translation << std::endl; out << "Rotation:" << std::endl << m.rotation; return out; } template Transform2Base PoseUpdate(const Transform2Base& t1, const Transform2Base& t2) { return Transform2Base(t1.translation + t1.rotation * t2.translation, t1.rotation * t2.rotation); } template Transform2Base PoseInverse(const Transform2Base& t) { return Transform2Base(-1.0 * t.rotation.Trans() * t.translation, t.rotation.Trans()); } template Transform2Base PoseDelta(const Transform2Base& t1, const Transform2Base& t2) { return Transform2Base( t1.rotation.Trans() * (t2.translation - t1.translation), t1.rotation.Trans() * t2.rotation); } typedef Transform2Base Transform2f; typedef Transform2Base Transform2d; typedef Transform2d Transform2; typedef Transform2 Tr2; } #endif