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.exceptions.Throw;
7 import org.opentrafficsim.core.network.NetworkException;
8 import org.opentrafficsim.core.object.StaticObject;
9 import org.opentrafficsim.road.network.lane.Lane;
10 import org.opentrafficsim.road.network.lane.object.detector.LaneDetector;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 public abstract class AbstractLaneBasedObject extends StaticObject implements LaneBasedObject
27 {
28
29 private static final long serialVersionUID = 20160909L;
30
31
32 private final Lane lane;
33
34
35 private final Length longitudinalPosition;
36
37
38
39
40
41
42
43
44
45
46
47
48 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
49 final PolyLine2d geometry, final Length height) throws NetworkException
50 {
51 super(id, getPoint(lane, longitudinalPosition), geometry, height);
52
53 Throw.whenNull(lane, "lane is null");
54 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
55 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLength().si,
56 NetworkException.class, "Position of the object on the lane is out of bounds");
57
58 this.lane = lane;
59 this.longitudinalPosition = longitudinalPosition;
60 }
61
62
63
64
65
66
67
68 private static OrientedPoint2d getPoint(final Lane lane, final Length longitudinalPosition)
69 {
70 return lane.getCenterLine().getLocationExtended(longitudinalPosition);
71 }
72
73
74
75
76
77
78
79
80
81
82 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
83 final PolyLine2d geometry) throws NetworkException
84 {
85 this(id, lane, longitudinalPosition, geometry, Length.ZERO);
86 }
87
88
89 @Override
90 protected void init() throws NetworkException
91 {
92 super.init();
93
94
95 if (!(this instanceof LaneDetector))
96 {
97 this.lane.addLaneBasedObject(this);
98 }
99 }
100
101
102 @Override
103 public final String getFullId()
104 {
105 return getLane().getFullId() + "." + super.getId();
106 }
107
108
109 @Override
110 public final Lane getLane()
111 {
112 return this.lane;
113 }
114
115
116 @Override
117 public final Length getLongitudinalPosition()
118 {
119 return this.longitudinalPosition;
120 }
121
122
123 @Override
124 @SuppressWarnings("checkstyle:designforextension")
125 public String toString()
126 {
127 return "LaneBasedObject[" + getId() + "]";
128 }
129
130 }