Skip to content
Snippets Groups Projects
Commit 46cdd56f authored by Brandon Lai-Cheong's avatar Brandon Lai-Cheong
Browse files

triangulation

parent ec957563
No related branches found
No related tags found
No related merge requests found
......@@ -23,7 +23,10 @@ add_executable(
tests/unit_tests/simple_triangle_tests.cpp
tests/unit_tests/contourize_tests.cpp
tests/unit_tests/point_in_triangle_tests.cpp
tests/unit_tests/triangulation_tests.cpp
tests/unit_tests/ear_removal_tests.cpp
src/utilities/intersections.cpp
tests/union_tests.cpp
src/shapes/triangle.cpp
src/shapes/triangle_edges.cpp
src/utilities/contourize.cpp
......@@ -32,6 +35,7 @@ add_executable(
src/shapes/point.cpp
src/union.cpp
src/utilities/triangulation.cpp
src/utilities/pointList.cpp
)
include_directories(include)
......
#pragma once
#define MAX_TRIANGLE_INTERSECTION_POINTS 12
\ No newline at end of file
#pragma once
#include <constants.h>
#include <point.h>
#include <vector>
struct PointNode {
int prevIndex;
int nextIndex;
Point p;
int next() const;
};
class PointList {
int size;
public:
PointNode points[MAX_TRIANGLE_INTERSECTION_POINTS];
PointList(const std::vector<Point> &points);
bool empty() const;
int prev(const PointNode &p) const;
int next(const PointNode &p) const;
void remove(int index);
};
\ No newline at end of file
#pragma once
#include "edge.h"
struct Triangle;
struct TriangleEdges {
......
#pragma once
#include <vector>
#include <optional>
#include <triangle.h>
struct Triangle;
class PointList;
struct Point;
std::optional<Triangle> removeEar(int &index, PointList &pointList, const std::vector<Point> &allPoints);
std::vector<Triangle> triangulate(std::vector<Point> points);
#include <pointList.h>
bool PointList::empty() const {
return size == 0;
}
int PointList::prev(const PointNode &p) const {
const int index = (p.prevIndex + size) % size;
return index;
}
int PointList::next(const PointNode &p) const {
const int index = p.nextIndex % size;
return index;
}
void PointList::remove(int i) {
if (size == 1) {
size--;
return;
}
const PointNode &p = points[i];
int prevInd = prev(p);
int nextInd = next(p);
points[prevInd].nextIndex = nextInd;
points[nextInd].prevIndex = prevInd;
size--;
}
PointList::PointList(const std::vector<Point> &points) : size {points.size()} {
for (int i = 0; i < points.size(); i++) {
this->points[i] = PointNode{i - 1, i + 1, points[i]};
}
}
\ No newline at end of file
......@@ -2,19 +2,76 @@
#include <list>
#include <point.h>
#include <triangle.h>
#include <optional>
#include <pointList.h>
bool isAnEar(Triangle triangle, const std::vector<Point> &points) {
for (const Point &p: points) {
if (triangle.pointInTriangle(p)) {
return false;
}
}
return true;
}
int mod(int a, int b) {
return (a + b) % b;
}
std::optional<Triangle> removeEar(int &index, PointList &pointList, const std::vector<Point> &allPoints) {
PointNode &p = pointList.points[index];
const int next = pointList.next(p);
const int prev = pointList.prev(p);
const PointNode &nextNode = pointList.points[next];
const PointNode &prevNode = pointList.points[prev];
/*
bool isAnEar(Point point, Triangle triangle, Point start[], Point end[]) {
Triangle candidate;
if (index > next_index && next_index < previous_index) {
candidate = Triangle{points[index], points[next_index], points[previous_index], 0};
}
if (index < next_index) {
candidate = Triangle{points[index], points[next_index], points[previous_index], 0};
}
*/
Triangle candidate = Triangle{p.p, nextNode.p, prevNode.p, 0};
if (isAnEar(candidate, allPoints)) {
index = pointList.next(nextNode);
pointList.remove(index);
return candidate;
}
index = prev;
return {};
}
*/
std::vector<Triangle> triangulate(std::vector<Point> points) {
//std::list<Point> polygon = std::list(points);
std::vector<Triangle> result;
for (int i = 0; i < points.size(); i++) {
PointList polygon = PointList(points);
int i = 0;
while (!polygon.empty()) {
std::optional<Triangle> t = removeEar(i, polygon, points);
if (t.has_value()) {
result.push_back(t.value());
}
}
return result;
......
#include <gtest/gtest.h>
#include <triangulation.h>
#include <vector>
#include <point.h>
#include <pointList.h>
TEST(EarRemovalTests, SingleEarRemoval) {
std::vector<Point> polygon = {
{3,48}, {52, 8}, {99,50}, {138,25},
{175, 77}, {131, 72}, {111, 113},
{72, 43}, {26, 55}, {29, 100}};
PointList pointList = PointList(polygon);
int index = 0;
auto t = removeEar(index, pointList, polygon);
EXPECT_FALSE(t.has_value());
EXPECT_TRUE(true);
auto t2 = removeEar(index, pointList, polygon);
auto t3 = removeEar(index, pointList, polygon);
}
\ No newline at end of file
#include "gtest/gtest.h"
#include <point.h>
#include <triangulation.h>
TEST(TriangulationTests, Page3Example) {
std::vector<Point> polygon = {
......@@ -8,6 +8,6 @@ TEST(TriangulationTests, Page3Example) {
{175, 77}, {131, 72}, {111, 113},
{72, 43}, {26, 55}, {29, 100}};
auto t = triangulate(polygon);
EXPECT_TRUE(t.size() > 0);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment