-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroute.c
4741 lines (4299 loc) · 142 KB
/
route.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
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license in the file COPYING
* or http://www.opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file COPYING.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2015 Saso Kiselkov. All rights reserved.
*/
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include "math.h"
#include "perf.h"
#include "log.h"
#include "route.h"
#define DIR_V(hdg) \
(hdg2dir(wmm_mag2true(wmm, (hdg), GEO2_TO_GEO3(cur_pos, 0))))
#define SAME_DIR(a, b) ((a).x * (b).x >= 0 && (a).y * (b).y >= 0)
#define ARC_JOIN_THR 1
#define STD_RATE1_TURN 3
#define STD_RATE2_TURN 1.5
#define STD_INCTP_ANGLE 30
#define INTCP_SRCH_DIST 1e9
#define ARC_START_THRESH 100
static void rlg_connect(route_t *route, route_leg_group_t *prev_rlg,
route_leg_group_t *next_rlg, bool_t allow_mod, bool_t allow_add_legs);
static void rlg_bypass(route_t *route, route_leg_group_t *rlg,
bool_t allow_mod, bool_t allow_add_legs);
static route_leg_t * last_leg_before_rlg(route_t *route,
route_leg_group_t *rlg);
static bool_t navprocs_related(const navproc_t *nv1, const navproc_t *nv2);
typedef geo_pos2_t (*leg_intc_func_t)(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_dir_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_dist_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_radial_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_manual_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_vect_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_alt_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static geo_pos2_t calc_proc_leg_intc(geo_pos2_t cur_pos,
const route_leg_t *rl, const list_t *legs, const wmm_t *wmm);
static bool_t rl_find_leg_seg(const route_leg_t *rl, geo_pos2_t oldpos,
const route_leg_t *next_rl, const wmm_t *wmm, route_seg_t *seg);
static double calc_arc_radius(double speed, double turn_rate);
static route_seg_t *rs_new_direct(geo_pos2_t start, geo_pos2_t end,
route_seg_join_type_t join_type);
static route_seg_t *rs_new_arc(geo_pos2_t start, geo_pos2_t end,
geo_pos2_t center, bool_t cw, route_seg_join_type_t join_type);
static double arc_seg_get_radius(const route_seg_t *rs);
/*
* Functions that compute leg intersections. Order must follow
* navproc_seg_type_t.
*/
static leg_intc_func_t const leg_intc_func_tbl[NAVPROC_SEG_TYPES] = {
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_ARC_TO_FIX */
calc_alt_leg_intc, /* NAVPROC_SEG_TYPE_CRS_TO_ALT */
calc_dist_leg_intc, /* NAVPROC_SEG_TYPE_CRS_TO_DME */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_CRS_TO_FIX */
calc_vect_leg_intc, /* NAVPROC_SEG_TYPE_CRS_TO_INTCP */
calc_radial_leg_intc, /* NAVPROC_SEG_TYPE_CRS_TO_RADIAL */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_DIR_TO_FIX */
calc_alt_leg_intc, /* NAVPROC_SEG_TYPE_FIX_TO_ALT */
calc_dist_leg_intc, /* NAVPROC_SEG_TYPE_FIX_TO_DIST */
calc_dist_leg_intc, /* NAVPROC_SEG_TYPE_FIX_TO_DME */
calc_manual_leg_intc, /* NAVPROC_SEG_TYPE_FIX_TO_MANUAL */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_HOLD_TO_ALT */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_HOLD_TO_FIX */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_HOLD_TO_MANUAL */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_INIT_FIX */
calc_proc_leg_intc, /* NAVPROC_SEG_TYPE_PROC_TURN */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_RADIUS_ARC_TO_FIX */
calc_dir_leg_intc, /* NAVPROC_SEG_TYPE_TRK_TO_FIX */
calc_alt_leg_intc, /* NAVPROC_SEG_TYPE_HDG_TO_ALT */
calc_dist_leg_intc, /* NAVPROC_SEG_TYPE_HDG_TO_DME */
calc_vect_leg_intc, /* NAVPROC_SEG_TYPE_HDG_TO_INTCP */
calc_manual_leg_intc, /* NAVPROC_SEG_TYPE_HDG_TO_MANUAL */
calc_radial_leg_intc /* NAVPROC_SEG_TYPE_HDG_TO_RADIAL */
};
/*
* Destroys and frees a route_seg_t.
*/
static inline void
rs_destroy(route_seg_t *seg)
{
ASSERT(!list_link_active(&seg->route_segs_node));
free(seg);
}
/*
* Destroys and frees a route_leg_t.
*/
static void
rl_destroy(route_leg_t *rl)
{
ASSERT(!list_link_active(&rl->leg_group_legs_node));
ASSERT(!list_link_active(&rl->route_legs_node));
free(rl);
}
/*
* Destroys and frees a route_leg_group_t. This also remove all of the
* leg group's associated legs. It does not handle reconnecting the adjacent
* leg groups.
*/
static void
rlg_destroy(route_t *route, route_leg_group_t *rlg)
{
if (rlg->type == ROUTE_LEG_GROUP_TYPE_DISCO) {
ASSERT(!list_is_empty(&rlg->legs));
}
for (route_leg_t *rl = list_head(&rlg->legs); rl != NULL;
rl = list_head(&rlg->legs)) {
list_remove(&rlg->legs, rl);
list_remove(&route->legs, rl);
rl_destroy(rl);
}
if (list_link_active(&rlg->route_leg_groups_node))
list_remove(&route->leg_groups, rlg);
list_destroy(&rlg->legs);
free(rlg);
}
/*
* Creates a new route leg group of `type' and returns it.
*/
static route_leg_group_t *
rlg_new(route_leg_group_type_t type, route_t *route)
{
route_leg_group_t *rlg = calloc(sizeof (*rlg), 1);
rlg->type = type;
rlg->route = route;
rlg->start_wpt = null_wpt;
rlg->end_wpt = null_wpt;
list_create(&rlg->legs, sizeof (route_leg_t),
offsetof(route_leg_t, leg_group_legs_node));
return (rlg);
}
/*
* Creates a new discontinuity leg group and inserts it after `prev_rlg'.
* A discontinuity cannot be the first or last leg group in a route.
*/
static void
rlg_new_disco(route_t *route, route_leg_group_t *prev_rlg)
{
route_leg_group_t *rlg = rlg_new(ROUTE_LEG_GROUP_TYPE_DISCO, route);
route_leg_t *rl = calloc(sizeof (*rl), 1);
/* A disco can't be first or last in the list */
ASSERT(prev_rlg != NULL);
ASSERT(list_tail(&route->leg_groups) != prev_rlg);
list_insert_after(&route->leg_groups, prev_rlg, rlg);
rl->disco = B_TRUE;
rl->rlg = rlg;
list_insert_head(&rlg->legs, rl);
list_insert_after(&route->legs, last_leg_before_rlg(route, rlg), rl);
}
/*
* Returns a pointer to the end wpt of a particular route leg. If the
* route leg doesn't end in a wpt, returns a pointer to `null_wpt' instead.
*/
static const wpt_t *
leg_get_end_wpt(const route_leg_t *leg)
{
return (!leg->disco ? navproc_seg_get_end_wpt(&leg->seg) : &null_wpt);
}
/*
* Sets the end wpt of `leg' to `wpt'. If the leg is of a type that doesn't
* take wpts, causes an assertion failure.
*/
static void
leg_set_end_wpt(route_leg_t *leg, const wpt_t *wpt)
{
navproc_seg_set_end_wpt(&leg->seg, wpt);
}
static bool_t
chk_awy_fix_adjacent(route_leg_group_t *rlg, const wpt_t *wpt, bool_t head)
{
unsigned i;
ASSERT(rlg->type == ROUTE_LEG_GROUP_TYPE_AIRWAY);
if (IS_NULL_WPT(&rlg->start_wpt) || IS_NULL_WPT(&rlg->end_wpt))
return (B_FALSE);
for (i = 0; i < rlg->awy->num_segs &&
!WPT_EQ(&rlg->awy->segs[i].endpt[0], head ? wpt : &rlg->end_wpt);
i++)
;
return (i < rlg->awy->num_segs &&
WPT_EQ(&rlg->awy->segs[i].endpt[1], head ? &rlg->start_wpt : wpt));
}
/*
* Creates a new DF (direct-to-fix) leg and returns it.
*/
static route_leg_t *
rl_new_direct(const wpt_t *fix, route_leg_group_t *rlg)
{
route_leg_t *rl = calloc(sizeof (*rl), 1);
rl->seg.type = NAVPROC_SEG_TYPE_DIR_TO_FIX;
rl->seg.term_cond.fix = *fix;
rl->rlg = rlg;
return (rl);
}
/*
* Looks for the last route leg occurring before any legs on `rlg' and returns
* it. If there no legs in front of `rlg', returns NULL instead.
*/
static route_leg_t *
last_leg_before_rlg(route_t *route, route_leg_group_t *rlg)
{
for (rlg = list_prev(&route->leg_groups, rlg); rlg != NULL;
rlg = list_prev(&route->leg_groups, rlg)) {
route_leg_t *rl = list_tail(&rlg->legs);
if (rl != NULL)
return (rl);
}
return (NULL);
}
/*
* Checks the leg of a leg group to make sure that it ends at the appropriate
* wpt.
*
* @param route The route to which the leg group belongs.
* @param rlg The route leg group to which the leg is supposed to belong.
* @param rl The route leg to check. If you pass NULL here, the leg will be
* created (as a DF leg) and will be inserted into the rlg leg group
* group following the `prev_rlg_rl' route leg.
* @param prev_rlg_rl The leg in the leg group which should precede the leg
* being checked. If you pass NULL, the new leg will be inserted at the
* start of the leg group's legs. If you pass non-NULL here, it must be
* the same leg as `prev_route_rl' (i.e. legs must be identically
* sequenced in the rlg's leg list as in the route's leg list).
* @param prev_route_rl The leg in the route which should precede the leg
* being checked. If you pass NULL, the new leg will be inserted at the
* start of the route's legs.
*/
static route_leg_t *
rlg_update_leg(route_t *route, route_leg_group_t *rlg, route_leg_t *rl,
const wpt_t *end_wpt, route_leg_t *prev_rlg_rl, route_leg_t *prev_route_rl)
{
if (rl == NULL) {
/* Route leg doesn't exist, recreate & reinsert. */
rl = rl_new_direct(end_wpt, rlg);
list_insert_after(&rlg->legs, prev_rlg_rl, rl);
list_insert_after(&route->legs, prev_route_rl, rl);
} else {
/* Route leg exists, check settings & position in leg list. */
ASSERT(rl->seg.type == NAVPROC_SEG_TYPE_DIR_TO_FIX);
ASSERT(rl != prev_rlg_rl);
ASSERT(rl != prev_route_rl);
if (!WPT_EQ(leg_get_end_wpt(rl), end_wpt)) {
/* End fix incorrect, reset */
leg_set_end_wpt(rl, end_wpt);
route->segs_dirty = B_TRUE;
}
ASSERT(prev_rlg_rl == NULL || prev_rlg_rl == prev_route_rl);
if (list_prev(&rlg->legs, rl) != prev_rlg_rl) {
/* Position in leg group leg sequence incorrect, move */
list_remove(&rlg->legs, rl);
list_insert_after(&rlg->legs, prev_rlg_rl, rl);
route->segs_dirty = B_TRUE;
}
if (list_prev(&route->legs, rl) != prev_route_rl) {
/* Position in route leg sequence incorrect, move */
list_remove(&route->legs, rl);
list_insert_after(&route->legs, prev_route_rl, rl);
route->segs_dirty = B_TRUE;
}
}
return (rl);
}
/*
* Updates the route legs of `rlg' to correspond to the rlg settings.
* This adds/removes route legs as necessary to complete the airway.
*/
static void
rlg_update_awy_legs(route_t *route, route_leg_group_t *rlg, bool_t lookup)
{
ASSERT(rlg->type == ROUTE_LEG_GROUP_TYPE_AIRWAY);
if (lookup) {
const wpt_t *endfix;
const airway_t *awy;
awy = airway_db_lookup(route->navdb->awydb, rlg->awy->name,
!IS_NULL_WPT(&rlg->start_wpt) ? &rlg->start_wpt : NULL,
!IS_NULL_WPT(&rlg->end_wpt) ? rlg->end_wpt.name : NULL,
&endfix);
VERIFY(awy != NULL);
ASSERT(IS_NULL_WPT(&rlg->end_wpt) ||
WPT_EQ(&rlg->end_wpt, endfix));
rlg->awy = awy;
}
if (IS_NULL_WPT(&rlg->start_wpt) || IS_NULL_WPT(&rlg->end_wpt)) {
/*
* If we're missing either the start/end wpt, we can't
* contain any legs, so the flush has changed us.
*/
if (list_head(&rlg->legs) == NULL)
return;
for (route_leg_t *rl = list_head(&rlg->legs); rl != NULL;
rl = list_head(&rlg->legs)) {
list_remove(&route->legs, rl);
list_remove(&rlg->legs, rl);
free(rl);
}
route->segs_dirty = B_TRUE;
} else {
route_leg_t *prev_route_rl = last_leg_before_rlg(route, rlg);
route_leg_t *prev_awy_rl = NULL;
route_leg_t *rl = list_head(&rlg->legs);
unsigned i = 0;
/* Locate the initial airway segment */
for (; i < rlg->awy->num_segs &&
!WPT_EQ(&rlg->start_wpt, &rlg->awy->segs[i].endpt[0]); i++)
;
ASSERT(i < rlg->awy->num_segs);
/* Pass over all airway segments in order & adapt our legs */
for (; i < rlg->awy->num_segs &&
!WPT_EQ(&rlg->end_wpt, &rlg->awy->segs[i].endpt[0]); i++) {
rl = rlg_update_leg(route, rlg, rl,
&rlg->awy->segs[i].endpt[1], prev_awy_rl,
prev_route_rl);
prev_awy_rl = rl;
prev_route_rl = rl;
rl = list_next(&rlg->legs, rl);
}
/* Delete any extraneous legs */
for (rl = list_next(&rlg->legs, prev_awy_rl); rl != NULL;
rl = list_next(&rlg->legs, prev_awy_rl)) {
list_remove(&rlg->legs, rl);
list_remove(&route->legs, rl);
free(rl);
route->segs_dirty = B_TRUE;
}
}
}
/*
* Updates the route leg of a DIRECT rlg to correspond to the rlg's settings.
*/
static void
rlg_update_direct_leg(route_t *route, route_leg_group_t *rlg)
{
ASSERT(rlg->type == ROUTE_LEG_GROUP_TYPE_DIRECT);
(void) rlg_update_leg(route, rlg, list_head(&rlg->legs), &rlg->end_wpt,
NULL, last_leg_before_rlg(route, rlg));
}
/*
* Returns the first non-DISCO route leg following `ref' in list `legs'
* If `ref' is NULL, returns the first leg group (which is never a DISCO).
*/
static route_leg_t *
rl_next_ndisc(const list_t *legs, const route_leg_t *ref)
{
if (ref == NULL)
return (list_head(legs));
for (route_leg_t *rl = list_next(legs, ref); rl;
rl = list_next(legs, rl)) {
if (!rl->disco)
return (rl);
}
return (NULL);
}
/*
* Returns the first non-DISCO route leg group following `ref'. If `ref'
* is NULL, returns the first leg group (which is never a DISCO).
*/
static route_leg_group_t *
rlg_next_ndisc(route_t *route, const route_leg_group_t *ref)
{
if (ref == NULL)
return (list_head(&route->leg_groups));
for (route_leg_group_t *rlg = list_next(&route->leg_groups, ref); rlg;
rlg = list_next(&route->leg_groups, rlg)) {
if (rlg->type != ROUTE_LEG_GROUP_TYPE_DISCO)
return (rlg);
}
return (NULL);
}
/*
* Returns the first non-DISCO route leg group preceding `ref'. If `ref'
* is NULL, returns the last leg group (which is never a DISCO).
*/
static route_leg_group_t *
rlg_prev_ndisc(route_t *route, const route_leg_group_t *ref)
{
if (ref == NULL)
return (list_tail(&route->leg_groups));
for (route_leg_group_t *rlg = list_prev(&route->leg_groups, ref); rlg;
rlg = list_prev(&route->leg_groups, rlg)) {
if (rlg->type != ROUTE_LEG_GROUP_TYPE_DISCO)
return (rlg);
}
return (NULL);
}
/*
* Returns the first non-DISCO route leg preceding `ref'.
*/
static route_leg_t *
rl_prev_ndisc(route_t *route, route_leg_t *ref)
{
for (route_leg_t *rl = list_prev(&route->legs, ref); rl;
rl = list_prev(&route->legs, rl)) {
if (!rl->disco)
return (rl);
}
return (NULL);
}
/*
* Returns the last non-DISCO route leg group, checking that it's not a DISCO.
*/
static route_leg_group_t *
rlg_tail_ndisc(route_t *route)
{
route_leg_group_t *rlg = list_tail(&route->leg_groups);
ASSERT(rlg == NULL || rlg->type != ROUTE_LEG_GROUP_TYPE_DISCO);
return (rlg);
}
static wpt_t
rlg_find_start_fix(const route_leg_group_t *rlg)
{
route_leg_t *rl = list_head(&rlg->legs);
ASSERT(rl != NULL);
return (*navproc_seg_get_start_wpt(&rl->seg));
}
static wpt_t
rlg_find_end_wpt(const route_leg_group_t *rlg)
{
route_leg_t *rl = list_tail(&rlg->legs);
ASSERT(rl != NULL);
return (*navproc_seg_get_end_wpt(&rl->seg));
}
/*
* Returns B_TRUE if the two navprocs are related, B_FALSE otherwise.
* Related navprocs are defined as:
* a) Both belonging to the same airport.
* b) Both being of the same class of navproc. There are two classes
* of navprocs:
* 1) Departure navprocs: SID, SID_COMMON, SID_TRANS.
* 2) Arrival navprocs: STAR, STAR_COMMON, STAR_TRANS, FINAL,
* FINAL_TRANS.
*/
static bool_t
navprocs_related(const navproc_t *nv1, const navproc_t *nv2)
{
return (nv1->arpt == nv2->arpt &&
((nv1->type <= NAVPROC_TYPE_SID_TRANS &&
nv2->type <= NAVPROC_TYPE_SID_TRANS) ||
(nv1->type >= NAVPROC_TYPE_STAR &&
nv2->type >= NAVPROC_TYPE_STAR)));
}
/*
* Given a route let and current altitude, determines what adjustment to the
* altitude is needed to satisfy the route leg's altitude constraint. If the
* leg's altitude is unconstrained, returns the input altitude.
*/
static double
rl_alt_lim_adj(const route_leg_t *rl, double alt)
{
alt_lim_t lim = route_l_get_alt_lim(rl);
switch (lim.type) {
case ALT_LIM_NONE:
return (alt);
case ALT_LIM_AT:
return (lim.alt1);
case ALT_LIM_AT_OR_ABV:
return (MAX(alt, lim.alt1));
case ALT_LIM_AT_OR_BLW:
return (MIN(alt, lim.alt1));
case ALT_LIM_BETWEEN:
return (MAX(MIN(alt, lim.alt1), lim.alt2));
default:
assert(0);
}
}
#define ALT_GUESS_DISPLACE 10.0
/*
* Given a route leg terminating a route segment and a segment starting
* position, attempts to complete the route segment. This function is a
* complement to rl_find_leg_seg. rl_find_leg_seg tries to determine a
* leg segment's starting point from a route leg and an initial aircraft
* position. Then if the leg segment can be completed using the same route
* leg, it passes that leg to rl_complete_seg to fill in the rest of the
* leg segment. Othewise it attempts to pass the *next* route leg following
* the one being examined to try and terminate the leg segment.
*
* This is because route legs can be one of the following:
*
* 1) A leg with a definite starting point but no end point at all
* (i.e. the route leg is just a single point): IF
* 2) A leg with a definite starting point, a direction of travel
* but no end point (i.e. the route leg is an intercept command):
* CI, VI, CR, VR
* 3) A leg with a definite starting point and an end point: AF,
*/
static bool_t
rl_complete_seg(const route_leg_t *rl, geo_pos2_t start, const wmm_t *wmm,
route_seg_t *seg)
{
if (rl->disco)
return (B_FALSE);
switch(rl->seg.type) {
case NAVPROC_SEG_TYPE_ARC_TO_FIX:
seg->type = ROUTE_SEG_TYPE_ARC;
seg->arc.center = rl->seg.leg_cmd.dme_arc.navaid.pos;
seg->arc.start = geo_displace_mag(&wgs84, wmm,
rl->seg.leg_cmd.dme_arc.navaid.pos,
rl->seg.leg_cmd.dme_arc.start_radial,
NM2MET(rl->seg.leg_cmd.dme_arc.radius));
seg->arc.end = rl->seg.term_cond.fix.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_CRS_TO_ALT:
seg->direct.start = start;
seg->direct.end = geo_displace_mag(&wgs84, wmm, start,
rl->seg.leg_cmd.hdg.hdg, NM2MET(ALT_GUESS_DISPLACE));
return (B_TRUE);
case NAVPROC_SEG_TYPE_CRS_TO_DME:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = calc_dist_leg_intc(start, rl, NULL, wmm);
return (B_TRUE);
case NAVPROC_SEG_TYPE_CRS_TO_FIX:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.term_cond.fix.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_CRS_TO_INTCP:
return (B_FALSE);
case NAVPROC_SEG_TYPE_CRS_TO_RADIAL:
case NAVPROC_SEG_TYPE_HDG_TO_RADIAL:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = calc_radial_leg_intc(start, rl, NULL, wmm);
return (B_TRUE);
case NAVPROC_SEG_TYPE_DIR_TO_FIX:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.term_cond.fix.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_FIX_TO_ALT:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = rl->seg.leg_cmd.fix_crs.fix.pos;
seg->direct.end = geo_displace_mag(&wgs84, wmm,
rl->seg.leg_cmd.fix_crs.fix.pos,
rl->seg.leg_cmd.fix_crs.crs, NM2MET(ALT_GUESS_DISPLACE));
return (B_TRUE);
case NAVPROC_SEG_TYPE_FIX_TO_DIST:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = rl->seg.leg_cmd.fix_crs.fix.pos;
seg->direct.end = geo_displace_mag(&wgs84, wmm,
rl->seg.leg_cmd.fix_crs.fix.pos,
rl->seg.leg_cmd.fix_crs.crs,
NM2MET(rl->seg.term_cond.dist));
return (B_TRUE);
case NAVPROC_SEG_TYPE_FIX_TO_DME:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = rl->seg.leg_cmd.fix_crs.fix.pos;
seg->direct.end = calc_dist_leg_intc(start, rl, NULL, wmm);
return (B_TRUE);
case NAVPROC_SEG_TYPE_FIX_TO_MANUAL:
return (B_FALSE);
case NAVPROC_SEG_TYPE_HOLD_TO_ALT:
case NAVPROC_SEG_TYPE_HOLD_TO_FIX:
case NAVPROC_SEG_TYPE_HOLD_TO_MANUAL:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.leg_cmd.hold.wpt.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_INIT_FIX:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.leg_cmd.fix.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_PROC_TURN:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.leg_cmd.proc_turn.startpt.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_RADIUS_ARC_TO_FIX:
return (B_FALSE);
case NAVPROC_SEG_TYPE_TRK_TO_FIX:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = rl->seg.term_cond.fix.pos;
return (B_TRUE);
case NAVPROC_SEG_TYPE_HDG_TO_ALT:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = geo_displace_mag(&wgs84, wmm, start,
rl->seg.leg_cmd.hdg.hdg, NM2MET(ALT_GUESS_DISPLACE));
return (B_TRUE);
case NAVPROC_SEG_TYPE_HDG_TO_DME:
seg->type = ROUTE_SEG_TYPE_DIRECT;
seg->direct.start = start;
seg->direct.end = calc_dist_leg_intc(start, rl, NULL, wmm);
return (B_TRUE);
case NAVPROC_SEG_TYPE_HDG_TO_INTCP:
case NAVPROC_SEG_TYPE_HDG_TO_MANUAL:
return (B_FALSE);
default:
assert(0);
}
}
/*
* Attempts to construct a leg segment for a leg starting at `rl'. This
* requires that the navproc segment type of the leg is one of:
* 1) AF: this type has a definite start & end position.
* 2) FA, FC, FD: this type has a definable end based on a previous
* segment end position.
* 3) IF: this type has a start position. We then attempt to connect it
* to the next leg.
*
* @param rl Current route leg for which to construct the leg segment.
* @param oldpos Previous known aircraft position prior to transitioning to
* the current leg.
* @param next_rl Next route leg following the current. NULL if unknown.
* @param wmm World magnetic model in effect for converting headings.
* @param seg Pointer to a route segment structure that will be filled with
* the leg segment information.
*
* @return B_TRUE if constructing the leg segment succeeded, B_FALSE if not.
*/
static bool_t
rl_find_leg_seg(const route_leg_t *rl, geo_pos2_t oldpos,
const route_leg_t *next_rl, const wmm_t *wmm, route_seg_t *seg)
{
switch (rl->seg.type) {
case NAVPROC_SEG_TYPE_ARC_TO_FIX:
case NAVPROC_SEG_TYPE_FIX_TO_ALT:
case NAVPROC_SEG_TYPE_FIX_TO_DIST:
case NAVPROC_SEG_TYPE_FIX_TO_DME:
return (rl_complete_seg(rl, oldpos, wmm, seg));
case NAVPROC_SEG_TYPE_INIT_FIX:
if (next_rl == NULL)
return (B_FALSE);
return (rl_complete_seg(next_rl, rl->seg.leg_cmd.fix.pos,
wmm, seg));
case NAVPROC_SEG_TYPE_PROC_TURN:
/* TODO */
return (B_FALSE);
default:
return (B_FALSE);
}
}
/*
* Finds the geodetic coordinate midpoint between two geodetic coordinates.
*/
static geo_pos2_t
find_geo_midpoint(geo_pos2_t a, geo_pos2_t b)
{
vect3_t a_v = geo2ecef(GEO2_TO_GEO3(a, 0), &wgs84);
vect3_t b_v = geo2ecef(GEO2_TO_GEO3(b, 0), &wgs84);
vect3_t r_v = vect3_mean(a_v, b_v);
geo_pos3_t r = ecef2geo(r_v, &wgs84);
return (GEO3_TO_GEO2(r));
}
/*
* Determines the best intersection of a flight route and a DME circle.
*/
static geo_pos2_t
find_best_circ_isect(geo_pos2_t cur_pos, double hdg, geo_pos2_t center_pos,
double radius, const wmm_t *wmm)
{
geo_pos2_t midpt = find_geo_midpoint(cur_pos, center_pos);
fpp_t fpp = gnomo_fpp_init(midpt, 0, &wgs84, B_TRUE);
vect2_t cur_pos_v, dir_v, center_v;
vect2_t isects[2];
vect2_t c2i[2]; /* from cur_pos to respective isect point */
vect2_t res;
unsigned n;
cur_pos_v = geo2fpp(cur_pos, &fpp);
dir_v = vect2_set_abs(hdg2dir(wmm_mag2true(wmm, hdg,
GEO2_TO_GEO3(cur_pos, 0))), INTCP_SRCH_DIST);
center_v = geo2fpp(center_pos, &fpp);
n = vect2circ_isect(dir_v, cur_pos_v, center_v, radius, B_TRUE, isects);
if (n == 0)
return (NULL_GEO_POS2);
if (n == 1) {
c2i[0] = vect2_sub(isects[0], cur_pos_v);
if (!SAME_DIR(c2i[0], dir_v))
return (NULL_GEO_POS2);
res = isects[0];
} else {
c2i[0] = vect2_sub(isects[0], cur_pos_v);
c2i[1] = vect2_sub(isects[1], cur_pos_v);
if (vect2_abs(c2i[0]) < vect2_abs(c2i[1]) &&
SAME_DIR(c2i[0], dir_v))
res = isects[0];
else if (SAME_DIR(c2i[1], dir_v))
res = isects[1];
else
return (NULL_GEO_POS2);
}
return (fpp2geo(res, &fpp));
}
static geo_pos2_t
calc_manual_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
UNUSED(cur_pos);
UNUSED(rl);
UNUSED(legs);
UNUSED(wmm);
ASSERT(rl->seg.type == NAVPROC_SEG_TYPE_FIX_TO_MANUAL ||
rl->seg.type == NAVPROC_SEG_TYPE_HDG_TO_MANUAL);
return (NULL_GEO_POS2);
}
static geo_pos2_t
calc_dir_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
UNUSED(cur_pos);
UNUSED(legs);
UNUSED(wmm);
switch (rl->seg.type) {
case NAVPROC_SEG_TYPE_ARC_TO_FIX:
case NAVPROC_SEG_TYPE_CRS_TO_FIX:
case NAVPROC_SEG_TYPE_DIR_TO_FIX:
case NAVPROC_SEG_TYPE_RADIUS_ARC_TO_FIX:
case NAVPROC_SEG_TYPE_TRK_TO_FIX:
return (rl->seg.term_cond.fix.pos);
case NAVPROC_SEG_TYPE_INIT_FIX:
return (rl->seg.leg_cmd.fix.pos);
case NAVPROC_SEG_TYPE_HOLD_TO_ALT:
case NAVPROC_SEG_TYPE_HOLD_TO_FIX:
case NAVPROC_SEG_TYPE_HOLD_TO_MANUAL:
return (rl->seg.leg_cmd.hold.wpt.pos);
default:
assert(0);
}
}
static geo_pos2_t
calc_dist_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
double hdg, dist; /* hdg in deg, dist in NM */
geo_pos2_t center;
UNUSED(legs);
if (IS_NULL_GEO_POS(cur_pos)) {
/* Can't resolve without a starting point */
openfmc_log(OPENFMC_LOG_ERR, "Cannot resolve %s leg to "
"%s/%.1lf: missing start pos",
navproc_seg_type2str(rl->seg.type),
rl->seg.term_cond.dme.navaid.name,
rl->seg.term_cond.dme.dist);
return (NULL_GEO_POS2);
}
ASSERT(rl->seg.type == NAVPROC_SEG_TYPE_CRS_TO_RADIAL ||
rl->seg.type == NAVPROC_SEG_TYPE_FIX_TO_DIST ||
rl->seg.type == NAVPROC_SEG_TYPE_FIX_TO_DME ||
rl->seg.type == NAVPROC_SEG_TYPE_HDG_TO_DME);
if (rl->seg.type == NAVPROC_SEG_TYPE_CRS_TO_DME) {
hdg = rl->seg.leg_cmd.hdg.hdg;
center = rl->seg.term_cond.dme.navaid.pos;
dist = rl->seg.term_cond.dme.dist;
} else if (rl->seg.type == NAVPROC_SEG_TYPE_FIX_TO_DIST) {
cur_pos = rl->seg.leg_cmd.fix_crs.fix.pos;
hdg = rl->seg.leg_cmd.fix_crs.crs;
center = rl->seg.leg_cmd.fix_crs.fix.pos;
dist = rl->seg.term_cond.dist;
} else if (rl->seg.type == NAVPROC_SEG_TYPE_FIX_TO_DME) {
cur_pos = rl->seg.leg_cmd.fix_crs.fix.pos;
hdg = rl->seg.leg_cmd.fix_crs.crs;
center = rl->seg.term_cond.dme.navaid.pos;
dist = rl->seg.term_cond.dme.dist;
} else { /* NAVPROC_SEG_TYPE_HDG_TO_DME */
hdg = rl->seg.leg_cmd.hdg.hdg;
center = rl->seg.term_cond.dme.navaid.pos;
dist = rl->seg.term_cond.dme.dist;
}
return (find_best_circ_isect(cur_pos, hdg, center, dist, wmm));
}
static geo_pos2_t
calc_radial_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
fpp_t fpp = gnomo_fpp_init(find_geo_midpoint(cur_pos,
rl->seg.term_cond.radial.navaid.pos), 0, &wgs84, B_TRUE);
vect2_t dir_v, cur_pos_v, navaid_v, radial_dir_v, isect, c2i;
UNUSED(legs);
ASSERT(rl->seg.type == NAVPROC_SEG_TYPE_CRS_TO_RADIAL ||
rl->seg.type == NAVPROC_SEG_TYPE_HDG_TO_RADIAL);
dir_v = DIR_V(rl->seg.leg_cmd.hdg.hdg);
radial_dir_v = DIR_V(rl->seg.term_cond.radial.radial);
navaid_v = geo2fpp(rl->seg.term_cond.radial.navaid.pos, &fpp);
cur_pos_v = geo2fpp(GEO3_TO_GEO2(cur_pos), &fpp);
isect = vect2vect_isect(dir_v, cur_pos_v, radial_dir_v, navaid_v,
B_FALSE);
c2i = vect2_sub(isect, cur_pos_v);
/* Check intersection exists and lies in direction of travel. */
if (!IS_NULL_VECT(isect) && SAME_DIR(c2i, dir_v)) {
return (fpp2geo(isect, &fpp));
} else {
return (NULL_GEO_POS2);
}
}
static geo_pos2_t
vect_seg_intc(geo_pos2_t cur_pos, double hdg, route_seg_t *next_rs,
const wmm_t *wmm)
{
if (next_rs->type == ROUTE_SEG_TYPE_DIRECT) {
fpp_t fpp = gnomo_fpp_init(GEO3_TO_GEO2(cur_pos), 0, &wgs84,
B_TRUE);
vect2_t cur_pos_v, dir_v, start_v, end_v, s2e, isect;
cur_pos_v = geo2fpp(cur_pos, &fpp);
start_v = geo2fpp(next_rs->direct.start, &fpp);
end_v = geo2fpp(next_rs->direct.end, &fpp);
s2e = vect2_sub(end_v, start_v);
dir_v = vect2_set_abs(DIR_V(hdg), INTCP_SRCH_DIST);
isect = vect2vect_isect(cur_pos_v, dir_v, start_v, s2e, B_TRUE);
if (IS_NULL_VECT(isect))
return (NULL_GEO_POS2);
return (fpp2geo(isect, &fpp));
} else {
ASSERT(next_rs->type == ROUTE_SEG_TYPE_ARC);
return (find_best_circ_isect(cur_pos, hdg, next_rs->arc.center,
arc_seg_get_radius(next_rs), wmm));
}
}
static geo_pos2_t
calc_vect_leg_intc(const geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
const route_leg_t *next_rl = rl_next_ndisc(legs, rl);
route_seg_t next_rs;
double hdg;
if (IS_NULL_GEO_POS(cur_pos) || next_rl == NULL)
return (NULL_GEO_POS2);
ASSERT(rl->seg.type == NAVPROC_SEG_TYPE_CRS_TO_INTCP ||
rl->seg.type == NAVPROC_SEG_TYPE_HDG_TO_INTCP);
hdg = rl->seg.leg_cmd.hdg.hdg;
if (!rl_find_leg_seg(next_rl, cur_pos, rl_next_ndisc(legs, next_rl),
wmm, &next_rs))
return (NULL_GEO_POS2);
return (vect_seg_intc(cur_pos, rl->seg.leg_cmd.hdg.hdg, &next_rs, wmm));
}
static geo_pos2_t
calc_alt_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
/* TODO */
UNUSED(cur_pos);
UNUSED(rl);
UNUSED(legs);
UNUSED(wmm);
return (NULL_GEO_POS2);
}
static geo_pos2_t
calc_proc_leg_intc(geo_pos2_t cur_pos, const route_leg_t *rl,
const list_t *legs, const wmm_t *wmm)
{
/* TODO */
UNUSED(cur_pos);
UNUSED(rl);
UNUSED(legs);
UNUSED(wmm);
return (NULL_GEO_POS2);
}
static geo_pos2_t
calc_leg_start_pos(const route_leg_t *targ_rl, const route_leg_t *rl,
geo_pos2_t cur_pos, const list_t *legs)
{
#define PROJ_LIMIT 100000
const route_leg_group_t *rlg = targ_rl->rlg;
const route_t *route = rlg->route;
ASSERT(targ_rl != NULL && rl != NULL);
ASSERT(!IS_NULL_GEO_POS(cur_pos));
for (; rl != targ_rl; rl = list_next(legs, rl)) {
ASSERT(rl != NULL);
if (rl->disco) {
cur_pos = NULL_GEO_POS2;
continue;
}
ASSERT(leg_intc_func_tbl[rl->seg.type] != NULL);
cur_pos = leg_intc_func_tbl[rl->seg.type](cur_pos, rl,
legs, route->navdb->wmm);
}
return (cur_pos);
}
/*
* Looks for the first suitable starting position on a route for leg segment
* computation. Suitable starting positions are (in order of preference):
* 1) departure runway threshold
* 2) departure airport reference point
* 3) any rlg up to lim_rlg that has a defined start_wpt
* If a starting position is found, it is returned, otherwise NULL_GEO_POS3
* is returned.
*/
static void
route_first_start_pos(const route_t *route, const route_leg_group_t *lim_rlg,
geo_pos3_t *posp, double *hdgp, const route_leg_group_t **rlg_start)
{
double alt = 0;
ASSERT(posp != NULL);
if (hdgp != NULL)
*hdgp = 0;
if (route->dep_rwy != NULL) {
if (rlg_start != NULL)
*rlg_start = NULL;
*posp = route->dep_rwy->thr_pos;
if (hdgp != NULL)
*hdgp = route->dep_rwy->hdg;
return;