00001 /** 00002 * @file PotentialfieldComposition.h 00003 * 00004 * Definition of class PotentialfieldComposition 00005 * 00006 * @author <a href="mailto:timlaue@informatik.uni-bremen.de">Tim Laue</a> 00007 */ 00008 00009 #ifndef POTENTIALFIELDCOMPOSITION_H_ 00010 #define POTENTIALFIELDCOMPOSITION_H_ 00011 00012 00013 #include "PfieldConfig.h" 00014 #include "PfieldDatatypes.h" 00015 #include <vector> 00016 #include <string> 00017 #include <list> 00018 #include <map> 00019 00020 class Object; 00021 class Parser; 00022 class Potentialfield; 00023 00024 00025 /** A type for describing the selection procedure of the next result*/ 00026 enum SelectionProcedure {BEST_OF_N, SUCCESSIVE_N_TIMES}; 00027 00028 /** A type for mapping names to indizes in vectors*/ 00029 typedef std::map<std::string, unsigned int> NameToIndexMap; 00030 00031 00032 /** 00033 * @class PotentialfieldResult 00034 * 00035 * Describes the result of a potential field 00036 */ 00037 class PotentialfieldResult 00038 { 00039 public: 00040 /** The validity of the result*/ 00041 double value; 00042 /** The name of the action*/ 00043 std::string action; 00044 /** Additional information about the action*/ 00045 std::string subAction; 00046 /** A motion, not used in all results*/ 00047 PfPose motion; 00048 /** Flag: true, if the result describes a possible action*/ 00049 bool actionPossible; 00050 /** The number of the selected field (for internal purposes)*/ 00051 unsigned int fieldNumber; 00052 /** The point of time, this result was generated*/ 00053 unsigned long timeStamp; 00054 00055 /** Copy operator 00056 * @param other Another PotentialfieldResult 00057 */ 00058 void operator = (const PotentialfieldResult& other) 00059 { 00060 value = other.value; 00061 action = other.action; 00062 subAction = other.subAction; 00063 motion = other.motion; 00064 actionPossible = other.actionPossible; 00065 fieldNumber = other.fieldNumber; 00066 timeStamp = other.timeStamp; 00067 } 00068 }; 00069 00070 00071 /** 00072 * @class ObjectStateDescription 00073 * 00074 * Describes the state of dynamic objects in a potential field 00075 */ 00076 class ObjectStateDescription 00077 { 00078 public: 00079 /** The name of the object state symbol*/ 00080 std::string objectName; 00081 /** The internal identifier*/ 00082 int objectId; 00083 /** The pose of the object*/ 00084 PfPose pose; 00085 /** Flag: true, if the object is active*/ 00086 bool isActive; 00087 00088 /** Constructor */ 00089 ObjectStateDescription() 00090 {} 00091 00092 /** Copy operator 00093 * @param other Another ObjectStateDescription 00094 */ 00095 void operator = (const ObjectStateDescription& other) 00096 { 00097 objectName = other.objectName; 00098 objectId = other.objectId; 00099 pose = other.pose; 00100 isActive = other.isActive; 00101 } 00102 00103 /** Copy-Constructor 00104 * @param other Another ObjectStateDescription 00105 */ 00106 ObjectStateDescription(const ObjectStateDescription& other) 00107 { 00108 objectName = other.objectName; 00109 objectId = other.objectId; 00110 pose = other.pose; 00111 isActive = other.isActive; 00112 } 00113 }; 00114 00115 00116 /** 00117 * @class PotentialfieldComposition 00118 * 00119 * The main class for the potential field implementation. 00120 * It contains all objects and fields, executes the fields and selects 00121 * the best action. 00122 */ 00123 class PotentialfieldComposition 00124 { 00125 public: 00126 /** Constructor */ 00127 PotentialfieldComposition(); 00128 00129 /** Destructor */ 00130 ~PotentialfieldComposition(); 00131 00132 /** Loads a configuration file 00133 * @param filename The name of the file 00134 */ 00135 void load(const std::string& filename); 00136 00137 /** Destroys all objects and fields*/ 00138 void close(); 00139 00140 /** Executes computation 00141 * @param result Returns the result 00142 */ 00143 void execute(PotentialfieldResult& result); 00144 00145 /** Sets the name 00146 * @param name The name 00147 */ 00148 void setName(const std::string& name) 00149 { this->name = name;} 00150 00151 /** Returns the name 00152 * @return The name 00153 */ 00154 std::string getName() const 00155 { return name;} 00156 00157 /** Sets the state of one object (may set desc.objectId) 00158 * @param desc The description of the object and its new state 00159 */ 00160 void setObjectState(ObjectStateDescription& desc); 00161 00162 /** Sets the own pose 00163 * @param pose The pose 00164 */ 00165 void setOwnPose(const PfPose& pose); 00166 00167 /** Adds a new field to the composition 00168 * @param field The field 00169 */ 00170 void addField(Potentialfield* field); 00171 00172 /** Adds a new object to the composition 00173 * @param object The object 00174 */ 00175 void addObject(Object* object); 00176 00177 /** Adds a new symbol for external object states 00178 * @param objectName The name of the symbol 00179 */ 00180 void addDynamicObjectState(const std::string& objectName); 00181 00182 /** Returns the internal id of a symbol 00183 * @param objectName The name of the symbol 00184 * @return The identifier 00185 */ 00186 unsigned int getIdFromObjectStateSymbol(const std::string& objectName); 00187 00188 /** Returns a object description 00189 * @param objectId The object identifier 00190 * @return The description 00191 */ 00192 ObjectStateDescription getDescriptionFromId(unsigned int objectId); 00193 00194 /** Returns a list of the names of all fields 00195 * @return A vector containing the names 00196 */ 00197 std::vector<std::string> getFieldNames(); 00198 00199 /** Sets parameters for field selection 00200 * @param selectionProcedure The selection mechanism 00201 * @param n A parameter for selectionProcedure 00202 */ 00203 void setFieldSelectionParameters(SelectionProcedure selectionProcedure, 00204 unsigned int n) 00205 { this->selectionProcedure = selectionProcedure; this->n = n;} 00206 00207 /** Computes an array of field values, used by visualization 00208 * @param fieldname The name of the potential field 00209 * @param x1 Minimum x-coordinate to compute 00210 * @param y1 Minimum y-coordinate to compute 00211 * @param x2 Maximum x-coordinate to compute 00212 * @param y2 Maximum y-coordinate to compute 00213 * @param xSteps The computation steps in x-direction 00214 * @param ySteps The computation steps in y-direction 00215 * @param value An array containing all computed values, memory has to be allocated BEFORE calling the function 00216 * @param max Returns the maximum value 00217 */ 00218 void getValueArray(const std::string& fieldname, double x1, double y1, 00219 double x2, double y2, int xSteps, int ySteps, double value[], double& max); 00220 00221 /** Computes an array of gradient directions, used by visualization 00222 * @param fieldname The name of the potential field 00223 * @param x1 Minimum x-coordinate to compute 00224 * @param y1 Minimum y-coordinate to compute 00225 * @param x2 Maximum x-coordinate to compute 00226 * @param y2 Maximum y-coordinate to compute 00227 * @param xSteps The computation steps in x-direction 00228 * @param ySteps The computation steps in y-direction 00229 * @param directions An array containing all computed gradients, memory has to be allocated BEFORE calling the function 00230 */ 00231 void getDirectionArray(const std::string& fieldname, double x1, double y1, 00232 double x2, double y2, int xSteps, int ySteps, PfVec directions[]); 00233 00234 /** Activates or deactivates a field 00235 * @param fieldname The name of the field 00236 * @param activation true, if the field is to be activated; false, otherwise 00237 */ 00238 void setFieldActivation(const std::string& fieldname, bool activation); 00239 00240 /** The PotentialfieldComposition is a friend of its parser*/ 00241 friend class Parser; 00242 00243 private: 00244 /** The name of the composition*/ 00245 std::string name; 00246 /** A list of all objects */ 00247 std::vector<Object*> objects; 00248 /** Fast mapping from object names to indizes*/ 00249 NameToIndexMap objectMap; 00250 /** The states of the dynamic objects*/ 00251 std::vector<ObjectStateDescription> dynamicObjectStates; 00252 /** Fast mapping from object state names to indizes*/ 00253 NameToIndexMap objectStateMap; 00254 /** A list of all fields */ 00255 std::vector<Potentialfield*> fields; 00256 /** Fast mapping from field names to indizes*/ 00257 NameToIndexMap fieldMap; 00258 /** A list for the results of all fields*/ 00259 std::vector<PotentialfieldResult> results; 00260 /** A list for the selected results of the last runs*/ 00261 std::list<PotentialfieldResult> resultList; 00262 /** The result of the last run*/ 00263 PotentialfieldResult lastResult; 00264 /** The own pose*/ 00265 PfPose ownPose; 00266 /** The parser*/ 00267 Parser* parser; 00268 /** A flag*/ 00269 bool fileLoaded; 00270 /** The procedure to choose the next active field*/ 00271 SelectionProcedure selectionProcedure; 00272 /** A parameter for the selectionProcedure*/ 00273 unsigned int n; 00274 00275 /** Selects the next result from the list 00276 * @return The result 00277 */ 00278 PotentialfieldResult selectNextResult(); 00279 00280 /** Adds a result to the list and keeps the size of the list <= n 00281 * @param result The result 00282 */ 00283 void addResultToList(const PotentialfieldResult& result); 00284 00285 /** Updates the state of all dynamic objects*/ 00286 void updateDynamicObjects(); 00287 00288 /** Returns the index of a field 00289 * @param fieldname The name of the field 00290 * @return The index 00291 */ 00292 unsigned int getFieldIndexFromName(const std::string& fieldname); 00293 }; 00294 00295 00296 #endif //POTENTIALFIELDCOMPOSITION_H_ 00297 00298 00299 00300 /* 00301 * $Log: PotentialfieldComposition.h,v $ 00302 * Revision 1.1.1.1 2004/05/22 17:37:33 cvsadm 00303 * created new repository GT2004_WM 00304 * 00305 * Revision 1.1 2004/01/20 15:42:19 tim 00306 * Added potential fields implementation 00307 * 00308 * Revision 1.5 2003/06/09 20:00:04 tim 00309 * Changed potentialfield architecture 00310 * 00311 * Revision 1.4 2003/03/30 15:32:09 tim 00312 * several minor changes 00313 * 00314 * Revision 1.3 2003/03/25 15:37:59 timrie 00315 * Doxygen-comments corrected 00316 * 00317 * Revision 1.2 2003/03/23 20:32:37 loetzsch 00318 * removed green compiler warning: no newline at end of file 00319 * 00320 * Revision 1.1 2003/03/23 17:51:27 tim 00321 * Added potentialfields 00322 * 00323 */