Skip to content

Commit 7f85ee7

Browse files
author
Peter
committed
Merge pull request graphhopper#134 from khuebner/master
Preparing support of turn restrictions (2): Encode turn costs depending on vehicle type
2 parents 2007a9c + df78b04 commit 7f85ee7

File tree

6 files changed

+428
-44
lines changed

6 files changed

+428
-44
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.graphhopper.routing.util;
2+
3+
/**
4+
* Very simple turn cost encoder, which stores the turn restriction in the first bit,
5+
* and the turn costs (in seconds) in x additional bits (0..2^x-1)
6+
*
7+
* @author karl.huebner
8+
*/
9+
public abstract class AbstractTurnCostEncoder implements TurnCostEncoder
10+
{
11+
private final int maxCostsBits;
12+
private final int costsMask;
13+
14+
protected int restrictionBit;
15+
protected int costShift;
16+
17+
public AbstractTurnCostEncoder( int maxCostsBits )
18+
{
19+
this.maxCostsBits = maxCostsBits;
20+
21+
int mask = 0;
22+
for (int i = 0; i < this.maxCostsBits; i++)
23+
{
24+
mask |= (1 << i);
25+
}
26+
this.costsMask = mask;
27+
28+
defineBits(0, 0);
29+
}
30+
31+
@Override
32+
public int defineBits( int index, int shift )
33+
{
34+
restrictionBit = 1 << shift;
35+
costShift = shift + 1;
36+
return shift + maxCostsBits + 1;
37+
}
38+
39+
@Override
40+
public boolean isRestricted( int flag )
41+
{
42+
return (flag & restrictionBit) != 0;
43+
}
44+
45+
@Override
46+
public int getCosts( int flag )
47+
{
48+
int result = (flag >> costShift) & costsMask;
49+
if (result >= Math.pow(2, maxCostsBits) || result < 0)
50+
{
51+
throw new IllegalStateException("Wrong encoding of turn costs");
52+
}
53+
return result;
54+
}
55+
56+
@Override
57+
public int flags( boolean restricted, int costs )
58+
{
59+
costs = Math.min(costs, (int) (Math.pow(2, maxCostsBits) - 1));
60+
int encode = costs << costShift;
61+
if (restricted)
62+
{
63+
encode |= restrictionBit;
64+
}
65+
return encode;
66+
}
67+
68+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.graphhopper.routing.util;
2+
3+
/**
4+
* Default implementation of {@link AbstractTurnCostEncoder} which
5+
* does not encode any costs yet, but only restrictions in one bit.
6+
* Therefore, each turn cost encoder requires only 1 bit storage size
7+
* at the moment.
8+
*
9+
* @author karl.huebner
10+
*/
11+
public class DefaultTurnCostEncoder extends AbstractTurnCostEncoder
12+
{
13+
/**
14+
* no costs, but only restrictions will be encoded
15+
*/
16+
public DefaultTurnCostEncoder()
17+
{
18+
this(0); //we don't need costs yet
19+
}
20+
21+
/**
22+
* Next to restrictions, turn costs will be encoded as well
23+
*
24+
* @param maxCosts the maximum costs to be encoded by this encoder, everything above this costs will be encoded as maxCosts
25+
*/
26+
public DefaultTurnCostEncoder( int maxCosts )
27+
{
28+
super(determineRequiredBits(maxCosts)); //determine the number of bits required to store maxCosts
29+
}
30+
31+
private static int determineRequiredBits( int number )
32+
{
33+
int bits = 0;
34+
while (number > 0)
35+
{
36+
number = number >> 1;
37+
bits++;
38+
}
39+
return bits;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)