diff --git a/CMakeLists.txt b/CMakeLists.txt index 414230a8b5c4fcf00ac10f3df35a136be792247d..270fdfceb833cb4770462d3e4457e6efb97eafdd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.12) set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + include(FetchContent) FetchContent_Declare( googletest @@ -41,6 +43,9 @@ add_executable( src/data_structures/box.cpp src/data_structures/quad_tree.cpp tests/quad_tree_tests.cpp + tests/edge_union_cases.cpp + tests/unit_tests/convex_triangulation_tests.cpp + src/utilities/convex_triangulation.cpp ) include_directories(include) diff --git a/include/convex_triangulation.h b/include/convex_triangulation.h new file mode 100644 index 0000000000000000000000000000000000000000..21c08ef86b4dc3876a9694ee5fe03d734fbcba74 --- /dev/null +++ b/include/convex_triangulation.h @@ -0,0 +1,8 @@ +#pragma once + +#include <triangle.h> +#include <point.h> +#include <vector> + +// Requires a convex shape +std::vector<Triangle> convexTriangulation(const std::vector<Point> &points, int depth, int triangleId); diff --git a/include/triangle.h b/include/triangle.h index 91803818bd86968ac472e13a93211b13b53c125c..13e1f6c9c73734841864f41d0479842fa8c59b20 100644 --- a/include/triangle.h +++ b/include/triangle.h @@ -11,4 +11,5 @@ struct Triangle Triangle(Point p1, Point p2, Point p3, int depth, int id=0); bool pointInTriangle(const Point &p) const; Point nextPoint(int pointIndex) const; + bool operator==(const Triangle &other) const; }; diff --git a/src/shapes/triangle.cpp b/src/shapes/triangle.cpp index 3371128803bce59698d93768468383734703fc4d..f7ad3edd8983f869a1abdcf2dba8bc834ad3051e 100644 --- a/src/shapes/triangle.cpp +++ b/src/shapes/triangle.cpp @@ -2,6 +2,14 @@ #include "triangle_edges.h" #include "edge.h" +bool Triangle::operator==(const Triangle &other) const { + return points[0] == other.points[0] && + points[1] == other.points[1] && + points[2] == other.points[2] && + depth == other.depth && + id == other.id; +} + bool Triangle::neighbours(const Triangle &other) const { return false; } @@ -23,4 +31,4 @@ bool Triangle::pointInTriangle(const Point &p) const { int nextPoint(int pointIndex) { return (pointIndex + 1) % 3; -} \ No newline at end of file +} diff --git a/src/utilities/convex_triangulation.cpp b/src/utilities/convex_triangulation.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c9ba15d41947163b4418f8c9955fa7bc084bfb61 --- /dev/null +++ b/src/utilities/convex_triangulation.cpp @@ -0,0 +1,20 @@ +#include <convex_triangulation.h> + +std::vector<Triangle> convexTriangulation(const std::vector<Point> &points, int depth, int triangleId) { + const int size = points.size(); + if (size < 3) { + return std::vector<Triangle>{}; + } + + const Triangle t1 = Triangle(points[0], points[1], points[2], depth, triangleId); + std::vector<Triangle> results = {t1}; + + + Point first = points[0]; + for (int i = size - 1; i >= 3; i--) { + results.push_back(Triangle(first, points[2], points[i], depth, triangleId)); + first = points[i]; + } + return results; + +} diff --git a/tests/edge_union_cases.cpp b/tests/edge_union_cases.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fb9cf7c67a650f0ce4bda357c4d887bf13454cce --- /dev/null +++ b/tests/edge_union_cases.cpp @@ -0,0 +1,16 @@ +#include <gtest/gtest.h> +#include <union.h> +#include <triangle.h> + +TEST(UnionEdgeTests, TriangleVertexOnEdge) { + Triangle t1 = Triangle({}, {}, {}, 1, 1); + Triangle t2 = Triangle({}, {}, {}, 2, 2); + + auto result = unionize(t1, t2); + +} + +TEST(UnionEdgeTests, TriangleEdgeLength0) { + Triangle t1 = Triangle({1,1}, {2,2}, {3,3}, 1, 1); + Triangle t2 = Triangle({}, {}, {}, 2, 2); +} diff --git a/tests/quad_tree_tests.cpp b/tests/quad_tree_tests.cpp index 54606f696fbd1bf669fd5d013ecd4a0e5ee4d759..cbe8689d49ba2bb1ef1ad1958da316e0335d98e0 100644 --- a/tests/quad_tree_tests.cpp +++ b/tests/quad_tree_tests.cpp @@ -1,6 +1,7 @@ #include <gtest/gtest.h> #include <quad_tree.h> +/* TEST (QuadTreeTest, OverlappingTriangles) { QuadTree q{Box{0, 10, 0, 10}}; @@ -18,7 +19,7 @@ TEST (QuadTreeTest, OverlappingTriangles) { EXPECT_EQ(q.pointIntersection(Point{1,1}), 1); } - +*/ TEST (QuadTreeTest, SingleInsertion) { QuadTree q{Box{0, 10, 0, 10}}; @@ -49,4 +50,4 @@ TEST (QuadTreeTest, PointOnTriangleQueryTest) { EXPECT_EQ(q.pointIntersection(Point{3.5, 1.5}), 1); EXPECT_EQ(q.pointIntersection(Point{-1,-1}), POINT_NOT_IN_QUADTREE); -} \ No newline at end of file +} diff --git a/tests/unit_tests/convex_triangulation_tests.cpp b/tests/unit_tests/convex_triangulation_tests.cpp new file mode 100644 index 0000000000000000000000000000000000000000..21c77eb55fb784707ce63a65565e58855ed19223 --- /dev/null +++ b/tests/unit_tests/convex_triangulation_tests.cpp @@ -0,0 +1,41 @@ +#include <gtest/gtest.h> +#include <convex_triangulation.h> +#include <vector> + +TEST(ConvexTriangulationTests, Simple) { + + Point p1{1,3}; + Point p2{2,2}; + Point p3{3,2.1}; + Point p4{4,3.2}; + Point p5{2.1,4}; + + std::vector<Point> points = { + p1, p2, p3, p4, p5 + }; + + auto results = convexTriangulation(points, 2, 2); + + std::vector<Triangle> expected_result { + Triangle(p1, p2, p3, 2, 2), + Triangle(p1, p3, p5, 2, 2), + Triangle(p5, p3, p4, 2, 2) + }; + + EXPECT_EQ(results, expected_result); + +} + +TEST(ConvexTriangulationTests, TrivialTriangle) { + std::vector<Point> points = { + {0,0}, + {2,0}, + {1.5, 2} + }; + + auto results = convexTriangulation(points, 1, 1); + Triangle expected_triangle = Triangle({0,0}, {2,0}, {1.5, 2}, 1, 1); + + EXPECT_EQ(results.size(), 1); + EXPECT_EQ(results[0], expected_triangle); +}