searchspace.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <limits>
00024
00025
00026
00027
00028
00029
00030
00031 #include "model/structures/instance.h"
00032 #include "model/structures/layer.h"
00033 #include "model/structures/map.h"
00034
00035 #include "searchspace.h"
00036
00037 namespace FIFE {
00038
00039 SearchSpace::SearchSpace(Layer* layer)
00040 : m_upperX(0), m_upperY(0), m_lowerX(0), m_lowerY(0), m_layer(layer) {
00041
00042 Map* map = layer->getMap();
00043 const std::list<Layer*>& layers = map->getLayers();
00044 ModelCoordinate min, max;
00045
00046 for(std::list<Layer*>::const_iterator i = layers.begin();
00047 i != layers.end();
00048 ++i) {
00049
00050 ModelCoordinate newMin, newMax;
00051 (*i)->getMinMaxCoordinates(newMin, newMax, layer);
00052
00053 if(newMin.x < min.x) {
00054 min.x = newMin.x;
00055 }
00056
00057 if(newMax.x > max.x) {
00058 max.x = newMax.x;
00059 }
00060
00061 if(newMin.y < min.y) {
00062 min.y = newMin.y;
00063 }
00064
00065 if(newMax.y > max.y) {
00066 max.y = newMax.y;
00067 }
00068 }
00069
00070 m_upperX = max.x;
00071 m_upperY = max.y;
00072 m_lowerX = min.x;
00073 m_lowerY = min.y;
00074 }
00075
00076 bool SearchSpace::isInSearchSpace(const Location& location) const {
00077 if(location.getLayer() != m_layer) {
00078 return false;
00079 }
00080 ModelCoordinate coordinates = location.getLayerCoordinates();
00081 if(coordinates.x >= m_lowerX && coordinates.x <= m_upperX
00082 && coordinates.y >= m_lowerY && coordinates.y <= m_upperY) {
00083 return true;
00084 }
00085 return false;
00086 }
00087
00088 ModelCoordinate SearchSpace::translateCoordsToSearchSpace(const ModelCoordinate& coords) const {
00089 ModelCoordinate newcoords;
00090 newcoords.x = coords.x - m_lowerX;
00091 newcoords.y = coords.y - m_lowerY;
00092 return newcoords;
00093 }
00094
00095 int SearchSpace::convertCoordToInt(const ModelCoordinate& coord) const {
00096 ModelCoordinate newcoords = translateCoordsToSearchSpace(coord);
00097 return newcoords.x + (newcoords.y * getWidth());
00098 }
00099
00100 ModelCoordinate SearchSpace::convertIntToCoord(const int cell) const {
00101 ModelCoordinate coord;
00102 int width = getWidth();
00103 coord.x = (cell % width) + m_lowerX;
00104 coord.y = (cell / width) + m_lowerY;
00105 return coord;
00106 }
00107
00108 int SearchSpace::getMaxIndex() const {
00109
00110
00111 int max_index = getWidth() + (getWidth() * getHeight());
00112 return max_index;
00113 }
00114 }