comparison gnv-artifacts/src/main/java/de/intevation/gnv/math/LinearToMap.java @ 805:bb7afd783321

Removed trailing whitespace. Added more javadoc. gnv-artifacts/trunk@887 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 08 Apr 2010 11:31:44 +0000
parents 6cff63d0c434
children f953c9a559d8
comparison
equal deleted inserted replaced
804:9058c08eac3a 805:bb7afd783321
5 import java.util.Iterator; 5 import java.util.Iterator;
6 import java.util.List; 6 import java.util.List;
7 import java.util.NoSuchElementException; 7 import java.util.NoSuchElementException;
8 8
9 /** 9 /**
10 * Given a list of line segments instances of this class are able
11 * to span a metric system between a start and an end point
12 * represented as scalar values to 2D coordinate on the
13 * course of the segments.
14 *
10 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 15 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
11 */ 16 */
12 public class LinearToMap 17 public class LinearToMap
13 { 18 {
19 /**
20 * Represents a segment of the line string.
21 */
14 public static final class Range { 22 public static final class Range {
15 private Range next; 23 private Range next;
16 24
17 private double from; 25 private double from;
18 private double to; 26 private double to;
21 private Coordinate p1; 29 private Coordinate p1;
22 private Coordinate p2; 30 private Coordinate p2;
23 31
24 private Interpolator interpolator; 32 private Interpolator interpolator;
25 33
34 /**
35 * Default constructor.
36 */
26 public Range() { 37 public Range() {
27 } 38 }
28 39
40 /**
41 * Constructor to create a segment that maps
42 * a coordinate pair to two scalar values.
43 * Interpolations inside this segment are done with
44 * a given interpolator.
45 * @param from Start point of the segment.
46 * @param to End point of the segment.
47 * @param interpolator The interpolator.
48 * @param p1 The scalar value mapped to the start point.
49 * @param p2 The scalar value mappend to the end point.
50 */
29 public Range( 51 public Range(
30 double from, 52 double from,
31 double to, 53 double to,
32 Interpolator interpolator, 54 Interpolator interpolator,
33 Coordinate p1, 55 Coordinate p1,
42 b = from == to 64 b = from == to
43 ? 0d 65 ? 0d
44 : 1.0d/(to - from); 66 : 1.0d/(to - from);
45 } 67 }
46 68
69 /**
70 * Interpolated a coordinate on the segment given a scalar value
71 * between the start and end point of the range.
72 * @param x The scalar value.
73 * @param v The interpolated value is stored here.
74 */
47 public void eval(double x, Coordinate v) { 75 public void eval(double x, Coordinate v) {
48 interpolator.interpolate((x - from)*b, v); 76 interpolator.interpolate((x - from)*b, v);
49 } 77 }
50 78
79 /**
80 * Checks if a given value is inside this segment.
81 * @param x The value to test
82 * @return true if inside, else false.
83 */
51 public boolean inside(double x) { 84 public boolean inside(double x) {
52 return x >= from && x <= to; 85 return x >= from && x <= to;
53 } 86 }
54 87
88 /**
89 * Returns the start point of this segment.
90 * @return The start point.
91 */
55 public Coordinate startPoint() { 92 public Coordinate startPoint() {
56 return p1; 93 return p1;
57 } 94 }
58 95
96 /**
97 * Return the end point of this segment.
98 * @return The end point.
99 */
59 public Coordinate endPoint() { 100 public Coordinate endPoint() {
60 return p2; 101 return p2;
61 } 102 }
62 } // class Range 103 } // class Range
63 104
105 /**
106 * The head of the internal range list.
107 */
64 protected Range head; 108 protected Range head;
109
110 /**
111 * The last accessed segment. Used to accelerate
112 * the access of the right segment.
113 */
65 protected Range last; 114 protected Range last;
66 115
116 /**
117 * Default constructor.
118 */
67 public LinearToMap() { 119 public LinearToMap() {
68 } 120 }
69 121
122 /**
123 * Constructor to create a LinearToMap that maps
124 * given scalar values to coordinates of a given
125 * list of line segments.
126 * @param path The list of line segments.
127 * @param from The start value mapped to the start point
128 * of the first line segment.
129 * @param to The end value mapped to the end point of
130 * the last line segment.
131 * @param metrics The metric used to span the 2D space.
132 */
70 public LinearToMap( 133 public LinearToMap(
71 List<? extends Coordinate> path, 134 List<? extends Coordinate> path,
72 double from, 135 double from,
73 double to, 136 double to,
74 Metrics metrics 137 Metrics metrics
106 } 169 }
107 rangeStart = rangeEnd; 170 rangeStart = rangeEnd;
108 } 171 }
109 } 172 }
110 173
174 /**
175 * Returns a segment on which a given value is found.
176 * @param diagramX The value.
177 * @return The segment or null if no matching segment was found.
178 */
111 protected Range locateRange(double diagramX) { 179 protected Range locateRange(double diagramX) {
112 180
113 if (last != null && last.inside(diagramX)) { 181 if (last != null && last.inside(diagramX)) {
114 return last; 182 return last;
115 } 183 }
123 } 191 }
124 192
125 return null; 193 return null;
126 } 194 }
127 195
196 /**
197 * Interpolates a coordinate at a given scalar position.
198 * @param diagramX The scalar position.
199 * @param v The interpolated coordinate is stored here.
200 * @return true if the scalar position is inside the
201 * spanned range of the line string, else false.
202 */
128 public boolean locate(double diagramX, Coordinate v) { 203 public boolean locate(double diagramX, Coordinate v) {
129 Range range = locateRange(diagramX); 204 Range range = locateRange(diagramX);
130 if (range == null) { 205 if (range == null) {
131 return false; 206 return false;
132 } 207 }
133 range.eval(diagramX, v); 208 range.eval(diagramX, v);
134 return true; 209 return true;
135 } 210 }
136 211
212 /**
213 * Returns the length of a given line string using
214 * a given metric.
215 * @param path The line string.
216 * @param metrics The used metric.
217 * @return The length of the line string.
218 */
137 public static double length( 219 public static double length(
138 List<? extends Coordinate> path, 220 List<? extends Coordinate> path,
139 Metrics metrics 221 Metrics metrics
140 ) { 222 ) {
141 double sum = 0d; 223 double sum = 0d;
145 sum += metrics.distance(p1, p2); 227 sum += metrics.distance(p1, p2);
146 } 228 }
147 return sum; 229 return sum;
148 } 230 }
149 231
232 /**
233 * Return the number of segments in this map.
234 * @return the number of segments.
235 */
150 public int numRanges() { 236 public int numRanges() {
151 int count = 0; 237 int count = 0;
152 Range current = head; 238 Range current = head;
153 while (current != null) { 239 while (current != null) {
154 ++count; 240 ++count;
155 current = current.next; 241 current = current.next;
156 } 242 }
157 return count; 243 return count;
158 } 244 }
159 245
246 /**
247 * Returns an iterator over all segments of this map.
248 * @return The iterator.
249 */
160 public Iterator ranges() { 250 public Iterator ranges() {
161 return new Iterator() { 251 return new Iterator() {
162 252
163 Range current = head; 253 Range current = head;
164 254

http://dive4elements.wald.intevation.org