comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearToMap.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents aec85d00d82c
children 9a828e5a2390
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 package de.intevation.gnv.math;
2
3 import com.vividsolutions.jts.geom.Coordinate;
4
5 import java.util.List;
6 import java.util.Iterator;
7 import java.util.NoSuchElementException;
8
9 /**
10 * @author Sascha L. Teichmann
11 */
12 public class LinearToMap
13 {
14 public static final class Range {
15 private Range next;
16
17 private double from;
18 private double to;
19 private double b;
20
21 private Coordinate p1;
22 private Coordinate p2;
23
24 private Interpolator interpolator;
25
26 public Range() {
27 }
28
29 public Range(
30 double from,
31 double to,
32 Interpolator interpolator,
33 Coordinate p1,
34 Coordinate p2
35 ) {
36 this.from = from;
37 this.to = to;
38 this.interpolator = interpolator;
39 this.p1 = p1;
40 this.p2 = p2;
41
42 b = from == to
43 ? 0d
44 : 1.0d/(to - from);
45 }
46
47 public void eval(double x, Coordinate v) {
48 interpolator.interpolate((x - from)*b, v);
49 }
50
51 public boolean inside(double x) {
52 return x >= from && x <= to;
53 }
54
55 public Coordinate startPoint() {
56 return p1;
57 }
58
59 public Coordinate endPoint() {
60 return p2;
61 }
62 } // class Range
63
64 protected Range head;
65 protected Range last;
66
67 public LinearToMap() {
68 }
69
70 public LinearToMap(
71 List<? extends Coordinate> path,
72 double from,
73 double to,
74 Metrics metrics
75 ) {
76 double diagramLength = Math.abs(to - from);
77
78 double worldLength = length(path, metrics);
79
80 double rangeStart = from;
81
82 Range last = null;
83
84 for (int i = 1, N = path.size(); i < N; ++i) {
85 Coordinate p1 = path.get(i-1);
86 Coordinate p2 = path.get(i);
87 double segmentLength = metrics.distance(p1, p2);
88
89 double relativeLength = segmentLength / worldLength;
90
91 double rangeLength = diagramLength * relativeLength;
92
93 double rangeEnd = rangeStart + rangeLength;
94
95 Range range = new Range(
96 rangeStart, rangeEnd,
97 metrics.getInterpolator(p1, p2),
98 p1, p2);
99
100 if (last == null) {
101 last = head = range;
102 }
103 else {
104 last.next = range;
105 last = range;
106 }
107 rangeStart = rangeEnd;
108 }
109 }
110
111 protected Range locateRange(double diagramX) {
112
113 if (last != null && last.inside(diagramX)) {
114 return last;
115 }
116
117 Range current = head;
118 while (current != null) {
119 if (current.inside(diagramX)) {
120 return last = current;
121 }
122 current = current.next;
123 }
124
125 return null;
126 }
127
128 public boolean locate(double diagramX, Coordinate v) {
129 Range range = locateRange(diagramX);
130 if (range == null) {
131 return false;
132 }
133 range.eval(diagramX, v);
134 return true;
135 }
136
137 public static double length(
138 List<? extends Coordinate> path,
139 Metrics metrics
140 ) {
141 double sum = 0d;
142 for (int i = path.size()-1; i >= 1; --i) {
143 Coordinate p1 = path.get(i);
144 Coordinate p2 = path.get(i-1);
145 sum += metrics.distance(p1, p2);
146 }
147 return sum;
148 }
149
150 public int numRanges() {
151 int count = 0;
152 Range current = head;
153 while (current != null) {
154 ++count;
155 current = current.next;
156 }
157 return count;
158 }
159
160 public Iterator ranges() {
161 return new Iterator() {
162
163 Range current = head;
164
165 public boolean hasNext() {
166 return current != null;
167 }
168
169 public Object next() {
170 if (!hasNext()) {
171 throw new NoSuchElementException();
172 }
173 Range x = current;
174 current = current.next;
175 return x;
176 }
177
178 public void remove() {
179 throw new UnsupportedOperationException();
180 }
181 };
182 }
183 }
184 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org