NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Vec3f.h
1 #ifndef VEC3F_H_
2 #define VEC3F_H_
3 
4 #include <QGLViewer/vec.h>
5 #include <QDebug>
6 #include <math.h>
7 
8 namespace margait_contrib
9 {
10 
11 class Vec3f;
12 
13 extern double operator*(const Vec3f& v, const Vec3f& w);
14 extern Vec3f operator*(const double scalar, const Vec3f& v);
15 extern Vec3f operator*(const Vec3f& v, const double scalar);
16 extern Vec3f operator/(const Vec3f& v, const double scalar);
17 
18 class Vec3f
19 {
20 public:
21 
22  double x, y, z;
23 
24  Vec3f(){ x=y=z=0.f;}
25  Vec3f(const double xx, const double yy, const double zz){x=xx; y=yy; z=zz;}
26  Vec3f(const qglviewer::Vec& other){x=other.x; y=other.y; z=other.z;}
27  Vec3f & operator=(const qglviewer::Vec& other) {x=other.x; y=other.y; z=other.z; return *this;}
28 
29  inline Vec3f operator+(const Vec3f& v) const {return Vec3f(x+v.x, y+v.y, z+v.z);}
30  inline Vec3f operator-() const {return Vec3f(-x, -y, -z);}
31  inline Vec3f operator-(const Vec3f& v) const {return Vec3f(x-v.x, y-v.y, z-v.z);}
32  inline Vec3f operator%(const Vec3f& v) const {return Vec3f(x*v.x, y*v.y, z*v.z);}
33  inline Vec3f operator%=(const Vec3f& v) const {return Vec3f(x*v.x, y*v.y, z*v.z);}
34  inline bool operator==(const Vec3f& v) const {return (x==v.x) && (y==v.y) && (z==v.z);}
35  inline bool operator!=(const Vec3f& v) const {return (x!=v.x) || (y!=v.y) || (z!=v.z);}
36  inline Vec3f operator/(const Vec3f& v) const {return Vec3f(x/v.x, y/v.y, z/v.z);}
37 
38  inline Vec3f& operator=(const Vec3f& v){ x=v.x; y=v.y; z=v.z; return *this;}
39  inline Vec3f& operator*=(const double scalar){ x*=scalar;y*=scalar; z*=scalar; return *this;}
40  inline Vec3f& operator/=(const double scalar){ x/=scalar;y/=scalar; z/=scalar; return *this;}
41  inline Vec3f& operator+=(const Vec3f& v){ x+=v.x; y+=v.y; z+=v.z; return *this;}
42  inline Vec3f& operator-=(const Vec3f& v){ x-=v.x; y-=v.y; z-= v.z; return *this;}
43 
44  operator const double*() const {return &x;} // For OpenGL
45 
46  /* Euklidischer Länge eines Vektors */
47  inline double norm() const { return sqrt(x*x+y*y+z*z);} // Euklidische Länge des Vektors zum Quadrat
48 
49  /* Euklidischer Länge eines Vektors zum Quadrat */
50  inline double norm2() const { return x*x+y*y+z*z;} // Euklidische Länge des Vektors zum Quadrat
51 
52  /* P-Norm of a vector. */
53  inline double normp(double p) const { return pow( pow(fabs(x), p) + pow(fabs(y), p) + pow(fabs(z), p), 1/p); }
54 
55  inline Vec3f& normalize()
56  {
57  //double n = norm(); x /= n; y /= n; return *this;
58  double n = norm();
59  if( n != 0.f)
60  {
61  x /= n; y /= n; z/= n;
62  }
63  else
64  {
65  //Vec2f returns (0,1) but we uses now (0,0,0)
66  x = 0.f; y = 0.f; z= 0.f;
67  }
68  return *this;
69  }
70 
71  inline Vec3f getNormalized() const
72  {
73  double n = norm();
74  if( n != 0.f)
75  return Vec3f(x / n, y / n, z/n);
76  else
77  return *this;
78  }
79 
80  inline void normiereAuf( const double length)
81  {
82  normalize();
83  *this *= length;
84  }
85  inline Vec3f normiertAuf( const double length)
86  {
87  Vec3f res = *this;
88  res.normiereAuf(length);
89  return res;
90  }
91  /* Euklidischer Abstand zu einem Vektors */
92  inline double dist(const Vec3f& v) const { return (*this-v).norm();}
93  /* Euklidischer Abstand zu einem Vektors zum Quadrat */
94  inline double dist2(const Vec3f& v) const { return (*this-v).norm2();}
95 
96 
97  inline void rotate(double angle)
98  {
99  double x_ = x;
100  double y_ = y;
101  x = x_ * cos(angle) + y_ * -sin(angle);
102  y = x_ * sin(angle) + y_ * cos(angle);
103  }
104 
105  inline bool operator<(const Vec3f& v ) const
106  {
107  return norm2() < v.norm2();
108  }
109 
110  inline Vec3f absComponents() const
111  {
112  return Vec3f(fabsf(x), fabsf(y), fabsf(z));
113  }
114 
115  double maxComponent()
116  {
117  if (fabsf(x)>fabsf(y))
118  if(fabsf(x)>fabsf(z))
119  return fabsf(x);
120  else
121  return fabsf(z);
122  else
123  if(fabsf(y)>fabsf(z))
124  return fabsf(y);
125  else
126  return fabsf(z);
127  }
128 
129  double minComponent()
130  {
131  if (fabsf(x)>fabsf(y))
132  if(fabsf(y)>fabsf(z))
133  return fabsf(z);
134  else
135  return fabsf(y);
136  else
137  if(fabsf(x)>fabsf(z))
138  return fabsf(z);
139  else
140  return fabsf(x);
141  }
142 };
143 
144 QDebug operator<<(QDebug dbg, const Vec3f &v);
145 
146 }
147 
148 #endif /* ACTION_H_ */