Version: 9.16.0
YACS::HMI::LinkAStar Class Reference

#include <LinkAStar.hxx>

Collaboration diagram for YACS::HMI::LinkAStar:

Public Member Functions

 LinkAStar (const LinkMatrix &linkMatrix)
 
 ~LinkAStar ()
 
bool computePath (LNode from, LNode to)
 
LNodePath givePath ()
 
bool isAlreadyInList (std::pair< int, int > n, const LNodeMap &aList)
 
void addNeighbours (std::pair< int, int > n)
 
std::pair< int, int > bestNode (const LNodeMap &aList)
 
void moveToClosedList (std::pair< int, int > n)
 
double distance (int i1, int j1, int i2, int j2)
 

Protected Attributes

const LinkMatrix_linkMatrix
 
LNodeMap _closedList
 
LNodeMap _openList
 
LNode _from
 
LNode _to
 
std::priority_queue< Cost_pq
 

Detailed Description

Definition at line 68 of file LinkAStar.hxx.

Constructor & Destructor Documentation

◆ LinkAStar()

LinkAStar::LinkAStar ( const LinkMatrix linkMatrix)

Definition at line 47 of file LinkAStar.cxx.

47 : _linkMatrix(linkMatrix), _from(0,0), _to(0,0)
48{
49 _closedList.clear();
50 _openList.clear();
51 _pq=std::priority_queue<Cost>();
52}
const LinkMatrix & _linkMatrix
Definition: LinkAStar.hxx:85
std::priority_queue< Cost > _pq
Definition: LinkAStar.hxx:90

References _closedList, _openList, and _pq.

◆ ~LinkAStar()

LinkAStar::~LinkAStar ( )

Definition at line 54 of file LinkAStar.cxx.

55{
56}

Member Function Documentation

◆ addNeighbours()

void LinkAStar::addNeighbours ( std::pair< int, int >  n)

Definition at line 118 of file LinkAStar.cxx.

119{
120 LCostNode tmp(currentNode);
121 int x = currentNode.first;
122 int y = currentNode.second;
123 for (int i = x-1; i <= x+1; i++)
124 {
125 if ((i<0) || (i >= _linkMatrix.imax()))
126 continue; // --- skip: outside matrix
127 for (int j = y-1; j <= y+1; j++)
128 {
129 if ((j<0) || (j >= _linkMatrix.jmax()))
130 continue; // --- skip: outside matrix
131
132 if ((i == x) && (j == y))
133 continue; // --- skip: current node
134
135 if ((i != x) && (j != y))
136 continue; // --- skip: diagonals (move only vertical or horizontal)
137
138 int cost = _linkMatrix.cost(i,j);
139 if (! cost)
140 continue; // --- skip: blocked
141
142 pair<int,int> pos(i,j);
144 continue; // --- skip: already in closed list
145
146 tmp.setGCost(_closedList[currentNode].getGCost() + cost*distance(x, y, i, j));
147 tmp.setHCost(distance(i, j, _to.getX(), _to.getY()));
148 tmp.setFCost(tmp.getGCost() + tmp.getHCost());
149 if (isAlreadyInList(pos, _openList))
150 {
151 if (tmp.getFCost() < _openList[pos].getFCost())
152 {
153 _openList[pos] = tmp; // --- new path better, update node
154 _pq.push(Cost(tmp.getFCost(),pos));
155 }
156 }
157 else
158 {
159 _openList[pos] = tmp; // --- add node
160 _pq.push(Cost(tmp.getFCost(),pos));
161 }
162 }
163 }
164}
int getX() const
Definition: LinkMatrix.hxx:52
int getY() const
Definition: LinkMatrix.hxx:53
bool isAlreadyInList(std::pair< int, int > n, const LNodeMap &aList)
Definition: LinkAStar.cxx:109
double distance(int i1, int j1, int i2, int j2)
Definition: LinkAStar.hxx:83
int cost(int i, int j) const
Definition: LinkMatrix.hxx:98

References _closedList, _linkMatrix, _openList, _pq, _to, YACS::HMI::LinkMatrix::cost(), distance(), YACS::HMI::LCostNode::getFCost(), YACS::HMI::LCostNode::getGCost(), YACS::HMI::LCostNode::getHCost(), YACS::HMI::LNode::getX(), YACS::HMI::LNode::getY(), yacsorb.CORBAEngineTest::i, YACS::HMI::LinkMatrix::imax(), isAlreadyInList(), YACS::HMI::LinkMatrix::jmax(), YACS::HMI::LCostNode::setFCost(), YACS::HMI::LCostNode::setGCost(), and YACS::HMI::LCostNode::setHCost().

Referenced by computePath().

◆ bestNode()

std::pair< int, int > LinkAStar::bestNode ( const LNodeMap aList)

Definition at line 166 of file LinkAStar.cxx.

167{
168 std::pair<int, int> pos;
169 do
170 {
171 pos=_pq.top().pos;
172 _pq.pop();
173 }
174 while(aList.count(pos)==0);
175 return pos;
176}

References _pq.

Referenced by computePath().

◆ computePath()

bool LinkAStar::computePath ( LNode  from,
LNode  to 
)

Definition at line 58 of file LinkAStar.cxx.

