17 #ifndef _IGNITION_VECTOR3_HH_
18 #define _IGNITION_VECTOR3_HH_
66 public:
Vector3(
const T &_x,
const T &_y,
const T &_z)
77 this->data[0] = _v[0];
78 this->data[1] = _v[1];
79 this->data[2] = _v[2];
89 return this->data[0] + this->data[1] + this->data[2];
97 return sqrt((this->data[0]-_pt[0])*(this->data[0]-_pt[0]) +
98 (this->data[1]-_pt[1])*(this->data[1]-_pt[1]) +
99 (this->data[2]-_pt[2])*(this->data[2]-_pt[2]));
116 return sqrt(this->data[0] * this->data[0] +
117 this->data[1] * this->data[1] +
118 this->data[2] * this->data[2]);
125 return this->data[0] * this->data[0] +
126 this->data[1] * this->data[1] +
127 this->data[2] * this->data[2];
134 T d = sqrt(this->data[0] * this->data[0] +
135 this->data[1] * this->data[1] +
136 this->data[2] * this->data[2]);
138 if (!equal<T>(d, static_cast<T>(0.0)))
152 this->data[0] = nearbyint(this->data[0]);
153 this->data[1] = nearbyint(this->data[1]);
154 this->data[2] = nearbyint(this->data[2]);
171 public:
inline void Set(T _x = 0, T _y = 0, T _z = 0)
183 return Vector3(this->data[1] * _v[2] - this->data[2] * _v[1],
184 this->data[2] * _v[0] - this->data[0] * _v[2],
185 this->data[0] * _v[1] - this->data[1] * _v[0]);
193 return this->data[0] * _v[0] +
194 this->data[1] * _v[1] +
195 this->data[2] * _v[2];
202 return Vector3(std::abs(this->data[0]),
203 std::abs(this->data[1]),
204 std::abs(this->data[2]));
211 static const T sqrZero = 1e-06 * 1e-06;
244 T d = ((*this) - _pt1).
Cross((*
this) - _pt2).
Length();
245 d = d / (_pt2 - _pt1).
Length();
254 if (_v[0] > this->data[0])
255 this->data[0] = _v[0];
256 if (_v[1] > this->data[1])
257 this->data[1] = _v[1];
258 if (_v[2] > this->data[2])
259 this->data[2] = _v[2];
267 if (_v[0] < this->data[0])
268 this->data[0] = _v[0];
269 if (_v[1] < this->data[1])
270 this->data[1] = _v[1];
271 if (_v[2] < this->data[2])
272 this->data[2] = _v[2];
294 this->data[0] = _v[0];
295 this->data[1] = _v[1];
296 this->data[2] = _v[2];
318 return Vector3(this->data[0] + _v[0],
319 this->data[1] + _v[1],
320 this->data[2] + _v[2]);
328 this->data[0] += _v[0];
329 this->data[1] += _v[1];
330 this->data[2] += _v[2];
339 return Vector3(-this->data[0], -this->data[1], -this->data[2]);
347 return Vector3(this->data[0] - _pt[0],
348 this->data[1] - _pt[1],
349 this->data[2] - _pt[2]);
357 this->data[0] -= _pt[0];
358 this->data[1] -= _pt[1];
359 this->data[2] -= _pt[2];
370 return Vector3(this->data[0] / _pt[0],
371 this->data[1] / _pt[1],
372 this->data[2] / _pt[2]);
381 this->data[0] /= _pt[0];
382 this->data[1] /= _pt[1];
383 this->data[2] /= _pt[2];
394 return Vector3(this->data[0] / _v,
418 return Vector3(this->data[0] * _p[0],
419 this->data[1] * _p[1],
420 this->data[2] * _p[2]);
429 this->data[0] *= _v[0];
430 this->data[1] *= _v[1];
431 this->data[2] *= _v[2];
473 return equal<T>(this->data[0], _v[0],
static_cast<T
>(0.001)) &&
474 equal<T>(this->data[1], _v[1], static_cast<T>(0.001)) &&
475 equal<T>(this->data[2], _v[2], static_cast<T>(0.001));
484 return !(*
this == _v);
493 return std::isfinite(static_cast<double>(this->data[0])) &&
494 std::isfinite(static_cast<double>(this->data[1])) &&
495 std::isfinite(static_cast<double>(this->data[2]));
503 if (!std::isfinite(static_cast<double>(this->data[0])))
505 if (!std::isfinite(static_cast<double>(this->data[1])))
507 if (!std::isfinite(static_cast<double>(this->data[2])))
520 return this->data[_index];
527 this->data[0] =
precision(this->data[0], _precision);
528 this->data[1] =
precision(this->data[1], _precision);
529 this->data[2] =
precision(this->data[2], _precision);
538 return equal<T>(this->data[0], _v[0]) &&
539 equal<T>(this->data[1], _v[1]) &&
540 equal<T>(this->data[2], _v[2]);
545 public:
inline T
X()
const
547 return this->data[0];
552 public:
inline T
Y()
const
554 return this->data[1];
559 public:
inline T
Z()
const
561 return this->data[2];
566 public:
inline T &
X()
568 return this->data[0];
573 public:
inline T &
Y()
575 return this->data[1];
580 public:
inline T &
Z()
582 return this->data[2];
587 public:
inline void X(
const T &_v)
594 public:
inline void Y(
const T &_v)
601 public:
inline void Z(
const T &_v)
626 _in.setf(std::ios_base::skipws);
static const Vector3 Zero
math::Vector3(0, 0, 0)
Definition: Vector3.hh:40
T & Y()
Get a mutable reference to the y value.
Definition: Vector3.hh:573
static const Vector3 UnitY
math::Vector3(0, 1, 0)
Definition: Vector3.hh:49
virtual ~Vector3()
Destructor.
Definition: Vector3.hh:83
T Length() const
Returns the length (magnitude) of the vector \ return the length.
Definition: Vector3.hh:114
void Set(T _x=0, T _y=0, T _z=0)
Set the contents of the vector.
Definition: Vector3.hh:171
bool IsFinite() const
See if a point is finite (e.g., not nan)
Definition: Vector3.hh:489
T Sum() const
Return the sum of the values.
Definition: Vector3.hh:87
T precision(const T &_a, const unsigned int &_precision)
get value at a specified precision
Definition: Helpers.hh:188
Vector3 operator-() const
Negation operator.
Definition: Vector3.hh:337
T Distance(const Vector3< T > &_pt) const
Calc distance to the given point.
Definition: Vector3.hh:95
Vector3 operator+(const Vector3< T > &_v) const
Addition operator.
Definition: Vector3.hh:316
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:150
bool Equal(const Vector3< T > &_v) const
Equality test.
Definition: Vector3.hh:536
T & Z()
Get a mutable reference to the z value.
Definition: Vector3.hh:580
Vector3< double > Vector3d
Definition: Vector3.hh:644
Vector3()
Constructor.
Definition: Vector3.hh:55
const Vector3< T > & operator/=(T _v)
Division assignment operator.
Definition: Vector3.hh:403
T X() const
Get the x value.
Definition: Vector3.hh:545
void Correct()
Corrects any nan values.
Definition: Vector3.hh:499
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:200
void Z(const T &_v)
Set the z value.
Definition: Vector3.hh:601
Vector3 Normalize()
Normalize the vector length.
Definition: Vector3.hh:132
Vector3(const T &_x, const T &_y, const T &_z)
Constructor.
Definition: Vector3.hh:66
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:191
bool operator==(const Vector3< T > &_v) const
Equal to operator.
Definition: Vector3.hh:471
Vector3 Perpendicular() const
Return a vector that is perpendicular to this one.
Definition: Vector3.hh:209
T operator[](size_t _index) const
Array subscript operator.
Definition: Vector3.hh:516
const Vector3< T > & operator/=(const Vector3< T > &_pt)
Division assignment operator.
Definition: Vector3.hh:379
Vector3 Cross(const Vector3< T > &_v) const
Return the cross product of this vector with another vector.
Definition: Vector3.hh:181
T & X()
Get a mutable reference to the x value.
Definition: Vector3.hh:566
T Y() const
Get the y value.
Definition: Vector3.hh:552
T Min() const
Get the minimum value in the vector.
Definition: Vector3.hh:284
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Vector3.hh:525
Vector3 Rounded() const
Get a rounded version of this vector.
Definition: Vector3.hh:160
const Vector3< T > operator/(T _v) const
Division operator.
Definition: Vector3.hh:392
Vector3 & operator=(const Vector3< T > &_v)
Assignment operator.
Definition: Vector3.hh:292
static const Vector3 One
math::Vector3(1, 1, 1)
Definition: Vector3.hh:43
void Min(const Vector3< T > &_v)
Set this vector's components to the minimum of itself and the passed in vector.
Definition: Vector3.hh:265
const Vector3< T > & operator*=(const Vector3< T > &_v)
Multiplication assignment operators.
Definition: Vector3.hh:427
void Y(const T &_v)
Set the y value.
Definition: Vector3.hh:594
Vector3< T > operator*(T _s) const
Multiplication operators.
Definition: Vector3.hh:439
const Vector3 & operator+=(const Vector3< T > &_v)
Addition assignment operator.
Definition: Vector3.hh:326
void Max(const Vector3< T > &_v)
Set this vector's components to the maximum of itself and the passed in vector.
Definition: Vector3.hh:252
T Z() const
Get the z value.
Definition: Vector3.hh:559
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:30
Vector3< float > Vector3f
Definition: Vector3.hh:645
T DistToLine(const Vector3< T > &_pt1, const Vector3 &_pt2)
Get distance to a line.
Definition: Vector3.hh:242
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
friend std::ostream & operator<<(std::ostream &_out, const ignition::math::Vector3< T > &_pt)
Stream insertion operator.
Definition: Vector3.hh:610
Vector3(const Vector3< T > &_v)
Copy constructor.
Definition: Vector3.hh:75
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:229
void X(const T &_v)
Set the x value.
Definition: Vector3.hh:587
T Distance(T _x, T _y, T _z) const
Calc distance to the given point.
Definition: Vector3.hh:107
const Vector3< T > & operator-=(const Vector3< T > &_pt)
Subtraction assignment operators.
Definition: Vector3.hh:355
Vector3< T > operator*(const Vector3< T > &_p) const
Multiplication operator.
Definition: Vector3.hh:416
T SquaredLength() const
Return the square of the length (magnitude) of the vector.
Definition: Vector3.hh:123
bool operator!=(const Vector3< T > &_v) const
Not equal to operator.
Definition: Vector3.hh:482
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition: Vector3.hh:46
const Vector3< T > & operator*=(T _v)
Multiplication operator.
Definition: Vector3.hh:458
Vector3 Round()
Round to near whole number, return the result.
Definition: Vector3.hh:150
Vector3 & operator=(T _v)
Assignment operator.
Definition: Vector3.hh:304
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition: Vector3.hh:52
Vector3< int > Vector3i
Definition: Vector3.hh:643
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:163
const Vector3< T > operator/(const Vector3< T > &_pt) const
Division operator.
Definition: Vector3.hh:368
T Max() const
Get the maximum value in the vector.
Definition: Vector3.hh:277
friend std::istream & operator>>(std::istream &_in, ignition::math::Vector3< T > &_pt)
Stream extraction operator.
Definition: Vector3.hh:622
friend Vector3< T > operator*(T _s, const Vector3< T > &_v)
Multiplication operators.
Definition: Vector3.hh:450
Vector3< T > operator-(const Vector3< T > &_pt) const
Subtraction operators.
Definition: Vector3.hh:345