From e645b307a7a32c3c3d12989ccf6d60e324392d60 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Sun, 1 Feb 2015 14:57:56 +0100 Subject: Begin work on UI --- Makefile | 9 ++++-- main.cpp | 13 ++++++-- problem.cpp | 8 ++++- problem.hpp | 50 +++++++++++++++++++++++------ ui.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ui.hpp | 64 +++++++++++++++++++++++++++++++++++++ 6 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 ui.cpp create mode 100644 ui.hpp diff --git a/Makefile b/Makefile index 30369a0..4a1dfb4 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,9 @@ BIN=pathfind -$(BIN): *.cpp *.hpp - g++ -o $@ *.cpp -lm -O2 -std=c++11 +OBJ=problem.o ui.o main.o + +$(BIN): *.hpp $(OBJ) + g++ -o $@ $(OBJ) -lm -lsfml-system -lsfml-window -lsfml-graphics -O2 -std=c++11 + +%.o: %.cpp *.hpp + g++ -c -o $@ $< -std=c++11 -O2 diff --git a/main.cpp b/main.cpp index 31616fa..d1fa143 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,17 @@ #include -#include "geom.hpp" -#include "problem.hpp" +#include "ui.hpp" int main() { - printf("%.10f\n", M_PI); + hilare_a_param p; + p.l = 30; + p.r_c_car = 15; + p.r_c_trolley = 12; + + UI the_ui(&p); + + the_ui.run(); + return 0; } diff --git a/problem.cpp b/problem.cpp index c196879..25061c5 100644 --- a/problem.cpp +++ b/problem.cpp @@ -17,7 +17,7 @@ bool hilare_a_mvt::intersects(const obstacle &o) const { } bool hilare_a_mvt::intersects(const problem &p) const { - for (auto i = p.map.begin(); i != p.map.end(); i++) { + for (auto i = p.obstacles.begin(); i != p.obstacles.end(); i++) { if (intersects(*i)) return true; } return false; @@ -32,4 +32,10 @@ solution solution::direct_sol(const hilare_a &pos_a, const hilare_a &pos_b) { return solution(vector()); } +// =============================== // +// IMPLEMENTATION FOR CLASS SOLVER // +// =============================== // + +// TODO + /* vim: set ts=4 sw=4 tw=0 noet :*/ diff --git a/problem.hpp b/problem.hpp index 7097e0c..a41e47a 100644 --- a/problem.hpp +++ b/problem.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include "geom.hpp" @@ -8,10 +9,14 @@ struct obstacle { circle c; }; -struct hilare_a { // System A +struct hilare_a_param { // paramètres globaux double l; double r_c_car, r_c_trolley; +}; + +struct hilare_a { // System A + hilare_a_param *param; // position actuelle double x, y, theta, phi; @@ -19,16 +24,16 @@ struct hilare_a { // System A vec pos() const { return vec(x, y); } vec dir() const { return vec::from_polar(1, theta); } - vec pos_trolley() const { - return pos() + vec::from_polar(l, theta + phi + M_PI); - } vec dir_trolley() const { - return vec::from_polar(1, theta + phi); + return vec::from_polar(1, theta + phi + M_PI); + } + vec pos_trolley() const { + return pos() + param->l * dir_trolley(); } }; struct problem { - std::vector map; + std::vector obstacles; hilare_a begin_pos, end_pos; }; @@ -45,10 +50,18 @@ struct hilare_a_mvt { // - avancer/reculer sur le cercle (c'est l'angle domega) hilare_a from, to; - vec center; + + bool is_arc; // true = circle arc ; false = straight line (phi = 0) + double dtheta_before; // rotation de la voiture sur elle-même avant + + // CAS D'UN ARC DE CERCLE + vec center; double domega; // angle parcouru sur le cercle de centre center + // CAS D'UN DEPLACEMENT EN LIGNE DROITE + double ds; // longueur par + double length(); // length of a movement bool intersects(const obstacle& o) const; // intersects an obstacle ? @@ -57,6 +70,7 @@ struct hilare_a_mvt { struct solution { std::vector movement; + solution() {} solution(const std::vector &m) : movement(m) {} // simple direct solution from A to B, takes into account no obstacles @@ -64,9 +78,27 @@ struct solution { // check if a solution intersects an obstacle of problem bool intersects(const problem &p) const; +}; + +struct solver_internal { + // intermediate data for the solver + // represents a graph of randomly chosen positions and simple solutions between them + std::vector pts; + std::map > paths; +}; + +class solver { + // mutex-protected asynchronous structure + + private: + //todo + + public: + solver(); - // calculate a solution - static solution make_solution(const problem &p); + void start(const problem &p); + bool finished(); + solution get_solution(); }; diff --git a/ui.cpp b/ui.cpp new file mode 100644 index 0000000..23a2b48 --- /dev/null +++ b/ui.cpp @@ -0,0 +1,104 @@ +#include "ui.hpp" + +/* + All modes : + - h, j, k, l : move around + - i, o : zoom in/out + +*/ + +UI::UI(hilare_a_param *p) { + _param = p; + + _p.begin_pos.param = _p.end_pos.param = p; + + _view.x0 = _view.y0 = 0; + _view.zoom = 1; +} + +void UI::run() { + _settings.antialiasingLevel = 8; + + _win.create(sf::VideoMode(800, 600), "PR", sf::Style::Default, _settings); + _win.setVerticalSyncEnabled(true); + _win.setFramerateLimit(30); + + while (_win.isOpen()) { + sf::Event ev; + while (_win.pollEvent(ev)) { + if (ev.type == sf::Event::Closed) + _win.close(); + + if (ev.type == sf::Event::TextEntered) { + int k = ev.text.unicode; + + // TODO + } + } + + _win.clear(sf::Color::Black); + + render_internal(); + render_solution(); + render_problem(); + + _win.display(); + } +} + +void UI::render_circle(const circle &c, sf::Color border, sf::Color inside, int w) { + double r = c.r * _view.zoom; + sf::CircleShape a(r); + a.setPosition(to_view(c.c)); + a.move(-r, -r); + a.setFillColor(inside); + a.setOutlineColor(border); + a.setOutlineThickness(w); + _win.draw(a); +} + +void UI::render_pos(const hilare_a &pos, sf::Color c) { + render_circle(circle(pos.pos(), pos.param->r_c_car), c, sf::Color::Transparent, 2); + + render_circle(circle(pos.pos_trolley(), pos.param->r_c_trolley), c, sf::Color::Transparent, 2); +} + +void UI::render_obstacle(const obstacle &o) { + render_circle(o.c, sf::Color::Transparent, sf::Color(100, 100, 100), 0); +} + +void UI::render_mvt(const hilare_a_mvt &m) { + // TODO +} + +void UI::render_problem() { + render_pos(_p.begin_pos, sf::Color::Blue); + render_pos(_p.end_pos, sf::Color::Red); + + for (auto i = _p.obstacles.begin(); i != _p.obstacles.end(); i++) { + render_obstacle(*i); + } +} + +void UI::render_solution() { + for (auto i = _s.movement.begin(); i != _s.movement.end(); i++) { + if (i != _s.movement.begin()) + render_pos(i->from, sf::Color::Green); + render_mvt(*i); + } +} + +void UI::render_internal() { + // TODO +} + +sf::Vector2f UI::to_view(const vec &p) { + return sf::Vector2f((p.x - _view.x0) * _view.zoom, (p.y - _view.y0) * _view.zoom); +} + +vec UI::from_view(const sf::Vector2f &p) { + return vec(p.x / _view.zoom + _view.x0, p.y / _view.zoom + _view.y0); +} + +/* vim: set ts=4 sw=4 tw=0 noet :*/ + diff --git a/ui.hpp b/ui.hpp new file mode 100644 index 0000000..552dfa2 --- /dev/null +++ b/ui.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include + +#include +#include + +#include "problem.hpp" + +enum { + M_NORMAL, + M_INS_OBSTACLE, + M_DEL_OBSTACLE, + M_SEL_BEGIN, + M_SEL_END +}; + +enum { + S_XY, + S_THETA, + S_PHI, + S_CENTER, + S_RADIUS +}; + +class UI { + private: + hilare_a_param *_param; + + problem _p; + + solution _s; + + struct { + double x0, y0, zoom; + } _view; + + sf::ContextSettings _settings; + sf::RenderWindow _win; + + // interaction mode + int _mode, _sel_step; + + public: + UI(hilare_a_param *p); + + void render_circle(const circle& c, sf::Color border, sf::Color inside, int linewidth); + void render_pos(const hilare_a &pos, sf::Color c); + void render_obstacle(const obstacle &o); + void render_mvt(const hilare_a_mvt &m); + + void render_problem(); + void render_solution(); + void render_internal(); + + sf::Vector2f to_view(const vec &p); + vec from_view(const sf::Vector2f &p); + + void run(); +}; + + +/* vim: set ts=4 sw=4 tw=0 noet :*/ + -- cgit v1.2.3