NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Vec2f.h
1 #ifndef VEC2F
2 #define VEC2F
3 
4 #include <QtGlobal>
5 #include <math.h>
6 #include <QDebug>
7 
8 class Vec2f;
9 
10 extern double operator*(const Vec2f& v, const Vec2f& w);
11 extern Vec2f operator*(const double scalar, const Vec2f& v);
12 extern Vec2f operator*(const Vec2f& v, const double scalar);
13 extern Vec2f operator/(const Vec2f& v, const double scalar);
14 extern Vec2f operator/(const double scalar, const Vec2f& v);
15 extern Vec2f operator-(const double scalar, const Vec2f& v);
16 
17 class Vec2f
18 {
19  public:
20  double x,y;
21  // Constructors
22  Vec2f(const Vec2f& v);
23  Vec2f(){ x=y=0.f;}
24  Vec2f(double xx, double yy){ x=xx; y=yy;}
25  Vec2f(double xx[2]) { x = xx[0]; y = xx[1]; }
26  Vec2f(const double xx[2]) { x = xx[0]; y = xx[1]; }
27 
28  //operators
29  inline void setZero() { x=y=0.f; }
30  inline bool isZero() const { return x==0.f && y==0.f; }
31  Vec2f rangeCut(const double &border) const;
32  Vec2f rangeCut(const double &lower_border, const double &upper_border) const;
33  double getmax() const { return x>y ? x : y; }
34  double getmin() const { return x<y ? x : y; }
35 
36  Vec2f mapped(double (*func)(double)) { return Vec2f(func(x), func(y)); }
37  void map(double (*func)(double)) { x = func(x); y = func(y); }
38  inline Vec2f operator+(const double& t) const {return Vec2f(x+t, y+t);}
39  inline Vec2f operator-(const double& t) const {return Vec2f(x-t, y-t);}
40  inline Vec2f operator+(const Vec2f& v) const {return Vec2f(x+v.x, y+v.y);}
41  inline Vec2f operator-() const {return Vec2f(-x, -y);}
42  inline Vec2f operator-(const Vec2f& v) const {return Vec2f(x-v.x, y-v.y);}
43  inline Vec2f& operator/=(const Vec2f& v) { x/=v.x; y/=v.y; return *this;}
44  inline Vec2f operator/(const Vec2f& v) const {return Vec2f(x/v.x, y/v.y);}
45  inline Vec2f operator%(const Vec2f& v) const {return Vec2f(x*v.x, y*v.y);}
46  inline Vec2f operator%=(const Vec2f& v) const {return Vec2f(x*v.x, y*v.y);} //auch von Felix
47  inline bool operator==(const Vec2f& v) const {return (x==v.x) && (y==v.y);}
48  inline bool operator!=(const Vec2f& v) const {return (x!=v.x) || (y!=v.y);}
49 
50  inline Vec2f& operator=(const Vec2f& v){ x=v.x; y=v.y; return *this;}
51  inline Vec2f& operator*=(const double scalar){ x*=scalar;y*=scalar; return *this;}
52  inline Vec2f& operator/=(const double scalar){ x/=scalar;y/=scalar; return *this;}
53  inline Vec2f& operator+=(const Vec2f& v){ x+=v.x; y+=v.y; return *this;}
54  inline Vec2f& operator-=(const Vec2f& v){ x-=v.x; y-=v.y; return *this;}
55 
56  // member functions
57  inline Vec2f& projectTo(const Vec2f& to)
58  {
59  *this = to * ( ((*this) * to) / to.norm2());
60  return *this;
61  }
62 
63  inline Vec2f projectionTo(const Vec2f& to) const
64  {
65  Vec2f v(*this);
66  v.projectTo(to);
67  return v;
68  }
69 
70  double ellipseDist(const Vec2f &p) const
71  {
72  double dx = p.x/x;
73  double dy = p.y/y;
74  return dx*dx + dy*dy;
75  }
76 
77  /* Kreuzprodukt zweier Vektoren */
78  inline double cross(const Vec2f& v2) const { return x*v2.y-y*v2.x; };
79  /* Manhatten Norm eines Vektors */
80  inline double norm1() const { return qAbs(x) + qAbs(y);}
81  /* Euklidischer Länge eines Vektors */
82  inline double norm() const { return sqrt(x*x+y*y);} // Euklidische Länge des Vektors zum Quadrat
83  /* Euklidischer Länge eines Vektors zum Quadrat */
84  inline double norm2() const { return x*x+y*y;} // Euklidische Länge des Vektors zum Quadrat
85  /* Winkel des Vectors in rad -pi..pi */
86  inline double angle() const { return atan2(y,x); }
87  inline void normalize()
88  {
89  //double n = norm(); x /= n; y /= n; return *this;
90  double n = norm();
91  if( n != 0.f)
92  {
93  x /= n; y /= n;
94  }
95  else
96  {
97  x = 0.f; y = 1.f;
98  }
99  }
100 
101  inline Vec2f normalize() const
102  {
103  double n = norm();
104  if( n != 0.f)
105  return Vec2f(x / n, y / n);
106  else
107  return *this;
108  }
109 
110  /* Euklidischer Abstand zu einem Vektors */
111  inline double dist(const Vec2f& v) const { return (*this-v).norm();}
112  /* Euklidischer Abstand zu einem Vektors zum Quadrat */
113  inline double dist2(const Vec2f& v) const { return (*this-v).norm2();}
114 
115  bool isLeftOf(const Vec2f& v) const
116  {
117  return (v.x * y - x * v.y) < 0;
118  }
119 
120  inline void rotate(double angle)
121  {
122  double x_ = x;
123  double y_ = y;
124  x = x_ * cos(angle) + y_ * -sin(angle);
125  y = x_ * sin(angle) + y_ * cos(angle);
126  }
127 
128  inline Vec2f rotated(double angle) const
129  {
130  Vec2f v(*this);
131  v.rotate(angle);
132  return v;
133  }
134 
135  inline bool operator<(const Vec2f& v ) const
136  {
137  return norm2() < v.norm2();
138  }
139 };
140 
141 QDebug operator<<(QDebug dbg, const Vec2f &v);
142 
143 #endif //VEC2F