pose2d.cpp
Go to the documentation of this file.
1 #include "tuw_geometry/pose2d.h"
2 using namespace tuw;
3 
4 Pose2D::Pose2D() : position_(), orientation_ ( 0 ) {};
5 Pose2D::Pose2D ( const Point2D &p, double orientation_ ) : position_ ( p ), orientation_ ( orientation_ ), cossin_uptodate_ ( false ) {};
7 Pose2D::Pose2D ( double x, double y, double orientation_ ) : position_ ( x,y ), orientation_ ( orientation_ ), cossin_uptodate_ ( false ) {};
8 Pose2D::Pose2D ( const cv::Vec<double, 3> &s ) : position_ ( s ( 0 ),s ( 1 ) ), orientation_ ( s ( 2 ) ), cossin_uptodate_ ( false ) {};
9 
16 Pose2D &Pose2D::set ( double x, double y, double phi ) {
17  angle_normalize ( phi, -M_PI, +M_PI );
18  position_ = cv::Vec<double, 3> ( x, y, 1 ), orientation_ = phi;
19  cossin_uptodate_ = false;
20  return *this;
21 }
29  position_.set ( position.x(), position.y() );
30  double dx = point_ahead.x() - position.x(), dy = point_ahead.y() - position.y();
31  orientation_ = atan2 ( dy,dx );
32  cossin_uptodate_ = false;
33  return *this;
34 }
39 Pose2D &Pose2D::set ( const Pose2D &p ) {
41  cossin_uptodate_ = false;
42  return *this;
43 }
47 const Point2D &Pose2D::position () const {
48  return position_;
49 }
54 Point2D Pose2D::point_ahead ( double d ) const {
56  return Point2D ( x() + costheta_ * d, y() + sintheta_ * d );
57 }
61 const double &Pose2D::x () const {
62  return position_[0];
63 }
67 const double &Pose2D::y () const {
68  return position_[1];
69 }
73 const double &Pose2D::theta () const {
74  return orientation_;
75 }
80  return position_;
81 }
85 double &Pose2D::x () {
86  return position_[0];
87 }
91 double &Pose2D::y () {
92  return position_[1];
93 }
97 double &Pose2D::theta () {
98  cossin_uptodate_ = false;
99  return orientation_;
100 }
101 
106 void Pose2D::set_x ( double v ) {
107  this->x() = v;
108 }
113 double Pose2D::get_x () const {
114  return this->x();
115 }
120 void Pose2D::set_y ( double v ) {
121  this->y() = v;
122 }
127 double Pose2D::get_y () const {
128  return this->y();
129 }
134 void Pose2D::set_theta ( double v ) {
135  this->theta () = v;
136 }
141 double Pose2D::get_theta () const {
142  return this->theta ();
143 }
144 
148  angle_normalize ( orientation_, -M_PI, +M_PI );
149 }
153 Tf2D Pose2D::tf () const {
155  return cv::Matx33d ( costheta_, -sintheta_, x(), sintheta_, costheta_, y(), 0, 0, 1. );
156 }
161 cv::Vec<double, 3> Pose2D::state_vector () const {
162  return cv::Vec<double, 3> ( x(), y(), theta() );
163 }
168 Pose2D Pose2D::inv () const {
169  Pose2D p ( -this->x(), -this->y(), -this->theta() );
170  return p;
171 }
179  //update_cached_cos_sin();
180  des.set(src.x() * costheta_ - src.y() * sintheta_ + src.h() * x(),
181  src.x() * sintheta_ + src.y() * costheta_ + src.h() * y(),
182  src.h() );
183  return des;
184 }
185 
191 Pose2D &Pose2D::operator += ( const cv::Vec<double, 3> &s ) {
192  this->x() += s.val[0], this->y() += s.val[1], this->theta() += s.val[2];
193  angle_normalize ( this->theta() );
194  cossin_uptodate_ = false;
195  return *this;
196 }
202 Pose2D &Pose2D::operator -= ( const cv::Vec<double, 3> &s ) {
203  this->x() -= s.val[0], this->y() -= s.val[1], angle_difference ( this->theta(), s.val[2] );
204  cossin_uptodate_ = false;
205  return *this;
206 }
213 Pose2D Pose2D::transform_into( const Pose2D &target ) const{
214  return Pose2D(this->x() - target.x(), this->y() - target.y(), angle_normalize (this->theta() - target.theta()));
215 }
216 
222  costheta_ = cos ( orientation_ );
223  sintheta_ = sin ( orientation_ );
224  cossin_uptodate_=true;
225 }
231  if ( cossin_uptodate_ ) {
232  return;
233  }
235 }
241 double Pose2D::theta_cos() const {
243  return costheta_;
244 }
250 double Pose2D::theta_sin() const {
252  return sintheta_;
253 }
259 std::string Pose2D::str(const char* format) const
260 {
261  char str[0xFF];
262  sprintf(str,format, x(), y(), theta());
263  return std::string(str);
264 }
265 
271 bool Pose2D::equal( const Pose2D& o, double tolerance) const {
272  double d_position = cv::norm(o.position() - this->position());
273  double d_angle = angle_difference(o.theta(),this->theta());
274  return (d_angle < tolerance) && (fabs(d_angle) < tolerance);
275 }
const double & y() const
Definition: point2d.cpp:49
bool cossin_uptodate_
Definition: pose2d.h:23
Pose2D inv() const
Definition: pose2d.cpp:168
double angle_normalize(double angle, double min_angle=-M_PI, double max_angle=+M_PI)
Definition: utils.h:43
double sintheta_
Definition: pose2d.h:22
Pose2D transform_into(const Pose2D &target) const
Definition: pose2d.cpp:213
double costheta_
rotation in rad
Definition: pose2d.h:22
double get_y() const
Definition: pose2d.cpp:127
std::string str(const char *format="[%6.4lf, %6.4lf, %6.5lf]") const
Definition: pose2d.cpp:259
Point2D point_ahead(double d=1.) const
Definition: pose2d.cpp:54
Pose2D & operator-=(const cv::Vec< double, 3 > &s)
Definition: pose2d.cpp:202
Pose2D & set(double x, const double y, double phi)
Definition: pose2d.cpp:16
const double & theta() const
Definition: pose2d.cpp:73
void set_y(double v)
Definition: pose2d.cpp:120
const double & h() const
Definition: point2d.cpp:63
void set_x(double v)
Definition: pose2d.cpp:106
const Point2D & position() const
Definition: pose2d.cpp:47
void recompute_cached_cos_sin() const
Definition: pose2d.cpp:221
Definition: command.h:8
void normalizeOrientation()
Definition: pose2d.cpp:147
const double & x() const
Definition: pose2d.cpp:61
const double & y() const
Definition: pose2d.cpp:67
double get_x() const
Definition: pose2d.cpp:113
double theta_sin() const
Definition: pose2d.cpp:250
void set_theta(double v)
Definition: pose2d.cpp:134
double angle_difference(double alpha0, double angle1)
Definition: utils.h:59
Point2D & transform_into_base(const Point2D &src, Point2D &des) const
Definition: pose2d.cpp:178
cv::Matx< double, 3, 3 > Tf2D
Definition: utils.h:10
Point2D position_
Definition: pose2d.h:20
double theta_cos() const
Definition: pose2d.cpp:241
bool equal(const Pose2D &o, double tolerance) const
Definition: pose2d.cpp:271
double orientation_
position
Definition: pose2d.h:21
const double & x() const
Definition: point2d.cpp:35
std::string format(const cv::Mat_< int8_t > &m)
Definition: utils.cpp:11
Point2D & set(double x, double y)
Definition: point2d.cpp:17
double get_theta() const
Definition: pose2d.cpp:141
cv::Vec< double, 3 > state_vector() const
Definition: pose2d.cpp:161
Tf2D tf() const
Definition: pose2d.cpp:153
void update_cached_cos_sin() const
Definition: pose2d.cpp:230
Pose2D & operator+=(const cv::Vec< double, 3 > &s)
Definition: pose2d.cpp:191


tuw_geometry
Author(s): Markus Bader
autogenerated on Mon Jun 10 2019 15:33:09