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

fixed contourization

parent 677b8866
No related branches found
No related tags found
No related merge requests found
......@@ -37,6 +37,9 @@ add_executable(
src/utilities/triangulation.cpp
src/utilities/pointList.cpp
tests/unit_tests/orientation_tests.cpp
tests/unit_tests/intersections_tests.cpp
src/data_structures/quad_tree.cpp
src/data_structures/box.cpp
)
include_directories(include)
......
......@@ -5,6 +5,6 @@ struct Triangle;
struct Point;
std::vector<Point> contourize(const Triangle &leftmostTriangle, const Triangle &otherTriangle, int leftmostInd);
std::vector<Point> contourize(const Triangle &t1, const Triangle &t2);
std::vector<Point> sortPoints(std::vector<Point> &points);
\ No newline at end of file
......@@ -16,6 +16,7 @@ class PointList {
PointNode points[MAX_TRIANGLE_INTERSECTION_POINTS];
PointList(const std::vector<Point> &points);
bool empty() const;
int getSize() const;
int prev(const PointNode &p) const;
int next(const PointNode &p) const;
void remove(int index);
......
......@@ -3,11 +3,12 @@
template<typename T>
class StaticVector{
int size;
int size = 0;
public:
T items [2];
int getSize() const;
void clear();
void push_back(T item);
void push_back(std::optional<T> item);
};
......@@ -25,4 +26,8 @@ template<typename T> void StaticVector<T>::push_back(std::optional<T> item) {
template<typename T> int StaticVector<T>::getSize() const {
return size;
}
template<typename T> void StaticVector<T>::clear() {
size = 0;
}
\ No newline at end of file
......@@ -57,16 +57,33 @@ struct IntersectionAndEdge
int edge;
};
std::vector<Point> contourize(const Triangle &leftmostTriangle, const Triangle &otherTriangle, int leftmostInd)
std::vector<Point> contourize(const Triangle &t1, const Triangle &t2)
{
int leftmostInd = 0;
bool t1Leftmost = true;
for (int i = 0; i< NB_TRIANGLE_SIDES; i++) {
if (t1.points[i].x < (t1Leftmost ? t1 : t2).points[leftmostInd].x) {
leftmostInd = i;
t1Leftmost = true;
}
if (t2.points[i].x < (t1Leftmost ? t1 : t2).points[leftmostInd].x) {
leftmostInd = i;
t1Leftmost = false;
}
}
std::vector<Point> result;
const Triangle &leftmostTriangle =t1Leftmost ? t1 : t2;
const Triangle &otherTriangle = t1Leftmost ? t2 : t1;
const TriangleEdges lftEdges(leftmostTriangle);
const TriangleEdges otherEdges(otherTriangle);
StaticVector<IntersectionAndEdge> intersectionVector;
bool usingLeftTriangle = true;
StaticVector<IntersectionAndEdge> intersectionVector;
int prev = leftmostInd;
do
{
......@@ -77,6 +94,7 @@ std::vector<Point> contourize(const Triangle &leftmostTriangle, const Triangle &
int nextPointInTriangle = (prev + 1) % NB_TRIANGLE_SIDES;
const Edge &e1 = Edge{currentTriangle.points[prev], currentTriangle.points[nextPointInTriangle]};
intersectionVector.clear();
for (int i = 0; i < NB_TRIANGLE_SIDES; i++)
{
auto intrs = intersection(e1, other.edges[i]);
......@@ -110,7 +128,7 @@ std::vector<Point> contourize(const Triangle &leftmostTriangle, const Triangle &
const Edge &e2 = other.edges[edgeIndex];
const std::pair<int, int> edgePointIndexes = TriangleEdges::otherPoint(edgeIndex);
if (!e1.positiveSide(e2.p1))
if (e1.positiveSide(e2.p1))
{
prev = edgePointIndexes.first;
}
......@@ -120,7 +138,7 @@ std::vector<Point> contourize(const Triangle &leftmostTriangle, const Triangle &
}
}
} while (!(prev == leftmostInd));
} while (!(prev == leftmostInd && usingLeftTriangle));
return result;
}
......
......@@ -64,6 +64,7 @@ std::optional<Point> intersection(const Edge &e1, const Edge &e2) {
if (withinEdge(e1, candPoint) && withinEdge(e2, candPoint)) {
return candPoint;
}
return {};
}
void intersections(const Edge &e1, const TriangleEdges &te, std::vector<Point> &results) {
for (int i = 0; i < NB_TRIANGLE_SIDES; i++) {
......
......@@ -4,6 +4,9 @@ bool PointList::empty() const {
return size == 0;
}
int PointList::getSize() const {
return size;
}
int PointList::prev(const PointNode &p) const {
const int index = (p.prevIndex + size) % size;
return index;
......@@ -14,6 +17,7 @@ int PointList::next(const PointNode &p) const {
return index;
}
void PointList::remove(int i) {
if (size == 1) {
size--;
......
......@@ -30,29 +30,17 @@ std::optional<Triangle> removeEar(int &index, PointList &pointList, const std::v
const PointNode &nextNode = pointList.points[next];
const PointNode &prevNode = pointList.points[prev];
/*
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);
index = next;
return candidate;
}
index = prev;
index = next;
return {};
}
......@@ -64,13 +52,20 @@ std::vector<Triangle> triangulate(std::vector<Point> points) {
PointList polygon = PointList(points);
int i = 0;
while (!polygon.empty()) {
while (polygon.getSize() >= 4) {
std::optional<Triangle> t = removeEar(i, polygon, points);
if (t.has_value()) {
result.push_back(t.value());
}
}
const PointNode curr = polygon.points[i];
const int next = polygon.next(curr);
const int prev = polygon.prev(curr);
Triangle last {curr.p, polygon.points[next].p, polygon.points[prev].p, 0};
result.push_back(last);
return result;
......
......@@ -7,9 +7,7 @@ TEST(ContourizeTest, TwoTriangles) {
auto t1 = Triangle(Point{0,0}, Point{5,0}, Point{2,3}, 0);
auto t2 = Triangle(Point{3,1}, Point{6,1}, Point{4,3}, 0);
auto newIntersections = intersections(t1, t2);
auto contour = contourize(t1, t2, newIntersections);
auto contour = contourize(t1, t2);
EXPECT_TRUE(contour.size() > 0);
std::vector<Point> expectedContour {Point{0,0},
......@@ -29,10 +27,10 @@ TEST (ContourizeTests, TriangleSingleDipTest) {
auto newIntersections = intersections(t1, t2);
auto contour = contourize(t1, t2, newIntersections);
auto contour = contourize(t1, t2);
std::vector<Point> expectedContour{
{0,0}, {5,0}, {5,2}, {7,3}, {8,6}, {4,3}, {2,6}
{0,0}, {5,0}, {4,2}, {7,3}, {8,6}, {4,3}, {2,6}
};
EXPECT_EQ(contour, expectedContour);
......
#include <intersections.h>
#include <gtest/gtest.h>
#include <edge.h>
TEST (IntersectionTests, EdgeToEdgeTest) {
Edge e1 {{5,0}, {2,6}};
Edge e2 {{1,1}, {4,2}};
auto p = intersection(e1, e2);
Point actual_point = p.value();
Point expected_point = Point{4,2};
EXPECT_TRUE(actual_point == expected_point);
}
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