Compare commits

..

No commits in common. "a4acfdee3ca3648d9ae6314a7653f5b7a2dad5c0" and "071ff11f0df0cac2e0aa72b763735deb05a18daa" have entirely different histories.

4 changed files with 158 additions and 137 deletions

View File

@ -4,6 +4,7 @@
#include <thread> #include <thread>
#include "Display.h" #include "Display.h"
//#include "Line.h"
#include "Point.h" #include "Point.h"
#include "Utility.h" #include "Utility.h"
@ -25,14 +26,17 @@ Display::Display (const std::vector<Point> &pts, int stepSize) : m_stepSize(step
{ {
const Point& pt = pts[i]; const Point& pt = pts[i];
sf::CircleShape shape(OFFSET); sf::CircleShape shape(OFFSET);
//shape.setPosition(pt.x() - OFFSET, pt.y() - OFFSET); // handle with origin or manually calc position
shape.setOrigin(OFFSET, OFFSET); shape.setOrigin(OFFSET, OFFSET);
shape.setPosition(pt.x(), pt.y()); shape.setPosition(pt.x(), pt.y());
shape.setFillColor(sf::Color::Green); shape.setFillColor(sf::Color::Green);
shape.setOutlineThickness(1.f); shape.setOutlineThickness(1.f);
shape.setOutlineColor(sf::Color::Black); shape.setOutlineColor(sf::Color::Black);
m_points.push_back(shape); m_points.push_back(shape);
//m_points.append(shape);
sf::Text label; sf::Text label;
//label.setPosition(pt.x() - OFFSET / 2, pt.y() - OFFSET / 2 - 3);
label.setOrigin(OFFSET / 2 - 1, OFFSET / 2 + 3); label.setOrigin(OFFSET / 2 - 1, OFFSET / 2 + 3);
label.setPosition(pt.x(), pt.y()); label.setPosition(pt.x(), pt.y());
label.setFont(m_font); label.setFont(m_font);
@ -40,6 +44,7 @@ Display::Display (const std::vector<Point> &pts, int stepSize) : m_stepSize(step
label.setCharacterSize(12); label.setCharacterSize(12);
label.setFillColor(sf::Color::Black); label.setFillColor(sf::Color::Black);
m_labels.push_back(label); m_labels.push_back(label);
//m_points.append(label);
} }
} }
@ -104,11 +109,6 @@ void Display::update ()
// https://en.wikipedia.org/wiki/Convex_hull_algorithms#Akl%E2%80%93Toussaint_heuristic // https://en.wikipedia.org/wiki/Convex_hull_algorithms#Akl%E2%80%93Toussaint_heuristic
unsigned int curStep = (m_step > 5) ? ((m_step - 2) % 4 + 2) : (m_step % 6); // skip init and first step after first run unsigned int curStep = (m_step > 5) ? ((m_step - 2) % 4 + 2) : (m_step % 6); // skip init and first step after first run
if (m_stepSize == 0 && curStep > 0)
{
std::cout << "any key to continue with next step...";
std::cin.get();
}
std::string text = "(" + std::to_string(m_step) + ") step " + std::to_string(curStep) + ": "; std::string text = "(" + std::to_string(m_step) + ") step " + std::to_string(curStep) + ": ";
if (curStep == 1) if (curStep == 1)
@ -116,6 +116,15 @@ void Display::update ()
// first step: select min - max x coordinates // first step: select min - max x coordinates
m_textStatus.setString(text + "select min - max x coordinates..."); m_textStatus.setString(text + "select min - max x coordinates...");
/* EDIT: manual iteration for combining x and x minmax; also need not previous sorting
std::pair<Point, Point> minmax = getMinMaxX(m_points);
std::cout << "min: " << minmax.first.x() << ", " << minmax.first.y() <<
", max: " << minmax.second.x() << ", " << minmax.second.y() << std::endl;
m_hull.setPrimitiveType(sf::Lines);
m_hull.append(sf::Vertex(sf::Vector2f(minmax.first.x(), minmax.first.y()), sf::Color::Blue));
m_hull.append(sf::Vertex(sf::Vector2f(minmax.second.x(), minmax.second.y()), sf::Color::Blue));*/
// if use AklToussaint heuristic // if use AklToussaint heuristic
bool useAkl = false; bool useAkl = false;
if (useAkl) if (useAkl)
@ -137,6 +146,16 @@ void Display::update ()
if (x > botRight.x && y > botRight.y) botRight = pos; if (x > botRight.x && y > botRight.y) botRight = pos;
} }
/* TODO: use a convex shape? Or build from vertices in render?
sf::ConvexShape convex;
convex.setPointCount(4);
convex.setPoint(0, topLeft);
convex.setPoint(1, topRight);
convex.setPoint(2, botRight);
convex.setPoint(3, botLeft);*/
//m_ hull as VertexArray
//m_hull.setPrimitiveType(sf::Lines);
m_hull.setPrimitiveType(sf::LineStrip); m_hull.setPrimitiveType(sf::LineStrip);
m_hull.append(sf::Vertex(topLeft, sf::Color::Blue)); m_hull.append(sf::Vertex(topLeft, sf::Color::Blue));
m_hull.append(sf::Vertex(topRight, sf::Color::Blue)); m_hull.append(sf::Vertex(topRight, sf::Color::Blue));
@ -149,45 +168,35 @@ void Display::update ()
sf::Vector2f left(WIDTH, HEIGHT); sf::Vector2f left(WIDTH, HEIGHT);
sf::Vector2f right(0, 0); sf::Vector2f right(0, 0);
int i_left = WIDTH; for (auto& pt : m_points)
int i_right = 0;
size_t points = m_points.size();
for (size_t i = 0; i < points; ++i)
{ {
sf::Vector2f pos = m_points[i].getPosition(); sf::Vector2f pos = pt.getPosition();
float x = pos.x; float x = pos.x;
if (x < left.x) //float y = pos.y;
{ if (x < left.x) left = pos;
i_left = i; if (x > right.x) right = pos;
left = pos;
}
if (x > right.x)
{
i_right = i;
right = pos;
}
} }
// adding starting points from most x to left //m_ hull as VertexArray
if (i_left < i_right) //m_hull.setPrimitiveType(sf::Lines);
{ m_hull.setPrimitiveType(sf::LineStrip);
std::swap(i_left, i_right); m_hull.append(sf::Vertex(left, sf::Color::Blue));
} m_hull.append(sf::Vertex(right, sf::Color::Blue));
for (int i = 0; i < 2; ++i)
{
int pos = (i == 0) ? i_left : i_right;
m_points[pos].setFillColor(sf::Color::Blue);
m_hullPoints.push_back(m_points[pos]);
m_points.erase(m_points.begin() + pos);
}
m_lines.push_back(Line(Point(left.x, left.y), Point(right.x, right.y))); m_lines.push_back(Line(Point(left.x, left.y), Point(right.x, right.y)));
m_lines.push_back(Line(Point(right.x, right.y), Point(left.x, left.y))); // add first line in both directions to work with "right side" m_lines.push_back(Line(Point(right.x, right.y), Point(left.x, left.y))); // add first line in both directions to work with "right side"
m_curLineIdx = 1; m_curLine = &m_lines[1];
m_curLine = m_lines[1];
// TODO: split points and use map // TODO: split points and use map
m_convex.setPointCount(2);
m_convex.setFillColor(sf::Color::Transparent);
m_convex.setOutlineColor(sf::Color::Red);
m_convex.setOutlineThickness(2);
m_convex.setPoint(0, left);
m_convex.setPoint(1, right);
// TODO: could add here -> set color of points for current split
} }
} }
else if (curStep == 2) else if (curStep == 2)
@ -195,44 +204,38 @@ void Display::update ()
// second step: split board // second step: split board
m_textStatus.setString(text + "split board..."); m_textStatus.setString(text + "split board...");
// get current line with points // TODO: get current line with points
while (m_curLineIdx == -1) if (m_curLine == nullptr)
{ {
size_t lines = m_lines.size(); size_t lines = m_lines.size();
if (lines < 1) // no more open lines -> fin if (lines < 1) // no more open lines -> fin
{ {
std::cout << "## no more open lines -> fin!" << std::endl;
m_points.clear(); m_points.clear();
m_textStatus.setString(text + "finished calculating convex hull in " + std::to_string(m_step / 5) + " cycles"); m_textStatus.setString(text + "finished calculating convex hull");
return; return;
} }
Line cand = m_lines[lines - 1]; m_curLine = &m_lines[lines - 1];
int positives = 0;
for (auto& pt : m_points)
{
positives += (sign(pt.getPosition().x, pt.getPosition().y,
cand.from().x(), cand.from().y(),
cand.to().x(), cand.to().y()) > 0) ? 1 : 0;
} }
if (positives > 0) std::cout << "lines " << std::to_string(m_lines.size()) << std::endl;
for (auto& line : m_lines)
{ {
m_curLineIdx = lines - 1; std::cout << " " <<
m_curLine = m_lines[lines - 1]; std::to_string(line.from().x()) << ", " <<
} std::to_string(line.from().y()) << " - " <<
else std::to_string(line.to().x()) << ", " <<
{ std::to_string(line.to().y()) << " " << std::endl;
m_lines.pop_back();
}
} }
for (auto& pt : m_points) for (auto& pt : m_points)
{ {
pt.setFillColor(sign( pt.setFillColor(sign(
pt.getPosition().x, pt.getPosition().y, pt.getPosition().x, pt.getPosition().y,
m_lines[m_curLineIdx].from().x(), m_lines[m_curLineIdx].from().y(), //m_hull[0].position.x, m_hull[0].position.y,
m_lines[m_curLineIdx].to().x(), m_lines[m_curLineIdx].to().y()) > 0 ? sf::Color::Red : sf::Color::Green); //m_hull[1].position.x, m_hull[1].position.y) > 0 ? sf::Color::Red : sf::Color::Green);
m_curLine->from().x(), m_curLine->from().y(),
m_curLine->to().x(), m_curLine->to().y()) > 0 ? sf::Color::Red : sf::Color::Green);
} }
} }
else if (curStep == 3) else if (curStep == 3)
@ -244,40 +247,70 @@ void Display::update ()
// third step: draw triangle, remove inner points // third step: draw triangle, remove inner points
m_textStatus.setString(text + "find furthest point and draw triangle..."); m_textStatus.setString(text + "find furthest point and draw triangle...");
sf::Vector2f pos; sf::Vector2f pos = m_points[0].getPosition();
float maxDistance = 0; float maxDistance = 0;
int i_cand = 0; for (auto& pt : m_points)
size_t points = m_points.size();
for (size_t i = 0; i < points; ++i)
{ {
if (m_points[i].getFillColor() == sf::Color::Green) continue; /*
if (pt.getFillColor() == sf::Color::Green)
{
std::cout << "distance to green point " << pt.getPosition().x << ", " << pt.getPosition().y << ": " << distance << std::endl;
}
else if (pt.getFillColor() == sf::Color::Red)
{
std::cout << "distance to red point " << pt.getPosition().x << ", " << pt.getPosition().y << ": " << distance << std::endl;
}
*/
if (pt.getFillColor() == sf::Color::Green) continue;
sf::Vector2f cand = m_points[i].getPosition();
float distance = pDistance( float distance = pDistance(
cand.x, cand.y, pt.getPosition().x, pt.getPosition().y,
m_lines[m_curLineIdx].from().x(), m_lines[m_curLineIdx].from().y(), //m_hull[0].position.x, m_hull[0].position.y,
m_lines[m_curLineIdx].to().x(), m_lines[m_curLineIdx].from().y()); //m_hull[1].position.x, m_hull[1].position.y);
m_curLine->from().x(), m_curLine->from().y(),
m_curLine->to().x(), m_curLine->from().y());
if (distance > maxDistance) if (distance > maxDistance)
{ {
i_cand = i; pos = pt.getPosition();
pos = cand;
maxDistance = distance; maxDistance = distance;
} }
} }
if (maxDistance > 0) if (maxDistance > 0)
{ {
// move point from all points to hull points std::cout << "max point at " << pos.x << ", " << pos.y << std::endl;
m_points[i_cand].setFillColor(sf::Color::Blue);
m_hullPoints.push_back(m_points[i_cand]);
std::cout << "removing pt " << m_points[i_cand].getPosition().x << ", " << m_points[i_cand].getPosition().y << " at index " << std::to_string(i_cand) << " after inserted to hullpoints" << std::endl;
m_points.erase(m_points.begin() + i_cand);
Point to = m_lines[m_curLineIdx].to(); // not append but insert between last line points
Point from = m_lines[m_curLineIdx].from(); // -> append a point, then swap values
m_lines.push_back(Line(Point(pos.x, pos.y),to)); size_t vertices = m_hull.getVertexCount();
m_lines[m_lines.size() - 2] = Line(from, Point(pos.x, pos.y)); // updates list entry m_hull.append(sf::Vertex(pos, sf::Color::Blue));
m_hull[vertices] = m_hull[vertices - 1];
m_hull[vertices - 1].position = pos;
size_t points = m_convex.getPointCount();
m_convex.setPointCount(points + 1);
m_convex.setPoint(points, pos);
//m_lines.push_back(Line(m_curLine->from(), Point(pos.x, pos.y)));
//m_lines.push_back(Line(Point(pos.x, pos.y), m_curLine->to()));
m_lines.push_back(Line(Point(pos.x, pos.y), m_curLine->to()));
//m_curLine->set_to(Point(pos.x, pos.y)); // only changes the value of the pointer
//(*m_curLine).set_to(Point(pos.x, pos.y)); // doesn't update list
//(*m_curLine) = Line(m_curLine->from(), Point(pos.x, pos.y)); // doesn't update list
m_lines[m_lines.size() - 2] = Line(m_curLine->from(), Point(pos.x, pos.y)); // updates list entry
std::cout << "added to lines " << std::to_string(m_lines.size()) << std::endl;
for (auto& line : m_lines)
{
std::cout << " " <<
std::to_string(line.from().x()) << ", " <<
std::to_string(line.from().y()) << " - " <<
std::to_string(line.to().x()) << ", " <<
std::to_string(line.to().y()) << " " << std::endl;
}
} }
else else
{ {
@ -289,48 +322,39 @@ void Display::update ()
// fourth step: remove inner points // fourth step: remove inner points
m_textStatus.setString(text + "remove inner points..."); m_textStatus.setString(text + "remove inner points...");
size_t lines = m_lines.size() - 1; // TODO: wip
if (lines > 2) /*size_t lines = m_lines.size() - 1;
{
// assure clockwise order
Point pt1 = m_lines[lines - 1].from(); Point pt1 = m_lines[lines - 1].from();
Point pt2 = m_lines[lines - 1].to(); Point pt2 = m_lines[lines - 1].to();
Point pt3 = m_lines[lines].to(); Point pt3 = m_lines[lines].to();
if (sign(pt2, pt1, pt3) > 0) for (size_t i = m_points.size(); i > 0; i--)
{
std::swap(pt1, pt2);
}
size_t points = m_points.size();
for (int i = points - 1; i >= 0; i--)
{ {
Point pt(m_points[i].getPosition().x, m_points[i].getPosition().y); Point pt(m_points[i].getPosition().x, m_points[i].getPosition().y);
if (IsPointInTriangle(pt, pt1, pt2, pt3)) if (IsPointInTriangle(pt, pt1, pt2, pt3))
{ {
std::cout << "remove pt inside triangle -> " << i << " with pos: " << pt.x() << ", " << pt.y() << std::endl;
m_points.erase(m_points.begin() + i); m_points.erase(m_points.begin() + i);
} }
else }*/
{ m_curLine = nullptr;
m_points[i].setFillColor(sf::Color::Green);
}
}
}
//m_curLine = nullptr;
//m_curLineIdx = -1;
} }
else if (curStep == 5) else if (curStep == 5)
{ {
// fifth step: adding new hull point // fifth step: adding new hull point
m_curLineIdx = -1; // TEMP: TEST check if ends
//m_curLine = m_lines[lines - 1]; //if (m_step >= 10) m_points.clear();
if (m_points.size() == 0) m_textStatus.setString(text + "finished calculating convex hull in " + std::to_string(m_step/5) + " cycles"); if (m_points.size() == 0) m_textStatus.setString(text + "finished calculating convex hull");
else m_textStatus.setString(text + "adding new hull point..."); else m_textStatus.setString(text + "adding new hull point...");
} }
else if (m_step > 0) m_textStatus.setString(text + "invalid status!"); else if (m_step > 0) m_textStatus.setString(text + "invalid status!");
if (m_stepSize == 0 && curStep > 0)
{
std::cout << "any key to continue with next step...";
std::cin.get();
}
if (curStep != 5 || m_points.size() > 0) m_step++; if (curStep != 5 || m_points.size() > 0) m_step++;
} }
@ -338,41 +362,42 @@ void Display::render (sf::RenderWindow &window)
{ {
window.clear(sf::Color::White); window.clear(sf::Color::White);
// draw already calculated hull points // draw already calculated hull
size_t elements = m_hullPoints.size(); //if (step >= 1)
for (size_t i = 0; i < elements; ++i)
{ {
window.draw(m_hullPoints[i]); /*size_t vertices = m_hull.getVertexCount();
for (size_t i = 0; i < vertices; ++i)
{
std::cout << "hull pt[" << i << "] color: " << std::to_string(m_hull[i].color.toInteger()) << " >> " <<
std::to_string(m_hull[i].color.a) << ", " <<
std::to_string(m_hull[i].color.r) << ", " <<
std::to_string(m_hull[i].color.g) << ", " <<
std::to_string(m_hull[i].color.b) << std::endl;
}*/
sf::Vertex ptTo; //window.draw(&m_hull[0], m_hull.size(), sf::Lines);
if (i < elements - 1) ptTo = sf::Vertex(m_hullPoints[i + 1].getPosition(), sf::Color::Blue); window.draw(&m_hull[0], m_hull.getVertexCount(), m_hull.getPrimitiveType());
else ptTo = sf::Vertex(m_hullPoints[0].getPosition(), sf::Color::Blue);
sf::Vertex line[] = { sf::Vertex(sf::Vector2f(m_hullPoints[i].getPosition()), sf::Color::Blue), ptTo };
window.draw(line, 2, sf::Lines); // TODO: either insert points at correct location
// or only sort, then draw final hull
//window.draw(m_convex);
} }
// always print remaining points // always print remaining points
elements = m_points.size(); size_t points = m_points.size();
for (size_t i = 0; i < elements; ++i) for (size_t i = 0; i < points; ++i)
{ {
window.draw(m_points[i]); window.draw(m_points[i]);
}
// seperately print labels for points
elements = m_labels.size();
for (size_t i = 0; i < elements; ++i)
{
window.draw(m_labels[i]); window.draw(m_labels[i]);
} }
// draw line the algorithm is currently working on // draw line the algorithm is currently working on
if (m_curLineIdx != -1) if (m_curLine != nullptr)
{ {
sf::Vertex line[] = sf::Vertex line[] =
{ {
sf::Vertex(sf::Vector2f(m_curLine.from().x(), m_curLine.from().y()), sf::Color::Red), sf::Vertex(sf::Vector2f(m_curLine->from().x(), m_curLine->from().y()), sf::Color::Red),
sf::Vertex(sf::Vector2f(m_curLine.to().x(), m_curLine.to().y()), sf::Color::Red) sf::Vertex(sf::Vector2f(m_curLine->to().x(), m_curLine->to().y()), sf::Color::Red)
}; };
window.draw(line, 2, sf::Lines); window.draw(line, 2, sf::Lines);
} }

View File

@ -5,7 +5,6 @@
// linux - sudo apt-get install libsfml-dev // linux - sudo apt-get install libsfml-dev
// windows - manual dl from https://www.sfml-dev.org/download.php // windows - manual dl from https://www.sfml-dev.org/download.php
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
//#include <list>
#include "Line.h" #include "Line.h"
@ -30,16 +29,12 @@ private:
//sf::VertexArray m_labels; //sf::VertexArray m_labels;
//std::vector<sf::Vertex> m_hull; //std::vector<sf::Vertex> m_hull;
std::vector<sf::CircleShape> m_hullPoints;
sf::VertexArray m_hull; sf::VertexArray m_hull;
//sf::ConvexShape m_convex; // shape for convex hull -> does not fit because points have to be aligned already sf::ConvexShape m_convex;
//std::map<Line, std::vector<Point> > m_pointsForLine; //std::map<Line, std::vector<Point> > m_pointsForLine;
std::vector<Line> m_lines; std::vector<Line> m_lines;
//std::list<Line> m_lines; Line *m_curLine = nullptr;
//Line *m_curLine = nullptr; // get any value if the underlying vector gets resized and moved in memory
Line m_curLine; // -> either make a copy
size_t m_curLineIdx = -1; // or just use index
unsigned int m_step = 0; unsigned int m_step = 0;
void update(); void update();

View File

@ -6,9 +6,9 @@ static float sign(float x, float y, float x1, float y1, float x2, float y2)
return (x - x2) * (y1 - y2) - (x1 - x2) * (y - y2); return (x - x2) * (y1 - y2) - (x1 - x2) * (y - y2);
} }
static float sign (const Point &pt, const Point &p1, const Point &p2) static float sign (const Point &p1, const Point &p2, const Point &p3)
{ {
return (pt.x() - p2.x()) * (p1.y() - p2.y()) - (p1.x() - p2.x()) * (pt.y() - p2.y()); return (p1.x() - p3.x()) * (p2.y() - p3.y()) - (p2.x() - p3.x()) * (p1.y() - p3.y());
} }
static bool IsPointInTriangle(const Point &pt, const Point &p1, const Point &p2, const Point &p3) static bool IsPointInTriangle(const Point &pt, const Point &p1, const Point &p2, const Point &p3)

View File

@ -102,7 +102,8 @@ int main (int argc, char **argv)
} }
} }
// debug print points // TODO: sort here, once and for all? xD
//sortPoints(points);
for (Point& pt : points) for (Point& pt : points)
{ {
std::cout << "pt: " << pt.x() << ", " << pt.y() << std::endl; std::cout << "pt: " << pt.x() << ", " << pt.y() << std::endl;