source: osm/applications/editors/josm/plugins/smed2/src/seamap/LightRender.java@ 29286

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

save

File size: 17.7 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
12public class LightRender {
13
14/*
15XY_t radial(XY_t centre, double radius, double angle) {
16 XY_t position;
17 position.x = centre.x - (radius * mile * sin(d2r(angle)));
18 position.y = centre.y - (radius * mile * cos(d2r(angle)));
19 return position;
20}
21
22void renderFlare(Item_t *item) {
23 char *col = light_colours[COL_MAG];
24 Obj_t *obj = getObj(item, LIGHTS, 0);
25 Att_t *att;
26 if (((att = getAtt(obj, COLOUR)) != NULL) && (att->val.val.l->next == NULL)) {
27 col = light_colours[att->val.val.l->val];
28 }
29 renderSymbol(item, LIGHTS, "light", "", col, CC, 0, 0, 120);
30}
31
32void renderSector(Item_t *item, int s, char *text, char *style, double offset, int dy) {
33 Obj_t *sector;
34 double start, end;
35 Att_t *att;
36 XY_t p0, p1;
37 double r0, r1;
38 double b0, b1, span;
39 char *col;
40 XY_t pos = findCentroid(item);
41 if ((sector = getObj(item, LIGHTS, s)) != NULL) {
42 strcpy(string1, (att = getAtt(sector, LITRAD)) != NULL ? att->val.val.a : "0.2");
43 if (((att = getAtt(sector, CATLIT)) != NULL) && (testAtt(att, LIT_DIR)) && ((att = getAtt(sector, ORIENT)) != NULL)) {
44 b0 = fmod(540.0 - att->val.val.f, 360.0);
45 if ((att = getAtt(sector, COLOUR)) != NULL) {
46 col = light_colours[att->val.val.l->val];
47 r0 = atof(string1);
48 p0 = radial(pos, r0, b0);
49 printf("<path d=\"M %g,%g L %g,%g\" style=\"fill:none;stroke:#808080;stroke-width:%g;stroke-dasharray:%g\"/>\n",
50 pos.x, pos.y, p0.x, p0.y, (4 * symbolScale[zoom]), (20 * symbolScale[zoom]));
51 start = fmod(b0 + 2.0, 360.0);
52 end = fmod(360.0 + b0 - 2.0, 360.0);
53 Obj_t *adj;
54 for (int i = s-1; i <= s+1; i++) {
55 if (i == s) continue;
56 if ((adj = getObj(item, LIGHTS, i)) == NULL) continue;
57 Att_t *att;
58 if (((att = getAtt(adj, CATLIT)) != NULL) && (testAtt(att, LIT_DIR)) && ((att = getAtt(adj, ORIENT)) != NULL)) {
59 b1 = fmod(540.0 - att->val.val.f, 360.0);
60 if (fabs(b0 - b1) > 180.0) {
61 if (b0 < b1) b0 += 360.0;
62 else b1 += 360.0;
63 }
64 if (fabs(b0 - b1) < 4.0) {
65 if (b1 > b0) start = fmod((720.0 + b0 + b1) / 2.0, 360.0);
66 else end = fmod((720.0 + b0 + b1) / 2.0, 360.0);
67 }
68 }
69 }
70 p0 = radial(pos, r0, start);
71 p1 = radial(pos, r0, end);
72 printf("<path id=\"%d\" d=\"M %g,%g A %g,%g,0,0,1,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g\"/>\n",
73 ++ref, p0.x, p0.y, r0*mile, r0*mile, p1.x, p1.y, col, (20 * symbolScale[zoom]));
74 if (att->val.val.l->next != NULL) {
75 char *col = light_colours[att->val.val.l->next->val];
76 r1 = r0 - (20 * symbolScale[zoom]/mile);
77 p0 = radial(pos, r1, start);
78 p1 = radial(pos, r1, end);
79 printf("<path d=\"M %g,%g A %g,%g,0,0,1,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g\"/>\n",
80 p0.x, p0.y, r1*mile, r1*mile, p1.x, p1.y, col, (20 * symbolScale[zoom]));
81 }
82 }
83 } else if ((att = getAtt(sector, SECTR1)) != NULL) {
84 start = fmod(540.0 - att->val.val.f, 360.0);
85 if ((att = getAtt(sector, SECTR2)) != NULL) {
86 end = fmod(540.0 - att->val.val.f, 360.0);
87 start += start < end ? 360.0 : 0.0;
88 if ((att = getAtt(sector, COLOUR)) != NULL) {
89 char *ttok, *etok;
90 char *radstr = strdup(string1);
91 int arc = 0;
92 col = light_colours[att->val.val.l->val];
93 r0 = 0.0;
94 b0 = b1 = start;
95 for (char *tpl = strtok_r(radstr, ";", &ttok); tpl != NULL; tpl = strtok_r(NULL, ";", &ttok)) {
96 p0 = radial(pos, r0, b0);
97 span = 0.0;
98 char *ele = strtok_r(tpl, ":", &etok);
99 if ((*tpl == ':') && (r0 == 0.0)) {
100 r1 = 0.2;
101 } else if (*tpl != ':') {
102 r1 = atof(tpl);
103 ele = strtok_r(NULL, ":", &etok);
104 }
105 while (ele != NULL) {
106 if (isalpha(*ele)) {
107 if (strcmp(ele, "suppress") == 0) arc = 2;
108 else if (strcmp(ele, "dashed") == 0) arc = 1;
109 else arc = 0;
110 } else {
111 span = atof(ele);
112 }
113 ele = strtok_r(NULL, ":", &etok);
114 }
115 if (span == 0.0) {
116 char *back = (ttok != NULL) ? strstr(ttok, "-") : NULL;
117 if (back != NULL) {
118 span = b0 - end + atof(back);
119 } else {
120 span = b0 - end;
121 }
122 }
123 if (r1 != r0) {
124 p1 = radial(pos, r1, b0);
125 if (!((start == 180.0) && (end == 180.0)))
126 printf("<path d=\"M %g,%g L %g,%g\" style=\"fill:none;stroke:#808080;stroke-width:%g;stroke-dasharray:%g\"/>\n",
127 p0.x, p0.y, p1.x, p1.y, (4 * symbolScale[zoom]), (20 * symbolScale[zoom]));
128 r0 = r1;
129 p0 = p1;
130 }
131 if (span < 0.0) {
132 b1 = end - span;
133 b1 = b1 > b0 ? b0 : b1;
134 b0 = b1;
135 b1 = end;
136 p0 = radial(pos, r0, b0);
137 } else {
138 b1 = b0 - span;
139 b1 = b1 < end ? end : b1;
140 }
141 p1 = radial(pos, r1, b1);
142 if ((b0 == 180.0) && (b1 == 180.0)) {
143 span = 360.0;
144 p1 = radial(pos, r1, b1+0.01);
145 }
146 if (arc == 0) {
147 if (p0.x < p1.x)
148 printf("<path id=\"%d\" d=\"M %g,%g A %g,%g,0,%d,1,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g\"/>\n",
149 ++ref, p0.x, p0.y, r1*mile, r1*mile, span>180.0, p1.x, p1.y, col, (20 * symbolScale[zoom]));
150 else
151 printf("<path id=\"%d\" d=\"M %g,%g A %g,%g,0,%d,0,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g\"/>\n",
152 ++ref, p1.x, p1.y, r1*mile, r1*mile, span>180.0, p0.x, p0.y, col, (20 * symbolScale[zoom]));
153 if (text != NULL) {
154 double chord = sqrt(pow((p0.x - p1.x), 2) + pow((p0.y - p1.y), 2));
155 if ((chord > (strlen(text) * textScale[zoom] * 50)) || ((b0 == 180.0) && (b1 == 180.0)))
156 drawLineText(item, text, style, offset, dy, ref);
157 }
158 } else if (arc == 1) {
159 printf("<path d=\"M %g,%g A %g,%g,0,%d,1,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g;stroke-opacity:0.5;stroke-dasharray:%g\"/>\n",
160 p0.x, p0.y, r1*mile, r1*mile, span>180.0, p1.x, p1.y, col, (10 * symbolScale[zoom]), (30 * symbolScale[zoom]));
161 }
162 if ((arc == 0) && (att->val.val.l->next != NULL)) {
163 char *col = light_colours[att->val.val.l->next->val];
164 double r2 = r1 - (20 * symbolScale[zoom]/mile);
165 XY_t p2 = radial(pos, r2, b0);
166 XY_t p3 = radial(pos, r2, b1);
167 printf("<path d=\"M %g,%g A %g,%g,0,%d,1,%g,%g\" style=\"fill:none;stroke:%s;stroke-width:%g\"/>\n",
168 p2.x, p2.y, r1*mile, r1*mile, span>180.0, p3.x, p3.y, col, (20 * symbolScale[zoom]));
169 }
170 b0 = b1;
171 if (b0 == end) break;
172 }
173 if (!((start == 180.0) && (end == 180.0)))
174 printf("<path d=\"M %g,%g L %g,%g\" style=\"fill:none;stroke:#808080;stroke-width:%g;stroke-dasharray:%g\"/>\n",
175 pos.x, pos.y, p1.x, p1.y, (4 * symbolScale[zoom]), (20 * symbolScale[zoom]));
176 free(radstr);
177 }
178 }
179 }
180 }
181}
182char *charString(Item_t *item, char *type, int idx) {
183 strcpy(string1, "");
184 Att_t *att = NULL;
185 Obj_t *obj = getObj(item, enumType(type), idx);
186 switch (enumType(type)) {
187 case CGUSTA:
188 strcpy(string1, "CG");
189 if ((obj != NULL) && (att = getAtt(obj, COMCHA)) != NULL)
190 sprintf(strchr(string1, 0), " Ch.%s", stringValue(att->val));
191 break;
192 case FOGSIG:
193 if (obj != NULL) {
194 if ((att = getAtt(obj, CATFOG)) != NULL)
195 strcat(string1, fog_signals[att->val.val.e]);
196 if ((att = getAtt(obj, SIGGRP)) != NULL)
197 sprintf(strchr(string1, 0), "(%s)", stringValue(att->val));
198 else
199 strcat(string1, " ");
200 if ((att = getAtt(obj, SIGPER)) != NULL)
201 sprintf(strchr(string1, 0), "%ss ", stringValue(att->val));
202 if ((att = getAtt(obj, VALMXR)) != NULL)
203 sprintf(strchr(string1, 0), "%sM", stringValue(att->val));
204 }
205 break;
206 case RTPBCN:
207 if (obj != NULL) {
208 if ((att = getAtt(obj, CATRTB)) != NULL)
209 strcat(string1, rtb_map[att->val.val.e]);
210 if ((att = getAtt(obj, SIGGRP)) != NULL)
211 sprintf(strchr(string1, 0), "(%s)", stringValue(att->val));
212 else
213 strcat(string1, " ");
214 if ((att = getAtt(obj, SIGPER)) != NULL)
215 sprintf(strchr(string1, 0), "%ss ", stringValue(att->val));
216 if ((att = getAtt(obj, VALMXR)) != NULL)
217 sprintf(strchr(string1, 0), "%sM", stringValue(att->val));
218 }
219 break;
220 case SISTAT:
221 strcpy(string1, "SS");
222 if (obj != NULL) {
223 if ((att = getAtt(obj, CATSIT)) != NULL)
224 strcat(string1, sit_map[att->val.val.l->val]);
225 if ((att = getAtt(obj, COMCHA)) != NULL)
226 sprintf(strchr(string1, 0), "\nCh.%s", stringValue(att->val));
227 }
228 break;
229 case SISTAW:
230 strcpy(string1, "SS");
231 if (obj != NULL) {
232 if ((att = getAtt(obj, CATSIW)) != NULL)
233 strcat(string1, siw_map[att->val.val.l->val]);
234 if ((att = getAtt(obj, COMCHA)) != NULL)
235 sprintf(strchr(string1, 0), "\nCh.%s", stringValue(att->val));
236 }
237 break;
238 case LIGHTS:
239 {
240 int secmax = countObjects(item, "light");
241 if ((idx == 0) && (secmax > 0)) {
242 struct SECT {
243 struct SECT *next;
244 int dir;
245 LitCHR_t chr;
246 ColCOL_t col;
247 ColCOL_t alt;
248 char *grp;
249 double per;
250 double rng;
251 } *lights = NULL;
252 for (int i = secmax; i > 0; i--) {
253 struct SECT *tmp = calloc(1, sizeof(struct SECT));
254 tmp->next = lights;
255 lights = tmp;
256 obj = getObj(item, LIGHTS, i);
257 if ((att = getAtt(obj, CATLIT)) != NULL) {
258 lights->dir = testAtt(att, LIT_DIR);
259 }
260 if ((att = getAtt(obj, LITCHR)) != NULL) {
261 lights->chr = att->val.val.e;
262 switch (lights->chr) {
263 case CHR_AL:
264 lights->chr = CHR_F;
265 break;
266 case CHR_ALOC:
267 lights->chr = CHR_OC;
268 break;
269 case CHR_ALLFL:
270 lights->chr = CHR_LFL;
271 break;
272 case CHR_ALFL:
273 lights->chr = CHR_FL;
274 break;
275 case CHR_ALFFL:
276 lights->chr = CHR_FFL;
277 break;
278 default:
279 break;
280 }
281 }
282 if ((att = getAtt(obj, SIGGRP)) != NULL) {
283 lights->grp = att->val.val.a;
284 } else {
285 lights->grp = "";
286 }
287 if ((att = getAtt(obj, SIGPER)) != NULL) {
288 lights->per = att->val.val.f;
289 }
290 if ((att = getAtt(obj, VALNMR)) != NULL) {
291 lights->rng = att->val.val.f;
292 }
293 if ((att = getAtt(obj, COLOUR)) != NULL) {
294 lights->col = att->val.val.l->val;
295 if (att->val.val.l->next != NULL)
296 lights->alt = att->val.val.l->next->val;
297 }
298 }
299 struct COLRNG {
300 int col;
301 double rng;
302 } colrng[14];
303 while (lights != NULL) {
304 strcpy(string2, "");
305 bzero(colrng, 14*sizeof(struct COLRNG));
306 colrng[lights->col].col = 1;
307 colrng[lights->col].rng = lights->rng;
308 struct SECT *this = lights;
309 struct SECT *next = lights->next;
310 while (next != NULL) {
311 if ((this->dir == next->dir) && (this->chr == next->chr) &&
312 (strcmp(this->grp, next->grp) == 0) && (this->per == next->per)) {
313 colrng[next->col].col = 1;
314 if (next->rng > colrng[next->col].rng)
315 colrng[next->col].rng = next->rng;
316 struct SECT *tmp = lights;
317 while (tmp->next != next) tmp = tmp->next;
318 tmp->next = next->next;
319 free(next);
320 next = tmp->next;
321 } else {
322 next = next->next;
323 }
324 }
325 if (this->chr != CHR_UNKN) {
326 if (this->dir) strcpy(string2, "Dir.");
327 strcat(string2, light_characters[this->chr]);
328 if (strcmp(this->grp, "") != 0) {
329 if (this->grp[0] == '(')
330 sprintf(strchr(string2, 0), "%s", this->grp);
331 else
332 sprintf(strchr(string2, 0), "(%s)", this->grp);
333 } else {
334 if (strlen(string2) > 0) strcat(string2, ".");
335 }
336 int n = 0;
337 for (int i = 0; i < 14; i++) if (colrng[i].col) n++;
338 double max = 0.0;
339 for (int i = 0; i < 14; i++) if (colrng[i].col && (colrng[i].rng > max)) max = colrng[i].rng;
340 double min = max;
341 for (int i = 0; i < 14; i++) if (colrng[i].col && (colrng[i].rng > 0.0) && (colrng[i].rng < min)) min = colrng[i].rng;
342 if (min == max) {
343 for (int i = 0; i < 14; i++) if (colrng[i].col) strcat(string2, light_letters[i]);
344 } else {
345 for (int i = 0; i < 14; i++) if (colrng[i].col && (colrng[i].rng == max)) strcat(string2, light_letters[i]);
346 for (int i = 0; i < 14; i++) if (colrng[i].col && (colrng[i].rng < max) && (colrng[i].rng > min)) strcat(string2, light_letters[i]);
347 for (int i = 0; i < 14; i++) if (colrng[i].col && colrng[i].rng == min) strcat(string2, light_letters[i]);
348 }
349 strcat(string2, ".");
350 if (this->per > 0.0) sprintf(strchr(string2, 0), "%gs", this->per);
351 if (max > 0.0) {
352 sprintf(strchr(string2, 0), "%g", max);
353 if (min != max) {
354 if (n == 2) strcat(string2, "/");
355 else if (n > 2) strcat(string2, "-");
356 if (min < max) sprintf(strchr(string2, 0), "%g", min);
357 }
358 strcat(string2, "M");
359 }
360 if (strlen(string1) > 0) strcat(string1, "\n");
361 strcat(string1, string2);
362 }
363 lights = this->next;
364 free(this);
365 this = lights;
366 }
367 } else {
368 if ((att = getAtt(obj, CATLIT)) != NULL) {
369 if (testAtt(att, LIT_DIR))
370 strcat(string1, "Dir");
371 }
372 if ((att = getAtt(obj, MLTYLT)) != NULL)
373 sprintf(strchr(string1, 0), "%s", stringValue(att->val));
374 if ((att = getAtt(obj, LITCHR)) != NULL) {
375 char *chrstr = strdup(stringValue(att->val));
376 Att_t *grp = getAtt(obj, SIGGRP);
377 if (grp != NULL) {
378 char *strgrp = strdup(stringValue(grp->val));
379 char *grpstr = strtok(strgrp, "()");
380 switch (att->val.val.e) {
381 case CHR_QLFL:
382 sprintf(strchr(string1, 0), "Q(%s)+LFl", grpstr);
383 break;
384 case CHR_VQLFL:
385 sprintf(strchr(string1, 0), "VQ(%s)+LFl", grpstr);
386 break;
387 case CHR_UQLFL:
388 sprintf(strchr(string1, 0), "UQ(%s)+LFl", grpstr);
389 break;
390 default:
391 sprintf(strchr(string1, 0), "%s(%s)", chrstr, grpstr);
392 break;
393 }
394 free(strgrp);
395 } else {
396 sprintf(strchr(string1, 0), "%s", chrstr);
397 }
398 free(chrstr);
399 }
400 if ((att = getAtt(obj, COLOUR)) != NULL) {
401 int n = countValues(att);
402 if (!((n == 1) && (idx == 0) && (testAtt(att, COL_WHT)))) {
403 if ((strlen(string1) > 0) && ((string1[strlen(string1)-1] != ')')))
404 strcat(string1, ".");
405 Lst_t *lst = att->val.val.l;
406 while (lst != NULL) {
407 strcat(string1, light_letters[lst->val]);
408 lst = lst->next;
409 }
410 }
411 }
412 if ((idx == 0) && (att = getAtt(obj, CATLIT)) != NULL) {
413 if (testAtt(att, LIT_VERT))
414 strcat(string1, "(vert)");
415 if (testAtt(att, LIT_HORI))
416 strcat(string1, "(hor)");
417 }
418 if ((strlen(string1) > 0) &&
419 ((getAtt(obj, SIGPER) != NULL) ||
420 (getAtt(obj, HEIGHT) != NULL) ||
421 (getAtt(obj, VALMXR) != NULL)) &&
422 (string1[strlen(string1)-1] != ')'))
423 strcat(string1, ".");
424 if ((att = getAtt(obj, SIGPER)) != NULL)
425 sprintf(strchr(string1, 0), "%ss", stringValue(att->val));
426 if ((idx == 0) && (item->objs.obj != LITMIN)) {
427 if ((att = getAtt(obj, HEIGHT)) != NULL)
428 sprintf(strchr(string1, 0), "%sm", stringValue(att->val));
429 if ((att = getAtt(obj, VALNMR)) != NULL)
430 sprintf(strchr(string1, 0), "%sM", stringValue(att->val));
431 }
432 if ((idx == 0) && (att = getAtt(obj, CATLIT)) != NULL) {
433 if (testAtt(att, LIT_FRNT))
434 strcat(string1, "(Front)");
435 if (testAtt(att, LIT_REAR))
436 strcat(string1, "(Rear)");
437 if (testAtt(att, LIT_UPPR))
438 strcat(string1, "(Upper)");
439 if (testAtt(att, LIT_LOWR))
440 strcat(string1, "(Lower)");
441 }
442 }
443 }
444 break;
445 default: break;
446 }
447 return string1;
448}
449*/
450
451}
Note: See TracBrowser for help on using the repository browser.