comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearToMap.java @ 361:aec85d00d82c

Added code to do 2D interpolations along a digitied track on the map. Not connected to the transition model, yet. gnv-artifacts/trunk@435 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 15 Dec 2009 22:25:53 +0000
parents ee760729f6b8
children 9a828e5a2390
comparison
equal deleted inserted replaced
360:ee760729f6b8 361:aec85d00d82c
1 package de.intevation.gnv.math; 1 package de.intevation.gnv.math;
2 2
3 import java.awt.geom.Point2D; 3 import com.vividsolutions.jts.geom.Coordinate;
4 4
5 import java.util.List; 5 import java.util.List;
6 import java.util.Iterator; 6 import java.util.Iterator;
7 import java.util.NoSuchElementException; 7 import java.util.NoSuchElementException;
8 8
9 /** 9 /**
10 * @author Sascha L. Teichmann 10 * @author Sascha L. Teichmann
11 */ 11 */
12 public class LinearToMap 12 public class LinearToMap
13 { 13 {
14 public interface Interpolator {
15
16 void interpolate(double t, Point2D v);
17
18 } // interface Interpolator
19
20 public interface Metrics {
21
22 double distance(Point2D p1, Point2D p2);
23
24 Interpolator getInterpolator(Point2D p1, Point2D p2);
25
26 } // interface Metrics
27
28 public static final class Range { 14 public static final class Range {
29 private Range next; 15 private Range next;
30 16
31 private double from; 17 private double from;
32 private double to; 18 private double to;
33 private double b; 19 private double b;
34 20
35 private Point2D p1; 21 private Coordinate p1;
36 private Point2D p2; 22 private Coordinate p2;
37 23
38 private Interpolator interpolator; 24 private Interpolator interpolator;
39 25
40 public Range() { 26 public Range() {
41 } 27 }
42 28
43 public Range( 29 public Range(
44 double from, 30 double from,
45 double to, 31 double to,
46 Interpolator interpolator, 32 Interpolator interpolator,
47 Point2D p1, 33 Coordinate p1,
48 Point2D p2 34 Coordinate p2
49 ) { 35 ) {
50 this.from = from; 36 this.from = from;
51 this.to = to; 37 this.to = to;
52 this.interpolator = interpolator; 38 this.interpolator = interpolator;
53 this.p1 = p1; 39 this.p1 = p1;
56 b = from == to 42 b = from == to
57 ? 0d 43 ? 0d
58 : 1.0d/(to - from); 44 : 1.0d/(to - from);
59 } 45 }
60 46
61 public void eval(double x, Point2D v) { 47 public void eval(double x, Coordinate v) {
62 interpolator.interpolate((x - from)*b, v); 48 interpolator.interpolate((x - from)*b, v);
63 } 49 }
64 50
65 public boolean inside(double x) { 51 public boolean inside(double x) {
66 return x >= from && x <= to; 52 return x >= from && x <= to;
67 } 53 }
68 54
69 public Point2D startPoint() { 55 public Coordinate startPoint() {
70 return p1; 56 return p1;
71 } 57 }
72 58
73 public Point2D endPoint() { 59 public Coordinate endPoint() {
74 return p2; 60 return p2;
75 } 61 }
76 } // class Range 62 } // class Range
77 63
78 protected Range head; 64 protected Range head;
80 66
81 public LinearToMap() { 67 public LinearToMap() {
82 } 68 }
83 69
84 public LinearToMap( 70 public LinearToMap(
85 List path, 71 List<? extends Coordinate> path,
86 double from, 72 double from,
87 double to, 73 double to,
88 Metrics metrics 74 Metrics metrics
89 ) { 75 ) {
90 double diagramLength = Math.abs(to - from); 76 double diagramLength = Math.abs(to - from);
91 77
92 double worldLength = length(path, metrics); 78 double worldLength = length(path, metrics);
93 79
94 double rangeStart = from; 80 double rangeStart = from;
95 81
96 Range last = null; 82 Range last = null;
97 83
98 for (int i = 1, N = path.size(); i < N; ++i) { 84 for (int i = 1, N = path.size(); i < N; ++i) {
99 Point2D p1 = (Point2D)path.get(i-1); 85 Coordinate p1 = path.get(i-1);
100 Point2D p2 = (Point2D)path.get(i); 86 Coordinate p2 = path.get(i);
101 double segmentLength = metrics.distance(p1, p2); 87 double segmentLength = metrics.distance(p1, p2);
102 88
103 double relativeLength = segmentLength / worldLength; 89 double relativeLength = segmentLength / worldLength;
104 90
105 double rangeLength = diagramLength * relativeLength; 91 double rangeLength = diagramLength * relativeLength;
137 } 123 }
138 124
139 return null; 125 return null;
140 } 126 }
141 127
142 public boolean locate(double diagramX, Point2D v) { 128 public boolean locate(double diagramX, Coordinate v) {
143 Range range = locateRange(diagramX); 129 Range range = locateRange(diagramX);
144 if (range == null) { 130 if (range == null) {
145 return false; 131 return false;
146 } 132 }
147 range.eval(diagramX, v); 133 range.eval(diagramX, v);
148 return true; 134 return true;
149 } 135 }
150 136
151 public static double length(List path, Metrics metrics) { 137 public static double length(
138 List<? extends Coordinate> path,
139 Metrics metrics
140 ) {
152 double sum = 0d; 141 double sum = 0d;
153 for (int i = path.size()-1; i >= 1; --i) { 142 for (int i = path.size()-1; i >= 1; --i) {
154 Point2D p1 = (Point2D)path.get(i); 143 Coordinate p1 = path.get(i);
155 Point2D p2 = (Point2D)path.get(i-1); 144 Coordinate p2 = path.get(i-1);
156 sum += metrics.distance(p1, p2); 145 sum += metrics.distance(p1, p2);
157 } 146 }
158 return sum; 147 return sum;
159 } 148 }
160 149

http://dive4elements.wald.intevation.org