diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f9f350..06ca22b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,9 +41,10 @@ find_package(CxxTest) if(CXXTEST_FOUND) include_directories(${CXXTEST_INCLUDE_DIR}) enable_testing() - CXXTEST_ADD_TEST(unittest_changecoord TestChangeCoord.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TestChangeCoord.h - geometry.cpp) + CXXTEST_ADD_TEST(unittest_changecoord TestChangeCoord.cpp TestChangeCoord.h geometry.cpp) + CXXTEST_ADD_TEST(unittest_line TestLine.cpp TestLine.h geometry.cpp) target_link_libraries(unittest_changecoord ${OpenCV_LIBS}) + target_link_libraries(unittest_line ${OpenCV_LIBS}) else() message(STATUS "Warning: Can't find CxxTest; testing will be disabled") endif() diff --git a/TestLine.h b/TestLine.h new file mode 100644 index 0000000..21b6a11 --- /dev/null +++ b/TestLine.h @@ -0,0 +1,49 @@ +#include + +#ifdef HAS_OPENCV3 +#include //Any OPENCV3 code +#else +#include //Any Opencv2 code +#endif +using namespace cv; +#include + +#include "geometry.h" + +class TestLine : public CxxTest::TestSuite +{ + public: + void testBisector(void) + { + // x-y = 0 + auto l = get_line(Point(0,0), Point(1,1)); + TS_ASSERT_EQUALS(l(0), -l(1)); + TS_ASSERT_EQUALS(l(2), 0); + } + + void testBisectorNegative(void) + { + // x+y = 0 + auto l = get_line(Point(0,0), Point(1,-1)); + TS_ASSERT_EQUALS(l(0), l(1)); + TS_ASSERT_EQUALS(l(2), 0); + } + + void testConstant(void) + { + // y = 42 + // 0x+1y-42=0 + auto l = get_line(Point(0,42), Point(10,42)); + TS_ASSERT_EQUALS(l(0), 0); + TS_ASSERT_EQUALS(l(2), -42*l(1)); + } + + void testVertical(void) + { + // x = 42 + // 1x+0y-42=0 + auto l = get_line(Point(42,0), Point(42,10)); + TS_ASSERT_EQUALS(l(1), 0); + TS_ASSERT_EQUALS(l(2), -42*l(0)); + } +}; diff --git a/geometry.cpp b/geometry.cpp index dd208a5..efd9a79 100644 --- a/geometry.cpp +++ b/geometry.cpp @@ -5,6 +5,9 @@ #else #include //Any Opencv2 code #endif +using namespace cv; + +#include "geometry.h" double dist(cv::Point a, cv::Point b) { @@ -39,3 +42,22 @@ map_point(double theta, cv::Point origin, cv::Point tomap) cv::Vec res_as_vector = Rt*p_as_vector; return cv::Point(res_as_vector); } + +cv::Vec +get_line(Point p, Point q) +{ + Vec line; //the three positions are a,b,c as in: ax + by + c = 0 + Point vector = q-p; + line(0) = - vector.y; + line(1) = vector.x; + line(2) = vector.y * p.x - vector.x * p.y; + assert(is_in_line(line, p)); + assert(is_in_line(line, q)); + return line; +} + +bool +is_in_line(cv::Vec line, Point p) +{ + return line(0)*p.x + line(1)*p.y + line(2) == 0; +} diff --git a/geometry.h b/geometry.h index f1c3ac9..2d9bfc5 100644 --- a/geometry.h +++ b/geometry.h @@ -7,3 +7,5 @@ double dist(cv::Point, cv::Point); double inner_angle(cv::Point, cv::Point, cv::Point); cv::Point map_point(double, cv::Point, cv::Point); +cv::Vec get_line(cv::Point, cv::Point); +bool is_in_line(cv::Vec line, cv::Point p);