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: static const Vector3 Zero;
46 
48  public: Vector3();
49 
54  public: Vector3(const double &_x, const double &_y, const double &_z);
55 
58  public: Vector3(const Vector3 &_v);
59 
61  public: virtual ~Vector3();
62 
65  public: double GetSum() const;
66 
70  public: double Distance(const Vector3 &_pt) const;
71 
77  public: double Distance(double _x, double _y, double _z) const;
78 
81  public: double GetLength() const;
82 
85  public: double GetSquaredLength() const;
86 
89  public: Vector3 Normalize();
90 
93  public: Vector3 Round();
94 
97  public: Vector3 GetRounded() const;
98 
103  public: inline void Set(double _x = 0, double _y = 0 , double _z = 0)
104  {
105  this->x = _x;
106  this->y = _y;
107  this->z = _z;
108  }
109 
112  public: Vector3 Cross(const Vector3 &_pt) const;
113 
116  public: double Dot(const Vector3 &_pt) const;
117 
120  public: Vector3 GetAbs() const;
121 
124  public: Vector3 GetPerpendicular() const;
125 
131  public: static Vector3 GetNormal(const Vector3 &_v1, const Vector3 &_v2,
132  const Vector3 &_v3);
133 
138  public: double GetDistToLine(const Vector3 &_pt1, const Vector3 &_pt2);
139 
143  public: void SetToMax(const Vector3 &_v);
144 
148  public: void SetToMin(const Vector3 &_v);
149 
152  public: double GetMax() const;
153 
156  public: double GetMin() const;
157 
161  public: Vector3 &operator =(const Vector3 &_v);
162 
166  public: Vector3 &operator =(double _value);
167 
171  public: Vector3 operator+(const Vector3 &_v) const;
172 
175  public: const Vector3 &operator+=(const Vector3 &_v);
176 
180  public: inline Vector3 operator-(const Vector3 &_pt) const
181  {
182  return Vector3(this->x - _pt.x,
183  this->y - _pt.y,
184  this->z - _pt.z);
185  }
186 
189  public: const Vector3 &operator-=(const Vector3 &_pt);
190 
195  public: const Vector3 operator/(const Vector3 &_pt) const;
196 
201  public: const Vector3 &operator/=(const Vector3 &_pt);
202 
206  public: const Vector3 operator/(double _v) const;
207 
211  public: const Vector3 &operator/=(double _v);
212 
216  public: Vector3 operator*(const Vector3 &_p) const;
217 
222  public: const Vector3 &operator*=(const Vector3 &_v);
223 
227  public: Vector3 operator*(double _v) const;
228 
232  public: const Vector3 &operator*=(double _v);
233 
238  public: bool operator ==(const Vector3 &_pt) const;
239 
244  public: bool operator!=(const Vector3 &_v) const;
245 
247  public: bool IsFinite() const;
248 
250  public: inline void Correct()
251  {
252  if (!finite(this->x))
253  this->x = 0;
254  if (!finite(this->y))
255  this->y = 0;
256  if (!finite(this->z))
257  this->z = 0;
258  }
259 
261  public: double operator[](unsigned int index) const;
262 
265  public: void Round(int _precision);
266 
271  public: bool Equal(const Vector3 &_v) const;
272 
274  public: double x;
275 
277  public: double y;
278 
280  public: double z;
281 
286  public: friend std::ostream &operator<<(std::ostream &_out,
287  const gazebo::math::Vector3 &_pt)
288  {
289  _out << _pt.x << " " << _pt.y << " " << _pt.z;
290  return _out;
291  }
292 
297  public: friend std::istream &operator>>(std::istream &_in,
299  {
300  // Skip white spaces
301  _in.setf(std::ios_base::skipws);
302  _in >> _pt.x >> _pt.y >> _pt.z;
303  return _in;
304  }
305  };
307  }
308 }
309 #endif
310