From 264cc7a73029b36bb4cf9f41ce7bf9c4a78331c3 Mon Sep 17 00:00:00 2001 From: Sourec Date: Thu, 17 Dec 2015 07:18:59 -0500 Subject: [PATCH] Rename Vector3D::getMag() to mag(), document Vector3D --- lib/hydra/Vector3D.cpp | 140 ++++++++++++++++++++++++++++++++++++++--- lib/hydra/Vector3D.h | 12 ++-- src/control/Path.cpp | 2 +- 3 files changed, 137 insertions(+), 17 deletions(-) diff --git a/lib/hydra/Vector3D.cpp b/lib/hydra/Vector3D.cpp index 26836e1..953e8e6 100644 --- a/lib/hydra/Vector3D.cpp +++ b/lib/hydra/Vector3D.cpp @@ -5,40 +5,75 @@ using namespace std; namespace Hydra { + /** + * @brief Constructor. Sets y and z to 0, and x to 1. + */ Vector3D::Vector3D() { xComp = 1.f; yComp = 0.f; zComp = 0.f; } + + /** + * @brief Constructor. Lets you set initial component values. + * @param newX X component + * @param newY Y component + * @param newZ Z component + */ Vector3D::Vector3D(double newX, double newY, double newZ) { xComp = newX; yComp = newY; zComp = newZ; } + + /** + * @brief Normalizes the vector, preserving the direction but setting magnitude to 1. + */ void Vector3D::normalize() { - double mag = getMag(); - xComp /= mag; - yComp /= mag; - zComp /= mag; + double m = mag(); + xComp /= m; + yComp /= m; + zComp /= m; } - double Vector3D::getMag() const + + /** + * @brief Returns the magnitude of the vector. + * @return Magnitude + */ + double Vector3D::mag() const { return sqrtf((xComp * xComp) + (yComp * yComp) + (zComp * zComp)); } + + /** + * @brief Sets the magnitude of the vector. + * @param newMag New magnitude. + */ void Vector3D::setMag(double newMag) { - double ratio = newMag / getMag(); + double ratio = newMag / mag(); xComp *= ratio; yComp *= ratio; zComp *= ratio; } + + /** + * @brief Gets the angle between two vectors. + * @param vec The second vector. + * @return Angle in radians. + */ double Vector3D::getADelt(Vector3D vec) const { - return acos((*this * vec) / (getMag() * vec.getMag())); + return acos((*this * vec) / (mag() * vec.mag())); } + + /** + * @brief Rotates the vector about the X axis. + * @param rad Angle of rotation in radians. + */ void Vector3D::rotateX(double rad) { double sine = sin(rad); @@ -53,6 +88,11 @@ namespace Hydra setMatrix(toMatrix() * xTransMat); //Multiply matrices. } + + /** + * @brief Rotates the vector about the Y axis. + * @param rad Angle of rotation in radians. + */ void Vector3D::rotateY(double rad) { double sine = sin(rad); @@ -67,6 +107,11 @@ namespace Hydra setMatrix(toMatrix() * yTransMat); } + + /** + * @brief Rotates the vector about the Z axis. + * @param rad Angle of rotation in radians. + */ void Vector3D::rotateZ(double rad) { rad *= -1; //Not sure why this is needed, but it works and makes it work properly. @@ -82,6 +127,10 @@ namespace Hydra setMatrix(toMatrix() * zTransMat); } + + /** + * @brief Standard vector addition. + */ Vector3D Vector3D::operator+(const Vector3D& vec) const { Vector3D newVec; @@ -90,6 +139,10 @@ namespace Hydra newVec.zComp = zComp + vec.zComp; return newVec; } + + /** + * @brief Standard vector subtraction + */ Vector3D Vector3D::operator-(const Vector3D& vec) const { Vector3D newVec; @@ -98,10 +151,20 @@ namespace Hydra newVec.zComp = zComp - vec.zComp; return newVec; } + + /** + * @brief Vector dot product. + * @return Dot product result. + */ double Vector3D::operator*(const Vector3D& vec) const { return (xComp * vec.xComp) + (yComp * vec.yComp) + (zComp * vec.zComp); } + + /** + * @brief Vector cross product. + * @return Resulting vector of vector cross product. + */ Vector3D Vector3D::operator%(const Vector3D& vec) const { Vector3D newVec; @@ -110,42 +173,93 @@ namespace Hydra newVec.zComp = (xComp * vec.yComp) - (yComp * vec.xComp); return newVec; } + + /** + * @brief Sets X component. + * @param newX New X component + */ void Vector3D::setX(double newX) { xComp = newX; } + + /** + * @brief Sets Y component. + * @param newY New Y component. + */ void Vector3D::setY(double newY) { yComp = newY; } + + /** + * @brief Sets new Z component. + * @param newZ New Z component. + */ void Vector3D::setZ(double newZ) { zComp = newZ; } + + /** + * @brief Returns the X component + * @return X component + */ double Vector3D::getX() const { return xComp; } + + /** + * @brief Returns the Y component. + * @return Y component + */ double Vector3D::getY() const { return yComp; } + + /** + * @brief Retusn the Z component. + * @return Z component + */ double Vector3D::getZ() const { return zComp; } + + /** + * @brief Returns the angle to the X axis + * @return Angle in radians + */ double Vector3D::getAngleX() const { - return acosf(xComp / getMag()); + return acosf(xComp / mag()); } + + /** + * @brief Returns the angle to the Y axis. + * @return Angle in radians + */ double Vector3D::getAngleY() const { - return acosf(yComp / getMag()); + return acosf(yComp / mag()); } + + /** + * @brief Returns the angle to the Z axis. + * @return Angle in radians + */ double Vector3D::getAngleZ() const { - return acosf(zComp / getMag()); + return acosf(zComp / mag()); } + + /** + * @brief Converts this vector to a 3x1 matrix. + * @return A 3x1 matrix of the vector components in order x,y,z + * @todo Flip matrix to 1x3 (XxY) + */ Matrix Vector3D::toMatrix() const { Matrix mat(3, 1); //Should work. @@ -154,6 +268,12 @@ namespace Hydra mat.setValue(zComp, 2, 0); return mat; } + + /** + * @brief Sets the components of this vector based on a 3x1 matrix + * @param mat A 3x1 matrix of vector components in order x,y,z + * @todo Flip matrix to 1x3 (XxY) + */ void Vector3D::setMatrix(Matrix mat) { if (mat.getXSize() != 3 || mat.getYSize() != 1) diff --git a/lib/hydra/Vector3D.h b/lib/hydra/Vector3D.h index b50a954..bedddcd 100644 --- a/lib/hydra/Vector3D.h +++ b/lib/hydra/Vector3D.h @@ -16,18 +16,18 @@ namespace Hydra Vector3D(double newX, double newY, double newZ); void normalize(); - double getMag() const; + double mag() const; void setMag(double newMag); - double getADelt(Vector3D vec) const; //!< Returns the angle between two vectors, in radians (?) + double getADelt(Vector3D vec) const; void rotateX(double rad); void rotateY(double rad); void rotateZ(double rad); - Vector3D operator+(const Vector3D& vec) const; //!< Standard addition - Vector3D operator-(const Vector3D& vec) const; //!< Standard subtraction - double operator*(const Vector3D& vec) const; //!< Dot product - Vector3D operator%(const Vector3D& vec) const; //!< Cross product + Vector3D operator+(const Vector3D& vec) const; + Vector3D operator-(const Vector3D& vec) const; + double operator*(const Vector3D& vec) const; + Vector3D operator%(const Vector3D& vec) const; void setX(double newX); void setY(double newY); diff --git a/src/control/Path.cpp b/src/control/Path.cpp index b37803e..167e72e 100644 --- a/src/control/Path.cpp +++ b/src/control/Path.cpp @@ -74,7 +74,7 @@ namespace ADBLib dir.setZ(startPos[rpsDir::Z] - rps->getPosition(rpsDir::Z)); } - if (dir.getMag() <= tolerance) + if (dir.mag() <= tolerance) { currentWP++; return false; //Distance within tolerance; advance waypoints.