Skip to content

Commit

Permalink
fixed test, added documentation, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Joey Andres committed Apr 25, 2015
1 parent d1f139a commit 5b4eb2a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 12 deletions.
72 changes: 72 additions & 0 deletions include/DouglasPeucker.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ struct p2dAccessor{
inline static double getY(const p2d& p) { return std::get<1>(p); }
};

/*!@class Distance2D
* @brief cartesian distance in 2D.
*
* @typename T 2D data type.
* @typename TAccessor2D an accessor class that contains getX and getY.
* defaults to void if T have getX and getY method.
*/
template<typename T, typename TAccessor2D = void>
class Distance2D{
public:
Expand All @@ -158,6 +165,11 @@ class Distance2D{
}
};

/*!@class Distance2D
* @brief cartesian distance in 2D.
*
* @typename T 2D data type.
*/
template<typename T>
class Distance2D<T, void>{
public:
Expand All @@ -168,6 +180,13 @@ class Distance2D<T, void>{
}
};

/*!@class PointSegmentDistance2D
* @brief Distance between point and a line segment.
*
* @typename T 2D data type.
* @typename TAccessor2D an accessor class that contains getX and getY.
* defaults to void if T have getX and getY method.
*/
template <typename T, typename TAccessor2D = void>
class PointSegmentDistance2D{
public:
Expand All @@ -188,6 +207,11 @@ class PointSegmentDistance2D{
}
};

/*!@class PointSegmentDistance2D
* @brief Distance between point and a line segment.
*
* @typename T 2D data type.
*/
template <typename T>
class PointSegmentDistance2D<T, void>{
public:
Expand All @@ -207,6 +231,13 @@ class PointSegmentDistance2D<T, void>{
}
};

/*!@class DouglasPuecker2D
* @brief Douglas-Peucker implementation for 2D line.
*
* @typename T 2D data type.
* @typename TAccessor2D an accessor class that contains getX and getY.
* defaults to void if T have getX and getY method.
*/
template <typename T, typename TAccessor2D = void>
class DouglasPuecker2D final : public DouglasPeuckerAbstract<T>{
public:
Expand All @@ -223,6 +254,11 @@ class DouglasPuecker2D final : public DouglasPeuckerAbstract<T>{
}
};

/*!@class DouglasPuecker2D
* @brief Douglas-Peucker implementation for 2D line.
*
* @typename T 2D data type.
*/
template <typename T>
class DouglasPuecker2D<T, void> final : public DouglasPeuckerAbstract<T>{
public:
Expand Down Expand Up @@ -256,6 +292,13 @@ struct p3dAccessor{
}
};

/*!@class Distance3D
* @brief cartesian distance in 3D.
*
* @typename T 3D data type.
* @typename TAccessor3D an accessor class that contains getX, getY and getZ.
* defaults to void if T have getX, getY, getZ.
*/
template<typename T, typename TAccessor3D = void>
class Distance3D{
public:
Expand All @@ -267,6 +310,11 @@ class Distance3D{
}
};

/*!@class Distance3D
* @brief cartesian distance in 3D.
*
* @typename T 3D data type.
*/
template<typename T>
class Distance3D<T, void>{
public:
Expand All @@ -278,6 +326,13 @@ class Distance3D<T, void>{
}
};

/*!@class PointSegmentDistance3D
* @brief Distance between point and a line segment.
*
* @typename T 3D data type.
* @typename TAccessor3D an accessor class that contains getX, getY and getZ.
* defaults to void if T have getX, getY, getZ.
*/
template <typename T, typename TAccessor3D = void>
class PointSegmentDistance3D{
public:
Expand Down Expand Up @@ -319,6 +374,11 @@ class PointSegmentDistance3D{
}
};

/*!@class PointSegmentDistance3D
* @brief Distance between point and a line segment.
*
* @typename T 3D data type.
*/
template <typename T>
class PointSegmentDistance3D<T, void>{
public:
Expand Down Expand Up @@ -361,6 +421,13 @@ class PointSegmentDistance3D<T, void>{
}
};

