17 #ifndef IGNITION_MATH_VECTOR3_HH_ 18 #define IGNITION_MATH_VECTOR3_HH_ 65 public:
Vector3(
const T &_x,
const T &_y,
const T &_z)
76 this->data[0] = _v[0];
77 this->data[1] = _v[1];
78 this->data[2] = _v[2];
88 return this->data[0] + this->data[1] + this->data[2];
96 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
97 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
98 (this->data[2]-_pt[2])*(this->data[2]-_pt[2]));
122 return std::pow(this->data[0], 2)
123 + std::pow(this->data[1], 2)
124 + std::pow(this->data[2], 2);
133 if (!equal<T>(d, static_cast<T>(0.0)))
156 this->data[0] = nearbyint(this->data[0]);
157 this->data[1] = nearbyint(this->data[1]);
158 this->data[2] = nearbyint(this->data[2]);
175 public:
inline void Set(T _x = 0, T _y = 0, T _z = 0)
187 return Vector3(this->data[1] * _v[2] - this->data[2] * _v[1],
188 this->data[2] * _v[0] - this->data[0] * _v[2],
189 this->data[0] * _v[1] - this->data[1] * _v[0]);
197 return this->data[0] * _v[0] +
198 this->data[1] * _v[1] +
199 this->data[2] * _v[2];
212 return std::abs(this->data[0] * _v[0]) +
213 std::abs(this->data[1] * _v[1]) +
214 std::abs(this->data[2] * _v[2]);
221 return Vector3(std::abs(this->data[0]),
222 std::abs(this->data[1]),
223 std::abs(this->data[2]));
230 static const T sqrZero = 1e-06 * 1e-06;
263 T d = ((*this) - _pt1).
Cross((*
this) - _pt2).
Length();
264 d = d / (_pt2 - _pt1).
Length();
273 if (_v[0] > this->data[0])
274 this->data[0] = _v[0];
275 if (_v[1] > this->data[1])
276 this->data[1] = _v[1];
277 if (_v[2] > this->data[2])
278 this->data[2] = _v[2];
286 if (_v[0] < this->data[0])
287 this->data[0] = _v[0];
288 if (_v[1] < this->data[1])
289 this->data[1] = _v[1];
290 if (_v[2] < this->data[2])
291 this->data[2] = _v[2];
313 this->data[0] = _v[0];
314 this->data[1] = _v[1];
315 this->data[2] = _v[2];
337 return Vector3(this->data[0] + _v[0],
338 this->data[1] + _v[1],
339 this->data[2] + _v[2]);
347 this->data[0] += _v[0];
348 this->data[1] += _v[1];
349 this->data[2] += _v[2];
371 return {_v.
X() + _s, _v.
Y() + _s, _v.
Z() + _s};
390 return Vector3(-this->data[0], -this->data[1], -this->data[2]);
398 return Vector3(this->data[0] - _pt[0],
399 this->data[1] - _pt[1],
400 this->data[2] - _pt[2]);
408 this->data[0] -= _pt[0];
409 this->data[1] -= _pt[1];
410 this->data[2] -= _pt[2];
432 return {_s - _v.
X(), _s - _v.
Y(), _s - _v.
Z()};
453 return Vector3(this->data[0] / _pt[0],
454 this->data[1] / _pt[1],
455 this->data[2] / _pt[2]);
464 this->data[0] /= _pt[0];
465 this->data[1] /= _pt[1];
466 this->data[2] /= _pt[2];
477 return Vector3(this->data[0] / _v,
501 return Vector3(this->data[0] * _p[0],
502 this->data[1] * _p[1],
503 this->data[2] * _p[2]);
512 this->data[0] *= _v[0];
513 this->data[1] *= _v[1];
514 this->data[2] *= _v[2];
535 return {_v.
X() * _s, _v.
Y() * _s, _v.
Z() * _s};
557 return equal<T>(this->data[0], _v[0], _tol)
558 && equal<T>(this->data[1], _v[1], _tol)
559 && equal<T>(this->data[2], _v[2], _tol);
568 return this->
Equal(_v, static_cast<T>(1e-3));
577 return !(*
this == _v);
586 return std::isfinite(static_cast<double>(this->data[0])) &&
587 std::isfinite(static_cast<double>(this->data[1])) &&
588 std::isfinite(static_cast<double>(this->data[2]));
596 if (!std::isfinite(static_cast<double>(this->data[0])))
598 if (!std::isfinite(static_cast<double>(this->data[1])))
600 if (!std::isfinite(static_cast<double>(this->data[2])))
617 this->data[0] =
precision(this->data[0], _precision);
618 this->data[1] =
precision(this->data[1], _precision);
619 this->data[2] =
precision(this->data[2], _precision);
628 return equal<T>(this->data[0], _v[0]) &&
629 equal<T>(this->data[1], _v[1]) &&
630 equal<T>(this->data[2], _v[2]);
635 public:
inline T
X()
const 637 return this->data[0];
642 public:
inline T
Y()
const 644 return this->data[1];
649 public:
inline T
Z()
const 651 return this->data[2];
656 public:
inline T &
X()
658 return this->data[0];
663 public:
inline T &
Y()
665 return this->data[1];
670 public:
inline T &
Z()
672 return this->data[2];
677 public:
inline void X(
const T &_v)
684 public:
inline void Y(
const T &_v)
691 public:
inline void Z(
const T &_v)
700 public:
bool operator<(const Vector3<T> &_pt)
const 702 return this->data[0] < _pt[0] || this->data[1] < _pt[1] ||
703 this->data[2] < _pt[2];
726 _in.setf(std::ios_base::skipws);
Vector3 Rounded() const
Get a rounded version of this vector.
Definition: Vector3.hh:164
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition: Vector3.hh:39
T X() const
Get the x value.
Definition: Vector3.hh:635
T & Y()
Get a mutable reference to the y value.
Definition: Vector3.hh:663
static const Vector3 UnitY
math::Vector3(0, 1, 0)
Definition: Vector3.hh:48
virtual ~Vector3()
Destructor.
Definition: Vector3.hh:82
bool Equal(const Vector3 &_v, const T &_tol) const
Equality test with tolerance.
Definition: Vector3.hh:555
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition: Vector3.hh:175
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector3.hh:113
Vector3< T > operator-(const T _s) const
Subtraction operators.
Definition: Vector3.hh:418
friend Vector3< T > operator-(const T _s, const Vector3< T > &_v)
Subtraction operators.
Definition: Vector3.hh:429
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:576
bool operator==(const Vector3< T > &_v) const
Equal to operator.
Definition: Vector3.hh:566
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:213
T Max() const
Get the maximum value in the vector.
Definition: Vector3.hh:296
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:515
Vector3 operator-() const
Negation operator.
Definition: Vector3.hh:388
T & Z()
Get a mutable reference to the z value.
Definition: Vector3.hh:670
Vector3< double > Vector3d
Definition: Vector3.hh:744
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:219
Vector3()
Constructor.
Definition: Vector3.hh:54
const Vector3< T > operator/(const Vector3< T > &_pt) const
Division operator.
Definition: Vector3.hh:451
Vector3 Normalized() const
Return a normalized vector.
Definition: Vector3.hh:145
const Vector3< T > & operator/=(T _v)
Division assignment operator.
Definition: Vector3.hh:486
void Correct()
Corrects any nan values.
Definition: Vector3.hh:592
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector3.hh:582
void Z(const T &_v)
Set the z value.
Definition: Vector3.hh:691
Vector3 Normalize()
Normalize the vector length.
Definition: Vector3.hh:129
Vector3(const T &_x, const T &_y, const T &_z)
Constructor.
Definition: Vector3.hh:65
const Vector3< T > & operator/=(const Vector3< T > &_pt)
Division assignment operator.
Definition: Vector3.hh:462
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:195
T & X()
Get a mutable reference to the x value.
Definition: Vector3.hh:656
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector3.hh:120
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Vector3.hh:615
bool Equal(const Vector3< T > &_v) const
Equality test.
Definition: Vector3.hh:626
Vector3 & operator=(const Vector3< T > &_v)
Assignment operator.
Definition: Vector3.hh:311
static const Vector3 One
math::Vector3(1, 1, 1)
Definition: Vector3.hh:42
void Min(const Vector3< T > &_v)
Set this vector's components to the minimum of itself and the passed in vector.
Definition: Vector3.hh:284
const Vector3< T > & operator*=(const Vector3< T > &_v)
Multiplication assignment operators.
Definition: Vector3.hh:510
void Y(const T &_v)
Set the y value.
Definition: Vector3.hh:684
friend Vector3< T > operator+(const T _s, const Vector3< T > &_v)
Addition operators.
Definition: Vector3.hh:368
T Y() const
Get the y value.
Definition: Vector3.hh:642
T Min() const
Get the minimum value in the vector.
Definition: Vector3.hh:303
const Vector3 & operator+=(const Vector3< T > &_v)
Addition assignment operator.
Definition: Vector3.hh:345
void Max(const Vector3< T > &_v)
Set this vector's components to the maximum of itself and the passed in vector.
Definition: Vector3.hh:271
T operator[](const size_t _index) const
Array subscript operator.
Definition: Vector3.hh:608
Vector3< float > Vector3f
Definition: Vector3.hh:745
T Distance(T _x, T _y, T _z) const
Calc distance to the given point.
Definition: Vector3.hh:106
const Vector3< T > & operator-=(const T _s)
Subtraction assignment operator.
Definition: Vector3.hh:438
static const size_t IGN_TWO_SIZE_T
size_t type with a value of 2
Definition: Helpers.hh:219
Vector3< T > operator-(const Vector3< T > &_pt) const
Subtraction operators.
Definition: Vector3.hh:396
T DistToLine(const Vector3< T > &_pt1, const Vector3 &_pt2)
Get distance to a line.
Definition: Vector3.hh:261
bool operator!=(const Vector3< T > &_v) const
Not equal to operator.
Definition: Vector3.hh:575
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:36
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Vector3< T > &_pt)
Stream insertion operator.
Definition: Vector3.hh:710
const Vector3< T > & operator+=(const T _s)
Addition assignment operator.
Definition: Vector3.hh:377
Vector3(const Vector3< T > &_v)
Copy constructor.
Definition: Vector3.hh:74
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:248
void X(const T &_v)
Set the x value.
Definition: Vector3.hh:677
T AbsDot(const Vector3< T > &_v) const
Return the absolute dot product of this vector and another vector.
Definition: Vector3.hh:210
T Z() const
Get the z value.
Definition: Vector3.hh:649
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition: Vector3.hh:185
const Vector3< T > & operator-=(const Vector3< T > &_pt)
Subtraction assignment operators.
Definition: Vector3.hh:406
Vector3< T > operator*(const Vector3< T > &_p) const
Multiplication operator.
Definition: Vector3.hh:499
Vector3< T > operator*(T _s) const
Multiplication operators.
Definition: Vector3.hh:522
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition: Vector3.hh:45
Vector3 operator+(const Vector3< T > &_v) const
Addition operator.
Definition: Vector3.hh:335
const Vector3< T > & operator*=(T _v)
Multiplication operator.
Definition: Vector3.hh:541
T Distance(const Vector3< T > &_pt) const
Calc distance to the given point.
Definition: Vector3.hh:94
Vector3 Round()
Round to near whole number, return the result.
Definition: Vector3.hh:154
Vector3 & operator=(T _v)
Assignment operator.
Definition: Vector3.hh:323
const Vector3< T > operator/(T _v) const
Division operator.
Definition: Vector3.hh:475
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition: Vector3.hh:51
Vector3< int > Vector3i
Definition: Vector3.hh:743
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:528
Vector3< T > operator+(const T _s) const
Addition operators.
Definition: Vector3.hh:357
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:392
Vector3 Perpendicular() const
Return a vector that is perpendicular to this one.
Definition: Vector3.hh:228
T Sum() const
Return the sum of the values.
Definition: Vector3.hh:86
friend std::istream & operator>>(std::istream &_in, ignition::math::Vector3< T > &_pt)
Stream extraction operator.
Definition: Vector3.hh:722
friend Vector3< T > operator*(T _s, const Vector3< T > &_v)
Multiplication operators.
Definition: Vector3.hh:533