1 package org.opentrafficsim.editor.extensions.map.edit;
2
3 import org.djutils.draw.point.Point2d;
4 import org.opentrafficsim.animation.OtsRenderable;
5 import org.opentrafficsim.base.geometry.OtsShape;
6 import org.opentrafficsim.editor.extensions.map.EditorMap;
7
8 /**
9 * Convenience class where sub-classes can use {@link #px(double)} or {@link #px(float[])} to obtain a paint stroke defined at
10 * pixel level for a locatable that is otherwise scaled in world coordinates.
11 * <p>
12 * Copyright (c) 2026-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.<br>
13 * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
14 * @author Wouter Schakel
15 * @param <L> locatable type
16 */
17 public abstract class MapAnnotation<L extends OtsShape> extends OtsRenderable<L>
18 {
19
20 /** Default point. */
21 static final Point2d ZERO = new Point2d(0.0, 0.0);
22
23 /** Map. */
24 private final EditorMap map;
25
26 /**
27 * Constructor.
28 * @param source source
29 * @param map map
30 */
31 public MapAnnotation(final L source, final EditorMap map)
32 {
33 super(source, map.getContextualized());
34 this.map = map;
35 }
36
37 /**
38 * Returns the map.
39 * @return map
40 */
41 protected EditorMap getMap()
42 {
43 return this.map;
44 }
45
46 /**
47 * Returns a world line width that corresponds to a pixel width of the input value.
48 * @param width intended pixel line width
49 * @return world line width that corresponds to a pixel width of the input value
50 */
51 protected float px(final double width)
52 {
53 return isScale() ? (float) (width * this.map.getPanel().pxScale()) : (float) width;
54 }
55
56 /**
57 * Scales the value in the input array such that an intended pixel dash pattern is translated in to current world
58 * coordinates.
59 * @param dash dash pattern
60 * @return input array
61 */
62 protected float[] px(final float[] dash)
63 {
64 if (!isScale())
65 {
66 return dash;
67 }
68 double scale = this.map.getPanel().pxScale();
69 for (int i = 0; i < dash.length; i++)
70 {
71 dash[i] = (float) (dash[i] * scale);
72 }
73 return dash;
74 }
75
76 }