/*!@class DouglasPuecker3D
* @brief Douglas-Peucker implementation for 3d line.
*
* @typename T 3D data type.
* @typename TAccessor3D an accessor class that contains getX, getY and getZ.
* defaults to void if T have getX, getY, getZ.
*/
template <typename T, typename TAccessor3D = void>
class DouglasPuecker3D final : public DouglasPeuckerAbstract<T>{
public:
Expand All @@ -379,6 +446,11 @@ class DouglasPuecker3D final : public DouglasPeuckerAbstract<T>{
}
};

/*!@class DouglasPuecker3D
* @brief Douglas-Peucker implementation for 3d line.
*
* @typename T 3D data type.
*/
template <typename T>
class DouglasPuecker3D<T, void> final : public DouglasPeuckerAbstract<T>{
public:
Expand Down
22 changes: 10 additions & 12 deletions test/src/DouglasPeuckerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,28 @@ struct point3dAccessor{
};

void DouglasPeuckerTest::distanceTest() {
DouglasPuecker2D<p2d, p2dAccessor> dp2d;
CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0,
dp2d._distance(p2d(-2, 1),
p2d(1, 5)), 0.001F);
double result = Distance2D<p2d, p2dAccessor>::getDistance(p2d(-2, 1),
p2d(1, 5));
CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0, result, 0.001F);
}

void DouglasPeuckerTest::distanceTest2() {
DouglasPuecker2D<point2d> dp2d;
CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0,
dp2d._distance(point2d(-2, 1),
point2d(1, 5)), 0.001F);
double result = Distance2D<point2d>::getDistance(point2d(-2, 1),
point2d(1, 5));
CPPUNIT_ASSERT_DOUBLES_EQUAL(5.0, result, 0.001F);
}


void DouglasPeuckerTest::pointSegmentDistance() {
DouglasPuecker2D<p2d, p2dAccessor> dp2d;
DouglasPuecker2D<p2d, p2dAccessor> dp2d(std::list<p2d>({}));
double result = dp2d._pointSegmentDistance(p2d(1.0F, -2.0F/3.0F),
p2d(2.0F, 0.0F),
p2d(5.0F, 6.0F));
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.328F, result, 0.001F);
}

void DouglasPeuckerTest::pointSegmentDistance2() {
DouglasPuecker2D<point2d> dp2d;
DouglasPuecker2D<point2d> dp2d(std::list<point2d>({}));
double result = dp2d._pointSegmentDistance(point2d(1.0F, -2.0F/3.0F),
point2d(2.0F, 0.0F),
point2d(5.0F, 6.0F));
Expand All @@ -81,15 +79,15 @@ void DouglasPeuckerTest::pointSegmentDistance2() {
// For simplicity, we are just going to test if this is consistent with the 2D,
// by just adding 0 as the magntide of 3rd dimension.
void DouglasPeuckerTest::pointSegmentDistance3D() {
DouglasPuecker3D<p3d, p3dAccessor> dp3d;
DouglasPuecker3D<p3d, p3dAccessor> dp3d(std::list<p3d>({}));
double result = dp3d._pointSegmentDistance(p3d(1.0F, -2.0F/3.0F, 0.0f),
p3d(2.0F, 0.0F, 0.0f),
p3d(5.0F, 6.0F, 0.0f));
CPPUNIT_ASSERT_DOUBLES_EQUAL(3.328F, result, 0.001F);
}

void DouglasPeuckerTest::pointSegmentDistance3D2() {
DouglasPuecker3D<point3d> dp3d;
DouglasPuecker3D<point3d> dp3d(std::list<point3d>({}));
double result = dp3d._pointSegmentDistance(point3d(1.0F, -2.0F/3.0F, 0.0f),
point3d(2.0F, 0.0F, 0.0f),
point3d(5.0F, 6.0F, 0.0f));
Expand Down

0 comments on commit 5b4eb2a

Please sign in to comment.