852122fdb6e7aa9cf07c6fb9180afd9a0b737c27
[routino] / src / types.h
1 /***************************************
2  $Header: /home/amb/routino/src/RCS/types.h,v 1.40 2010/05/27 17:25:23 amb Exp $
3
4  Type definitions
5
6  Part of the Routino routing software.
7  ******************/ /******************
8  This file Copyright 2008-2010 Andrew M. Bishop
9
10  This program is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Affero General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14
15  This program is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU Affero General Public License for more details.
19
20  You should have received a copy of the GNU Affero General Public License
21  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  ***************************************/
23
24
25 #ifndef TYPES_H
26 #define TYPES_H    /*+ To stop multiple inclusions. +*/
27
28 #include <stdint.h>
29 #include <math.h>
30
31
32 #ifndef M_PI
33 #define M_PI 3.14159265358979323846
34 #endif
35
36
37 /* Constants and macros for handling them */
38
39
40 /*+ The latitude and longitude conversion factor from floating point (radians) to integer. +*/
41 #define LAT_LONG_SCALE (1024*65536)
42
43 /*+ The latitude and longitude integer range within each bin. +*/
44 #define LAT_LONG_BIN   65536
45
46
47 /*+ A flag to mark a node as a super-node. +*/
48 #define NODE_SUPER     ((index_t)0x80000000)
49
50 /*+ A segment index excluding the super-node flag. +*/
51 #define SEGMENT(xxx)   (index_t)((xxx)&(~NODE_SUPER))
52
53
54 /*+ An undefined node index. +*/
55 #define NO_NODE        (~(index_t)0)
56
57 /*+ An undefined segment index. +*/
58 #define NO_SEGMENT     (~(index_t)0)
59
60 /*+ An undefined way index. +*/
61 #define NO_WAY         (~(index_t)0)
62
63
64 /*+ A flag to mark a segment as one-way from node1 to node2. +*/
65 #define ONEWAY_1TO2    ((distance_t)0x80000000)
66
67 /*+ A flag to mark a segment as one-way node2 to node1. +*/
68 #define ONEWAY_2TO1    ((distance_t)0x40000000)
69
70 /*+ A flag to mark a segment as a super-segment. +*/
71 #define SEGMENT_SUPER  ((distance_t)0x20000000)
72
73 /*+ A flag to mark a segment as a normal segment. +*/
74 #define SEGMENT_NORMAL ((distance_t)0x10000000)
75
76 /*+ The real distance ignoring the ONEWAY_* and SEGMENT_* flags. +*/
77 #define DISTANCE(xx)   ((distance_t)(xx)&(~(ONEWAY_1TO2|ONEWAY_2TO1|SEGMENT_SUPER|SEGMENT_NORMAL)))
78
79 /*+ The distance flags selecting only the ONEWAY_* and SEGMENT_* flags. +*/
80 #define DISTFLAG(xx)   ((distance_t)(xx)&(ONEWAY_1TO2|ONEWAY_2TO1|SEGMENT_SUPER|SEGMENT_NORMAL))
81
82
83 /*+ A very large almost infinite distance. +*/
84 #define INF_DISTANCE   DISTANCE(~0)
85
86 /*+ A very large almost infinite score. +*/
87 #define INF_SCORE      (score_t)1E30
88
89
90 /* Simple Types */
91
92
93 /*+ A node, segment or way index. +*/
94 typedef uint32_t index_t;
95
96
97 /*+ A node latitude or longitude. +*/
98 typedef int32_t  latlong_t;
99
100 /*+ A node latitude or longitude bin number. +*/
101 typedef int16_t  ll_bin_t;
102
103 /*+ A node latitude or longitude offset. +*/
104 typedef uint16_t ll_off_t;
105
106
107 /*+ Conversion from a latlong (integer latitude or longitude) to a bin number. +*/
108 #define latlong_to_bin(xxx) (ll_bin_t)((latlong_t)((xxx)&~(LAT_LONG_BIN-1))/LAT_LONG_BIN)
109
110 /*+ Conversion from a bin number to a latlong (integer latitude or longitude). +*/
111 #define bin_to_latlong(xxx) ((latlong_t)(xxx)*LAT_LONG_BIN)
112
113 /*+ Conversion from a latlong (integer latitude or longitude) to a bin offset. +*/
114 #define latlong_to_off(xxx) (ll_off_t)((latlong_t)(xxx)&(LAT_LONG_BIN-1))
115
116 /*+ Conversion from a bin offset to a latlong (integer latitude or longitude). +*/
117 #define off_to_latlong(xxx) ((latlong_t)(xxx))
118
119
120 /*+ Conversion from a latitude or longitude in radians to a latlong (integer latitude or longitude). +*/
121 #define radians_to_latlong(xxx) ((latlong_t)floor((xxx)*LAT_LONG_SCALE))
122
123 /*+ Conversion from a latlong (integer latitude or longitude) to a latitude or longitude in radians. +*/
124 #define latlong_to_radians(xxx) ((double)(xxx)/LAT_LONG_SCALE)
125
126
127 /*+ Conversion from radians to degrees. +*/
128 #define radians_to_degrees(xxx) ((xxx)*(180.0/M_PI))
129
130 /*+ Conversion from degrees to radians. +*/
131 #define degrees_to_radians(xxx) ((xxx)*(M_PI/180.0))
132
133
134 /*+ A distance, measured in metres. +*/
135 typedef uint32_t distance_t;
136
137 /*+ A duration, measured in 1/10th seconds. +*/
138 typedef uint32_t duration_t;
139
140 /*+ A routing optimisation score. +*/
141 typedef float score_t;
142
143
144 /*+ Conversion from distance_t to kilometres. +*/
145 #define distance_to_km(xx) ((double)(xx)/1000.0)
146
147 /*+ Conversion from metres to distance_t. +*/
148 #define km_to_distance(xx) ((distance_t)((double)(xx)*1000.0))
149
150 /*+ Conversion from duration_t to minutes. +*/
151 #define duration_to_minutes(xx) ((double)(xx)/600.0)
152
153 /*+ Conversion from duration_t to hours. +*/
154 #define duration_to_hours(xx)   ((double)(xx)/36000.0)
155
156 /*+ Conversion from hours to duration_t. +*/
157 #define hours_to_duration(xx)   ((duration_t)((double)(xx)*36000.0))
158
159 /*+ Conversion from distance_t and speed_t to duration_t. +*/
160 #define distance_speed_to_duration(xx,yy) ((duration_t)(((double)(xx)/(double)(yy))*(36000.0/1000.0)))
161
162
163 /*+ The type of a way. +*/
164 typedef uint8_t waytype_t;
165
166 /*+ The different types of a way. +*/
167 typedef enum _Highway
168  {
169   Way_Motorway    = 1,
170   Way_Trunk       = 2,
171   Way_Primary     = 3,
172   Way_Secondary   = 4,
173   Way_Tertiary    = 5,
174   Way_Unclassified= 6,
175   Way_Residential = 7,
176   Way_Service     = 8,
177   Way_Track       = 9,
178   Way_Cycleway    =10,
179   Way_Path        =11,
180   Way_Steps       =12,
181
182   Way_Count       =13,       /* One more than the number of highway types. */
183
184   Way_OneWay      =32,
185   Way_Roundabout  =64
186  }
187  Highway;
188
189 #define HIGHWAY(xx) ((xx)&0x1f)
190
191
192 /*+ The different methods of transport. +*/
193 typedef enum _Transport
194  {
195   Transport_None       =  0,
196
197   Transport_Foot       =  1,
198   Transport_Horse      =  2,
199   Transport_Wheelchair =  3,
200   Transport_Bicycle    =  4,
201   Transport_Moped      =  5,
202   Transport_Motorbike  =  6,
203   Transport_Motorcar   =  7,
204   Transport_Goods      =  8,
205   Transport_HGV        =  9,
206   Transport_PSV        = 10,
207
208   Transport_Count      = 11     /*+ One more than the number of transport types. +*/
209  }
210  Transport;
211
212
213 /*+ The allowed traffic on a way. +*/
214 typedef uint16_t wayallow_t;
215
216 #define ALLOWED(xx)  (1<<((xx)-1))
217
218 /*+ The different allowed traffic on a way. +*/
219 typedef enum _Allowed
220  {
221   Allow_Foot       = ALLOWED(Transport_Foot      ),
222   Allow_Horse      = ALLOWED(Transport_Horse     ),
223   Allow_Wheelchair = ALLOWED(Transport_Wheelchair),
224   Allow_Bicycle    = ALLOWED(Transport_Bicycle   ),
225   Allow_Moped      = ALLOWED(Transport_Moped     ),
226   Allow_Motorbike  = ALLOWED(Transport_Motorbike ),
227   Allow_Motorcar   = ALLOWED(Transport_Motorcar  ),
228   Allow_Goods      = ALLOWED(Transport_Goods     ),
229   Allow_HGV        = ALLOWED(Transport_HGV       ),
230   Allow_PSV        = ALLOWED(Transport_PSV       ),
231
232   Allow_ALL        = 65535
233  }
234  Allowed;
235
236
237 /*+ The individual properties of a highway. +*/
238 typedef enum _Property
239  {
240   Property_None      = 0,
241
242   Property_Paved     = 1,
243   Property_Multilane = 2,
244   Property_Bridge    = 3,
245   Property_Tunnel    = 4,
246
247   Property_Count     = 5       /* One more than the number of property types. */
248  }
249  Property;
250
251
252 /*+ The combined set of properties of a way. +*/
253 typedef uint8_t wayprop_t;
254
255 #define PROPERTIES(xx)  (1<<((xx)-1))
256
257 /*+ The different properties of a way. +*/
258 typedef enum _Properties
259  {
260   Properties_Paved     = PROPERTIES(Property_Paved),
261   Properties_Multilane = PROPERTIES(Property_Multilane),
262   Properties_Bridge    = PROPERTIES(Property_Bridge),
263   Properties_Tunnel    = PROPERTIES(Property_Tunnel),
264
265   Properties_ALL       = 255
266  }
267  Properties;
268
269
270 /*+ The speed limit of a way, measured in km/hour. +*/
271 typedef uint8_t speed_t;
272
273 /*+ The maximum weight of a way, measured in 0.2 tonnes. +*/
274 typedef uint8_t weight_t;
275
276 /*+ The maximum height of a way, measured in 0.1 metres. +*/
277 typedef uint8_t height_t;
278
279 /*+ The maximum width of a way, measured in 0.1 metres. +*/
280 typedef uint8_t width_t;
281
282 /*+ The maximum length of a way, measured in 0.1 metres. +*/
283 typedef uint8_t length_t;
284
285
286 /*+ Conversion of km/hr to speed_t. +*/
287 #define kph_to_speed(xxx)      (speed_t)(xxx)
288
289 /*+ Conversion of speed_t to km/hr. +*/
290 #define speed_to_kph(xxx)      (int)(xxx)
291
292 /*+ Conversion of tonnes to weight_t. +*/
293 #define tonnes_to_weight(xxx)  (weight_t)((xxx)*5)
294
295 /*+ Conversion of weight_t to tonnes. +*/
296 #define weight_to_tonnes(xxx)  ((double)(xxx)/5.0)
297
298 /*+ Conversion of metres to height_t. +*/
299 #define metres_to_height(xxx)  (height_t)((xxx)*10)
300
301 /*+ Conversion of height_t to metres. +*/
302 #define height_to_metres(xxx)  ((double)(xxx)/10.0)
303
304 /*+ Conversion of metres to width_t. +*/
305 #define metres_to_width(xxx)   (width_t)((xxx)*10)
306
307 /*+ Conversion of width_t to metres. +*/
308 #define width_to_metres(xxx)   ((double)(xxx)/10.0)
309
310 /*+ Conversion of metres to length_t. +*/
311 #define metres_to_length(xxx)  (length_t)((xxx)*10)
312
313 /*+ Conversion of length_t to metres. +*/
314 #define length_to_metres(xxx)  ((double)(xxx)/10.0)
315
316
317 /* Data structures */
318
319 typedef struct _Node Node;
320
321 typedef struct _Nodes Nodes;
322
323 typedef struct _Segment Segment;
324
325 typedef struct _Segments Segments;
326
327 typedef struct _Way Way;
328
329 typedef struct _Ways Ways;
330
331
332 /* Functions */
333
334 Highway HighwayType(const char *highway);
335 Transport TransportType(const char *transport);
336 Property PropertyType(const char *property);
337
338 const char *HighwayName(Highway highway);
339 const char *TransportName(Transport transport);
340 const char *PropertyName(Property property);
341
342 const char *AllowedNameList(wayallow_t allowed);
343 const char *PropertiesNameList(wayprop_t properties);
344
345 const char *HighwayList(void);
346 const char *TransportList(void);
347 const char *PropertyList(void);
348
349
350 #endif /* TYPES_H */