/VectorField2D.cpp

http://nscow.googlecode.com/ · C++ · 103 lines · 82 code · 15 blank · 6 comment · 19 complexity · f103d8ce721e775ebe96f2008a6b7ecb MD5 · raw file

  1. /*
  2. * VectorField2D.cpp
  3. *
  4. * Created on: Apr 24, 2010
  5. * Author: wto
  6. */
  7. #include <algorithm>
  8. #include <iostream>
  9. #include <cmath>
  10. using namespace std;
  11. #include "VectorField2D.h"
  12. VectorField2D::VectorField2D(int xdim, int ydim, double boundryScale, double* data) :
  13. xdim(xdim), ydim(ydim), itsData(data), ownsData(false), itsBoundryScale(boundryScale) {
  14. if (itsData == 0) {
  15. ownsData = true;
  16. itsData = new double[size()];
  17. }
  18. }
  19. VectorField2D::~VectorField2D() {
  20. if (ownsData) {
  21. delete[] itsData;
  22. }
  23. }
  24. VectorField2D::VectorField2D(const VectorField2D& other) :
  25. xdim(other.xdim), ydim(other.ydim), itsData(new double[size()]), ownsData(true), itsBoundryScale(other.itsBoundryScale) {
  26. copy(other.begin(),other.end(),itsData);
  27. }
  28. VectorField2D& VectorField2D::operator=(const VectorField2D& other) {
  29. if(other.xdim != xdim && other.ydim != ydim) {
  30. cerr << "Warning: Mismatched vector field dimensions." << endl;
  31. return *this;
  32. }
  33. itsBoundryScale = other.itsBoundryScale;
  34. copy(other.begin(),other.end(),itsData);
  35. return *this;
  36. }
  37. void VectorField2D::print() const {
  38. for (int k = 0; k < 2; k++) {
  39. cout << "[";
  40. cout << value(0, 0, k);
  41. for (int x = 1; x < xdim; x++) {
  42. cout << " " << value(x, 0, k);
  43. }
  44. for (int y = 1; y < ydim; y++) {
  45. cout << endl << " " << value(0, y, k);
  46. for (int x = 1; x < xdim; x++) {
  47. cout << " " << value(x, y, k);
  48. }
  49. }
  50. cout << "]" << endl << flush;
  51. }
  52. }
  53. double VectorField2D::checkBoundry(int x, int y, int k) const {
  54. double scale = 1;
  55. if (x < 0) {
  56. scale = itsBoundryScale;
  57. x = 0;
  58. } else if (x > xdim-1) {
  59. scale = itsBoundryScale;
  60. x = xdim-1;
  61. }
  62. if (y < 0) {
  63. scale = itsBoundryScale;
  64. y = 0;
  65. } else if (y > ydim-1) {
  66. scale = itsBoundryScale;
  67. y = ydim-1;
  68. }
  69. return scale*value(x,y,k);
  70. }
  71. double VectorField2D::min() const {
  72. return *min_element(begin(),end());
  73. }
  74. double VectorField2D::max() const {
  75. return *max_element(begin(),end());
  76. }
  77. double VectorField2D::mean() const {
  78. double ans = 0.0;
  79. for (int x = 0; x < xdim; x++) {
  80. for (int y = 0; y < ydim; y++) {
  81. ans += sqrt(value(x,y,0)*value(x,y,0) + value(x,y,1)*value(x,y,1));
  82. }
  83. }
  84. return ans / size();
  85. }
  86. void VectorField2D::fill(double val) {
  87. fill_n(itsData,size(),val);
  88. }