[364] | 1 | /* |
---|
| 2 | * ---------------------------------------------------------------------- |
---|
| 3 | * Rappture::Field1D |
---|
| 4 | * This is a continuous, linear function defined by a series of |
---|
| 5 | * points for 1-dimensional structures. It's essentially a string |
---|
| 6 | * of (x,y) points where the y values can be interpolated at any |
---|
| 7 | * point within the range of defined values. |
---|
| 8 | * |
---|
| 9 | * ====================================================================== |
---|
| 10 | * AUTHOR: Michael McLennan, Purdue University |
---|
[3959] | 11 | * Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
[364] | 12 | * |
---|
| 13 | * See the file "license.terms" for information on usage and |
---|
| 14 | * redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
| 15 | * ====================================================================== |
---|
| 16 | */ |
---|
| 17 | #include <assert.h> |
---|
[372] | 18 | #include <math.h> |
---|
[364] | 19 | #include "RpField1D.h" |
---|
| 20 | |
---|
| 21 | using namespace Rappture; |
---|
| 22 | |
---|
| 23 | Field1D::Field1D() |
---|
[372] | 24 | : _vmin(NAN), |
---|
| 25 | _vmax(NAN), |
---|
| 26 | _meshPtr(NULL), |
---|
[364] | 27 | _counter(0) |
---|
| 28 | { |
---|
| 29 | _meshPtr = Ptr<Mesh1D>(new Mesh1D()); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | Field1D::Field1D(const Ptr<Mesh1D>& meshPtr) |
---|
| 33 | : _valuelist(), |
---|
[372] | 34 | _vmin(NAN), |
---|
| 35 | _vmax(NAN), |
---|
[364] | 36 | _meshPtr(meshPtr), |
---|
| 37 | _counter(0) |
---|
| 38 | { |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | Field1D::Field1D(const Field1D& field) |
---|
| 42 | : _valuelist(field._valuelist), |
---|
[372] | 43 | _vmin(field._vmin), |
---|
| 44 | _vmax(field._vmax), |
---|
[364] | 45 | _meshPtr(field._meshPtr), |
---|
| 46 | _counter(0) |
---|
| 47 | { |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | Field1D& |
---|
| 51 | Field1D::operator=(const Field1D& field) |
---|
| 52 | { |
---|
| 53 | _valuelist = field._valuelist; |
---|
[372] | 54 | _vmin = field._vmin; |
---|
| 55 | _vmax = field._vmax; |
---|
[364] | 56 | _meshPtr = field._meshPtr; |
---|
| 57 | _counter = field._counter; |
---|
[657] | 58 | return *this; |
---|
[364] | 59 | } |
---|
| 60 | |
---|
| 61 | Field1D::~Field1D() |
---|
| 62 | { |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int |
---|
| 66 | Field1D::add(double x) |
---|
| 67 | { |
---|
| 68 | Node1D node = Node1D(x); |
---|
| 69 | node.id(_counter++); |
---|
| 70 | |
---|
| 71 | _valuelist.push_back(0.0); |
---|
| 72 | _meshPtr->add(node); |
---|
| 73 | |
---|
| 74 | return node.id(); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | Field1D& |
---|
| 78 | Field1D::remove(int nodeId) |
---|
| 79 | { |
---|
| 80 | _meshPtr->remove(nodeId); |
---|
| 81 | return *this; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | Field1D& |
---|
| 85 | Field1D::clear() |
---|
| 86 | { |
---|
| 87 | _valuelist.clear(); |
---|
| 88 | _meshPtr->clear(); |
---|
| 89 | _counter = 0; |
---|
| 90 | return *this; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | int |
---|
| 94 | Field1D::size() const |
---|
| 95 | { |
---|
| 96 | return _meshPtr->size(); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | Node1D& |
---|
[370] | 100 | Field1D::atNode(int pos) |
---|
[364] | 101 | { |
---|
| 102 | return _meshPtr->at(pos); |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | double |
---|
| 106 | Field1D::rangeMin() const |
---|
| 107 | { |
---|
| 108 | return _meshPtr->rangeMin(); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | double |
---|
| 112 | Field1D::rangeMax() const |
---|
| 113 | { |
---|
| 114 | return _meshPtr->rangeMax(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | int |
---|
| 118 | Field1D::define(double x, double y) |
---|
| 119 | { |
---|
| 120 | Node1D node = Node1D(x); |
---|
| 121 | Cell1D cell = _meshPtr->locate(node); |
---|
| 122 | |
---|
[446] | 123 | if (x == cell.x(0) && !cell.isOutside()) { |
---|
[364] | 124 | define(cell.nodeId(0), y); |
---|
| 125 | } |
---|
[446] | 126 | else if (x == cell.x(1) && !cell.isOutside()) { |
---|
[364] | 127 | define(cell.nodeId(1), y); |
---|
| 128 | } |
---|
| 129 | else { |
---|
| 130 | int nodeId = _counter++; |
---|
| 131 | node.id(nodeId); |
---|
| 132 | _valuelist.push_back(0.0); |
---|
| 133 | _meshPtr->add(node); |
---|
| 134 | define(nodeId, y); |
---|
| 135 | } |
---|
[657] | 136 | return 0; |
---|
[364] | 137 | } |
---|
| 138 | |
---|
| 139 | int |
---|
| 140 | Field1D::define(int nodeId, double y) |
---|
| 141 | { |
---|
| 142 | _valuelist[nodeId] = y; |
---|
[372] | 143 | |
---|
[454] | 144 | if (isnan(_vmin) || isnan(_vmax)) { |
---|
[372] | 145 | _vmin = _vmax = y; |
---|
| 146 | } else { |
---|
| 147 | if (y < _vmin) { _vmin = y; } |
---|
| 148 | if (y > _vmax) { _vmax = y; } |
---|
| 149 | } |
---|
[657] | 150 | return 0; |
---|
[364] | 151 | } |
---|
| 152 | |
---|
| 153 | double |
---|
| 154 | Field1D::value(double x) const |
---|
| 155 | { |
---|
| 156 | Node1D node = Node1D(x); |
---|
| 157 | Cell1D cell = _meshPtr->locate(node); |
---|
| 158 | |
---|
| 159 | // if this is a normal cell, then interpolate values |
---|
| 160 | if (cell.nodeId(0) >= 0 && cell.nodeId(1) >= 0) { |
---|
| 161 | double y0 = _valuelist[cell.nodeId(0)]; |
---|
| 162 | double y1 = _valuelist[cell.nodeId(1)]; |
---|
| 163 | double xrange = cell.x(1) - cell.x(0); |
---|
| 164 | |
---|
| 165 | if (xrange == 0.0) { |
---|
| 166 | // interval undefined? then return avg y value |
---|
| 167 | return 0.5*(y1+y0); |
---|
| 168 | } |
---|
| 169 | // interpolate |
---|
| 170 | double delx = x - cell.x(0); |
---|
| 171 | return y0 + (delx/xrange)*(y1-y0); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | // extrapolate with constant value for x < xmin |
---|
| 175 | else if (cell.nodeId(0) >= 0) { |
---|
| 176 | return _valuelist[cell.nodeId(0)]; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | // extrapolate with constant value for x > xmax |
---|
| 180 | else if (cell.nodeId(1) >= 0) { |
---|
| 181 | return _valuelist[cell.nodeId(1)]; |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | // all else fails, return an empty value |
---|
| 185 | return 0.0; |
---|
| 186 | } |
---|
[372] | 187 | |
---|
| 188 | double |
---|
| 189 | Field1D::valueMin() const |
---|
| 190 | { |
---|
| 191 | return _vmin; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | double |
---|
| 195 | Field1D::valueMax() const |
---|
| 196 | { |
---|
| 197 | return _vmax; |
---|
| 198 | } |
---|