#include "MLVector.h" //#include "MLLine.h" const MLVector MLVector::invalid = MLVector(0, 0, 0, false); const MLVector MLVector::nullVector = MLVector(0, 0, 0, true); MLVector::MLVector(void) :m_x(0.0), m_y(0.0), m_z(0.0), m_valid(true) { } MLVector::MLVector( double vx, double vy, double vz /*= 0.0*/, bool valid_in /*= true*/ ) : m_x(vx), m_y(vy), m_z(vz) { valid = valid_in; } MLVector::~MLVector(void) { } void MLVector::set(double vx, double vy, double vz) { x = vx; y = vy; z = vz; valid = true; } /** * Sets a new position for the vector in polar coordinates. * * \param radius the radius or the distance * * \param angle the angle in rad */ void MLVector::setPolar(double radius, double angle) { x = radius * cos(angle); y = radius * sin(angle); z = 0.0; //valid = MLMath::isNormal(radius) && MLMath::isNormal(angle); } bool MLVector::isValid() const { return valid; } bool MLVector::isInside( const MLBox& b ) const { /* MLVector bMin = b.getMinimum(); MLVector bMax = b.getMaximum(); return (x >= bMin.x && x <= bMax.x && y >= bMin.y && y <= bMax.y && z >= bMin.z && z <= bMax.z); */ return false; } void MLVector::setX(double x) { m_x = x; } double MLVector::getX() const { return m_x; } double& MLVector::getX() { return m_x; } void MLVector::setY(double y) { m_y = y; } double MLVector::getY() const { return m_y; } double& MLVector::getY() { return m_y; } void MLVector::setZ(double z) { m_z = z; } double MLVector::getZ() const { return m_z; } double& MLVector::getZ() { return m_z; } void MLVector::setValid(bool valid) { m_valid = valid; } bool MLVector::getValid() const { return m_valid; } /** * \return True if this vector and the given vector are almost equal * (see RS::PointTolerance). * * \param tol Tolerance in X, Y and Z. bool MLVector::equalsFuzzy(const MLVector& v, double tol) const { return (fabs(x-v.x)= minX && x <= maxX && y >= minY && y <= maxY); } void MLVector::setAngle(double a) { double m = getMagnitude(); setPolar(m, a); } /** * \return The angle from zero to this vector (in rad). */ double MLVector::getAngle() const { double ret = 0.0; double m = getMagnitude2d(); if (m > 1.0e-6) { double dp = getDotProduct(*this, MLVector(1.0, 0.0)); if (dp / m >= 1.0) { ret = 0.0; } else if (dp / m < -1.0) { ret = M_PI; } else { ret = acos(dp / m); } if (y < 0.0) { ret = 2*M_PI - ret; } } return ret; } /** * \return Angle between this vector and XY plane (horizontal plane). */ double MLVector::getAngleToPlaneXY() const { MLVector n(0, 0, 1); if (getMagnitude() < 1.0e-4) { return M_PI / 2; } else if ((getDotProduct(*this, n) / (getMagnitude() * 1)) > 1.0) { return 0.0; } else { return M_PI / 2 - acos(getDotProduct(*this, n) / (getMagnitude() * 1)); } } /** * \return The angle from this and the given coordinate (in rad). */ double MLVector::getAngleTo(const MLVector& v) const { if (!valid || !v.valid) { return RNANDOUBLE; } else { return (v - (*this)).getAngle(); } } /** * Sets the vector magnitude without chaning the direction. */ void MLVector::setMagnitude2d(double m) { double a = getAngle(); setPolar(m, a); } /** * \return Magnitude (length) of the vector. */ double MLVector::getMagnitude() const { if (!valid) { return RNANDOUBLE; } // Note that the z coordinate is also needed for 2d // (due to definition of crossP()) return 0;// sqrt(MLMath::pow(x, 2) + MLMath::pow(y, 2) + MLMath::pow(z, 2)); } /** * \return Magnitude (length) of the vector projected to the x/y plane (2d). */ double MLVector::getMagnitude2d() const { if (!valid) { return RNANDOUBLE; } return 0;// sqrt(MLMath::pow(x, 2) + MLMath::pow(y, 2)); } /** * \return Square of magnitude (length). */ double MLVector::getSquaredMagnitude() const { if (!valid) { return RNANDOUBLE; } return 0;// MLMath::pow(x, 2) + MLMath::pow(y, 2) + MLMath::pow(z, 2); } /** * Linear interpolation between this and \c v by fraction 't'. */ MLVector MLVector::getLerp(const MLVector& v, double t) const { return MLVector(x + (v.x - x) * t, y + (v.y - y) * t, z + (v.z - z) * t); } /** * \return Unit vector for this vector. */ MLVector MLVector::getUnitVector() const { return *this / getMagnitude(); } /** * binary + operator. */ MLVector MLVector::operator +(const MLVector& v) const { return MLVector(x + v.x, y + v.y, z + v.z, valid && v.valid); } /** * binary - operator. */ MLVector MLVector::operator -(const MLVector& v) const { return MLVector(x - v.x, y - v.y, z - v.z, valid && v.valid); } /** * binary * operator. */ MLVector MLVector::operator *(double s) const { return MLVector(x * s, y * s, z * s, valid); } /** * binary / operator. */ MLVector MLVector::operator /(double s) const { return MLVector(x / s, y / s, z / s, valid); } /** * unary - operator. */ MLVector MLVector::operator -() const { return getNegated(); } /** * \return New vector with negated components. */ MLVector MLVector::getNegated() const { return MLVector(-x, -y, -z, valid); } /** * Normalizes this vector and returns a reference to this vector. */ MLVector MLVector::normalize() { *this = getNormalized(); return *this; } /** * \return A new unit vector with the same direction as this vector. */ MLVector MLVector::getNormalized() const { /* double l = getMagnitude(); if (l