00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 #ifndef PATHFIND_TILING_H
00066 #define PATHFIND_TILING_H
00067
00068 #include <math.h>
00069 #include "environment.h"
00070 #include "graph.h"
00071 #include "util.h"
00072
00073
00074
00075 namespace PathFind
00076 {
00077 const int COST_ONE = 100;
00078
00079 const int COST_SQRT2 = 142;
00080
00081
00082 class TilingEdgeInfo
00083 {
00084 public:
00085 TilingEdgeInfo(int cost)
00086 : m_cost(cost)
00087 {
00088 assert(cost == COST_ONE || cost == COST_SQRT2);
00089 }
00090
00091 int getCost() const
00092 {
00093 return m_cost;
00094 }
00095
00096 private:
00097 int m_cost;
00098 };
00099
00100
00101 class TilingNodeInfo
00102 {
00103 public:
00104 TilingNodeInfo();
00105
00106 TilingNodeInfo(bool isObstacle, int row, int column);
00107
00108 int getColumn() const
00109 {
00110 return m_column;
00111 }
00112
00113 int getRow() const
00114 {
00115 return m_row;
00116 }
00117
00118 bool isObstacle() const
00119 {
00120 return m_isObstacle;
00121 }
00122
00123 void setObstacle(bool isObstacle)
00124 {
00125 m_isObstacle = isObstacle;
00126 }
00127
00128 private:
00129 bool m_isObstacle;
00130
00131 int m_column;
00132
00133 int m_row;
00134 };
00135
00136
00137
00138
00139 class Tiling
00140 : public Environment
00141 {
00142 public:
00143 typedef enum {
00144
00145
00146
00147 HEX,
00148
00149
00150 OCTILE,
00151
00152
00153 OCTILE_UNICOST,
00154
00155
00156 TILE
00157 } Type;
00158
00159 Tiling(Type type, int rows, int columns);
00160
00161 Tiling(const Tiling & tiling, int horizOrigin, int vertOrigin,
00162 int width, int height);
00163
00164
00165
00166
00167
00168
00169 Tiling(LineReader& reader);
00170
00171 void clearObstacles();
00172
00173 int getHeuristic(int start, int target) const;
00174
00175 int getMaxCost() const;
00176
00177 int getMinCost() const;
00178
00179 int getNodeId(int row, int column) const
00180 {
00181 assert(row >= 0 && row < m_rows);
00182 assert(column >= 0 && column < m_columns);
00183 return row * m_columns + column;
00184 }
00185
00186 int getNumberNodes() const;
00187
00188 void getSuccessors(int nodeId, int lastNodeId,
00189 vector<Successor>& result) const;
00190
00191 bool isValidNodeId(int nodeId) const;
00192
00193 void printFormatted(ostream& o) const;
00194
00195 void printFormatted(ostream& o, int start, int target) const;
00196
00197 void printFormatted(ostream& o, const vector<int>& path) const;
00198
00199
00200
00201
00202 void printLabels(ostream& o, const vector<char>& labels) const;
00203
00204 void printPathAndLabels(ostream& o, const vector<int>& path,
00205 const vector<char>& labels) const;
00206
00207 void setObstacles(float obstaclePercentage, bool avoidDiag=false);
00208
00209 int getWidth() const
00210 {
00211 return m_columns;
00212 }
00213
00214 int getHeight() const
00215 {
00216 return m_rows;
00217 }
00218
00219 TilingNodeInfo & getNodeInfo(int nodeId) const
00220 {
00221 return (TilingNodeInfo &)m_graph.getNodeInfo(nodeId);
00222 }
00223
00224 Type getType() const
00225 {
00226 return m_type;
00227 }
00228
00229 private:
00230 typedef Graph<TilingNodeInfo, TilingEdgeInfo>::Edge TilingEdge;
00231
00232 typedef Graph<TilingNodeInfo, TilingEdgeInfo> TilingGraph;
00233
00234 typedef Graph<TilingNodeInfo, TilingEdgeInfo>::Node TilingNode;
00235
00236
00237 int m_columns;
00238
00239 int m_maxEdges;
00240
00241 int m_rows;
00242
00243 Type m_type;
00244
00245 TilingGraph m_graph;
00246
00247
00248 void addOutEdge(int nodeId, int row, int col, int cost);
00249
00250 bool conflictDiag(int row, int col, int roff, int coff );
00251
00252 void createEdges();
00253
00254 void createNodes();
00255
00256 vector<char> getCharVector() const;
00257
00258 static int getMaxEdges(Type type);
00259
00260 void init(Type type, int rows, int columns);
00261
00262 void printFormatted(ostream& o, const vector<char>& chars) const;
00263
00264 bool pruneNode(int targetNodeId, int lastNodeId) const;
00265
00266
00267
00268
00269 void readObstacles(LineReader& reader);
00270 };
00271 }
00272
00273
00274
00275 #endif