From 473f307688b4db6646b0ef5175bd5d3fea3063ff Mon Sep 17 00:00:00 2001 From: Brandon Lai-Cheong <blaicheo@uwaterloo.ca> Date: Thu, 21 Nov 2024 13:02:13 -0500 Subject: [PATCH] Added neighbour property --- CMakeLists.txt | 1 + include/triangle.h | 7 +++++-- src/shapes/triangle.cpp | 8 +------- src/union.cpp | 5 ++++- tests/edge_union_cases.cpp | 5 +++++ tests/union_neighbours_tests.cpp | 13 +++++++++++++ tests/union_tests.cpp | 6 +++--- 7 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 tests/union_neighbours_tests.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e486617..61c608d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ add_executable( src/debug_utilities/print_triangles.cpp src/utilities/split_triangle.cpp tests/unit_tests/orientation_tests.cpp + tests/union_neighbours_tests.cpp ) include_directories(include) diff --git a/include/triangle.h b/include/triangle.h index aaf6580..4c7592e 100644 --- a/include/triangle.h +++ b/include/triangle.h @@ -1,6 +1,7 @@ #pragma once #include "point.h" #include "print_triangle.h" +#include <vector> // points specified counterclockwise struct Triangle @@ -8,8 +9,10 @@ struct Triangle Point points[3]; int depth; int id; - bool neighbours(const Triangle &other) const; - Triangle(const Point &p1, const Point &p2, const Point &p3, int depth = 0, int id = 0); + std::vector<int> neighbours; + + + Triangle(const Point &p1, const Point &p2, const Point &p3, int depth = 0, int id = 0, const std::vector<int> neighbours = {}); 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 0624f8f..97aff7a 100644 --- a/src/shapes/triangle.cpp +++ b/src/shapes/triangle.cpp @@ -2,13 +2,7 @@ #include "triangle_edges.h" #include "edge.h" - - -bool Triangle::neighbours(const Triangle &other) const { - return false; -} - -Triangle::Triangle(const Point &p1, const Point &p2, const Point &p3, int depth, int id) : points{p1,p2,p3}, depth{depth}, id{id} {} +Triangle::Triangle(const Point &p1, const Point &p2, const Point &p3, int depth, int id, std::vector<int> neighbours) : points{p1,p2,p3}, depth{depth}, id{id}, neighbours{neighbours} {} bool Triangle::pointInTriangle(const Point &p) const { // all tests must be positive diff --git a/src/union.cpp b/src/union.cpp index 054a3ed..48d60ba 100644 --- a/src/union.cpp +++ b/src/union.cpp @@ -40,6 +40,9 @@ std::vector<Triangle> unionizeTopAndBottom(const Triangle &top, const Triangle & std::vector<Triangle> unionize(const Triangle &t1, const Triangle &t2) { + if (std::find(t1.neighbours.begin(), t1.neighbours.end(), t2.id) != t1.neighbours.end()) { + return {t1, t2}; + } if (intersections(t1, t2).empty()) { return {t1, t2}; @@ -49,4 +52,4 @@ std::vector<Triangle> unionize(const Triangle &t1, const Triangle &t2) return unionizeTopAndBottom(t1, t2); } return unionizeTopAndBottom(t2, t1); -} \ No newline at end of file +} diff --git a/tests/edge_union_cases.cpp b/tests/edge_union_cases.cpp index fb9cf7c..30bf2e4 100644 --- a/tests/edge_union_cases.cpp +++ b/tests/edge_union_cases.cpp @@ -14,3 +14,8 @@ TEST(UnionEdgeTests, TriangleEdgeLength0) { Triangle t1 = Triangle({1,1}, {2,2}, {3,3}, 1, 1); Triangle t2 = Triangle({}, {}, {}, 2, 2); } + +TEST(UnionEdgeTests, TriangleOnSide) { + Triangle t1 = Triangle({}, {}, {}, 1, 1); + Triangle t2 = Triangle({}, {}, {}, 2, 2); +} diff --git a/tests/union_neighbours_tests.cpp b/tests/union_neighbours_tests.cpp new file mode 100644 index 0000000..0896426 --- /dev/null +++ b/tests/union_neighbours_tests.cpp @@ -0,0 +1,13 @@ +#include <gtest/gtest.h> +#include <triangle.h> +#include <union.h> + +TEST (UnionNeighbourTests, NeighbourTest) { + Triangle t1 = Triangle({}, {}, {}, 1, 1, {2}); + Triangle t2 = Triangle({}, {}, {}, 2, 2, {1}); + + auto results = unionize(t1, t2); + std::vector<Triangle> expected ={t1, t2} ; + + EXPECT_EQ(results, expected); +} diff --git a/tests/union_tests.cpp b/tests/union_tests.cpp index 38d36c2..ace3d5b 100644 --- a/tests/union_tests.cpp +++ b/tests/union_tests.cpp @@ -128,9 +128,9 @@ TEST(UnionTests, StarTest) results = unionize(bottom, top); std::vector<Triangle> expected_results_2 = { - Triangle({0,3}, {0.8,3}, {0.540541,3.64865}, 1, 3), - Triangle({5,3}, {4.30233,3.83721}, {3.8,3}, 1, 3), - Triangle({2.5,6}, {1.66667,5}, {3.33333,5}, 1, 3), + Triangle({0,3}, {0.8,3}, {0.540541,3.64865}, 3, 1), + Triangle({5,3}, {4.30233,3.83721}, {3.8,3}, 3, 1), + Triangle({2.5,6}, {1.66667,5}, {3.33333,5}, 3, 1), Triangle({0,5}, {2,0}, {5,5}, 2, 2) }; -- GitLab