source: trunk/src2/core/RpField1D.cc @ 829

Last change on this file since 829 was 657, checked in by dkearney, 17 years ago

cleaned up all the compile time warnings in core rappture2 objects

File size: 3.9 KB
Line 
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
11 *  Copyright (c) 2004-2006  Purdue Research Foundation
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>
18#include <math.h>
19#include "RpField1D.h"
20
21using namespace Rappture;
22
23Field1D::Field1D()
24  : _vmin(NAN),
25    _vmax(NAN),
26    _meshPtr(NULL),
27    _counter(0)
28{
29    _meshPtr = Ptr<Mesh1D>(new Mesh1D());
30}
31
32Field1D::Field1D(const Ptr<Mesh1D>& meshPtr)
33  : _valuelist(),
34    _vmin(NAN),
35    _vmax(NAN),
36    _meshPtr(meshPtr),
37    _counter(0)
38{
39}
40
41Field1D::Field1D(const Field1D& field)
42  : _valuelist(field._valuelist),
43    _vmin(field._vmin),
44    _vmax(field._vmax),
45    _meshPtr(field._meshPtr),
46    _counter(0)
47{
48}
49
50Field1D&
51Field1D::operator=(const Field1D& field)
52{
53    _valuelist = field._valuelist;
54    _vmin = field._vmin;
55    _vmax = field._vmax;
56    _meshPtr = field._meshPtr;
57    _counter = field._counter;
58    return *this;
59}
60
61Field1D::~Field1D()
62{
63}
64
65int
66Field1D::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
77Field1D&
78Field1D::remove(int nodeId)
79{
80    _meshPtr->remove(nodeId);
81    return *this;
82}
83
84Field1D&
85Field1D::clear()
86{
87    _valuelist.clear();
88    _meshPtr->clear();
89    _counter = 0;
90    return *this;
91}
92
93int
94Field1D::size() const
95{
96    return _meshPtr->size();
97}
98
99Node1D&
100Field1D::atNode(int pos)
101{
102    return _meshPtr->at(pos);
103}
104
105double
106Field1D::rangeMin() const
107{
108    return _meshPtr->rangeMin();
109}
110
111double
112Field1D::rangeMax() const
113{
114    return _meshPtr->rangeMax();
115}
116
117int
118Field1D::define(double x, double y)
119{
120    Node1D node = Node1D(x);
121    Cell1D cell = _meshPtr->locate(node);
122
123    if (x == cell.x(0) && !cell.isOutside()) {
124        define(cell.nodeId(0), y);
125    }
126    else if (x == cell.x(1) && !cell.isOutside()) {
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    }
136    return 0;
137}
138
139int
140Field1D::define(int nodeId, double y)
141{
142    _valuelist[nodeId] = y;
143
144    if (isnan(_vmin) || isnan(_vmax)) {
145        _vmin = _vmax = y;
146    } else {
147        if (y < _vmin) { _vmin = y; }
148        if (y > _vmax) { _vmax = y; }
149    }
150    return 0;
151}
152
153double
154Field1D::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}
187
188double
189Field1D::valueMin() const
190{
191    return _vmin;
192}
193
194double
195Field1D::valueMax() const
196{
197    return _vmax;
198}
Note: See TracBrowser for help on using the repository browser.