basic.h
Go to the documentation of this file.
1 #ifndef _BASIC_H_
2 #define _BASIC_H_
3 #ifndef _USE_MATH_DEFINES
4 #define _USE_MATH_DEFINES
5 #endif
6 #include <cmath>
7 #include <cassert>
8 #include <GfxTL/VectorXD.h>
9 
10 #ifndef DLL_LINKAGE
11 #define DLL_LINKAGE
12 #endif
13 
14 typedef float Scalar;
15 
17 {
18 
19 public:
20  typedef float ScalarType;
21  enum { Dim = 3 };
22 
23  Vec3f() {}
24  Vec3f(float x, float y, float z)
25  {
26  setValue(x, y, z);
27  }
28 
30  {
31  for (unsigned int i = 0; i < 3; ++i)
32  {
33  vec[i] = v[i];
34  }
35  }
36 
37  explicit Vec3f(const float v[3])
38  {
39  setValue(v);
40  }
41 
42  Vec3f& setValue(const float v[3])
43  {
44  for (int i = 0; i < 3; i++)
45  {
46  vec[i] = v[i];
47  }
48  return *this;
49  }
50 
51  const float* getValue() const
52  {
53  return vec;
54  }
55 
56  Vec3f& setValue(float x, float y, float z)
57  {
58  vec[0] = x;
59  vec[1] = y;
60  vec[2] = z;
61  return *this;
62  }
63 
64  void getValue(float& x, float& y, float& z) const
65  {
66  x = vec[0];
67  y = vec[1];
68  z = vec[2];
69  }
70 
71 
72  operator const float* () const
73  {
74  return vec;
75  }
76 
77  operator float* ()
78  {
79  return vec;
80  }
81 
82  /*float& operator [] (int i)
83  {
84  return vec[i];
85  }
86 
87  const float& operator [] (int i) const
88  {
89  return vec[i];
90  }*/
91 
92  float dot(const Vec3f& v) const
93  {
94  float s = vec[0] * v.vec[0];
95  for (int i = 1; i < 3; i++)
96  {
97  s += vec[i] * v.vec[i];
98  }
99  return s;
100  }
101 
102  float dot(const float* v) const
103  {
104  return vec[0] * v[0] + vec[1] * v[1] + vec[2] * v[2];
105  }
106 
107  Vec3f cross(const Vec3f& v) const
108  {
109  return Vec3f(vec[1] * v.vec[2] - vec[2] * v.vec[1],
110  vec[2] * v.vec[0] - vec[0] * v.vec[2],
111  vec[0] * v.vec[1] - vec[1] * v.vec[0]);
112  }
113 
114 
115  float sqrLength() const
116  {
117  return dot(*this);
118  }
119 
120  float length() const
121  {
122  return (float) std::sqrt(dot(*this));
123  }
124 
125  float normalize()
126  {
127  float len = length();
128  if (len > 0)
129  {
130  *this /= len;
131  }
132  return len;
133  }
134 
135  bool equals(const Vec3f& v, float tolerance) const
136  {
137  return ((*this - v).sqrLength() <= tolerance);
138  }
139 
140  friend bool operator == (const Vec3f& v1, const Vec3f& v2)
141  {
142  for (int i = 0; i < 3; i++)
143  if (v1.vec[i] != v2.vec[i])
144  {
145  return false;
146  }
147  return true;
148  }
149 
150  friend bool operator < (const Vec3f& v1, const Vec3f& v2)
151  {
152  for (int i = 0; i < 3; i++)
153  if (v1.vec[i] >= v2.vec[i])
154  {
155  return false;
156  }
157  return true;
158  }
159  friend bool operator <= (const Vec3f& v1, const Vec3f& v2)
160  {
161  for (int i = 0; i < 3; i++)
162  if (v1.vec[i] > v2.vec[i])
163  {
164  return false;
165  }
166  return true;
167  }
168  friend bool operator > (const Vec3f& v1, const Vec3f& v2)
169  {
170  for (int i = 0; i < 3; i++)
171  if (v1.vec[i] <= v2.vec[i])
172  {
173  return false;
174  }
175  return true;
176  }
177  friend bool operator >= (const Vec3f& v1, const Vec3f& v2)
178  {
179  for (int i = 0; i < 3; i++)
180  if (v1.vec[i] < v2.vec[i])
181  {
182  return false;
183  }
184  return true;
185  }
186 
187  friend bool operator != (const Vec3f& v1, const Vec3f& v2)
188  {
189  return !(v1 == v2);
190  }
191 
193  {
194  for (int i = 0; i < 3; i++)
195  {
196  vec[i] *= s;
197  }
198  return *this;
199  }
200 
202  {
203  for (int i = 0; i < 3; i++)
204  {
205  vec[i] /= s;
206  }
207  return *this;
208  }
209 
211  {
212  for (int i = 0; i < 3; i++)
213  {
214  vec[i] += v.vec[i];
215  }
216  return *this;
217  }
218 
220  {
221  for (int i = 0; i < 3; i++)
222  {
223  vec[i] -= v.vec[i];
224  }
225  return *this;
226  }
227 
228  void negate()
229  {
230  for (int i = 0; i < 3; i++)
231  {
232  vec[i] = -vec[i];
233  }
234  }
235 
236 
237  friend Vec3f operator * (float s, const Vec3f& v)
238  {
239  return Vec3f(v) *= s;
240  }
241  friend Vec3f operator * (const Vec3f& v, float s)
242  {
243  return Vec3f(v) *= s;
244  }
245  friend Vec3f operator / (const Vec3f& v, float s)
246  {
247  return Vec3f(v) /= s;
248  }
249  friend Vec3f operator + (const Vec3f& v1, const Vec3f& v2)
250  {
251  return Vec3f(v1) += v2;
252  }
253  friend Vec3f operator - (const Vec3f& v1, const Vec3f& v2)
254  {
255  return Vec3f(v1) -= v2;
256  }
257  friend Vec3f operator - (const Vec3f& v1)
258  {
259  Vec3f v2 = v1;
260  v2.negate();
261  return v2;
262  }
263  Vec3f operator*(const Vec3f& v) const
264  {
265  return Vec3f(vec[0] * v.vec[0], vec[1] * v.vec[1], vec[2] * v.vec[2]);
266  }
267 
268  // Konvertierung:
269 
271  {
272  for (int i = 0; i < 3; i++)
273  {
274  vec[i] = (float) v[i];
275  }
276  return *this;
277  }
278 
279  void getValue(Vec3f& v) const
280  {
281  for (int i = 0; i < 3; i++)
282  {
283  v[i] = (Scalar) vec[i];
284  }
285  }
286 
287 private:
288  float vec[3];
289 };
290 
291 #endif //_BASIC_H_
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
GfxTL::VectorXD
Definition: MatrixXX.h:21
Vec3f::getValue
void getValue(float &x, float &y, float &z) const
Definition: basic.h:64
armarx::operator-=
std::vector< T > & operator-=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:103
Vec3f::normalize
float normalize()
Definition: basic.h:125
Vec3f
Definition: basic.h:16
Vec3f::cross
Vec3f cross(const Vec3f &v) const
Definition: basic.h:107
armarx::operator/=
std::vector< T > & operator/=(std::vector< T > &v1, double c)
Definition: VectorHelpers.h:131
IceStorm::operator!=
bool operator!=(const IceStorm::Subscriber &, const IceStorm::Subscriber &)
Definition: Subscriber.cpp:1064
armarx::operator*=
std::vector< T > & operator*=(std::vector< T > &v1, double c)
Definition: VectorHelpers.h:116
armarx::operator-
std::vector< T > operator-(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:61
Vec3f::length
float length() const
Definition: basic.h:120
VectorXD.h
Vec3f::equals
bool equals(const Vec3f &v, float tolerance) const
Definition: basic.h:135
Vec3f::dot
float dot(const float *v) const
Definition: basic.h:102
Vec3f::getValue
const float * getValue() const
Definition: basic.h:51
IceStormElection::operator==
bool operator==(const ReplicaObserver &lhs, const ReplicaObserver &rhs)
Definition: Election.h:1599
armarx::operator+=
std::vector< T > & operator+=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:89
armarx::detail::operator*
StreamPrinter< Fnc > operator*(StreamPrinterTag, Fnc &&f)
Definition: LoggingUtil.h:44
Vec3f::operator*
Vec3f operator*(const Vec3f &v) const
Definition: basic.h:263
armarx::operator>=
bool operator>=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:236
operator+
armarx::Vector3IBase operator+(const armarx::Vector3IBase &lhs, const armarx::Vector3IBase &rhs)
Definition: AStarPathPlanner.cpp:28
Vec3f::ScalarType
float ScalarType
Definition: basic.h:20
IceStormElection::operator<
bool operator<(const ReplicaObserver &lhs, const ReplicaObserver &rhs)
Definition: Election.h:1604
Vec3f::Vec3f
Vec3f()
Definition: basic.h:23
Vec3f::Vec3f
Vec3f(const float v[3])
Definition: basic.h:37
Vec3f::sqrLength
float sqrLength() const
Definition: basic.h:115
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
Vec3f::dot
float dot(const Vec3f &v) const
Definition: basic.h:92
Vec3f::negate
void negate()
Definition: basic.h:228
float
#define float
Definition: 16_Level.h:22
Vec3f::Vec3f
Vec3f(float x, float y, float z)
Definition: basic.h:24
Scalar
float Scalar
Definition: basic.h:14
Vec3f::Vec3f
Vec3f(const GfxTL::Vector3Df &v)
Definition: basic.h:29
Vec3f::getValue
void getValue(Vec3f &v) const
Definition: basic.h:279
dot
double dot(const Point &x, const Point &y)
Definition: point.hpp:53
Vec3f::setValue
Vec3f & setValue(const float v[3])
Definition: basic.h:42
Vec3f::setValue
Vec3f & setValue(const Vec3f &v)
Definition: basic.h:270
armarx::operator<=
bool operator<=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:228
DLL_LINKAGE
#define DLL_LINKAGE
Definition: basic.h:11
GfxTL::Vec3f
VectorXD< 3, float > Vec3f
Definition: VectorXD.h:691
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::core::time::operator/
Frequency operator/(const double cyclesPerDuration, const Duration &duration)
Definition: Frequency.cpp:255
Vec3f::setValue
Vec3f & setValue(float x, float y, float z)
Definition: basic.h:56
armarx::operator>
bool operator>(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:232