1 package org.opentrafficsim.road.network.lane;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.opentrafficsim.core.geometry.OTSLine3D;
7 import org.opentrafficsim.core.gtu.GTUDirectionality;
8 import org.opentrafficsim.core.gtu.GTUException;
9
10 import nl.tudelft.simulation.language.Throw;
11 import nl.tudelft.simulation.language.d3.DirectedPoint;
12
13
14
15
16
17
18
19
20
21
22
23
24 public class DirectedLanePosition implements Serializable
25 {
26
27 private static final long serialVersionUID = 20151111L;
28
29
30 private final Lane lane;
31
32
33 private final Length position;
34
35
36 private final GTUDirectionality gtuDirection;
37
38
39
40
41
42
43
44
45
46 public DirectedLanePosition(final Lane lane, final Length position, final GTUDirectionality gtuDirection)
47 throws GTUException
48 {
49 super();
50 Throw.when(lane == null, GTUException.class, "lane is null");
51 Throw.when(position == null, GTUException.class, "position is null");
52 Throw.when(gtuDirection == null, GTUException.class, "gtuDirection is null");
53 this.lane = lane;
54 this.position = position;
55 this.gtuDirection = gtuDirection;
56 }
57
58
59
60
61
62 public final Lane getLane()
63 {
64 return this.lane;
65 }
66
67
68
69
70
71 public final Length getPosition()
72 {
73 return this.position;
74 }
75
76
77
78
79
80
81 public final GTUDirectionality getGtuDirection()
82 {
83 return this.gtuDirection;
84 }
85
86
87
88
89
90 public DirectedPoint getLocation()
91 {
92
93 OTSLine3D centerLine = this.lane.getCenterLine();
94 double centerLineLength = centerLine.getLengthSI();
95 double fraction = this.position.si / centerLineLength;
96 DirectedPoint p = centerLine.getLocationFractionExtended(fraction);
97 if (this.gtuDirection.equals(GTUDirectionality.DIR_PLUS))
98 {
99 return p;
100 }
101 return new DirectedPoint(p.x, p.y, p.z, p.getRotX(), p.getRotY(), p.getRotZ() + Math.PI);
102 }
103
104
105 @Override
106 public int hashCode()
107 {
108 final int prime = 31;
109 int result = 1;
110 result = prime * result + ((this.gtuDirection == null) ? 0 : this.gtuDirection.hashCode());
111 result = prime * result + ((this.lane == null) ? 0 : this.lane.hashCode());
112 result = prime * result + ((this.position == null) ? 0 : this.position.hashCode());
113 return result;
114 }
115
116
117 @Override
118 public boolean equals(final Object obj)
119 {
120 if (this == obj)
121 return true;
122 if (obj == null)
123 return false;
124 if (getClass() != obj.getClass())
125 return false;
126 DirectedLanePosition other = (DirectedLanePosition) obj;
127 if (this.gtuDirection != other.gtuDirection)
128 return false;
129 if (this.lane == null)
130 {
131 if (other.lane != null)
132 return false;
133 }
134 else if (!this.lane.equals(other.lane))
135 return false;
136 if (this.position == null)
137 {
138 if (other.position != null)
139 return false;
140 }
141 else if (!this.position.equals(other.position))
142 return false;
143 return true;
144 }
145
146
147 @Override
148 public String toString()
149 {
150 return "DirectedLanePosition [lane=" + this.lane + ", position=" + this.position + ", gtuDirection="
151 + this.gtuDirection + "]";
152 }
153
154 }