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];
208 return std::abs(this->data[0] * _v[0]) +
209 std::abs(this->data[1] * _v[1]) +
210 std::abs(this->data[2] * _v[2]);
217 return Vector3(std::abs(this->data[0]),
218 std::abs(this->data[1]),
219 std::abs(this->data[2]));
226 static const T sqrZero = 1e-06 * 1e-06;
259 T d = ((*this) - _pt1).
Cross((*
this) - _pt2).
Length();
260 d = d / (_pt2 - _pt1).
Length();
269 if (_v[0] > this->data[0])
270 this->data[0] = _v[0];
271 if (_v[1] > this->data[1])
272 this->data[1] = _v[1];
273 if (_v[2] > this->data[2])
274 this->data[2] = _v[2];
282 if (_v[0] < this->data[0])
283 this->data[0] = _v[0];
284 if (_v[1] < this->data[1])
285 this->data[1] = _v[1];
286 if (_v[2] < this->data[2])
287 this->data[2] = _v[2];
309 this->data[0] = _v[0];
310 this->data[1] = _v[1];
311 this->data[2] = _v[2];
333 return Vector3(this->data[0] + _v[0],
334 this->data[1] + _v[1],
335 this->data[2] + _v[2]);
343 this->data[0] += _v[0];
344 this->data[1] += _v[1];
345 this->data[2] += _v[2];
354 return Vector3(-this->data[0], -this->data[1], -this->data[2]);
362 return Vector3(this->data[0] - _pt[0],
363 this->data[1] - _pt[1],
364 this->data[2] - _pt[2]);
372 this->data[0] -= _pt[0];
373 this->data[1] -= _pt[1];
374 this->data[2] -= _pt[2];
385 return Vector3(this->data[0] / _pt[0],
386 this->data[1] / _pt[1],
387 this->data[2] / _pt[2]);
396 this->data[0] /= _pt[0];
397 this->data[1] /= _pt[1];
398 this->data[2] /= _pt[2];
409 return Vector3(this->data[0] / _v,
433 return Vector3(this->data[0] * _p[0],
434 this->data[1] * _p[1],
435 this->data[2] * _p[2]);
444 this->data[0] *= _v[0];
445 this->data[1] *= _v[1];
446 this->data[2] *= _v[2];
488 return equal<T>(this->data[0], _v[0],
static_cast<T
>(0.001)) &&
489 equal<T>(this->data[1], _v[1], static_cast<T>(0.001)) &&
490 equal<T>(this->data[2], _v[2], static_cast<T>(0.001));
499 return !(*
this == _v);
508 return std::isfinite(static_cast<double>(this->data[0])) &&
509 std::isfinite(static_cast<double>(this->data[1])) &&
510 std::isfinite(static_cast<double>(this->data[2]));
518 if (!std::isfinite(static_cast<double>(this->data[0])))
520 if (!std::isfinite(static_cast<double>(this->data[1])))
522 if (!std::isfinite(static_cast<double>(this->data[2])))
535 return this->data[_index];
542 this->data[0] =
precision(this->data[0], _precision);
543 this->data[1] =
precision(this->data[1], _precision);
544 this->data[2] =
precision(this->data[2], _precision);
553 return equal<T>(this->data[0], _v[0]) &&
554 equal<T>(this->data[1], _v[1]) &&
555 equal<T>(this->data[2], _v[2]);
560 public:
inline T
X()
const
562 return this->data[0];
567 public:
inline T
Y()
const
569 return this->data[1];
574 public:
inline T
Z()
const
576 return this->data[2];
581 public:
inline T &
X()
583 return this->data[0];
588 public:
inline T &
Y()
590 return this->data[1];
595 public:
inline T &
Z()
597 return this->data[2];
602 public:
inline void X(
const T &_v)
609 public:
inline void Y(
const T &_v)
616 public:
inline void Z(
const T &_v)
641 _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:588
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
T AbsDot(const Vector3< T > &_v) const
Return the absolute dot product of this vector and another vector.
Definition: Vector3.hh:206
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:504
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:250
Vector3 operator-() const
Negation operator.
Definition: Vector3.hh:352
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:331
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:211
bool Equal(const Vector3< T > &_v) const
Equality test.
Definition: Vector3.hh:551
T & Z()
Get a mutable reference to the z value.
Definition: Vector3.hh:595
Vector3< double > Vector3d
Definition: Vector3.hh:659
Vector3()
Constructor.
Definition: Vector3.hh:55
const Vector3< T > & operator/=(T _v)
Division assignment operator.
Definition: Vector3.hh:418
T X() const
Get the x value.
Definition: Vector3.hh:560
void Correct()
Corrects any nan values.
Definition: Vector3.hh:514
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:215
void Z(const T &_v)
Set the z value.
Definition: Vector3.hh:616
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:486
Vector3 Perpendicular() const
Return a vector that is perpendicular to this one.
Definition: Vector3.hh:224
T operator[](size_t _index) const
Array subscript operator.
Definition: Vector3.hh:531
const Vector3< T > & operator/=(const Vector3< T > &_pt)
Division assignment operator.
Definition: Vector3.hh:394
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:581
T Y() const
Get the y value.
Definition: Vector3.hh:567
T Min() const
Get the minimum value in the vector.
Definition: Vector3.hh:299
void Round(int _precision)
Round all values to _precision decimal places.
Definition: Vector3.hh:540
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:407
Vector3 & operator=(const Vector3< T > &_v)
Assignment operator.
Definition: Vector3.hh:307
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:280
const Vector3< T > & operator*=(const Vector3< T > &_v)
Multiplication assignment operators.
Definition: Vector3.hh:442
void Y(const T &_v)
Set the y value.
Definition: Vector3.hh:609
Vector3< T > operator*(T _s) const
Multiplication operators.
Definition: Vector3.hh:454
const Vector3 & operator+=(const Vector3< T > &_v)
Addition assignment operator.
Definition: Vector3.hh:341
void Max(const Vector3< T > &_v)
Set this vector's components to the maximum of itself and the passed in vector.
Definition: Vector3.hh:267
T Z() const
Get the z value.
Definition: Vector3.hh:574
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:37
Vector3< float > Vector3f
Definition: Vector3.hh:660
T DistToLine(const Vector3< T > &_pt1, const Vector3 &_pt2)
Get distance to a line.
Definition: Vector3.hh:257
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:625
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:244
void X(const T &_v)
Set the x value.
Definition: Vector3.hh:602
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:370
Vector3< T > operator*(const Vector3< T > &_p) const
Multiplication operator.
Definition: Vector3.hh:431
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:497
static const Vector3 UnitX
math::Vector3(1, 0, 0)
Definition: Vector3.hh:46
const Vector3< T > & operator*=(T _v)
Multiplication operator.
Definition: Vector3.hh:473
Vector3 Round()
Round to near whole number, return the result.
Definition: Vector3.hh:150
Vector3 & operator=(T _v)
Assignment operator.
Definition: Vector3.hh:319
static const Vector3 UnitZ
math::Vector3(0, 0, 1)
Definition: Vector3.hh:52
Vector3< int > Vector3i
Definition: Vector3.hh:658
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:224
const Vector3< T > operator/(const Vector3< T > &_pt) const
Division operator.
Definition: Vector3.hh:383
T Max() const
Get the maximum value in the vector.
Definition: Vector3.hh:292
friend std::istream & operator>>(std::istream &_in, ignition::math::Vector3< T > &_pt)
Stream extraction operator.
Definition: Vector3.hh:637
friend Vector3< T > operator*(T _s, const Vector3< T > &_v)
Multiplication operators.
Definition: Vector3.hh:465
Vector3< T > operator-(const Vector3< T > &_pt) const
Subtraction operators.
Definition: Vector3.hh:360