Skip to content

Commit

Permalink
Rename Vector3D::getMag() to mag(), document Vector3D
Browse files Browse the repository at this point in the history
  • Loading branch information
C7C8 committed Dec 17, 2015
1 parent 2d787f9 commit 264cc7a
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 17 deletions.
140 changes: 130 additions & 10 deletions lib/hydra/Vector3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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.
Expand All @@ -82,6 +127,10 @@ namespace Hydra

setMatrix(toMatrix() * zTransMat);
}

/**
* @brief Standard vector addition.
*/
Vector3D Vector3D::operator+(const Vector3D& vec) const
{
Vector3D newVec;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions lib/hydra/Vector3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/control/Path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 264cc7a

Please sign in to comment.