1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include "Coord.h"
using namespace std;
template <class TYPE>
class Map{
public:
enum IfOutOfBounds{
ExpandResolution, //resize the map so that the Point can fit in
DontCreatePoint, //does not create new point,ofb
YoloCreatePoint, //creates point,though ofb
ErrorAndNoCreate //cout's error message and does not create new point,ofb
};
private:
vector<Coord>Points;
TYPE in;
TYPE bg;
Coord resolution;
const IfOutOfBounds action;
public:
/*A*/ Map(const IfOutOfBounds& action=ErrorAndNoCreate);
/*T+A*/ Map(const TYPE&,const TYPE&,const IfOutOfBounds& action=ErrorAndNoCreate);
/*TR+A*/ Map(const TYPE&,const TYPE&,const Coord& resolution,const IfOutOfBounds& action=ErrorAndNoCreate);
/*TP+A*/ Map(const TYPE&,const TYPE&,const vector<Coord>& Points,const IfOutOfBounds& action=ErrorAndNoCreate);
/*TP+R+A*/ Map(const TYPE&,const TYPE&,const vector<Coord>& Points,const Coord& newRes,const IfOutOfBounds& action=ErrorAndNoCreate);
/*P+A*/ Map(const vector<Coord>& Points,const IfOutOfBounds& action=ErrorAndNoCreate);
/*R+A*/ Map(const Coord& resolution,const IfOutOfBounds& action=ErrorAndNoCreate);
/*PR+A*/ Map(const vector<Coord>& Points,const Coord& newRes,const IfOutOfBounds& action=ErrorAndNoCreate);
Map(ifstream&,const IfOutOfBounds& action=ErrorAndNoCreate);
Map(const string& filename,const IfOutOfBounds& action=ErrorAndNoCreate);
void setFromFile(ifstream&);
void setFromFile(const string& filename);
void setInChar(const TYPE&);
void setBgChar(const TYPE&);
void setChars(const TYPE&,const TYPE&);
void setPoints(const vector<Coord>&);
void addPoint(const Coord& point);
void addPoints(const vector<Coord>&);
void clearPoint(const Coord&);
void clearPoints(const vector<Coord>&);
void clearAllPoints();
void resize(const Coord&);
vector<Coord> getAllPoints();
Coord getResolution();
void printAllPoints();
//virtual void display()=0;
};
template <class TYPE>
/*A*/ Map<TYPE>::Map(const IfOutOfBounds& action) : action(action){}
template <class TYPE>
/*T+A*/ Map<TYPE>::Map(const TYPE& in,const TYPE& bg,const IfOutOfBounds& action) : in(in), bg(bg), action(action){}
template <class TYPE>
/*TR+A*/ Map<TYPE>::Map(const TYPE& in,const TYPE& bg,const Coord& resolution,const IfOutOfBounds& action): in(in), bg(bg), resolution(resolution), action(action){}
template <class TYPE>
/*TP+A*/ Map<TYPE>::Map(const TYPE& in,const TYPE& bg,const vector<Coord>& Points,const IfOutOfBounds& action): Map(in,bg,Points,Coord(0,0),action){}
template <class TYPE>
/*TPR+A*/ Map<TYPE>::Map(const TYPE& in,const TYPE& bg,const vector<Coord>& Points,const Coord& newRes,const IfOutOfBounds& action) : in(in), bg(bg), Points(Points), action(action){
if(newRes==Coord(0,0)){
Coord maxRes;
unsigned int i;
for(i=0;i<Points.size();i++){
if(Points.at(i).x>maxRes.x){
maxRes.x=Points.at(i).x;
}
if(Points.at(i).y>maxRes.y){
maxRes.y=Points.at(i).y;
}
}
resolution=maxRes;
}
else{
resize(newRes);
}
}
template <class TYPE>
/*P+A*/ Map<TYPE>::Map(const vector<Coord>& Points,const IfOutOfBounds& action=ErrorAndNoCreate) : Map(TYPE(),TYPE(),Points,Coord(0,0),action){}
template <class TYPE>
/*R+A*/ Map<TYPE>::Map(const Coord& resolution,const IfOutOfBounds& action) : resolution(resolution),action(action){}
template <class TYPE>
/*PR+A*/ Map<TYPE>::Map(const vector<Coord>& Points,const Coord& resolution,const IfOutOfBounds& action) : Points(Points), resolution(resolution), action(action){}
template <class TYPE>
Map<TYPE>::Map(ifstream& file,const IfOutOfBounds& action) : action(action){
setFromFile(file);
}
template <class TYPE>
Map<TYPE>::Map(const string& filename,const IfOutOfBounds& action) : action(action){
setFromFile(filename);
}
template<class TYPE>
void Map<TYPE>::setFromFile(ifstream& src){
if(!src.is_open()){
cout<<"CANNOT OPEN FILE"<<endl;
return;
}
string temp;
bool rezPassed=false;
getline(src,temp);
if(temp.substr(0,2)=="x="){
resolution.x=abs(atoi(temp.substr(2,temp.size()-2).c_str()));
temp="";
getline(src,temp);
if(temp.substr(0,2)=="y="){
resolution.y=abs(atoi(temp.substr(2,temp.size()-2).c_str()));
}
else{
cout<<"WRONG FORMAT"<<endl;
return;
}
rezPassed=true;
temp="";
getline(src,temp);
}
if(temp[0]=='('){
do{
addPoint(Coord(atoi(temp.substr(1,1).c_str()),atoi(temp.substr(3,1).c_str())));
}
while(getline(src,temp));
}
else{
if(!rezPassed){ //not de primu if,daca nu a fost passanit x si y(rezolutia)
cout<<"WRONG FORMAT"<<endl;
return;
}
}
}
template<class TYPE>
void Map<TYPE>::setFromFile(const string& filename){
setFromFile(ifstream(filename));
}
template <class TYPE>
inline void Map<TYPE>::setInChar(const TYPE& nIn){
in=nIn;
}
template <class TYPE>
inline void Map<TYPE>::setBgChar(const TYPE& nBg){
bg=nBg;
}
template <class TYPE>
inline void Map<TYPE>::setChars(const TYPE& nIn,const TYPE& nBg){
setInChar(nIn);
setBgChar(nBg);
}
template <class TYPE>
void Map<TYPE>::setPoints(const vector<Coord>& nPoints){
clearAllPoints();
addPoints(nPoints);
}
template <class TYPE>
void Map<TYPE>::addPoint(const Coord& point){
unsigned int i;
for(i=0;i<Points.size();i++){
if(Points.at(i)==point){
return;
}
}
if(point.x>=resolution.x||point.y>=resolution.y){
switch (action){
case ExpandResolution:
if(point.x>=resolution.x){
resize(Coord(point.x+1,resolution.y));
}
if(point.y>=resolution.y){
resize(Coord(resolution.x,point.y+1));
}
break;
case DontCreatePoint:
//nema
return;
case ErrorAndNoCreate:
//nema
cout<<"ERROR: "<<point<<" OutOfBounds"<<endl;
return;
case YoloCreatePoint:
//creates
break;
}
}
Points.push_back(point);
}
template <class TYPE>
void Map<TYPE>::addPoints(const vector<Coord>& nPoints){
unsigned int i;
for(i=0;i<nPoints.size();i++){
addPoint(nPoints.at(i);
}
}
template <class TYPE>
void Map<TYPE>::clearPoint(const Coord& point){
unsigned int i;
for(i=0;i<Points.size();i++){
if(Points.at(i)==point){
Points.erase(Points.begin()+i);
}
}
}
template <class TYPE>
void Map<TYPE>::clearPoints(const vector<Coord>& dPoints){
unsigned int i;
for(i=0;i<dPoints.size();i++){
clearPoint(dPoints.at(i);
}
}
template <class TYPE>
inline void Map<TYPE>::clearAllPoints(){
Points.erase(Points.begin(),Points.end());
}
template <class TYPE>
void Map<TYPE>::resize(const Coord& newRes){
unsigned int i;
for(i=0;i<Points.size();i++){
if(Points.at(i).x>=newRes.x||Points.at(i).y>=newRes.y){
Points.erase(Points.begin()+i);
i--;
}
}
resolution=newRes;
}
template <class TYPE>
inline vector<Coord> Map<TYPE>::getAllPoints(){
return Points;
}
template <class TYPE>
inline Coord Map<TYPE>::getResolution(){
return resolution;
}
template <class TYPE>
void Map<TYPE>::printAllPoints(){
unsigned int i;
for(i=0;i<Points.size();i++){
cout<<i<<". "<<Points.at(i)<<endl;
}
}
|