1 package org.opentrafficsim.road.network.lane.object;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djutils.exceptions.Throw;
5 import org.opentrafficsim.core.geometry.DirectedPoint;
6 import org.opentrafficsim.core.geometry.OtsLine3d;
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 private final DirectedPoint location;
39
40
41
42
43
44
45
46
47
48
49
50
51 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
52 final OtsLine3d geometry, final Length height) throws NetworkException
53 {
54 super(id, geometry, height);
55
56 Throw.whenNull(lane, "lane is null");
57 Throw.whenNull(longitudinalPosition, "longitudinal position is null");
58 Throw.when(longitudinalPosition.si < 0.0 || longitudinalPosition.si > lane.getCenterLine().getLengthSI(),
59 NetworkException.class, "Position of the object on the lane is out of bounds");
60
61 this.lane = lane;
62 this.longitudinalPosition = longitudinalPosition;
63 DirectedPoint p = lane.getCenterLine().getLocationExtended(this.longitudinalPosition);
64 this.location = new DirectedPoint(p.x, p.y, p.z + 0.01, p.getRotX(), p.getRotY(), p.getRotZ());
65 }
66
67
68
69
70
71
72
73
74
75
76 protected AbstractLaneBasedObject(final String id, final Lane lane, final Length longitudinalPosition,
77 final OtsLine3d geometry) throws NetworkException
78 {
79 this(id, lane, longitudinalPosition, geometry, Length.ZERO);
80 }
81
82
83 @Override
84 protected void init() throws NetworkException
85 {
86 super.init();
87
88
89 if (!(this instanceof LaneDetector))
90 {
91 this.lane.addLaneBasedObject(this);
92 }
93 }
94
95
96 @Override
97 public final String getFullId()
98 {
99 return getLane().getFullId() + "." + super.getId();
100 }
101
102
103 @Override
104 public final Lane getLane()
105 {
106 return this.lane;
107 }
108
109
110 @Override
111 public final Length getLongitudinalPosition()
112 {
113 return this.longitudinalPosition;
114 }
115
116
117 @Override
118 @SuppressWarnings("checkstyle:designforextension")
119 public DirectedPoint getLocation()
120 {
121 return this.location;
122 }
123
124
125 @Override
126 @SuppressWarnings("checkstyle:designforextension")
127 public String toString()
128 {
129 return "LaneBasedObject[" + getId() + "]";
130 }
131
132 }