-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDetector.java
More file actions
93 lines (80 loc) · 2.23 KB
/
Detector.java
File metadata and controls
93 lines (80 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright 2018 David Prentiss
*/
package sim.app.agentcity;
import sim.util.*;
import sim.engine.*;
import java.util.Arrays;
public class Detector implements Steppable {
// Required for serialization
private static final long serialVersionUID = 1;
// Stopper
Stoppable stopper;
// Properties
public final int idNum;
public final int minX;
public final int maxX;
public final int minY;
public final int maxY;
// Variables
AgentCity ac;
Direction direction;
long step;
int stepIdx;
int nextIdx;
Vehicle vehicle;
int bufferSize;
Bag b = new Bag();
Bag vehicles = new Bag();
Bag buffer = new Bag();
private int width;
private int height;
private Int2D[] cells;
// Accessors
/** Constructor */
public Detector(int id, int minX, int maxX, int minY, int maxY) {
idNum = id;
this.minX = minX;
this.maxX = maxX;
this.minY = minY;
this.maxY = maxY;
this.width = maxX - minX + 1;
this.height = maxY - minY + 1;
cells = new Int2D[this.width * this.height];
int i = 0;
for (int j = 0; j < this.width; j++) {
for (int k = 0; k < this.height; k++) {
cells[i] = new Int2D(j + minX, k + minY);
i++;
}
}
}
/** Constructor */
/*
public Detector(int id, Int2D cell, int numLanes) {
direction = Direction.byInt(ac.roadGrid.get(cell.x, cell.y));
}
*/
public void step(final SimState state) {
ac = (AgentCity)state;
for (int i = 0; i < cells.length; i++) {
b = ac.agentGrid.getObjectsAtLocation(cells[i]);
if (b == null) { continue; }
vehicle = (Vehicle)b.objs[0];
buffer.add(vehicle);
if (vehicles.contains(vehicle)) continue;
vehicles.add(vehicle);
System.out.print(vehicle);
}
System.out.println(vehicles.numObjs);
int j = 0;
while (j < vehicles.numObjs) {
if (buffer.contains(vehicles.objs[j])) {
j++;
} else {
vehicles.remove(vehicles.objs[j]);
}
}
buffer.clear();
}
}