59{
60 _closedList.clear();
61 _openList.clear();
62 _pq=std::priority_queue<Cost>();
63 _from = from;
64 _to = to;
65 bool isPath = false;
66
67 //pair<int, int> curPos(0, 0);
68 //LCostNode startCost(curPos);
69
70 pair<int, int> curPos = _from.getPos();
71 LCostNode startCost(curPos);
72 _openList[curPos] = startCost;
73 moveToClosedList(curPos);
74 addNeighbours(curPos);
75
76 while (! ((curPos.first == _to.getX()) && (curPos.second == _to.getY()))
77 && (!_openList.empty()))
78 {
79 curPos = bestNode(_openList);
80 moveToClosedList(curPos);
81 DEBTRACE("curPos(" << curPos.first << "," << curPos.second << ")");
82 addNeighbours(curPos);
83 }
84
85 if ((curPos.first == _to.getX()) && (curPos.second == _to.getY()))
86 isPath = true;
87
88 return isPath;
89}
#define DEBTRACE(msg)
Definition: YacsTrace.hxx:31
std::pair< int, int > getPos() const
Definition: LinkMatrix.hxx:54
std::pair< int, int > bestNode(const LNodeMap &aList)
Definition: LinkAStar.cxx:166
void moveToClosedList(std::pair< int, int > n)
Definition: LinkAStar.cxx:178
void addNeighbours(std::pair< int, int > n)
Definition: LinkAStar.cxx:118

References _closedList, _from, _openList, _pq, _to, addNeighbours(), bestNode(), DEBTRACE, YACS::HMI::LNode::getPos(), YACS::HMI::LNode::getX(), YACS::HMI::LNode::getY(), and moveToClosedList().

Referenced by YACS::HMI::SceneComposedNodeItem::rebuildLinks().

◆ distance()

double YACS::HMI::LinkAStar::distance ( int  i1,
int  j1,
int  i2,
int  j2 
)
inline

Definition at line 83 of file LinkAStar.hxx.

83{ return abs(i1-i2)+abs(j1-j2);};

References testCppPluginInvokation::i1.

Referenced by addNeighbours().

◆ givePath()

LNodePath LinkAStar::givePath ( )

Definition at line 91 of file LinkAStar.cxx.

92{
93 LNodePath aPath;
94 aPath.clear();
95 aPath.push_front(_to);
96
97 LNode current = _to;
98 while (! current.isEqual(_from))
99 {
100 current = LNode(_closedList[current.getPos()].getParent());
101 aPath.push_front(current);
102 DEBTRACE("(" << current.getX() << "," << current.getY() << ")");
103 }
104// aPath.push_front(_from);
105// DEBTRACE("(" << _from.getX() << "," << _from.getY() << ")");
106 return aPath;
107}
bool isEqual(const LNode &o) const
Definition: LinkMatrix.hxx:55
std::list< LNode > LNodePath
Definition: LinkMatrix.hxx:62

References _closedList, _from, _to, DEBTRACE, YACS::HMI::LNode::getPos(), YACS::HMI::LNode::getX(), YACS::HMI::LNode::getY(), and YACS::HMI::LNode::isEqual().

Referenced by YACS::HMI::SceneComposedNodeItem::rebuildLinks().

◆ isAlreadyInList()

bool LinkAStar::isAlreadyInList ( std::pair< int, int >  n,
const LNodeMap aList 
)

Definition at line 109 of file LinkAStar.cxx.

110{
111 LNodeMap::const_iterator it = aList.find(n);
112 if (it == aList.end())
113 return false;
114 else
115 return true;
116}

References gui.Appli::n.

Referenced by addNeighbours().

◆ moveToClosedList()

void LinkAStar::moveToClosedList ( std::pair< int, int >  n)

Definition at line 178 of file LinkAStar.cxx.

179{
180 _closedList[pos] = _openList[pos];
181 if (_openList.erase(pos) == 0)
182 DEBTRACE("node not in open list, can't delete");
183}

References _closedList, _openList, and DEBTRACE.

Referenced by computePath().

Member Data Documentation

◆ _closedList

LNodeMap YACS::HMI::LinkAStar::_closedList
protected

Definition at line 86 of file LinkAStar.hxx.

Referenced by addNeighbours(), computePath(), givePath(), LinkAStar(), and moveToClosedList().

◆ _from

LNode YACS::HMI::LinkAStar::_from
protected

Definition at line 88 of file LinkAStar.hxx.

Referenced by computePath(), and givePath().

◆ _linkMatrix

const LinkMatrix& YACS::HMI::LinkAStar::_linkMatrix
protected

Definition at line 85 of file LinkAStar.hxx.

Referenced by addNeighbours().

◆ _openList

LNodeMap YACS::HMI::LinkAStar::_openList
protected

Definition at line 87 of file LinkAStar.hxx.

Referenced by addNeighbours(), computePath(), LinkAStar(), and moveToClosedList().

◆ _pq

std::priority_queue<Cost> YACS::HMI::LinkAStar::_pq
protected

Definition at line 90 of file LinkAStar.hxx.

Referenced by addNeighbours(), bestNode(), computePath(), and LinkAStar().

◆ _to

LNode YACS::HMI::LinkAStar::_to
protected

Definition at line 89 of file LinkAStar.hxx.

Referenced by addNeighbours(), computePath(), and givePath().


The documentation for this class was generated from the following files: