source: proiecte/ptvs/src/vnsim/vehicular/simulator/intersections/IntersectionWithTrafficLights.java @ 31

Last change on this file since 31 was 31, checked in by (none), 14 years ago
File size: 5.2 KB
Line 
1/************************************************************************************
2 * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University
3 * All rights reserved.
4 * Refer to LICENSE for terms and conditions of use.
5 ***********************************************************************************/
6package vnsim.vehicular.simulator.intersections;
7
8import java.io.File;
9import java.util.ArrayList;
10
11import vnsim.map.object.Globals;
12import vnsim.vehicular.simulator.*;
13
14public class IntersectionWithTrafficLights extends Intersection {
15
16        /** <code>serialVersionUID</code> */
17        private static final long serialVersionUID = -5883719321862303634L;
18
19        // indices corresponding with the "segments" Vector
20        // public ArrayList<TrafficLightInfo> lightInfos;
21        public ArrayList<Integer> currentColor; // 1=RED, 2=YELLOW, 3=GREEN
22
23        public int lastCycleStartTime = 0;
24        public int cycleLength;
25
26        public void addCrossingSegment(DirectedRoadSegment drs) {
27                if (segments == null) {
28                        segments = new ArrayList<DirectedRoadSegment>();
29                        currentColor = new ArrayList<Integer>();
30                }
31                if (!segments.contains(drs)) {
32                        segments.add(drs);
33                        currentColor.add(0);
34                }
35        }
36
37        // public void addTrafficLightInfo(TrafficLightInfo info) {
38        // if(lightInfos==null) {
39        // lightInfos=new ArrayList<TrafficLightInfo> ();
40        // }
41        // lightInfos.add(info);
42        // }
43
44        public synchronized boolean isGreen(RouteSegment src) {
45                for (int i = 0; i < segments.size(); i++) {
46                        if (segments.get(i).isSameSegment(src)) {
47                                if (currentColor.get(i).intValue() == 3)
48                                        return true;
49                                else
50                                        return false;
51                        }
52                }
53                System.err.println("ERROR! isGreen called, but segment not found in intersection!");
54                System.err.println("->"+src.roadIndex+" "+src.pt1+" "+src.pt2);
55                for (int i = 0; i < segments.size(); i++) {
56                        System.err.println("\t->"+segments.get(i).roadIndex+" "+segments.get(i).pointIndex);   
57                }
58                return false;
59        }
60
61        public synchronized boolean isGreen(int k) {
62                if (currentColor.get(k).intValue() == 3)
63                        return true;
64                return false;
65        }
66
67        public synchronized boolean isRed(int k) {
68                if (currentColor.get(k).intValue() == 1)
69                        return true;
70                return false;
71        }
72
73        public synchronized boolean isYellow(RouteSegment src) {
74                for (int i = 0; i < segments.size(); i++) {
75                        if (segments.get(i).isSameSegment(src)) {
76                                if (currentColor.get(i).intValue() == 2)
77                                        return true;
78                                else
79                                        return false;
80                        }
81                }
82                System.out.println("ERROR! isYellow called, but segment not found in intersection!");
83                return false;
84        }
85
86        public boolean mustStop(RouteSegment src, RouteSegment dst) {
87                return false;
88        }
89
90        public boolean havePriority(RouteSegment seg1, RouteSegment seg2) {
91                return false;
92        }
93
94        public synchronized ArrayList<DirectedRoadSegment> yieldFor(RouteSegment seg1, RouteSegment seg2) {
95                // a vehicle coming on "seg1", going on "seg2"
96                // must yield to vehicles coming on which segments?
97                ArrayList<DirectedRoadSegment> ret = new ArrayList<DirectedRoadSegment>();
98                int i;
99                for (i = 0; i < segments.size(); i++) {
100                        if (segments.get(i).isSameSegment(seg1))
101                                break;
102                }
103                int j;
104                for (j = 0; j < segments.size(); j++) {
105                        if (segments.get(j).isSameSegment(seg2))
106                                break;
107                }
108                if (i == segments.size() || j == segments.size())
109                        System.out.println("*** ERROR! yieldFor - wrong parameters!");
110                for (int k = 0; k < segments.size(); k++) {
111                        if (k == i || k == j)
112                                continue;
113                        if (isOrder(i, k, j) && isGreen(k)) {
114                                ret.add(segments.get(k));
115                        }
116                }
117                return ret;
118        }
119
120        public boolean isLeftTurn(RouteSegment seg1, RouteSegment seg2) {
121                DirectedRoadSegment s1 = null, s2 = null;
122                int i;
123                for (i = 0; i < segments.size(); i++) {
124                        if (segments.get(i).isSameSegment(seg1)) {
125                                s1 = segments.get(i);
126                                break;
127                        }
128                }
129                int j;
130                for (j = 0; j < segments.size(); j++) {
131                        if (segments.get(j).isSameSegment(seg2)) {
132                                s2 = segments.get(j);
133                                break;
134                        }
135                }
136                if (s1 != null && s1.left != null && s1.left.equals(s2))
137                        return true;
138                return false;
139        }
140
141        // returns the maximum allowed speed when the light is green
142        public double getMaximumSpeed(RouteSegment seg1, RouteSegment seg2) {
143                return 20.0;
144        }
145
146        public double getInfluenceDistance(RouteSegment seg1, RouteSegment seg2) {
147                return 0.100;
148        }
149
150        public double getInfluenceDistanceLaneChange(RouteSegment seg1, RouteSegment seg2) {
151                return 0.200;
152        }
153
154        public boolean hasTrafficLights() {
155                return true;
156        }
157
158        public String toString() {
159                String ret = "";
160                ret = ret + segments + " - " + currentColor;
161                return ret;
162        }
163
164        public void step(int crtTime) {
165
166                int thisSec = (int) (crtTime / (Globals.executionFPS));
167                int prevSec = (int) ((crtTime - 1) / (Globals.executionFPS));
168                int aux = thisSec % this.cycleLength;
169                int prev = prevSec % this.cycleLength;
170
171                synchronized (this) {
172                        this.currentColor = new ArrayList<Integer>();
173                        for (int k = 0; k < this.segments.size(); k++) {
174                                TrafficLightInfo info = segments.get(k).lightInfo;
175                                int color = info.getColor(aux);
176                                this.currentColor.add(new Integer(color));
177                                if (info.getColor(prev) != color) {
178                                        segments.get(k).changePhase(null, crtTime, color);
179                                }
180                        }
181                }
182        }
183
184}
Note: See TracBrowser for help on using the repository browser.