MATSIM
PositionInfo.java
Go to the documentation of this file.
1 package org.matsim.vis.snapshotwriters;
2 
3 import org.matsim.api.core.v01.Coord;
4 import org.matsim.api.core.v01.Id;
9 
10 import static java.util.Objects.requireNonNull;
11 
24 public class PositionInfo implements AgentSnapshotInfo {
25 
26  private static final double TWO_PI = 2.0 * Math.PI;
27  private static final double PI_HALF = Math.PI / 2.0;
28 
29  private final Id<Person> agentId;
30  private final Id<Link> linkId;
31  private final Id<Vehicle> vehicleId;
32  private final double easting;
33  private final double northing;
34  private final double colorValue;
35  private final AgentState agentState;
36  private final DrivingState drivingState;
37  private final int user;
38 
39  private PositionInfo(Id<Person> agentId, Id<Link> linkId, Id<Vehicle> vehicleId, double easting, double northing, double colorValue, AgentState agentState, DrivingState drivingState, int user) {
40  this.agentId = agentId;
41  this.linkId = linkId;
42  this.vehicleId = vehicleId;
43  this.easting = easting;
44  this.northing = northing;
45  this.colorValue = colorValue;
46  this.agentState = agentState;
47  this.drivingState = drivingState;
48  this.user = user;
49  }
50 
51  @Override
52  public final Id<Person> getId() {
53  return this.agentId;
54  }
55 
56  @Override
57  public final Id<Link> getLinkId() {
58  return this.linkId;
59  }
60 
61  @Override
62  public final Id<Vehicle> getVehicleId() {
63  return vehicleId;
64  }
65 
66  @Override
67  public final double getEasting() {
68  return this.easting;
69  }
70 
71  @Override
72  public final double getNorthing() {
73  return this.northing;
74  }
75 
76  @Override
77  public final double getAzimuth() {
78  throw new RuntimeException("this is deprecated. Get over it.");
79  }
80 
81  @Override
82  public final double getColorValueBetweenZeroAndOne() {
83  return this.colorValue;
84  }
85 
86  @Override
87  public final AgentState getAgentState() {
88  return this.agentState;
89  }
90 
91  @Override
92  public final DrivingState getDrivingState() {
93  return this.drivingState;
94  }
95 
96  @Override
97  public int getUserDefined() {
98  return this.user;
99  }
100 
101  @Override
102  public String toString() {
103  return "PositionInfo; agentId: " + this.agentId.toString()
104  + " easting: " + this.easting
105  + " northing: " + this.northing;
106  }
107 
108  public static class DirectBuilder {
109 
110  private double easting;
111  private double northing;
113  private double colorValue;
114  private int userDefined;
115  private AgentState agentState;
116  private Id<Link> linkId;
118 
119  public DirectBuilder() {
120  }
121 
122  public DirectBuilder setEasting(double easting) {
123  this.easting = easting;
124  return this;
125  }
126 
127  public DirectBuilder setNorthing(double northing) {
128  this.northing = northing;
129  return this;
130  }
131 
133  this.personId = personId;
134  return this;
135  }
136 
137  public DirectBuilder setColorValue(double colorValue) {
138  this.colorValue = colorValue;
139  return this;
140  }
141 
142  public DirectBuilder setUserDefined(int userDefined) {
143  this.userDefined = userDefined;
144  return this;
145  }
146 
147  public DirectBuilder setAgentState(AgentState agentState) {
148  this.agentState = agentState;
149  return this;
150  }
151 
153  this.linkId = id;
154  return this;
155  }
156 
158  this.vehicleId = id;
159  return this;
160  }
161 
163  return new PositionInfo(personId, linkId, vehicleId, easting, northing, colorValue, agentState, null, userDefined);
164  }
165  }
166 
167  public static class LinkBasedBuilder {
168 
170 
171  private Id<Person> agentId = null;
172  private Id<Link> linkId = null;
173  private Id<Vehicle> vehicleId = null;
174  private Coord fromCoord;
175  private Coord toCoord;
176  private double linkLength;
177  private int lane;
178  private double distanceOnLink;
179  private AgentSnapshotInfo.AgentState agentState = null;
180  private AgentSnapshotInfo.DrivingState drivingState = null;
181  private double colorValue;
182  private int user;
183 
184  public LinkBasedBuilder() {
185  }
186 
188  this.linkWidthCalculator = linkWidthCalculator;
189  return this;
190  }
191 
193  this.agentId = personId;
194  return this;
195  }
196 
198  this.linkId = linkId;
199  return this;
200  }
201 
203  this.vehicleId = vehicleId;
204  return this;
205  }
206 
208  this.agentState = agentState;
209  return this;
210  }
211 
213  this.drivingState = drivingState;
214  return this;
215  }
216 
217  public LinkBasedBuilder setFromCoord(Coord fromCoord) {
218  this.fromCoord = fromCoord;
219  return this;
220  }
221 
223  this.toCoord = toCoord;
224  return this;
225  }
226 
227  public LinkBasedBuilder setLinkLength(double linkLength) {
228  this.linkLength = linkLength;
229  return this;
230  }
231 
232  public LinkBasedBuilder setLane(int lane) {
233  this.lane = lane;
234  return this;
235  }
236 
237  public LinkBasedBuilder setDistanceOnLink(double distanceOnLink) {
238  this.distanceOnLink = distanceOnLink;
239  return this;
240  }
241 
242  public LinkBasedBuilder setColorValue(double colorValue) {
243  this.colorValue = colorValue;
244  return this;
245  }
246 
247  public LinkBasedBuilder setUser(int user) {
248  this.user = user;
249  return this;
250  }
251 
253 
254  requireNonNull(this.linkWidthCalculator);
255  requireNonNull(fromCoord);
256  requireNonNull(toCoord);
257 
258  var theta = calculateTheta(this.fromCoord, this.toCoord);
259  var euclideanLength = CoordUtils.calcEuclideanDistance(this.fromCoord, this.toCoord);
260  var correction = calculateCorrection(euclideanLength, this.linkLength);
261  var lanePosition = linkWidthCalculator.calculateLanePosition(lane);
262  var easting = fromCoord.getX()
263  + (Math.cos(theta) * distanceOnLink * correction)
264  + (Math.sin(theta) * lanePosition);
265  var northing = fromCoord.getY()
266  + Math.sin(theta) * distanceOnLink * correction
267  - Math.cos(theta) * lanePosition;
268 
269  return new PositionInfo(
270  this.agentId, this.linkId, this.vehicleId,
271  easting, northing,
272  colorValue, agentState, drivingState, user);
273  }
274 
275  private double calculateTheta(Coord startCoord, Coord endCoord) {
276 
277  double dx = -startCoord.getX() + endCoord.getX();
278  double dy = -startCoord.getY() + endCoord.getY();
279  double theta;
280  if (dx > 0) {
281  theta = Math.atan(dy / dx);
282  } else if (dx < 0) {
283  theta = Math.PI + Math.atan(dy / dx);
284  } else { // i.e. DX==0
285  if (dy > 0) {
286  theta = PI_HALF;
287  } else if (dy < 0) {
288  theta = -PI_HALF;
289  } else { // i.e. DX==0 && DY==0
290  theta = 0.833 * Math.PI; // some default direction towards north north east
291  }
292  }
293  if (theta < 0.0) theta += TWO_PI;
294 
295  return theta;
296  }
297 
298  private double calculateCorrection(double euclideanLength, double curvedLength) {
299  return curvedLength != 0 ? euclideanLength / curvedLength : 0;
300  }
301  }
302 }
static double calcEuclideanDistance(Coord coord, Coord other)
DirectBuilder setPersonId(Id< Person > personId)
PositionInfo(Id< Person > agentId, Id< Link > linkId, Id< Vehicle > vehicleId, double easting, double northing, double colorValue, AgentState agentState, DrivingState drivingState, int user)
DirectBuilder setAgentState(AgentState agentState)