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

Last change on this file since 29270 was 29270, checked in by malcolmh, 12 years ago

save

File size: 7.4 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.geom.*;
14import java.util.*;
15
16import s57.S57att.*;
17import s57.S57obj.*;
18import s57.S57val.*;
19import s57.S57val;
20import seamap.SeaMap;
21import seamap.SeaMap.*;
22import symbols.Symbols;
23import symbols.Symbols.*;
24
25public class Renderer {
26
27 static MapHelper helper;
28 static SeaMap map;
29 static double sScale;
30 static double tScale;
31 static Graphics2D g2;
32 static int zoom;
33
34 public static void reRender(Graphics2D g, int z, double factor, SeaMap m, MapHelper h) {
35 g2 = g;
36 zoom = z;
37 helper = h;
38 map = m;
39 sScale = Symbols.symbolScale[zoom]*factor;
40 tScale = Symbols.textScale[zoom]*factor;
41 if (map != null) {
42 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
43 g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
44 Rules.rules(map, zoom);
45 }
46 }
47
48 public static AttMap getAtts(Feature feature, Obj obj, int idx) {
49 HashMap<Integer, AttMap> objs = feature.objs.get(obj);
50 if (objs == null) return null;
51 else return objs.get(idx);
52 }
53
54 public static Object getAttVal(Feature feature, Obj obj, int idx, Att att) {
55 AttMap atts = getAtts(feature, obj, idx);
56 if (atts == null) return S57val.nullVal(att);
57 else {
58 AttItem item = atts.get(att);
59 if (item == null) return S57val.nullVal(att);
60 return item.val;
61 }
62 }
63
64 public static void symbol(Feature feature, Symbol symbol, Obj obj, Delta delta) {
65 Point2D point = helper.getPoint(feature.centre);
66 if (obj == null) {
67 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, null);
68 } else {
69 ArrayList<ColCOL> colours = (ArrayList<ColCOL>) getAttVal(feature, obj, 0, Att.COLOUR);
70 ArrayList<ColPAT> pattern = (ArrayList<ColPAT>) getAttVal(feature, obj, 0, Att.COLPAT);
71 Symbols.drawSymbol(g2, symbol, sScale, point.getX(), point.getY(), delta, new Scheme(pattern, colours));
72 }
73 }
74
75 private static Rectangle symbolSize(Symbol symbol) {
76 Symbol ssymb = symbol;
77 while (ssymb != null) {
78 for (Instr item : symbol) {
79 if (item.type == Prim.BBOX) {
80 return (Rectangle) item.params;
81 }
82 if (item.type == Prim.SYMB) {
83 ssymb = ((SubSymbol)item.params).instr;
84 break;
85 }
86 }
87 if (ssymb == symbol)
88 break;
89 }
90 return null;
91 }
92
93 public static void lineSymbols(Feature feature, Symbol prisymb, double space, Symbol secsymb, int ratio) {
94 if (feature.flag != Fflag.POINT) {
95 Rectangle prect = symbolSize(prisymb);
96 Rectangle srect = symbolSize(secsymb);
97 if (srect == null)
98 ratio = 0;
99 if (prect != null) {
100 ArrayList<Long> ways = new ArrayList<Long>();
101 double psize = Math.abs(prect.getY()) * sScale;
102 double ssize = (srect != null) ? Math.abs(srect.getY()) * sScale : 0;
103// if (map.outers.containsKey(feature.refs)) {
104// ways.addAll(map.mpolys.get(map.outers.get(feature.refs)));
105// } else {
106// if (map.mpolys.containsKey(feature.refs)) {
107// ways.addAll(map.mpolys.get(feature.refs));
108// } else {
109// ways.add(feature.refs);
110// }
111// }
112 Point2D prev = new Point2D.Double();
113 Point2D next = new Point2D.Double();
114 Point2D curr = new Point2D.Double();
115 Point2D succ = new Point2D.Double();
116 boolean gap = true;
117 boolean piv = false;
118 double len = 0;
119 double angle = 0;
120 int scount = ratio;
121 Symbol symbol = prisymb;
122 for (long way : ways) {
123 boolean first = true;
124/* for (long node : map.ways.get(way)) {
125 prev = next;
126 next = helper.getPoint(map.points.get(node));
127 angle = Math.atan2(next.getY() - prev.getY(), next.getX() - prev.getX());
128 piv = true;
129 if (first) {
130 curr = succ = next;
131 gap = (space > 0);
132 scount = ratio;
133 symbol = prisymb;
134 len = gap ? psize * space * 0.5 : psize;
135 first = false;
136 } else {
137 while (curr.distance(next) >= len) {
138 if (piv) {
139 double rem = len;
140 double s = prev.distance(next);
141 double p = curr.distance(prev);
142 if ((s > 0) && (p > 0)) {
143 double n = curr.distance(next);
144 double theta = Math.acos((s * s + p * p - n * n) / 2 / s / p);
145 double phi = Math.asin(p / len * Math.sin(theta));
146 rem = len * Math.sin(Math.PI - theta - phi) / Math.sin(theta);
147 }
148 succ = new Point2D.Double(prev.getX() + (rem * Math.cos(angle)), prev.getY() + (rem * Math.sin(angle)));
149 piv = false;
150 } else {
151 succ = new Point2D.Double(curr.getX() + (len * Math.cos(angle)), curr.getY() + (len * Math.sin(angle)));
152 }
153 if (!gap) {
154 Symbols.drawSymbol(g2, symbol, sScale, curr.getX(), curr.getY(),
155 new Delta(Handle.BC, AffineTransform.getRotateInstance(Math.atan2((succ.getY() - curr.getY()), (succ.getX() - curr.getX()) + Math.toRadians(90)))), null);
156 }
157 if (space > 0) gap = !gap;
158 curr = succ;
159 len = gap ? (psize * space) : (--scount == 0) ? ssize : psize;
160 if (scount == 0) {
161 symbol = secsymb;
162 scount = ratio;
163 } else {
164 symbol = prisymb;
165 }
166 }
167 }
168 }
169*/ }
170 }
171 }
172 }
173
174 public static void lineVector (Feature feature, LineStyle style) {
175 Path2D.Double p = new Path2D.Double();
176 p.setWindingRule(GeneralPath.WIND_EVEN_ODD);
177 switch (feature.flag) {
178 case LINE:
179 break;
180 case AREA:
181 break;
182/* ArrayList<Long> ways = new ArrayList<Long>();
183 if (map.outers.containsKey(feature.refs)) {
184 ways.addAll(map.mpolys.get(map.outers.get(feature.refs)));
185 } else {
186 if (map.mpolys.containsKey(feature.refs)) {
187 ways.addAll(map.mpolys.get(feature.refs));
188 } else {
189 ways.add(feature.refs);
190 }
191 }
192 for (long way : ways) {
193 boolean first = true;
194 for (long node : map.ways.get(way)) {
195 Point2D point = helper.getPoint(map.points.get(node));
196 if (first) {
197 p.moveTo(point.getX(), point.getY());
198 first = false;
199 } else {
200 p.lineTo(point.getX(), point.getY());
201 }
202 }
203 }
204 if (style.line != null) {
205 if (style.dash != null) {
206 float[] dash = new float[style.dash.length];
207 System.arraycopy(style.dash, 0, dash, 0, style.dash.length);
208 for (int i = 0; i < style.dash.length; i++) {
209 dash[i] *= (float) sScale;
210 }
211 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 1, dash, 0));
212 } else {
213 g2.setStroke(new BasicStroke((float) (style.width * sScale), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
214 }
215 g2.setPaint(style.line);
216 g2.draw(p);
217 }
218 if (style.fill != null) {
219 g2.setPaint(style.fill);
220 g2.fill(p);
221 }*/
222 }
223 }
224
225 public static void labelText (Feature feature, String str, Font font, Color colour, Delta delta) {
226 Symbol label = new Symbol();
227 label.add(new Instr(Prim.TEXT, new Caption(str, font, colour, (delta == null) ? new Delta(Handle.CC, null) : delta)));
228 Point2D point = helper.getPoint(feature.centre);
229 Symbols.drawSymbol(g2, label, tScale, point.getX(), point.getY(), null, null);
230 }
231
232 public static void lineText (Feature feature, String str, Font font, double offset, double dy) {
233
234 }
235}
Note: See TracBrowser for help on using the repository browser.