00001 #include "GT2005Clustering.h"
00002
00003 GT2005Clustering::GT2005Clustering()
00004 {
00005 cl = GT2005Clusterliste();
00006
00007 vhshort = 8;
00008 vhmiddle = 8;
00009 vhlong = 16;
00010
00011 yshort = 0;
00012 ymiddle = 80;
00013 ylong = 110;
00014
00015 hv = 20;
00016
00017 vh = 4;
00018
00019 lImage = 208;
00020 hImage = 160;
00021
00022 }
00023
00024
00025
00026
00027 void GT2005Clustering::addLine(Vector2<int> startInt, Vector2<int> endInt, int length )
00028 {
00029 Vector2<double> start(startInt.x,startInt.y);
00030 Vector2<double> end(endInt.x,endInt.y);
00031
00032
00033
00034 if(fabs(start.x - end.x) < fabs(start.y - end.y))
00035 vertical = true;
00036 else
00037 vertical = false;
00038
00039
00040
00041 if(start.x <= end.x)
00042 {
00043 xmin = start.x;
00044 xmax = end.x;
00045 }
00046 else
00047 {
00048 xmin = end.x;
00049 xmax = start.x;
00050 }
00051 if(start.y <= end.y)
00052 {
00053 ymin = start.y;
00054 ymax = end.y;
00055 }
00056 else
00057 {
00058 ymin = end.y;
00059 ymax = start.y;
00060 }
00061
00062
00063
00064 if(cl.getEmpty())
00065 {
00066 GT2005Cluster firstCluster = GT2005Cluster(xmin,xmax,ymin,ymax);
00067 vhIter = cl.getVNaviBegin();
00068 cl.addCluster(firstCluster, vhIter, true);
00069 }
00070 else
00071
00072 Cluster1(vertical);
00073
00074 }
00075
00076
00077
00078 void GT2005Clustering::Cluster1(bool v)
00079 {
00080
00081 if(v)
00082 {
00083 prepV(xmin);
00084
00085
00086
00087
00088
00089
00090
00091
00092 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getXMin() > xmax))))
00093 markV(xmin, xmax, ymin, ymax, vh);
00094 else
00095 {
00096 GT2005Cluster scanline = GT2005Cluster(xmin, xmax, ymin, ymax);
00097 cIter = *vhIter;
00098 if((vhIter != vhIter2) && (cIter->getXMin() <= xmax) && (xmin > cIter->getXMin()))
00099 ++vhIter;
00100 cl.addCluster(scanline, vhIter, true);
00101 }
00102
00103
00104
00105
00106 if(ymin >= ylong)
00107 {
00108
00109 prepH(ymin);
00110 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00111 markH(xmin, xmax, ymin, ymax, vhlong);
00112
00113 }
00114
00115
00116 else if((ymin >= ymiddle) && (ymin < ylong))
00117 {
00118
00119
00120 if(ymax < ylong)
00121 {
00122
00123 prepH(ymin);
00124 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00125 markH(xmin, xmax, ymin, ymax, vhmiddle);
00126 }
00127 else
00128 {
00129
00130
00131 prepH(ymin);
00132 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ylong - 1))))
00133 markH(xmin, xmax, ymin, ylong - 1, vhmiddle);
00134
00135
00136 prepH(ylong - 1);
00137 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00138 markH(xmin, xmax, ylong - 1, ymax, vhlong);
00139 }
00140 }
00141
00142 else if((ymin >= yshort) && (ymin < ymiddle))
00143 {
00144
00145
00146 if(ymax < ymiddle)
00147 {
00148
00149 prepH(ymin);
00150 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00151 markH(xmin, xmax, ymin, ymax, vhshort);
00152 }
00153 else if((ylong > ymax) && (ymax >= ymiddle))
00154 {
00155
00156 prepH(ymin);
00157 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymiddle-1))))
00158 markH(xmin, xmax, ymin, ymiddle - 1, vhshort);
00159
00160
00161 prepH(ymiddle -1);
00162 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00163 markH(xmin, xmax, ymiddle - 1, ymax, vhmiddle);
00164 }
00165 else if(ymax >= ylong)
00166 {
00167
00168 prepH(ymin);
00169 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymiddle - 1))))
00170 markH(xmin, xmax, ymin, ymiddle - 1, vhshort);
00171
00172
00173 prepH(ymiddle - 1);
00174 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ylong - 1))))
00175 markH(xmin, xmax, ymiddle - 1, ylong - 1, vhmiddle);
00176
00177
00178 prepH(ylong - 1);
00179 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00180 markH(xmin, xmax, ylong - 1, ymax, vhlong);
00181 }
00182
00183 }
00184 }
00185
00186 else
00187 {
00188 prepH(ymin);
00189 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getYMin() > ymax))))
00190 markH(xmin,xmax,ymin,ymax,vh);
00191 else
00192 {
00193 GT2005Cluster scanline = GT2005Cluster(xmin, xmax, ymin, ymax);
00194 cIter = *vhIter;
00195 if((vhIter != vhIter2) && (cIter->getYMin() <= ymax) && (ymin > cIter->getYMin()))
00196 ++vhIter;
00197 cl.addCluster(scanline, vhIter, false);
00198 }
00199
00200
00201
00202
00203 prepV(xmin);
00204 if(!((vhIter == vhIter2) || ((vhIter != vhIter2) && (cIter->getXMin() > xmax))))
00205 markV(xmin, xmax, ymin, ymax,hv );
00206 }
00207
00208 }
00209
00210
00211
00212
00213
00214
00215 void GT2005Clustering::prepV(double xminimum)
00216 {
00217
00218 vhIter = cl.getVNaviBegin();
00219 vhIter2 = cl.getVNaviEnd();
00220 cIter = *vhIter;
00221
00222
00223 while((vhIter != vhIter2) && (cIter->getXMax() < xminimum))
00224 {
00225 ++vhIter;
00226 if(vhIter != vhIter2)
00227 cIter = *vhIter;
00228 }
00229
00230 }
00231
00232
00233
00234
00235 void GT2005Clustering::prepH(double yminimum)
00236 {
00237
00238 vhIter = cl.getHNaviBegin();
00239 vhIter2 = cl.getHNaviEnd();
00240 cIter = *vhIter;
00241
00242
00243 while((vhIter != vhIter2) && (cIter->getYMax() < yminimum))
00244 {
00245 ++vhIter;
00246 if(vhIter != vhIter2)
00247 cIter = *vhIter;
00248 }
00249 }
00250
00251
00252
00253
00254 void GT2005Clustering::markV(double xminimum, double xmaximum, double yminimum, double ymaximum, int expansion )
00255 {
00256
00257
00258 cfirst = NULL;
00259 clast = NULL;
00260
00261 cinput = vhIter;
00262
00263
00264 cIter = *vhIter;
00265
00266 while((vhIter != vhIter2) && (cIter->getXMin() <= xmaximum))
00267 {
00268
00269 if(cIter->getXMax() >= xminimum)
00270 {
00271
00272
00273 if((cIter->getYMax() >= (yminimum - expansion)) && (cIter->getYMin() <= (ymaximum + expansion)))
00274 {
00275 if(cfirst == NULL)
00276 cfirst = vhIter;
00277 clast = vhIter;
00278 cIter->setCandidate(true);
00279 }
00280 }
00281 ++vhIter;
00282 if(vhIter != vhIter2)
00283 cIter = *vhIter;
00284 }
00285
00286 if(cfirst != NULL)
00287 {
00288 clusterFinal(true, xminimum,xmaximum,yminimum,ymaximum);
00289 }
00290 else
00291 {
00292 GT2005Cluster scanline = GT2005Cluster(xminimum, xmaximum, yminimum, ymaximum);
00293 cIter = *cinput;
00294 if((cinput != vhIter2) && (cIter->getXMin() <= xmaximum) && (xminimum > cIter->getXMin()))
00295 ++cinput;
00296 cl.addCluster(scanline, cinput, true);
00297 }
00298 }
00299
00300
00301
00302 void GT2005Clustering::markH(double xminimum, double xmaximum, double yminimum, double ymaximum, int expansion)
00303 {
00304
00305
00306 cfirst = NULL;
00307 clast = NULL;
00308 cinput = vhIter;
00309
00310 cIter = *vhIter;
00311
00312 while((vhIter != vhIter2) && (cIter->getYMin() <= ymaximum))
00313 {
00314
00315 if(cIter->getYMax() >= yminimum)
00316 {
00317 if((cIter->getXMin() <= (xmaximum + expansion)) && (cIter->getXMax() >= (xminimum - expansion)))
00318 {
00319 if(cfirst == NULL)
00320 cfirst = vhIter;
00321 clast = vhIter;
00322 cIter->setCandidate(true);
00323 }
00324
00325 }
00326 ++vhIter;
00327 if(vhIter != vhIter2)
00328 cIter = *vhIter;
00329 }
00330
00331 if(cfirst != NULL)
00332 {
00333 clusterFinal(false, xminimum,xmaximum,yminimum,ymaximum);
00334 }
00335 else
00336 {
00337 GT2005Cluster scanline = GT2005Cluster(xminimum, xmaximum, yminimum, ymaximum);
00338 cIter = *cinput;
00339 if((cinput != vhIter2) && (cIter->getYMin() <= ymaximum) && (yminimum > cIter->getYMin()))
00340 ++cinput;
00341 cl.addCluster(scanline, cinput, false);
00342 }
00343
00344 }
00345
00346 void GT2005Clustering::clusterFinal(bool v, double xminimum, double xmaximum, double yminimum, double ymaximum)
00347 {
00348
00349 double newXMIN;
00350 double newXMAX;
00351 double newYMIN;
00352 double newYMAX;
00353
00354 cIter = *cfirst;
00355 if(xminimum < cIter->getXMin())
00356 newXMIN = xminimum;
00357 else
00358 newXMIN = cIter->getXMin();
00359 if(xmaximum > cIter->getXMax())
00360 newXMAX = xmaximum;
00361 else
00362 newXMAX = cIter->getXMax();
00363 if(yminimum < cIter->getYMin())
00364 newYMIN = yminimum;
00365 else
00366 newYMIN = cIter->getYMin();
00367 if(ymaximum > cIter->getYMax())
00368 newYMAX = ymaximum;
00369 else
00370 newYMAX = cIter->getYMax();
00371
00372
00373
00374 if(v)
00375 {
00376 if(cfirst == cl.getVNaviBegin())
00377 cinput = NULL;
00378 else
00379 {
00380 cinput = --cfirst;
00381 ++cfirst;
00382 }
00383 }
00384 else
00385 {
00386 if(cfirst == cl.getHNaviBegin())
00387 cinput = NULL;
00388 else
00389 {
00390 cinput = --cfirst;
00391 ++cfirst;
00392 }
00393 }
00394
00395 ++cfirst;
00396 ++clast;
00397 for(vhIter = cfirst; vhIter != clast; ++vhIter)
00398 {
00399 cl.removeCluster(cIter);
00400 cIter = *vhIter;
00401 if(cIter->getCandidate() == true)
00402 {
00403 if(cIter->getXMin() < newXMIN)
00404 newXMIN = cIter->getXMin();
00405 if(cIter->getXMax() > newXMAX)
00406 newXMAX = cIter->getXMax();
00407 if(cIter->getYMin() < newYMIN)
00408 newYMIN = cIter->getYMin();
00409 if(cIter->getYMax() > newYMAX)
00410 newYMAX = cIter->getYMax();
00411 }
00412 }
00413 cl.removeCluster(cIter);
00414
00415
00416 GT2005Cluster newCluster = GT2005Cluster(newXMIN, newXMAX, newYMIN, newYMAX);
00417 if(cinput == NULL)
00418 {
00419 if(v)
00420 cinput = cl.getVNaviBegin();
00421 else
00422 cinput = cl.getHNaviBegin();
00423 }
00424 else
00425 ++cinput;
00426 cl.addCluster(newCluster, cinput, v);
00427
00428 }
00429
00430 int GT2005Clustering::getSize()
00431 {
00432 hlist.clear();
00433 hlist = cl.getMidpoints();
00434 return(hlist.size());
00435 }
00436
00437
00438 Vector2<double> GT2005Clustering::getMidpoint(int e)
00439 {
00440 hlist.clear();
00441 hlist = cl.getMidpoints();
00442
00443 hiterator = hlist.begin();
00444 for(int i = 0; i < e; ++i)
00445 {
00446 ++hiterator;
00447 }
00448 return (*hiterator);
00449 }
00450
00451 Vector2<double> GT2005Clustering::getMidpointX(int e)
00452 {
00453 vhIter = cl.getVNaviBegin();
00454 for(int i = 0; i < e; ++i)
00455 {
00456 ++vhIter;
00457 }
00458 cIter = *vhIter;
00459 return(cIter->getMidpoint());
00460 }
00461
00462 Vector2<double> GT2005Clustering::getMidpointY(int e)
00463 {
00464 vhIter = cl.getHNaviBegin();
00465 for(int i = 0; i < e; ++i)
00466 {
00467 ++vhIter;
00468 }
00469 cIter = *vhIter;
00470 return(cIter->getMidpoint());
00471 }
00472
00473 GT2005Cluster GT2005Clustering::getClusterX(int e)
00474 {
00475 vhIter = cl.getVNaviBegin();
00476 for(int i = 0; i < e; ++i)
00477 {
00478 ++vhIter;
00479 }
00480 cIter = *vhIter;
00481 return(*cIter);
00482 }
00483
00484 GT2005Cluster GT2005Clustering::getClusterY(int e)
00485 {
00486 vhIter = cl.getHNaviBegin();
00487 for(int i = 0; i < e; ++i)
00488 {
00489 ++vhIter;
00490 }
00491 cIter = *vhIter;
00492 return(*cIter);
00493 }
00494
00495 void GT2005Clustering::reset()
00496 {
00497 cl.deleteAll();
00498 }
00499
00500