#### /VectorField2D.cpp

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