#ifndef __JVECTOR3D__ #define __JVECTOR3D__ #include #include "JIO/JSerialisable.hh" #include "JGeometry2D/JVector2D.hh" namespace JGEOMETRY3D { namespace { using JIO::JReader; using JIO::JWriter; using JGEOMETRY2D::JVector2D; } /** * Data structure for vector in three dimensions. */ class JVector3D : public JVector2D { public: /** * Default constructor. */ JVector3D() : JVector2D(), __z(0.0) {} /** * Constructor. * * \param x x value * \param y y value * \param z z value */ JVector3D(const double x, const double y, const double z) : JVector2D(x,y), __z(z) {} /** * Read JVector3D from input. * * \param in JReader * \param vector JVector3D * \return JReader */ friend inline JReader& operator>>(JReader& in, JVector3D& vector) { in >> static_cast(vector); in >> vector.__z; return in; } /** * Write JVector3D to output. * * \param out JWriter * \param vector JVector3D * \return JWriter */ friend inline JWriter& operator<<(JWriter& out, const JVector3D& vector) { out << static_cast(vector); out << vector.__z; return out; } /** * Get z position. * * \return z position */ double getZ() const { return __z; } /** * Add vector. * * \param vector * \return this vector */ JVector3D& add(const JVector3D& vector) { __x += vector.getX(); __y += vector.getY(); __z += vector.getZ(); return *this; } /** * Subtract vector. * * \param vector * \return this vector */ JVector3D& sub(const JVector3D& vector) { __x -= vector.getX(); __y -= vector.getY(); __z -= vector.getZ(); return *this; } /** * Scale vector. * * \param scale factor * \return this vector */ JVector3D& scale(const double factor) { __x *= factor; __y *= factor; __z *= factor; return *this; } /** * Negate vector. * * \return this vector */ JVector3D& negate() { __x = -__x; __y = -__y; __z = -__z; return *this; } /** * Add vector. * * \param vector * \return this vector */ JVector3D& operator+=(const JVector3D& vector) { __x += vector.getX(); __y += vector.getY(); __z += vector.getZ(); return *this; } /** * Subtract vector. * * \param vector * \return this vector */ JVector3D& operator-=(const JVector3D& vector) { __x -= vector.getX(); __y -= vector.getY(); __z -= vector.getZ(); return *this; } /** * Scale vector. * * \param scale factor * \return this vector */ JVector3D& operator*=(const double factor) { __x *= factor; __y *= factor; __z *= factor; return *this; } /** * Scale vector. * * \param scale factor * \return this vector */ JVector3D& operator/=(const double factor) { __x /= factor; __y /= factor; __z /= factor; return *this; } /** * Get length squared. * * \return square of length */ double getLengthSquared() const { return JVector2D::getLengthSquared() + getZ()*getZ(); } /** * Get length. * * \return length */ double getLength() const { return sqrt(getLengthSquared()); } protected: double __z; }; /** * Add operator. * * \param a JVector3D * \param b JVector3D * \result a + b */ inline JVector3D operator+(const JVector3D& a, const JVector3D& b) { return JVector3D(a).add(b); } /** * Subtract operator. * * \param a JVector3D * \param b JVector3D * \result a - b */ inline JVector3D operator-(const JVector3D& a, const JVector3D& b) { return JVector3D(a).sub(b); } /** * Multiply operator. * * \param a constant * \param b JVector3D * \result a * b */ inline JVector3D operator*(const double a, const JVector3D& b) { return JVector3D(b).scale(a); } /** * Divide operator. * * \param a JVector3D * \param b constant * \result a / b */ inline JVector3D operator/(const JVector3D& a, const double b) { return JVector3D(a).scale(1.0/b); } } #endif