1 package org.opentrafficsim.kpi.sampling;
2
3 import java.io.Serializable;
4 import java.util.Iterator;
5 import java.util.LinkedHashSet;
6 import java.util.Set;
7
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.djutils.exceptions.Throw;
10 import org.djutils.immutablecollections.ImmutableIterator;
11 import org.opentrafficsim.kpi.interfaces.LaneData;
12 import org.opentrafficsim.kpi.interfaces.LinkData;
13 import org.opentrafficsim.kpi.sampling.CrossSection.LanePosition;
14
15
16
17
18
19
20
21
22
23
24
25
26 public class CrossSection implements Serializable, Iterable<LanePosition>
27 {
28
29
30 private static final long serialVersionUID = 20160929L;
31
32
33 private final Set<LanePosition> lanePositions;
34
35
36
37
38
39 public CrossSection(final Set<LanePosition> lanePositions)
40 {
41 Throw.whenNull(lanePositions, "Lane positions may not be null.");
42 this.lanePositions = new LinkedHashSet<>(lanePositions);
43 }
44
45
46
47
48
49
50
51
52 public CrossSection(final LinkData<?> link, final double fraction)
53 {
54 Throw.whenNull(link, "Link lane positions may not be null.");
55 this.lanePositions = new LinkedHashSet<>();
56 for (LaneData<?> lane : link.getLanes())
57 {
58 LanePosition lanePosition = new LanePosition(lane, lane.getLength().times(fraction));
59 this.lanePositions.add(lanePosition);
60 }
61 }
62
63
64
65
66
67 public final int size()
68 {
69 return this.lanePositions.size();
70 }
71
72
73
74
75
76 public final Set<LanePosition> getLanePositions()
77 {
78 return new LinkedHashSet<>(this.lanePositions);
79 }
80
81
82
83
84
85 @Override
86 public final Iterator<LanePosition> iterator()
87 {
88 return new ImmutableIterator<>(this.lanePositions.iterator());
89 }
90
91 @Override
92 public String toString()
93 {
94 return "CrossSection [lanePositions=" + this.lanePositions + "]";
95 }
96
97
98
99
100
101
102 @SuppressWarnings("javadoc")
103 public record LanePosition(LaneData<?> lane, Length position)
104 {
105
106
107
108 public LanePosition
109 {
110 Throw.whenNull(lane, "lane is null");
111 Throw.whenNull(position, "position is null");
112 }
113 }
114
115 }