Vector3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012-2015 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 #ifndef _GAZEBO_VECTOR3_HH_
18 #define _GAZEBO_VECTOR3_HH_
19 
20 #include <math.h>
21 #include <iostream>
22 #include <fstream>
23 #include <ignition/math/Vector3.hh>
24 
25 #include "gazebo/math/Helpers.hh"
26 #include "gazebo/util/system.hh"
27 
28 namespace gazebo
29 {
30  namespace math
31  {
34 
40  {
42  public: static const Vector3 Zero;
43 
45  public: static const Vector3 One;
46 
48  public: static const Vector3 UnitX;
49 
51  public: static const Vector3 UnitY;
52 
54  public: static const Vector3 UnitZ;
55 
57  public: Vector3();
58 
63  public: Vector3(const double &_x, const double &_y, const double &_z);
64 
67  public: Vector3(const ignition::math::Vector3d &_v);
68 
71  public: Vector3(const Vector3 &_v);
72 
74  public: virtual ~Vector3();
75 
78  public: double GetSum() const;
79 
83  public: double Distance(const Vector3 &_pt) const;
84 
90  public: double Distance(double _x, double _y, double _z) const;
91 
94  public: double GetLength() const;
95 
98  public: double GetSquaredLength() const;
99 
102  public: Vector3 Normalize();
103 
106  public: Vector3 Round();
107 
110  public: Vector3 GetRounded() const;
111 
116  public: inline void Set(double _x = 0, double _y = 0 , double _z = 0)
117  {
118  this->x = _x;
119  this->y = _y;
120  this->z = _z;
121  }
122 
125  public: Vector3 Cross(const Vector3 &_pt) const;
126 
129  public: double Dot(const Vector3 &_pt) const;
130 
133  public: Vector3 GetAbs() const;
134 
137  public: Vector3 GetPerpendicular() const;
138 
144  public: static Vector3 GetNormal(const Vector3 &_v1, const Vector3 &_v2,
145  const Vector3 &_v3);
146 
151  public: double GetDistToLine(const Vector3 &_pt1, const Vector3 &_pt2);
152 
156  public: void SetToMax(const Vector3 &_v);
157 
161  public: void SetToMin(const Vector3 &_v);
162 
165  public: double GetMax() const;
166 
169  public: double GetMin() const;
170 
173  public: ignition::math::Vector3d Ign() const;
174 
178  public: Vector3 &operator=(const ignition::math::Vector3d &_v);
179 
183  public: Vector3 &operator =(const Vector3 &_v);
184 
188  public: Vector3 &operator =(double _value);
189 
193  public: Vector3 operator+(const Vector3 &_v) const;
194 
197  public: const Vector3 &operator+=(const Vector3 &_v);
198 
201  public: inline Vector3 operator-() const
202  {
203  return Vector3(-this->x, -this->y, -this->z);
204  }
205 
209  public: inline Vector3 operator-(const Vector3 &_pt) const
210  {
211  return Vector3(this->x - _pt.x,
212  this->y - _pt.y,
213  this->z - _pt.z);
214  }
215 
218  public: const Vector3 &operator-=(const Vector3 &_pt);
219 
224  public: const Vector3 operator/(const Vector3 &_pt) const;
225 
230  public: const Vector3 &operator/=(const Vector3 &_pt);
231 
235  public: const Vector3 operator/(double _v) const;
236 
240  public: const Vector3 &operator/=(double _v);
241 
245  public: Vector3 operator*(const Vector3 &_p) const;
246 
251  public: const Vector3 &operator*=(const Vector3 &_v);
252 
257  public: friend inline Vector3 operator*(double _s,
258  const Vector3 &_v)
259  { return Vector3(_v.x * _s, _v.y * _s, _v.z * _s); }
260 
264  public: Vector3 operator*(double _v) const;
265 
269  public: const Vector3 &operator*=(double _v);
270 
275  public: bool operator ==(const Vector3 &_pt) const;
276 
281  public: bool operator!=(const Vector3 &_v) const;
282 
284  public: bool IsFinite() const;
285 
287  public: inline void Correct()
288  {
289  if (!std::isfinite(this->x))
290  this->x = 0;
291  if (!std::isfinite(this->y))
292  this->y = 0;
293  if (!std::isfinite(this->z))
294  this->z = 0;
295  }
296 
298  public: double operator[](unsigned int index) const;
299 
302  public: void Round(int _precision);
303 
308  public: bool Equal(const Vector3 &_v) const;
309 
311  public: double x;
312 
314  public: double y;
315 
317  public: double z;
318 
323  public: friend std::ostream &operator<<(std::ostream &_out,
324  const gazebo::math::Vector3 &_pt)
325  {
326  _out << precision(_pt.x, 6) << " " << precision(_pt.y, 6) << " "
327  << precision(_pt.z, 6);
328  return _out;
329  }
330 
335  public: friend std::istream &operator>>(std::istream &_in,
337  {
338  // Skip white spaces
339  _in.setf(std::ios_base::skipws);
340  _in >> _pt.x >> _pt.y >> _pt.z;
341  return _in;
342  }
343  };
345  }
346 }
347 #endif
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition: Vector3.hh:54
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:39
friend std::istream & operator>>(std::istream &_in, gazebo::math::Vector3 &_pt)
Stream extraction operator.
Definition: Vector3.hh:335
double x
X location.
Definition: Vector3.hh:311
double z
Z location.
Definition: Vector3.hh:317
Vector3 operator-() const
Negation operator.
Definition: Vector3.hh:201
void Correct()
Corrects any nan values.
Definition: Vector3.hh:287
friend std::ostream & operator<<(std::ostream &_out, const gazebo::math::Vector3 &_pt)
Stream insertion operator.
Definition: Vector3.hh:323
static const Vector3 One
math::Vector3(1, 1, 1)
Definition: Vector3.hh:45
#define GZ_MATH_VISIBLE
Definition: system.hh:141
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition: Vector3.hh:48
void Set(double _x=0, double _y=0, double _z=0)
Set the contents of the vector.
Definition: Vector3.hh:116
Vector3 operator-(const Vector3 &_pt) const
Subtraction operators.
Definition: Vector3.hh:209
static const Vector3 UnitY
math::Vector3(0, 1, 0)
Definition: Vector3.hh:51
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition: Vector3.hh:42
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:182
friend Vector3 operator*(double _s, const Vector3 &_v)
Multiplication operators.
Definition: Vector3.hh:257
double y
Y location.
Definition: Vector3.hh:314