#ifndef __JSEGMENT3D__ #define __JSEGMENT3D__ #include #include #include "JIO/JSerialisable.hh" #include "JGeometry3D/JPosition3D.hh" /** * \author mdejong */ namespace JGEOMETRY3D {} namespace JPP { using namespace JGEOMETRY3D; } namespace JGEOMETRY3D { using JIO::JReader; using JIO::JWriter; /** * Type definition of line segment in two dimensions. */ typedef std::pair JSegment3D_t; /** * Line segment in two dimensions. */ class JSegment3D : public JSegment3D_t { public: /** * Default constructor. */ JSegment3D() : JSegment3D_t() {} /** * Constructor. * * \param A start position * \param B end position */ JSegment3D(const JVector3D& A, const JVector3D& B) : JSegment3D_t(A,B) {} /** * Get length squared. * * \return square of length */ double getLengthSquared() const { return JVector3D(this->second - this->first).getLengthSquared(); } /** * Get length. * * \return length */ double getLength() const { return sqrt(getLengthSquared()); } /** * Get squared of distance to point. * * \param point point * \param precision precision * \return square of distance */ double getDistanceSquared(const JVector3D& point, const double precision = 1.0e-8) const { JVector3D D(this->second - this->first); const double gp = D.getLengthSquared(); if (gp > precision) { const JVector3D U(point - this->first); double u = D.getDot(U); if (u < 0.0) u = 0.0; else if (u > gp) u = 1.0; else u /= gp; D.mul(u); D.sub(U); return D.getLengthSquared(); } else { return this->first.getDistanceSquared(point); } } /** * Get distance to point. * * \param point point * \return distance */ double getDistance(const JVector3D& point) const { return sqrt(getDistanceSquared(point)); } /** * Get dot product. * * \param segment segment * \return dot product */ double getDot(const JSegment3D& segment) const { return JVector3D(this->second - this->first).getDot(segment.second - segment.first); } /** * Read segment from input. * * \param in reader * \param segment segment * \return reader */ friend inline JReader& operator>>(JReader& in, JSegment3D& segment) { in >> segment.first; in >> segment.second; return in; } /** * Write segment to output. * * \param out writer * \param segment segment * \return writer */ friend inline JWriter& operator<<(JWriter& out, const JSegment3D& segment) { out << segment.first; out << segment.second; return out; } }; } #endif