source: osm/applications/editors/josm/plugins/smed2/src/seamap/Renderer.java@ 30023

Last change on this file since 30023 was 30023, checked in by malcolmh, 13 years ago

save

File size: 12.5 KB
Line 
1/* Copyright 2013 Malcolm Herring
2 *
3 * This is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License.
6 *
7 * For a copy of the GNU General Public License, see <http://www.gnu.org/licenses/>.
8 */
9
10package seamap;
11
12import java.awt.*;
13import java.awt.font.*;
14import java.awt.geom.*;
15import java.awt.image.*;
16import java.util.*;
17
18import s57.S57att.*;
19import s57.S57obj.*;
20import s57.S57val.*;
21import s57.S57val;
22import seamap.SeaMap;
23import seamap.SeaMap.*;
24import seamap.SeaMap.Area;
25import symbols.Symbols;
26import symbols.Symbols.*;
27
28public class Renderer {
29
30 public static final EnumMap<ColCOL, Color> bodyColours = new EnumMap<ColCOL, Color>(ColCOL.class);
31 static {
32 bodyColours.put(ColCOL.COL_UNK, new Color(0, true));
33 bodyColours.put(ColCOL.COL_WHT, new Color(0xffffff));
34 bodyColours.put(ColCOL.COL_BLK, new Color(0x000000));
35 bodyColours.put(ColCOL.COL_RED, new Color(0xd40000));
36 bodyColours.put(ColCOL.COL_GRN, new Color(0x00d400));
37 bodyColours.put(ColCOL.COL_BLU, Color.blue);
38 bodyColours.put(ColCOL.COL_YEL, new Color(0xffd400));
39 bodyColours.put(ColCOL.COL_GRY, Color.gray);
40 bodyColours.put(ColCOL.COL_BRN, new Color(0x8b4513));
41 bodyColours.put(ColCOL.COL_AMB, new Color(0xfbf00f));
42 bodyColours.put(ColCOL.COL_VIO, new Color(0xee82ee));
43 bodyColours.put(ColCOL.COL_ORG, Color.orange);
44 bodyColours.put(ColCOL.COL_MAG, new Color(0xf000f0));
45 bodyColours.put(ColCOL.COL_PNK, Color.pink);
46 }
47
48 public static final EnumMap<ColPAT, Patt> pattMap = new EnumMap<ColPAT, Patt>(ColPAT.class);
49 static {
50 pattMap.put(ColPAT.PAT_UNKN, Patt.Z);
51 pattMap.put(ColPAT.PAT_HORI, Patt.H);
52 pattMap.put(ColPAT.PAT_VERT, Patt.V);
53 pattMap.put(ColPAT.PAT_DIAG, Patt.D);
54 pattMap.put(ColPAT.PAT_BRDR, Patt.B);
55 pattMap.put(ColPAT.PAT_SQUR, Patt.S);
56 pattMap.put(ColPAT.PAT_CROS, Patt.C);
57 pattMap.put(ColPAT.PAT_SALT, Patt.X);
58 pattMap.put(ColPAT.PAT_STRP, Patt.H);
59 }
60
61 public static final double symbolScale[] = { 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.61, 0.372, 0.227, 0.138, 0.0843, 0.0514, 0.0313, 0.0191, 0.0117, 0.007, 0.138 };
62
63 public static final double textScale[] = { 256.0, 128.0, 64.0, 32.0, 16.0, 8.0, 4.0, 2.0, 1.0, 0.5556, 0.3086, 0.1714, 0.0953, 0.0529, 0.0294, 0.0163, 0.0091, 0.0050, 0.0028, 0.0163 };
64
65 static MapHelper helper;
66 static SeaMap map;
67 static double sScale;
68 static double tScale;
69 static Graphics2D g2;
70 static int zoom;
71
72 public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
73 g2 = g;
74 zoom = z;
75 helper = h;
76 map = m;
77 sScale = symbolScale[zoom] * factor;
78 tScale = textScale[zoom] * factor;
79 if (map != null) {
80 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
81 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
82 Rules.rules(map, zoom);
83 }
84 }
85
86 public static AttMap getAtts(Feature feature, Obj obj, int idx) {
87 HashMap<Integer, AttMap> objs = feature.objs.get(obj);
88 if (objs == null)
89 return null;
90 else
91 return objs.get(idx);
92 }
93
94 public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
95 AttMap atts = getAtts(feature, obj, idx);
96 if (atts == null)
97 return S57val.nullVal(att);
98 else {
99 AttItem item = atts.get(att);
100 if (item == null)
101 return S57val.nullVal(att);
102 return item.val;
103 }
104 }
105
106 public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta, Scheme scheme) {
107 Point2D point = helper.getPoint(feature.centre);
108 if (obj == null) {
109 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, scheme);
110 } else {
111 ArrayList<Color> colours = new ArrayList<Color>();
112 for (ColCOL col : (ArrayList<ColCOL>)getAttVal(feature, obj, 0, Att.COLOUR)) {
113 colours.add(bodyColours.get(col));
114 }
115 ArrayList<Patt> patterns = new ArrayList<Patt>();
116 for(ColPAT pat: (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT)) {
117 patterns.add(pattMap.get(pat));
118 }
119 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, new Scheme(patterns, colours));
120 }
121 }
122
123 private static Rectangle symbolSize(Symbol symbol) {
124 Symbol ssymb = symbol;
125 while (ssymb != null) {
126 for (Instr item : symbol) {
127 if (item.type == Prim.BBOX) {
128 return (Rectangle) item.params;
129 }
130 if (item.type == Prim.SYMB) {
131 ssymb = ((SubSymbol) item.params).instr;
132 break;
133 }
134 }
135 if (ssymb == symbol)
136 break;
137 }
138 return null;
139 }
140
141 private double mile(Feature feature) {
142 return Math.pow(2, zoom) * 256 / (21600 * Math.abs(Math.cos(feature.centre.lat)));
143 }
144
145 public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, int ratio, Color col) {
146 Area area;
147 switch (feature.flag) {
148 case LINE:
149 Edge edge = map.edges.get(feature.refs);
150 area = map.new Area();
151 area.add(map.new Bound(map.new Side(edge, true), true));
152 break;
153 case AREA:
154 area = map.areas.get(feature.refs);
155 break;
156 default:
157 return;
158 }
159 Rectangle prect = symbolSize(prisymb);
160 Rectangle srect = symbolSize(secsymb);
161 if (srect == null)
162 ratio = 0;
163 if (prect != null) {
164 double psize = Math.abs(prect.getY()) * sScale;
165 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
166 Point2D prev = new Point2D.Double();
167 Point2D next = new Point2D.Double();
168 Point2D curr = new Point2D.Double();
169 Point2D succ = new Point2D.Double();
170 boolean gap = true;
171 boolean piv = false;
172 double len = 0;
173 double angle = 0;
174 int scount = ratio;
175 Symbol symbol = prisymb;
176 for (Bound bound : area) {
177 BoundIterator bit = map.new BoundIterator(bound);
178 boolean first = true;
179 while (bit.hasNext()) {
180 prev = next;
181 next = helper.getPoint(bit.next());
182 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
183 piv = true;
184 if (first) {
185 curr = succ = next;
186 gap = (space > 0);
187 scount = ratio;
188 symbol = prisymb;
189 len = gap ? psize * space * 0.5 : psize;
190 first = false;
191 } else {
192 while (curr.distance(next) >= len) {
193 if (piv) {
194 double rem = len;
195 double s = prev.distance(next);
196 double p = curr.distance(prev);
197 if ((s > 0) && (p > 0)) {
198 double n = curr.distance(next);
199 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
200 double phi = Math.asin(p / len * Math.sin(theta));
201 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
202 }
203 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
204 piv = false;
205 } else {
206 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
207 }
208 if (!gap) {
209 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(),
210 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))),
211 new Scheme(col));
212 }
213 if (space > 0)
214 gap = !gap;
215 curr = succ;
216 len = gap ? (psize * space) : (--scount == 0) ? ssize : psize;
217 if (scount == 0) {
218 symbol = secsymb;
219 scount = ratio;
220 } else {
221 symbol = prisymb;
222 }
223 }
224 }
225 }
226 }
227 }
228 }
229
230 public static void lineVector(Feature feature, LineStyle style) {
231 Path2D.Double p = new Path2D.Double();
232 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
233 Point2D point;
234 switch (feature.flag) {
235 case LINE:
236 EdgeIterator eit = map.new EdgeIterator(map.edges.get(feature.refs), true);
237 point = helper.getPoint(eit.next());
238 p.moveTo(point.getX(), point.getY());
239 while (eit.hasNext()) {
240 point = helper.getPoint(eit.next());
241 p.lineTo(point.getX(), point.getY());
242 }
243 break;
244 case AREA:
245 for (Bound bound : map.areas.get(feature.refs)) {
246 BoundIterator bit = map.new BoundIterator(bound);
247 point = helper.getPoint(bit.next());
248 p.moveTo(point.getX(), point.getY());
249 while (bit.hasNext()) {
250 point = helper.getPoint(bit.next());
251 p.lineTo(point.getX(), point.getY());
252 }
253 }
254 break;
255 }
256 if (style.line != null) {
257 if (style.dash != null) {
258 float[] dash = new float[style.dash.length];
259 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
260 for (int i = 0; i < style.dash.length; i++) {
261 dash[i] *= (float) sScale;
262 }
263 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
264 } else {
265 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
266 }
267 g2.setPaint(style.line);
268 g2.draw(p);
269 }
270 if (style.fill != null) {
271 g2.setPaint(style.fill);
272 g2.fill(p);
273 }
274 }
275
276 public static void fillPattern(Feature feature, BufferedImage image) {
277 Path2D.Double p = new Path2D.Double();
278 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
279 Point2D point;
280 switch (feature.flag) {
281 case POINT:
282 point = helper.getPoint(feature.centre);
283 g2.drawImage(image, new AffineTransformOp(AffineTransform.getScaleInstance(sScale, sScale), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
284 (int)(point.getX() - (50 * sScale)), (int)(point.getY() - (50 * sScale)));
285 break;
286 case AREA:
287 for (Bound bound : map.areas.get(feature.refs)) {
288 BoundIterator bit = map.new BoundIterator(bound);
289 point = helper.getPoint(bit.next());
290 p.moveTo(point.getX(), point.getY());
291 while (bit.hasNext()) {
292 point = helper.getPoint(bit.next());
293 p.lineTo(point.getX(), point.getY());
294 }
295 }
296 g2.setPaint(new TexturePaint(image, new Rectangle(0, 0, 1 + (int)(100 * sScale), 1 + (int)(100 * sScale))));
297 g2.fill(p);
298 break;
299 }
300 }
301
302 public static void labelText(Feature feature, String str, Font font, Color colour, Delta delta) {
303 Symbol label = new Symbol();
304 label.add(new Instr(Prim.TEXT, new Caption(str, font, colour, (delta == null) ? new Delta(Handle.CC, null) : delta)));
305 Point2D point = helper.getPoint(feature.centre);
306 Symbols.drawSymbol(g2, label, sScale, point.getX(), point.getY(), null, null);
307 }
308
309 public static void lineText(Feature feature, String str, Font font, Color colour, double offset, double dy) {
310 Area area;
311 switch (feature.flag) {
312 case LINE:
313 Edge edge = map.edges.get(feature.refs);
314 area = map.new Area();
315 area.add(map.new Bound(map.new Side(edge, true), true));
316 break;
317 case AREA:
318 area = map.areas.get(feature.refs);
319 break;
320 default:
321 return;
322 }
323// Rectangle prect = symbolSize(prisymb);
324 if (!str.isEmpty()) {
325 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
326 FontRenderContext frc = g2.getFontRenderContext();
327 GlyphVector gv = font.deriveFont((float)(font.getSize()*tScale)).createGlyphVector(frc, str);
328// double psize = Math.abs(prect.getX());
329 Point2D prev = new Point2D.Double();
330 Point2D next = new Point2D.Double();
331 Point2D curr = new Point2D.Double();
332 Point2D succ = new Point2D.Double();
333 boolean gap = true;
334 boolean piv = false;
335 double len = 0;
336 double angle = 0;
337 for (Bound bound : area) {
338 BoundIterator bit = map.new BoundIterator(bound);
339 boolean first = true;
340 while (bit.hasNext()) {
341 prev = next;
342 next = helper.getPoint(bit.next());
343 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
344 piv = true;
345 if (first) {
346 curr = succ = next;
347// gap = (space > 0);
348// len = gap ? psize * space * 0.5 : psize;
349 first = false;
350 } else {
351 while (curr.distance(next) >= len) {
352 if (piv) {
353 double rem = len;
354 double s = prev.distance(next);
355 double p = curr.distance(prev);
356 if ((s > 0) && (p > 0)) {
357 double n = curr.distance(next);
358 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
359 double phi = Math.asin(p / len * Math.sin(theta));
360 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
361 }
362 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
363 piv = false;
364 } else {
365 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
366 }
367 if (!gap) {
368// Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(), new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX())) + Math.toRadians(90))), null);
369 }
370 gap = false;
371 curr = succ;
372// len = psize;
373 }
374 }
375 }
376 }
377 }
378 }
379}
Note: See TracBrowser for help on using the repository browser.