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