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 2012 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"
31 
32 namespace gazebo
33 {
34  namespace math
35  {
38 
43  class Vector3
44  {
46  public: static const Vector3 Zero;
47 
49  public: Vector3();
50 
55  public: Vector3(const double &_x, const double &_y, const double &_z);
56 
59  public: Vector3(const Vector3 &_v);
60 
62  public: virtual ~Vector3();
63 
66  public: double GetSum() const;
67 
71  public: double Distance(const Vector3 &_pt) const;
72 
78  public: double Distance(double _x, double _y, double _z) const;
79 
82  public: double GetLength() const;
83 
86  public: double GetSquaredLength() const;
87 
90  public: Vector3 Normalize();
91 
94  public: Vector3 Round();
95 
98  public: Vector3 GetRounded() const;
99 
104  public: inline void Set(double _x = 0, double _y = 0 , double _z = 0)
105  {
106  this->x = _x;
107  this->y = _y;
108  this->z = _z;
109  }
110 
113  public: Vector3 Cross(const Vector3 &_pt) const;
114 
117  public: double Dot(const Vector3 &_pt) const;
118 
121  public: Vector3 GetAbs() const;
122 
125  public: Vector3 GetPerpendicular() const;
126 
132  public: static Vector3 GetNormal(const Vector3 &_v1, const Vector3 &_v2,
133  const Vector3 &_v3);
134 
139  public: double GetDistToLine(const Vector3 &_pt1, const Vector3 &_pt2);
140 
144  public: void SetToMax(const Vector3 &_v);
145 
149  public: void SetToMin(const Vector3 &_v);
150 
153  public: double GetMax() const;
154 
157  public: double GetMin() const;
158 
162  public: Vector3 &operator =(const Vector3 &_v);
163 
167  public: Vector3 &operator =(double _value);
168 
172  public: Vector3 operator+(const Vector3 &_v) const;
173 
176  public: const Vector3 &operator+=(const Vector3 &_v);
177 
180  public: inline Vector3 operator-() const
181  {
182  return Vector3(-this->x, -this->y, -this->z);
183  }
184 
188  public: inline Vector3 operator-(const Vector3 &_pt) const
189  {
190  return Vector3(this->x - _pt.x,
191  this->y - _pt.y,
192  this->z - _pt.z);
193  }
194 
197  public: const Vector3 &operator-=(const Vector3 &_pt);
198 
203  public: const Vector3 operator/(const Vector3 &_pt) const;
204 
209  public: const Vector3 &operator/=(const Vector3 &_pt);
210 
214  public: const Vector3 operator/(double _v) const;
215 
219  public: const Vector3 &operator/=(double _v);
220 
224  public: Vector3 operator*(const Vector3 &_p) const;
225 
230  public: const Vector3 &operator*=(const Vector3 &_v);
231 
235  public: friend inline Vector3 operator*(double _s,
236  const Vector3 &_v)
237  { return Vector3(_v.x * _s, _v.y * _s, _v.z * _s); }
238 
242  public: Vector3 operator*(double _v) const;
243 
247  public: const Vector3 &operator*=(double _v);
248 
253  public: bool operator ==(const Vector3 &_pt) const;
254 
259  public: bool operator!=(const Vector3 &_v) const;
260 
262  public: bool IsFinite() const;
263 
265  public: inline void Correct()
266  {
267  if (!finite(this->x))
268  this->x = 0;
269  if (!finite(this->y))
270  this->y = 0;
271  if (!finite(this->z))
272  this->z = 0;
273  }
274 
276  public: double operator[](unsigned int index) const;
277 
280  public: void Round(int _precision);
281 
286  public: bool Equal(const Vector3 &_v) const;
287 
289  public: double x;
290 
292  public: double y;
293 
295  public: double z;
296 
301  public: friend std::ostream &operator<<(std::ostream &_out,
302  const gazebo::math::Vector3 &_pt)
303  {
304  _out << precision(_pt.x, 6) << " " << precision(_pt.y, 6) << " "
305  << precision(_pt.z, 6);
306  return _out;
307  }
308 
313  public: friend std::istream &operator>>(std::istream &_in,
315  {
316  // Skip white spaces
317  _in.setf(std::ios_base::skipws);
318  _in >> _pt.x >> _pt.y >> _pt.z;
319  return _in;
320  }
321  };
323  }
324 }
325 #endif
326