00001 //----------------------------------------------------------------------------- 00002 /** @file tiling.h 00003 Search environment using tilings and obstacles. 00004 00005 $Id: tiling_8h-source.html,v 1.1.1.1 2003/08/07 19:41:39 emarkus Exp $ 00006 $Source: /usr/cvsroot/www_pathfind/libpathfind/0.1.0/doc/tiling_8h-source.html,v $ 00007 */ 00008 //----------------------------------------------------------------------------- 00009 /** @page tilingfileformat Tiling file format 00010 The file format for tilings is as follows: 00011 00012 @verbatim 00013 type tile|octile|octile_unicost|hex 00014 height <height> 00015 width <width> 00016 map 00017 <obstacle map using one line per row and one character per column.> 00018 <The character '@' means obstacle, '.' means no obstacle.> 00019 @endverbatim 00020 00021 Example: 00022 00023 @verbatim 00024 type tile 00025 height 10 00026 width 20 00027 map 00028 .................... 00029 .................... 00030 ...........@........ 00031 ...@@@@@@@@@..@..... 00032 ..............@..... 00033 ...@@@@@@@@@..@..... 00034 ...@.......@..@..... 00035 ...@.......@..@..... 00036 ...........@........ 00037 .................... 00038 @endverbatim 00039 00040 @see @ref tilinghexgridmapping 00041 */ 00042 /** @page tilinghexgridmapping Tiling hex grid mapping 00043 Hex grids use the following mapping between (column,row) to nodes: 00044 00045 @image html hexgrid.png "Hex grid mapping of (column,row)" 00046 00047 @see @ref tilinghexgridmappingascii 00048 */ 00049 /** @page tilinghexgridmappingascii Tiling hex grid mapping (ASCII version) 00050 Hex grids use the following mapping between (column,row) to nodes: 00051 00052 @verbatim 00053 ___ ___ 00054 /0,0\___/2,0\___ 00055 \___/1,0\___/3,0\ 00056 /0,1\___/2,1\___/ 00057 \___/1,1\___/3,1\ 00058 /0,2\___/2,2\___/ 00059 \___/1,2\___/3,2\ 00060 \___/ \___/ 00061 @endverbatim 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 /** Generic edge data for Tiling::TilingGraph. */ 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 /** Generic node data for Tiling::TilingGraph. */ 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 /** %Search environment implementing a regular grid. 00137 Nodes can be blocked. Edge costs are uniform. 00138 */ 00139 class Tiling 00140 : public Environment 00141 { 00142 public: 00143 typedef enum { 00144 /** Hex grid type. 00145 @see @ref tilinghexgridmapping 00146 */ 00147 HEX, 00148 00149 /** Octiles with cost 1 to adjacent and sqrt(2) to diagonal. */ 00150 OCTILE, 00151 00152 /** Octiles with uniform cost 1 to adjacent and diagonal. */ 00153 OCTILE_UNICOST, 00154 00155 /** Tile grid type. */ 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 /** Construct tiling from a file. 00165 @exception ReadError 00166 Reading failed or syntax error in file. 00167 @see @ref tilingfileformat 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 /** Print formatted map with char labels. 00200 Space characters are not printed on the map. 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 /** Read obstacles from a file. 00267 @exception ReadError Reading failed or syntax error in file. 00268 */ 00269 void readObstacles(LineReader& reader); 00270 }; 00271 } 00272 00273 //----------------------------------------------------------------------------- 00274 00275 #endif