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
00030 namespace qvdta
00031 {
00033
00034 QVPolyline::QVPolyline(): QList<QPoint>(),
00035 closed(false), direction(false)
00036 {
00037 qDebug() << "QVPolyline()";
00038 qDebug() << "QVPolyline() <~ return";
00039 };
00040
00041 QVPolyline::QVPolyline(const QVPolyline &polyline): QList<QPoint>(polyline),
00042 closed(polyline.closed), direction(polyline.direction)
00043 {
00044 qDebug() << "QVPolyline(const QVPolyline &)";
00045 qDebug() << "QVPolyline(const QVPolyline &) <~ return";
00046 };
00047
00048 QPoint linesIntersection(QPoint a, QPoint b, QPoint c, QPoint d)
00049
00050 {
00051 double x1 = a.rx(), x2 = b.rx(), x3 = c.rx(), x4 = d.rx();
00052 double y1 = a.ry(), y2 = b.ry(), y3 = c.ry(), y4 = d.ry();
00053
00054 double denominador = (y4 - y3)*(x2 - x1) - (x4 - x3)*(y2 - y1);
00055 if (denominador == 0)
00056 return QPoint( (int)(b.rx() + c.rx())/2, (int)(b.ry() + c.ry())/2 );
00057
00058 double ua = (x4 - x3)*(y1 - y3) - (y4 - y3)*(x1 - x3),
00059 ub = (x2 - x1)*(y1 - y3) - (y2 - y1)*(x1 - x3);
00060
00061 QPoint p = QPoint(
00062 (int) (x1 + ua*(x2 - x1) / denominador),
00063 (int) (y1 + ub*(y2 - y1) / denominador)
00064 );
00065 return p;
00066 }
00067
00069
00070 void drawPoints(const QList<QPoint> &hotPoints, QVImage<uChar,3> &dest)
00071 {
00072 QListIterator<QPoint> iterator(hotPoints);
00073 uChar pixel[3] = { 255,0,0 };
00074
00075 while(iterator.hasNext())
00076 {
00077 QPoint p = iterator.next();
00078 QVPolyline rectangle = QVPolyline::rectangle(p.x()-2, p.y()-2, p.x()+2, p.y()+2);
00079 draw(dest, rectangle, pixel,true,true);
00080 }
00081 }
00082
00083 void drawPoints(const QList<QPoint> &hotPoints, QVImage<uChar> &dest)
00084 {
00085 QListIterator<QPoint> iterator(hotPoints);
00086 while(iterator.hasNext())
00087 {
00088 QPoint p = iterator.next();
00089 QVPolyline rectangle = QVPolyline::rectangle(p.x()-2, p.y()-2, p.x()+2, p.y()+2);
00090 draw(dest, rectangle, 255,true,true);
00091 }
00092 }
00093
00094
00095 void draw(QVImage<uChar> &image, const QVPolyline &polyline, const uChar constant, bool linked, bool safe)
00096 {
00097 Q_ASSERT_X(polyline.size() > 0, "drawPolyline()", "polyline size equals zero");
00098
00099 QListIterator<QPoint> iterator(polyline);
00100 QRect roi = image.getROI();
00101
00102 QPoint previous;
00103
00104 if (linked)
00105 previous = iterator.next();
00106
00107 QVIMAGE_INIT_WRITE(uChar,image);
00108 while (iterator.hasNext())
00109 {
00110 QPoint actual = iterator.next();
00111 int x = actual.x(), y = actual.y();
00112 if (!linked && !safe)
00113 QVIMAGE_PIXEL(image, x, y,0) = constant;
00114 else if (linked)
00115 {
00116 QVPolyline line = QVPolyline::line(x, y, previous.rx(), previous.ry());
00117 draw(image, line, constant, false, safe);
00118 previous = actual;
00119 }
00120 else if (roi.contains(x, y))
00121 QVIMAGE_PIXEL(image, x, y,0) = constant;
00122 }
00123 }
00124
00125 void draw(QVImage<uChar,3> &image, const QVPolyline &polyline, const uChar constant[3], bool linked, bool safe)
00126 {
00127 Q_ASSERT_X(polyline.size() > 0, "drawPolyline()", "polyline size equals zero");
00128
00129
00130 QListIterator<QPoint> iterator(polyline);
00131 QRect roi = image.getROI();
00132
00133 QPoint previous;
00134
00135 if (linked)
00136 previous = iterator.next();
00137
00138 QVIMAGE_INIT_WRITE(uChar,image);
00139 while (iterator.hasNext())
00140 {
00141 QPoint actual = iterator.next();
00142 int x = actual.x(), y = actual.y();
00143 if (!linked && !safe)
00144 {
00145 QVIMAGE_PIXEL(image, x, y,0) = constant[0];
00146 QVIMAGE_PIXEL(image, x, y,1) = constant[1];
00147 QVIMAGE_PIXEL(image, x, y,2) = constant[2];
00148
00149
00150
00151 }
00152 else if (linked)
00153 {
00154 QVPolyline line = QVPolyline::line(actual.rx(), actual.ry(), previous.rx(), previous.ry());
00155 draw(image, line, constant, false, safe);
00156 previous = actual;
00157 }
00158 else if (roi.contains(actual.rx(), actual.ry()))
00159 {
00160 QVIMAGE_PIXEL(image, x, y,0) = constant[0];
00161 QVIMAGE_PIXEL(image, x, y,1) = constant[1];
00162 QVIMAGE_PIXEL(image, x, y,2) = constant[2];
00163
00164
00165
00166 }
00167 }
00168 }
00169
00170
00171 void draw(QVImage<uChar> &img, const QList< QVPolyline > &polylineList, const uChar constant, bool linked, bool safe)
00172 {
00173 QListIterator<QVPolyline> iterator(polylineList);
00174 while (iterator.hasNext())
00175 draw(img, iterator.next(), constant, linked, safe);
00176 }
00177
00178 void draw(QVImage<uChar,3> &img,const QList< QVPolyline > &polylineList, const uChar constant[3],bool linked,bool safe)
00179 {
00180 QListIterator<QVPolyline> iterator(polylineList);
00181 while (iterator.hasNext())
00182 draw(img, iterator.next(), constant, linked, safe);
00183 }
00184
00185
00186 QVPolyline QVPolyline::ellipse(uInt n, float x, float y, float maxRadio, float minRadio, float ang)
00187 {
00188 QVPolyline ellipse;
00189 float w = 2*CONST_PI/(n-1);
00190 float maxArg = (maxRadio+minRadio)/2;
00191 float minArg = (maxRadio-minRadio)/2;
00192
00193 for (uInt t = 0; t < n; t++)
00194 ellipse.append(QPoint ( (uInt) (x + maxArg*cos(ang+w*t) + minArg*cos(ang-w*t)),
00195 (uInt) (y + maxArg*sin(ang+w*t) + minArg*sin(ang-w*t))
00196 ));
00197 return ellipse;
00198 }
00199
00200 QVPolyline QVPolyline::rectangle(int x1, int y1, int x2, int y2)
00201 {
00202 QVPolyline rectangle;
00203 rectangle.append(QPoint( x1, y1 ));
00204 rectangle.append(QPoint( x1, y2 ));
00205 rectangle.append(QPoint( x2, y2 ));
00206 rectangle.append(QPoint( x2, y1 ));
00207 rectangle.append(QPoint( x1, y1 ));
00208 return rectangle;
00209 }
00210
00211 QVPolyline QVPolyline::line(int x1, int y1, int x2, int y2)
00212 {
00213 QVPolyline line;
00214
00215 line.append(QPoint( x1, y1 ));
00216
00217 if (x1 == x2 && y1 == y2)
00218 return line;
00219
00220 int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;
00221
00222 dx=x2-x1;
00223 dy=y2-y1;
00224 dxabs=abs(dx);
00225 dyabs=abs(dy);
00226 sdx=SIGN(dx);
00227 sdy=SIGN(dy);
00228 x=dyabs>>1;
00229 y=dxabs>>1;
00230 px=x1;
00231 py=y1;
00232
00233 if (dxabs>=dyabs)
00234 for(i=0;i<dxabs;i++)
00235 {
00236 y+=dyabs;
00237 if (y>=dxabs)
00238 {
00239 y-=dxabs;
00240 py+=sdy;
00241 }
00242 px+=sdx;
00243 line.append(QPoint( px, py ));
00244 }
00245 else
00246 for(i=0;i<dyabs;i++)
00247 {
00248 x+=dxabs;
00249 if (x>=dyabs)
00250 {
00251 x-=dyabs;
00252 px+=sdx;
00253 }
00254 py+=sdy;
00255 line.append(QPoint( px, py ));
00256 }
00257
00258 return line;
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
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
00407 #define dist2(A,B) (((A).rx() -(B).rx())*((A).rx() -(B).rx()) + ((A).ry() -(B).ry())*((A).ry() -(B).ry()))
00408 #define dot(A,B) ( (A).rx()*(B).rx() + (A).ry()*(B).ry() )
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
00519
00520
00521
00522
00523 }