aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/geo/Vector3D.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/geo/Vector3D.h')
-rw-r--r--meowpp/geo/Vector3D.h74
1 files changed, 74 insertions, 0 deletions
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__