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 Nate Koenig
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 
30 
31 namespace gazebo
32 {
33  namespace math
34  {
37 
42  class Vector3
43  {
45  public: Vector3();
46 
51  public: Vector3(const double &_x, const double &_y, const double &_z);
52 
55  public: Vector3(const Vector3 &_v);
56 
58  public: virtual ~Vector3();
59 
62  public: double GetSum() const;
63 
67  public: double Distance(const Vector3 &_pt) const;
68 
74  public: double Distance(double _x, double _y, double _z) const;
75 
78  public: double GetLength() const;
79 
82  public: double GetSquaredLength() const;
83 
86  public: Vector3 Normalize();
87 
90  public: Vector3 Round();
91 
94  public: Vector3 GetRounded() const;
95 
100  public: inline void Set(double _x = 0, double _y = 0 , double _z = 0)
101  {
102  this->x = _x;
103  this->y = _y;
104  this->z = _z;
105  }
106 
109  public: Vector3 Cross(const Vector3 &_pt) const;
110 
113  public: double Dot(const Vector3 &_pt) const;
114 
117  public: Vector3 GetAbs() const;
118 
121  public: Vector3 GetPerpendicular() const;
122 
128  public: static Vector3 GetNormal(const Vector3 &_v1, const Vector3 &_v2,
129  const Vector3 &_v3);
130 
135  public: double GetDistToLine(const Vector3 &_pt1, const Vector3 &_pt2);
136 
140  public: void SetToMax(const Vector3 &_v);
141 
145  public: void SetToMin(const Vector3 &_v);
146 
149  public: double GetMax() const;
150 
153  public: double GetMin() const;
154 
158  public: Vector3 &operator =(const Vector3 &_v);
159 
163  public: Vector3 &operator =(double _value);
164 
168  public: Vector3 operator+(const Vector3 &_v) const;
169 
172  public: const Vector3 &operator+=(const Vector3 &_v);
173 
177  public: inline Vector3 operator-(const Vector3 &_pt) const
178  {
179  return Vector3(this->x - _pt.x,
180  this->y - _pt.y,
181  this->z - _pt.z);
182  }
183 
186  public: const Vector3 &operator-=(const Vector3 &_pt);
187 
192  public: const Vector3 operator/(const Vector3 &_pt) const;
193 
198  public: const Vector3 &operator/=(const Vector3 &_pt);
199 
203  public: const Vector3 operator/(double _v) const;
204 
208  public: const Vector3 &operator/=(double _v);
209 
213  public: Vector3 operator*(const Vector3 &_p) const;
214 
219  public: const Vector3 &operator*=(const Vector3 &_v);
220 
224  public: Vector3 operator*(double _v) const;
225 
229  public: const Vector3 &operator*=(double _v);
230 
235  public: bool operator ==(const Vector3 &_pt) const;
236 
241  public: bool operator!=(const Vector3 &_v) const;
242 
244  public: bool IsFinite() const;
245 
247  public: inline void Correct()
248  {
249  if (!finite(this->x))
250  this->x = 0;
251  if (!finite(this->y))
252  this->y = 0;
253  if (!finite(this->z))
254  this->z = 0;
255  }
256 
258  public: double operator[](unsigned int index) const;
259 
262  public: void Round(int _precision);
263 
268  public: bool Equal(const Vector3 &_v) const;
269 
271  public: double x;
272 
274  public: double y;
275 
277  public: double z;
278 
283  public: friend std::ostream &operator<<(std::ostream &_out,
284  const gazebo::math::Vector3 &_pt)
285  {
286  _out << _pt.x << " " << _pt.y << " " << _pt.z;
287  return _out;
288  }
289 
294  public: friend std::istream &operator>>(std::istream &_in,
296  {
297  // Skip white spaces
298  _in.setf(std::ios_base::skipws);
299  _in >> _pt.x >> _pt.y >> _pt.z;
300  return _in;
301  }
302  };
304  }
305 }
306 #endif
307