aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/geo/Vector3D.h
blob: 08b5705b6a9c10cebf8bef751832dd5b2e30c68f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef   geo_Vector3D_H__
#define   geo_Vector3D_H__

#include <cmath>
#include "../utility.h"

namespace meow{
  template<class Scalar>
  class Vector3D{
    private:
      Scalar _x, _y, _z, _w;
    public:
      //
      Vector3D(): _x(0), _y(0), _z(0), _w(0){ }
      Vector3D(Scalar const& x,
               Scalar const& y,
               Scalar const& z)  : _x(   x), _y(   y), _z(   z), _w(   0){ }
      Vector3D(Scalar const& x,
               Scalar const& y,
               Scalar const& z,
               Scalar const& w)  : _x(   x), _y(   y), _z(   z), _w(   w){ }
      Vector3D(Vector3D const& v): _x(v._x), _y(v._y), _z(v._z), _w(v._w){ }
      //
      Scalar const& x() const{ return _x; }
      Scalar const& y() const{ return _y; }
      Scalar const& z() const{ return _z; }
      Scalar const& w() const{ return _w; }
      Scalar      & x(Scalar const& s){ _x = s; return _x; }
      Scalar      & y(Scalar const& s){ _y = s; return _y; }
      Scalar      & z(Scalar const& s){ _z = s; return _z; }
      Scalar      & w(Scalar const& s){ _w = s; return _w; }
      Vector3D& operator()(Scalar const& x, Scalar const& y, Scalar const& z                 ){ _x = x; _y = y; _z = z;         return *this; }
      Vector3D& operator()(Scalar const& x, Scalar const& y, Scalar const& z, Scalar const& w){ _x = x; _y = y; _z = z; _w = w; return *this; }
      Scalar      & operator[](size_t n)      { return (n == 0 ? _x : (n == 1 ? _y : (n == 2 ? _z : _w))); }
      Scalar const& operator[](size_t n) const{ return (n == 0 ? _x : (n == 1 ? _y : (n == 2 ? _z : _w))); }
      //
      Vector3D operator-() const{ return Vector3D(-_x, -_y, -_z, _w); }
      //
      Vector3D operator+(Vector3D const& v) const{ return Vector3D(_x + v._x, _y + v._y, _z + v._z, _w); }
      Vector3D operator-(Vector3D const& v) const{ return Vector3D(_x - v._x, _y - v._y, _z - v._z, _w); }
      Vector3D operator*(Scalar   const& s) const{ return Vector3D(_x *    s, _y *    s, _z *    s, _w); }
      Vector3D operator/(Scalar   const& s) const{ return Vector3D(_x /    s, _y /    s, _z /    s, _w); }
      //
      Vector3D& operator+=(Vector3D const& v){ _x += v._x; _y += v._y; _z += v._z; return *this; }
      Vector3D& operator-=(Vector3D const& v){ _x -= v._x; _y -= v._y; _z -= v._z; return *this; }
      Vector3D& operator*=(Scalar   const& s){ _x *=    s; _y *=    s; _z *=    s; return *this; }
      Vector3D& operator/=(Scalar   const& s){ _x /=    s; _y /=    s; _z /=    s; return *this; }
      //
      Scalar dot  (Vector3D const& v) const{ return _x * v._x + _y * v._y + _z * v._z; }
      Scalar cross(Vector3D const& v) const{ return Vector3D(_y * v._z - _z * v._y,
                                                             _z * v._x - _x * v._z,
                                                             _x * v._y - _y - v._x,
                                                             _w); }
      Scalar length () const{ return sqrt(squ(_x) + squ(_y) + squ(_z)); }
      Scalar length3() const{ return      squ(_x) + squ(_y) + squ(_z) ; }
      //
      Vector3D normal() const{
        Scalar len(length());
        return Vector3D(_x / len, _y / len, _z / len, _w);
      }
      Vector3D rotation(Vector3D const& axis, Scalar theta) const{
        Vector3D a(axis.normal());
        Vector3D h(a * a.dot(*this));
        Vector3D x((*this) - a);
        Vector3D y(a.cross(*this));
        return h + x * cos(theta) + y * sin(theta);
      }
      Vector3D reflection(Vector3D const& v) const{
        return v * v.dot(*this) * 2 / v.length2() - *this;
      }
  };
}

#endif // geo_Vector3D_H__