45#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_2D_INL
46#define LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_2D_INL
62template<
typename T>
inline
63Vector2D<T>::Vector2D():
71template<
typename T>
inline
72Vector2D<T>::Vector2D(TParam x, TParam y):
82Vector2D<T>::Vector2D(
const Vector2D<U>& other):
83 x(static_cast<TValue>(other.x)),
84 y(static_cast<TValue>(other.y))
92Vector2D<T>::Vector2D(
const U& x,
const U& y):
93 x(static_cast<TValue>(x)),
94 y(static_cast<TValue>(y))
100template<
typename T>
inline
101typename Vector2D<T>::TConstReference Vector2D<T>::operator[](
size_t index)
const
103 LASS_ASSERT(index < dimension);
104 return *(&x + index);
109template<
typename T>
inline
110typename Vector2D<T>::TReference Vector2D<T>::operator[](
size_t index)
112 LASS_ASSERT(index < dimension);
113 return *(&x + index);
120template<
typename T>
inline
123 return *(&x + num::mod(index, dimension));
130template<
typename T>
inline
133 return *(&x + num::mod(index, dimension));
140template<
typename T>
inline
148template<
typename T>
inline
158template<
typename T>
inline
170template<
typename T>
inline
182template<
typename T>
inline
194template<
typename T>
inline
206template<
typename T>
inline
218template<
typename T>
inline
230template<
typename T>
inline
242template<
typename T>
inline
254template<
typename T>
inline
257 return x == TNumTraits::zero && y == TNumTraits::zero;
264template<
typename T>
inline
267 return num::isNaN(x) || num::isNaN(y);
274template<
typename T>
inline
277 return dot(*
this, *
this);
284template<
typename T>
inline
301 Vector2D<T> result(*
this);
313 Vector2D<T> result(TNumTraits::one, TNumTraits::one);
322template<
typename T>
inline
325 return Vector2D<T>(-y, x);
335 Vector2D<T> result(*
this);
336 result *=
dot(other, *
this);
345template<
typename T>
inline
353template<
typename T>
inline
356 return 2 *
project(other) - other;
366 return Vector2D<T>(op(x), op(y));
373template<
typename T>
inline
384template <
class RandomGenerator>
387 std::uniform_real_distribution<T> distribution(0, 2 * TNumTraits::pi);
388 const TValue theta = distribution(generator);
389 return Vector2D<T>(num::cos(theta), num::sin(theta));
397template<
typename T>
inline
398typename Vector2D<T>::TValue
dot(
const Vector2D<T>& a,
const Vector2D<T>& b)
400 return a.x * b.x + a.y * b.y;
408template<
typename T>
inline
409typename Vector2D<T>::TValue
cos(
const Vector2D<T>& a,
const Vector2D<T>& b)
428template<
typename T>
inline
429typename Vector2D<T>::TValue
perpDot(
const Vector2D<T>& a,
const Vector2D<T>& b)
431 return a.x * b.y - a.y * b.x;
438template<
typename T>
inline
441 return a.x == b.x && a.y == b.y;
448template<
typename T>
inline
449bool operator!=(
const Vector2D<T>& a,
const Vector2D<T>& b)
459template<
typename T>
inline
460Vector2D<T>
operator+(
const Vector2D<T>& a,
const Vector2D<T>& b)
462 Vector2D<T> result(a);
472template<
typename T>
inline
473Vector2D<T>
operator-(
const Vector2D<T>& a,
const Vector2D<T>& b)
475 Vector2D<T> result(a);
485template<
typename T>
inline
486Vector2D<T>
operator*(
const Vector2D<T>& a,
const Vector2D<T>& b)
488 Vector2D<T> result(a);
498template<
typename T>
inline
499Vector2D<T>
operator/(
const Vector2D<T>& a,
const Vector2D<T>& b)
501 Vector2D<T> result(a);
511template<
typename T>
inline
512Vector2D<T>
operator+(
const Vector2D<T>& a,
typename Vector2D<T>::TParam b)
514 Vector2D<T> result(a);
524template<
typename T>
inline
525Vector2D<T>
operator-(
const Vector2D<T>& a,
typename Vector2D<T>::TParam b)
527 Vector2D<T> result(a);
537template<
typename T>
inline
538Vector2D<T>
operator*(
const Vector2D<T>& a,
typename Vector2D<T>::TParam b)
540 Vector2D<T> result(a);
550template<
typename T>
inline
551Vector2D<T>
operator/(
const Vector2D<T>& a,
typename Vector2D<T>::TParam b)
553 Vector2D<T> result(a);
563template<
typename T>
inline
564Vector2D<T>
operator+(
typename Vector2D<T>::TParam a,
const Vector2D<T>& b)
566 Vector2D<T> result(b);
576template<
typename T>
inline
577Vector2D<T>
operator-(
typename Vector2D<T>::TParam a,
const Vector2D<T>& b)
579 Vector2D<T> result(-b);
589template<
typename T>
inline
590Vector2D<T>
operator*(
typename Vector2D<T>::TParam a,
const Vector2D<T>& b)
592 Vector2D<T> result(b);
603inline Vector2D<T>
pointwiseMin(
const Vector2D<T>& a,
const Vector2D<T>& b)
605 return Vector2D<T>(std::min(a.x, b.x), std::min(a.y, b.y));
614inline Vector2D<T>
pointwiseMax(
const Vector2D<T>& a,
const Vector2D<T>& b)
616 return Vector2D<T>(std::max(a.x, b.x), std::max(a.y, b.y));
625inline Vector2D<T>
lerp(
const Vector2D<T>& a,
const Vector2D<T>& b,
typename Vector2D<T>::TParam t)
627 Vector2D<T> result = b;
638template<
typename T,
typename Char,
typename Traits>
639std::basic_ostream<Char, Traits>& operator<<(
640 std::basic_ostream<Char, Traits>& stream,
const Vector2D<T>& b)
642 LASS_ENFORCE_STREAM(stream) <<
"(" << b.x <<
", " << b.y <<
")";
651template<
typename T,
typename Char,
typename Traits>
652std::basic_istream<Char, Traits>& operator>>(
653 std::basic_istream<Char, Traits>& stream, Vector2D<T>& b)
661 stream.clear(std::ios::failbit);
666 stream >> result.x >> c;
670 stream.clear(std::ios::failbit);
675 stream >> result.y >> c;
678 stream.clear(std::ios::failbit);
691io::XmlOStream& operator<<(io::XmlOStream& stream,
const Vector2D<T>& b)
693 LASS_ENFORCE_STREAM(stream)
694 <<
"<Vector2D>" << b.x <<
" " << b.y <<
"</Vector2D>\n";
LineSegment3D< T, PP > project(const Plane3D< T, EP, NP > &plane, const LineSegment3D< T, PP > &lineSegment)
project a linesegment on a plane.
T norm(const T &x)
return norm of x as if x is real part of complex number: sqr(x)
set of geometrical primitives
Library for Assembled Shared Sources.
const Vector2D< T > perp() const
return the vector perpendicular to this one, 90° CCW (to the left).
const Vector2D< T > & operator+() const
A weird way to get back the same object.
void normalize()
Normalize vector.
Vector2D< T > operator*(const Vector2D< T > &a, const Vector2D< T > &b)
Componentwise multiplication.
Vector2D< T >::TValue cos(const Vector2D< T > &a, const Vector2D< T > &b)
returns cosine of angle between both vectors.
bool isNaN() const
Return true if at least one of the components is NaN.
Vector2D< T > & operator/=(const Vector2D< T > &other)
Componentwise division.
Vector2D< T >::TValue dot(const Vector2D< T > &a, const Vector2D< T > &b)
dot product.
Vector2D< T > operator/(const Vector2D< T > &a, typename Vector2D< T >::TParam b)
divide all components of a by b
Vector2D< T > operator*(const Vector2D< T > &a, typename Vector2D< T >::TParam b)
muliply all components of a by b
const Vector2D< T > reject(const Vector2D< T > &other) const
Project vector on this one.
Vector2D< T > lerp(const Vector2D< T > &a, const Vector2D< T > &b, typename Vector2D< T >::TParam t)
interpolate linearly between two vectors: a + t * (b - a)
Vector2D::TConstReference at(signed index) const
Wrap index around range.
Vector2D< T > pointwiseMin(const Vector2D< T > &a, const Vector2D< T > &b)
return a vector with, for each coordinate, the minimum value of a and b
const TValue norm() const
Return norm of vector.
Vector2D< T > operator-(const Vector2D< T > &a, const Vector2D< T > &b)
componentwise subtraction
Vector2D< T > operator+(typename Vector2D< T >::TParam a, const Vector2D< T > &b)
add a to all components of b
const Vector2D< T > normal() const
return a unit vector with same direction/sense as this vector.
bool isZero() const
Return true if all the components are (exactly!) zero.
Vector2D< T > operator+(const Vector2D< T > &a, typename Vector2D< T >::TParam b)
add b to all components of a.
const Vector2D< T > transform(T(*iOperator)(T)) const
apply a function to every component
Vector2D< T > operator-(const Vector2D< T > &a, typename Vector2D< T >::TParam b)
subtract b of all components of a.
Vector2D< T >::TValue perpDot(const Vector2D< T > &a, const Vector2D< T > &b)
perp dot product (cross product for 2D vectors).
static Vector2D< T > random(RandomGenerator &generator)
Random unit vector.
Vector2D< T > operator*(typename Vector2D< T >::TParam a, const Vector2D< T > &b)
multiply all components of b with a
const Vector2D< T > reciprocal() const
return the reciprocal version of this vector
Vector2D< T > operator/(const Vector2D< T > &a, const Vector2D< T > &b)
Componentwise division.
const TValue squaredNorm() const
Return squared norm of vector.
Vector2D< T > & operator-=(const Vector2D< T > &other)
componentwise subtraction
Vector2D< T > operator-(typename Vector2D< T >::TParam a, const Vector2D< T > &b)
subtract a of all components of b
Vector2D< T > operator+(const Vector2D< T > &a, const Vector2D< T > &b)
componentwise addition
const Vector2D< T > project(const Vector2D< T > &other) const
Project vector on this one.
Vector2D< T > & operator+=(const Vector2D< T > &other)
componentwise addition
Vector2D< T > & operator*=(const Vector2D< T > &other)
Componentwise multiplication.
Vector2D< T > pointwiseMax(const Vector2D< T > &a, const Vector2D< T > &b)
return a vector with, for each coordinate, the maximum value of a and b