Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/Interpolation2D.java @ 807:a645bd23c1c8
Added more javadoc. Removed trailing whitespace.
gnv-artifacts/trunk@889 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Thu, 08 Apr 2010 15:24:45 +0000 |
parents | 6cff63d0c434 |
children | 05bf8534a35a |
comparison
equal
deleted
inserted
replaced
806:2cea76f1112e | 807:a645bd23c1c8 |
---|---|
8 import java.util.List; | 8 import java.util.List; |
9 | 9 |
10 import org.apache.log4j.Logger; | 10 import org.apache.log4j.Logger; |
11 | 11 |
12 /** | 12 /** |
13 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | 13 * Interpolates along a given line string. This used to generate |
14 * "horizontale Schnittprofile". | |
15 * | |
16 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> | |
14 */ | 17 */ |
15 public final class Interpolation2D | 18 public final class Interpolation2D |
16 { | 19 { |
17 private static Logger log = Logger.getLogger(Interpolation2D.class); | 20 private static Logger log = Logger.getLogger(Interpolation2D.class); |
18 | 21 |
22 /** | |
23 * If the number of input points are over this threshold | |
24 * some culling strategies are applied to reduce the amount of points. | |
25 */ | |
19 public static final int CULL_POINT_THRESHOLD = Integer.getInteger( | 26 public static final int CULL_POINT_THRESHOLD = Integer.getInteger( |
20 "gnv.interpolation2d.cull.point.threshold", 1000); | 27 "gnv.interpolation2d.cull.point.threshold", 1000); |
21 | 28 |
29 /** | |
30 * Numerical stability epsilon. | |
31 */ | |
22 public static final double EPS = 1e-6d; | 32 public static final double EPS = 1e-6d; |
23 | 33 |
34 /** | |
35 * Callback to send the interpolated values back to the | |
36 * caller without building large temporary strcutures. | |
37 */ | |
24 public interface Consumer { | 38 public interface Consumer { |
39 /** | |
40 * Sends an interpolated point back to the called. | |
41 * The interpolated parameter is stored in the z component. | |
42 * @param point The interpolated point. | |
43 * @param success true if interpolation at the point | |
44 * succeed, else false. | |
45 */ | |
25 void interpolated(Coordinate point, boolean success); | 46 void interpolated(Coordinate point, boolean success); |
26 } // interface Consumer | 47 } // interface Consumer |
27 | 48 |
28 private Interpolation2D() { | 49 private Interpolation2D() { |
29 } | 50 } |
30 | 51 |
52 /** | |
53 * Calculates the relevant area for a given line string. | |
54 * @param path The line string. | |
55 * @param points The sample points. | |
56 * @return The relevant area. | |
57 */ | |
31 public static final Envelope relevantArea( | 58 public static final Envelope relevantArea( |
32 List<? extends Coordinate> path, | 59 List<? extends Coordinate> path, |
33 List<? extends Coordinate> points | 60 List<? extends Coordinate> points |
34 ) { | 61 ) { |
35 return relevantArea(path, points, CULL_POINT_THRESHOLD); | 62 return relevantArea(path, points, CULL_POINT_THRESHOLD); |
36 } | 63 } |
37 | 64 |
65 /** | |
66 * Calculates the relevant area for a given bounding box. | |
67 * @param pathBBox The bounding box. | |
68 * @param points The sample points. | |
69 * @return The relevant area. | |
70 */ | |
38 public static final Envelope relevantArea( | 71 public static final Envelope relevantArea( |
39 Envelope pathBBox, | 72 Envelope pathBBox, |
40 List<? extends Coordinate> points | 73 List<? extends Coordinate> points |
41 ) { | 74 ) { |
42 return relevantArea(pathBBox, points, CULL_POINT_THRESHOLD); | 75 return relevantArea(pathBBox, points, CULL_POINT_THRESHOLD); |
43 } | 76 } |
44 | 77 |
78 /** | |
79 * Calculates the relevant area for a given bounding box. | |
80 * @param pathBBox The bounding box. | |
81 * @param points The sample points. | |
82 * @param threshold If the number of sample points | |
83 * are below this threshold no relevant area is calculated. | |
84 * @return The relevant area. | |
85 */ | |
45 public static final Envelope relevantArea( | 86 public static final Envelope relevantArea( |
46 Envelope pathBBox, | 87 Envelope pathBBox, |
47 List<? extends Coordinate> points, | 88 List<? extends Coordinate> points, |
48 int threshold | 89 int threshold |
49 ) { | 90 ) { |
52 : relevantArea( | 93 : relevantArea( |
53 pathBBox, | 94 pathBBox, |
54 pointsBoundingBox(points)); | 95 pointsBoundingBox(points)); |
55 } | 96 } |
56 | 97 |
98 /** | |
99 * Calculates the relevant area for a given line string. | |
100 * @param path The line string. | |
101 * @param points The sample points. | |
102 * @param threshold If the number of sample points | |
103 * are below this threshold no relevant area is calculated. | |
104 * @return The relevant area. | |
105 */ | |
57 public static final Envelope relevantArea( | 106 public static final Envelope relevantArea( |
58 List<? extends Coordinate> path, | 107 List<? extends Coordinate> path, |
59 List<? extends Coordinate> points, | 108 List<? extends Coordinate> points, |
60 int threshold | 109 int threshold |
61 ) { | 110 ) { |
64 : relevantArea( | 113 : relevantArea( |
65 pointsBoundingBox(path), | 114 pointsBoundingBox(path), |
66 pointsBoundingBox(points)); | 115 pointsBoundingBox(points)); |
67 } | 116 } |
68 | 117 |
118 /** | |
119 * Heuristic function to define a 'relevant area'. | |
120 * @param pathBBox The bounding box of the line line string. | |
121 * @param pointsBBox The bounding box of the sample points. | |
122 * @return The relevant area. | |
123 */ | |
69 public static final Envelope relevantArea( | 124 public static final Envelope relevantArea( |
70 Envelope pathBBox, | 125 Envelope pathBBox, |
71 Envelope pointsBBox | 126 Envelope pointsBBox |
72 ) { | 127 ) { |
73 double pathArea = pathBBox.getWidth()*pathBBox.getHeight(); | 128 double pathArea = pathBBox.getWidth()*pathBBox.getHeight(); |
88 pathBBox.expandBy(extra); | 143 pathBBox.expandBy(extra); |
89 | 144 |
90 return pathBBox; | 145 return pathBBox; |
91 } | 146 } |
92 | 147 |
148 /** | |
149 * Solves quadratic equation a*x*x + b*x + c = 0. | |
150 * @param a a-coefficent. | |
151 * @param b b-coefficent | |
152 * @param c c-coefficent. | |
153 * @return the solution of the equation, or null if not solvable. | |
154 */ | |
93 public static final double [] solveQuadratic( | 155 public static final double [] solveQuadratic( |
94 double a, double b, double c | 156 double a, double b, double c |
95 ) { | 157 ) { |
96 double d = b*b - 4d*a*c; | 158 double d = b*b - 4d*a*c; |
97 if (d < 0d) { return null; } | 159 if (d < 0d) { return null; } |
101 b = -b; | 163 b = -b; |
102 | 164 |
103 return new double [] { a*(b + d), a*(b - d) }; | 165 return new double [] { a*(b + d), a*(b - d) }; |
104 } | 166 } |
105 | 167 |
168 /** | |
169 * Return the element of a two element array which | |
170 * is greater or equal zero. | |
171 * @param x The two values. | |
172 * @return The value which is greater or equal zero. | |
173 */ | |
106 public static final double pos(double [] x) { | 174 public static final double pos(double [] x) { |
107 return x[0] >= 0 ? x[0] : x[1]; | 175 return x[0] >= 0 ? x[0] : x[1]; |
108 } | 176 } |
109 | 177 |
110 | 178 |
179 /** | |
180 * Calculates the bounding box of a given line string. | |
181 * @param path The line string. | |
182 * @return The bounding box. | |
183 */ | |
111 public static Envelope pointsBoundingBox( | 184 public static Envelope pointsBoundingBox( |
112 List<? extends Coordinate> path | 185 List<? extends Coordinate> path |
113 ) { | 186 ) { |
114 int N = path.size(); | 187 int N = path.size(); |
115 Envelope area = new Envelope(path.get(N-1)); | 188 Envelope area = new Envelope(path.get(N-1)); |
119 } | 192 } |
120 | 193 |
121 return area; | 194 return area; |
122 } | 195 } |
123 | 196 |
197 /** | |
198 * Interpolates linearly a number of coordinates and parameter values along | |
199 * a given line string path. The results are issued to a consumer. | |
200 * @param path The line string path. | |
201 * @param points The sample points. | |
202 * @param from Start point as a scalar value linear | |
203 * referenced on the line string. | |
204 * @param to End point of as a scalar value linear | |
205 * referenced on the line string. | |
206 * @param steps Number of points to be interpolated. | |
207 * @param metrics The used metric. | |
208 * @param consumer The callback to retrieve the result points. | |
209 */ | |
124 public static void interpolate( | 210 public static void interpolate( |
125 List<? extends Coordinate> path, | 211 List<? extends Coordinate> path, |
126 List<? extends Point2d> points, | 212 List<? extends Point2d> points, |
127 double from, | 213 double from, |
128 double to, | 214 double to, |
223 log.debug("interpolations: " + | 309 log.debug("interpolations: " + |
224 interpolations + " / " + missedInterpolations); | 310 interpolations + " / " + missedInterpolations); |
225 } | 311 } |
226 } | 312 } |
227 | 313 |
314 /** | |
315 * Linear interpolate a value between (x1, y1) and (x2, y2) at | |
316 * a given x-value. | |
317 * @param x1 x component of first point. | |
318 * @param y1 y component of first point. | |
319 * @param x2 x component of second point. | |
320 * @param y2 y component of second point. | |
321 * @param x The x value. | |
322 * @return The intepolated result. | |
323 */ | |
228 public static final double interpolate( | 324 public static final double interpolate( |
229 double x1, double y1, | 325 double x1, double y1, |
230 double x2, double y2, | 326 double x2, double y2, |
231 double x | 327 double x |
232 ) { | 328 ) { |