00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00024
00025 #include <qvdta/qvpolyline.h>
00026
00027 #define CONST_PI ((double)3.14159)
00028 #define SIGN(X) (((X)>=0)?1:-1)
00029
00031
00032
00033
00034 #define xA(Index) (coorX[previous[Index]])
00035 #define yA(Index) (coorY[previous[Index]])
00036 #define xB(Index) (coorX[Index])
00037 #define yB(Index) (coorY[Index])
00038 #define xC(Index) (coorX[next[Index]])
00039 #define yC(Index) (coorY[next[Index]])
00040 #define ABS(X) (((X)>=0)?(X):-(X))
00041 #define TRIANGLE_AREA(I) ABS(xA(I)*yC(I) - xA(I)*yB(I) + xB(I)*yA(I) - xB(I)*yC(I) + xC(I)*yB(I) - xC(I)*yA(I))
00042
00043 void IterativePointElimination(const QVPolyline &polyline, QVPolyline &result, const double minAreaDiv2)
00044 {
00045
00046
00047 const uInt polylineSize = polyline.size(), minArea = minAreaDiv2 * 2;
00048
00049 if (polylineSize < 3) return;
00050
00051
00052
00053 uInt *previous = new uInt[polylineSize],
00054 *next = new uInt[polylineSize],
00055 *valid = new uInt[polylineSize],
00056 *area = new uInt[polylineSize],
00057 discartablePoints = 0,
00058 validPoints = polylineSize;
00059
00060 int *coorX = new int[polylineSize],
00061 *coorY = new int[polylineSize];
00062
00063 for (uInt i=0; i< polylineSize; i++)
00064 {
00065 previous[i] = i-1;
00066 next[i] = i+1;
00067 coorX[i] = polyline.at(i).x();
00068 coorY[i] = polyline.at(i).y();
00069 }
00070
00071 previous[0] = polylineSize-1;
00072 next[polylineSize-1] = 0;
00073
00074
00075 for (uInt i=0; i < polylineSize; i++)
00076 {
00077 valid[i] = true;
00078 area[i] = TRIANGLE_AREA(i);
00079 }
00080
00081
00082
00083
00084
00085 uInt index = 0, lastIndex = previous[index];
00086 bool cond = true;
00087
00088 while(cond && validPoints > 3)
00089 {
00090 Q_ASSERT(valid[index]);
00091 Q_ASSERT(valid[next[index]]);
00092 Q_ASSERT(valid[previous[index]]);
00093 if (area[index] < minArea)
00094 {
00095
00096 valid[index] = false;
00097 next[previous[index]] = next[index];
00098 previous[next[index]] = previous[index];
00099 lastIndex = previous[index];
00100 validPoints--;
00101
00102
00103 index = next[index];
00104 area[index] = TRIANGLE_AREA(index);
00105 area[previous[index]] = TRIANGLE_AREA(previous[index]);
00106 }
00107 else {
00108
00109
00110 if (index == lastIndex)
00111 cond = false;
00112
00113
00114 index = next[index];
00115 }
00116 }
00117
00118
00119
00120 if (validPoints == 3)
00121 {
00122 Q_ASSERT(valid[index]);
00123 Q_ASSERT(area[index] == area[next[index]]);
00124 Q_ASSERT(area[index] == area[previous[index]]);
00125 Q_ASSERT(index == previous[previous[index]]);
00126
00127 if (area[index] < minArea)
00128 return;
00129 }
00130
00131
00132 for (uInt n=0; n< polylineSize; n++)
00133 if (valid[n])
00134 result.append(polyline.at(n));
00135 result.closed = polyline.closed;
00136 result.direction = polyline.direction;
00137
00138 delete previous;
00139 delete next;
00140 delete valid;
00141 delete area;
00142 delete coorX;
00143 delete coorY;
00144 }
00145
00146
00147 QVPolyline::QVPolyline(): QList<QPoint>(),
00148 closed(false), direction(false)
00149 {
00150 qDebug() << "QVPolyline()";
00151 qDebug() << "QVPolyline() <~ return";
00152 };
00153
00154 QVPolyline::QVPolyline(const QVPolyline &polyline): QList<QPoint>(polyline),
00155 closed(polyline.closed), direction(polyline.direction)
00156 {
00157 qDebug() << "QVPolyline(const QVPolyline &)";
00158 qDebug() << "QVPolyline(const QVPolyline &) <~ return";
00159 };
00160
00161 QPoint linesIntersection(QPoint a, QPoint b, QPoint c, QPoint d)
00162
00163 {
00164 double x1 = a.rx(), x2 = b.rx(), x3 = c.rx(), x4 = d.rx();
00165 double y1 = a.ry(), y2 = b.ry(), y3 = c.ry(), y4 = d.ry();
00166
00167 double denominador = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
00168 if (denominador == 0)
00169 return QPoint( (int)(b.rx() + c.rx())/2, (int)(b.ry() + c.ry())/2 );
00170
00171 double ua = (x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3),
00172 ub = (x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3);
00173
00174 QPoint p = QPoint(
00175 (int) (x1 + ua*(x2 - x1) / denominador),
00176 (int) (y1 + ub*(y2 - y1) / denominador)
00177 );
00178 return p;
00179 }
00180
00182
00183 void drawPoints(const QList<QPoint> &hotPoints, QVImage<uChar,3> &dest)
00184 {
00185 QListIterator<QPoint> iterator(hotPoints);
00186 uChar pixel[3] = { 255,0,0 };
00187
00188 while(iterator.hasNext())
00189 {
00190 QPoint p = iterator.next();
00191 QVPolyline rectangle = QVPolyline::rectangle(p.x()-2, p.y()-2, p.x()+2, p.y()+2);
00192 draw(dest, rectangle, pixel,true,true);
00193 }
00194 }
00195
00196 void drawPoints(const QList<QPoint> &hotPoints, QVImage<uChar> &dest)
00197 {
00198 QListIterator<QPoint> iterator(hotPoints);
00199 while(iterator.hasNext())
00200 {
00201 QPoint p = iterator.next();
00202 QVPolyline rectangle = QVPolyline::rectangle(p.x()-2, p.y()-2, p.x()+2, p.y()+2);
00203 draw(dest, rectangle, 255,true,true);
00204 }
00205 }
00206
00207
00208 void draw(QVImage<uChar> &image, const QVPolyline &polyline, const uChar constant, bool linked, bool safe)
00209 {
00210 Q_ASSERT_X(polyline.size() > 0, "drawPolyline()", "polyline size equals zero");
00211
00212 QListIterator<QPoint> iterator(polyline);
00213 QRect roi = image.getROI();
00214
00215 QPoint previous;
00216
00217 if (linked)
00218 previous = iterator.next();
00219
00220 QVIMAGE_INIT_WRITE(uChar,image);
00221 while (iterator.hasNext())
00222 {
00223 QPoint actual = iterator.next();
00224 int x = actual.x(), y = actual.y();
00225 if (!linked && !safe)
00226 QVIMAGE_PIXEL(image, x, y,0) = constant;
00227 else if (linked)
00228 {
00229 QVPolyline line = QVPolyline::line(x, y, previous.rx(), previous.ry());
00230 draw(image, line, constant, false, safe);
00231 previous = actual;
00232 }
00233 else if (roi.contains(x, y))
00234 QVIMAGE_PIXEL(image, x, y,0) = constant;
00235 }
00236 }
00237
00238 void draw(QVImage<uChar,3> &image, const QVPolyline &polyline, const uChar constant[3], bool linked, bool safe)
00239 {
00240 Q_ASSERT_X(polyline.size() > 0, "drawPolyline()", "polyline size equals zero");
00241
00242
00243 QListIterator<QPoint> iterator(polyline);
00244 QRect roi = image.getROI();
00245
00246 QPoint previous;
00247
00248 if (linked)
00249 previous = iterator.next();
00250
00251 QVIMAGE_INIT_WRITE(uChar,image);
00252 while (iterator.hasNext())
00253 {
00254 QPoint actual = iterator.next();
00255 int x = actual.x(), y = actual.y();
00256 if (!linked && !safe)
00257 {
00258 QVIMAGE_PIXEL(image, x, y,0) = constant[0];
00259 QVIMAGE_PIXEL(image, x, y,1) = constant[1];
00260 QVIMAGE_PIXEL(image, x, y,2) = constant[2];
00261
00262
00263
00264 }
00265 else if (linked)
00266 {
00267 QVPolyline line = QVPolyline::line(actual.rx(), actual.ry(), previous.rx(), previous.ry());
00268 draw(image, line, constant, false, safe);
00269 previous = actual;
00270 }
00271 else if (roi.contains(actual.rx(), actual.ry()))
00272 {
00273 QVIMAGE_PIXEL(image, x, y,0) = constant[0];
00274 QVIMAGE_PIXEL(image, x, y,1) = constant[1];
00275 QVIMAGE_PIXEL(image, x, y,2) = constant[2];
00276
00277
00278
00279 }
00280 }
00281 }
00282
00283
00284 void draw(QVImage<uChar> &img, const QList< QVPolyline > &polylineList, const uChar constant, bool linked, bool safe)
00285 {
00286 QListIterator<QVPolyline> iterator(polylineList);
00287 while (iterator.hasNext())
00288 draw(img, iterator.next(), constant, linked, safe);
00289 }
00290
00291 void draw(QVImage<uChar,3> &img,const QList< QVPolyline > &polylineList, const uChar constant[3],bool linked,bool safe)
00292 {
00293 QListIterator<QVPolyline> iterator(polylineList);
00294 while (iterator.hasNext())
00295 draw(img, iterator.next(), constant, linked, safe);
00296 }
00297
00298
00299 QVPolyline QVPolyline::ellipse(uInt n, float x, float y, float maxRadio, float minRadio, float ang)
00300 {
00301 QVPolyline ellipse;
00302 float w = 2*CONST_PI/(n-1);
00303 float maxArg = (maxRadio+minRadio)/2;
00304 float minArg = (maxRadio-minRadio)/2;
00305
00306 for (uInt t = 0; t < n; t++)
00307 ellipse.append(QPoint ( (uInt) (x + maxArg*cos(ang+w*t) + minArg*cos(ang-w*t)),
00308 (uInt) (y + maxArg*sin(ang+w*t) + minArg*sin(ang-w*t))
00309 ));
00310 return ellipse;
00311 }
00312
00313 QVPolyline QVPolyline::rectangle(int x1, int y1, int x2, int y2)
00314 {
00315 QVPolyline rectangle;
00316 rectangle.append(QPoint( x1, y1 ));
00317 rectangle.append(QPoint( x1, y2 ));
00318 rectangle.append(QPoint( x2, y2 ));
00319 rectangle.append(QPoint( x2, y1 ));
00320 rectangle.append(QPoint( x1, y1 ));
00321 return rectangle;
00322 }
00323
00324 QVPolyline QVPolyline::line(int x1, int y1, int x2, int y2)
00325 {
00326 QVPolyline line;
00327
00328 line.append(QPoint( x1, y1 ));
00329
00330 if (x1 == x2 && y1 == y2)
00331 return line;
00332
00333 int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
00334
00335 dx=x2-x1;
00336 dy=y2-y1;
00337 dxabs=abs(dx);
00338 dyabs=abs(dy);
00339 sdx=SIGN(dx);
00340 sdy=SIGN(dy);
00341 x=dyabs>>1;
00342 y=dxabs>>1;
00343 px=x1;
00344 py=y1;
00345
00346 if (dxabs>=dyabs)
00347 for(i=0;i<dxabs;i++)
00348 {
00349 y+=dyabs;
00350 if (y>=dxabs)
00351 {
00352 y-=dxabs;
00353 py+=sdy;
00354 }
00355 px+=sdx;
00356 line.append(QPoint( px, py ));
00357 }
00358 else
00359 for(i=0;i<dyabs;i++)
00360 {
00361 x+=dxabs;
00362 if (x>=dyabs)
00363 {
00364 x-=dyabs;
00365 px+=sdx;
00366 }
00367 py+=sdy;
00368 line.append(QPoint( px, py ));
00369 }
00370
00371 return line;
00372 }
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00520 #define dist2(A,B) (((A).rx() -(B).rx())*((A).rx() -(B).rx()) + ((A).ry() -(B).ry())*((A).ry() -(B).ry()))
00521 #define dot(A,B) ( (A).rx()*(B).rx() + (A).ry()*(B).ry() )
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636