replace GraphEdge with cost function
This commit is contained in:
parent
c2f500d3b5
commit
01c14fefe3
@ -19,14 +19,14 @@ Engine::Engine() :
|
|||||||
m_clock = new sf::Clock();
|
m_clock = new sf::Clock();
|
||||||
m_clock->restart();
|
m_clock->restart();
|
||||||
m_renderer = new SparrowRenderer();
|
m_renderer = new SparrowRenderer();
|
||||||
m_renderer->setCamera(NULL);
|
//m_renderer->setCamera(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
Engine::~Engine()
|
Engine::~Engine()
|
||||||
{
|
{
|
||||||
delete m_clock;
|
delete m_clock;
|
||||||
if(m_window != NULL)
|
if(m_window != NULL)
|
||||||
m_renderer->destroyGL();
|
// m_renderer->destroyGL();
|
||||||
delete m_renderer;
|
delete m_renderer;
|
||||||
if(m_window != NULL)
|
if(m_window != NULL)
|
||||||
{
|
{
|
||||||
|
14
src/main.cpp
14
src/main.cpp
@ -16,7 +16,7 @@ int main(){
|
|||||||
scene.addChild("camera");
|
scene.addChild("camera");
|
||||||
|
|
||||||
Engine engine;
|
Engine engine;
|
||||||
engine.getRenderer()->setCamera(&camera);
|
//engine.getRenderer()->setCamera(&camera);
|
||||||
engine.createWindow("test");
|
engine.createWindow("test");
|
||||||
engine.setScene("testScene");
|
engine.setScene("testScene");
|
||||||
engine.start();
|
engine.start();
|
||||||
@ -32,12 +32,12 @@ int main(){
|
|||||||
GraphNode n5 = GraphNode();
|
GraphNode n5 = GraphNode();
|
||||||
n5.setValue(5);
|
n5.setValue(5);
|
||||||
|
|
||||||
n1.addNeighbours(&n2,6);
|
n1.addNeighbours(&n2);
|
||||||
n1.addNeighbours(&n3,3);
|
n1.addNeighbours(&n3);
|
||||||
n2.addNeighbours(&n4,2);
|
n2.addNeighbours(&n4);
|
||||||
n3.addNeighbours(&n4,20);
|
n3.addNeighbours(&n4);
|
||||||
n3.addNeighbours(&n2,2);
|
n3.addNeighbours(&n2);
|
||||||
n3.addNeighbours(&n5,10);
|
n3.addNeighbours(&n5);
|
||||||
|
|
||||||
std::vector<GraphNode*> path = PathFinder::a_star(&n1,&n4,true);
|
std::vector<GraphNode*> path = PathFinder::a_star(&n1,&n4,true);
|
||||||
std::cout << "Path Size: " << path.size() << std::endl;
|
std::cout << "Path Size: " << path.size() << std::endl;
|
||||||
|
@ -5,27 +5,22 @@ using namespace std;
|
|||||||
|
|
||||||
GraphNode::GraphNode()
|
GraphNode::GraphNode()
|
||||||
{
|
{
|
||||||
m_neighbours = vector<GraphEdge>();
|
m_neighbours = vector<GraphNode*>();
|
||||||
}
|
}
|
||||||
|
|
||||||
GraphNode::GraphNode(vector<GraphEdge> neighbours):m_neighbours(neighbours)
|
GraphNode::GraphNode(vector<GraphNode*> neighbours):m_neighbours(neighbours)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphNode::addNeighbours(GraphNode* n,float f){
|
void GraphNode::addNeighbours(GraphNode* node){
|
||||||
GraphEdge edge = GraphEdge(n,f);
|
m_neighbours.push_back(node);
|
||||||
addNeighbours(edge);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GraphNode::addNeighbours(GraphEdge edge){
|
|
||||||
m_neighbours.push_back(edge);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int GraphNode::getNbNeighbours(){
|
int GraphNode::getNbNeighbours(){
|
||||||
return m_neighbours.size();
|
return m_neighbours.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<GraphEdge> GraphNode::getNeighbours(){
|
vector<GraphNode*> GraphNode::getNeighbours(){
|
||||||
return m_neighbours;
|
return m_neighbours;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,11 +28,6 @@ void GraphNode::print(std::string prefix)
|
|||||||
{
|
{
|
||||||
cout << prefix << "Node_id : ";
|
cout << prefix << "Node_id : ";
|
||||||
cout << prefix << m_testvalue << endl;
|
cout << prefix << m_testvalue << endl;
|
||||||
// cout << prefix << "Neighbours list :" << endl;
|
|
||||||
//prefix += "\t";
|
|
||||||
//for_each (m_neighbours.begin(), m_neighbours.end(),[prefix](GraphEdge voisin) {
|
|
||||||
// voisin.getTarget()->print(prefix);
|
|
||||||
//});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphNode::setValue(int a){
|
void GraphNode::setValue(int a){
|
||||||
@ -48,22 +38,11 @@ int GraphNode::getValue(){
|
|||||||
return m_testvalue;
|
return m_testvalue;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GraphNode::setPriority(float a){
|
void GraphNode::setPriority(float priority){
|
||||||
m_priority = a;
|
m_priority = priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
float GraphNode::getPriority(){
|
float GraphNode::getPriority(){
|
||||||
return m_priority;
|
return m_priority;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
GraphEdge::GraphEdge(GraphNode* gn,float v):m_target(gn),m_weight(v)
|
|
||||||
{}
|
|
||||||
|
|
||||||
GraphNode* GraphEdge::getTarget(){
|
|
||||||
return m_target;
|
|
||||||
}
|
|
||||||
|
|
||||||
float GraphEdge::getWeight(){
|
|
||||||
return m_weight;
|
|
||||||
}
|
|
||||||
|
@ -8,22 +8,25 @@ class GraphEdge;
|
|||||||
|
|
||||||
class GraphNode
|
class GraphNode
|
||||||
{
|
{
|
||||||
std::vector<GraphEdge> m_neighbours;
|
std::vector<GraphNode*> m_neighbours;
|
||||||
int m_testvalue; //temp variable
|
int m_testvalue; //temp variable
|
||||||
float m_priority;
|
float m_priority;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GraphNode();
|
GraphNode();
|
||||||
GraphNode(std::vector<GraphEdge>);
|
GraphNode(std::vector<GraphNode*>);
|
||||||
|
|
||||||
std::vector<GraphEdge> getNeighbours();
|
std::vector<GraphNode*> getNeighbours();
|
||||||
void addNeighbours(GraphNode*,float);
|
void addNeighbours(GraphNode*);
|
||||||
void addNeighbours(GraphEdge);
|
|
||||||
int getNbNeighbours();
|
int getNbNeighbours();
|
||||||
|
virtual float cost(GraphNode*){
|
||||||
|
std::cout << "WARNING : You're using the default cost function. For better result, redefine the cost function in GraphNode." << std::endl;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
virtual float heuristic(GraphNode*){
|
virtual float heuristic(GraphNode*){
|
||||||
std::cout << "WARNING : You're using the default heuristic. For better result, redefine the heuristic function in GraphNode." << std::endl;
|
std::cout << "WARNING : You're using the default heuristic function. For better result, redefine the heuristic function in GraphNode." << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
};
|
}
|
||||||
|
|
||||||
void setValue(int);
|
void setValue(int);
|
||||||
int getValue();
|
int getValue();
|
||||||
@ -32,15 +35,4 @@ public:
|
|||||||
void print(std::string prefix);
|
void print(std::string prefix);
|
||||||
};
|
};
|
||||||
|
|
||||||
class GraphEdge
|
|
||||||
{
|
|
||||||
GraphNode* m_target;
|
|
||||||
float m_weight;
|
|
||||||
|
|
||||||
public:
|
|
||||||
GraphEdge(GraphNode*,float);
|
|
||||||
GraphNode* getTarget();
|
|
||||||
float getWeight();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // GRAPH_H
|
#endif // GRAPH_H
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include "loader.h"
|
#include "loader.h"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <streambuf>
|
#include <streambuf>
|
||||||
|
#include <cstring>
|
||||||
#include <image.h>
|
#include <image.h>
|
||||||
#include <SFML/Graphics/Image.hpp>
|
#include <SFML/Graphics/Image.hpp>
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
PathFinder::PathFinder(){
|
PathFinder::PathFinder(){
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<GraphNode*> PathFinder::a_star(GraphNode* start,GraphNode* goal,bool debug)
|
std::vector<GraphNode*> PathFinder::a_star(GraphNode* start, GraphNode* goal, bool debug)
|
||||||
{
|
{
|
||||||
// Check if priorityqueue sort value in right order.
|
// Check if priorityqueue sort value in right order.
|
||||||
std::priority_queue<GraphNode*,std::vector<GraphNode*>,ComparePriority> frontier = std::priority_queue<GraphNode*,std::vector<GraphNode*>,ComparePriority>();
|
std::priority_queue<GraphNode*,std::vector<GraphNode*>,ComparePriority> frontier = std::priority_queue<GraphNode*,std::vector<GraphNode*>,ComparePriority>();
|
||||||
@ -42,22 +42,22 @@ std::vector<GraphNode*> PathFinder::a_star(GraphNode* start,GraphNode* goal,bool
|
|||||||
}
|
}
|
||||||
|
|
||||||
// for all neighbours of current node
|
// for all neighbours of current node
|
||||||
for (GraphEdge next : current->getNeighbours()){
|
for (GraphNode* next : current->getNeighbours()){
|
||||||
float new_cost = cost[current] + next.getWeight();
|
float new_cost = cost[current] + current->cost(next);
|
||||||
if(debug) std::cout << "\tExploring neighbours node " << next.getTarget()->getValue() << " with cost " << new_cost << std::endl;
|
if(debug) std::cout << "\tExploring neighbours node " << next->getValue() << " with cost " << new_cost << std::endl;
|
||||||
|
|
||||||
if ((cost.count(next.getTarget()) == 0) || (new_cost < cost[next.getTarget()])){
|
if ((cost.count(next) == 0) || (new_cost < cost[next])){
|
||||||
// affect processed cost to next node in cost_map
|
// affect processed cost to next node in cost_map
|
||||||
cost[next.getTarget()] = new_cost;
|
cost[next] = new_cost;
|
||||||
|
|
||||||
// calcul priority of node with heuristic,and add it to frontier
|
// calcul priority of node with heuristic,and add it to frontier
|
||||||
float priority = new_cost + next.getTarget()->heuristic(goal);
|
float priority = new_cost + next->heuristic(goal);
|
||||||
if(debug) std::cout << "\t\t Priority: " << priority << std::endl;
|
if(debug) std::cout << "\t\t Priority: " << priority << std::endl;
|
||||||
next.getTarget()->setPriority(priority);
|
next->setPriority(priority);
|
||||||
visited.erase(next.getTarget());
|
visited.erase(next);
|
||||||
frontier.push(next.getTarget());
|
frontier.push(next);
|
||||||
// memorize predecessor for next
|
// memorize predecessor for next
|
||||||
pred[next.getTarget()] = current;
|
pred[next] = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user