Library of Assembled Shared Sources
vector_2d.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45/** @struct lass::prim::Vector2D
46 * @brief 2D Vector
47 * @author BdG
48 * @date 2003
49 */
50
51
52
53#ifndef LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_2D_H
54#define LASS_GUARDIAN_OF_INCLUSION_PRIM_VECTOR_2D_H
55
56
57
58
59#include "prim_common.h"
60#include "../io/xml_o_stream.h"
61
62
63
64namespace lass
65{
66
67namespace prim
68{
69
70template<typename T>
71struct LASS_SIMD_ALIGN Vector2D
72{
73 typedef Vector2D<T> TSelf;
74
75 typedef typename util::CallTraits<T>::TValue TValue;
76 typedef typename util::CallTraits<T>::TParam TParam;
77 typedef typename util::CallTraits<T>::TReference TReference;
78 typedef typename util::CallTraits<T>::TConstReference TConstReference;
79 typedef typename num::NumTraits<T> TNumTraits;
80
81 enum { dimension = 2 }; /**< number of dimensions of vector */
82
83 template <typename U> struct Rebind
84 {
85 typedef Vector2D<U> Type;
86 };
87
88
89 // public data
90
91 TValue x;
92 TValue y;
93
94
95 // methods
96
97 Vector2D();
98 Vector2D(TParam x, TParam y);
99 template <typename U> explicit Vector2D(const Vector2D<U>& other);
100 template <typename U> Vector2D(const U& x, const U& y);
101
102 typename Vector2D::TConstReference operator[](size_t index) const;
103 typename Vector2D::TReference operator[](size_t index);
104 typename Vector2D::TConstReference at(signed index) const;
105 typename Vector2D::TReference at(signed index);
106
107 const Vector2D<T>& operator+() const;
108 const Vector2D<T> operator-() const;
109 Vector2D<T>& operator+=(const Vector2D<T>& other);
110 Vector2D<T>& operator-=(const Vector2D<T>& other);
111 Vector2D<T>& operator*=(const Vector2D<T>& other);
112 Vector2D<T>& operator/=(const Vector2D<T>& other);
113 Vector2D<T>& operator+=(TParam other);
114 Vector2D<T>& operator-=(TParam other);
115 Vector2D<T>& operator*=(TParam other);
116 Vector2D<T>& operator/=(TParam other);
117
118 bool isZero() const;
119 bool isNaN() const;
120 const TValue squaredNorm() const;
121 const TValue norm() const;
122 const Vector2D<T> normal() const;
123 const Vector2D<T> reciprocal() const;
124 const Vector2D<T> perp() const;
125 const Vector2D<T> project(const Vector2D<T>& other) const;
126 const Vector2D<T> reject(const Vector2D<T>& other) const;
127 const Vector2D<T> reflect(const Vector2D<T>& other) const;
128 const Vector2D<T> transform(T (*iOperator)(T)) const;
129
130 void normalize();
131
132 template <class RandomGenerator> static Vector2D<T> random(RandomGenerator& generator);
133};
134
135template<typename T> typename Vector2D<T>::TValue dot(const Vector2D<T>& a, const Vector2D<T>& b);
136template<typename T> typename Vector2D<T>::TValue perpDot(const Vector2D<T>& a, const Vector2D<T>& b);
137template<typename T> typename Vector2D<T>::TValue cos(const Vector2D<T>& a, const Vector2D<T>& b);
138
139template<typename T> bool operator==(const Vector2D<T>& a, const Vector2D<T>& b);
140template<typename T> bool operator!=(const Vector2D<T>& a, const Vector2D<T>& b);
141
142template<typename T> Vector2D<T> operator+(const Vector2D<T>& a, const Vector2D<T>& b);
143template<typename T> Vector2D<T> operator-(const Vector2D<T>& a, const Vector2D<T>& b);
144template<typename T> Vector2D<T> operator*(const Vector2D<T>& a, const Vector2D<T>& b);
145template<typename T> Vector2D<T> operator/(const Vector2D<T>& a, const Vector2D<T>& b);
146
147template<typename T> Vector2D<T> operator+(const Vector2D<T>& a, typename Vector2D<T>::TParam b);
148template<typename T> Vector2D<T> operator-(const Vector2D<T>& a, typename Vector2D<T>::TParam b);
149template<typename T> Vector2D<T> operator*(const Vector2D<T>& a, typename Vector2D<T>::TParam b);
150template<typename T> Vector2D<T> operator/(const Vector2D<T>& a, typename Vector2D<T>::TParam b);
151
152template<typename T> Vector2D<T> operator+(typename Vector2D<T>::TParam a, const Vector2D<T>& b);
153template<typename T> Vector2D<T> operator-(typename Vector2D<T>::TParam a, const Vector2D<T>& b);
154template<typename T> Vector2D<T> operator*(typename Vector2D<T>::TParam a, const Vector2D<T>& b);
155// we don't allow scalar / vector. you can do scalar * vector.reciprocal() for that.
156
157template<typename T> Vector2D<T> pointwiseMin(const Vector2D<T>& a, const Vector2D<T>& b);
158template<typename T> Vector2D<T> pointwiseMax(const Vector2D<T>& a, const Vector2D<T>& b);
159
160template <typename T> Vector2D<T> lerp(typename Vector2D<T>::TParam a, const Vector2D<T>& b, typename Vector2D<T>::TParam t);
161
162template<typename T, typename Char, typename Traits>
163std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& stream, const Vector2D<T>& b);
164template<typename T, typename Char, typename Traits>
165std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& ioIStream, Vector2D<T>& b);
166
167template<typename T> io::XmlOStream& operator<<(io::XmlOStream& stream, const Vector2D<T>& b);
168
169
170
171}
172
173}
174
175#include "vector_2d.inl"
176
177#define LASS_PRIM_HAVE_PY_EXPORT_TRAITS_VECTOR_2D
178#ifdef LASS_GUARDIAN_OF_INCLUSION_UTIL_PYOBJECT_PLUS_H
180#endif
181
182#endif
Output stream for writing a selection of geometric primitives to XML files.
#define LASS_SIMD_ALIGN
if LASS_SIMD_ALIGNMENT is set, use LASS_SIMD_ALIGN to align some structures on SIMD alignment boundar...
set of geometrical primitives
Definition aabb_2d.h:81
Library for Assembled Shared Sources.
Definition config.h:53
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.
Vector2D< T > & operator-=(TParam other)
subtract other of each component of this.
void normalize()
Normalize vector.
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 > & operator+=(TParam other)
add other to each component of this.
Vector2D< T > & operator*=(TParam other)
multiply each component of this with other.
const Vector2D< T > reject(const Vector2D< T > &other) const
Project vector on this one.
Vector2D::TConstReference at(signed index) const
Wrap index around range.
const TValue norm() const
Return norm of vector.
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.
const Vector2D< T > transform(T(*iOperator)(T)) const
apply a function to every component
static Vector2D< T > random(RandomGenerator &generator)
Random unit vector.
const Vector2D< T > reciprocal() const
return the reciprocal version of this vector
Vector2D< T > & operator/=(TParam other)
divide each component of this by other.
const TValue squaredNorm() const
Return squared norm of vector.
Vector2D< T > & operator-=(const Vector2D< T > &other)
componentwise subtraction
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::TReference at(signed index)
Wrap index around range.