17#ifndef IGNITION_MATH_VECTOR3_HH_
18#define IGNITION_MATH_VECTOR3_HH_
26#include <ignition/math/config.hh>
32 inline namespace IGNITION_MATH_VERSION_NAMESPACE
68 public:
Vector3(
const T &_x,
const T &_y,
const T &_z)
79 this->data[0] = _v[0];
80 this->data[1] = _v[1];
81 this->data[2] = _v[2];
91 return this->data[0] + this->data[1] + this->data[2];
99 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
100 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
101 (this->data[2]-_pt[2])*(this->data[2]-_pt[2]));
125 return std::pow(this->data[0], 2)
126 + std::pow(this->data[1], 2)
127 + std::pow(this->data[2], 2);
136 if (!
equal<T>(d,
static_cast<T
>(0.0)))
159 this->data[0] = nearbyint(this->data[0]);
160 this->data[1] = nearbyint(this->data[1]);
161 this->data[2] = nearbyint(this->data[2]);
178 public:
inline void Set(T _x = 0, T _y = 0, T _z = 0)
190 return Vector3(this->data[1] * _v[2] - this->data[2] * _v[1],
191 this->data[2] * _v[0] - this->data[0] * _v[2],
192 this->data[0] * _v[1] - this->data[1] * _v[0]);
200 return this->data[0] * _v[0] +
201 this->data[1] * _v[1] +
202 this->data[2] * _v[2];
215 return std::abs(this->data[0] * _v[0]) +
216 std::abs(this->data[1] * _v[1]) +
217 std::abs(this->data[2] * _v[2]);
224 return Vector3(std::abs(this->data[0]),
225 std::abs(this->data[1]),
226 std::abs(this->data[2]));
233 static const T sqrZero = 1e-06 * 1e-06;
266 T d = ((*this) - _pt1).
Cross((*
this) - _pt2).
Length();
267 d = d / (_pt2 - _pt1).
Length();
276 if (_v[0] > this->data[0])
277 this->data[0] = _v[0];
278 if (_v[1] > this->data[1])
279 this->data[1] = _v[1];
280 if (_v[2] > this->data[2])
281 this->data[2] = _v[2];
289 if (_v[0] < this->data[0])
290 this->data[0] = _v[0];
291 if (_v[1] < this->data[1])
292 this->data[1] = _v[1];
293 if (_v[2] < this->data[2])
294 this->data[2] = _v[2];
301 return std::max(std::max(this->data[0], this->data[1]), this->data[2]);
308 return std::min(std::min(this->data[0], this->data[1]), this->data[2]);
316 this->data[0] = _v[0];
317 this->data[1] = _v[1];
318 this->data[2] = _v[2];
340 return Vector3(this->data[0] + _v[0],
341 this->data[1] + _v[1],
342 this->data[2] + _v[2]);
350 this->data[0] += _v[0];
351 this->data[1] += _v[1];
352 this->data[2] += _v[2];
374 return {_v.
X() + _s, _v.
Y() + _s, _v.
Z() + _s};
393 return Vector3(-this->data[0], -this->data[1], -this->data[2]);
401 return Vector3(this->data[0] - _pt[0],
402 this->data[1] - _pt[1],
403 this->data[2] - _pt[2]);
411 this->data[0] -= _pt[0];
412 this->data[1] -= _pt[1];
413 this->data[2] -= _pt[2];
435 return {_s - _v.
X(), _s - _v.
Y(), _s - _v.
Z()};
456 return Vector3(this->data[0] / _pt[0],
457 this->data[1] / _pt[1],
458 this->data[2] / _pt[2]);
467 this->data[0] /= _pt[0];
468 this->data[1] /= _pt[1];
469 this->data[2] /= _pt[2];
480 return Vector3(this->data[0] / _v,
504 return Vector3(this->data[0] * _p[0],
505 this->data[1] * _p[1],
506 this->data[2] * _p[2]);
515 this->data[0] *= _v[0];
516 this->data[1] *= _v[1];
517 this->data[2] *= _v[2];
538 return {_v.
X() * _s, _v.
Y() * _s, _v.
Z() * _s};
560 return equal<T>(this->data[0], _v[0], _tol)
561 &&
equal<T>(this->data[1], _v[1], _tol)
562 &&
equal<T>(this->data[2], _v[2], _tol);
571 return this->
Equal(_v,
static_cast<T
>(1e-3));
580 return !(*
this == _v);
589 return std::isfinite(
static_cast<double>(this->data[0])) &&
590 std::isfinite(
static_cast<double>(this->data[1])) &&
591 std::isfinite(
static_cast<double>(this->data[2]));
599 if (!std::isfinite(
static_cast<double>(this->data[0])))
601 if (!std::isfinite(
static_cast<double>(this->data[1])))
603 if (!std::isfinite(
static_cast<double>(this->data[2])))
629 this->data[0] =
precision(this->data[0], _precision);
630 this->data[1] =
precision(this->data[1], _precision);
631 this->data[2] =
precision(this->data[2], _precision);
640 return equal<T>(this->data[0], _v[0]) &&
647 public:
inline T
X()
const
649 return this->data[0];
654 public:
inline T
Y()
const
656 return this->data[1];
661 public:
inline T
Z()
const
663 return this->data[2];
668 public:
inline T &
X()
670 return this->data[0];
675 public:
inline T &
Y()
677 return this->data[1];
682 public:
inline T &
Z()
684 return this->data[2];
689 public:
inline void X(
const T &_v)
696 public:
inline void Y(
const T &_v)
703 public:
inline void Z(
const T &_v)
714 return this->data[0] < _pt[0] || this->data[1] < _pt[1] ||
715 this->data[2] < _pt[2];
738 _in.setf(std::ios_base::skipws);
The Vector3 class represents the generic vector containing 3 elements.
Definition Vector3.hh:40
void X(const T &_v)
Set the x value.
Definition Vector3.hh:689
friend Vector3< T > operator*(T _s, const Vector3< T > &_v)
Multiplication operators.
Definition Vector3.hh:536
static const Vector3 One
math::Vector3(1, 1, 1)
Definition Vector3.hh:45
Vector3 Abs() const
Get the absolute value of the vector.
Definition Vector3.hh:222
Vector3()
Constructor.
Definition Vector3.hh:57
Vector3< T > operator*(T _s) const
Multiplication operators.
Definition Vector3.hh:525
Vector3 Perpendicular() const
Return a vector that is perpendicular to this one.
Definition Vector3.hh:231
Vector3 Round()
Round to near whole number, return the result.
Definition Vector3.hh:157
const Vector3< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition Vector3.hh:441
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition Vector3.hh:585
T Sum() const
Return the sum of the values.
Definition Vector3.hh:89
void Y(const T &_v)
Set the y value.
Definition Vector3.hh:696
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Vector3< T > &_pt)
Stream insertion operator.
Definition Vector3.hh:722
void Round(int _precision)
Round all values to _precision decimal places.
Definition Vector3.hh:627
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition Vector3.hh:178
void Correct()
Corrects any nan values.
Definition Vector3.hh:595
void Min(const Vector3< T > &_v)
Set this vector's components to the minimum of itself and the passed in vector.
Definition Vector3.hh:287
static const Vector3 UnitY
math::Vector3(0, 1, 0)
Definition Vector3.hh:51
T Z() const
Get the z value.
Definition Vector3.hh:661
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition Vector3.hh:123
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition Vector3.hh:558
T & X()
Get a mutable reference to the x value.
Definition Vector3.hh:668
const Vector3 & operator+=(const Vector3< T > &_v)
Addition assignment operator.
Definition Vector3.hh:348
T Max() const
Get the maximum value in the vector.
Definition Vector3.hh:299
friend std::istream & operator>>(std::istream &_in, ignition::math::Vector3< T > &_pt)
Stream extraction operator.
Definition Vector3.hh:734
void Max(const Vector3< T > &_v)
Set this vector's components to the maximum of itself and the passed in vector.
Definition Vector3.hh:274
Vector3 & operator=(T _v)
Assignment operator.
Definition Vector3.hh:326
T Distance(T _x, T _y, T _z) const
Calc distance to the given point.
Definition Vector3.hh:109
T Y() const
Get the y value.
Definition Vector3.hh:654
Vector3< T > operator+(const T _s) const
Addition operators.
Definition Vector3.hh:360
T & operator[](const std::size_t _index)
Array subscript operator.
Definition Vector3.hh:611
const Vector3< T > & operator*=(T _v)
Multiplication operator.
Definition Vector3.hh:544
T Length() const
Returns the length (magnitude) of the vector.
Definition Vector3.hh:116
T Min() const
Get the minimum value in the vector.
Definition Vector3.hh:306
bool Equal(const Vector3< T > &_v) const
Equality test.
Definition Vector3.hh:638
T AbsDot(const Vector3< T > &_v) const
Return the absolute dot product of this vector and another vector.
Definition Vector3.hh:213
T operator[](const std::size_t _index) const
Const-qualified array subscript operator.
Definition Vector3.hh:620
Vector3 Normalize()
Normalize the vector length.
Definition Vector3.hh:132
const Vector3< T > & operator-=(const Vector3< T > &_pt)
Subtraction assignment operators.
Definition Vector3.hh:409
T & Z()
Get a mutable reference to the z value.
Definition Vector3.hh:682
Vector3(const Vector3< T > &_v)
Copy constructor.
Definition Vector3.hh:77
friend Vector3< T > operator-(const T _s, const Vector3< T > &_v)
Subtraction operators.
Definition Vector3.hh:432
Vector3< T > operator-(const Vector3< T > &_pt) const
Subtraction operators.
Definition Vector3.hh:399
Vector3 Rounded() const
Get a rounded version of this vector.
Definition Vector3.hh:167
virtual ~Vector3()
Destructor.
Definition Vector3.hh:85
T Distance(const Vector3< T > &_pt) const
Calc distance to the given point.
Definition Vector3.hh:97
Vector3< T > operator-(const T _s) const
Subtraction operators.
Definition Vector3.hh:421
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition Vector3.hh:48
Vector3 Normalized() const
Return a normalized vector.
Definition Vector3.hh:148
static Vector3 Normal(const Vector3< T > &_v1, const Vector3< T > &_v2, const Vector3< T > &_v3)
Get a normal vector to a triangle.
Definition Vector3.hh:251
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition Vector3.hh:198
Vector3 operator+(const Vector3< T > &_v) const
Addition operator.
Definition Vector3.hh:338
void Z(const T &_v)
Set the z value.
Definition Vector3.hh:703
const Vector3< T > & operator/=(T _v)
Division assignment operator.
Definition Vector3.hh:489
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition Vector3.hh:188
const Vector3< T > operator/(const Vector3< T > &_pt) const
Division operator.
Definition Vector3.hh:454
Vector3 operator-() const
Negation operator.
Definition Vector3.hh:391
T X() const
Get the x value.
Definition Vector3.hh:647
const Vector3< T > & operator*=(const Vector3< T > &_v)
Multiplication assignment operators.
Definition Vector3.hh:513
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition Vector3.hh:42
Vector3(const T &_x, const T &_y, const T &_z)
Constructor.
Definition Vector3.hh:68
Vector3 & operator=(const Vector3< T > &_v)
Assignment operator.
Definition Vector3.hh:314
friend Vector3< T > operator+(const T _s, const Vector3< T > &_v)
Addition operators.
Definition Vector3.hh:371
T & Y()
Get a mutable reference to the y value.
Definition Vector3.hh:675
const Vector3< T > & operator/=(const Vector3< T > &_pt)
Division assignment operator.
Definition Vector3.hh:465
bool operator<(const Vector3< T > &_pt) const
Less than operator.
Definition Vector3.hh:712
Vector3< T > operator*(const Vector3< T > &_p) const
Multiplication operator.
Definition Vector3.hh:502
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition Vector3.hh:54
bool operator==(const Vector3< T > &_v) const
Equal to operator.
Definition Vector3.hh:569
const Vector3< T > & operator+=(const T _s)
Addition assignment operator.
Definition Vector3.hh:380
bool operator!=(const Vector3< T > &_v) const
Not equal to operator.
Definition Vector3.hh:578
T DistToLine(const Vector3< T > &_pt1, const Vector3 &_pt2)
Get distance to a line.
Definition Vector3.hh:264
const Vector3< T > operator/(T _v) const
Division operator.
Definition Vector3.hh:478
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition Helpers.hh:579
Vector3< double > Vector3d
Definition Vector3.hh:756
Vector3< float > Vector3f
Definition Vector3.hh:757
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition Helpers.hh:395
Vector3< int > Vector3i
Definition Vector3.hh:755
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition Helpers.hh:222
bool equal(const T &_a, const T &_b, const T &_epsilon=T(1e-6))
check if two values are equal, within a tolerance
Definition Helpers.hh:545
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition Helpers.hh:216