00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef O2SCL_CONTOUR_H
00024 #define O2SCL_CONTOUR_H
00025
00026 #include <gsl/gsl_math.h>
00027 #include <o2scl/smart_interp.h>
00028 #include <o2scl/omatrix_tlate.h>
00029 #include <o2scl/pinside.h>
00030
00031 #ifndef DOXYGENP
00032 namespace o2scl {
00033 #endif
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126 class contour {
00127
00128 public:
00129
00130 contour();
00131 ~contour();
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 template<class vec_t, class mat_t>
00147 int set_data(size_t sizex, size_t sizey, const vec_t &x_fun,
00148 const vec_t &y_fun, const mat_t &udata) {
00149 int i;
00150
00151 if (sizex<2 || sizey<2) {
00152 set_err_ret("Not enough data (must be at least 2x2) in set_data().",
00153 gsl_einval);
00154 }
00155
00156 free_memory();
00157
00158 nx=sizex;
00159 ny=sizey;
00160 xfun=new ovector(nx);
00161 yfun=new ovector(ny);
00162 for(int is=0;is<nx;is++) (*xfun)[is]=x_fun[is];
00163 for(int is=0;is<ny;is++) (*yfun)[is]=y_fun[is];
00164
00165 data=new ovector *[ny];
00166 for(i=0;i<ny;i++) {
00167 data[i]=new ovector(nx);
00168 for(int j=0;j<nx;j++) {
00169 (*(data[i]))[j]=udata[i][j];
00170 }
00171 }
00172
00173 return 0;
00174 }
00175
00176
00177
00178
00179
00180
00181
00182
00183 template<class vec_t> int set_levels(size_t nlevels, vec_t &ulevels) {
00184 nlev=nlevels;
00185 levels.allocate(nlevels);
00186 for(size_t i=0;i<nlevels;i++) {
00187 levels[i]=ulevels[i];
00188 }
00189 levels_set=true;
00190 return 0;
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 template<class vec_t>
00205 int calc_contours(vec_t &new_levels, bool debug=false) {
00206 int i,j,k,ileft,iright,m;
00207
00208 for(i=0;i<nlev;i++) new_levels[i]=levels[i];
00209
00210 if (nx==0) {
00211 set_err("Data not set in calc_contours().",gsl_einval);
00212 return 0;
00213 }
00214 if (levels_set==false) {
00215 set_err("Contour levels not set in calc_contours().",gsl_einval);
00216 return 0;
00217 }
00218
00219
00220 if (ncurves!=0) {
00221 csizes.clear();
00222 vals.clear();
00223 xc.clear();
00224 yc.clear();
00225 ncurves=0;
00226 }
00227
00228
00229 for(j=0;j<((int)(re.size()));j++) {
00230 delete re[j];
00231 delete be[j];
00232 delete redges[j];
00233 delete bedges[j];
00234 }
00235 re.clear();
00236 be.clear();
00237 redges.clear();
00238 bedges.clear();
00239
00240
00241 int nint;
00242 double *xi, *yi;
00243
00244
00245 bool foundline, linedone;
00246 int fp;
00247
00248
00249 std::vector<double> x, y;
00250
00251
00252
00253 linear_interp<ovector_view> it1;
00254 linear_interp<ovector_const_reverse> it2;
00255 linear_interp<ovector_const_subvector> it3;
00256 linear_interp<ovector_const_subvector_reverse> it4;
00257 sm_interp *si=new sm_interp(it1,it2,it3,it4);
00258
00259
00260 ncurves=0;
00261
00262
00263 for(i=0;i<nlev;i++) {
00264
00265
00266 rep=new omatrix_int(ny-1,nx);
00267 bep=new omatrix_int(ny,nx-1);
00268 redgesp=new omatrix(ny-1,nx);
00269 bedgesp=new omatrix(ny,nx-1);
00270 re.push_back(rep);
00271 be.push_back(bep);
00272 redges.push_back(redgesp);
00273 bedges.push_back(bedgesp);
00274
00275 if (verbose>0) {
00276 std::cout << "\nLooking for edges for level: "
00277 << new_levels[i] << std::endl;
00278 }
00279
00280
00281 find_intersections(i,new_levels[i]);
00282
00283 if (verbose>0) {
00284 std::cout << "\nInterpolating edge intersections for level: "
00285 << new_levels[i] << std::endl;
00286 }
00287
00288
00289 right_edges(new_levels[i],si);
00290
00291
00292 bottom_edges(new_levels[i],si);
00293
00294 if (verbose>0) {
00295 std::cout << "\nPiecing together contour lines for level: "
00296 << new_levels[i] << std::endl;
00297 }
00298
00299
00300 foundline=true;
00301 while(foundline==true) {
00302 for(j=0;j<nx;j++) {
00303 for(k=0;k<ny;k++) {
00304 foundline=false;
00305
00306
00307 if (k<ny-1 && (*rep)[k][j]==edge) {
00308 if (verbose>0) {
00309 std::cout << "Starting contour line: " << std::endl;
00310 std::cout << "(" << (*xfun)[j] << ", " << (*redgesp)[k][j]
00311 << ")" << std::endl;
00312 }
00313 x.push_back((*xfun)[j]);
00314 y.push_back((*redgesp)[k][j]);
00315 (*rep)[k][j]++;
00316
00317
00318 process_line(j,k,dright,x,y,true);
00319 if (verbose>0) {
00320 std::cout << "Computing other side of line." << std::endl;
00321 }
00322 process_line(j,k,dright,x,y,false);
00323 foundline=true;
00324 }
00325
00326
00327 if (j<nx-1 && foundline==false && (*bep)[k][j]==edge) {
00328 if (verbose>0) {
00329 std::cout << "Starting contour line: " << std::endl;
00330 std::cout << "(" << (*bedgesp)[k][j] << ", " << (*yfun)[k]
00331 << ")" << std::endl;
00332 }
00333 x.push_back((*bedgesp)[k][j]);
00334 y.push_back((*yfun)[k]);
00335 (*bep)[k][j]++;
00336
00337
00338 process_line(j,k,dbottom,x,y,true);
00339 if (verbose>0) {
00340 std::cout << "Computing other side of line." << std::endl;
00341 }
00342 process_line(j,k,dbottom,x,y,false);
00343 foundline=true;
00344 }
00345
00346
00347
00348 if (foundline==true) {
00349 csizes.push_back(x.size());
00350 vals.push_back(new_levels[i]);
00351 ncurves++;
00352 std::vector<double> xv, yv;
00353 for(m=0;m<((int)x.size());m++) {
00354 xv.push_back(x[m]);
00355 yv.push_back(y[m]);
00356 }
00357 xc.push_back(xv);
00358 yc.push_back(yv);
00359 x.clear();
00360 y.clear();
00361 }
00362 }
00363 }
00364 }
00365 }
00366
00367 return ncurves;
00368 }
00369
00370
00371 int get_contour_size(int index);
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 template<class vec_t> int get_contour(int index, double &val, int &csize,
00385 vec_t &x, vec_t &y) {
00386 int i;
00387 if (index>=ncurves) {
00388 set_err_ret("No curve with specified index in get_contour().",
00389 gsl_efailed);
00390 }
00391
00392 csize=csizes[index];
00393 val=vals[index];
00394 for(i=0;i<csize;i++) {
00395 x[i]=xc[index][i];
00396 y[i]=yc[index][i];
00397 }
00398 return 0;
00399 }
00400
00401
00402
00403
00404 int regions(bool larger);
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 int regrid_data(size_t xfact, size_t yfact);
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434 int get_data(size_t &sizex, size_t &sizey, const ovector *&x_fun,
00435 const ovector *&y_fun, const ovector **&udata) {
00436 if (nx==0) {
00437 set_err_ret("Data not set in calc().",gsl_einval);
00438 }
00439 sizex=nx;
00440 sizey=ny;
00441 x_fun=xfun;
00442 y_fun=yfun;
00443 udata=(const ovector **)data;
00444 return 0;
00445 }
00446
00447
00448
00449 int get_edges(const std::vector<omatrix_int *> *rints,
00450 const std::vector<omatrix_int *> *bints,
00451 const std::vector<omatrix *> *rpoints,
00452 const std::vector<omatrix *> *bpoints);
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 int get_edges_for_level(size_t nl, omatrix_int &rints, omatrix_int &bints,
00463 omatrix &rpoints, omatrix &bpoints);
00464
00465
00466
00467
00468 int verbose;
00469
00470
00471 double lev_adjust;
00472
00473 #ifndef DOXYGEN_INTERNAL
00474
00475 protected:
00476
00477
00478
00479 static const int dright=0;
00480 static const int dbottom=1;
00481
00482
00483
00484
00485 static const int empty=0;
00486 static const int edge=1;
00487 static const int contourp=2;
00488 static const int endpoint=3;
00489
00490
00491
00492
00493 static const int efound=1;
00494 static const int enot_found=0;
00495
00496
00497
00498
00499 int nx, ny;
00500 ovector *xfun, *yfun, **data;
00501
00502
00503 int new_debug;
00504
00505
00506
00507 int nlev;
00508 ovector levels;
00509 bool levels_set;
00510
00511
00512
00513
00514 int ncurves;
00515 std::vector<int> csizes;
00516 std::vector<double> vals;
00517 std::vector< std::vector<double> > xc, yc;
00518
00519
00520
00521
00522 std::vector<omatrix *> redges, bedges;
00523 std::vector<omatrix_int *> re, be;
00524 omatrix_int *rep;
00525 omatrix_int *bep;
00526 omatrix *redgesp;
00527 omatrix *bedgesp;
00528
00529
00530
00531 pinside pi;
00532
00533
00534 int find_next_point_right(int j, int k, int &jnext, int &knext,
00535 int &dir_next, int nsw=1);
00536
00537
00538 int find_next_point_bottom(int j, int k, int &jnext, int &knext,
00539 int &dir_next, int nsw=1);
00540
00541
00542 int find_intersections(size_t ilev, double &level);
00543
00544
00545 int right_edges(double level, o2scl::sm_interp *si);
00546
00547
00548 int bottom_edges(double level, o2scl::sm_interp *si);
00549
00550
00551 int process_line(int j, int k, int dir, std::vector<double> &x,
00552 std::vector<double> &y, bool first=true);
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 bool is_point_inside_old(double x1, double y1, const ovector_view &x,
00571 const ovector_view &y, double xscale=0.01,
00572 double yscale=0.01);
00573
00574
00575 int free_memory();
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591 bool lines_cross_old(double x1, double y1, double x2, double y2,
00592 double x3, double y3, double x4, double y4);
00593
00594
00595 int check_data();
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 int smooth_contours(size_t nfact);
00608
00609 #endif
00610
00611 };
00612
00613 #ifndef DOXYGENP
00614 }
00615 #endif
00616
00617 #endif
00618
00619