Changeset 17338 for trunk/Mars/mcore
- Timestamp:
- 11/21/13 17:26:27 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Mars/mcore/Interpolator2D.h
r17143 r17338 20 20 #include <math.h> 21 21 #include <vector> 22 #include <fstream> 22 23 23 24 class Interpolator2D … … 44 45 { 45 46 unsigned int i; 47 point(unsigned int _i, const vec &_v) : vec(_v), i(_i) { } 46 48 point(unsigned int _i=0, double _x=0, double _y=0) : vec(_x, _y), i(_i) { } 47 49 }; … … 296 298 // -------------------------------------------------------------------------- 297 299 // 300 //! helper function to read a grid from a simple file 301 //! (alternating x, y) 302 //! 303 //! @param filename 304 //! filename of ascii file with data 305 //! 306 //! @returns 307 //! a vector of point with the x and y values. 308 //! in case of failure the vector is empty 309 // 310 static std::vector<Interpolator2D::vec> ReadGrid(const std::string &filename) 311 { 312 std::vector<Interpolator2D::vec> grid; 313 314 std::ifstream fin(filename); 315 if (!fin.is_open()) 316 return std::vector<Interpolator2D::vec>(); 317 318 while (1) 319 { 320 double x, y; 321 fin >> x; 322 fin >> y; 323 if (!fin) 324 break; 325 326 grid.emplace_back(x, y); 327 } 328 329 return fin.bad() ? std::vector<Interpolator2D::vec>() : grid; 330 } 331 332 // -------------------------------------------------------------------------- 333 // 298 334 //! Set a new input grid (the points at which values are known). 299 335 //! Invalidates the output grid and the calculated weights. … … 309 345 //! y coordinates of data points 310 346 // 311 void SetInputGrid( int n, double *x, double *y)347 void SetInputGrid(unsigned int n, double *x, double *y) 312 348 { 313 349 circles.clear(); … … 317 353 inputGrid.clear(); 318 354 inputGrid.reserve(n); 319 for ( int i=0; i<n; i++)355 for (unsigned int i=0; i<n; i++) 320 356 inputGrid.emplace_back(i, x[i], y[i]); 321 357 … … 331 367 inputGrid.clear(); 332 368 inputGrid.reserve(v.size()); 333 for ( std::size_t i=0; i<v.size(); i++)334 inputGrid.emplace_back(i, v[i] .x, v[i].y);369 for (unsigned int i=0; i<v.size(); i++) 370 inputGrid.emplace_back(i, v[i]); 335 371 336 372 CalculateGrid(); 337 373 } 374 375 /* 376 void SetInputGrid(const std::vector<Interpolator2D::point> &v) 377 { 378 circles.clear(); 379 weights.clear(); 380 outputGrid.clear(); 381 382 inputGrid.clear(); 383 inputGrid.reserve(v.size()); 384 for (unsigned int i=0; i<v.size(); i++) 385 inputGrid.emplace_back(v[i], i); 386 387 CalculateGrid(); 388 }*/ 389 390 bool ReadInputGrid(const std::string &filename) 391 { 392 const auto grid = ReadGrid(filename); 393 if (grid.empty()) 394 return false; 395 396 SetInputGrid(grid); 397 return true; 398 } 399 338 400 339 401 // -------------------------------------------------------------------------- … … 355 417 //! case of success 356 418 // 357 bool SetOutputGrid( int n, double *x, double *y)358 { 359 if (inputGrid.empty() )419 bool SetOutputGrid(std::size_t n, double *x, double *y) 420 { 421 if (inputGrid.empty() && n==0) 360 422 return false; 361 423 … … 364 426 outputGrid.clear(); 365 427 outputGrid.reserve(n); 366 for ( int i=0; i<n; i++)428 for (std::size_t i=0; i<n; i++) 367 429 outputGrid.emplace_back(i, x[i], y[i]); 368 430 … … 370 432 } 371 433 434 /* 372 435 bool SetOutputGrid(const std::vector<std::pair<double,double>> &v) 373 436 { 374 if (inputGrid.empty() )437 if (inputGrid.empty() || v.empty()) 375 438 return false; 376 439 … … 383 446 384 447 return CalculateWeights(); 385 } 448 }*/ 449 450 bool SetOutputGrid(const std::vector<Interpolator2D::vec> &v) 451 { 452 if (inputGrid.empty()) 453 return false; 454 455 weights.clear(); 456 457 outputGrid.clear(); 458 outputGrid.reserve(v.size()); 459 for (std::size_t i=0; i<v.size(); i++) 460 outputGrid.emplace_back(i, v[i]); 461 462 return CalculateWeights(); 463 } 464 465 bool ReadOutputGrid(const std::string &filename) 466 { 467 const auto grid = ReadGrid(filename); 468 if (grid.empty()) 469 return false; 470 471 return SetOutputGrid(grid); 472 } 473 386 474 387 475 // --------------------------------------------------------------------------
Note:
See TracChangeset
for help on using the changeset viewer.