forked from mkoloberdin/waze
/
roadmap_line.c
351 lines (250 loc) · 8.68 KB
/
roadmap_line.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* roadmap_line.c - Manage the tiger lines.
*
* LICENSE:
*
* Copyright 2002 Pascal F. Martin
* Copyright 2008 Ehud Shabtai
*
* This file is part of RoadMap.
*
* RoadMap is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* RoadMap is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with RoadMap; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* SYNOPSYS:
*
* int roadmap_line_in_square (int square, int cfcc, int *first, int *last);
* void roadmap_line_from (int line, RoadMapPosition *position);
* void roadmap_line_to (int line, RoadMapPosition *position);
*
* int roadmap_line_count (void);
*
* These functions are used to retrieve the points that make the lines.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#define DECLARE_ROADMAP_LINE
#include "roadmap.h"
#include "roadmap_dbread.h"
#include "roadmap_tile_model.h"
#include "roadmap_db_line.h"
#include "roadmap_point.h"
#include "roadmap_math.h"
#include "roadmap_shape.h"
#include "roadmap_square.h"
#include "roadmap_layer.h"
#include "roadmap_locator.h"
#include "roadmap_range.h"
#include "roadmap_line.h"
static char *RoadMapLineType = "RoadMapLineContext";
RoadMapLineContext *RoadMapLineActive = NULL;
static void *roadmap_line_map (const roadmap_db_data_file *file) {
RoadMapLineContext *context;
context = (RoadMapLineContext *) malloc (sizeof(RoadMapLineContext));
if (context == NULL) {
roadmap_log (ROADMAP_ERROR, "no more memory");
return NULL;
}
context->type = RoadMapLineType;
if (!roadmap_db_get_data (file,
model__tile_line_data,
sizeof (RoadMapLine),
(void**)&(context->Line),
&(context->LineCount))) {
roadmap_log (ROADMAP_ERROR, "invalid line/data structure");
goto roadmap_line_map_abort;
}
if (!roadmap_db_get_data (file,
model__tile_line_bysquare1,
sizeof (RoadMapLineBySquare),
(void**)&(context->LineBySquare1),
&(context->LineBySquare1Count))) {
roadmap_log (ROADMAP_ERROR, "invalid line/bysquare1 structure");
goto roadmap_line_map_abort;
}
if (context->LineBySquare1->first_broken[ROADMAP_DIRECTION_COUNT * 2] > 0) {
if (!roadmap_db_get_data (file,
model__tile_line_broken,
sizeof (unsigned short),
(void**)&(context->BrokenLine),
&(context->BrokenLineCount))) {
roadmap_log (ROADMAP_ERROR, "invalid line/broken structure");
goto roadmap_line_map_abort;
}
if (context->LineBySquare1->first_broken[ROADMAP_DIRECTION_COUNT * 2] != context->BrokenLineCount) {
roadmap_log (ROADMAP_ERROR, "broken count mismatch");
goto roadmap_line_map_abort;
}
} else {
context->BrokenLine = NULL;
context->BrokenLineCount = 0;
}
if (context->LineBySquare1->num_roundabout > 0) {
if (!roadmap_db_get_data (file,
model__tile_line_roundabout,
sizeof (unsigned short),
(void**)&(context->RoundaboutLine),
&(context->RoundaboutLineCount))) {
roadmap_log (ROADMAP_ERROR, "invalid line/roundabout structure");
goto roadmap_line_map_abort;
}
if (context->LineBySquare1->num_roundabout != context->RoundaboutLineCount) {
roadmap_log (ROADMAP_ERROR, "roundabout count mismatch");
goto roadmap_line_map_abort;
}
} else {
context->RoundaboutLine = NULL;
context->RoundaboutLineCount = 0;
}
return context;
roadmap_line_map_abort:
free(context);
return NULL;
}
static void roadmap_line_activate (void *context) {
RoadMapLineContext *line_context = (RoadMapLineContext *) context;
VERIFY_THREAD("roadmap_line_activate")
if ((line_context != NULL) &&
(line_context->type != RoadMapLineType)) {
roadmap_log (ROADMAP_FATAL, "invalid line context activated");
}
RoadMapLineActive = line_context;
}
static void roadmap_line_unmap (void *context) {
RoadMapLineContext *line_context = (RoadMapLineContext *) context;
if (line_context->type != RoadMapLineType) {
roadmap_log (ROADMAP_FATAL, "unmapping invalid line context");
}
free (line_context);
}
roadmap_db_handler RoadMapLineHandler = {
"dictionary",
roadmap_line_map,
roadmap_line_activate,
roadmap_line_unmap
};
int roadmap_line_from_is_fake (int line) {
return RoadMapLineActive->Line[line].from & POINT_FAKE_FLAG;
}
int roadmap_line_to_is_fake (int line) {
return RoadMapLineActive->Line[line].to & POINT_FAKE_FLAG;
}
int roadmap_line_count (void) {
if (RoadMapLineActive == NULL) return 0; /* No line. */
return RoadMapLineActive->LineCount;
}
int roadmap_line_length (int line) {
RoadMapPosition p1;
RoadMapPosition p2;
int length = 0;
int first_shape;
int last_shape;
int i;
roadmap_line_from (line, &p1);
if (roadmap_line_shapes (line, &first_shape, &last_shape) > 0) {
p2 = p1;
for (i = first_shape; i <= last_shape; i++) {
roadmap_shape_get_position (i, &p2);
length += roadmap_math_distance (&p1, &p2);
p1 = p2;
}
}
roadmap_line_to (line, &p2);
length += roadmap_math_distance (&p1, &p2);
return length;
}
int roadmap_line_get_range (int line) {
int range;
if (RoadMapLineActive == NULL) return -1; /* No line. */
VERIFY_THREAD("roadmap_line_get_range")
if (line < 0 || line >= RoadMapLineActive->LineCount) {
roadmap_log (ROADMAP_ERROR, "illegal line index %d", line);
return -1;
}
range = RoadMapLineActive->Line[line].range;
if (range == ROADMAP_LINE_NO_RANGE) return -1;
return range;
}
int roadmap_line_get_street (int line) {
return roadmap_range_get_street (roadmap_line_get_range (line));
}
void roadmap_line_points (int line, int *from, int *to) {
VERIFY_THREAD("roadmap_line_points")
#ifdef DEBUG
if (line < 0 || line >= RoadMapLineActive->LineCount) {
roadmap_log (ROADMAP_FATAL, "illegal line index %d", line);
}
#endif
*from = RoadMapLineActive->Line[line].from & POINT_REAL_MASK;
*to = RoadMapLineActive->Line[line].to & POINT_REAL_MASK;
}
void roadmap_line_from_point (int line, int *from) {
VERIFY_THREAD("roadmap_line_from_point")
#ifdef DEBUG
if (line < 0 || line >= RoadMapLineActive->LineCount) {
roadmap_log (ROADMAP_FATAL, "illegal line index %d", line);
}
#endif
*from = RoadMapLineActive->Line[line].from & POINT_REAL_MASK;
}
void roadmap_line_to_point (int line, int *to) {
VERIFY_THREAD("roadmap_line_to_point")
#ifdef DEBUG
if (line < 0 || line >= RoadMapLineActive->LineCount) {
roadmap_log (ROADMAP_FATAL, "illegal line index %d", line);
}
#endif
*to = RoadMapLineActive->Line[line].to & POINT_REAL_MASK;
}
void roadmap_line_point_ids (int line, int *id_from, int *id_to) {
VERIFY_THREAD("roadmap_line_point_ids")
#ifdef DEBUG
if (line < 0 || line >= RoadMapLineActive->LineCount) {
roadmap_log (ROADMAP_FATAL, "illegal line index %d", line);
}
#endif
*id_from = roadmap_point_db_id (RoadMapLineActive->Line[line].from & POINT_REAL_MASK);
*id_to = roadmap_point_db_id (RoadMapLineActive->Line[line].to & POINT_REAL_MASK);
}
int roadmap_line_cfcc (int line_id) {
unsigned short *index;
int cfcc;
if (RoadMapLineActive == NULL) return 0; /* No line. */
index = RoadMapLineActive->LineBySquare1[0].next;
if (index[ROADMAP_ROAD_FIRST - 1] > line_id) return 0; // not a road
for (cfcc = ROADMAP_ROAD_FIRST; cfcc <= ROADMAP_ROAD_LAST; cfcc++) {
if (index[cfcc] > line_id) return cfcc;
}
return 0;
}
int roadmap_line_broken_range (int direction, int broken_to, int *first, int *last) {
unsigned short *pos;
if (RoadMapLineActive == NULL) return 0; /* No line. */
pos = RoadMapLineActive->LineBySquare1->first_broken + (direction * 2 + broken_to);
*first = *pos++;
*last = (*pos) - 1;
return (*last) >= (*first);
}
int roadmap_line_get_broken (int broken_id) {
return RoadMapLineActive->BrokenLine[broken_id];
}
SegmentContext roadmap_line_context (int line_id) {
int i;
for (i = 0; i < RoadMapLineActive->RoundaboutLineCount; i++) {
if (line_id == RoadMapLineActive->RoundaboutLine[i]) return SEG_ROUNDABOUT;
}
return SEG_ROAD;
}