Map2DPosVar.cpp
Go to the documentation of this file.
00001 #include "Map2DPosVar.h"
00002 #include <exception>
00003 
00004 Map2DPosVar::Map2DPosVar(string name, int width, int height) : IVariable()
00005 {
00006         this->variableName = name;
00007         this->width = width;
00008         this->height = height;
00009 }
00010 
00011 Map2DPosVar::~Map2DPosVar(void)
00012 {
00013 }
00014 
00015 
00016 string Map2DPosVar::getVariableName()
00017 {
00018         return variableName;
00019 }
00020 
00021 vector<SharedPointer<IVariableValue> > Map2DPosVar::getValues()
00022 {
00023         vector<SharedPointer<IVariableValue> > result;
00024         for(int y = 0; y < height; y++)
00025         {
00026                 for(int x = 0; x < width; x++)
00027                 {
00028                         SharedPointer<Map2DPosValue> newEntry(new Map2DPosValue(variableName, x, y, height));
00029                         result.push_back(newEntry);
00030                 }
00031         }
00032         return result;
00033 }
00034 
00035 SharedPointer<IVariableValue> Map2DPosVar::getValueByName(string valName)
00036 {
00037         vector<SharedPointer<IVariableValue> > values = this->getValues();
00038         for(int i = 0; i < values.size(); i++)
00039         {
00040                 if(values[i]->getValueName().compare(valName) == 0)
00041                 {
00042                         return values[i];
00043                 }
00044         }
00045 
00046         throw runtime_error("Cannot find value : " + valName + " in variable : " + this->getVariableName());
00047 }
00048 
00049 vector<SharedPointer<IVariableValue> > Map2DPosVar::getInitialValues()
00050 {
00051         vector<SharedPointer<IVariableValue> > result;
00052         result.push_back(initPos);
00053         return result;
00054 }
00055 
00056 int Map2DPosVar::getNumValues()
00057 {
00058         return height* width;
00059 }
00060 void Map2DPosVar::setInitPos(SharedPointer<Map2DPosValue> pos)
00061 {
00062         pos->prob = 1.0;
00063         initPos = pos;
00064 }
00065 vector<SharedPointer<Map2DPosValue> > Map2DPosVar::getAdjPos(SharedPointer<Map2DPosValue>  src, double distance)
00066 {
00067        vector<SharedPointer<Map2DPosValue> > result;
00068         int range = (int)ceil(distance);
00069         for(int xdiff = -range ; xdiff < range + 1; xdiff ++)
00070         {
00071             for(int ydiff = -range ; ydiff < range + 1; ydiff ++)
00072             {
00073                 if(xdiff == 0 && ydiff == 0)
00074                 {
00075                     continue;
00076                 }
00077 
00078                                 SharedPointer<Map2DPosValue> temp = src->duplicate();
00079                 temp->x += xdiff;
00080                 temp->y += ydiff;
00081                                 if(temp->x >= this->width)
00082                 {
00083                     continue;
00084                 }
00085                 if(temp->x < 0)
00086                 {
00087                     //temp.x = 0;
00088                     continue;
00089                 }
00090                 if(temp->y >= this->height)
00091                 {
00092                     //temp.y = temp.height -1;
00093                     continue;
00094                 }
00095                 if(temp->y < 0)
00096                 {
00097                     //temp.y = 0;
00098                     continue;
00099                 }
00100                                 result.push_back(temp);
00101             }
00102         }
00103         return result;
00104     }
00105 
00106 vector<SharedPointer<Map2DPosValue> > Map2DPosVar::getDestSidePosPerpendicularToMovement(SharedPointer<Map2DPosValue>  src, SharedPointer<Map2DPosValue>  dest)
00107 {
00108     vector<SharedPointer<Map2DPosValue> > sideCells;
00109         int dX = 0;
00110         int dY = 0;
00111         int incX = 0;
00112         int incY = 0;
00113         if(dest->x > src->x)
00114         {
00115             incX = -1;
00116         }
00117         else if(dest->x < src->x)
00118         {
00119             incX = 1;
00120         }
00121 
00122         if(dest->y > src->y)
00123         {
00124             incY = -1;
00125         }
00126         else if(dest->y < src->y)
00127         {
00128             incY = 1;
00129         }
00130         
00131 
00132         if(dest->x != src->x)
00133         {
00134             // movement in x
00135             dY = 1;
00136         }
00137         else if(dest->y != src->y)
00138         {
00139             // movement in y
00140             dX = 1;
00141         }
00142         else
00143         {
00144             // did not move...
00145             //System.out.println("Did not move!");
00146             dX = 1;
00147         }
00148         
00149         {
00150             SharedPointer<Map2DPosValue> sideCellVar = src->duplicate();
00151             sideCellVar->x += dX;
00152             sideCellVar->y += dY;
00153             if(sideCellVar->x < this->width && sideCellVar->y < this->height)
00154             {
00155                                 sideCells.push_back(sideCellVar);
00156             }
00157         }
00158         
00159         {
00160                         SharedPointer<Map2DPosValue> sideCellVar = src->duplicate();
00161             sideCellVar->x -= dX;
00162             sideCellVar->y -= dY;
00163             if(sideCellVar->x >= 0 && sideCellVar->y >= 0)
00164             {
00165                                 sideCells.push_back(sideCellVar);
00166             }
00167         }
00168 
00169         {
00170             if(incX != 0 || incY != 0)
00171             {
00172                 // only add this cell if it actually moves
00173                 SharedPointer<Map2DPosValue> sideCellVar = src->duplicate();
00174                 sideCellVar->x += incX;
00175                 sideCellVar->y += incY;
00176                 if(sideCellVar->x < this->width && sideCellVar->y < this->height && sideCellVar->x >= 0 && sideCellVar->y >= 0)
00177                 {
00178                                         sideCells.push_back(sideCellVar);
00179                 }
00180             }
00181         }
00182         
00183 
00184 
00185         return sideCells;
00186 }


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29