PeriDyno 1.0.0
Loading...
Searching...
No Matches
Vector2D.inl
Go to the documentation of this file.
1#include <limits>
2#include <glm/gtx/norm.hpp>
3
4namespace dyno
5{
6
7 template <typename T>
8 DYN_FUNC Vector<T, 2>::Vector()
9 :Vector(0) //delegating ctor
10 {
11 }
12
13 template <typename T>
14 DYN_FUNC Vector<T, 2>::Vector(T x)
15 : Vector(x, x) //delegating ctor
16 {
17 }
18
19 template <typename T>
20 DYN_FUNC Vector<T, 2>::Vector(T x, T y)
21 : data_(x, y)
22 {
23 }
24
25 template <typename T>
26 DYN_FUNC Vector<T, 2>::Vector(const Vector<T, 2>& vec)
27 : data_(vec.data_)
28 {
29
30 }
31
32 template <typename T>
33 DYN_FUNC Vector<T, 2>::~Vector()
34 {
35
36 }
37
38 template <typename T>
39 DYN_FUNC T& Vector<T, 2>::operator[] (unsigned int idx)
40 {
41 return const_cast<T &>(static_cast<const Vector<T, 2> &>(*this)[idx]);
42 }
43
44 template <typename T>
45 DYN_FUNC const T& Vector<T, 2>::operator[] (unsigned int idx) const
46 {
47 return data_[idx];
48 }
49
50 template <typename T>
51 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator+ (const Vector<T, 2> &vec2) const
52 {
53 return Vector<T, 2>(*this) += vec2;
54 }
55
56 template <typename T>
57 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator+= (const Vector<T, 2> &vec2)
58 {
59 data_ += vec2.data_;
60 return *this;
61 }
62
63 template <typename T>
64 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator- (const Vector<T, 2> &vec2) const
65 {
66 return Vector<T, 2>(*this) -= vec2;
67 }
68
69 template <typename T>
70 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator-= (const Vector<T, 2> &vec2)
71 {
72 data_ -= vec2.data_;
73 return *this;
74 }
75
76 template <typename T>
77 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator*(const Vector<T, 2> &vec2) const
78 {
79 return Vector<T, 2>(data_[0] * vec2[0], data_[1] * vec2[1]);
80 }
81
82 template <typename T>
83 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator*=(const Vector<T, 2> &vec2)
84 {
85 data_[0] *= vec2.data_[0]; data_[1] *= vec2.data_[1];
86 return *this;
87 }
88
89 template <typename T>
90 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator/(const Vector<T, 2> &vec2) const
91 {
92 return Vector<T, 2>(data_[0] / vec2[0], data_[1] / vec2[1]);
93 }
94
95 template <typename T>
96 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator/=(const Vector<T, 2> &vec2)
97 {
98 data_[0] /= vec2.data_[0]; data_[1] /= vec2.data_[1];;
99 return *this;
100 }
101
102 template <typename T>
103 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator=(const Vector<T, 2> &vec2)
104 {
105 data_ = vec2.data_;
106 return *this;
107 }
108
109 template <typename T>
110 DYN_FUNC bool Vector<T, 2>::operator== (const Vector<T, 2> &vec2) const
111 {
112 return data_ == vec2.data_;
113 }
114
115 template <typename T>
116 DYN_FUNC bool Vector<T, 2>::operator!= (const Vector<T, 2> &vec2) const
117 {
118 return !((*this) == vec2);
119 }
120
121 template <typename T>
122 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator+(T value) const
123 {
124 return Vector<T, 2>(*this) += value;
125 }
126
127
128 template <typename T>
129 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator+= (T value)
130 {
131 data_ += value;
132 return *this;
133 }
134
135 template <typename T>
136 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator-(T value) const
137 {
138 return Vector<T, 2>(*this) -= value;
139 }
140
141 template <typename T>
142 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator-= (T value)
143 {
144 data_ -= value;
145 return *this;
146 }
147
148 template <typename T>
149 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator* (T scale) const
150 {
151 return Vector<T, 2>(*this) *= scale;
152 }
153
154 template <typename T>
155 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator*= (T scale)
156 {
157 data_ *= scale;
158 return *this;
159 }
160
161 template <typename T>
162 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator/ (T scale) const
163 {
164 return Vector<T, 2>(*this) /= scale;
165 }
166
167 template <typename T>
168 DYN_FUNC Vector<T, 2>& Vector<T, 2>::operator/= (T scale)
169 {
170 data_ /= scale;
171 return *this;
172 }
173
174 template <typename T>
175 DYN_FUNC const Vector<T, 2> Vector<T, 2>::operator-(void) const
176 {
177 Vector<T, 2> res;
178 res.data_ = -data_;
179 return res;
180 }
181
182 template <typename T>
183 DYN_FUNC T Vector<T, 2>::norm() const
184 {
185 return glm::length(data_);
186 }
187
188 template <typename T>
189 DYN_FUNC T Vector<T, 2>::normSquared() const
190 {
191 return glm::length2(data_);
192 }
193
194 template <typename T>
195 DYN_FUNC Vector<T, 2>& Vector<T, 2>::normalize()
196 {
197 data_ = glm::length(data_) > glm::epsilon<T>() ? glm::normalize(data_) : glm::tvec2<T>(0, 0);
198 return *this;
199 }
200
201 template <typename T>
202 DYN_FUNC T Vector<T, 2>::cross(const Vector<T, 2>& vec2) const
203 {
204 return (*this)[0] * vec2[1] - (*this)[1] * vec2[0];
205 }
206
207 template <typename T>
208 DYN_FUNC T Vector<T, 2>::dot(const Vector<T, 2>& vec2) const
209 {
210 return glm::dot(data_, vec2.data_);
211 }
212
213 template <typename T>
214 DYN_FUNC Vector<T, 2> Vector<T, 2>::minimum(const Vector<T, 2> & vec2) const
215 {
216 Vector<T, 2> res;
217 res[0] = data_[0] < vec2[0] ? data_[0] : vec2[0];
218 res[1] = data_[1] < vec2[1] ? data_[1] : vec2[1];
219 return res;
220 }
221
222 template <typename T>
223 DYN_FUNC Vector<T, 2> Vector<T, 2>::maximum(const Vector<T, 2> & vec2) const
224 {
225 Vector<T, 2> res;
226 res[0] = data_[0] > vec2[0] ? data_[0] : vec2[0];
227 res[1] = data_[1] > vec2[1] ? data_[1] : vec2[1];
228 return res;
229 }
230
231 //make * operator commutative
232 template <typename S, typename T>
233 DYN_FUNC const Vector<T, 2> operator *(S scale, const Vector<T, 2> &vec)
234 {
235 return vec * (T)scale;
236 }
237
238} //end of namespace dyno
DYN_FUNC Vector()
Definition VectorBase.h:26
#define T(t)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
TEMPLATE_TYPENAME_T MAT4_T scale(MAT4_T const &m, VEC3_T const &v)
Definition vgMath.h:506
vgm::Vec2 vec2
Definition vgMath.h:630