#ifndef __JPOSITION2D__ #define __JPOSITION2D__ #include #include #include "JIO/JSerialisable.hh" #include "JLang/JManip.hh" #include "JGeometry2D/JVector2D.hh" #include "JGeometry2D/JAngle2D.hh" #include "JGeometry2D/JVersor2D.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 position in two dimensions. */ class JPosition2D : public JVector2D { public: using JVector2D::getDot; using JVector2D::getPerpDot; using JVector2D::transform; /** * Default constructor. */ JPosition2D() : JVector2D() {} /** * Constructor. * * \param pos position */ JPosition2D(const JVector2D& pos) : JVector2D(pos) {} /** * Constructor. * * \param angle angle */ JPosition2D(const JAngle2D& angle) : JVector2D(angle.getDX(), angle.getDY()) {} /** * Constructor. * * \param dir direction */ JPosition2D(const JVersor2D& dir) : JVector2D(dir.getDX(), dir.getDY()) {} /** * Constructor. * * \param x x value * \param y y value */ JPosition2D(const double x, const double y) : JVector2D(x,y) {} /** * Get position. * * \return position */ const JPosition2D& getPosition() const { return static_cast(*this); } /** * Get position. * * \return position */ JPosition2D& getPosition() { return static_cast(*this); } /** * Set position. * * \param pos position */ void setPosition(const JVector2D& pos) { static_cast(*this) = pos; } /** * Type conversion operator. * * \return angle */ operator JAngle2D() const { return JAngle2D(getX(), getY()); } /** * Type conversion operator. * * \return direction */ operator JVersor2D() const { return JVersor2D(getX(), getY()); } /** * Rotate. * * \param R rotation matrix * \return this position */ JPosition2D& rotate(const JRotation2D& R) { R.rotate(__x, __y); return *this; } /** * Rotate back. * * \param R rotation matrix * \return this position */ JPosition2D& rotate_back(const JRotation2D& R) { R.rotate_back(__x, __y); return *this; } /** * Get dot product. * * \param angle angle * \return dot product */ double getDot(const JAngle2D& angle) const { return getX() * angle.getDX() + getY() * angle.getDY(); } /** * Get dot product. * * \param versor versor * \return dot product */ double getDot(const JVersor2D& versor) const { return getX() * versor.getDX() + getY() * versor.getDY(); } /** * Get perpendicular dot product. * * \param angle angle * \return perpendicular dot product */ double getPerpDot(const JAngle2D& angle) const { return getX() * angle.getDY() - getY() * angle.getDX(); } /** * Get perpendicular dot product. * * \param dir direction * \return perpendicular dot product */ double getPerpDot(const JVersor2D& dir) const { return getX() * dir.getDY() - getY() * dir.getDX(); } /** * Read position from input. * * \param in input stream * \param position position * \return input stream */ friend inline std::istream& operator>>(std::istream& in, JPosition2D& position) { in >> position.__x >> position.__y; return in; } /** * Write position to output. * * \param out output stream * \param position position * \return output stream */ friend inline std::ostream& operator<<(std::ostream& out, const JPosition2D& position) { const JFormat format(out, getFormat(JFormat_t(9, 3, std::ios::fixed | std::ios::showpos))); out << format << position.getX() << ' ' << format << position.getY(); return out; } /** * Read position from input. * * \param in reader * \param position position * \return reader */ friend inline JReader& operator>>(JReader& in, JPosition2D& position) { in >> position.__x; in >> position.__y; return in; } /** * Write position to output. * * \param out writer * \param position position * \return writer */ friend inline JWriter& operator<<(JWriter& out, const JPosition2D& position) { out << position.__x; out << position.__y; return out; } }; } #endif