comparison gnv-artifacts/src/main/java/de/intevation/gnv/raster/Palette.java @ 801:d766fe2d917a

More javadoc. gnv-artifacts/trunk@883 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 06 Apr 2010 16:53:43 +0000
parents 6cff63d0c434
children feae2f9d6c6f
comparison
equal deleted inserted replaced
800:db5b04ecb426 801:d766fe2d917a
11 import org.w3c.dom.Document; 11 import org.w3c.dom.Document;
12 import org.w3c.dom.Element; 12 import org.w3c.dom.Element;
13 import org.w3c.dom.NodeList; 13 import org.w3c.dom.NodeList;
14 14
15 /** 15 /**
16 * Lookup mechanism to map double values from a list of
17 * ranges onto a set of colors and vice versa.
16 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> 18 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
17 */ 19 */
18 public class Palette 20 public class Palette
19 implements ValueToIndex 21 implements ValueToIndex
20 { 22 {
21 private static Logger log = Logger.getLogger(Palette.class); 23 private static Logger log = Logger.getLogger(Palette.class);
22 24
25 /**
26 * An entry in the lookup table.
27 */
23 public static final class Entry 28 public static final class Entry
24 implements Comparable 29 implements Comparable
25 { 30 {
26 private Entry left; 31 private Entry left;
27 private Entry right; 32 private Entry right;
35 40
36 private String description; 41 private String description;
37 42
38 private Color color; 43 private Color color;
39 44
45 /**
46 * Default constructor
47 */
40 public Entry() { 48 public Entry() {
41 } 49 }
42 50
51 /**
52 * Copy constructor
53 * @param other The entry to copy.
54 */
43 public Entry(Entry other) { 55 public Entry(Entry other) {
44 index = other.index; 56 index = other.index;
45 externalIndex = other.externalIndex; 57 externalIndex = other.externalIndex;
46 from = other.from; 58 from = other.from;
47 to = other.to; 59 to = other.to;
48 description = other.description; 60 description = other.description;
49 color = other.color; 61 color = other.color;
50 } 62 }
51 63
64 /**
65 * Constructor to set the palette index, the exteral index,
66 * the value range, the color and the description.
67 * @param index The pallette index
68 * @param externalIndex The external index
69 * @param from Start of the interval
70 * @param to End of the interval
71 * @param color The color belonging to this interval.
72 * @param description The description of this range.
73 */
52 public Entry( 74 public Entry(
53 int index, 75 int index,
54 int externalIndex, 76 int externalIndex,
55 double from, 77 double from,
56 double to, 78 double to,
63 this.to = to; 85 this.to = to;
64 this.color = color; 86 this.color = color;
65 this.description = description; 87 this.description = description;
66 } 88 }
67 89
90 /**
91 * Compares two entries by the start point of the interval.
92 * @param other
93 * @return -1 if this start point is before other. +1 if this
94 * start point is after, else 0.
95 */
68 public int compareTo(Object other) { 96 public int compareTo(Object other) {
69 Entry e = (Entry)other; 97 Entry e = (Entry)other;
70 if (from < e.from) return -1; 98 if (from < e.from) return -1;
71 if (from > e.from) return +1; 99 if (from > e.from) return +1;
72 return 0; 100 return 0;
73 } 101 }
74 102
103 /**
104 * The starting point of the interval.
105 * @return The starting point.
106 */
75 public double getFrom() { 107 public double getFrom() {
76 return from; 108 return from;
77 } 109 }
78 110
111 /**
112 * The end point of the interval.
113 * @return The end point.
114 */
79 public double getTo() { 115 public double getTo() {
80 return to; 116 return to;
81 } 117 }
82 118
119 /**
120 * The color of the entry.
121 * @return The color.
122 */
83 public Color getColor() { 123 public Color getColor() {
84 return color; 124 return color;
85 } 125 }
86 126
127 /**
128 * The external index of this entry. Useful to model
129 * external joins.
130 * @return The external index.
131 */
87 public int getExternalIndex() { 132 public int getExternalIndex() {
88 return externalIndex; 133 return externalIndex;
89 } 134 }
90 135
136 /**
137 * Searches for an entry which has a range that contains
138 * the given value.
139 * @param value The value to be searched.
140 * @return The entry containing the value or null if no such
141 * entry exists.
142 */
91 public Entry locateEntry(double value) { 143 public Entry locateEntry(double value) {
92 Entry current = this; 144 Entry current = this;
93 while (current != null) { 145 while (current != null) {
94 boolean b = value >= current.from; 146 boolean b = value >= current.from;
95 if (b && value <= current.to) { 147 if (b && value <= current.to) {
100 : current.left; 152 : current.left;
101 } 153 }
102 return null; 154 return null;
103 } 155 }
104 156
157 /**
158 * Returns the palette index for a given value.
159 * @param value The value to search.
160 * @return The index of the palette entry or -1 if
161 * no such entry exists.
162 */
105 public int locate(double value) { 163 public int locate(double value) {
106 Entry entry = locateEntry(value); 164 Entry entry = locateEntry(value);
107 return entry != null 165 return entry != null
108 ? entry.index 166 ? entry.index
109 : -1; 167 : -1;
110 } 168 }
111 169
170 /**
171 * Searches for an interval with a given index.
172 * @param index The index to be searched.
173 * @return The entry with the given index or null if no
174 * such entry exists.
175 */
112 public Entry findByIndex(int index) { 176 public Entry findByIndex(int index) {
113 Entry current = this; 177 Entry current = this;
114 while (current != null) { 178 while (current != null) {
115 if (current.index == index) { 179 if (current.index == index) {
116 break; 180 break;
120 : current.right; 184 : current.right;
121 } 185 }
122 return current; 186 return current;
123 } 187 }
124 188
189 /**
190 * The description of this entry.
191 * @return The description.
192 */
125 public String getDescription() { 193 public String getDescription() {
126 return description; 194 return description;
127 } 195 }
128 196
197 /**
198 * Determines if the range of this entry has
199 * infinitive length.
200 * @return true if the range of this entry has infinitive
201 * length else false.
202 */
129 public boolean isInfinity() { 203 public boolean isInfinity() {
130 return from == -Double.MAX_VALUE 204 return from == -Double.MAX_VALUE
131 || to == Double.MAX_VALUE; 205 || to == Double.MAX_VALUE;
132 } 206 }
133 } // class Entry 207 } // class Entry
134 208
209 /**
210 * Internal table of the entrys.
211 */
135 protected Entry [] entries; 212 protected Entry [] entries;
213
214 /**
215 * Root of the entry tree used to map values to entries.
216 */
136 protected Entry lookup; 217 protected Entry lookup;
218
219 /**
220 * The list of colors used by this palette.
221 */
137 protected Color [] rgbs; 222 protected Color [] rgbs;
138 223
139 private Entry buildLookup(Entry [] entries, int lo, int hi) { 224 private Entry buildLookup(Entry [] entries, int lo, int hi) {
140 if (lo > hi) { 225 if (lo > hi) {
141 return null; 226 return null;
145 entry.left = buildLookup(entries, lo, mid-1); 230 entry.left = buildLookup(entries, lo, mid-1);
146 entry.right = buildLookup(entries, mid+1, hi); 231 entry.right = buildLookup(entries, mid+1, hi);
147 return entry; 232 return entry;
148 } 233 }
149 234
235 /**
236 * Default constructor.
237 */
150 public Palette() { 238 public Palette() {
151 } 239 }
152 240
241 /**
242 * Constructor to create the palette from an XML document.
243 * @param document The document with palette information.
244 */
153 public Palette(Document document) { 245 public Palette(Document document) {
154 246
155 NodeList rangeNodes = document.getElementsByTagName("range"); 247 NodeList rangeNodes = document.getElementsByTagName("range");
156 248
157 entries = new Entry[rangeNodes.getLength()]; 249 entries = new Entry[rangeNodes.getLength()];
170 } 262 }
171 263
172 buildLookup(); 264 buildLookup();
173 } 265 }
174 266
267 /**
268 * Constructor to split a given palette into a given number of
269 * equal sized sub entries. Ranges of infinitive length are not
270 * splitted.
271 * @param original The source palette.
272 * @param N The number of chunks to split single ranges into.
273 */
175 public Palette(Palette original, int N) { 274 public Palette(Palette original, int N) {
176 if (N < 2) { 275 if (N < 2) {
177 throw new IllegalArgumentException("N < 2"); 276 throw new IllegalArgumentException("N < 2");
178 } 277 }
179 278
251 } 350 }
252 return Color.decode(s); 351 return Color.decode(s);
253 } 352 }
254 353
255 354
355 /**
356 * Internal method to build the lookup tree.
357 */
256 protected void buildLookup() { 358 protected void buildLookup() {
257 Arrays.sort(entries); 359 Arrays.sort(entries);
258 lookup = buildLookup(entries, 0, entries.length-1); 360 lookup = buildLookup(entries, 0, entries.length-1);
259 } 361 }
260 362
363 /**
364 * Creates a new palette which has ranges that are subdivided
365 * into a given number of sub ranges each.
366 * @param N The number od sub divisions of each range.
367 * @return The new created palette.
368 */
261 public Palette subdivide(int N) { 369 public Palette subdivide(int N) {
262 return new Palette(this, N); 370 return new Palette(this, N);
263 } 371 }
264 372
373 /**
374 * Return the number of entries in this palette.
375 * @return The number of entries.
376 */
265 public int getSize() { 377 public int getSize() {
266 return rgbs.length; 378 return rgbs.length;
267 } 379 }
268 380
381 /**
382 * Return the color of an entry for a given index.
383 * @param index The index.
384 * @return The color.
385 */
269 public Color getColor(int index) { 386 public Color getColor(int index) {
270 return rgbs[index]; 387 return rgbs[index];
271 } 388 }
272 389
390 /**
391 * Return the RGB value
392 * @param index of an entry for a given index.
393 * @return rgb value
394 */
273 public int indexToRGB(int index) { 395 public int indexToRGB(int index) {
274 return rgbs[index].getRGB(); 396 return rgbs[index].getRGB();
275 } 397 }
276 398
399 /**
400 * Searches for the index of an entry which contains the
401 * given value.
402 * @param value The value to be searched.
403 * @return The index of the entry or -1 if no entry is found.
404 */
277 public int toIndex(double value) { 405 public int toIndex(double value) {
278 return lookup.locate(value); 406 return lookup.locate(value);
279 } 407 }
280 408
409 /**
410 * Searches for the an entry which contains the given value.
411 * @param value The value to be searched.
412 * @return The entry which contains the value or null if
413 * no such entry is found.
414 */
281 public Entry getEntry(double value) { 415 public Entry getEntry(double value) {
282 return lookup.locateEntry(value); 416 return lookup.locateEntry(value);
283 } 417 }
284 418
419 /**
420 * Searches the entry for a given index.
421 * @param index The index of the entry.
422 * @return The entry or null if no such entry is found.
423 */
285 public Entry getEntryByIndex(int index) { 424 public Entry getEntryByIndex(int index) {
286 return lookup.findByIndex(index); 425 return lookup.findByIndex(index);
287 } 426 }
288 } 427 }
289 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 428 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org