All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2014 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 /* Desc: The world; all models are collected here
18  * Author: Nate Koenig
19  * Date: 3 Apr 2007
20  */
21 
22 #ifndef _VECTOR3_HH_
23 #define _VECTOR3_HH_
24 
25 #include <math.h>
26 #include <iostream>
27 #include <fstream>
28 
29 #include "gazebo/math/Helpers.hh"
30 
31 namespace gazebo
32 {
33  namespace math
34  {
37 
42  class Vector3
43  {
45  public: static const Vector3 Zero;
46 
48  public: static const Vector3 One;
49 
51  public: static const Vector3 UnitX;
52 
54  public: static const Vector3 UnitY;
55 
57  public: static const Vector3 UnitZ;
58 
60  public: Vector3();
61 
66  public: Vector3(const double &_x, const double &_y, const double &_z);
67 
70  public: Vector3(const Vector3 &_v);
71 
73  public: virtual ~Vector3();
74 
77  public: double GetSum() const;
78 
82  public: double Distance(const Vector3 &_pt) const;
83 
89  public: double Distance(double _x, double _y, double _z) const;
90 
93  public: double GetLength() const;
94 
97  public: double GetSquaredLength() const;
98 
101  public: Vector3 Normalize();
102 
105  public: Vector3 Round();
106 
109  public: Vector3 GetRounded() const;
110 
115  public: inline void Set(double _x = 0, double _y = 0 , double _z = 0)
116  {
117  this->x = _x;
118  this->y = _y;
119  this->z = _z;
120  }
121 
124  public: Vector3 Cross(const Vector3 &_pt) const;
125 
128  public: double Dot(const Vector3 &_pt) const;
129 
132  public: Vector3 GetAbs() const;
133 
136  public: Vector3 GetPerpendicular() const;
137 
143  public: static Vector3 GetNormal(const Vector3 &_v1, const Vector3 &_v2,
144  const Vector3 &_v3);
145 
150  public: double GetDistToLine(const Vector3 &_pt1, const Vector3 &_pt2);
151 
155  public: void SetToMax(const Vector3 &_v);
156 
160  public: void SetToMin(const Vector3 &_v);
161 
164  public: double GetMax() const;
165 
168  public: double GetMin() const;
169 
173  public: Vector3 &operator =(const Vector3 &_v);
174 
178  public: Vector3 &operator =(double _value);
179 
183  public: Vector3 operator+(const Vector3 &_v) const;
184 
187  public: const Vector3 &operator+=(const Vector3 &_v);
188 
191  public: inline Vector3 operator-() const
192  {
193  return Vector3(-this->x, -this->y, -this->z);
194  }
195 
199  public: inline Vector3 operator-(const Vector3 &_pt) const
200  {
201  return Vector3(this->x - _pt.x,
202  this->y - _pt.y,
203  this->z - _pt.z);
204  }
205 
208  public: const Vector3 &operator-=(const Vector3 &_pt);
209 
214  public: const Vector3 operator/(const Vector3 &_pt) const;
215 
220  public: const Vector3 &operator/=(const Vector3 &_pt);
221 
225  public: const Vector3 operator/(double _v) const;
226 
230  public: const Vector3 &operator/=(double _v);
231 
235  public: Vector3 operator*(const Vector3 &_p) const;
236 
241  public: const Vector3 &operator*=(const Vector3 &_v);
242 
247  public: friend inline Vector3 operator*(double _s,
248  const Vector3 &_v)
249  { return Vector3(_v.x * _s, _v.y * _s, _v.z * _s); }
250 
254  public: Vector3 operator*(double _v) const;
255 
259  public: const Vector3 &operator*=(double _v);
260 
265  public: bool operator ==(const Vector3 &_pt) const;
266 
271  public: bool operator!=(const Vector3 &_v) const;
272 
274  public: bool IsFinite() const;
275 
277  public: inline void Correct()
278  {
279  if (!finite(this->x))
280  this->x = 0;
281  if (!finite(this->y))
282  this->y = 0;
283  if (!finite(this->z))
284  this->z = 0;
285  }
286 
288  public: double operator[](unsigned int index) const;
289 
292  public: void Round(int _precision);
293 
298  public: bool Equal(const Vector3 &_v) const;
299 
301  public: double x;
302 
304  public: double y;
305 
307  public: double z;
308 
313  public: friend std::ostream &operator<<(std::ostream &_out,
314  const gazebo::math::Vector3 &_pt)
315  {
316  _out << precision(_pt.x, 6) << " " << precision(_pt.y, 6) << " "
317  << precision(_pt.z, 6);
318  return _out;
319  }
320 
325  public: friend std::istream &operator>>(std::istream &_in,
327  {
328  // Skip white spaces
329  _in.setf(std::ios_base::skipws);
330  _in >> _pt.x >> _pt.y >> _pt.z;
331  return _in;
332  }
333  };
335  }
336 }
337 #endif