1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.AlphaComposite;
|
---|
8 | import java.awt.BasicStroke;
|
---|
9 | import java.awt.Color;
|
---|
10 | import java.awt.Composite;
|
---|
11 | import java.awt.Graphics2D;
|
---|
12 | import java.awt.LinearGradientPaint;
|
---|
13 | import java.awt.MultipleGradientPaint;
|
---|
14 | import java.awt.Paint;
|
---|
15 | import java.awt.Point;
|
---|
16 | import java.awt.Rectangle;
|
---|
17 | import java.awt.RenderingHints;
|
---|
18 | import java.awt.Stroke;
|
---|
19 | import java.awt.image.BufferedImage;
|
---|
20 | import java.awt.image.DataBufferInt;
|
---|
21 | import java.awt.image.Raster;
|
---|
22 | import java.io.BufferedReader;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.time.Instant;
|
---|
25 | import java.util.ArrayList;
|
---|
26 | import java.util.Arrays;
|
---|
27 | import java.util.Collections;
|
---|
28 | import java.util.LinkedList;
|
---|
29 | import java.util.List;
|
---|
30 | import java.util.Objects;
|
---|
31 | import java.util.Random;
|
---|
32 |
|
---|
33 | import javax.swing.ImageIcon;
|
---|
34 |
|
---|
35 | import org.openstreetmap.josm.data.Bounds;
|
---|
36 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
---|
37 | import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
|
---|
38 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
39 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
40 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
41 | import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeEvent;
|
---|
42 | import org.openstreetmap.josm.data.gpx.GpxData.GpxDataChangeListener;
|
---|
43 | import org.openstreetmap.josm.data.gpx.Line;
|
---|
44 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
45 | import org.openstreetmap.josm.data.preferences.NamedColorProperty;
|
---|
46 | import org.openstreetmap.josm.gui.MapView;
|
---|
47 | import org.openstreetmap.josm.gui.MapViewState;
|
---|
48 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
49 | import org.openstreetmap.josm.gui.layer.MapViewGraphics;
|
---|
50 | import org.openstreetmap.josm.gui.layer.MapViewPaintable;
|
---|
51 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.MapViewEvent;
|
---|
52 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationEvent;
|
---|
53 | import org.openstreetmap.josm.gui.layer.MapViewPaintable.PaintableInvalidationListener;
|
---|
54 | import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
|
---|
55 | import org.openstreetmap.josm.io.CachedFile;
|
---|
56 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
57 | import org.openstreetmap.josm.tools.ColorScale;
|
---|
58 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
59 | import org.openstreetmap.josm.tools.Logging;
|
---|
60 | import org.openstreetmap.josm.tools.Stopwatch;
|
---|
61 | import org.openstreetmap.josm.tools.Utils;
|
---|
62 | import org.openstreetmap.josm.tools.date.Interval;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Class that helps to draw large set of GPS tracks with different colors and options
|
---|
66 | * @since 7319
|
---|
67 | */
|
---|
68 | public class GpxDrawHelper implements SoMChangeListener, MapViewPaintable.LayerPainter, PaintableInvalidationListener, GpxDataChangeListener {
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * The default color property that is used for drawing GPX points.
|
---|
72 | * @since 15496
|
---|
73 | */
|
---|
74 | public static final NamedColorProperty DEFAULT_COLOR_PROPERTY = new NamedColorProperty(marktr("gps point"), Color.magenta);
|
---|
75 |
|
---|
76 | private final GpxData data;
|
---|
77 | private final GpxLayer layer;
|
---|
78 |
|
---|
79 | // draw lines between points belonging to different segments
|
---|
80 | private boolean forceLines;
|
---|
81 | // use alpha blending for line draw
|
---|
82 | private boolean alphaLines;
|
---|
83 | // draw direction arrows on the lines
|
---|
84 | private boolean arrows;
|
---|
85 | /** width of line for paint **/
|
---|
86 | private int lineWidth;
|
---|
87 | /** don't draw lines if longer than x meters **/
|
---|
88 | private int maxLineLength;
|
---|
89 | // draw lines
|
---|
90 | private boolean lines;
|
---|
91 | /** paint large dots for points **/
|
---|
92 | private boolean large;
|
---|
93 | private int largesize;
|
---|
94 | private boolean hdopCircle;
|
---|
95 | /** paint direction arrow with alternate math. may be faster **/
|
---|
96 | private boolean arrowsFast;
|
---|
97 | /** don't draw arrows nearer to each other than this **/
|
---|
98 | private int arrowsDelta;
|
---|
99 | private double minTrackDurationForTimeColoring;
|
---|
100 |
|
---|
101 | /** maximum value of displayed HDOP, minimum is 0 */
|
---|
102 | private int hdoprange;
|
---|
103 |
|
---|
104 | private static final double PHI = Utils.toRadians(15);
|
---|
105 |
|
---|
106 | //// Variables used only to check cache validity
|
---|
107 | private boolean computeCacheInSync;
|
---|
108 | private int computeCacheMaxLineLengthUsed;
|
---|
109 | private Color computeCacheColorUsed;
|
---|
110 | private boolean computeCacheColorDynamic;
|
---|
111 | private ColorMode computeCacheColored;
|
---|
112 | private int computeCacheVelocityTune;
|
---|
113 | private int computeCacheHeatMapDrawColorTableIdx;
|
---|
114 | private boolean computeCacheHeatMapDrawPointMode;
|
---|
115 | private int computeCacheHeatMapDrawGain;
|
---|
116 | private int computeCacheHeatMapDrawLowerLimit;
|
---|
117 |
|
---|
118 | private Color colorCache;
|
---|
119 | private Color colorCacheTransparent;
|
---|
120 |
|
---|
121 | //// Color-related fields
|
---|
122 | /** Mode of the line coloring **/
|
---|
123 | private ColorMode colored;
|
---|
124 | /** max speed for coloring - allows to tweak line coloring for different speed levels. **/
|
---|
125 | private int velocityTune;
|
---|
126 | private boolean colorModeDynamic;
|
---|
127 | private Color neutralColor;
|
---|
128 | private int largePointAlpha;
|
---|
129 |
|
---|
130 | // default access is used to allow changing from plugins
|
---|
131 | private ColorScale velocityScale;
|
---|
132 | /** Colors (without custom alpha channel, if given) for HDOP painting. **/
|
---|
133 | private ColorScale hdopScale;
|
---|
134 | private ColorScale qualityScale;
|
---|
135 | private ColorScale fixScale;
|
---|
136 | private ColorScale dateScale;
|
---|
137 | private ColorScale directionScale;
|
---|
138 |
|
---|
139 | /** Opacity for hdop points **/
|
---|
140 | private int hdopAlpha;
|
---|
141 |
|
---|
142 | // lookup array to draw arrows without doing any math
|
---|
143 | private static final int ll0 = 9;
|
---|
144 | private static final int sl4 = 5;
|
---|
145 | private static final int sl9 = 3;
|
---|
146 | private static final int[][] dir = {
|
---|
147 | {+sl4, +ll0, +ll0, +sl4}, {-sl9, +ll0, +sl9, +ll0},
|
---|
148 | {-ll0, +sl4, -sl4, +ll0}, {-ll0, -sl9, -ll0, +sl9},
|
---|
149 | {-sl4, -ll0, -ll0, -sl4}, {+sl9, -ll0, -sl9, -ll0},
|
---|
150 | {+ll0, -sl4, +sl4, -ll0}, {+ll0, +sl9, +ll0, -sl9}
|
---|
151 | };
|
---|
152 |
|
---|
153 | /** heat map parameters **/
|
---|
154 |
|
---|
155 | // draw small extra line
|
---|
156 | private boolean heatMapDrawExtraLine;
|
---|
157 | // used index for color table (parameter)
|
---|
158 | private int heatMapDrawColorTableIdx;
|
---|
159 | // use point or line draw mode
|
---|
160 | private boolean heatMapDrawPointMode;
|
---|
161 | // extra gain > 0 or < 0 attenuation, 0 = default
|
---|
162 | private int heatMapDrawGain;
|
---|
163 | // do not draw elements with value lower than this limit
|
---|
164 | private int heatMapDrawLowerLimit;
|
---|
165 |
|
---|
166 | // normal buffered image and draw object (cached)
|
---|
167 | private BufferedImage heatMapImgGray;
|
---|
168 | private Graphics2D heatMapGraph2d;
|
---|
169 |
|
---|
170 | // some cached values
|
---|
171 | Rectangle heatMapCacheScreenBounds = new Rectangle();
|
---|
172 | MapViewState heatMapMapViewState;
|
---|
173 | int heatMapCacheLineWith;
|
---|
174 |
|
---|
175 | // copied value for line drawing
|
---|
176 | private final List<Integer> heatMapPolyX = new ArrayList<>();
|
---|
177 | private final List<Integer> heatMapPolyY = new ArrayList<>();
|
---|
178 |
|
---|
179 | // setup color maps used by heat map
|
---|
180 | private static final Color[] heatMapLutColorJosmInferno = createColorFromResource("inferno");
|
---|
181 | private static final Color[] heatMapLutColorJosmViridis = createColorFromResource("viridis");
|
---|
182 | private static final Color[] heatMapLutColorJosmBrown2Green = createColorFromResource("brown2green");
|
---|
183 | private static final Color[] heatMapLutColorJosmRed2Blue = createColorFromResource("red2blue");
|
---|
184 |
|
---|
185 | private static final Color[] rtkLibQualityColors = {
|
---|
186 | Color.GREEN, // Fixed, solution by carrier‐based relative positioning and the integer ambiguity is properly resolved.
|
---|
187 | Color.ORANGE, // Float, solution by carrier‐based relative positioning but the integer ambiguity is not resolved.
|
---|
188 | Color.PINK, // Reserved
|
---|
189 | Color.BLUE, // DGPS, solution by code‐based DGPS solutions or single point positioning with SBAS corrections
|
---|
190 | Color.RED, // Single, solution by single point positioning
|
---|
191 | Color.CYAN // PPP
|
---|
192 | };
|
---|
193 |
|
---|
194 | private static final String[] rtkLibQualityNames = {
|
---|
195 | tr("1 - Fixed"),
|
---|
196 | tr("2 - Float"),
|
---|
197 | tr("3 - Reserved"),
|
---|
198 | tr("4 - DGPS"),
|
---|
199 | tr("5 - Single"),
|
---|
200 | tr("6 - PPP")
|
---|
201 | };
|
---|
202 |
|
---|
203 | /**
|
---|
204 | * @see GpxConstants#FIX_VALUES
|
---|
205 | */
|
---|
206 | private static final Color[] gpsFixQualityColors = {
|
---|
207 | Color.MAGENTA, //None
|
---|
208 | new Color(255, 125, 0), //2D (orange-red)
|
---|
209 | Color.ORANGE, //3D
|
---|
210 | Color.CYAN, //DGPS
|
---|
211 | new Color(150, 255, 150), //PPS (light-green)
|
---|
212 | Color.GREEN, //RTK
|
---|
213 | Color.YELLOW, //Float RTK
|
---|
214 | Color.RED, //Estimated
|
---|
215 | Color.BLUE, //Manual
|
---|
216 | Color.GRAY //Simulated
|
---|
217 | };
|
---|
218 |
|
---|
219 | private static final String[] gpsFixQualityNames = {
|
---|
220 | tr("None"),
|
---|
221 | tr("2D"),
|
---|
222 | tr("3D"),
|
---|
223 | tr("DGPS"),
|
---|
224 | tr("PPS"),
|
---|
225 | tr("RTK"),
|
---|
226 | tr("Float RTK"),
|
---|
227 | tr("Estimated"),
|
---|
228 | tr("Manual"),
|
---|
229 | tr("Simulated")
|
---|
230 | };
|
---|
231 |
|
---|
232 | // user defined heatmap color
|
---|
233 | private Color[] heatMapLutColor = createColorLut(0, Color.BLACK, Color.WHITE);
|
---|
234 |
|
---|
235 | // The heat map was invalidated since the last draw.
|
---|
236 | private boolean gpxLayerInvalidated;
|
---|
237 |
|
---|
238 | private void setupColors() {
|
---|
239 | hdopAlpha = Config.getPref().getInt("hdop.color.alpha", -1);
|
---|
240 | velocityScale = ColorScale.createHSBScale(256);
|
---|
241 | /** Colors (without custom alpha channel, if given) for HDOP painting. **/
|
---|
242 | hdopScale = ColorScale.createHSBScale(256).makeReversed().addTitle(tr("HDOP"));
|
---|
243 | qualityScale = ColorScale.createFixedScale(rtkLibQualityColors).addTitle(tr("Quality")).addColorBarTitles(rtkLibQualityNames);
|
---|
244 | fixScale = ColorScale.createFixedScale(gpsFixQualityColors).addTitle(tr("GPS fix")).addColorBarTitles(gpsFixQualityNames);
|
---|
245 | dateScale = ColorScale.createHSBScale(256).addTitle(tr("Time"));
|
---|
246 | directionScale = ColorScale.createCyclicScale(256).setIntervalCount(4).addTitle(tr("Direction"));
|
---|
247 |
|
---|
248 | systemOfMeasurementChanged(null, null);
|
---|
249 | }
|
---|
250 |
|
---|
251 | @Override
|
---|
252 | public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
|
---|
253 | SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
|
---|
254 | velocityScale.addTitle(tr("Velocity, {0}", som.speedName));
|
---|
255 | layer.invalidate();
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * Different color modes
|
---|
260 | */
|
---|
261 | public enum ColorMode {
|
---|
262 | /**
|
---|
263 | * No special colors
|
---|
264 | */
|
---|
265 | NONE,
|
---|
266 | /**
|
---|
267 | * Color by velocity
|
---|
268 | */
|
---|
269 | VELOCITY,
|
---|
270 | /**
|
---|
271 | * Color by accuracy
|
---|
272 | */
|
---|
273 | HDOP,
|
---|
274 | /**
|
---|
275 | * Color by traveling direction
|
---|
276 | */
|
---|
277 | DIRECTION,
|
---|
278 | /**
|
---|
279 | * Color by time
|
---|
280 | */
|
---|
281 | TIME,
|
---|
282 | /**
|
---|
283 | * Color using a heatmap instead of normal lines
|
---|
284 | */
|
---|
285 | HEATMAP,
|
---|
286 | /**
|
---|
287 | * Color by quality (RTKLib)
|
---|
288 | */
|
---|
289 | QUALITY,
|
---|
290 | /**
|
---|
291 | * Color by GPS fix
|
---|
292 | */
|
---|
293 | FIX;
|
---|
294 |
|
---|
295 | static ColorMode fromIndex(final int index) {
|
---|
296 | return values()[index];
|
---|
297 | }
|
---|
298 |
|
---|
299 | int toIndex() {
|
---|
300 | return Arrays.asList(values()).indexOf(this);
|
---|
301 | }
|
---|
302 | }
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Constructs a new {@code GpxDrawHelper}.
|
---|
306 | * @param gpxLayer The layer to draw
|
---|
307 | * @since 12157
|
---|
308 | */
|
---|
309 | public GpxDrawHelper(GpxLayer gpxLayer) {
|
---|
310 | layer = gpxLayer;
|
---|
311 | data = gpxLayer.data;
|
---|
312 | data.addChangeListener(this);
|
---|
313 |
|
---|
314 | layer.addInvalidationListener(this);
|
---|
315 | SystemOfMeasurement.addSoMChangeListener(this);
|
---|
316 | setupColors();
|
---|
317 | }
|
---|
318 |
|
---|
319 | /**
|
---|
320 | * Read coloring mode for specified layer from preferences
|
---|
321 | * @return coloring mode
|
---|
322 | */
|
---|
323 | public ColorMode getColorMode() {
|
---|
324 | try {
|
---|
325 | int i = optInt("colormode");
|
---|
326 | if (i == -1) i = 0; //global
|
---|
327 | return ColorMode.fromIndex(i);
|
---|
328 | } catch (IndexOutOfBoundsException e) {
|
---|
329 | Logging.warn(e);
|
---|
330 | }
|
---|
331 | return ColorMode.NONE;
|
---|
332 | }
|
---|
333 |
|
---|
334 | private String opt(String key) {
|
---|
335 | return GPXSettingsPanel.getLayerPref(layer, key);
|
---|
336 | }
|
---|
337 |
|
---|
338 | private boolean optBool(String key) {
|
---|
339 | return Boolean.parseBoolean(opt(key));
|
---|
340 | }
|
---|
341 |
|
---|
342 | private int optInt(String key) {
|
---|
343 | return GPXSettingsPanel.getLayerPrefInt(layer, key);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Read all drawing-related settings from preferences
|
---|
348 | **/
|
---|
349 | public void readPreferences() {
|
---|
350 | forceLines = optBool("lines.force");
|
---|
351 | arrows = optBool("lines.arrows");
|
---|
352 | arrowsFast = optBool("lines.arrows.fast");
|
---|
353 | arrowsDelta = optInt("lines.arrows.min-distance");
|
---|
354 | lineWidth = optInt("lines.width");
|
---|
355 | alphaLines = optBool("lines.alpha-blend");
|
---|
356 |
|
---|
357 | int l = optInt("lines");
|
---|
358 | // -1 = global (default: all)
|
---|
359 | // 0 = none
|
---|
360 | // 1 = local
|
---|
361 | // 2 = all
|
---|
362 | if (!data.fromServer) { //local settings apply
|
---|
363 | maxLineLength = optInt("lines.max-length.local");
|
---|
364 | lines = l != 0; // don't draw if "none"
|
---|
365 | } else {
|
---|
366 | maxLineLength = optInt("lines.max-length");
|
---|
367 | lines = l != 0 && l != 1; //don't draw if "none" or "local only"
|
---|
368 | }
|
---|
369 | large = optBool("points.large");
|
---|
370 | largesize = optInt("points.large.size");
|
---|
371 | hdopCircle = optBool("points.hdopcircle");
|
---|
372 | colored = getColorMode();
|
---|
373 | velocityTune = optInt("colormode.velocity.tune");
|
---|
374 | colorModeDynamic = optBool("colormode.dynamic-range");
|
---|
375 | /* good HDOP's are between 1 and 3, very bad HDOP's go into 3 digit values */
|
---|
376 | hdoprange = Config.getPref().getInt("hdop.range", 7);
|
---|
377 | minTrackDurationForTimeColoring = optInt("colormode.time.min-distance");
|
---|
378 | largePointAlpha = optInt("points.large.alpha") & 0xFF;
|
---|
379 |
|
---|
380 | // get heatmap parameters
|
---|
381 | heatMapDrawExtraLine = optBool("colormode.heatmap.line-extra");
|
---|
382 | heatMapDrawColorTableIdx = optInt("colormode.heatmap.colormap");
|
---|
383 | heatMapDrawPointMode = optBool("colormode.heatmap.use-points");
|
---|
384 | heatMapDrawGain = optInt("colormode.heatmap.gain");
|
---|
385 | heatMapDrawLowerLimit = optInt("colormode.heatmap.lower-limit");
|
---|
386 |
|
---|
387 | // shrink to range
|
---|
388 | heatMapDrawGain = Utils.clamp(heatMapDrawGain, -10, 10);
|
---|
389 | neutralColor = DEFAULT_COLOR_PROPERTY.get();
|
---|
390 | velocityScale.setNoDataColor(neutralColor);
|
---|
391 | dateScale.setNoDataColor(neutralColor);
|
---|
392 | hdopScale.setNoDataColor(neutralColor);
|
---|
393 | qualityScale.setNoDataColor(neutralColor);
|
---|
394 | fixScale.setNoDataColor(neutralColor);
|
---|
395 | directionScale.setNoDataColor(neutralColor);
|
---|
396 |
|
---|
397 | largesize += lineWidth;
|
---|
398 | }
|
---|
399 |
|
---|
400 | @Override
|
---|
401 | public void paint(MapViewGraphics graphics) {
|
---|
402 | Bounds clipBounds = graphics.getClipBounds().getLatLonBoundsBox();
|
---|
403 | List<WayPoint> visibleSegments = listVisibleSegments(clipBounds);
|
---|
404 | if (!visibleSegments.isEmpty()) {
|
---|
405 | readPreferences();
|
---|
406 | drawAll(graphics.getDefaultGraphics(), graphics.getMapView(), visibleSegments, clipBounds);
|
---|
407 | if (graphics.getMapView().getLayerManager().getActiveLayer() == layer) {
|
---|
408 | drawColorBar(graphics.getDefaultGraphics(), graphics.getMapView());
|
---|
409 | }
|
---|
410 | }
|
---|
411 | }
|
---|
412 |
|
---|
413 | private List<WayPoint> listVisibleSegments(Bounds box) {
|
---|
414 | WayPoint last = null;
|
---|
415 | LinkedList<WayPoint> visibleSegments = new LinkedList<>();
|
---|
416 |
|
---|
417 | ensureTrackVisibilityLength();
|
---|
418 | for (Line segment : getLinesIterable(layer.trackVisibility)) {
|
---|
419 |
|
---|
420 | for (WayPoint pt : segment) {
|
---|
421 | Bounds b = new Bounds(pt.getCoor());
|
---|
422 | if (pt.drawLine && last != null) {
|
---|
423 | b.extend(last.getCoor());
|
---|
424 | }
|
---|
425 | if (b.intersects(box)) {
|
---|
426 | if (last != null && (visibleSegments.isEmpty()
|
---|
427 | || visibleSegments.getLast() != last)) {
|
---|
428 | if (last.drawLine) {
|
---|
429 | WayPoint l = new WayPoint(last);
|
---|
430 | l.drawLine = false;
|
---|
431 | visibleSegments.add(l);
|
---|
432 | } else {
|
---|
433 | visibleSegments.add(last);
|
---|
434 | }
|
---|
435 | }
|
---|
436 | visibleSegments.add(pt);
|
---|
437 | }
|
---|
438 | last = pt;
|
---|
439 | }
|
---|
440 | }
|
---|
441 | return visibleSegments;
|
---|
442 | }
|
---|
443 |
|
---|
444 | protected Iterable<Line> getLinesIterable(final boolean[] trackVisibility) {
|
---|
445 | return data.getLinesIterable(trackVisibility);
|
---|
446 | }
|
---|
447 |
|
---|
448 | /** ensures the trackVisibility array has the correct length without losing data.
|
---|
449 | * TODO: Make this nicer by syncing the trackVisibility automatically.
|
---|
450 | * additional entries are initialized to true;
|
---|
451 | */
|
---|
452 | private void ensureTrackVisibilityLength() {
|
---|
453 | final int l = data.getTracks().size();
|
---|
454 | if (l == layer.trackVisibility.length)
|
---|
455 | return;
|
---|
456 | final int m = Math.min(l, layer.trackVisibility.length);
|
---|
457 | layer.trackVisibility = Arrays.copyOf(layer.trackVisibility, l);
|
---|
458 | for (int i = m; i < l; i++) {
|
---|
459 | layer.trackVisibility[i] = true;
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Draw all enabled GPX elements of layer.
|
---|
465 | * @param g the common draw object to use
|
---|
466 | * @param mv the meta data to current displayed area
|
---|
467 | * @param visibleSegments segments visible in the current scope of mv
|
---|
468 | * @param clipBounds the clipping rectangle for the current view
|
---|
469 | * @since 14748 : new parameter clipBounds
|
---|
470 | */
|
---|
471 | public void drawAll(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, Bounds clipBounds) {
|
---|
472 |
|
---|
473 | final Stopwatch stopwatch = Stopwatch.createStarted();
|
---|
474 |
|
---|
475 | checkCache();
|
---|
476 |
|
---|
477 | // STEP 2b - RE-COMPUTE CACHE DATA *********************
|
---|
478 | if (!computeCacheInSync) { // don't compute if the cache is good
|
---|
479 | calculateColors();
|
---|
480 | // update the WaiPoint.drawline attributes
|
---|
481 | visibleSegments.clear();
|
---|
482 | visibleSegments.addAll(listVisibleSegments(clipBounds));
|
---|
483 | }
|
---|
484 |
|
---|
485 | fixColors(visibleSegments);
|
---|
486 |
|
---|
487 | // backup the environment
|
---|
488 | Composite oldComposite = g.getComposite();
|
---|
489 | Stroke oldStroke = g.getStroke();
|
---|
490 | Paint oldPaint = g.getPaint();
|
---|
491 |
|
---|
492 | // set hints for the render
|
---|
493 | g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
|
---|
494 | Config.getPref().getBoolean("mappaint.gpx.use-antialiasing", false) ?
|
---|
495 | RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
|
---|
496 |
|
---|
497 | if (lineWidth > 0) {
|
---|
498 | g.setStroke(new BasicStroke(lineWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
---|
499 | }
|
---|
500 |
|
---|
501 | // global enabled or select via color
|
---|
502 | boolean useHeatMap = ColorMode.HEATMAP == colored;
|
---|
503 |
|
---|
504 | // default global alpha level
|
---|
505 | float layerAlpha = 1.00f;
|
---|
506 |
|
---|
507 | // extract current alpha blending value
|
---|
508 | if (oldComposite instanceof AlphaComposite) {
|
---|
509 | layerAlpha = ((AlphaComposite) oldComposite).getAlpha();
|
---|
510 | }
|
---|
511 |
|
---|
512 | // use heatmap background layer
|
---|
513 | if (useHeatMap) {
|
---|
514 | drawHeatMap(g, mv, visibleSegments);
|
---|
515 | } else {
|
---|
516 | // use normal line style or alpha-blending lines
|
---|
517 | if (!alphaLines) {
|
---|
518 | drawLines(g, mv, visibleSegments);
|
---|
519 | } else {
|
---|
520 | drawLinesAlpha(g, mv, visibleSegments, layerAlpha);
|
---|
521 | }
|
---|
522 | }
|
---|
523 |
|
---|
524 | // override global alpha settings (smooth overlay)
|
---|
525 | if (alphaLines || useHeatMap) {
|
---|
526 | g.setComposite(AlphaComposite.SrcOver.derive(0.25f * layerAlpha));
|
---|
527 | }
|
---|
528 |
|
---|
529 | // normal overlays
|
---|
530 | drawArrows(g, mv, visibleSegments);
|
---|
531 | drawPoints(g, mv, visibleSegments);
|
---|
532 |
|
---|
533 | // restore environment
|
---|
534 | g.setPaint(oldPaint);
|
---|
535 | g.setStroke(oldStroke);
|
---|
536 | g.setComposite(oldComposite);
|
---|
537 |
|
---|
538 | // show some debug info
|
---|
539 | if (Logging.isDebugEnabled() && !visibleSegments.isEmpty()) {
|
---|
540 | Logging.debug(stopwatch.toString("gpxdraw::draw") +
|
---|
541 | "(" +
|
---|
542 | "segments= " + visibleSegments.size() +
|
---|
543 | ", per 10000 = " + Utils.getDurationString(10_000 * stopwatch.elapsed() / visibleSegments.size()) +
|
---|
544 | ")"
|
---|
545 | );
|
---|
546 | }
|
---|
547 | }
|
---|
548 |
|
---|
549 | /**
|
---|
550 | * Calculate colors of way segments based on latest configuration settings
|
---|
551 | */
|
---|
552 | public void calculateColors() {
|
---|
553 | double minval = +1e10;
|
---|
554 | double maxval = -1e10;
|
---|
555 | WayPoint oldWp = null;
|
---|
556 |
|
---|
557 | if (colorModeDynamic) {
|
---|
558 | if (colored == ColorMode.VELOCITY) {
|
---|
559 | final List<Double> velocities = new ArrayList<>();
|
---|
560 | for (Line segment : getLinesIterable(null)) {
|
---|
561 | if (!forceLines) {
|
---|
562 | oldWp = null;
|
---|
563 | }
|
---|
564 | for (WayPoint trkPnt : segment) {
|
---|
565 | if (!trkPnt.isLatLonKnown()) {
|
---|
566 | continue;
|
---|
567 | }
|
---|
568 | if (oldWp != null && trkPnt.getTimeInMillis() > oldWp.getTimeInMillis()) {
|
---|
569 | double vel = trkPnt.greatCircleDistance(oldWp)
|
---|
570 | / (trkPnt.getTime() - oldWp.getTime());
|
---|
571 | velocities.add(vel);
|
---|
572 | }
|
---|
573 | oldWp = trkPnt;
|
---|
574 | }
|
---|
575 | }
|
---|
576 | Collections.sort(velocities);
|
---|
577 | if (velocities.isEmpty()) {
|
---|
578 | velocityScale.setRange(0, 120/3.6);
|
---|
579 | } else {
|
---|
580 | minval = velocities.get(velocities.size() / 20); // 5% percentile to remove outliers
|
---|
581 | maxval = velocities.get(velocities.size() * 19 / 20); // 95% percentile to remove outliers
|
---|
582 | velocityScale.setRange(minval, maxval);
|
---|
583 | }
|
---|
584 | } else if (colored == ColorMode.HDOP) {
|
---|
585 | for (Line segment : getLinesIterable(null)) {
|
---|
586 | for (WayPoint trkPnt : segment) {
|
---|
587 | Object val = trkPnt.get(GpxConstants.PT_HDOP);
|
---|
588 | if (val != null) {
|
---|
589 | double hdop = ((Float) val).doubleValue();
|
---|
590 | if (hdop > maxval) {
|
---|
591 | maxval = hdop;
|
---|
592 | }
|
---|
593 | if (hdop < minval) {
|
---|
594 | minval = hdop;
|
---|
595 | }
|
---|
596 | }
|
---|
597 | }
|
---|
598 | }
|
---|
599 | if (minval >= maxval) {
|
---|
600 | hdopScale.setRange(0, 100);
|
---|
601 | } else {
|
---|
602 | hdopScale.setRange(minval, maxval);
|
---|
603 | }
|
---|
604 | }
|
---|
605 | oldWp = null;
|
---|
606 | } else { // color mode not dynamic
|
---|
607 | velocityScale.setRange(0, velocityTune);
|
---|
608 | hdopScale.setRange(0, hdoprange);
|
---|
609 | qualityScale.setRange(1, rtkLibQualityColors.length);
|
---|
610 | fixScale.setRange(0, gpsFixQualityColors.length);
|
---|
611 | }
|
---|
612 | double now = System.currentTimeMillis()/1000.0;
|
---|
613 | if (colored == ColorMode.TIME) {
|
---|
614 | Interval interval = data.getMinMaxTimeForAllTracks().orElse(new Interval(Instant.EPOCH, Instant.now()));
|
---|
615 | minval = interval.getStart().getEpochSecond();
|
---|
616 | maxval = interval.getEnd().getEpochSecond();
|
---|
617 | dateScale.setRange(minval, maxval);
|
---|
618 | }
|
---|
619 |
|
---|
620 | // Now the colors for all the points will be assigned
|
---|
621 | for (Line segment : getLinesIterable(null)) {
|
---|
622 | if (!forceLines) { // don't draw lines between segments, unless forced to
|
---|
623 | oldWp = null;
|
---|
624 | }
|
---|
625 | for (WayPoint trkPnt : segment) {
|
---|
626 | trkPnt.customColoring = segment.getColor();
|
---|
627 | if (Double.isNaN(trkPnt.lat()) || Double.isNaN(trkPnt.lon())) {
|
---|
628 | continue;
|
---|
629 | }
|
---|
630 | // now we are sure some color will be assigned
|
---|
631 | Color color = null;
|
---|
632 |
|
---|
633 | if (colored == ColorMode.HDOP) {
|
---|
634 | color = hdopScale.getColor((Float) trkPnt.get(GpxConstants.PT_HDOP));
|
---|
635 | } else if (colored == ColorMode.QUALITY) {
|
---|
636 | color = qualityScale.getColor((Integer) trkPnt.get(GpxConstants.RTKLIB_Q));
|
---|
637 | } else if (colored == ColorMode.FIX) {
|
---|
638 | Object fixval = trkPnt.get(GpxConstants.PT_FIX);
|
---|
639 | if (fixval != null) {
|
---|
640 | int fix = GpxConstants.FIX_VALUES.indexOf(fixval);
|
---|
641 | if (fix >= 0) {
|
---|
642 | color = fixScale.getColor(fix);
|
---|
643 | }
|
---|
644 | }
|
---|
645 | }
|
---|
646 | if (oldWp != null) { // other coloring modes need segment for calcuation
|
---|
647 | double dist = trkPnt.greatCircleDistance(oldWp);
|
---|
648 | boolean noDraw = false;
|
---|
649 | switch (colored) {
|
---|
650 | case VELOCITY:
|
---|
651 | double dtime = trkPnt.getTime() - oldWp.getTime();
|
---|
652 | if (dtime > 0) {
|
---|
653 | color = velocityScale.getColor(dist / dtime);
|
---|
654 | } else {
|
---|
655 | color = velocityScale.getNoDataColor();
|
---|
656 | }
|
---|
657 | break;
|
---|
658 | case DIRECTION:
|
---|
659 | double dirColor = oldWp.bearing(trkPnt);
|
---|
660 | color = directionScale.getColor(dirColor);
|
---|
661 | break;
|
---|
662 | case TIME:
|
---|
663 | double t = trkPnt.getTime();
|
---|
664 | // skip bad timestamps and very short tracks
|
---|
665 | if (t > 0 && t <= now && maxval - minval > minTrackDurationForTimeColoring) {
|
---|
666 | color = dateScale.getColor(t);
|
---|
667 | } else {
|
---|
668 | color = dateScale.getNoDataColor();
|
---|
669 | }
|
---|
670 | break;
|
---|
671 | default: // Do nothing
|
---|
672 | }
|
---|
673 | if (!noDraw && (!segment.isUnordered() || !data.fromServer) && (maxLineLength == -1 || dist <= maxLineLength)) {
|
---|
674 | trkPnt.drawLine = true;
|
---|
675 | double bearing = oldWp.bearing(trkPnt);
|
---|
676 | trkPnt.dir = ((int) (bearing / Math.PI * 4 + 1.5)) % 8;
|
---|
677 | } else {
|
---|
678 | trkPnt.drawLine = false;
|
---|
679 | }
|
---|
680 | } else { // make sure we reset outdated data
|
---|
681 | trkPnt.drawLine = false;
|
---|
682 | color = segment.getColor();
|
---|
683 | }
|
---|
684 | if (color != null) {
|
---|
685 | trkPnt.customColoring = color;
|
---|
686 | }
|
---|
687 | oldWp = trkPnt;
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | // heat mode
|
---|
692 | if (ColorMode.HEATMAP == colored) {
|
---|
693 |
|
---|
694 | // get new user color map and refresh visibility level
|
---|
695 | heatMapLutColor = createColorLut(heatMapDrawLowerLimit,
|
---|
696 | selectColorMap(neutralColor != null ? neutralColor : Color.WHITE, heatMapDrawColorTableIdx));
|
---|
697 |
|
---|
698 | // force redraw of image
|
---|
699 | heatMapMapViewState = null;
|
---|
700 | }
|
---|
701 |
|
---|
702 | computeCacheInSync = true;
|
---|
703 | }
|
---|
704 |
|
---|
705 | /**
|
---|
706 | * Draw all GPX ways segments
|
---|
707 | * @param g the common draw object to use
|
---|
708 | * @param mv the meta data to current displayed area
|
---|
709 | * @param visibleSegments segments visible in the current scope of mv
|
---|
710 | */
|
---|
711 | private void drawLines(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
---|
712 | if (lines) {
|
---|
713 | Point old = null;
|
---|
714 | for (WayPoint trkPnt : visibleSegments) {
|
---|
715 | if (!trkPnt.isLatLonKnown()) {
|
---|
716 | old = null;
|
---|
717 | continue;
|
---|
718 | }
|
---|
719 | Point screen = mv.getPoint(trkPnt);
|
---|
720 | // skip points that are on the same screenposition
|
---|
721 | if (trkPnt.drawLine && old != null && ((old.x != screen.x) || (old.y != screen.y))) {
|
---|
722 | g.setColor(trkPnt.customColoring);
|
---|
723 | g.drawLine(old.x, old.y, screen.x, screen.y);
|
---|
724 | }
|
---|
725 | old = screen;
|
---|
726 | }
|
---|
727 | }
|
---|
728 | }
|
---|
729 |
|
---|
730 | /**
|
---|
731 | * Draw all GPX arrays
|
---|
732 | * @param g the common draw object to use
|
---|
733 | * @param mv the meta data to current displayed area
|
---|
734 | * @param visibleSegments segments visible in the current scope of mv
|
---|
735 | */
|
---|
736 | private void drawArrows(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
---|
737 | /****************************************************************
|
---|
738 | ********** STEP 3b - DRAW NICE ARROWS **************************
|
---|
739 | ****************************************************************/
|
---|
740 | if (lines && arrows && !arrowsFast) {
|
---|
741 | Point old = null;
|
---|
742 | Point oldA = null; // last arrow painted
|
---|
743 | for (WayPoint trkPnt : visibleSegments) {
|
---|
744 | if (!trkPnt.isLatLonKnown()) {
|
---|
745 | old = null;
|
---|
746 | continue;
|
---|
747 | }
|
---|
748 | if (trkPnt.drawLine) {
|
---|
749 | Point screen = mv.getPoint(trkPnt);
|
---|
750 | // skip points that are on the same screenposition
|
---|
751 | if (old != null
|
---|
752 | && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
|
---|
753 | || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
|
---|
754 | g.setColor(trkPnt.customColoring);
|
---|
755 | double t = Math.atan2((double) screen.y - old.y, (double) screen.x - old.x) + Math.PI;
|
---|
756 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t - PHI)),
|
---|
757 | (int) (screen.y + 10 * Math.sin(t - PHI)));
|
---|
758 | g.drawLine(screen.x, screen.y, (int) (screen.x + 10 * Math.cos(t + PHI)),
|
---|
759 | (int) (screen.y + 10 * Math.sin(t + PHI)));
|
---|
760 | oldA = screen;
|
---|
761 | }
|
---|
762 | old = screen;
|
---|
763 | }
|
---|
764 | } // end for trkpnt
|
---|
765 | }
|
---|
766 |
|
---|
767 | /****************************************************************
|
---|
768 | ********** STEP 3c - DRAW FAST ARROWS **************************
|
---|
769 | ****************************************************************/
|
---|
770 | if (lines && arrows && arrowsFast) {
|
---|
771 | Point old = null;
|
---|
772 | Point oldA = null; // last arrow painted
|
---|
773 | for (WayPoint trkPnt : visibleSegments) {
|
---|
774 | LatLon c = trkPnt.getCoor();
|
---|
775 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
776 | continue;
|
---|
777 | }
|
---|
778 | if (trkPnt.drawLine) {
|
---|
779 | Point screen = mv.getPoint(trkPnt);
|
---|
780 | // skip points that are on the same screenposition
|
---|
781 | if (old != null
|
---|
782 | && (oldA == null || screen.x < oldA.x - arrowsDelta || screen.x > oldA.x + arrowsDelta
|
---|
783 | || screen.y < oldA.y - arrowsDelta || screen.y > oldA.y + arrowsDelta)) {
|
---|
784 | g.setColor(trkPnt.customColoring);
|
---|
785 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][0], screen.y
|
---|
786 | + dir[trkPnt.dir][1]);
|
---|
787 | g.drawLine(screen.x, screen.y, screen.x + dir[trkPnt.dir][2], screen.y
|
---|
788 | + dir[trkPnt.dir][3]);
|
---|
789 | oldA = screen;
|
---|
790 | }
|
---|
791 | old = screen;
|
---|
792 | }
|
---|
793 | } // end for trkpnt
|
---|
794 | }
|
---|
795 | }
|
---|
796 |
|
---|
797 | /**
|
---|
798 | * Draw all GPX points
|
---|
799 | * @param g the common draw object to use
|
---|
800 | * @param mv the meta data to current displayed area
|
---|
801 | * @param visibleSegments segments visible in the current scope of mv
|
---|
802 | */
|
---|
803 | private void drawPoints(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
---|
804 | /****************************************************************
|
---|
805 | ********** STEP 3d - DRAW LARGE POINTS AND HDOP CIRCLE *********
|
---|
806 | ****************************************************************/
|
---|
807 | if (large || hdopCircle) {
|
---|
808 | final int halfSize = largesize/2;
|
---|
809 | for (WayPoint trkPnt : visibleSegments) {
|
---|
810 | LatLon c = trkPnt.getCoor();
|
---|
811 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
812 | continue;
|
---|
813 | }
|
---|
814 | Point screen = mv.getPoint(trkPnt);
|
---|
815 |
|
---|
816 | if (hdopCircle && trkPnt.get(GpxConstants.PT_HDOP) != null) {
|
---|
817 | // hdop value
|
---|
818 | float hdop = (Float) trkPnt.get(GpxConstants.PT_HDOP);
|
---|
819 | if (hdop < 0) {
|
---|
820 | hdop = 0;
|
---|
821 | }
|
---|
822 | Color customColoringTransparent = hdopAlpha < 0 ? trkPnt.customColoring :
|
---|
823 | new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (hdopAlpha << 24), true);
|
---|
824 | g.setColor(customColoringTransparent);
|
---|
825 | // hdop circles
|
---|
826 | int hdopp = mv.getPoint(new LatLon(
|
---|
827 | trkPnt.getCoor().lat(),
|
---|
828 | trkPnt.getCoor().lon() + 2d*6*hdop*360/40000000d)).x - screen.x;
|
---|
829 | g.drawArc(screen.x-hdopp/2, screen.y-hdopp/2, hdopp, hdopp, 0, 360);
|
---|
830 | }
|
---|
831 | if (large) {
|
---|
832 | // color the large GPS points like the gps lines
|
---|
833 | if (trkPnt.customColoring != null) {
|
---|
834 | if (trkPnt.customColoring.equals(colorCache) && colorCacheTransparent != null) {
|
---|
835 | g.setColor(colorCacheTransparent);
|
---|
836 | } else {
|
---|
837 | Color customColoringTransparent = largePointAlpha < 0 ? trkPnt.customColoring :
|
---|
838 | new Color((trkPnt.customColoring.getRGB() & 0x00ffffff) | (largePointAlpha << 24), true);
|
---|
839 |
|
---|
840 | g.setColor(customColoringTransparent);
|
---|
841 | colorCache = trkPnt.customColoring;
|
---|
842 | colorCacheTransparent = customColoringTransparent;
|
---|
843 | }
|
---|
844 | }
|
---|
845 | g.fillRect(screen.x-halfSize, screen.y-halfSize, largesize, largesize);
|
---|
846 | }
|
---|
847 | } // end for trkpnt
|
---|
848 | } // end if large || hdopcircle
|
---|
849 |
|
---|
850 | /****************************************************************
|
---|
851 | ********** STEP 3e - DRAW SMALL POINTS FOR LINES ***************
|
---|
852 | ****************************************************************/
|
---|
853 | if (!large && lines) {
|
---|
854 | g.setColor(neutralColor);
|
---|
855 | for (WayPoint trkPnt : visibleSegments) {
|
---|
856 | LatLon c = trkPnt.getCoor();
|
---|
857 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
858 | continue;
|
---|
859 | }
|
---|
860 | if (!trkPnt.drawLine) {
|
---|
861 | g.setColor(trkPnt.customColoring);
|
---|
862 | Point screen = mv.getPoint(trkPnt);
|
---|
863 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
864 | }
|
---|
865 | } // end for trkpnt
|
---|
866 | } // end if large
|
---|
867 |
|
---|
868 | /****************************************************************
|
---|
869 | ********** STEP 3f - DRAW SMALL POINTS INSTEAD OF LINES ********
|
---|
870 | ****************************************************************/
|
---|
871 | if (!large && !lines) {
|
---|
872 | g.setColor(neutralColor);
|
---|
873 | for (WayPoint trkPnt : visibleSegments) {
|
---|
874 | LatLon c = trkPnt.getCoor();
|
---|
875 | if (Double.isNaN(c.lat()) || Double.isNaN(c.lon())) {
|
---|
876 | continue;
|
---|
877 | }
|
---|
878 | Point screen = mv.getPoint(trkPnt);
|
---|
879 | g.setColor(trkPnt.customColoring);
|
---|
880 | g.drawRect(screen.x, screen.y, 0, 0);
|
---|
881 | } // end for trkpnt
|
---|
882 | } // end if large
|
---|
883 | }
|
---|
884 |
|
---|
885 | /**
|
---|
886 | * Draw GPX lines by using alpha blending
|
---|
887 | * @param g the common draw object to use
|
---|
888 | * @param mv the meta data to current displayed area
|
---|
889 | * @param visibleSegments segments visible in the current scope of mv
|
---|
890 | * @param layerAlpha the color alpha value set for that operation
|
---|
891 | */
|
---|
892 | private void drawLinesAlpha(Graphics2D g, MapView mv, List<WayPoint> visibleSegments, float layerAlpha) {
|
---|
893 |
|
---|
894 | // 1st. backup the paint environment ----------------------------------
|
---|
895 | Composite oldComposite = g.getComposite();
|
---|
896 | Stroke oldStroke = g.getStroke();
|
---|
897 | Paint oldPaint = g.getPaint();
|
---|
898 |
|
---|
899 | // 2nd. determine current scale factors -------------------------------
|
---|
900 |
|
---|
901 | // adjust global settings
|
---|
902 | final int globalLineWidth = Utils.clamp(lineWidth, 1, 20);
|
---|
903 |
|
---|
904 | // cache scale of view
|
---|
905 | final double zoomScale = mv.getDist100Pixel() / 50.0f;
|
---|
906 |
|
---|
907 | // 3rd. determine current paint parameters -----------------------------
|
---|
908 |
|
---|
909 | // alpha value is based on zoom and line with combined with global layer alpha
|
---|
910 | float theLineAlpha = (float) Utils.clamp((0.50 / zoomScale) / (globalLineWidth + 1), 0.01, 0.50) * layerAlpha;
|
---|
911 | final int theLineWith = (int) (lineWidth / zoomScale) + 1;
|
---|
912 |
|
---|
913 | // 4th setup virtual paint area ----------------------------------------
|
---|
914 |
|
---|
915 | // set line format and alpha channel for all overlays (more lines -> few overlap -> more transparency)
|
---|
916 | g.setStroke(new BasicStroke(theLineWith, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
---|
917 | g.setComposite(AlphaComposite.SrcOver.derive(theLineAlpha));
|
---|
918 |
|
---|
919 | // last used / calculated entries
|
---|
920 | Point lastPaintPnt = null;
|
---|
921 |
|
---|
922 | // 5th draw the layer ---------------------------------------------------
|
---|
923 |
|
---|
924 | // for all points
|
---|
925 | for (WayPoint trkPnt : visibleSegments) {
|
---|
926 |
|
---|
927 | // transform coordinates
|
---|
928 | final Point paintPnt = mv.getPoint(trkPnt);
|
---|
929 |
|
---|
930 | // skip single points
|
---|
931 | if (lastPaintPnt != null && trkPnt.drawLine && !lastPaintPnt.equals(paintPnt)) {
|
---|
932 |
|
---|
933 | // set different color
|
---|
934 | g.setColor(trkPnt.customColoring);
|
---|
935 |
|
---|
936 | // draw it
|
---|
937 | g.drawLine(lastPaintPnt.x, lastPaintPnt.y, paintPnt.x, paintPnt.y);
|
---|
938 | }
|
---|
939 |
|
---|
940 | lastPaintPnt = paintPnt;
|
---|
941 | }
|
---|
942 |
|
---|
943 | // @last restore modified paint environment -----------------------------
|
---|
944 | g.setPaint(oldPaint);
|
---|
945 | g.setStroke(oldStroke);
|
---|
946 | g.setComposite(oldComposite);
|
---|
947 | }
|
---|
948 |
|
---|
949 | /**
|
---|
950 | * Generates a linear gradient map image
|
---|
951 | *
|
---|
952 | * @param width image width
|
---|
953 | * @param height image height
|
---|
954 | * @param colors 1..n color descriptions
|
---|
955 | * @return image object
|
---|
956 | */
|
---|
957 | protected static BufferedImage createImageGradientMap(int width, int height, Color... colors) {
|
---|
958 |
|
---|
959 | // create image an paint object
|
---|
960 | final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
---|
961 | final Graphics2D g = img.createGraphics();
|
---|
962 |
|
---|
963 | float[] fract = new float[ colors.length ];
|
---|
964 |
|
---|
965 | // distribute fractions (define position of color in map)
|
---|
966 | for (int i = 0; i < colors.length; ++i) {
|
---|
967 | fract[i] = i * (1.0f / colors.length);
|
---|
968 | }
|
---|
969 |
|
---|
970 | // draw the gradient map
|
---|
971 | LinearGradientPaint gradient = new LinearGradientPaint(0, 0, width, height, fract, colors,
|
---|
972 | MultipleGradientPaint.CycleMethod.NO_CYCLE);
|
---|
973 | g.setPaint(gradient);
|
---|
974 | g.fillRect(0, 0, width, height);
|
---|
975 | g.dispose();
|
---|
976 |
|
---|
977 | // access it via raw interface
|
---|
978 | return img;
|
---|
979 | }
|
---|
980 |
|
---|
981 | /**
|
---|
982 | * Creates a distributed colormap by linear blending between colors
|
---|
983 | * @param lowerLimit lower limit for first visible color
|
---|
984 | * @param colors 1..n colors
|
---|
985 | * @return array of Color objects
|
---|
986 | */
|
---|
987 | protected static Color[] createColorLut(int lowerLimit, Color... colors) {
|
---|
988 |
|
---|
989 | // number of lookup entries
|
---|
990 | final int tableSize = 256;
|
---|
991 |
|
---|
992 | // access it via raw interface
|
---|
993 | final Raster imgRaster = createImageGradientMap(tableSize, 1, colors).getData();
|
---|
994 |
|
---|
995 | // the pixel storage
|
---|
996 | int[] pixel = new int[1];
|
---|
997 |
|
---|
998 | Color[] colorTable = new Color[tableSize];
|
---|
999 |
|
---|
1000 | // map the range 0..255 to 0..pi/2
|
---|
1001 | final double mapTo90Deg = Math.PI / 2.0 / 255.0;
|
---|
1002 |
|
---|
1003 | // create the lookup table
|
---|
1004 | for (int i = 0; i < tableSize; i++) {
|
---|
1005 |
|
---|
1006 | // get next single pixel
|
---|
1007 | imgRaster.getDataElements(i, 0, pixel);
|
---|
1008 |
|
---|
1009 | // get color and map
|
---|
1010 | Color c = new Color(pixel[0]);
|
---|
1011 |
|
---|
1012 | // smooth alpha like sin curve
|
---|
1013 | int alpha = (i > lowerLimit) ? (int) (Math.sin((i-lowerLimit) * mapTo90Deg) * 255) : 0;
|
---|
1014 |
|
---|
1015 | // alpha with pre-offset, first color -> full transparent
|
---|
1016 | alpha = alpha > 0 ? (20 + alpha) : 0;
|
---|
1017 |
|
---|
1018 | // shrink to maximum bound
|
---|
1019 | if (alpha > 255) {
|
---|
1020 | alpha = 255;
|
---|
1021 | }
|
---|
1022 |
|
---|
1023 | // increase transparency for higher values ( avoid big saturation )
|
---|
1024 | if (i > 240 && 255 == alpha) {
|
---|
1025 | alpha -= (i - 240);
|
---|
1026 | }
|
---|
1027 |
|
---|
1028 | // fill entry in table, assign a alpha value
|
---|
1029 | colorTable[i] = new Color(c.getRed(), c.getGreen(), c.getBlue(), alpha);
|
---|
1030 | }
|
---|
1031 |
|
---|
1032 | // transform into lookup table
|
---|
1033 | return colorTable;
|
---|
1034 | }
|
---|
1035 |
|
---|
1036 | /**
|
---|
1037 | * Creates a darker color
|
---|
1038 | * @param in Color object
|
---|
1039 | * @param adjust darker adjustment amount
|
---|
1040 | * @return new Color
|
---|
1041 | */
|
---|
1042 | protected static Color darkerColor(Color in, float adjust) {
|
---|
1043 |
|
---|
1044 | final float r = (float) in.getRed()/255;
|
---|
1045 | final float g = (float) in.getGreen()/255;
|
---|
1046 | final float b = (float) in.getBlue()/255;
|
---|
1047 |
|
---|
1048 | return new Color(r*adjust, g*adjust, b*adjust);
|
---|
1049 | }
|
---|
1050 |
|
---|
1051 | /**
|
---|
1052 | * Creates a colormap by using a static color map with 1..n colors (RGB 0.0 ..1.0)
|
---|
1053 | * @param str the filename (without extension) to look for into data/gpx
|
---|
1054 | * @return the parsed colormap
|
---|
1055 | */
|
---|
1056 | protected static Color[] createColorFromResource(String str) {
|
---|
1057 |
|
---|
1058 | // create resource string
|
---|
1059 | final String colorFile = "resource://data/gpx/" + str + ".txt";
|
---|
1060 |
|
---|
1061 | List<Color> colorList = new ArrayList<>();
|
---|
1062 |
|
---|
1063 | // try to load the file
|
---|
1064 | try (CachedFile cf = new CachedFile(colorFile); BufferedReader br = cf.getContentReader()) {
|
---|
1065 |
|
---|
1066 | String line;
|
---|
1067 |
|
---|
1068 | // process lines
|
---|
1069 | while ((line = br.readLine()) != null) {
|
---|
1070 |
|
---|
1071 | // use comma as separator
|
---|
1072 | String[] column = line.split(",", -1);
|
---|
1073 |
|
---|
1074 | // empty or comment line
|
---|
1075 | if (column.length < 3 || column[0].startsWith("#")) {
|
---|
1076 | continue;
|
---|
1077 | }
|
---|
1078 |
|
---|
1079 | // extract RGB value
|
---|
1080 | float r = Float.parseFloat(column[0]);
|
---|
1081 | float g = Float.parseFloat(column[1]);
|
---|
1082 | float b = Float.parseFloat(column[2]);
|
---|
1083 |
|
---|
1084 | // some color tables are 0..1.0 and some 0.255
|
---|
1085 | float scale = (r < 1 && g < 1 && b < 1) ? 1 : 255;
|
---|
1086 |
|
---|
1087 | colorList.add(new Color(r/scale, g/scale, b/scale));
|
---|
1088 | }
|
---|
1089 | } catch (IOException e) {
|
---|
1090 | throw new JosmRuntimeException(e);
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | // fallback if empty or failed
|
---|
1094 | if (colorList.isEmpty()) {
|
---|
1095 | colorList.add(Color.BLACK);
|
---|
1096 | colorList.add(Color.WHITE);
|
---|
1097 | } else {
|
---|
1098 | // add additional darker elements to end of list
|
---|
1099 | final Color lastColor = colorList.get(colorList.size() - 1);
|
---|
1100 | colorList.add(darkerColor(lastColor, 0.975f));
|
---|
1101 | colorList.add(darkerColor(lastColor, 0.950f));
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | return createColorLut(0, colorList.toArray(new Color[0]));
|
---|
1105 | }
|
---|
1106 |
|
---|
1107 | /**
|
---|
1108 | * Returns the next user color map
|
---|
1109 | *
|
---|
1110 | * @param userColor - default or fallback user color
|
---|
1111 | * @param tableIdx - selected user color index
|
---|
1112 | * @return color array
|
---|
1113 | */
|
---|
1114 | protected static Color[] selectColorMap(Color userColor, int tableIdx) {
|
---|
1115 |
|
---|
1116 | // generate new user color map ( dark, user color, white )
|
---|
1117 | Color[] userColor1 = createColorLut(0, userColor.darker(), userColor, userColor.brighter(), Color.WHITE);
|
---|
1118 |
|
---|
1119 | // generate new user color map ( white -> color )
|
---|
1120 | Color[] userColor2 = createColorLut(0, Color.WHITE, Color.WHITE, userColor);
|
---|
1121 |
|
---|
1122 | // generate new user color map
|
---|
1123 | Color[] colorTrafficLights = createColorLut(0, Color.WHITE, Color.GREEN.darker(), Color.YELLOW, Color.RED);
|
---|
1124 |
|
---|
1125 | // decide what, keep order is sync with setting on GUI
|
---|
1126 | Color[][] lut = {
|
---|
1127 | userColor1,
|
---|
1128 | userColor2,
|
---|
1129 | colorTrafficLights,
|
---|
1130 | heatMapLutColorJosmInferno,
|
---|
1131 | heatMapLutColorJosmViridis,
|
---|
1132 | heatMapLutColorJosmBrown2Green,
|
---|
1133 | heatMapLutColorJosmRed2Blue
|
---|
1134 | };
|
---|
1135 |
|
---|
1136 | // default case
|
---|
1137 | Color[] nextUserColor = userColor1;
|
---|
1138 |
|
---|
1139 | // select by index
|
---|
1140 | if (tableIdx >= 0 && tableIdx < lut.length) {
|
---|
1141 | nextUserColor = lut[ tableIdx ];
|
---|
1142 | }
|
---|
1143 |
|
---|
1144 | // adjust color map
|
---|
1145 | return nextUserColor;
|
---|
1146 | }
|
---|
1147 |
|
---|
1148 | /**
|
---|
1149 | * Generates a Icon
|
---|
1150 | *
|
---|
1151 | * @param userColor selected user color
|
---|
1152 | * @param tableIdx tabled index
|
---|
1153 | * @param size size of the image
|
---|
1154 | * @return a image icon that shows the
|
---|
1155 | */
|
---|
1156 | public static ImageIcon getColorMapImageIcon(Color userColor, int tableIdx, int size) {
|
---|
1157 | return new ImageIcon(createImageGradientMap(size, size, selectColorMap(userColor, tableIdx)));
|
---|
1158 | }
|
---|
1159 |
|
---|
1160 | /**
|
---|
1161 | * Draw gray heat map with current Graphics2D setting
|
---|
1162 | * @param gB the common draw object to use
|
---|
1163 | * @param mv the meta data to current displayed area
|
---|
1164 | * @param listSegm segments visible in the current scope of mv
|
---|
1165 | * @param foreComp composite use to draw foreground objects
|
---|
1166 | * @param foreStroke stroke use to draw foreground objects
|
---|
1167 | * @param backComp composite use to draw background objects
|
---|
1168 | * @param backStroke stroke use to draw background objects
|
---|
1169 | */
|
---|
1170 | private void drawHeatGrayLineMap(Graphics2D gB, MapView mv, List<WayPoint> listSegm,
|
---|
1171 | Composite foreComp, Stroke foreStroke,
|
---|
1172 | Composite backComp, Stroke backStroke) {
|
---|
1173 |
|
---|
1174 | // draw foreground
|
---|
1175 | boolean drawForeground = foreComp != null && foreStroke != null;
|
---|
1176 |
|
---|
1177 | // set initial values
|
---|
1178 | gB.setStroke(backStroke); gB.setComposite(backComp);
|
---|
1179 |
|
---|
1180 | // get last point in list
|
---|
1181 | final WayPoint lastPnt = !listSegm.isEmpty() ? listSegm.get(listSegm.size() - 1) : null;
|
---|
1182 |
|
---|
1183 | // for all points, draw single lines by using optimized drawing
|
---|
1184 | for (WayPoint trkPnt : listSegm) {
|
---|
1185 |
|
---|
1186 | // get transformed coordinates
|
---|
1187 | final Point paintPnt = mv.getPoint(trkPnt);
|
---|
1188 |
|
---|
1189 | // end of line segment or end of list reached
|
---|
1190 | if (!trkPnt.drawLine || (lastPnt == trkPnt)) {
|
---|
1191 |
|
---|
1192 | // convert to primitive type
|
---|
1193 | final int[] polyXArr = heatMapPolyX.stream().mapToInt(Integer::intValue).toArray();
|
---|
1194 | final int[] polyYArr = heatMapPolyY.stream().mapToInt(Integer::intValue).toArray();
|
---|
1195 |
|
---|
1196 | // a.) draw background
|
---|
1197 | gB.drawPolyline(polyXArr, polyYArr, polyXArr.length);
|
---|
1198 |
|
---|
1199 | // b.) draw extra foreground
|
---|
1200 | if (drawForeground && heatMapDrawExtraLine) {
|
---|
1201 |
|
---|
1202 | gB.setStroke(foreStroke); gB.setComposite(foreComp);
|
---|
1203 | gB.drawPolyline(polyXArr, polyYArr, polyXArr.length);
|
---|
1204 | gB.setStroke(backStroke); gB.setComposite(backComp);
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | // drop used points
|
---|
1208 | heatMapPolyX.clear(); heatMapPolyY.clear();
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | // store only the integer part (make sense because pixel is 1:1 here)
|
---|
1212 | heatMapPolyX.add((int) paintPnt.getX());
|
---|
1213 | heatMapPolyY.add((int) paintPnt.getY());
|
---|
1214 | }
|
---|
1215 | }
|
---|
1216 |
|
---|
1217 | /**
|
---|
1218 | * Map the gray map to heat map and draw them with current Graphics2D setting
|
---|
1219 | * @param g the common draw object to use
|
---|
1220 | * @param imgGray gray scale input image
|
---|
1221 | * @param sampleRaster the line with for drawing
|
---|
1222 | * @param outlineWidth line width for outlines
|
---|
1223 | */
|
---|
1224 | private void drawHeatMapGrayMap(Graphics2D g, BufferedImage imgGray, int sampleRaster, int outlineWidth) {
|
---|
1225 |
|
---|
1226 | final int[] imgPixels = ((DataBufferInt) imgGray.getRaster().getDataBuffer()).getData();
|
---|
1227 |
|
---|
1228 | // samples offset and bounds are scaled with line width derived from zoom level
|
---|
1229 | final int offX = Math.max(1, sampleRaster);
|
---|
1230 | final int offY = Math.max(1, sampleRaster);
|
---|
1231 |
|
---|
1232 | final int maxPixelX = imgGray.getWidth();
|
---|
1233 | final int maxPixelY = imgGray.getHeight();
|
---|
1234 |
|
---|
1235 | // always full or outlines at big samples rasters
|
---|
1236 | final boolean drawOutlines = (outlineWidth > 0) && ((0 == sampleRaster) || (sampleRaster > 10));
|
---|
1237 |
|
---|
1238 | // backup stroke
|
---|
1239 | final Stroke oldStroke = g.getStroke();
|
---|
1240 |
|
---|
1241 | // use basic stroke for outlines and default transparency
|
---|
1242 | g.setStroke(new BasicStroke(outlineWidth));
|
---|
1243 |
|
---|
1244 | int lastPixelX = 0;
|
---|
1245 | int lastPixelColor = 0;
|
---|
1246 |
|
---|
1247 | // resample gray scale image with line linear weight of next sample in line
|
---|
1248 | // process each line and draw pixels / rectangles with same color with one operations
|
---|
1249 | for (int y = 0; y < maxPixelY; y += offY) {
|
---|
1250 |
|
---|
1251 | // the lines offsets
|
---|
1252 | final int lastLineOffset = maxPixelX * (y+0);
|
---|
1253 | final int nextLineOffset = maxPixelX * (y+1);
|
---|
1254 |
|
---|
1255 | for (int x = 0; x < maxPixelX; x += offX) {
|
---|
1256 |
|
---|
1257 | int thePixelColor = 0; int thePixelCount = 0;
|
---|
1258 |
|
---|
1259 | // sample the image (it is gray scale)
|
---|
1260 | int offset = lastLineOffset + x;
|
---|
1261 |
|
---|
1262 | // merge next pixels of window of line
|
---|
1263 | for (int k = 0; k < offX && (offset + k) < nextLineOffset; k++) {
|
---|
1264 | thePixelColor += imgPixels[offset+k] & 0xFF;
|
---|
1265 | thePixelCount++;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | // mean value
|
---|
1269 | thePixelColor = thePixelCount > 0 ? (thePixelColor / thePixelCount) : 0;
|
---|
1270 |
|
---|
1271 | // restart -> use initial sample
|
---|
1272 | if (0 == x) {
|
---|
1273 | lastPixelX = 0; lastPixelColor = thePixelColor - 1;
|
---|
1274 | }
|
---|
1275 |
|
---|
1276 | boolean bDrawIt = false;
|
---|
1277 |
|
---|
1278 | // when one of segment is mapped to black
|
---|
1279 | bDrawIt = bDrawIt || (lastPixelColor == 0) || (thePixelColor == 0);
|
---|
1280 |
|
---|
1281 | // different color
|
---|
1282 | bDrawIt = bDrawIt || (Math.abs(lastPixelColor-thePixelColor) > 0);
|
---|
1283 |
|
---|
1284 | // when line is finished draw always
|
---|
1285 | bDrawIt = bDrawIt || (y >= (maxPixelY-offY));
|
---|
1286 |
|
---|
1287 | if (bDrawIt) {
|
---|
1288 |
|
---|
1289 | // draw only foreground pixels
|
---|
1290 | if (lastPixelColor > 0) {
|
---|
1291 |
|
---|
1292 | // gray to RGB mapping
|
---|
1293 | g.setColor(heatMapLutColor[ lastPixelColor ]);
|
---|
1294 |
|
---|
1295 | // box from from last Y pixel to current pixel
|
---|
1296 | if (drawOutlines) {
|
---|
1297 | g.drawRect(lastPixelX, y, offX + x - lastPixelX, offY);
|
---|
1298 | } else {
|
---|
1299 | g.fillRect(lastPixelX, y, offX + x - lastPixelX, offY);
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | // restart detection
|
---|
1304 | lastPixelX = x; lastPixelColor = thePixelColor;
|
---|
1305 | }
|
---|
1306 | }
|
---|
1307 | }
|
---|
1308 |
|
---|
1309 | // recover
|
---|
1310 | g.setStroke(oldStroke);
|
---|
1311 | }
|
---|
1312 |
|
---|
1313 | /**
|
---|
1314 | * Collect and draw GPS segments and displays a heat-map
|
---|
1315 | * @param g the common draw object to use
|
---|
1316 | * @param mv the meta data to current displayed area
|
---|
1317 | * @param visibleSegments segments visible in the current scope of mv
|
---|
1318 | */
|
---|
1319 | private void drawHeatMap(Graphics2D g, MapView mv, List<WayPoint> visibleSegments) {
|
---|
1320 |
|
---|
1321 | // get bounds of screen image and projection, zoom and adjust input parameters
|
---|
1322 | final Rectangle screenBounds = new Rectangle(mv.getWidth(), mv.getHeight());
|
---|
1323 | final MapViewState mapViewState = mv.getState();
|
---|
1324 | final double zoomScale = mv.getDist100Pixel() / 50.0f;
|
---|
1325 |
|
---|
1326 | // adjust global settings ( zero = default line width )
|
---|
1327 | final int globalLineWidth = (0 == lineWidth) ? 1 : Utils.clamp(lineWidth, 1, 20);
|
---|
1328 |
|
---|
1329 | // 1st setup virtual paint area ----------------------------------------
|
---|
1330 |
|
---|
1331 | // new image buffer needed
|
---|
1332 | final boolean imageSetup = null == heatMapImgGray || !heatMapCacheScreenBounds.equals(screenBounds);
|
---|
1333 |
|
---|
1334 | // screen bounds changed, need new image buffer ?
|
---|
1335 | if (imageSetup) {
|
---|
1336 | // we would use a "pure" grayscale image, but there is not efficient way to map gray scale values to RGB)
|
---|
1337 | heatMapImgGray = new BufferedImage(screenBounds.width, screenBounds.height, BufferedImage.TYPE_INT_ARGB);
|
---|
1338 | heatMapGraph2d = heatMapImgGray.createGraphics();
|
---|
1339 | heatMapGraph2d.setBackground(new Color(0, 0, 0, 255));
|
---|
1340 | heatMapGraph2d.setColor(Color.WHITE);
|
---|
1341 |
|
---|
1342 | // fast draw ( maybe help or not )
|
---|
1343 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
|
---|
1344 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
|
---|
1345 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
|
---|
1346 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
|
---|
1347 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
|
---|
1348 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
|
---|
1349 | heatMapGraph2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
|
---|
1350 |
|
---|
1351 | // cache it
|
---|
1352 | heatMapCacheScreenBounds = screenBounds;
|
---|
1353 | }
|
---|
1354 |
|
---|
1355 | // 2nd. determine current scale factors -------------------------------
|
---|
1356 |
|
---|
1357 | // the line width (foreground: draw extra small footprint line of track)
|
---|
1358 | int lineWidthB = (int) Math.max(1.5f * (globalLineWidth / zoomScale) + 1, 2);
|
---|
1359 | int lineWidthF = lineWidthB > 2 ? (globalLineWidth - 1) : 0;
|
---|
1360 |
|
---|
1361 | // global alpha adjustment
|
---|
1362 | float lineAlpha = (float) Utils.clamp((0.40 / zoomScale) / (globalLineWidth + 1), 0.01, 0.40);
|
---|
1363 |
|
---|
1364 | // adjust 0.15 .. 1.85
|
---|
1365 | float scaleAlpha = 1.0f + ((heatMapDrawGain/10.0f) * 0.85f);
|
---|
1366 |
|
---|
1367 | // add to calculated values
|
---|
1368 | float lineAlphaBPoint = (float) Utils.clamp((lineAlpha * 0.65) * scaleAlpha, 0.001, 0.90);
|
---|
1369 | float lineAlphaBLine = (float) Utils.clamp((lineAlpha * 1.00) * scaleAlpha, 0.001, 0.90);
|
---|
1370 | float lineAlphaFLine = (float) Utils.clamp((lineAlpha / 1.50) * scaleAlpha, 0.001, 0.90);
|
---|
1371 |
|
---|
1372 | // 3rd Calculate the heat map data by draw GPX traces with alpha value ----------
|
---|
1373 |
|
---|
1374 | // recalculation of image needed
|
---|
1375 | final boolean imageRecalc = !mapViewState.equalsInWindow(heatMapMapViewState)
|
---|
1376 | || gpxLayerInvalidated
|
---|
1377 | || heatMapCacheLineWith != globalLineWidth;
|
---|
1378 |
|
---|
1379 | // need re-generation of gray image ?
|
---|
1380 | if (imageSetup || imageRecalc) {
|
---|
1381 |
|
---|
1382 | // clear background
|
---|
1383 | heatMapGraph2d.clearRect(0, 0, heatMapImgGray.getWidth(), heatMapImgGray.getHeight());
|
---|
1384 |
|
---|
1385 | // point or line blending
|
---|
1386 | if (heatMapDrawPointMode) {
|
---|
1387 | heatMapGraph2d.setComposite(AlphaComposite.SrcOver.derive(lineAlphaBPoint));
|
---|
1388 | drawHeatGrayDotMap(heatMapGraph2d, mv, visibleSegments, lineWidthB);
|
---|
1389 |
|
---|
1390 | } else {
|
---|
1391 | drawHeatGrayLineMap(heatMapGraph2d, mv, visibleSegments,
|
---|
1392 | lineWidthF > 1 ? AlphaComposite.SrcOver.derive(lineAlphaFLine) : null,
|
---|
1393 | new BasicStroke(lineWidthF, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND),
|
---|
1394 | AlphaComposite.SrcOver.derive(lineAlphaBLine),
|
---|
1395 | new BasicStroke(lineWidthB, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
|
---|
1396 | }
|
---|
1397 |
|
---|
1398 | // remember draw parameter
|
---|
1399 | heatMapMapViewState = mapViewState;
|
---|
1400 | heatMapCacheLineWith = globalLineWidth;
|
---|
1401 | gpxLayerInvalidated = false;
|
---|
1402 | }
|
---|
1403 |
|
---|
1404 | // 4th. Draw data on target layer, map data via color lookup table --------------
|
---|
1405 | drawHeatMapGrayMap(g, heatMapImgGray, lineWidthB > 2 ? (int) (lineWidthB*1.25f) : 1, lineWidth > 2 ? (lineWidth - 2) : 1);
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 | /**
|
---|
1409 | * Draw a dotted heat map
|
---|
1410 | *
|
---|
1411 | * @param gB the common draw object to use
|
---|
1412 | * @param mv the meta data to current displayed area
|
---|
1413 | * @param listSegm segments visible in the current scope of mv
|
---|
1414 | * @param drawSize draw size of draw element
|
---|
1415 | */
|
---|
1416 | private static void drawHeatGrayDotMap(Graphics2D gB, MapView mv, List<WayPoint> listSegm, int drawSize) {
|
---|
1417 |
|
---|
1418 | // typical rendering rate -> use realtime preview instead of accurate display
|
---|
1419 | final double maxSegm = 25_000, nrSegms = listSegm.size();
|
---|
1420 |
|
---|
1421 | // determine random drop rate
|
---|
1422 | final double randomDrop = Math.min(nrSegms > maxSegm ? (nrSegms - maxSegm) / nrSegms : 0, 0.70f);
|
---|
1423 |
|
---|
1424 | // http://www.nstb.tc.faa.gov/reports/PAN94_0716.pdf#page=22
|
---|
1425 | // Global Average Position Domain Accuracy, typical -> not worst case !
|
---|
1426 | // < 4.218 m Vertical
|
---|
1427 | // < 2.168 m Horizontal
|
---|
1428 | final double pixelRmsX = (100 / mv.getDist100Pixel()) * 2.168;
|
---|
1429 | final double pixelRmsY = (100 / mv.getDist100Pixel()) * 4.218;
|
---|
1430 |
|
---|
1431 | Point lastPnt = null;
|
---|
1432 |
|
---|
1433 | // for all points, draw single lines
|
---|
1434 | for (WayPoint trkPnt : listSegm) {
|
---|
1435 |
|
---|
1436 | // get transformed coordinates
|
---|
1437 | final Point paintPnt = mv.getPoint(trkPnt);
|
---|
1438 |
|
---|
1439 | // end of line segment or end of list reached
|
---|
1440 | if (trkPnt.drawLine && null != lastPnt) {
|
---|
1441 | drawHeatSurfaceLine(gB, paintPnt, lastPnt, drawSize, pixelRmsX, pixelRmsY, randomDrop);
|
---|
1442 | }
|
---|
1443 |
|
---|
1444 | // remember
|
---|
1445 | lastPnt = paintPnt;
|
---|
1446 | }
|
---|
1447 | }
|
---|
1448 |
|
---|
1449 | /**
|
---|
1450 | * Draw a dotted surface line
|
---|
1451 | *
|
---|
1452 | * @param g the common draw object to use
|
---|
1453 | * @param fromPnt start point
|
---|
1454 | * @param toPnt end point
|
---|
1455 | * @param drawSize size of draw elements
|
---|
1456 | * @param rmsSizeX RMS size of circle for X (width)
|
---|
1457 | * @param rmsSizeY RMS size of circle for Y (height)
|
---|
1458 | * @param dropRate Pixel render drop rate
|
---|
1459 | */
|
---|
1460 | private static void drawHeatSurfaceLine(Graphics2D g,
|
---|
1461 | Point fromPnt, Point toPnt, int drawSize, double rmsSizeX, double rmsSizeY, double dropRate) {
|
---|
1462 |
|
---|
1463 | // collect frequently used items
|
---|
1464 | final long fromX = (long) fromPnt.getX(); final long deltaX = (long) (toPnt.getX() - fromX);
|
---|
1465 | final long fromY = (long) fromPnt.getY(); final long deltaY = (long) (toPnt.getY() - fromY);
|
---|
1466 |
|
---|
1467 | // use same random values for each point
|
---|
1468 | final Random heatMapRandom = new Random(fromX+fromY+deltaX+deltaY);
|
---|
1469 |
|
---|
1470 | // cache distance between start and end point
|
---|
1471 | final int dist = (int) Math.abs(fromPnt.distance(toPnt));
|
---|
1472 |
|
---|
1473 | // number of increment ( fill wide distance tracks )
|
---|
1474 | double scaleStep = Math.max(1.0f / dist, dist > 100 ? 0.10f : 0.20f);
|
---|
1475 |
|
---|
1476 | // number of additional random points
|
---|
1477 | int rounds = Math.min(drawSize/2, 1)+1;
|
---|
1478 |
|
---|
1479 | // decrease random noise at high drop rate ( more accurate draw of fewer points )
|
---|
1480 | rmsSizeX *= (1.0d - dropRate);
|
---|
1481 | rmsSizeY *= (1.0d - dropRate);
|
---|
1482 |
|
---|
1483 | double scaleVal = 0;
|
---|
1484 |
|
---|
1485 | // interpolate line draw ( needs separate point instead of line )
|
---|
1486 | while (scaleVal < (1.0d-0.0001d)) {
|
---|
1487 |
|
---|
1488 | // get position
|
---|
1489 | final double pntX = fromX + scaleVal * deltaX;
|
---|
1490 | final double pntY = fromY + scaleVal * deltaY;
|
---|
1491 |
|
---|
1492 | // add random distribution around sampled point
|
---|
1493 | for (int k = 0; k < rounds; k++) {
|
---|
1494 |
|
---|
1495 | // add error distribution, first point with less error
|
---|
1496 | int x = (int) (pntX + heatMapRandom.nextGaussian() * (k > 0 ? rmsSizeX : rmsSizeX/4));
|
---|
1497 | int y = (int) (pntY + heatMapRandom.nextGaussian() * (k > 0 ? rmsSizeY : rmsSizeY/4));
|
---|
1498 |
|
---|
1499 | // draw it, even drop is requested
|
---|
1500 | if (heatMapRandom.nextDouble() >= dropRate) {
|
---|
1501 | g.fillRect(x-drawSize, y-drawSize, drawSize, drawSize);
|
---|
1502 | }
|
---|
1503 | }
|
---|
1504 | scaleVal += scaleStep;
|
---|
1505 | }
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | /**
|
---|
1509 | * Apply default color configuration to way segments
|
---|
1510 | * @param visibleSegments segments visible in the current scope of mv
|
---|
1511 | */
|
---|
1512 | private void fixColors(List<WayPoint> visibleSegments) {
|
---|
1513 | for (WayPoint trkPnt : visibleSegments) {
|
---|
1514 | if (trkPnt.customColoring == null) {
|
---|
1515 | trkPnt.customColoring = neutralColor;
|
---|
1516 | }
|
---|
1517 | }
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | /**
|
---|
1521 | * Check cache validity set necessary flags
|
---|
1522 | */
|
---|
1523 | private void checkCache() {
|
---|
1524 | // CHECKSTYLE.OFF: BooleanExpressionComplexity
|
---|
1525 | if ((computeCacheMaxLineLengthUsed != maxLineLength)
|
---|
1526 | || (computeCacheColored != colored)
|
---|
1527 | || (computeCacheVelocityTune != velocityTune)
|
---|
1528 | || (computeCacheColorDynamic != colorModeDynamic)
|
---|
1529 | || (computeCacheHeatMapDrawColorTableIdx != heatMapDrawColorTableIdx)
|
---|
1530 | || !Objects.equals(neutralColor, computeCacheColorUsed)
|
---|
1531 | || (computeCacheHeatMapDrawPointMode != heatMapDrawPointMode)
|
---|
1532 | || (computeCacheHeatMapDrawGain != heatMapDrawGain)
|
---|
1533 | || (computeCacheHeatMapDrawLowerLimit != heatMapDrawLowerLimit)
|
---|
1534 | ) {
|
---|
1535 | // CHECKSTYLE.ON: BooleanExpressionComplexity
|
---|
1536 | computeCacheMaxLineLengthUsed = maxLineLength;
|
---|
1537 | computeCacheInSync = false;
|
---|
1538 | computeCacheColorUsed = neutralColor;
|
---|
1539 | computeCacheColored = colored;
|
---|
1540 | computeCacheVelocityTune = velocityTune;
|
---|
1541 | computeCacheColorDynamic = colorModeDynamic;
|
---|
1542 | computeCacheHeatMapDrawColorTableIdx = heatMapDrawColorTableIdx;
|
---|
1543 | computeCacheHeatMapDrawPointMode = heatMapDrawPointMode;
|
---|
1544 | computeCacheHeatMapDrawGain = heatMapDrawGain;
|
---|
1545 | computeCacheHeatMapDrawLowerLimit = heatMapDrawLowerLimit;
|
---|
1546 | }
|
---|
1547 | }
|
---|
1548 |
|
---|
1549 | /**
|
---|
1550 | * callback when data is changed, invalidate cached configuration parameters
|
---|
1551 | */
|
---|
1552 | @Override
|
---|
1553 | public void gpxDataChanged(GpxDataChangeEvent e) {
|
---|
1554 | computeCacheInSync = false;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | /**
|
---|
1558 | * Draw all GPX arrays
|
---|
1559 | * @param g the common draw object to use
|
---|
1560 | * @param mv the meta data to current displayed area
|
---|
1561 | */
|
---|
1562 | public void drawColorBar(Graphics2D g, MapView mv) {
|
---|
1563 | int w = mv.getWidth();
|
---|
1564 |
|
---|
1565 | // set do default
|
---|
1566 | g.setComposite(AlphaComposite.SrcOver.derive(1.00f));
|
---|
1567 |
|
---|
1568 | if (colored == ColorMode.HDOP) {
|
---|
1569 | hdopScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
|
---|
1570 | } else if (colored == ColorMode.QUALITY) {
|
---|
1571 | qualityScale.drawColorBar(g, w-30, 50, 20, 100, 1.0);
|
---|
1572 | } else if (colored == ColorMode.FIX) {
|
---|
1573 | fixScale.drawColorBar(g, w-30, 50, 20, 175, 1.0);
|
---|
1574 | } else if (colored == ColorMode.VELOCITY) {
|
---|
1575 | SystemOfMeasurement som = SystemOfMeasurement.getSystemOfMeasurement();
|
---|
1576 | velocityScale.drawColorBar(g, w-30, 50, 20, 100, som.speedValue);
|
---|
1577 | } else if (colored == ColorMode.DIRECTION) {
|
---|
1578 | directionScale.drawColorBar(g, w-30, 50, 20, 100, 180.0/Math.PI);
|
---|
1579 | }
|
---|
1580 | }
|
---|
1581 |
|
---|
1582 | @Override
|
---|
1583 | public void paintableInvalidated(PaintableInvalidationEvent event) {
|
---|
1584 | gpxLayerInvalidated = true;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 | @Override
|
---|
1588 | public void detachFromMapView(MapViewEvent event) {
|
---|
1589 | SystemOfMeasurement.removeSoMChangeListener(this);
|
---|
1590 | layer.removeInvalidationListener(this);
|
---|
1591 | data.removeChangeListener(this);
|
---|
1592 | }
|
---|
1593 | }
|
---|