forked from mkoloberdin/waze
/
roadmap_navigate.c
1778 lines (1402 loc) · 62.1 KB
/
roadmap_navigate.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* roadmap_navigate.c - Basic navigation engine for RoadMap.
*
* LICENSE:
*
* Copyright 2003 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:
*
* See roadmap_navigate.h.
*/
#include <string.h>
#include <stdlib.h>
#include "roadmap.h"
#include "roadmap_gui.h"
#include "roadmap_math.h"
#include "roadmap_config.h"
#include "roadmap_message.h"
#include "roadmap_line.h"
#include "roadmap_line_route.h"
#include "roadmap_square.h"
#include "roadmap_street.h"
#include "roadmap_layer.h"
#include "roadmap_display.h"
#include "roadmap_locator.h"
#include "roadmap_fuzzy.h"
#include "roadmap_adjust.h"
#include "roadmap_plugin.h"
#include "roadmap_point.h"
#include "roadmap_string.h"
#include "roadmap_trip.h"
#include "roadmap_alerter.h"
#include "animation/roadmap_animation.h"
#include "roadmap_time.h"
#include "roadmap_tile_manager.h"
//FIXME remove when navigation will support plugin lines
#include "editor/editor_plugin.h"
#include "editor/editor_cleanup.h"
#include "roadmap_navigate.h"
#include "roadmap_plugin.h"
static RoadMapConfigDescriptor RoadMapNavigateMinMobileSpeedCfg =
ROADMAP_CONFIG_ITEM("Navigation", "Min Mobile Knots");
static RoadMapConfigDescriptor RoadMapNavigateMaxJamSpeedCfg =
ROADMAP_CONFIG_ITEM("Navigation", "Max Jam Knots");
static RoadMapConfigDescriptor RoadMapNavigateMinJamDistanceFromEndCfg =
ROADMAP_CONFIG_ITEM("Navigation", "Min Jam Distance");
static int RoadMapNavigateMinMobileSpeed = -1;
static int RoadMapNavigateMaxJamSpeed = -1;
static int RoadMapNavigateJammedSince = 0;
static int RoadMapNavigateMinJamDistanceFromEnd = 0;
static RoadMapTileCallback TileCbNext = NULL;
static RoadMapConfigDescriptor RoadMapNavigateFlag =
ROADMAP_CONFIG_ITEM("Navigation", "Enable");
#define MIN_SPEED_INIT_TIME 10
#define HIGH_SPEED_FILTER 32 //32 knot = 60 kph
//#define DEBUG_PRINTS //TODO: remove this when not debugging
typedef struct {
RoadMapNavigateRouteCB callbacks;
PluginLine current_line;
PluginLine next_line;
int enabled;
int initialized;
} RoadMapNavigateRoute;
typedef struct{
int index;
RoadMapFuzzy result;
BOOL found;
BOOL in_route;
RoadMapTracking tracking;
} RoadMapNavCandidates;
static void animation_set_callback (void *context);
static void animation_ended_callback (void *context);
static RoadMapAnimationCallbacks gAnimationCallbacks =
{
animation_set_callback,
animation_ended_callback
};
static const char* GPS_OBJECT = "displayed_gps";
#define ROADMAP_NAVIGATE_ROUTE_NULL \
{ {0}, PLUGIN_LINE_NULL, PLUGIN_LINE_NULL, 0 }
static RoadMapNavigateRoute RoadMapRouteInfo;
/* The navigation flag will be set according to the session's content,
* so that the user selected state is kept from one session to
* the next.
*/
static int RoadMapNavigateEnabled = 0;
/* Avoid doing navigation work when the position has not changed. */
static RoadMapGpsPosition RoadMapLatestGpsPosition = {0, 0, 0, -1, INVALID_STEERING};
static RoadMapGpsPosition RoadMapLatestValidPosition = {0, 0, 0, -1, INVALID_STEERING};
static RoadMapPosition RoadMapLatestPosition;
static time_t RoadMapLatestUpdate;
static time_t RoadMapLatestGpsTime = 0;
#define ROADMAP_NEIGHBOURHOUD 16
static RoadMapTracking RoadMapConfirmedStreet = ROADMAP_TRACKING_NULL;
static RoadMapNeighbour RoadMapConfirmedLine = ROADMAP_NEIGHBOUR_NULL;
static RoadMapNeighbour RoadMapNeighbourhood[ROADMAP_NEIGHBOURHOUD];
#define MAX_SEGMENT_CHANGED_CALLBACKS 5
static RoadMapNavigateSegmentChangedCB NavigateSegmentChangedCB[MAX_SEGMENT_CHANGED_CALLBACKS];
void roadmap_navigate_register_segment_changed(RoadMapNavigateSegmentChangedCB cb){
int index;
for (index = 0; index < MAX_SEGMENT_CHANGED_CALLBACKS; index++) {
if (NavigateSegmentChangedCB[index] == NULL){
NavigateSegmentChangedCB[index] = cb;
break;
}
}
}
void roadmap_navigate_unregister_segment_changed(RoadMapNavigateSegmentChangedCB cb){
int index;
for (index = 0; index < MAX_SEGMENT_CHANGED_CALLBACKS; index++) {
if (NavigateSegmentChangedCB[index] == cb){
NavigateSegmentChangedCB[index] = NULL;
break;
}
}
}
static void on_segment_changed_inform_clients(PluginLine *line, int direction){
int index;
for (index = 0; index < MAX_SEGMENT_CHANGED_CALLBACKS; index++) {
if (NavigateSegmentChangedCB[index] != NULL)
(*NavigateSegmentChangedCB[index])(line, direction);
}
}
static void roadmap_navigate_trace (const char *format, PluginLine *line) {
char text[1024];
PluginStreet street;
PluginStreetProperties properties;
if (! roadmap_log_enabled (ROADMAP_DEBUG)) return;
#ifdef __SYMBIAN32__
return;
#endif
roadmap_plugin_get_street (line, &street);
roadmap_plugin_get_street_properties (line, &properties, 0);
roadmap_message_set ('#', properties.address);
roadmap_message_set ('N', properties.street);
roadmap_message_set ('C', properties.city);
text[0] = 0;
if (roadmap_message_format (text, sizeof(text), format)) {
printf ("%s (plugin_id %d, line_id %d, street_id %d)\n",
text,
roadmap_plugin_get_id (line),
roadmap_plugin_get_line_id (line),
roadmap_plugin_get_street_id (&street));
}
}
static void roadmap_navigate_adjust_focus
(RoadMapArea *focus, const RoadMapGuiPoint *focused_point) {
RoadMapPosition focus_position;
roadmap_math_to_position (focused_point, &focus_position, 1);
if (focus_position.longitude < focus->west) {
focus->west = focus_position.longitude;
}
if (focus_position.longitude > focus->east) {
focus->east = focus_position.longitude;
}
if (focus_position.latitude < focus->south) {
focus->south = focus_position.latitude;
}
if (focus_position.latitude > focus->north) {
focus->north = focus_position.latitude;
}
}
#ifdef OGL_TILE
static void roadmap_navigate_animate_gps (const RoadMapGpsPosition *new_gps_pos) {
RoadMapAnimation *animation;
const RoadMapGpsPosition *current_gps_pos;
RoadMapPosition from_pos, to_pos;
int from_dir, to_dir;
RoadMapNeighbour candidate;
int distance = 0;
static BOOL set_once = FALSE;
current_gps_pos = roadmap_trip_get_gps_position("GPS");
if (current_gps_pos && set_once) {
from_dir = current_gps_pos->steering;
roadmap_math_normalize_orientation(&from_dir);
to_dir = new_gps_pos->steering;
roadmap_math_normalize_orientation(&to_dir);
if (to_dir - from_dir >= 180 ||
to_dir - from_dir <= -180) {
if (360 - from_dir < 360 - to_dir)
from_dir = from_dir - 360;
else
to_dir = to_dir - 360;
}
to_pos.longitude = new_gps_pos->longitude;
to_pos.latitude = new_gps_pos->latitude;
if (RoadMapConfirmedStreet.valid &&
roadmap_plugin_get_distance
(&to_pos,
&RoadMapConfirmedLine.line,
&candidate) &&
roadmap_plugin_same_line (&candidate.line, &RoadMapConfirmedLine.line)) {
if (current_gps_pos->latitude == candidate.intersection.latitude &&
current_gps_pos->longitude == candidate.intersection.longitude &&
current_gps_pos->speed == new_gps_pos->speed &&
from_dir == to_dir) {
return;
}
} else if (current_gps_pos->latitude == new_gps_pos->latitude &&
current_gps_pos->longitude == new_gps_pos->longitude &&
current_gps_pos->speed == new_gps_pos->speed &&
from_dir == to_dir) {
return;
}
from_pos.longitude = current_gps_pos->longitude;
from_pos.latitude = current_gps_pos->latitude;
distance = roadmap_math_distance(&from_pos, &to_pos);
}
if (!current_gps_pos ||
distance > 100 ||
!set_once) {
set_once = TRUE;
roadmap_trip_set_mobile ("GPS", new_gps_pos);
return;
}
animation = roadmap_animation_create();
if (animation) {
snprintf(animation->object_id, ANIMATION_MAX_OBJECT_LENGTH, "%s", GPS_OBJECT);
animation->properties_count = 4;
// lon / lat
animation->properties[0].type = ANIMATION_PROPERTY_POSITION_X;
animation->properties[0].from = current_gps_pos->longitude;
animation->properties[0].to = new_gps_pos->longitude;
animation->properties[1].type = ANIMATION_PROPERTY_POSITION_Y;
animation->properties[1].from = current_gps_pos->latitude;
animation->properties[1].to = new_gps_pos->latitude;
// rotation
animation->properties[2].type = ANIMATION_PROPERTY_ROTATION;
animation->properties[2].from = from_dir;
animation->properties[2].to = to_dir;
// speed
animation->properties[3].type = ANIMATION_PROPERTY_SPEED;
animation->properties[3].from = current_gps_pos->speed;
animation->properties[3].to = new_gps_pos->speed;
//altitude is currently not used...
animation->duration = RM_SCREEN_CAR_ANIMATION;
animation->priority = ANIMATION_PRIORITY_GPS;
animation->callbacks = &gAnimationCallbacks;
roadmap_animation_register(animation);
}
}
#endif
static void roadmap_navigate_set_mobile (const RoadMapGpsPosition *gps,
const RoadMapPosition *fixed_pos,
int fixed_streering) {
RoadMapGpsPosition fixed_gps_pos;
fixed_gps_pos = *gps;
if (fixed_pos != NULL) {
fixed_gps_pos.longitude = fixed_pos->longitude;
fixed_gps_pos.latitude = fixed_pos->latitude;
if (roadmap_math_delta_direction(fixed_streering, gps->steering) < 90) {
fixed_gps_pos.steering = fixed_streering;
} else if (roadmap_math_delta_direction(
fixed_streering + 180, gps->steering) < 90) {
fixed_gps_pos.steering = fixed_streering + 180;
if (fixed_gps_pos.steering > 359) fixed_gps_pos.steering -= 360;
}
}
#ifdef OGL_TILE
roadmap_navigate_animate_gps (&fixed_gps_pos);
#else
roadmap_trip_set_mobile ("GPS", &fixed_gps_pos);
#endif
roadmap_navigate_check_alerts (&fixed_gps_pos);
RoadMapLatestUpdate = time(NULL);
}
int roadmap_navigate_get_neighbours
(const RoadMapPosition *position, int scale, int accuracy, int max_shapes,
RoadMapNeighbour *neighbours, int max, int type) {
int count = 0;
int layers[128];
int layer_count;
RoadMapArea focus;
RoadMapGuiPoint focus_point;
RoadMapPosition focus_position;
roadmap_log_push ("roadmap_navigate_get_neighbours");
if (roadmap_math_point_is_visible (position)) {
accuracy = ADJ_SCALE(accuracy); //AviR - patch
roadmap_math_coordinate (position, &focus_point);
roadmap_math_rotate_project_coordinate (&focus_point);
focus_point.x += accuracy;
focus_point.y += accuracy;
roadmap_math_to_position (&focus_point, &focus_position, 1);
focus.west = focus_position.longitude;
focus.east = focus_position.longitude;
focus.north = focus_position.latitude;
focus.south = focus_position.latitude;
accuracy *= 2;
focus_point.x -= accuracy;
roadmap_navigate_adjust_focus (&focus, &focus_point);
focus_point.y -= accuracy;
roadmap_navigate_adjust_focus (&focus, &focus_point);
focus_point.x += accuracy;
roadmap_navigate_adjust_focus (&focus, &focus_point);
} else {
accuracy *= 100;
focus.west = position->longitude - accuracy;
focus.east = position->longitude + accuracy;
focus.north = position->latitude + accuracy;
focus.south = position->latitude - accuracy;
}
if (type == LAYER_VISIBLE_ROADS) {
layer_count = roadmap_layer_visible_roads (layers, 128);
} else {
layer_count = roadmap_layer_all_roads (layers, 128);
}
if (layer_count > 0) {
roadmap_math_set_focus (&focus);
count = roadmap_street_get_closest
(position, scale, layers, layer_count, max_shapes, neighbours, max);
count = roadmap_plugin_get_closest
(position, layers, layer_count, max_shapes, neighbours, count, max);
roadmap_math_release_focus ();
}
roadmap_log_pop ();
return count;
}
void roadmap_navigate_disable (void) {
RoadMapNavigateEnabled = 0;
roadmap_display_hide ("Approach");
roadmap_display_hide ("Current Street");
roadmap_config_set (&RoadMapNavigateFlag, "no");
}
void roadmap_navigate_enable (void) {
RoadMapNavigateEnabled = 1;
roadmap_config_set (&RoadMapNavigateFlag, "yes");
}
int roadmap_navigate_retrieve_line
(const RoadMapPosition *position, int scale, int accuracy, PluginLine *line,
int *distance, int type) {
RoadMapNeighbour closest;
if (roadmap_navigate_get_neighbours
(position, scale, accuracy, 1, &closest, 1, type) <= 0) {
return -1;
}
*distance = closest.distance;
if (roadmap_plugin_activate_db (&closest.line) == -1) {
return -1;
}
*line = closest.line;
return 0;
}
int roadmap_navigate_retrieve_line_force_name
(const RoadMapPosition *position, int scale, int accuracy, PluginLine *line,
int *distance, int type, int orig_square, const char *name,
int route, int fake_from, int fake_to,
RoadMapPosition *force_from, RoadMapPosition *force_to) {
RoadMapNeighbour candidate[5];
RoadMapStreetProperties street;
int i;
int n;
n = roadmap_navigate_get_neighbours
(position, scale, accuracy, 1, candidate, 5, type);
if (n <= 0) {
return -1;
}
for (i = 0; i < n; i++) {
if (candidate[i].line.plugin_id == ROADMAP_PLUGIN_ID &&
orig_square != candidate[i].line.square) {
roadmap_square_set_current (candidate[i].line.square);
roadmap_street_get_properties (candidate[i].line.line_id, &street);
if ((!strcmp (name, roadmap_street_get_full_name (&street))) &&
(roadmap_line_route_get_direction (candidate[i].line.line_id, ROUTE_DIRECTION_ANY) == route) ) {
if ((fake_from && !roadmap_line_from_is_fake (candidate[i].line.line_id)) ||
(fake_to && !roadmap_line_to_is_fake (candidate[i].line.line_id))) {
continue;
}
if (force_from || force_to) {
RoadMapPosition from;
RoadMapPosition to;
roadmap_street_extend_line_ends (&candidate[i].line, &from, &to, FLAG_EXTEND_BOTH, NULL, NULL);
if (abs (from.longitude - force_from->longitude) < 4 &&
abs (from.latitude - force_from->latitude) < 4 &&
abs (to.longitude - force_to->longitude) < 4 &&
abs (to.latitude - force_to->latitude) < 4) {
break;
}
} else {
break;
}
}
}
}
if (i >= n) {
return -1;
}
if (roadmap_plugin_activate_db (&(candidate[i].line)) == -1) {
return -1;
}
*distance = candidate[i].distance;
*line = candidate[i].line;
return 0;
}
int roadmap_navigate_fuzzify_internal (RoadMapTracking *tracked,
RoadMapTracking *previous_street,
RoadMapNeighbour *previous_line,
RoadMapNeighbour *line,
int against_direction,
int direction,
int confidence,
int speed,
int accuracy) {
RoadMapFuzzy fuzzyfied_distance;
RoadMapFuzzy fuzzyfied_direction;
RoadMapFuzzy fuzzyfied_direction_with_line = 0;
RoadMapFuzzy fuzzyfied_direction_against_line = 0;
RoadMapFuzzy connected;
int line_direction = 0;
int azymuth_with_line = 0;
int azymuth_against_line = 0;
int symetric = 0;
int distance = line->distance;
tracked->debug.direction = roadmap_fuzzy_false();
tracked->debug.distance = roadmap_fuzzy_false();
tracked->debug.connected = roadmap_fuzzy_false();
tracked->opposite_street_direction = against_direction;
if (!previous_street || !previous_street->valid) {
previous_line = NULL;
}
if (0 && accuracy > 0) {
//printf("distance reduced from: %d to ", distance);
distance -= accuracy/10;
if (distance < 0)
distance = 0;
//printf("%d\n", distance);
}
fuzzyfied_distance = roadmap_fuzzy_distance (distance);
if (! roadmap_fuzzy_is_acceptable (fuzzyfied_distance)) {
return roadmap_fuzzy_false ();
}
line_direction = roadmap_plugin_get_direction (&line->line, ROUTE_CAR_ALLOWED);
if ((line_direction == ROUTE_DIRECTION_NONE) ||
(line_direction == ROUTE_DIRECTION_ANY)) {
symetric = 1;
} else if (against_direction) {
if (line_direction == ROUTE_DIRECTION_WITH_LINE) {
line_direction = ROUTE_DIRECTION_AGAINST_LINE;
} else {
line_direction = ROUTE_DIRECTION_WITH_LINE;
}
}
if (symetric || (line_direction == ROUTE_DIRECTION_WITH_LINE)) {
azymuth_with_line = roadmap_math_azymuth (&line->from, &line->to);
fuzzyfied_direction_with_line =
roadmap_fuzzy_direction (azymuth_with_line, direction, 0);
}
if (symetric || (line_direction == ROUTE_DIRECTION_AGAINST_LINE)) {
azymuth_against_line = roadmap_math_azymuth (&line->to, &line->from);
fuzzyfied_direction_against_line =
roadmap_fuzzy_direction (azymuth_against_line, direction, 0);
}
if (fuzzyfied_direction_against_line >
fuzzyfied_direction_with_line) {
fuzzyfied_direction = fuzzyfied_direction_against_line;
tracked->azymuth = azymuth_against_line;
tracked->line_direction = ROUTE_DIRECTION_AGAINST_LINE;
} else {
fuzzyfied_direction = fuzzyfied_direction_with_line;
tracked->azymuth = azymuth_with_line;
tracked->line_direction = ROUTE_DIRECTION_WITH_LINE;
}
if (! roadmap_fuzzy_is_acceptable (fuzzyfied_direction)) {
return roadmap_fuzzy_false ();
}
if (previous_line != NULL) {
connected = roadmap_fuzzy_connected(line, previous_line, previous_street->line_direction,
tracked->line_direction, &tracked->entry);
} else {
connected = roadmap_fuzzy_true ();
}
tracked->debug.direction = fuzzyfied_direction;
tracked->debug.distance = fuzzyfied_distance;
tracked->debug.connected = connected;
return roadmap_fuzzy_and (connected,
roadmap_fuzzy_and (fuzzyfied_distance, fuzzyfied_direction));
}
int roadmap_navigate_fuzzify (RoadMapTracking *tracked,
RoadMapTracking *previous_street,
RoadMapNeighbour *previous_line,
RoadMapNeighbour *line,
int against_direction,
int direction) {
return roadmap_navigate_fuzzify_internal (tracked, previous_street, previous_line, line, against_direction, direction, 0, -1, -1);
}
static int roadmap_navigate_confirm_intersection
(const RoadMapGpsPosition *position) {
int delta;
if (!PLUGIN_VALID(RoadMapConfirmedStreet.intersection)) return 0;
delta = roadmap_math_delta_direction (position->steering,
RoadMapConfirmedStreet.azymuth);
return (delta < 90 && delta > -90);
}
/* Check if the given street block is a possible intersection and
* evaluate how good an intersection guess it would be.
* The criteria for a good intersection is that it must be as far
* as possible from matching our current direction.
*/
static RoadMapFuzzy roadmap_navigate_is_intersection
(PluginLine *line,
int direction,
const RoadMapPosition *crossing) {
RoadMapPosition line_to;
RoadMapPosition line_from;
if (roadmap_plugin_same_line (line, &RoadMapConfirmedLine.line))
return roadmap_fuzzy_false();
roadmap_plugin_line_to (line, &line_to);
roadmap_plugin_line_from (line, &line_from);
if ((line_from.latitude != crossing->latitude) ||
(line_from.longitude != crossing->longitude)) {
if ((line_to.latitude != crossing->latitude) ||
(line_to.longitude != crossing->longitude)) {
return roadmap_fuzzy_false();
}
}
return roadmap_fuzzy_not
(roadmap_fuzzy_direction
(roadmap_math_azymuth (&line_from, &line_to), direction, 1));
}
/* Find the next intersection point.
* We compare the current direction with the azimuth to the two ends of
* the closest segment of line. From there, we deduct which end of the line
* we are going to. Note that we do not consider the azimuth toward the ends
* of the line, as the line might be a curve, and the result might be quite
* confusing (imagine a mountain road..).
* Once we know where we are going, we select a street that intersects
* this point and seems the best guess as an intersection.
*/
static int roadmap_navigate_find_intersection
(const RoadMapGpsPosition *position, PluginLine *found) {
int cfcc;
int square;
int first_line;
int last_line;
int delta_to;
int delta_from;
RoadMapPosition crossing;
RoadMapFuzzy match;
RoadMapFuzzy best_match;
RoadMapStreetProperties properties;
int fips = roadmap_locator_active ();
PluginLine plugin_lines[100];
int count;
int i;
delta_from =
roadmap_math_delta_direction
(position->steering,
roadmap_math_azymuth (&RoadMapLatestPosition,
&RoadMapConfirmedLine.from));
delta_to =
roadmap_math_delta_direction
(position->steering,
roadmap_math_azymuth (&RoadMapLatestPosition,
&RoadMapConfirmedLine.to));
if (delta_to < delta_from - 30) {
roadmap_plugin_line_to (&RoadMapConfirmedLine.line, &crossing);
} else if (delta_from < delta_to - 30) {
roadmap_plugin_line_from (&RoadMapConfirmedLine.line, &crossing);
} else {
return 0; /* Not sure enough. */
}
/* Should we compare to RoadMapConfirmedStreet.entry so
* to detect U-turns ?
*/
/* Now we know which endpoint we are headed toward.
* Let find if there is any street block that represents a valid
* intersection street. We select the one that matches the best.
*/
best_match = roadmap_fuzzy_false();
square = roadmap_square_search (&crossing, 0);
if (square != -1) {
roadmap_square_set_current (square);
for (cfcc = ROADMAP_ROAD_FIRST; cfcc <= ROADMAP_ROAD_LAST; ++cfcc) {
if (roadmap_line_in_square
(square, cfcc, &first_line, &last_line) > 0) {
int line;
for (line = first_line; line <= last_line; ++line) {
PluginLine p_line;
p_line.plugin_id = ROADMAP_PLUGIN_ID;
p_line.line_id = line;
p_line.cfcc = cfcc;
p_line.square = square;
p_line.fips = fips;
if (roadmap_plugin_override_line (line, cfcc, fips)) {
continue;
}
match = roadmap_navigate_is_intersection
(&p_line, position->steering, &crossing);
if (best_match < match) {
PluginStreet street;
roadmap_street_get_properties (line, &properties);
roadmap_plugin_set_street
(&street, ROADMAP_PLUGIN_ID, square, properties.street);
if ((properties.street > 0) &&
!roadmap_plugin_same_street
(&street,
&RoadMapConfirmedStreet.street)) {
*found = p_line;
best_match = match;
}
}
}
}
}
}
/* search plugin lines */
count = roadmap_plugin_find_connected_lines
(&crossing,
plugin_lines,
sizeof(plugin_lines)/sizeof(*plugin_lines));
for (i=0; i<count; i++) {
PluginStreet street;
match = roadmap_navigate_is_intersection
(&plugin_lines[i], position->steering, &crossing);
if (best_match < match) {
roadmap_plugin_get_street (&plugin_lines[i], &street);
if (!roadmap_plugin_same_street
(&street,
&RoadMapConfirmedStreet.street)) {
*found = plugin_lines[i];
best_match = match;
}
}
}
if (! roadmap_fuzzy_is_acceptable (best_match)) {
roadmap_display_hide ("Current Street");
return 0;
}
roadmap_navigate_trace
("Announce crossing %N, %C|Announce crossing %N", found);
if (!roadmap_plugin_same_line
(found, &RoadMapConfirmedStreet.intersection)) {
RoadMapConfirmedStreet.intersection = *found;
if (!RoadMapRouteInfo.enabled) {
// roadmap_display_activate ("Approach", found, &crossing, &street);
}
}
return 1;
}
static void roadmap_navigate_update_jammed_status (int gps_speed) {
static int is_mobile = 0;
if ((!is_mobile) && gps_speed >= RoadMapNavigateMinMobileSpeed) {
roadmap_log(ROADMAP_INFO, "Hit mobility speed");
is_mobile = 1;
}
if (is_mobile) {
if (RoadMapNavigateJammedSince == 0 && gps_speed < RoadMapNavigateMaxJamSpeed) {
RoadMapNavigateJammedSince = time (NULL);
roadmap_log(ROADMAP_INFO, "Entering Jam");
} else if (RoadMapNavigateJammedSince > 0 && gps_speed > RoadMapNavigateMaxJamSpeed) {
RoadMapNavigateJammedSince = 0;
roadmap_log(ROADMAP_INFO, "Leaving Jam");
}
}
}
int roadmap_navigate_is_jammed (PluginLine *line, int *direction) {
PluginLine currentLine;
int currentDirection;
RoadMapGpsPosition pos_curr;
RoadMapPosition pos_end;
int distance_to_end;
if (!RoadMapNavigateJammedSince) {
//roadmap_log (ROADMAP_DEBUG, "Not in jammed state -- not sending data to server");
return 0;
}
if (-1 == roadmap_navigate_get_current (&pos_curr, ¤tLine, ¤tDirection)) {
roadmap_log (ROADMAP_ERROR, "Don't have current location -- can't decide jammed state");
return 0;
}
// check if we are close to segment's end point
if (ROUTE_DIRECTION_AGAINST_LINE == currentDirection) {
roadmap_street_extend_line_ends(¤tLine, &pos_end, NULL, FLAG_EXTEND_FROM, NULL, NULL);
} else {
roadmap_street_extend_line_ends(¤tLine, NULL, &pos_end, FLAG_EXTEND_TO, NULL, NULL);
}
distance_to_end = roadmap_math_distance((RoadMapPosition*)&pos_curr, &pos_end);
roadmap_log (ROADMAP_DEBUG, "Jammed, distance to end of segment id=%d is %d:", currentLine.line_id, distance_to_end);
*line = currentLine;
*direction = currentDirection;
return (distance_to_end > RoadMapNavigateMinJamDistanceFromEnd);
}
void roadmap_navigate_locate (const RoadMapGpsPosition *gps_position, time_t gps_time) {
RoadMapNavCandidates candidates[ROADMAP_NEIGHBOURHOUD];
BOOL candidate_in_route;
BOOL current_in_route = FALSE;
int i;
int count;
RoadMapPosition context_save_pos;
zoom_t context_save_zoom;
static RoadMapPosition last_azymuth_point;
static time_t first_speed_time = 0;
RoadMapFuzzy result;
static RoadMapFuzzy confidence = 0;
static uint32_t confidence_time = 0;
uint32_t now_ms = roadmap_time_get_millis();
int confidence_delta = now_ms - confidence_time;
static RoadMapTracking candidate;
static time_t lost_route_time = 0;
int old_direction = RoadMapConfirmedStreet.line_direction;
int distance_threashold = gps_position->accuracy;
if (distance_threashold < 20)
distance_threashold = 20;
zoom_t zoom = 20;
if (confidence_delta > 1000)
confidence_delta = 1000;
// editor cleanup on first GPS point
if (RoadMapLatestGpsTime == 0) {
editor_cleanup_test( (int)(gps_time - 3 * 24 * 60 * 60), FALSE);
}
if (first_speed_time == 0 && gps_position->speed > roadmap_gps_speed_accuracy()) {
first_speed_time = time (NULL);
}
RoadMapLatestGpsTime = gps_time;
roadmap_navigate_update_jammed_status (gps_position->speed);
roadmap_math_get_context (&context_save_pos, &context_save_zoom);
if ( roadmap_screen_is_hd_screen() )
#ifndef IPHONE_NATIVE
zoom *= 2;
#else
zoom = 33; //TODO: check this logic
#endif //IPHONE_NATIVE
roadmap_math_set_context ((RoadMapPosition *)gps_position, zoom);
roadmap_square_request_location ((const RoadMapPosition *)gps_position);
if (0 && (gps_position->speed < roadmap_gps_speed_accuracy()) &&
first_speed_time > 0 &&
(time(NULL) - first_speed_time > MIN_SPEED_INIT_TIME)) {
#ifdef DEBUG_PRINTS
printf ("speed: %d\n", gps_position->speed);
#endif
RoadMapLatestGpsPosition.speed = gps_position->speed;
if ((RoadMapLatestGpsPosition.latitude == gps_position->latitude) &&
(RoadMapLatestGpsPosition.longitude == gps_position->longitude))
goto ret;
/* Check if last gps position is valid */
if (RoadMapLatestGpsPosition.steering == INVALID_STEERING) {
RoadMapLatestGpsPosition = *gps_position;
last_azymuth_point = *(RoadMapPosition*)gps_position;
roadmap_navigate_set_mobile(gps_position, NULL, 0);
} else {
if ((time(NULL) - RoadMapLatestUpdate) >= 2) {
int distance = roadmap_math_distance(
(RoadMapPosition *) &RoadMapLatestGpsPosition,
(RoadMapPosition *) gps_position);
if (distance > 15) {
if (RoadMapConfirmedStreet.valid) {
RoadMapNeighbour candidate;
if (!roadmap_plugin_get_distance
((RoadMapPosition *)gps_position,
&RoadMapConfirmedLine.line,
&candidate) ||
!roadmap_plugin_same_line
(&candidate.line, &RoadMapConfirmedLine.line)) {
goto ret;
}
RoadMapLatestGpsPosition.longitude = gps_position->longitude;
RoadMapLatestGpsPosition.latitude = gps_position->latitude;
last_azymuth_point = *(RoadMapPosition*)gps_position;
roadmap_navigate_set_mobile(&RoadMapLatestGpsPosition,
&RoadMapConfirmedLine.intersection,
roadmap_math_azymuth(&RoadMapConfirmedLine.from,
&RoadMapConfirmedLine.to)
);