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

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

save

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