diff options
Diffstat (limited to 'meowpp')
-rw-r--r-- | meowpp/geo/Vector2D.h | 67 | ||||
-rw-r--r-- | meowpp/geo/Vector3D.h | 74 |
2 files changed, 141 insertions, 0 deletions
diff --git a/meowpp/geo/Vector2D.h b/meowpp/geo/Vector2D.h new file mode 100644 index 0000000..0b18138 --- /dev/null +++ b/meowpp/geo/Vector2D.h @@ -0,0 +1,67 @@ +#ifndef Vector2D_H__ +#define Vector2D_H__ + +#include <cmath> +#include "../utility.h" + +namespace meow{ + + template<class Scalar> + class Vector2D{ + private: + Scalar _x, _y, _w; + public: + // + Vector2D(): _x(0), _y(0), _w(0){ } + Vector2D(Scalar const& x, + Scalar const& y) : _x( x), _y( y), _w( 0){ } + Vector2D(Scalar const& x, + Scalar const& y, + Scalar const& w) : _x( x), _y( y), _w( w){ } + Vector2D(Vector2D const& v): _x(v._x), _y(v._y), _w(v._w){ } + // + Scalar const& x() const{ return _x; } + Scalar const& y() const{ return _y; } + 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 & w(Scalar const& s){ _w = s; return _w; } + Vector2D& operator()(Scalar const& x, Scalar const& y) { _x = x; _y = y; return *this; } + Vector2D& operator()(Scalar const& x, Scalar const& y, Scalar const& w){ _x = x; _y = y; _w = w; return *this; } + Scalar& operator[](size_t n) { return (n == 0 ? _x : (n == 1 ? _y : _w)); } + Scalar const& operator[](size_t n) const{ return (n == 0 ? _x : (n == 1 ? _y : _w)); } + // + Vector2D operator-() const{ return Vector2D(-_x, -_y, _w); } + Vector2D operator~() const{ return Vector2D(-_y, _x, _w); } + // + Vector2D operator+(Vector2D const& v) const{ return Vector2D(_x + v._x, _y + v._y, _w); } + Vector2D operator-(Vector2D const& v) const{ return Vector2D(_x - v._x, _y - v._y, _w); } + Vector2D operator*(Scalar const& s) const{ return Vector2D(_x * s, _y * s, _w); } + Vector2D operator/(Scalar const& s) const{ return Vector2D(_x / s, _y / s, _w); } + // + Vector2D& operator+=(Vector2D const& v){ _x += v._x; _y += v._y; return *this; } + Vector2D& operator-=(Vector2D const& v){ _x -= v._x; _y -= v._y; return *this; } + Vector2D& operator*=(Scalar const& s){ _x *= s; _y *= s; return *this; } + Vector2D& operator/=(Scalar const& s){ _x /= s; _y /= s; return *this; } + // + Scalar dot (Vector2D const& v) const{ return _x * v._x + _y * v._y; } + Scalar cross(Vector2D const& v) const{ return _x * v._y - _y * v._x; } + Scalar length () const{ return sqrt(squ(_x) + squ(_y)); } + Scalar length2() const{ return squ(_x) + squ(_y) ; } + // + Vector2D normal() const{ + Scalar len(length()); + return Vector2D(_x / len, _y / len, _w); + } + Vector2D rotation(Scalar theta) const{ + Vector2D i(cos(-theta), sin(-theta)); + return Vector2D(i.dot(*this), i.cross(*this), _w); + } + Vector2D reflection(Vector2D const& v) const{ + return v * v.dot(*this) * 2 / v.length2() - *this; + } + }; + +} + +#endif // Vector2D_H__ diff --git a/meowpp/geo/Vector3D.h b/meowpp/geo/Vector3D.h new file mode 100644 index 0000000..f7a669a --- /dev/null +++ b/meowpp/geo/Vector3D.h @@ -0,0 +1,74 @@ +#ifndef Vector3D_H__ +#define 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 // Vector3D_H__ |