Browse Source

geometry: creates line (in implicit form)

boyska 7 years ago
parent
commit
aa2d7ba622
4 changed files with 76 additions and 2 deletions
  1. 3 2
      CMakeLists.txt
  2. 49 0
      TestLine.h
  3. 22 0
      geometry.cpp
  4. 2 0
      geometry.h

+ 3 - 2
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()

+ 49 - 0
TestLine.h

@@ -0,0 +1,49 @@
+#include <math.h>
+
+#ifdef HAS_OPENCV3
+#include <opencv2/core.hpp> //Any OPENCV3 code
+#else
+#include <opencv2/core/core.hpp> //Any Opencv2 code
+#endif
+using namespace cv;
+#include <cxxtest/TestSuite.h>
+
+#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));
+        }
+};

+ 22 - 0
geometry.cpp

@@ -5,6 +5,9 @@
 #else
 #include <opencv2/core/core.hpp> //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<double,2> res_as_vector = Rt*p_as_vector;
     return cv::Point(res_as_vector);
 }
+
+cv::Vec<double,3>
+get_line(Point p, Point q)
+{
+    Vec<double,3> 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<double,3> line, Point p)
+{
+    return line(0)*p.x + line(1)*p.y + line(2) == 0;
+}

+ 2 - 0
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<double,3> get_line(cv::Point, cv::Point);
+bool is_in_line(cv::Vec<double,3> line, cv::Point p);