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

implemented contouring

parent 7dedd049
No related branches found
No related tags found
No related merge requests found
...@@ -26,6 +26,9 @@ add_executable( ...@@ -26,6 +26,9 @@ add_executable(
src/shapes/triangle_edges.cpp src/shapes/triangle_edges.cpp
src/contourize/contourize.cpp src/contourize/contourize.cpp
src/shapes/edge.cpp src/shapes/edge.cpp
src/orientation/orientation.cpp
src/shapes/point.cpp
src/union.cpp
) )
include_directories(src) include_directories(src)
......
#include <shapes/triangle.h> #include <shapes/triangle.h>
#include <vector> #include <vector>
#include "contourize.h" #include "contourize.h"
#include <orientation/orientation.h>
std::vector<Point> sortPoints(std::vector<Point> &points) { Triangle clockwiseToCounterclockwise(const Triangle &t) {
return Triangle(t.points[0], t.points[2], t.points[1], t.depth);
}
//
bool isCounterClockwiseForAll(const Point &previous, const Point &candidate, const std::vector<Point> & points) {
for (const auto &point : points) {
if (previous == point || candidate == point) {
continue;
}
if (orientation(previous, candidate, point) != Counterclockwise) {
return false;
}
}
return true;
}
Point nextPoint(const Point &previous, std::vector<Point> candidates, const std::vector<Point> points) {
for (int i = 0; i < candidates.size(); i++) {
if (isCounterClockwiseForAll(previous, candidates[i], points)) {
Point p = candidates[i];
candidates.erase(i + candidates.begin());
return p;
}
}
}
std::vector<Point> sortPoints(const Triangle &t1, const Triangle &t2, const std::vector<Point> points) {
std::vector<Point> result;
const int NUM_TRIANGLE_POINTS = 3;
// using convex hull algorithm
// start at leftmost point
Point minX = t1.points[0];
for (int i = 1; i < NUM_TRIANGLE_POINTS; i++) {
if (t1.points[i].x < minX.x) {
minX = t1.points[i];
}
}
for (int i = 0; i < NUM_TRIANGLE_POINTS; i++) {
if (t2. points[i].x < minX.x) {
minX = t2.points[i];
}
}
// filter out points
std::vector<Point> candidates;
for (int i = 1; i < NUM_TRIANGLE_POINTS; i++) {
if (!(t1.points[i] == minX)) {
candidates.push_back(t1.points[i]);
}
}
for (int i = 0; i < NUM_TRIANGLE_POINTS; i++) {
if (t2.points[i] != minX) {
candidates.push_back(t2.points[i]);
}
}
Point previous = minX;
for (int i = 0; i < candidates.size(); i++) {
nextPoint(previous, candidates, points);
}
} }
......
#include "orientation.h"
#include <shapes/point.h>
Orientation orientation(const Point &p1, const Point &p2, const Point &p3) {
int val = (p2.y - p1.y) * (p3.x - p2.x) - (p2.x - p1.x) * (p3.y - p2.y);
if (val == 0) {
return Collinear;
}
return (val > 0) ? Clockwise : Counterclockwise;
}
\ No newline at end of file
#pragma once
struct Point;
enum Orientation {
Clockwise, Collinear, Counterclockwise
};
Orientation orientation(const Point &p1, const Point &p2, const Point &p3);
\ No newline at end of file
#include "point.h"
bool Point::operator==(const Point &other) const {
return x == other.x && y == other.y;
}
\ No newline at end of file
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
struct Point { struct Point {
float x, y; float x, y;
bool operator==(const Point &other) const;
}; };
\ No newline at end of file
#include "triangulation.h"
std::vector<Triangle> triangulate(std::vector<Point> &points) {
}
\ No newline at end of file
#pragma once #pragma once
#include <vector>
struct Triangle;
struct Point;
std::vector<Triangle> triangulate(std::vector<Point> &points);
\ No newline at end of file
#include "gtest/gtest.h"
...@@ -12,3 +12,4 @@ TEST (TriangleIntersectionTests, TriangleIntersection) { ...@@ -12,3 +12,4 @@ TEST (TriangleIntersectionTests, TriangleIntersection) {
} }
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