Debian Codegraph @

src/uk/me/parabola/imgfmt/app/net/RouteRestriction.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2008
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * Create date: 07-Jul-2008
14 */
15 package uk.me.parabola.imgfmt.app.net;
16
17 import java.util.ArrayList;
18
19 import java.util.List;
20
21 import uk.me.parabola.imgfmt.app.ImgFileWriter;
22 import static uk.me.parabola.imgfmt.app.net.AccessTagsAndBits.*;
23
24
25 /**
26 * A restriction in the routing graph.
27 *
28 * A routing restriction has two or more arcs.
29 * The first arc is the "from" arc, the last is the "to" arc,
30 * and other arc is a "via" arc.
31 *
32 * A from-to-via restriction says you can't go along arc "to"
33 * if you came to node to.getSource() == from.getSource()
34 * via the inverse arc of "from". We're using the inverse of
35 * "from" since that has the information we need for writing
36 * the Table C entry.
37 *
38 * @author Robert Vollmert
39 */
40 public class RouteRestriction {
41 // first three bytes of the header -- might specify the type of restriction
42 // and when it is active
43 private static final byte RESTRICTION_TYPE = 0x05; // 0x07 spotted, meaning?
44
45 // To specify that a node is given by a relative offset instead
46 // of an entry to Table B.
47 private static final int F_INTERNAL = 0x8000;
48
49 // the arcs
50 private final List<RouteArc> arcs;
51
52 private final RouteNode viaNode;
53 // offset in Table C
54 private int offsetSize;
55 private int offsetC;
56
57 // last restriction in a node
58 private boolean last;
59
60 // mask that specifies which vehicle types the restriction doesn't apply to
61 private final byte exceptMask;
62 private final byte flags; // meaning of bits 0x01 and 0x10 are not clear
63
64 private static final byte F_EXCEPT_FOOT = 0x02;
65 private static final byte F_EXCEPT_EMERGENCY = 0x04;
66 private static final byte F_MORE_EXCEPTIONS = 0x08;
67
68 private static final byte EXCEPT_CAR = 0x01;
69 private static final byte EXCEPT_BUS = 0x02;
70 private static final byte EXCEPT_TAXI = 0x04;
71 private static final byte EXCEPT_DELIVERY = 0x10;
72 private static final byte EXCEPT_BICYCLE = 0x20;
73 private static final byte EXCEPT_TRUCK = 0x40;
74
75 /**
76 *
77 * @param viaNode the node to which this restriction is related
78 * @param traffArcs the arcs that describe the "forbidden" path
79 * @param mkgmapExceptMask the exception mask in the mkgmap format
80 */
81 public RouteRestriction(RouteNode viaNode, List<RouteArc> traffArcs, byte mkgmapExceptMask) {
82 this.viaNode = viaNode;
83 this.arcs = new ArrayList<>(traffArcs);
84 for (int i = 0; i < arcs.size(); i++){
85 RouteArc arc = arcs.get(i);
86 assert arc.getDest() != viaNode;
87 }
88 byte tmpFlags = 0;
89
90 if ((mkgmapExceptMask & FOOT) != 0)
91 tmpFlags |= F_EXCEPT_FOOT;
92 if ((mkgmapExceptMask & EMERGENCY) != 0)
93 tmpFlags |= F_EXCEPT_EMERGENCY;
94
95 exceptMask = translateExceptMask(mkgmapExceptMask);
96 if(exceptMask != 0)
97 tmpFlags |= F_MORE_EXCEPTIONS;
98
99 int numArcs = arcs.size();
100 assert numArcs < 8;
101 tmpFlags |= ((numArcs) << 5);
102 this.flags = tmpFlags;
103 }
104
105
106 /**
107 * Translate the mkgmap internal representation of vehicles to the one used in the img format
108 * @param mkgmapExceptMask
109 * @return
110 */
111 private static byte translateExceptMask(byte mkgmapExceptMask) {
112 byte mask = 0;
113 if ((mkgmapExceptMask & CAR) != 0)
114 mask |= EXCEPT_CAR;
115 if ((mkgmapExceptMask & BUS) != 0)
116 mask |= EXCEPT_BUS;
117 if ((mkgmapExceptMask & TAXI) != 0)
118 mask |= EXCEPT_TAXI;
119 if ((mkgmapExceptMask & DELIVERY) != 0)
120 mask |= EXCEPT_DELIVERY;
121 if ((mkgmapExceptMask & BIKE) != 0)
122 mask |= EXCEPT_BICYCLE;
123 if ((mkgmapExceptMask & TRUCK) != 0)
124 mask |= EXCEPT_TRUCK;
125 return mask;
126 }
127
128
129 private static int calcOffset(RouteNode node, int tableOffset) {
130 int offset = tableOffset - node.getOffsetNod1();
131 assert offset >= 0 : "node behind start of tables";
132 assert offset < 0x8000 : "node offset too large";
133 return offset | F_INTERNAL;
134 }
135
136 public List<RouteArc> getArcs(){
137 return arcs;
138 }
139
140 /**
141 * Writes a Table C entry with 3 or more nodes.
142 *
143 * @param writer The writer.
144 * @param tableOffset The offset in NOD 1 of the tables area.
145 *
146 */
147 public void write(ImgFileWriter writer, int tableOffset) {
148 writer.put1u(RESTRICTION_TYPE);
149
150 writer.put(flags);
151 writer.put1u(0); // meaning ?
152
153 if(exceptMask != 0)
154 writer.put1u(exceptMask);
155
156 int numArcs = arcs.size();
157 int[] offsets = new int[numArcs+1];
158 int pos = 0;
159 boolean viaWritten = false;
160 for (int i = 0; i < numArcs; i++){
161 RouteArc arc = arcs.get(i);
162 // the arcs must have a specific order and direction
163 // first arc: dest is from node , last arc: dest is to node
164 // if there only two arcs, both will have the via node as source node.
165 // For more n via nodes, the order is like this:
166 // from <- via(1) <- via(2) <- ... <- this via node -> via( n-1) -> via(n) -> to
167 if (arc.isInternal())
168 offsets[pos++] = calcOffset(arc.getDest(), tableOffset);
169 else
170 offsets[pos++] = arc.getIndexB();
171 if (!viaWritten && arc.getSource() == viaNode) {
172 // there will be two nodes with source node = viaNode, but we write the source only once
173 offsets[pos++] = calcOffset(viaNode, tableOffset);
174 viaWritten = true;
175 }
176 }
177
178 for (int offset : offsets)
179 writer.put2u(offset);
180
181 for (RouteArc arc: arcs)
182 writer.put1u(arc.getIndexA());
183 }
184
185 /**
186 * Write this restriction's offset within Table C into a node record.
187 */
188 public void writeOffset(ImgFileWriter writer) {
189 assert 0 < offsetSize && offsetSize <= 2 : "illegal offset size";
190 int offset = offsetC;
191 if (offsetSize == 1) {
192 assert offset < 0x80;
193 if (last)
194 offset |= 0x80;
195 writer.put1u(offset);
196 } else {
197 assert offset < 0x8000;
198 if (last)
199 offset |= 0x8000;
200 writer.put2u(offset);
201 }
202 }
203
204 /**
205 * Size in bytes of the Table C entry.
206 */
207 public int getSize() {
208 int size = 3; // header length
209 if(exceptMask != 0)
210 ++size;
211 size += arcs.size() + (arcs.size()+1) * 2;
212 return size;
213 }
214
215 public void setOffsetC(int offsetC) {
216 this.offsetC = offsetC;
217 }
218
219 public int getOffsetC() {
220 return offsetC;
221 }
222
223 public void setOffsetSize(int size) {
224 offsetSize = size;
225 }
226
227 public void setLast() {
228 last = true;
229 }
230 }