#ifndef __JDIRECTION2D__ #define __JDIRECTION2D__ #include #include #include "JIO/JSerialisable.hh" #include "JLang/JManip.hh" #include "JGeometry2D/JVersor2D.hh" #include "JGeometry2D/JAngle2D.hh" #include "JGeometry2D/JVector2D.hh" #include "JGeometry2D/JRotation2D.hh" /** * \author mdejong */ namespace JGEOMETRY2D {} namespace JPP { using namespace JGEOMETRY2D; } namespace JGEOMETRY2D { using JIO::JReader; using JIO::JWriter; /** * Data structure for direction in two dimensions. */ class JDirection2D : public JVersor2D { public: using JVersor2D::getDot; using JVersor2D::getPerpDot; /** * Default constructor. */ JDirection2D() : JVersor2D() {} /** * Constructor. * * \param dir direction */ JDirection2D(const JVersor2D& dir) : JVersor2D(dir.getDY(), dir.getDX()) {} /** * Constructor. * * \param angle angle */ JDirection2D(const JAngle2D& angle) : JVersor2D(angle.getDX(), angle.getDY()) {} /** * Constructor. * * \param pos position */ JDirection2D(const JVector2D& pos) : JVersor2D(pos.getY(),pos.getX()) {} /** * Constructor. * * \param dx dx value * \param dy dy value */ JDirection2D(const double dx, const double dy) : JVersor2D(dx, dy) {} /** * Get direction. * * \return direction */ const JDirection2D& getDirection() const { return static_cast(*this); } /** * Get direction. * * \return direction */ JDirection2D& getDirection() { return static_cast(*this); } /** * Set direction. * * \param dir direction */ void setDirection(const JDirection2D& dir) { static_cast(*this) = dir; } /** * Type conversion operator. * * \return angle */ operator JAngle2D() const { return JAngle2D(getDX(), getDY()); } /** * Type conversion operator. * * \return position */ operator JVector2D() const { return JVector2D(getDX(), getDY()); } /** * Transform. * * \param T matrix * \return this direction */ JDirection2D& transform(const JMatrix2D& T) { T.transform(__dx, __dy); normalise(); return *this; } /** * Rotate. * * \param R rotation matrix * \return this direction */ JDirection2D& rotate(const JRotation2D& R) { R.rotate(__dx, __dy); normalise(); return *this; } /** * Rotate back. * * \param R rotation matrix * \return this direction */ JDirection2D& rotate_back(const JRotation2D& R) { R.rotate_back(__dx, __dy); normalise(); return *this; } /** * Get dot product. * * \param angle angle * \return dot product */ double getDot(const JAngle2D& angle) const { return getDX() * angle.getDX() + getDY() * angle.getDY(); } /** * Get dot product. * * \param pos position * \return dot product */ double getDot(const JVector2D& pos) const { return getDX() * pos.getX() + getDY() * pos.getY(); } /** * Get perpendicular dot product. * * \param angle angle * \return perpendicular dot product */ double getPerpDot(const JAngle2D& angle) const { return getDX() * angle.getDY() - getDY() * angle.getDX(); } /** * Get perpendicular dot product. * * \param pos position * \return perpendicular dot product */ double getPerpDot(const JVector2D& pos) const { return getDX() * pos.getY() - getDY() * pos.getX(); } /** * Read direction from input. * * \param in input stream * \param direction direction * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JDirection2D& direction) { in >> direction.__dx >> direction.__dy; direction.normalise(); return in; } /** * Write direction to output. * * \param out output stream * \param direction direction * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JDirection2D& direction) { const JFormat format(out, getFormat(JFormat_t(9, 6, std::ios::fixed | std::ios::showpos))); out << format << direction.getDX() << ' ' << format << direction.getDY(); return out; } /** * Read direction from input. * * \param in reader * \param direction direction * \return reader */ friend inline JReader& operator>>(JReader& in, JDirection2D& direction) { in >> direction.__dx; in >> direction.__dy; return in; } /** * Write direction to output. * * \param out writer * \param direction direction * \return writer */ friend inline JWriter& operator<<(JWriter& out, const JDirection2D& direction) { out << direction.__dx; out << direction.__dy; return out; } }; } #endif