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