Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #ifndef __SP_TOOL_PATHFINDER_H__
00009 #define __SP_TOOL_PATHFINDER_H__
00010
00011
00012 #include "Base/spStandard.hpp"
00013
00014 #ifdef SP_COMPILE_WITH_PATHFINDER
00015
00016
00017 #include "Base/spBaseObject.hpp"
00018 #include "Base/spDimension.hpp"
00019
00020 #include <list>
00021 #include <map>
00022
00023
00024 namespace sp
00025 {
00026 namespace tool
00027 {
00028
00029
00030 class PathEdge;
00031
00036 class SP_EXPORT PathNode : public BaseObject
00037 {
00038
00039 public:
00040
00041 PathNode();
00042 PathNode(const dim::vector3df &Position, void* Data = 0);
00043 ~PathNode();
00044
00045
00046
00048 void setPosition(const dim::vector3df &Position);
00049
00051 std::list<PathNode*> getNeighbors() const;
00052
00053
00054
00056 inline dim::vector3df getPosition() const
00057 {
00058 return Position_;
00059 }
00060
00062 inline const std::list<PathEdge*>& getIncidentEdges() const
00063 {
00064 return Edges_;
00065 }
00066
00067 private:
00068
00069 friend class PathEdge;
00070 friend class PathGraph;
00071
00072
00073
00074 struct SP_EXPORT SNeighbor
00075 {
00076 SNeighbor(PathNode* Predecessor, PathNode* Neighbor);
00077 ~SNeighbor();
00078
00079
00080 PathNode* Node;
00081 f32 Distance;
00082 };
00083
00084
00085
00086 void addEdge(PathEdge* Edge);
00087 void removeEdge(PathEdge* Edge);
00088 void updateNeighbors();
00089
00090
00091
00092 inline f32 getMinWayCosts() const
00093 {
00094 return WayCosts_ + DirectDistance_;
00095 }
00096
00097
00098
00099 dim::vector3df Position_;
00100
00101 f32 WayCosts_;
00102 f32 DirectDistance_;
00103
00104 std::list<PathEdge*> Edges_;
00105 std::list<SNeighbor> Neighbors_;
00106
00107 PathNode* Predecessor_;
00108
00109 };
00110
00111
00116 class SP_EXPORT PathEdge : public BaseObject
00117 {
00118
00119 public:
00120
00121 PathEdge();
00122 PathEdge(PathNode* From, PathNode* To, bool Adjusted = false);
00123 ~PathEdge();
00124
00125
00126
00128 inline PathNode* getFrom() const
00129 {
00130 return From_;
00131 }
00133 inline PathNode* getTo() const
00134 {
00135 return To_;
00136 }
00137
00139 inline bool getAdjusted() const
00140 {
00141 return Adjusted_;
00142 }
00143
00145 inline f32 getDistance() const
00146 {
00147 return Distance_;
00148 }
00149
00150 private:
00151
00152 friend class PathNode;
00153 friend class PathGraph;
00154
00155
00156
00157 void updateNodePosition(PathNode* Node);
00158
00159
00160
00161 PathNode* From_;
00162 PathNode* To_;
00163
00164 f32 Distance_;
00165 bool Adjusted_;
00166
00167 };
00168
00169
00174 class SP_EXPORT PathGraph
00175 {
00176
00177 public:
00178
00179 PathGraph();
00180 virtual ~PathGraph();
00181
00182
00183
00190 PathNode* addNode(const dim::vector3df &Position, void* Data = 0);
00191
00193 void removeNode(PathNode* Node);
00194
00196 void clearNodeList();
00197
00207 PathEdge* addEdge(PathNode* From, PathNode* To, bool Adjusted = false);
00208
00210 void removeEdge(PathEdge* Edge);
00211
00213 void clearEdgeList();
00214
00230 void createGrid(
00231 const dim::vector3df &From, const dim::vector3df &To, const dim::vector3di &Steps,
00232 const std::vector<bool> &Bitmap = std::vector<bool>(), bool DiagonalEdges = true
00233 );
00234
00236 virtual std::list<PathNode*> findPath(PathNode* From, PathNode* To);
00237
00244 virtual bool findPath(PathNode* From, PathNode* To, std::list<PathNode*> &Path);
00245
00247 virtual std::list<PathNode*> findPath(const dim::vector3df &From, const dim::vector3df &To);
00248
00250 virtual bool findPath(const dim::vector3df &From, const dim::vector3df &To, std::list<PathNode*> &Path);
00251
00252
00253
00255 inline bool foundPath() const
00256 {
00257 return isSolved_;
00258 }
00259
00261 inline const std::list<PathNode*>& getNodeList() const
00262 {
00263 return NodeList_;
00264 }
00266 inline const std::list<PathEdge*>& getEdgeList() const
00267 {
00268 return EdgeList_;
00269 }
00270
00271 protected:
00272
00273
00274
00275 bool nextStep();
00276
00277 PathNode* getNextNode();
00278 void addNodeToQueue(PathNode* Node, PathNode* Predecessor, const f32 WayCosts);
00279
00280 void constructPath(std::list<PathNode*> &Path, PathNode* NextNode);
00281
00282
00283
00284 std::list<PathNode*> NodeList_;
00285 std::list<PathEdge*> EdgeList_;
00286
00287 std::list<PathNode*> NodeQueue_;
00288 std::map<PathNode*, bool> ClosedMap_;
00289
00290 PathNode* StartNode_, * TargetNode_;
00291
00292 bool isSolved_;
00293
00294 };
00295
00296
00297 }
00298
00299 }
00300
00301
00302 #endif
00303
00304 #endif
00305
00306
00307
00308