[idlescreen] r211 committed - Finished Vector2D tests and debugging.

1 view
Skip to first unread message

idles...@googlecode.com

unread,
Feb 13, 2010, 6:18:33 PM2/13/10
to dev-idl...@googlegroups.com
Revision: 211
Author: jeff.backus
Date: Sat Feb 13 15:17:14 2010
Log: Finished Vector2D tests and debugging.

http://code.google.com/p/idlescreen/source/detail?r=211

Modified:
/branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp
/branches/backus_dev/src/common/utility/Point2D.cpp
/branches/backus_dev/src/common/utility/Vector2D.cpp
/branches/backus_dev/src/common/utility/Vector2D.h
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
/branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h

=======================================
--- /branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp Sun Feb
7 18:40:30 2010
+++ /branches/backus_dev/src/common/unit_tests/unit_tests_main.cpp Sat Feb
13 15:17:14 2010
@@ -1,3 +1,6 @@
+#include <iostream>
+using namespace std;
+
#include <QtTest/QtTest>
#include <QList>
#include "utility/unit_tests/Test_MiscFuncs.h"
@@ -31,5 +34,6 @@
}
}

+ cout<<"=---> Total Tests Failed: "<<retVal<<endl;
return retVal;
}
=======================================
--- /branches/backus_dev/src/common/utility/Point2D.cpp Sun Feb 7 18:40:30
2010
+++ /branches/backus_dev/src/common/utility/Point2D.cpp Sat Feb 13 15:17:14
2010
@@ -117,8 +117,8 @@
}

