comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/XYColumn.java @ 875:5e9efdda6894

merged gnv-artifacts/1.0
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:56 +0200
parents 05bf8534a35a
children f953c9a559d8
comparison
equal deleted inserted replaced
722:bb3ffe7d719e 875:5e9efdda6894
1 package de.intevation.gnv.math;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.List;
6
7 import org.apache.commons.math.FunctionEvaluationException;
8 import org.apache.commons.math.MathException;
9
10 import org.apache.commons.math.analysis.UnivariateRealFunction;
11
12 import org.apache.commons.math.analysis.interpolation.SplineInterpolator;
13 import org.apache.commons.math.analysis.interpolation.UnivariateRealInterpolator;
14
15 import org.apache.log4j.Logger;
16
17 /**
18 * A column of discrete attributed height values located at a point.
19 * Values between the discrete height values are interpolated.
20 *
21 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
22 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
23 */
24 public class XYColumn
25 extends Point2d
26 implements UnivariateRealFunction
27 {
28 private static Logger log = Logger.getLogger(XYColumn.class);
29
30 /**
31 * The list of discrete height values.
32 */
33 protected List<HeightValue> values;
34
35 /**
36 * The curve used to interpolate the points between the
37 * discrete height values.
38 */
39 protected transient UnivariateRealFunction curve;
40
41 /**
42 * Default constructor.
43 */
44 public XYColumn() {
45 values = new ArrayList<HeightValue>();
46 }
47
48 /**
49 * Constructor to create an XYColumn with a given (x, y) coordinate
50 * and an (i, j) index tuple.
51 * @param x The x coordinate.
52 * @param y The y coordinate.
53 * @param i The i component.
54 * @param j The j component.
55 */
56 public XYColumn(double x, double y, int i, int j) {
57 super(x, y, i, j);
58 values = new ArrayList<HeightValue>();
59 }
60
61 /**
62 * Adds a given height value to the list of height values.
63 * @param value The height value.
64 */
65 public void add(HeightValue value) {
66 values.add(value);
67 }
68
69 /**
70 * Returns the list of height values.
71 * @return The list of height values.
72 */
73 public List<HeightValue> getValues() {
74 return values;
75 }
76
77 public double value(double depth) {
78 try {
79 if (curve != null) {
80 HeightValue h = values.get(0);
81 // extrapolate beyond boundaries by repeating
82 if (depth > h.z) return h.v;
83 h = values.get(values.size()-1);
84 if (depth < h.z) return h.v;
85 return curve.value(depth);
86 }
87 }
88 catch (FunctionEvaluationException fee) {
89 log.error("evaluation failed", fee);
90 }
91
92 return Double.NaN;
93 }
94
95 /**
96 * Prepares this XYColumn to be queried. A given XYDepth
97 * object is used to fill the values to a certain depth.
98 * @param xyDepth To figure out the depth a the cordinate.
99 * @return true if preparation succeeds else false.
100 */
101 public boolean prepare(XYDepth xyDepth) {
102 if (curve == null) {
103 int N = values.size();
104 if (N == 0) {
105 log.error("no points for interpolation");
106 return false;
107 }
108
109 if (N == 1) {
110 // only one value -> constant function
111 curve = new ConstantFunction(values.get(0).v);
112 }
113 else { // more than on value
114 double depth = xyDepth.depth(this);
115 Collections.sort(values, HeightValue.INV_Z_COMPARATOR);
116
117 // if there is no value at 0 repeat first value
118 HeightValue first = values.get(0);
119 if (first.z < 0d) {
120 values.add(0, new HeightValue(0d, first.v, first.k-1));
121 ++N;
122 }
123
124 // if there is no value at depth repeat last value
125 HeightValue last = values.get(N-1);
126 if (last.z > depth) {
127 values.add(new HeightValue(depth, last.v, last.k+1));
128 ++N;
129 }
130 if (N < 3) { // interpolate linear
131 first = values.get(0);
132 last = values.get(N-1);
133 curve = new LinearFunction.Univariate(
134 first.z, first.v,
135 last.z, last.v);
136 }
137 else { // higher degree interpolation
138 double [] z = new double[N];
139 double [] v = new double[N];
140 for (int i = 0; i < N; ++i) {
141 HeightValue h = values.get(N-1-i);
142 z[i] = h.z;
143 v[i] = h.v;
144 }
145 try {
146 curve = getInterpolator().interpolate(z, v);
147 }
148 catch (MathException me) {
149 log.error("interpolation failed", me);
150 return false;
151 }
152 }
153 }
154 }
155 return true;
156 }
157
158 /**
159 * Returns the interpolator used to interpolate the values between
160 * the discrete height values. This class returns an instance of
161 * {@link org.apache.commons.math.analysis.interpolation.SplineInterpolator}.
162 * Override this if you want to use another kind of interpolation.
163 * @return The interpolator to be used.
164 */
165 protected UnivariateRealInterpolator getInterpolator() {
166 return new SplineInterpolator();
167 }
168 }
169 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org