Templates -- Meow  1.1.4
A C++ template which is unable and also not allowed to compile to obj-file first.
Vectors.h
Go to the documentation of this file.
1 #ifndef geo_Vectors_H__
2 #define geo_Vectors_H__
3 
4 #include "../math/utility.h"
5 #include "../math/Vector.h"
6 #include "../math/Matrix.h"
7 
8 #include <cmath>
9 
10 namespace meow{
11 
17 template<class Scalar>
18 class Vector2D {
19 private:
20  Scalar x_, y_;
21 public:
23  Vector2D(): x_(0), y_(0) {
24  }
25 
27  Vector2D(Vector2D const& v): x_(v.x_), y_(v.y_) {
28  }
29 
31  Vector2D(Scalar const& s): x_(s), y_(s) {
32  }
33 
35  Vector2D(Scalar const& sx, Scalar const& sy): x_(sx), y_(sy) {
36  }
37 
39  Vector2D(Vector<Scalar> const& v): x_(v(0)), y_(v(1)) {
40  }
41 
43  Vector2D(Vector<Scalar> const& v, size_t i): x_(v(i)), y_(v(i + 1)) {
44  }
45 
48  }
49 
51  Vector2D& copyFrom(Vector2D const& v) {
52  return xy(v.x(), v.y());
53  }
54 
56  Scalar const& x() const {
57  return x_;
58  }
59 
61  Scalar& xGet() {
62  return x_;
63  }
64 
66  Scalar& yGet() {
67  return y_;
68  }
69 
71  Scalar const& y() const {
72  return y_;
73  }
74 
76  Scalar const& x(Scalar const& s) {
77  x_ = s;
78  return x();
79  }
80 
82  Scalar const& y(Scalar const& s) {
83  y_ = s;
84  return y();
85  }
86 
88  Vector2D& xy(Scalar const& sx, Scalar const& sy){
89  x(sx);
90  y(sy);
91  return *this;
92  }
93 
95  Vector2D positive() const {
96  return *this;
97  }
98 
100  Vector2D negative() const {
101  return Vector2D(-x(), -y());
102  }
103 
105  Vector2D right()const{
106  return Vector2D(-y(), x());
107  }
108 
110  Vector2D add(Vector2D const& v) const {
111  return Vector2D(x() + v.x(), y() + v.y());
112  }
113 
115  Vector2D& added(Vector2D const& v) {
116  return xy(x() + v.x(), y() + v.y());
117  }
118 
120  Vector2D sub(Vector2D const& v) const {
121  return Vector2D(x() - v.x(), y() - v.y());
122  }
123 
125  Vector2D& subed(Vector2D const& v) {
126  return xy(x() - v.x(), y() - v.y());
127  }
128 
130  Vector2D mul(Scalar const& s) const {
131  return Vector2D(x() * s, y() * s);
132  }
133 
135  Vector2D& muled(Scalar const& s) {
136  return xy(x() * s, y() * s);
137  }
138 
140  Vector2D div(Scalar const& s) const {
141  return Vector2D(x() / s, y() / s);
142  }
143 
145  Vector2D& dived(Scalar const& s) {
146  return xy(x() / s, y() / s);
147  }
148 
150  Scalar mul(Vector2D const& v) const {
151  return dot(v);
152  }
153 
155  Scalar dot(Vector2D const& v) const {
156  return x() * v.x() + y() * v.y();
157  }
158 
160  Scalar cross(Vector2D const& v) const {
161  return x() * v.y() - y() * v.x();
162  }
163 
165  Scalar length() const {
166  return Scalar(sqrt(double(length2())));
167  }
168 
170  Scalar length2() const {
171  return dot(*this);
172  }
173 
175  Vector2D normalize() const {
176  return div(length());
177  }
178 
181  return dived(length());
182  }
183 
185  Vector2D rotate(Scalar const& theta) const {
186  Scalar cs(cos(-double(theta)));
187  Scalar sn(sin(-double(theta)));
188  Vector2D<Scalar> new_x(cs, sn);
189  return Vector2D(new_x.dot(*this), new_x.cross(*this));
190  }
191 
193  Vector2D& rotated(Scalar const& theta) {
194  return copyFrom(rotate(theta));
195  }
196 
198  Vector2D reflect(Vector2D const& v) const {
199  return v.mul(v.dot(*this) * 2 / v.length2()).sub(*this);
200  }
201 
204  return copyFrom(reflecte(v));
205  }
206 
209  static Matrix<Scalar> ret(2, 1, Scalar(0));
210  ret(0, 0, x());
211  ret(1, 0, y());
212  return ret;
213  }
214 
216  Matrix<Scalar> matrix(Scalar const& homo) const {
217  static Matrix<Scalar> ret(3, 1, Scalar(0));
218  ret(0, 0, x());
219  ret(1, 0, y());
220  ret(2, 0, homo);
221  return ret;
222  }
223 
224  Scalar const& operator()(size_t n) const {
225  return (n == 0 ? x() : y());
226  }
227 
228  Vector2D& operator()(Scalar const& sx, Scalar const& sy) {
229  return xy(sx, sy);
230  }
231 
232  Vector2D operator+() const { return positive(); }
233  Vector2D operator-() const { return negative(); }
234  Vector2D operator~() const { return right (); }
235 
236  Vector2D operator+(Vector2D const& v) const { return add(v); }
237  Vector2D operator-(Vector2D const& v) const { return sub(v); }
238  Vector2D operator*(Scalar const& s) const { return mul(s); }
239  Vector2D operator/(Scalar const& s) const { return div(s); }
240  Scalar operator*(Vector2D const& v) const { return mul(v); }
241 
242  Vector2D& operator=(Vector2D const& v) { return copyFrom(v); }
243  Vector2D& operator+=(Vector2D const& v) { return added(v); }
244  Vector2D& operator-=(Vector2D const& v) { return subed(v); }
245  Vector2D& operator*=(Scalar const& s) { return muled(s); }
246  Vector2D& operator/=(Scalar const& s) { return dived(s); }
247 };
248 
254 template<class Scalar>
255 class Vector3D{
256 private:
257  Scalar x_, y_, z_;
258 public:
260  Vector3D(): x_(0), y_(0), z_(0) {
261  }
262 
264  Vector3D(Vector3D const& v): x_(v.x_), y_(v.y_), z_(v.z_) {
265  }
266 
268  Vector3D(Scalar const& s): x_(s), y_(s), z_(s) {
269  }
270 
272  Vector3D(Scalar const& sx,
273  Scalar const& sy,
274  Scalar const& sz): x_(sx), y_(sy), z_(sz) {
275  }
276 
278  Vector3D(Vector<Scalar> const& v): x_(v(0)), y_(v(1)), z_(v(2)) {
279  }
280 
282  Vector3D(Vector<Scalar> const& v, size_t i): x_(v(i)), y_(v(i+1)), z_(v(i+2)){
283  }
284 
287  }
288 
291  return xyz(v.x(), v.y(), v.z());
292  }
293 
295  Scalar const& x() const{
296  return x_;
297  }
298 
300  Scalar const& y() const{
301  return y_;
302  }
303 
305  Scalar const& z() const{
306  return z_;
307  }
308 
310  Scalar& xGet() {
311  return x_;
312  }
313 
315  Scalar& yGet() {
316  return y_;
317  }
318 
320  Scalar& zGet() {
321  return z_;
322  }
323 
325  Scalar const& x(Scalar const& s) {
326  x_ = s;
327  return x();
328  }
329 
331  Scalar const& y(Scalar const& s) {
332  y_ = s;
333  return y();
334  }
335 
337  Scalar const& z(Scalar const& s) {
338  z_ = s;
339  return z();
340  }
341 
343  Vector3D& xyz(Scalar const& sx, Scalar const& sy, Scalar const& sz) {
344  x(sx);
345  y(sy);
346  z(sz);
347  return *this;
348  }
349 
351  Vector3D positive() const {
352  return *this;
353  }
354 
356  Vector3D negative() const {
357  return Vector3D(-x(), -y(), -z());
358  }
359 
361  Vector3D add(Vector3D const& v) const {
362  return Vector3D(x() + v.x(), y() + v.y(), z() + v.z());
363  }
364 
366  Vector3D& added(Vector3D const& v) {
367  return xyz(x() + v.x(), y() + v.y(), z() + v.z());
368  }
369 
371  Vector3D sub(Vector3D const& v) const {
372  return Vector3D(x() - v.x(), y() - v.y(), z() - v.z());
373  }
374 
376  Vector3D& subed(Vector3D const& v) {
377  return xyz(x() - v.x(), y() - v.y(), z() - v.z());
378  }
379 
381  Vector3D mul(Scalar const& s) const {
382  return Vector3D(x() * s, y() * s, z() * s);
383  }
384 
386  Vector3D& muled(Scalar const& s) {
387  return xyz(x() * s, y() * s, z() * s);
388  }
389 
391  Vector3D div(Scalar const& s) const {
392  return Vector3D(x() / s, y() / s, z() / s);
393  }
394 
396  Vector3D& dived(Scalar const& s) {
397  return xyz(x() / s, y() / s, z() / s);
398  }
399 
401  Scalar mul(Vector3D const& v) const {
402  return dot(v);
403  }
404 
406  Scalar dot(Vector3D const& v) const {
407  return x() * v.x() + y() * v.y() + z() * v.z();
408  }
409 
411  Vector3D cross(Vector3D const& v) const {
412  return Vector3D(y() * v.z() - z() * v.y(),
413  z() * v.x() - x() * v.z(),
414  x() * v.y() - y() * v.x());
415  }
416 
418  Vector3D& crossed(Vector3D const& v) {
419  return copyFrom(cross(v));
420  }
421 
423  Scalar length() const {
424  return Scalar(sqrt(double(length2())));
425  }
426 
428  Scalar length2() const {
429  return dot(*this);
430  }
431 
433  Vector3D normalize() const {
434  return div(length());
435  }
436 
439  return dived(length());
440  }
441 
443  Vector3D rotate(Vector3D const& axis, double theta) const {
444  Vector3D a(axis.normalize());
445  Vector3D xx(sub(a) .mul(cos(theta)));
446  Vector3D yy(a.cross(*this).mul(sin(theta)));
447  return a.mul(a.dot(*this)).add(xx).add(yy);
448  }
449 
451  Vector3D& rotated(Vector3D const& axis, double theta) {
452  return copyFrom(rotate(axis, theta));
453  }
454 
456  Vector3D reflect(Vector3D const& v) const {
457  return v.mul(v.dot(*this) * 2 / v.length2()).sub(*this);
458  }
459 
462  return copyFrom(reflecte(v));
463  }
464 
467  static Matrix<Scalar> ret(3, 1, Scalar(0));
468  ret(0, 0, x());
469  ret(1, 0, y());
470  ret(2, 0, z());
471  return ret;
472  }
473 
475  Matrix<Scalar> matrix(Scalar const& homo) const {
476  static Matrix<Scalar> ret(4, 1, Scalar(0));
477  ret(0, 0, x());
478  ret(1, 0, y());
479  ret(2, 0, z());
480  ret(3, 0, homo);
481  return ret;
482  }
483 
484  Scalar const& operator()(size_t n) const {
485  return (n == 0 ? x() : (n == 1 ? y() : z()));
486  }
487 
488  Vector3D& operator()(Scalar const& sx, Scalar const& sy, Scalar const& sz) {
489  return xyz(sx, sy, sz);
490  }
491 
492  Vector3D operator+() const { return positive(); }
493  Vector3D operator-() const { return negative(); }
494 
495  Vector3D operator+(Vector3D const& v) const { return add(v); }
496  Vector3D operator-(Vector3D const& v) const { return sub(v); }
497  Vector3D operator*(Scalar const& s) const { return mul(s); }
498  Vector3D operator/(Scalar const& s) const { return div(s); }
499  Scalar operator*(Vector3D const& v) const { return mul(v); }
500 
501  Vector3D& operator=(Vector3D const& v) { return copyFrom(v); }
502  Vector3D& operator+=(Vector3D const& v) { return added(v); }
503  Vector3D& operator-=(Vector3D const& v) { return subed(v); }
504  Vector3D& operator*=(Scalar const& s) { return muled(s); }
505  Vector3D& operator/=(Scalar const& s) { return dived(s); }
506 };
507 
508 }
509 
510 #endif // geo_Vectors_H__
Matrix< Scalar > matrix(Scalar const &homo) const
return a 3x1 matrix form of itself
Definition: Vectors.h:475
~Vector2D()
destructor
Definition: Vectors.h:47
Vector3D & normalized()
normalize itself
Definition: Vectors.h:438
Vector3D & subed(Vector3D const &v)
Let itself substract v.
Definition: Vectors.h:376
Vector3D & operator-=(Vector3D const &v)
Definition: Vectors.h:503
Scalar const & z(Scalar const &s)
modify z
Definition: Vectors.h:337
Scalar & xGet()
access x with non constant reference
Definition: Vectors.h:310
Vector2D & operator+=(Vector2D const &v)
Definition: Vectors.h:243
Vector3D(Vector< Scalar > const &v)
constructor (from another Vector)
Definition: Vectors.h:278
Scalar const & y(Scalar const &s)
modify y
Definition: Vectors.h:82
Scalar & zGet()
access z with non constant reference
Definition: Vectors.h:320
Scalar const & x(Scalar const &s)
modify x
Definition: Vectors.h:325
Vector3D & crossed(Vector3D const &v)
crossed
Definition: Vectors.h:418
Vector2D reflect(Vector2D const &v) const
return reflect from given vector v
Definition: Vectors.h:198
Scalar const & x() const
access x
Definition: Vectors.h:56
Vector2D(Scalar const &s)
constructor (s, s)
Definition: Vectors.h:31
Scalar & xGet()
access x with non constant reference
Definition: Vectors.h:61
Scalar const & z() const
access z
Definition: Vectors.h:305
Vector2D rotate(Scalar const &theta) const
return rotate theta degree of itself
Definition: Vectors.h:185
Vector2D div(Scalar const &s) const
return (*this)/s , where s is a scalar
Definition: Vectors.h:140
Matrix< Scalar > matrix(Scalar const &homo) const
return a 3x1 matrix form of itself
Definition: Vectors.h:216
Vector2D & muled(Scalar const &s)
Let itself mulitple s.
Definition: Vectors.h:135
Vector3D rotate(Vector3D const &axis, double theta) const
return rotate theta degree by axis of itself
Definition: Vectors.h:443
Vector2D right() const
return count-clockwise rotate 90 degree of itself
Definition: Vectors.h:105
Vector2D negative() const
return -(*this)
Definition: Vectors.h:100
Vector2D positive() const
return +(*this)
Definition: Vectors.h:95
Vector3D & operator=(Vector3D const &v)
Definition: Vectors.h:501
Vector2D & dived(Scalar const &s)
Let itself divide s.
Definition: Vectors.h:145
Scalar operator*(Vector2D const &v) const
Definition: Vectors.h:240
Vector3D operator-(Vector3D const &v) const
Definition: Vectors.h:496
Vector3D & copyFrom(Vector3D const &v)
copy
Definition: Vectors.h:290
Scalar & yGet()
access y with non constant reference
Definition: Vectors.h:66
Vector3D div(Scalar const &s) const
return (*this)/s , where s is a scalar
Definition: Vectors.h:391
Vector3D(Scalar const &s)
constructor (s, s)
Definition: Vectors.h:268
Vector3D & xyz(Scalar const &sx, Scalar const &sy, Scalar const &sz)
modify x and y
Definition: Vectors.h:343
Vector3D & added(Vector3D const &v)
Let itself add v.
Definition: Vectors.h:366
Vector2D operator*(Scalar const &s) const
Definition: Vectors.h:238
Vector3D & dived(Scalar const &s)
Let itself divide s.
Definition: Vectors.h:396
Vector3D cross(Vector3D const &v) const
cross
Definition: Vectors.h:411
Scalar mul(Vector3D const &v) const
same as dot(v)
Definition: Vectors.h:401
Scalar operator*(Vector3D const &v) const
Definition: Vectors.h:499
Vector2D & operator-=(Vector2D const &v)
Definition: Vectors.h:244
Vector2D & operator*=(Scalar const &s)
Definition: Vectors.h:245
Scalar mul(Vector2D const &v) const
same as dot(v)
Definition: Vectors.h:150
Vector3D mul(Scalar const &s) const
return (*this)*s , where s is a scalar
Definition: Vectors.h:381
Scalar const & operator()(size_t n) const
Definition: Vectors.h:484
Vector2D operator~() const
Definition: Vectors.h:234
Vector2D operator-(Vector2D const &v) const
Definition: Vectors.h:237
Scalar const & y() const
access y
Definition: Vectors.h:300
Vector2D add(Vector2D const &v) const
return (*this)+v
Definition: Vectors.h:110
Scalar const & operator()(size_t n) const
Definition: Vectors.h:224
Vector3D sub(Vector3D const &v) const
return (*this)-v
Definition: Vectors.h:371
vector
Definition: Vector.h:19
Vector2D & operator()(Scalar const &sx, Scalar const &sy)
Definition: Vectors.h:228
Vector3D & operator/=(Scalar const &s)
Definition: Vectors.h:505
Matrix< Scalar > matrix() const
return a 3x1 matrix form of itself
Definition: Vectors.h:466
Vector2D & xy(Scalar const &sx, Scalar const &sy)
modify x and y
Definition: Vectors.h:88
3D's vector
Definition: Vectors.h:255
Vector3D(Scalar const &sx, Scalar const &sy, Scalar const &sz)
constructor (sx, sy)
Definition: Vectors.h:272
Vector2D mul(Scalar const &s) const
return (*this)*s , where s is a scalar
Definition: Vectors.h:130
Vector2D & rotated(Scalar const &theta)
Let itself rotate theta degree.
Definition: Vectors.h:193
Vector3D operator/(Scalar const &s) const
Definition: Vectors.h:498
Vector2D operator/(Scalar const &s) const
Definition: Vectors.h:239
Vector3D negative() const
return -(*this)
Definition: Vectors.h:356
Vector3D operator+() const
Definition: Vectors.h:492
Vector2D operator+(Vector2D const &v) const
Definition: Vectors.h:236
Vector2D(Vector< Scalar > const &v)
constructor (from another Vector)
Definition: Vectors.h:39
Scalar dot(Vector3D const &v) const
dot
Definition: Vectors.h:406
Vector3D add(Vector3D const &v) const
return (*this)+v
Definition: Vectors.h:361
Scalar & yGet()
access y with non constant reference
Definition: Vectors.h:315
Vector2D operator+() const
Definition: Vectors.h:232
Scalar length2() const
same as dot(*this)
Definition: Vectors.h:170
Vector3D()
consturctor (0, 0)
Definition: Vectors.h:260
Vector3D operator+(Vector3D const &v) const
Definition: Vectors.h:495
Vector2D operator-() const
Definition: Vectors.h:233
Scalar cross(Vector2D const &v) const
cross
Definition: Vectors.h:160
Vector2D & copyFrom(Vector2D const &v)
copy
Definition: Vectors.h:51
Vector2D & operator=(Vector2D const &v)
Definition: Vectors.h:242
Vector2D sub(Vector2D const &v) const
return (*this)-v
Definition: Vectors.h:120
Vector3D positive() const
return +(*this)
Definition: Vectors.h:351
Vector2D & added(Vector2D const &v)
Let itself add v.
Definition: Vectors.h:115
Vector3D & operator*=(Scalar const &s)
Definition: Vectors.h:504
Vector3D(Vector3D const &v)
consturctor (from another Vector3D)
Definition: Vectors.h:264
Scalar length2() const
same as dot(*this)
Definition: Vectors.h:428
Scalar const & x() const
access x
Definition: Vectors.h:295
Vector2D & subed(Vector2D const &v)
Let itself substract v.
Definition: Vectors.h:125
~Vector3D()
destructor
Definition: Vectors.h:286
Vector3D & operator+=(Vector3D const &v)
Definition: Vectors.h:502
Scalar length() const
sqrt of length2
Definition: Vectors.h:423
Vector2D & operator/=(Scalar const &s)
Definition: Vectors.h:246
Vector3D normalize() const
return normalize form of itself
Definition: Vectors.h:433
Scalar dot(Vector2D const &v) const
dot
Definition: Vectors.h:155
Vector2D(Vector2D const &v)
consturctor (from another Vector2D)
Definition: Vectors.h:27
Vector3D operator-() const
Definition: Vectors.h:493
Vector3D & rotated(Vector3D const &axis, double theta)
Let itself rotate theta degree.
Definition: Vectors.h:451
Vector2D()
consturctor (0, 0)
Definition: Vectors.h:23
Vector3D & reflected(Vector3D const &v)
reflect itself given vector v
Definition: Vectors.h:461
Vector2D & reflected(Vector2D const &v)
reflect itself given vector v
Definition: Vectors.h:203
Scalar const & y() const
access y
Definition: Vectors.h:71
Vector3D & muled(Scalar const &s)
Let itself mulitple s.
Definition: Vectors.h:386
Vector2D & normalized()
normalize itself
Definition: Vectors.h:180
Matrix< Scalar > matrix() const
return a 2x1 matrix form of itself
Definition: Vectors.h:208
Vector3D operator*(Scalar const &s) const
Definition: Vectors.h:497
Vector2D normalize() const
return normalize form of itself
Definition: Vectors.h:175
Vector3D & operator()(Scalar const &sx, Scalar const &sy, Scalar const &sz)
Definition: Vectors.h:488
Vector2D(Scalar const &sx, Scalar const &sy)
constructor (sx, sy)
Definition: Vectors.h:35
Scalar length() const
sqrt of length2
Definition: Vectors.h:165
Vector3D reflect(Vector3D const &v) const
return reflect from given vector v
Definition: Vectors.h:456
2D's vector
Definition: Vectors.h:18
Scalar const & y(Scalar const &s)
modify y
Definition: Vectors.h:331
Vector2D(Vector< Scalar > const &v, size_t i)
constructor (from another Vector, i-th)
Definition: Vectors.h:43
Vector3D(Vector< Scalar > const &v, size_t i)
constructor (from another Vector, i-th)
Definition: Vectors.h:282
Scalar const & x(Scalar const &s)
modify x
Definition: Vectors.h:76