bool Point2D::operator==(const Point2D& other) {
- return (relativeCompare(1.0+_x, 1.0+other._x, _epsilon) &&
- relativeCompare(1.0+_y,1.0+other._y, _epsilon));
+ return (relativeCompare(_x, other._x, _epsilon) &&
+ relativeCompare(_y,other._y, _epsilon));
}
bool Point2D::operator!=(const Point2D& other) {
return !(*this == other);
=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.cpp Wed Feb 10
18:19:00 2010
+++ /branches/backus_dev/src/common/utility/Vector2D.cpp Sat Feb 13
15:17:14 2010
@@ -7,7 +7,7 @@
/**
* Sets the default epsilon value.
*/
-void Vector2D::setDefaultEpsilon(const double& epsilon) {
+void Vector2D::setDefaultEpsilon(const double epsilon) {
DEFAULT_EPSILON = epsilon;
}

@@ -21,7 +21,7 @@
/**
* Sets the epsilon value for this object.
*/
-void Vector2D::setEpsilon(const double &epsilon) {
+void Vector2D::setEpsilon(const double epsilon) {
_epsilon = epsilon;
}

@@ -132,7 +132,7 @@
/**
* Returns the dot product of the specified vector with this vector.
*/
-double Vector2D::dot(const Vector2D other) {
+double Vector2D::dot(const Vector2D& other) {
return (_x*other._x+_y*other._y);
}

@@ -140,7 +140,7 @@
/**
* Returns true if the specified vector is parallel to this vector.
*/
-bool Vector2D::isParallelTo(const Vector2D other) {
+bool Vector2D::isParallelTo(const Vector2D& other) {
Vector2D thisUnitV = *this;
Vector2D otherUnitV = other;
thisUnitV.normalize();
@@ -158,7 +158,7 @@
/**
* Returns true if the specified vector is normal to this vector.
*/
-bool Vector2D::isNormalTo(const Vector2D other) {
+bool Vector2D::isNormalTo(const Vector2D& other) {
Vector2D thisV = *this;
Vector2D otherNormV = other;

@@ -181,7 +181,7 @@
* If one or both of the vectors has a length of zero, a value of
* 0.0 is returned.
*/
-double Vector2D::getAngle(const Vector2D other) {
+double Vector2D::getAngle(const Vector2D& other) {

// we know that mag(v1)*mag(v2)*cos(theta)=normal(v1) dot v2
// thus theta = arccos( (normal(v1) dot v2) / ( mag(v1)*mag(v2) ) )
@@ -217,13 +217,43 @@
* @param a Origination point.
* @param scalar The amount to scale this vector by first.
*/
-Point2D Vector2D::getPoint(const Point2D a, const double scalar) {
+Point2D Vector2D::getPoint(const Point2D& a, const double scalar) {
Point2D retVal = a;
retVal.setX(retVal.getX()+scalar*_x);
retVal.setY(retVal.getY()+scalar*_y);

return retVal;
}
+
+/**
+ * Calculates the distance from point a to point b. If point b is
+ * not on the line described by this vector and point a, NaN is returned.
+ */
+double Vector2D::getDistanceToPoint(const Point2D& a, const Point2D& b) {
+ Point2D inA = a;
+ Point2D inB = b;
+
+ // check to ensure points aren't the same.
+ if(inA == inB) {
+ return 0.0;
+ }
+
+ // Check to ensure point is on line
+ Vector2D replica = *this;
+ if(replica.isOnLine(inA, inB)) {
+ // use the first dimension that isn't 0.0. If both are, then return
+ // NAN (shouldn't get to that point).
+ if(!relativeCompare(_x, 0.0, _epsilon)) {
+ return (inB.getX()-inA.getX())/_x;
+ } else if(!relativeCompare(_y, 0.0, _epsilon)) {
+ return (inB.getY()-inA.getY())/_y;
+ } else {
+ return NAN;
+ }
+ }
+ // not the same line, return NaN.
+ return NAN;
+}

/**
* Checks to see if point B is on the line specified
@@ -232,7 +262,7 @@
* @param a A point known to be on the line.
* @param b The point to check.
*/
-bool Vector2D::isOnLine(const Point2D a, const Point2D b) {
+bool Vector2D::isOnLine(const Point2D& a, const Point2D& b) {
// check to see if the points are the same. If so, easy peasy!
Point2D copyA = a;
if(copyA == b) {
@@ -241,15 +271,43 @@

// create a replica and ensure that its magnitude isn't 0.0.
Vector2D replica = *this;
+ replica.normalize();
if(relativeCompare(replica.magnitude(),0.0, _epsilon)) {
return true;
}
+
// If the vector from point B to point A is parallel to vector V, then
// point B is on the line.
Vector2D v2;
v2 = b - a;
return replica.isParallelTo(v2);
}
+
+double Vector2D::getIntersectingMul(const Point2D& pA, const Vector2D& vB,
+ const Point2D& pB) {
+
+ Point2D rPA = pA;
+ Point2D rPB = pB;
+ Vector2D rVB = vB;
+ Vector2D rVA = *this;
+
+ // calculate the denominator of the intersect formula. If it is 0,
+ // then the lines are parallel or one or both vectors are undefined.
+ double denom = rVB._y*rVA._x - rVB._x*rVA._y;
+ if(!relativeCompare(denom,0.0, _epsilon)) {
+ // if the points are the same, then return the point.
+ if(rPA == rPB) {
+ return 0.0;
+ } else {
+ // otherwise, calculate the intersection point.
+ double numer = rVB._x*(rPA.getY() - rPB.getY()) -
+ rVB._y*(rPA.getX() - rPB.getX());
+ return (numer / denom);
+ }
+ }
+ // lines are parallel or vectors are undefined.
+ return NAN;
+}

/**
* Finds the point of intersection of the line specified
@@ -266,19 +324,21 @@
* @param result The resulting point.
* @return True if the lines intersect, false if they do not.
*/
-bool Vector2D::getIntersectingPt(const Point2D pA, const Vector2D vB,
- const Point2D pB, Point2D& result) {
-
- Point2D rPA = pA;
- Point2D rPB = pB;
- Vector2D rVB = vB;
- double numer = rVB._x*(rPB.getY() - rPA.getY()) -
- rVB._y*(rPB.getX() - rPA.getX());
- double denom = rVB._y*_x - rVB._x*_y;
- if(denom != 0) {
- Vector2D vF = (*this) * (numer / denom);
- result = rPA + vF;
- }
+bool Vector2D::getIntersectingPt(const Point2D& pA, const Vector2D& vB,
+ const Point2D& pB, Point2D& result) {
+
+ double mul = getIntersectingMul(pA, vB, pB);
+
+ if(!isnan(mul)) {
+ Vector2D rV = *this;
+ rV *= mul;
+ result = pA;
+ result = pA + rV;
+ return true;
+ }
+
+ result.setX(NAN);
+ result.setY(NAN);
return false;
}

=======================================
--- /branches/backus_dev/src/common/utility/Vector2D.h Wed Feb 10 18:19:00
2010
+++ /branches/backus_dev/src/common/utility/Vector2D.h Sat Feb 13 15:17:14
2010
@@ -101,17 +101,17 @@
/**
* Returns the dot product of the specified vector with this vector.
*/
- double dot(const Vector2D other);
+ double dot(const Vector2D& other);

/**
* Returns true if the specified vector is parallel to this vector.
*/
- bool isParallelTo(const Vector2D other);
+ bool isParallelTo(const Vector2D& other);

/**
* Returns true if the specified vector is normal to this vector.
*/
- bool isNormalTo(const Vector2D other);
+ bool isNormalTo(const Vector2D& other);

/**
* Calculates the angle between the specified vector and this vector.
@@ -119,7 +119,7 @@
* If one or both of the vectors has a length of zero, a value of
* 0.0 is returned.
*/
- double getAngle(const Vector2D other);
+ double getAngle(const Vector2D& other);

/**
* Rotates the vector by the angle radians.
@@ -134,7 +134,13 @@
* @param a Origination point.
* @param scalar The amount to scale this vector by first.
*/
- Point2D getPoint(const Point2D a, const double scalar=1.0);
+ Point2D getPoint(const Point2D& a, const double scalar=1.0);
+
+ /**
+ * Calculates the distance from point a to point b. If point b is
+ * not on the line described by this vector and point a, NaN is returned.
+ */
+ double getDistanceToPoint(const Point2D& a, const Point2D& b);

/**
* Checks to see if point B is on the line specified
@@ -143,12 +149,42 @@
* @param a A point known to be on the line.
* @param b The point to check.
*/
- bool isOnLine(const Point2D a, const Point2D b);
+ bool isOnLine(const Point2D& a, const Point2D& b);
+
+ /**
+ * Finds the point of intersection of the line specified
+ * by this vector and point A and the line specified by
+ * vector V2 and point B. Returns false if the lines
+ * do not intersect. The resulting point is put into result.
+ *
+ * @param a A point on the line specified by this vector.
+ * @param v2 The vector describing the other line.
+ * @param b A point on the line described by vector v.
+ * @param result The resulting point.
+ * @return the multiplier, or NAN if lines don't intersect.
+ */
+ double getIntersectingMul(const Point2D& pA, const Vector2D& vB,
+ const Point2D& pB);
+
+ /**
+ * Finds the point of intersection of the line specified
+ * by this vector and point A and the line specified by
+ * vector V2 and point B. Returns false if the lines
+ * do not intersect. The resulting point is put into result.
+ *
+ * @param a A point on the line specified by this vector.
+ * @param v2 The vector describing the other line.
+ * @param b A point on the line described by vector v.
+ * @param result The resulting point.
+ * @return True if the lines intersect, false if they do not.
+ */
+ bool getIntersectingPt(const Point2D& pA, const Vector2D& vB,
+ const Point2D& pB, Point2D& result);

/**
* Sets the default epsilon value.
*/
- static void setDefaultEpsilon(const double& epsilon);
+ static void setDefaultEpsilon(const double epsilon);

/**
* Gets the default epsilon value.
@@ -158,7 +194,7 @@
/**
* Sets the epsilon value for this object.
*/
- void setEpsilon(const double &epsilon);
+ void setEpsilon(const double epsilon);

/**
* Gets the epsilon value for this object.
@@ -169,21 +205,6 @@
bool operator==(const Vector2D& other);
bool operator!=(const Vector2D& other);

- /**
- * Finds the point of intersection of the line specified
- * by this vector and point A and the line specified by
- * vector V2 and point B. Returns false if the lines
- * do not intersect. The resulting point is put into result.
- *
- * @param a A point on the line specified by this vector.
- * @param v2 The vector describing the other line.
- * @param b A point on the line described by vector v.
- * @param result The resulting point.
- * @return True if the lines intersect, false if they do not.
- */
- bool getIntersectingPt(const Point2D pA, const Vector2D vB,
- const Point2D pB, Point2D& result);
-
Vector2D& operator*(const double val);
Vector2D& operator*=(const double val);
Vector2D& operator/(const double val);
=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
Wed Feb 10 18:19:00 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.cpp
Sat Feb 13 15:17:14 2010
@@ -534,6 +534,46 @@
QVERIFY(relativeCompare(result.getY(),0.0, 1.0e-6));

}
+
+void Test_Vector2D::Test_GetDistanceToPoint() {
+ Vector2D vector;
+ Point2D origin;
+ Point2D testPoint;
+
+ // positive slope, point on line
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(5.3,8.3);
+ QVERIFY(relativeCompare(vector.getDistanceToPoint(origin, testPoint),
+ 3.3, 1e-6));
+
+ // positive slope, point not on line
+ vector.setValue(1.0,1.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(5.3,7.3);
+ QVERIFY(isnan(vector.getDistanceToPoint(origin, testPoint)));
+
+ // vertical vector
+ vector.setValue(4.0,0.0);
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(0.3,5.0);
+ QVERIFY(relativeCompare(vector.getDistanceToPoint(origin, testPoint),
+ -1.7/4.0, 1e-6));
+
+ // horizontal vector
+ vector.setValue(0.0,1.7);
+ origin.setValue(7.1,5.0);
+ testPoint.setValue(7.1,8.3);
+ QVERIFY(relativeCompare(vector.getDistanceToPoint(origin, testPoint),
+ 3.3/1.7, 1e-6));
+
+ // default vector
+ // positive slope, point on line
+ vector.setValue();
+ origin.setValue(2.0,5.0);
+ testPoint.setValue(5.3,8.3);
+ QVERIFY(isnan(vector.getDistanceToPoint(origin, testPoint)));
+}

void Test_Vector2D::Test_IsOnLine() {
Vector2D vector;
@@ -571,8 +611,104 @@
QVERIFY(vector.isOnLine(origin,testPoint));
}

-void Test_Vector2D::Test_GetIntersectingPt() {
- QFAIL("Not implemented yet!");
+void Test_Vector2D::Test_Intersections() {
+ Vector2D vA;
+ Vector2D vB;
+ Point2D pA;
+ Point2D pB;
+ Point2D pI;
+ bool bResult;
+
+ // 60 degree case, multiplier
+ vA.setValue(1.0,1.0);
+ pA.setValue(2.0,3.0);
+ vB.setValue(1.0,1.0);
+ vB.rotate(1.047198);
+ pB.setValue(-4.0,5.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(relativeCompare(vA.getIntersectingMul(pA, vB, pB),-4.3093987,
1e-6));
+
+ // Normal case
+ vA.setValue(1.0,1.0);
+ pA.setValue(2.0,3.0);
+ vB.setValue(-1.0,1.0);
+ pB.setValue(-4.0,1.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(bResult);
+ QVERIFY(relativeCompare(pI.getX(),-2.0, 1e-6));
+ QVERIFY(relativeCompare(pI.getY(),-1.0, 1e-6));
+
+ // Parallel case
+ vA.setValue(1.0,2.0);
+ pA.setValue(4.0,1.0);
+ vB.setValue(8.0,16.0);
+ pB.setValue(17.0,15.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(!bResult);
+ QVERIFY(isnan(pI.getX()));
+ QVERIFY(isnan(pI.getY()));
+
+ // Normal case, same point
+ vA.setValue(1.0,4.0);
+ pA.setValue(7.0,9.0);
+ vB.setValue(-4.0,1.0);
+ pB.setValue(7.0,9.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(bResult);
+ QVERIFY(relativeCompare(pI.getX(),7.0, 1e-6));
+ QVERIFY(relativeCompare(pI.getY(),9.0, 1e-6));
+
+ // Parallel case, same point
+ vA.setValue(1.0,1.0);
+ pA.setValue(2.0,3.0);
+ vB.setValue(1.0,1.0);
+ pB.setValue(-4.0,5.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(!bResult);
+ QVERIFY(isnan(pI.getX()));
+ QVERIFY(isnan(pI.getY()));
+
+ // 60 degree case
+ vA.setValue(1.0,1.0);
+ pA.setValue(2.0,3.0);
+ vB.setValue(1.0,1.0);
+ vB.rotate(1.047198);
+ pB.setValue(-4.0,5.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(bResult);
+ QVERIFY(relativeCompare(pI.getX(),-2.3093987, 1e-6));
+ QVERIFY(relativeCompare(pI.getY(),-1.3093987, 1e-6));
+
+ // 60 degree case, same point
+ vA.setValue(1.0,1.0);
+ pA.setValue(2.0,3.0);
+ vB.setValue(1.0,1.0);
+ vB.rotate(1.047198);
+ pB.setValue(2.0,3.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(bResult);
+ QVERIFY(relativeCompare(pI.getX(),2.0, 1e-6));
+ QVERIFY(relativeCompare(pI.getY(),3.0, 1e-6));
+
+ // first vector is default
+ vA.setValue();
+ pA.setValue(2.0,3.0);
+ vB.setValue(-1.0,1.0);
+ pB.setValue(-4.0,5.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(!bResult);
+ QVERIFY(isnan(pI.getX()));
+ QVERIFY(isnan(pI.getY()));
+
+ // second vector is default
+ vA.setValue(1.7,4.9);
+ pA.setValue(2.0,3.0);
+ vB.setValue();
+ pB.setValue(-4.0,5.0);
+ bResult=vA.getIntersectingPt(pA, vB, pB, pI);
+ QVERIFY(!bResult);
+ QVERIFY(isnan(pI.getX()));
+ QVERIFY(isnan(pI.getY()));
}

void Test_Vector2D::Test_OperatorSet() {
=======================================
--- /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h Wed
Feb 10 18:19:00 2010
+++ /branches/backus_dev/src/common/utility/unit_tests/Test_Vector2D.h Sat
Feb 13 15:17:14 2010
@@ -49,8 +49,9 @@
void Test_GetAngle();
void Test_Rotate();
void Test_GetPoint();
+ void Test_GetDistanceToPoint();
void Test_IsOnLine();
- void Test_GetIntersectingPt();
+ void Test_Intersections();

void Test_OperatorSet();
void Test_OperatorEqual();

Reply all
Reply to author
Forward
0 new messages