45#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_3D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_3D_INL
63template<
typename T>
inline
64Vector3D<T>::Vector3D():
73template<
typename T>
inline
74Vector3D<T>::Vector3D(TParam x, TParam y, TParam z):
84template <
typename U>
inline
85Vector3D<T>::Vector3D(
const Vector3D<U>& other):
86 x(static_cast<TValue>(other.x)),
87 y(static_cast<TValue>(other.y)),
88 z(static_cast<TValue>(other.z))
95template <
typename U>
inline
96Vector3D<T>::Vector3D(
const U& x,
const U& y,
const U& z):
97 x(static_cast<TValue>(x)),
98 y(static_cast<TValue>(y)),
99 z(static_cast<TValue>(z))
106typename Vector3D<T>::TConstReference Vector3D<T>::operator[](
size_t index)
const
108 LASS_ASSERT(index < dimension);
109 return *(&x + index);
115typename Vector3D<T>::TReference Vector3D<T>::operator[](
size_t index)
117 LASS_ASSERT(index < dimension);
118 return *(&x + index);
128 return *(&x + num::mod(index, dimension));
138 return *(&x + num::mod(index, dimension));
163template<
typename T>
inline
189template<
typename T>
inline
202template<
typename T>
inline
215template<
typename T>
inline
228template<
typename T>
inline
241template<
typename T>
inline
254template<
typename T>
inline
267template<
typename T>
inline
270 return x == TNumTraits::zero && y == TNumTraits::zero && z == TNumTraits::zero;
277template<
typename T>
inline
280 return num::isNaN(x) || num::isNaN(y) || num::isNaN(z);
287template<
typename T>
inline
290 return dot(*
this, *
this);
297template<
typename T>
inline
313 if (absX > absY && absX > absZ)
317 else if (absY > absZ)
337 Vector3D<T> result(*
this);
349 Vector3D<T> result(TNumTraits::one, TNumTraits::one, TNumTraits::one);
361 Vector3D<T> result(*
this);
362 result *=
dot(other, *
this);
371template<
typename T>
inline
379template<
typename T>
inline
382 return 2 *
project(other) - other;
392 return Vector3D<T>(op(x), op(y), op(z));
399template<
typename T>
inline
410template <
class RandomGenerator>
413 std::uniform_real_distribution<T> zDistribution(-TNumTraits::one, std::nextafter(TNumTraits::one, TNumTraits::max));
414 const TValue z = zDistribution(generator);
415 const TValue r = num::sqrt(1 -
num::sqr(z));
417 std::uniform_real_distribution<T> thetaDistribution(TNumTraits::zero, 2 * TNumTraits::pi);
418 const TValue theta = thetaDistribution(generator);
420 return Vector3D<T>(r * num::cos(theta), r * num::sin(theta), z);
428template<
typename T>
inline
429typename Vector3D<T>::TValue
dot(
const Vector3D<T>& a,
const Vector3D<T>& b)
431 return a.x * b.x + a.y * b.y + a.z * b.z;
439template<
typename T>
inline
440Vector3D<T>
cross(
const Vector3D<T>& a,
const Vector3D<T>& b)
442 return Vector3D<T>(a.y * b.z - a.z * b.y,
443 a.z * b.x - a.x * b.z,
444 a.x * b.y - a.y * b.x);
451template<
typename T>
inline
454 return a.x == b.x && a.y == b.y && a.z == b.z;
461template<
typename T>
inline
462bool operator!=(
const Vector3D<T>& a,
const Vector3D<T>& b)
472template<
typename T>
inline
473Vector3D<T>
operator+(
const Vector3D<T>& a,
const Vector3D<T>& b)
475 Vector3D<T> result(a);
485template<
typename T>
inline
486Vector3D<T>
operator-(
const Vector3D<T>& a,
const Vector3D<T>& b)
488 Vector3D<T> result(a);
498template<
typename T>
inline
499Vector3D<T>
operator*(
const Vector3D<T>& a,
const Vector3D<T>& b)
501 Vector3D<T> result(a);
511template<
typename T>
inline
512Vector3D<T>
operator/(
const Vector3D<T>& a,
const Vector3D<T>& b)
514 Vector3D<T> result(a);
524template<
typename T>
inline
525Vector3D<T>
operator+(
const Vector3D<T>& a,
typename Vector3D<T>::TParam b)
527 Vector3D<T> result(a);
537template<
typename T>
inline
538Vector3D<T>
operator-(
const Vector3D<T>& a,
typename Vector3D<T>::TParam b)
540 Vector3D<T> result(a);
550template<
typename T>
inline
551Vector3D<T>
operator*(
const Vector3D<T>& a,
typename Vector3D<T>::TParam b)
553 Vector3D<T> result(a);
563template<
typename T>
inline
564Vector3D<T>
operator/(
const Vector3D<T>& a,
typename Vector3D<T>::TParam b)
566 Vector3D<T> result(a);
576template<
typename T>
inline
577Vector3D<T>
operator+(
typename Vector3D<T>::TParam a,
const Vector3D<T>& b)
579 Vector3D<T> result(b);
589template<
typename T>
inline
590Vector3D<T>
operator-(
typename Vector3D<T>::TParam a,
const Vector3D<T>& b)
592 Vector3D<T> result(-b);
602template<
typename T>
inline
603Vector3D<T>
operator*(
typename Vector3D<T>::TParam a,
const Vector3D<T>& b)
605 Vector3D<T> result(b);
616inline Vector3D<T>
pointwiseMin(
const Vector3D<T>& a,
const Vector3D<T>& b)
618 return Vector3D<T>(std::min(a.x, b.x), std::min(a.y, b.y), std::min(a.z, b.z));
627inline Vector3D<T>
pointwiseMax(
const Vector3D<T>& a,
const Vector3D<T>& b)
629 return Vector3D<T>(std::max(a.x, b.x), std::max(a.y, b.y), std::max(a.z, b.z));
638inline Vector3D<T>
lerp(
const Vector3D<T>& a,
const Vector3D<T>& b,
typename Vector3D<T>::TParam t)
640 Vector3D<T> result = b;
651template<
typename T,
typename Char,
typename Traits>
652std::basic_ostream<Char, Traits>& operator<<(
653 std::basic_ostream<Char, Traits>& stream,
const Vector3D<T>& b)
655 LASS_ENFORCE_STREAM(stream) <<
"(" << b.x <<
", " << b.y <<
", " << b.z <<
")";
663template<
typename T,
typename Char,
typename Traits>
664std::basic_istream<Char, Traits>& operator>>(
665 std::basic_istream<Char, Traits>& stream, Vector3D<T>& b)
673 stream.clear(std::ios::failbit);
678 stream >> result.x >> c;
681 stream.clear(std::ios::failbit);
686 stream >> result.y >> c;
689 stream.clear(std::ios::failbit);
694 stream >> result.z >> c;
697 stream.clear(std::ios::failbit);
710io::XmlOStream& operator<<(io::XmlOStream& stream,
const Vector3D<T>& b)
712 LASS_ENFORCE_STREAM(stream)
713 <<
"<Vector3D>" << b.x <<
" " << b.y <<
" " << b.z <<
"</Vector3D>\n";
LineSegment3D< T, PP > project(const Plane3D< T, EP, NP > &plane, const LineSegment3D< T, PP > &lineSegment)
project a linesegment on a plane.
cyclic iterator over xyz indices
T sqr(const T &x)
return x * x
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
T abs(const T &x)
if x < 0 return -x, else return x.
set of geometrical primitives
Library for Assembled Shared Sources.
const Vector3D< T > project(const Vector3D< T > &other) const
Project vector on this one.
Vector3D< T > operator*(const Vector3D< T > &a, typename Vector3D< T >::TParam b)
muliply all components of a by b
Vector3D::TConstReference at(signed index) const
Wrap index around range.
Vector3D< T > operator+(typename Vector3D< T >::TParam a, const Vector3D< T > &b)
add a to all components of b
const TValue squaredNorm() const
Return squared norm of vector.
void normalize()
Normalize vector.
Vector3D< T > pointwiseMax(const Vector3D< T > &a, const Vector3D< T > &b)
return a vector with, for each coordinate, the maximum value of a and b
const Vector3D< T > reject(const Vector3D< T > &other) const
Reject vector from this one.
const Vector3D< T > transform(T(*op)(T)) const
apply a function to every component
Vector3D< T > operator-(const Vector3D< T > &a, const Vector3D< T > &b)
componentwise subtraction
Vector3D< T > operator+(const Vector3D< T > &a, const Vector3D< T > &b)
componentwise addition
Vector3D< T > operator*(typename Vector3D< T >::TParam a, const Vector3D< T > &b)
multiply all components of b with a
Vector3D< T > operator*(const Vector3D< T > &a, const Vector3D< T > &b)
Componentwise multiplication.
Vector3D< T > operator-(const Vector3D< T > &a, typename Vector3D< T >::TParam b)
subtract b of all components of a.
XYZ majorAxis() const
Return axis with the greatest absolute value.
const TValue norm() const
Return norm of vector.
Vector3D< T > & operator/=(const Vector3D< T > &other)
Componentwise division.
Vector3D< T > operator/(const Vector3D< T > &a, typename Vector3D< T >::TParam b)
divide all components of a by b
Vector3D< T > & operator-=(const Vector3D< T > &other)
componentwise subtraction
Vector3D< T >::TValue dot(const Vector3D< T > &a, const Vector3D< T > &b)
dot product.
static Vector3D< T > random(RandomGenerator &generator)
Random unit vector.
bool isNaN() const
Return true if at least one of the components is NaN.
Vector3D< T > pointwiseMin(const Vector3D< T > &a, const Vector3D< T > &b)
return a vector with, for each coordinate, the minimum value of a and b
Vector3D< T > & operator+=(const Vector3D< T > &other)
componentwise addition
bool isZero() const
Return true if all the components are (exactly!) zero.
Vector3D< T > operator-(typename Vector3D< T >::TParam a, const Vector3D< T > &b)
subtract a of all components of b
const Vector3D< T > & operator+() const
A weird way to get back the same object.
Vector3D< T > operator/(const Vector3D< T > &a, const Vector3D< T > &b)
Componentwise division.
Vector3D< T > cross(const Vector3D< T > &a, const Vector3D< T > &b)
cross product
const Vector3D< T > normal() const
return a unit vector with same direction/sense as this vector.
Vector3D< T > lerp(const Vector3D< T > &a, const Vector3D< T > &b, typename Vector3D< T >::TParam t)
interpolate linearly between two vectors: a + t * (b - a)
const Vector3D< T > reciprocal() const
return the reciprocal version of this vector
Vector3D< T > operator+(const Vector3D< T > &a, typename Vector3D< T >::TParam b)
add b to all components of a.
Vector3D< T > & operator*=(const Vector3D< T > &other)
Componentwise multiplication.