1 package org.opentrafficsim.road.network.lane.object;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djutils.draw.line.PolyLine2d;
5 import org.djutils.draw.point.OrientedPoint2d;
6 import org.djutils.draw.point.Point2d;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.core.dsol.OtsSimulatorInterface;
9 import org.opentrafficsim.core.object.LocatedObject;
10 import org.opentrafficsim.road.network.lane.Lane;
11
12
13
14
15
16
17
18
19
20
21
22 public interface LaneBasedObject extends LocatedObject
23 {
24
25
26 Lane getLane();
27
28
29 Length getLongitudinalPosition();
30
31
32
33
34
35 default Length getLength()
36 {
37 return Length.ZERO;
38 }
39
40 @Override
41 OrientedPoint2d getLocation();
42
43
44
45
46
47 default PolyLine2d getLine()
48 {
49 return makeLine(getLane(), getLongitudinalPosition());
50 }
51
52
53
54
55
56 default OtsSimulatorInterface getSimulator()
57 {
58 return getLane().getLink().getSimulator();
59 }
60
61
62
63
64
65
66
67 static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition)
68 {
69 return makeLine(lane, longitudinalPosition, 0.9);
70 }
71
72
73
74
75
76
77
78
79 static PolyLine2d makeLine(final Lane lane, final Length longitudinalPosition, final double relativeWidth)
80 {
81 Throw.whenNull(lane, "lane is null");
82 Throw.whenNull(longitudinalPosition, "position is null");
83 Throw.whenNull(relativeWidth, "relatve width is null");
84 double w50 = lane.getWidth(longitudinalPosition).si * 0.5 * relativeWidth;
85 OrientedPoint2d c = lane.getCenterLine().getLocationExtended(longitudinalPosition);
86 double a = c.getDirZ();
87 Point2d p1 = new Point2d(c.x + w50 * Math.cos(a + Math.PI / 2), c.y - w50 * Math.sin(a + Math.PI / 2));
88 Point2d p2 = new Point2d(c.x - w50 * Math.cos(a + Math.PI / 2), c.y + w50 * Math.sin(a + Math.PI / 2));
89 return new PolyLine2d(p1, p2);
90 }
91
92 }