| | | 1 | | namespace Itinero.Instructions.Types; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// A base instruction. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// An instruction is a piece that describes a traveller how they should behave to reach their destination. |
| | | 8 | | /// An instruction applies on one or more segments, and contains two parts: |
| | | 9 | | /// - The next meters to travel, e.g. 'follow along road XYZ, which has properties P and Q' |
| | | 10 | | /// - What to do at the end of the segment, e.g. 'turn left' or even 'follow along then next road' |
| | | 11 | | /// An instruction thus can be |
| | | 12 | | /// - 'Follow along road XYZ, then turn right' |
| | | 13 | | /// - 'Follow road XYZ, then cross street ABC' |
| | | 14 | | /// - 'Follow road XYZ, then get onto the roundabout' |
| | | 15 | | /// - 'Take the Nth exit of the roundabout, this is ~straight on' (note that this instruction possibly spans multipl |
| | | 16 | | /// segments of the roundabout) |
| | | 17 | | /// A client which outputs actual text for humans, might even need a little bit more of lookahead in the instruction |
| | | 18 | | /// in order to generate stuff as: |
| | | 19 | | /// - 'Follow road XYZ, then cross the street into DEF'. |
| | | 20 | | /// This is _not_ handled here |
| | | 21 | | /// This means we have two things to track: |
| | | 22 | | /// - The properties of the road segment we are travelling |
| | | 23 | | /// - The next maneuver to make |
| | | 24 | | /// Subclasses of Instruction might add additional information, such as 'cross XYZ at the end', 'you are currently |
| | | 25 | | /// travelling a roundabout', ... |
| | | 26 | | /// Note that we should also consider the multiple presentations of the instructions: |
| | | 27 | | /// - Voice instructions, which should be spoken at the right time before the traveller has to make a maneuver |
| | | 28 | | /// - Written instructions, on app, which should show the appropriate text at the right time |
| | | 29 | | /// - Extra information, e.g. about the street currently travelled, the next street, the upcoming turn, ... |
| | | 30 | | /// </remarks> |
| | | 31 | | public class BaseInstruction |
| | | 32 | | { |
| | | 33 | | /// <summary> |
| | | 34 | | /// Constructs a BaseInstruction. See class definition for documentation |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="route"><see cref="Route"/></param> |
| | | 37 | | /// <param name="shapeIndex"><see cref="ShapeIndex"/></param> |
| | | 38 | | /// <param name="shapeIndexEnd"><see cref="ShapeIndexEnd"/></param> |
| | | 39 | | /// <param name="turnDegrees"><see cref="TurnDegrees"/></param> |
| | 59 | 40 | | public BaseInstruction(IndexedRoute route, |
| | 59 | 41 | | int shapeIndex, int shapeIndexEnd, int turnDegrees) |
| | 59 | 42 | | { |
| | 59 | 43 | | this.Route = route; |
| | 59 | 44 | | this.ShapeIndex = shapeIndex; |
| | 59 | 45 | | this.ShapeIndexEnd = shapeIndexEnd; |
| | 59 | 46 | | this.TurnDegrees = turnDegrees; |
| | 59 | 47 | | this.Type = Tp(this); |
| | 59 | 48 | | } |
| | | 49 | | |
| | 44 | 50 | | public BaseInstruction(IndexedRoute route, int shapeIndex, double turnDegrees) |
| | 44 | 51 | | { |
| | 44 | 52 | | this.Route = route; |
| | 44 | 53 | | this.ShapeIndex = shapeIndex; |
| | 44 | 54 | | this.ShapeIndexEnd = shapeIndex + 1; |
| | 44 | 55 | | this.TurnDegrees = turnDegrees.NormalizeDegrees(); |
| | 44 | 56 | | this.Type = Tp(this); |
| | 44 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The index of the start of the segment this instruction is applicable on; i.e. the traveller arrived at the s |
| | | 61 | | /// which starts at 'ShapeIndex', what should they do next? |
| | | 62 | | /// </summary> |
| | 202 | 63 | | public int ShapeIndex { get; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// The index where the described instruction stops. |
| | | 67 | | /// <remarks> |
| | | 68 | | /// Will often (but not always) be ShapeIndex + 1. The start- and endinstruction have ShapeIndexEnd == |
| | | 69 | | /// ShapeIndex; some others describe multiple segments |
| | | 70 | | /// </remarks> |
| | | 71 | | /// </summary> |
| | 262 | 72 | | public int ShapeIndexEnd { get; } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// The amount of degrees to turn at the end of the road. |
| | | 76 | | /// 0° is straight on, positive is turning left and negative is turning right |
| | | 77 | | /// </summary> |
| | 207 | 78 | | public int TurnDegrees { get; } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Gets the type of instruction. |
| | | 82 | | /// </summary> |
| | 226 | 83 | | public string Type { get; } |
| | | 84 | | |
| | 205 | 85 | | public IndexedRoute Route { get; } |
| | | 86 | | |
| | | 87 | | private static string Tp(object o) |
| | 103 | 88 | | { |
| | 103 | 89 | | var tp = o.GetType().Name.ToLower(); |
| | 103 | 90 | | return tp.EndsWith("instruction") ? tp.Substring(0, tp.Length - "instruction".Length) : tp; |
| | 103 | 91 | | } |
| | | 92 | | |
| | | 93 | | public override string ToString() |
| | 0 | 94 | | { |
| | 0 | 95 | | return $"Follow from p{this.ShapeIndex} to p{this.ShapeIndexEnd}, where you turn {this.TurnDegrees}°"; |
| | 0 | 96 | | } |
| | | 97 | | } |