1 package org.opentrafficsim.road.gtu.generator;
2
3 import java.rmi.RemoteException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Comparator;
7 import java.util.EnumMap;
8 import java.util.LinkedHashMap;
9 import java.util.LinkedHashSet;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.Set;
13
14 import org.djunits.unit.SpeedUnit;
15 import org.djunits.value.vdouble.scalar.Length;
16 import org.djunits.value.vdouble.scalar.Speed;
17 import org.djutils.draw.point.Point3d;
18 import org.djutils.exceptions.Throw;
19 import org.opentrafficsim.core.geometry.Bounds;
20 import org.opentrafficsim.core.geometry.DirectedPoint;
21 import org.opentrafficsim.core.geometry.OTSGeometryException;
22 import org.opentrafficsim.core.gtu.GTUDirectionality;
23 import org.opentrafficsim.core.gtu.GTUType;
24 import org.opentrafficsim.core.math.Draw;
25 import org.opentrafficsim.core.network.Link;
26 import org.opentrafficsim.core.network.LinkDirection;
27 import org.opentrafficsim.core.network.NetworkException;
28 import org.opentrafficsim.core.network.Node;
29 import org.opentrafficsim.core.network.route.Route;
30 import org.opentrafficsim.road.gtu.generator.GeneratorPositions.RoadPosition.BySpeed;
31 import org.opentrafficsim.road.gtu.generator.GeneratorPositions.RoadPosition.ByValue;
32 import org.opentrafficsim.road.gtu.strategical.route.RouteGeneratorOD;
33 import org.opentrafficsim.road.network.lane.CrossSectionLink;
34 import org.opentrafficsim.road.network.lane.DirectedLanePosition;
35 import org.opentrafficsim.road.network.lane.Lane;
36
37 import nl.tudelft.simulation.dsol.animation.Locatable;
38 import nl.tudelft.simulation.jstats.streams.StreamInterface;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 public final class GeneratorPositions implements Locatable
54 {
55
56
57 private final GeneratorZonePosition position;
58
59
60 private final StreamInterface stream;
61
62
63 private final DirectedPoint location;
64
65
66 private final Bounds bounds;
67
68
69 private final Set<GeneratorLanePosition> allPositions = new LinkedHashSet<>();
70
71
72
73
74
75
76
77 @SuppressWarnings("synthetic-access")
78 private GeneratorPositions(final GeneratorZonePosition position, final LaneBiases biases, final StreamInterface stream)
79 {
80 this.position = position;
81 for (GeneratorLinkPosition glp : this.position.positions)
82 {
83 if (glp.getDirection().equals(GTUDirectionality.DIR_MINUS))
84 {
85 System.out.println("Hmm... GTUDirectionality is DIR_MINUS: " + glp);
86 }
87 }
88 this.stream = stream;
89 double x = 0.0;
90 double y = 0.0;
91 double xMin = Double.POSITIVE_INFINITY;
92 double xMax = Double.NEGATIVE_INFINITY;
93 double yMin = Double.POSITIVE_INFINITY;
94 double yMax = Double.NEGATIVE_INFINITY;
95 int n = 0;
96 for (GeneratorLinkPosition linkPosition : position.positions)
97 {
98 for (GeneratorLanePosition lanePosition : linkPosition.positions)
99 {
100 this.allPositions.add(lanePosition);
101 for (DirectedLanePosition pos : lanePosition.getPosition())
102 {
103 DirectedPoint point;
104 try
105 {
106 point = pos.getLane().getCenterLine().getLocation(pos.getPosition());
107 }
108 catch (OTSGeometryException exception)
109 {
110 point = new DirectedPoint(0, 0, 0);
111 }
112 x += point.x;
113 y += point.y;
114 xMin = xMin < point.x ? xMin : point.x;
115 yMin = yMin < point.y ? yMin : point.y;
116 xMax = xMax > point.x ? xMax : point.x;
117 yMax = yMax > point.y ? yMax : point.y;
118 n++;
119 }
120 }
121 }
122 this.location = new DirectedPoint(x / n, y / n, 0);
123 this.bounds = new Bounds(new Point3d(xMin, yMin, 0.0), new Point3d(xMax, yMax, 0.0));
124 }
125
126
127
128
129
130
131
132
133 public static GeneratorPositions create(final Set<DirectedLanePosition> positions, final StreamInterface stream)
134 {
135 return create(positions, stream, null, null, null);
136 }
137
138
139
140
141
142
143
144
145
146 public static GeneratorPositions create(final Set<DirectedLanePosition> positions, final StreamInterface stream,
147 final LaneBiases biases)
148 {
149 return create(positions, stream, biases, null, null);
150 }
151
152
153
154
155
156
157
158
159
160
161 public static GeneratorPositions create(final Set<DirectedLanePosition> positions, final StreamInterface stream,
162 final Map<CrossSectionLink, Double> linkWeights, final Map<CrossSectionLink, Node> viaNodes)
163 {
164 return create(positions, stream, null, linkWeights, viaNodes);
165 }
166
167
168
169
170
171
172
173
174
175
176 public static GeneratorPositions create(final Set<DirectedLanePosition> positions, final StreamInterface stream,
177 final LaneBiases laneBiases, final Map<CrossSectionLink, Double> linkWeights,
178 final Map<CrossSectionLink, Node> viaNodes)
179 {
180
181
182 Map<LinkDirection, Set<DirectedLanePosition>> linkSplit = new LinkedHashMap<>();
183 for (DirectedLanePosition position : positions)
184 {
185 if (!linkSplit.containsKey(position.getLinkDirection()))
186 {
187 linkSplit.put(position.getLinkDirection(), new LinkedHashSet<>());
188 }
189 linkSplit.get(position.getLinkDirection()).add(position);
190 }
191
192
193 List<GeneratorLinkPosition> linkPositions = new ArrayList<>();
194 for (LinkDirection linkDirection : linkSplit.keySet())
195 {
196 List<Lane> lanes = ((CrossSectionLink) linkDirection.getLink()).getLanes();
197
198 Collections.sort(lanes, new Comparator<Lane>()
199 {
200
201 @Override
202 public int compare(final Lane lane1, final Lane lane2)
203 {
204 Length lat1 = linkDirection.getDirection().isPlus() ? lane1.getDesignLineOffsetAtBegin() : lane1
205 .getDesignLineOffsetAtEnd().neg();
206 Length lat2 = linkDirection.getDirection().isPlus() ? lane2.getDesignLineOffsetAtBegin() : lane2
207 .getDesignLineOffsetAtEnd().neg();
208 return lat1.compareTo(lat2);
209 }
210 });
211
212 List<GeneratorLanePosition> lanePositions = new ArrayList<>();
213 for (DirectedLanePosition lanePosition : linkSplit.get(linkDirection))
214 {
215 Set<DirectedLanePosition> set = new LinkedHashSet<>();
216 set.add(lanePosition);
217 lanePositions.add(new GeneratorLanePosition(lanes.indexOf(lanePosition.getLane()) + 1, set,
218 (CrossSectionLink) linkDirection.getLink()));
219 }
220
221 CrossSectionLink link = (CrossSectionLink) linkDirection.getLink();
222 if (linkWeights == null)
223 {
224 linkPositions.add(new GeneratorLinkPosition(lanePositions, link, stream, laneBiases));
225 }
226 else
227 {
228 Double weight = linkWeights.get(link);
229 Throw.whenNull(weight, "Using link weights for GTU generation, but no weight for link %s is defined.", link);
230 linkPositions.add(new GeneratorLinkPosition(lanePositions, link, stream, laneBiases, weight, viaNodes.get(
231 link)));
232 }
233 }
234
235
236 return new GeneratorPositions(new GeneratorZonePosition(linkPositions), laneBiases, stream);
237
238 }
239
240
241
242
243
244
245
246
247
248 public GeneratorLinkPosition draw(final GTUType gtuType, final Node destination, final Route route)
249 {
250 return this.position.draw(gtuType, this.stream, destination, route);
251 }
252
253
254 @Override
255 public DirectedPoint getLocation()
256 {
257 return this.location;
258 }
259
260
261 @Override
262 public Bounds getBounds() throws RemoteException
263 {
264 return this.bounds;
265 }
266
267
268
269
270
271 public Set<GeneratorLanePosition> getAllPositions()
272 {
273 return this.allPositions;
274 }
275
276
277
278
279
280
281 public Speed speedLimit(final GTUType gtuType)
282 {
283 Speed speedLimit = null;
284 for (GeneratorLanePosition pos : this.allPositions)
285 {
286 for (DirectedLanePosition lane : pos.getPosition())
287 {
288 try
289 {
290 Speed limit = lane.getLane().getSpeedLimit(gtuType);
291 if (speedLimit == null || limit.lt(speedLimit))
292 {
293 speedLimit = limit;
294 }
295 }
296 catch (NetworkException exception)
297 {
298
299 }
300 }
301 }
302 Throw.when(speedLimit == null, IllegalStateException.class, "No speed limit could be determined for GTUType %s.",
303 gtuType);
304 return speedLimit;
305 }
306
307
308
309
310
311
312
313
314
315
316
317
318
319 public static final class GeneratorLanePosition
320 {
321
322
323 private final int laneNumber;
324
325
326 private final Set<DirectedLanePosition> position;
327
328
329 private final CrossSectionLink link;
330
331
332
333
334
335
336
337 GeneratorLanePosition(final int laneNumber, final Set<DirectedLanePosition> position, final CrossSectionLink link)
338 {
339 this.laneNumber = laneNumber;
340 this.position = position;
341 this.link = link;
342 }
343
344
345
346
347
348 int getLaneNumber()
349 {
350 return this.laneNumber;
351 }
352
353
354
355
356
357
358 boolean allows(final GTUType gtuType)
359 {
360 for (DirectedLanePosition pos : this.position)
361 {
362 if (pos.getLane().getLaneType().isCompatible(gtuType, pos.getGtuDirection()))
363 {
364 return true;
365 }
366 }
367 return false;
368 }
369
370
371
372
373
374 Set<DirectedLanePosition> getPosition()
375 {
376 return this.position;
377 }
378
379
380
381
382
383 CrossSectionLink getLink()
384 {
385 return this.link;
386 }
387
388
389
390
391
392 GTUDirectionality getDirection()
393 {
394 return this.position.iterator().next().getGtuDirection();
395 }
396
397
398 @Override
399 public String toString()
400 {
401 return "GeneratorLanePosition [laneNumber=" + this.laneNumber + ", position=" + this.position + ", link="
402 + this.link + "]";
403 }
404
405 }
406
407
408
409
410
411
412
413
414
415
416
417
418
419 public static final class GeneratorLinkPosition
420 {
421
422
423 private final List<GeneratorLanePosition> positions;
424
425
426 private final CrossSectionLink link;
427
428
429 private final StreamInterface stream;
430
431
432 private final LaneBiases laneBiases;
433
434
435 private final double weight;
436
437
438 private final Node viaNode;
439
440
441
442
443
444
445
446
447 GeneratorLinkPosition(final List<GeneratorLanePosition> positions, final CrossSectionLink link,
448 final StreamInterface stream, final LaneBiases laneBiases)
449 {
450 this.positions = positions;
451 this.link = link;
452 this.stream = stream;
453 this.laneBiases = laneBiases;
454 this.weight = -1;
455 this.viaNode = null;
456 }
457
458
459
460
461
462
463
464
465
466
467 GeneratorLinkPosition(final List<GeneratorLanePosition> positions, final CrossSectionLink link,
468 final StreamInterface stream, final LaneBiases laneBiases, final double weight, final Node viaNode)
469 {
470 this.positions = positions;
471 this.link = link;
472 this.stream = stream;
473 this.laneBiases = laneBiases;
474 this.weight = weight;
475 this.viaNode = viaNode;
476 }
477
478
479
480
481
482 CrossSectionLink getLink()
483 {
484 return this.link;
485 }
486
487
488
489
490
491
492 double getWeight(final GTUType gtuType)
493 {
494 if (this.weight < 0.0)
495 {
496 return getNumberOfLanes(gtuType);
497 }
498 return this.weight;
499 }
500
501
502
503
504
505 Node getViaNode()
506 {
507 return this.viaNode;
508 }
509
510
511
512
513
514
515 int getNumberOfLanes(final GTUType gtuType)
516 {
517 int numberOfLanes = 0;
518 for (GeneratorLanePosition lanePosition : this.positions)
519 {
520 if (lanePosition.allows(gtuType))
521 {
522 numberOfLanes++;
523 }
524 }
525 return numberOfLanes;
526 }
527
528
529
530
531
532
533
534
535
536
537 GeneratorLanePosition draw(final GTUType gtuType, final Map<Integer, Integer> unplaced, final Speed desiredSpeed)
538 {
539 Map<GeneratorLanePosition, Double> map = new LinkedHashMap<>();
540 for (int i = 0; i < this.positions.size(); i++)
541 {
542 GeneratorLanePosition lanePosition = this.positions.get(i);
543 if (lanePosition.allows(gtuType))
544 {
545 GTUType type = gtuType;
546 boolean found = false;
547 while (this.laneBiases != null && !found && type != null)
548 {
549 if (this.laneBiases.contains(type))
550 {
551 found = true;
552 int laneNum = lanePosition.getLaneNumber();
553 int unplacedTemplates = unplaced == null ? 0 : unplaced.getOrDefault(laneNum, 0);
554 double w = this.laneBiases.getBias(type).calculateWeight(laneNum, getNumberOfLanes(gtuType),
555 unplacedTemplates, desiredSpeed);
556 map.put(lanePosition, w);
557 }
558 type = type.getParent();
559 }
560 if (!found)
561 {
562 map.put(lanePosition, 1.0);
563 }
564 }
565 }
566 if (0 == map.size())
567 {
568 System.err.println("This really, really can't work...");
569 }
570 return Draw.drawWeighted(map, this.stream);
571 }
572
573
574
575
576
577 GTUDirectionality getDirection()
578 {
579 return this.positions.get(0).getDirection();
580 }
581
582
583 @Override
584 public String toString()
585 {
586 return "GeneratorLinkPosition [positions=" + this.positions + "]";
587 }
588
589
590
591
592
593 public Speed speedLimit(final GTUType gtuType)
594 {
595 Speed speedLimit = null;
596 for (GeneratorLanePosition pos : this.positions)
597 {
598 for (DirectedLanePosition lane : pos.getPosition())
599 {
600 try
601 {
602 Speed limit = lane.getLane().getSpeedLimit(gtuType);
603 if (speedLimit == null || limit.lt(speedLimit))
604 {
605 speedLimit = limit;
606 }
607 }
608 catch (NetworkException exception)
609 {
610
611 }
612 }
613 }
614 Throw.when(speedLimit == null, IllegalStateException.class, "No speed limit could be determined for GTUType %s.",
615 gtuType);
616 return speedLimit;
617 }
618
619 }
620
621
622
623
624
625
626
627
628
629
630
631
632
633 private static final class GeneratorZonePosition
634 {
635
636
637 private final List<GeneratorLinkPosition> positions;
638
639
640
641
642
643 GeneratorZonePosition(final List<GeneratorLinkPosition> positions)
644 {
645 this.positions = positions;
646 }
647
648
649
650
651
652
653
654
655
656
657
658 GeneratorLinkPosition draw(final GTUType gtuType, final StreamInterface stream, final Node destination,
659 final Route route)
660 {
661 Map<GeneratorLinkPosition, Double> map = new LinkedHashMap<>();
662 for (int i = 0; i < this.positions.size(); i++)
663 {
664 GeneratorLinkPosition glp = this.positions.get(i);
665 Link link = glp.getLink();
666 GTUDirectionality direction = glp.getDirection();
667 if (route != null)
668 {
669 int from = route.indexOf(direction.isPlus() ? link.getStartNode() : link.getEndNode());
670 int to = route.indexOf(direction.isPlus() ? link.getEndNode() : link.getStartNode());
671 if (from > -1 && to > -1 && to - from == 1)
672 {
673 map.put(glp, glp.getWeight(gtuType));
674 }
675 }
676 else
677 {
678
679 if (glp.getViaNode() != null)
680 {
681
682 Route r = RouteGeneratorOD.getDefaultRouteSupplier(stream).getRoute(glp.getViaNode(), destination,
683 gtuType);
684 if (r != null)
685 {
686 map.put(glp, glp.getWeight(gtuType));
687 }
688 }
689 else
690 {
691 map.put(glp, glp.getWeight(gtuType));
692 }
693 }
694 }
695 return Draw.drawWeighted(map, stream);
696 }
697
698
699 @Override
700 public String toString()
701 {
702 return "GeneratorZonePosition [positions=" + this.positions + "]";
703 }
704
705 }
706
707
708
709
710
711
712
713
714
715
716
717
718
719 public static final class LaneBiases
720 {
721
722
723 private final Map<GTUType, LaneBias> biases = new LinkedHashMap<>();
724
725
726
727
728
729
730
731 public LaneBiases addBias(final GTUType gtuType, final LaneBias bias)
732 {
733 Throw.whenNull(gtuType, "GTU type may not be null.");
734 Throw.whenNull(bias, "Bias may not be null.");
735 this.biases.put(gtuType, bias);
736 return this;
737 }
738
739
740
741
742
743
744 public boolean contains(final GTUType gtuType)
745 {
746 return this.biases.containsKey(gtuType);
747 }
748
749
750
751
752
753
754 public LaneBias getBias(final GTUType gtuType)
755 {
756 return this.biases.getOrDefault(gtuType, LaneBias.NONE);
757 }
758
759
760 @Override
761 public String toString()
762 {
763 return "LaneBiases [" + this.biases + "]";
764 }
765
766 }
767
768
769
770
771
772
773
774
775
776
777
778
779
780 public static final class LaneBiasDefaults
781 {
782
783 private final EnumMap<GTUType.DEFAULTS, LaneBias> biases = new EnumMap<>(GTUType.DEFAULTS.class);
784
785
786
787
788
789
790
791 public LaneBiasDefaults addBias(final GTUType.DEFAULTS gtuEnum, final LaneBias bias)
792 {
793 Throw.whenNull(gtuEnum, "GTU type enum may not be null.");
794 Throw.whenNull(bias, "Bias may not be null.");
795 this.biases.put(gtuEnum, bias);
796 return this;
797 }
798
799
800
801
802
803
804 public boolean contains(final GTUType.DEFAULTS gtuEnum)
805 {
806 return this.biases.containsKey(gtuEnum);
807 }
808
809
810
811
812
813
814 public LaneBias getBias(final GTUType.DEFAULTS gtuEnum)
815 {
816 return this.biases.getOrDefault(gtuEnum, LaneBias.NONE);
817 }
818
819
820 @Override
821 public String toString()
822 {
823 return "LaneBiases [" + this.biases + "]";
824 }
825 }
826
827
828
829
830
831
832
833
834
835
836
837
838
839 public static final class LaneBias
840 {
841
842
843 public static final LaneBias NONE = new LaneBias(new ByValue(0.0), 0.0, Integer.MAX_VALUE);
844
845
846 public static final LaneBias WEAK_LEFT = new LaneBias(new ByValue(1.0), 1.0, Integer.MAX_VALUE);
847
848
849 public static final LaneBias LEFT = new LaneBias(new ByValue(1.0), 2.0, Integer.MAX_VALUE);
850
851
852 public static final LaneBias STRONG_LEFT = new LaneBias(new ByValue(1.0), 5.0, Integer.MAX_VALUE);
853
854
855 public static final LaneBias WEAK_MIDDLE = new LaneBias(new ByValue(0.5), 1.0, Integer.MAX_VALUE);
856
857
858 public static final LaneBias MIDDLE = new LaneBias(new ByValue(0.5), 2.0, Integer.MAX_VALUE);
859
860
861 public static final LaneBias STRONG_MIDDLE = new LaneBias(new ByValue(0.5), 5.0, Integer.MAX_VALUE);
862
863
864 public static final LaneBias WEAK_RIGHT = new LaneBias(new ByValue(0.0), 1.0, Integer.MAX_VALUE);
865
866
867 public static final LaneBias RIGHT = new LaneBias(new ByValue(0.0), 2.0, Integer.MAX_VALUE);
868
869
870 public static final LaneBias STRONG_RIGHT = new LaneBias(new ByValue(0.0), 5.0, Integer.MAX_VALUE);
871
872
873 public static final LaneBias TRUCK_RIGHT = new LaneBias(new ByValue(0.0), 5.0, 2);
874
875
876
877
878
879
880
881 public static LaneBias bySpeed(final Speed leftSpeed, final Speed rightSpeed)
882 {
883 return new LaneBias(new BySpeed(leftSpeed, rightSpeed), 2.0, Integer.MAX_VALUE);
884 }
885
886
887
888
889
890
891
892 public static LaneBias bySpeed(final double leftSpeedKm, final double rightSpeedKm)
893 {
894 return bySpeed(new Speed(leftSpeedKm, SpeedUnit.KM_PER_HOUR), new Speed(rightSpeedKm, SpeedUnit.KM_PER_HOUR));
895 }
896
897
898 private final RoadPosition roadPosition;
899
900
901 private final double bias;
902
903
904 private final double stickyLanes;
905
906
907
908
909
910
911
912 public LaneBias(final RoadPosition roadPosition, final double bias, final double stickyLanes)
913 {
914 Throw.when(bias < 0.0, IllegalArgumentException.class, "Bias should be positive or 0.");
915 Throw.when(stickyLanes < 1.0, IllegalArgumentException.class, "Sticky lanes should be 1.0 or larger.");
916 this.roadPosition = roadPosition;
917 this.bias = bias;
918 this.stickyLanes = stickyLanes;
919 }
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955 public double calculateWeight(final int laneNumFromRight, final int numberOfLanes, final int numberOfUnplacedGTUs,
956 final Speed desiredSpeed)
957 {
958 double d = Math.abs((1.0 + this.roadPosition.getValue(desiredSpeed) * (numberOfLanes - 1.0)) - laneNumFromRight);
959 if (d >= this.stickyLanes)
960 {
961 return 0.0;
962 }
963 return 1.0 / (Math.pow(d + 1.0, this.bias) * (numberOfUnplacedGTUs + 1.0));
964 }
965
966
967 @Override
968 public int hashCode()
969 {
970 final int prime = 31;
971 int result = 1;
972 long temp;
973 temp = Double.doubleToLongBits(this.bias);
974 result = prime * result + (int) (temp ^ (temp >>> 32));
975 result = prime * result + ((this.roadPosition == null) ? 0 : this.roadPosition.hashCode());
976 temp = Double.doubleToLongBits(this.stickyLanes);
977 result = prime * result + (int) (temp ^ (temp >>> 32));
978 return result;
979 }
980
981
982 @Override
983 public boolean equals(final Object obj)
984 {
985 if (this == obj)
986 {
987 return true;
988 }
989 if (obj == null)
990 {
991 return false;
992 }
993 if (getClass() != obj.getClass())
994 {
995 return false;
996 }
997 LaneBias other = (LaneBias) obj;
998 if (Double.doubleToLongBits(this.bias) != Double.doubleToLongBits(other.bias))
999 {
1000 return false;
1001 }
1002 if (this.roadPosition == null)
1003 {
1004 if (other.roadPosition != null)
1005 {
1006 return false;
1007 }
1008 }
1009 else if (!this.roadPosition.equals(other.roadPosition))
1010 {
1011 return false;
1012 }
1013 if (Double.doubleToLongBits(this.stickyLanes) != Double.doubleToLongBits(other.stickyLanes))
1014 {
1015 return false;
1016 }
1017 return true;
1018 }
1019
1020
1021 @Override
1022 public String toString()
1023 {
1024 return "Bias [roadPosition=" + this.roadPosition + ", bias=" + this.bias + ", stickyLanes=" + this.stickyLanes
1025 + "]";
1026 }
1027
1028 }
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042 public interface RoadPosition
1043 {
1044
1045
1046
1047
1048
1049
1050 double getValue(Speed desiredSpeed);
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064 public class ByValue implements RoadPosition
1065 {
1066
1067
1068 private double value;
1069
1070
1071
1072
1073
1074 public ByValue(final double value)
1075 {
1076 Throw.when(value < 0.0 || value > 1.0, IllegalArgumentException.class,
1077 "Road position value should be in the range [0...1].");
1078 this.value = value;
1079 }
1080
1081
1082 @Override
1083 public double getValue(final Speed desiredSpeed)
1084 {
1085 return this.value;
1086 }
1087
1088
1089 @Override
1090 public int hashCode()
1091 {
1092 final int prime = 31;
1093 int result = 1;
1094 long temp;
1095 temp = Double.doubleToLongBits(this.value);
1096 result = prime * result + (int) (temp ^ (temp >>> 32));
1097 return result;
1098 }
1099
1100
1101 @Override
1102 public boolean equals(final Object obj)
1103 {
1104 if (this == obj)
1105 {
1106 return true;
1107 }
1108 if (obj == null)
1109 {
1110 return false;
1111 }
1112 if (getClass() != obj.getClass())
1113 {
1114 return false;
1115 }
1116 ByValue other = (ByValue) obj;
1117 if (Double.doubleToLongBits(this.value) != Double.doubleToLongBits(other.value))
1118 {
1119 return false;
1120 }
1121 return true;
1122 }
1123
1124 }
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 class BySpeed implements RoadPosition
1139 {
1140
1141
1142 private Speed leftSpeed;
1143
1144
1145 private Speed rightSpeed;
1146
1147
1148
1149
1150
1151
1152 public BySpeed(final Speed leftSpeed, final Speed rightSpeed)
1153 {
1154 Throw.when(leftSpeed.eq(rightSpeed), IllegalArgumentException.class,
1155 "Left speed and right speed may not be equal. Use LaneBias.NONE.");
1156 this.leftSpeed = leftSpeed;
1157 this.rightSpeed = rightSpeed;
1158 }
1159
1160
1161 @Override
1162 public double getValue(final Speed desiredSpeed)
1163 {
1164 Throw.whenNull(desiredSpeed,
1165 "Peeked desired speed from a strategical planner factory is null, while a lane bias depends on desired speed.");
1166 double value = (desiredSpeed.si - this.rightSpeed.si) / (this.leftSpeed.si - this.rightSpeed.si);
1167 return value < 0.0 ? 0.0 : (value > 1.0 ? 1.0 : value);
1168 }
1169
1170
1171 @Override
1172 public int hashCode()
1173 {
1174 final int prime = 31;
1175 int result = 1;
1176 result = prime * result + ((this.leftSpeed == null) ? 0 : this.leftSpeed.hashCode());
1177 result = prime * result + ((this.rightSpeed == null) ? 0 : this.rightSpeed.hashCode());
1178 return result;
1179 }
1180
1181
1182 @Override
1183 public boolean equals(final Object obj)
1184 {
1185 if (this == obj)
1186 {
1187 return true;
1188 }
1189 if (obj == null)
1190 {
1191 return false;
1192 }
1193 if (getClass() != obj.getClass())
1194 {
1195 return false;
1196 }
1197 BySpeed other = (BySpeed) obj;
1198 if (this.leftSpeed == null)
1199 {
1200 if (other.leftSpeed != null)
1201 {
1202 return false;
1203 }
1204 }
1205 else if (!this.leftSpeed.equals(other.leftSpeed))
1206 {
1207 return false;
1208 }
1209 if (this.rightSpeed == null)
1210 {
1211 if (other.rightSpeed != null)
1212 {
1213 return false;
1214 }
1215 }
1216 else if (!this.rightSpeed.equals(other.rightSpeed))
1217 {
1218 return false;
1219 }
1220 return true;
1221 }
1222
1223 }
1224
1225 }
1226
1227 }