-
Notifications
You must be signed in to change notification settings - Fork 85
/
viewshed.py
1675 lines (1356 loc) · 55.5 KB
/
viewshed.py
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
from math import atan, fabs
from math import pi as PI
from math import sqrt
from typing import Union
import numpy as np
import xarray
from .gpu_rtx import has_rtx
from .utils import has_cuda_and_cupy, is_cupy_array, ngjit
E_ROW_ID = 0
E_COL_ID = 1
E_TYPE_ID = 2
E_ANG_ID = 3
E_ELEV_0 = 4
E_ELEV_1 = 5
E_ELEV_2 = 6
AE_ANG_ID = 0
AE_ELEV_0 = 1
AE_ELEV_1 = 2
AE_ELEV_2 = 3
TN_KEY_ID = 0
TN_GRAD_0 = 1
TN_GRAD_1 = 2
TN_GRAD_2 = 3
TN_ANG_0 = 4
TN_ANG_1 = 5
TN_ANG_2 = 6
TN_MAX_GRAD_ID = 7
TN_COLOR_ID = 0
TN_LEFT_ID = 1
TN_RIGHT_ID = 2
TN_PARENT_ID = 3
NIL_ID = -1
# view options default values
OBS_ELEV = 0
TARGET_ELEV = 0
# if a cell is invisible, its value is set to -1
INVISIBLE = -1
# color of node in red-black Tree
RB_RED = 0
RB_BLACK = 1
# event type
ENTERING_EVENT = 1
EXITING_EVENT = -1
CENTER_EVENT = 0
# this value is returned by findMaxValueWithinDist() if there is no key within
# that distance
SMALLEST_GRAD = -9999999999999999999999.0
@ngjit
def _compare(a, b):
if a < b:
return -1
if a > b:
return 1
return 0
@ngjit
def _find_value_min_value(tree_vals, node_id):
return min(tree_vals[node_id][TN_GRAD_0],
tree_vals[node_id][TN_GRAD_1],
tree_vals[node_id][TN_GRAD_2])
def _print_tree(status_struct):
for i in range(len(status_struct)):
print(i, status_struct[i][0])
def _print_tv(tv):
print('key=', tv[TN_KEY_ID],
'grad=', tv[TN_GRAD_0], tv[TN_GRAD_1], tv[TN_GRAD_2],
'ang=', tv[TN_ANG_0], tv[TN_ANG_1], tv[TN_ANG_2],
'max_grad=', tv[TN_MAX_GRAD_ID])
return
@ngjit
def _create_tree_nodes(tree_vals, tree_nodes, x, val, color=RB_RED):
# Create a TreeNode using given TreeValue
# every node has null nodes as children initially, create one such object
# for easy management
tree_vals[x][TN_KEY_ID] = val[TN_KEY_ID]
tree_vals[x][TN_GRAD_0] = val[TN_GRAD_0]
tree_vals[x][TN_GRAD_1] = val[TN_GRAD_1]
tree_vals[x][TN_GRAD_2] = val[TN_GRAD_2]
tree_vals[x][TN_ANG_0] = val[TN_ANG_0]
tree_vals[x][TN_ANG_1] = val[TN_ANG_1]
tree_vals[x][TN_ANG_2] = val[TN_ANG_2]
tree_vals[x][TN_MAX_GRAD_ID] = SMALLEST_GRAD
tree_nodes[x][TN_COLOR_ID] = color
tree_nodes[x][TN_LEFT_ID] = NIL_ID
tree_nodes[x][TN_RIGHT_ID] = NIL_ID
tree_nodes[x][TN_PARENT_ID] = NIL_ID
return
@ngjit
def _tree_minimum(tree_nodes, x):
while tree_nodes[x][TN_LEFT_ID] != NIL_ID:
x = tree_nodes[x][TN_LEFT_ID]
return x
# function used by deletion
@ngjit
def _tree_successor(tree_nodes, x):
# Find the highest successor of a node in the tree
if tree_nodes[x][TN_RIGHT_ID] != NIL_ID:
return _tree_minimum(tree_nodes, tree_nodes[x][TN_RIGHT_ID])
y = tree_nodes[x][TN_PARENT_ID]
while y != NIL_ID and x == tree_nodes[y][TN_RIGHT_ID]:
x = y
if tree_nodes[y][TN_PARENT_ID] == NIL_ID:
return y
y = tree_nodes[y][TN_PARENT_ID]
return y
@ngjit
def _find_max_value(node_value):
# Find the max value in the given tree.
return node_value[TN_MAX_GRAD_ID]
@ngjit
def _left_rotate(tree_vals, tree_nodes, root, x):
# A utility function to left rotate subtree rooted with a node.
y = tree_nodes[x][TN_RIGHT_ID]
# fix x
x_left = tree_nodes[x][TN_LEFT_ID]
y_left = tree_nodes[y][TN_LEFT_ID]
if tree_vals[x_left][TN_MAX_GRAD_ID] > tree_vals[y_left][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[x_left][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[y_left][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, x)
if tmp_max > min_value:
tree_vals[x][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[x][TN_MAX_GRAD_ID] = min_value
# fix y
y_right = tree_nodes[y][TN_RIGHT_ID]
if tree_vals[x][TN_MAX_GRAD_ID] > tree_vals[y_right][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[x][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[y_right][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, y)
if tmp_max > min_value:
tree_vals[y][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[y][TN_MAX_GRAD_ID] = min_value
# left rotation
# see pseudo code on page 278 CLRS
# turn y's left subtree into x's right subtree
tree_nodes[x][TN_RIGHT_ID] = tree_nodes[y][TN_LEFT_ID]
y_left = tree_nodes[y][TN_LEFT_ID]
tree_nodes[y_left][TN_PARENT_ID] = x
# link x's parent to y
tree_nodes[y][TN_PARENT_ID] = tree_nodes[x][TN_PARENT_ID]
if tree_nodes[x][TN_PARENT_ID] == NIL_ID:
root = y
else:
x_parent = tree_nodes[x][TN_PARENT_ID]
if x == tree_nodes[x_parent][TN_LEFT_ID]:
tree_nodes[x_parent][TN_LEFT_ID] = y
else:
tree_nodes[x_parent][TN_RIGHT_ID] = y
tree_nodes[y][TN_LEFT_ID] = x
tree_nodes[x][TN_PARENT_ID] = y
return root
@ngjit
def _right_rotate(tree_vals, tree_nodes, root, y):
# A utility function to right rotate subtree rooted with a node.
x = tree_nodes[y][TN_LEFT_ID]
# fix y
x_right = tree_nodes[x][TN_RIGHT_ID]
y_right = tree_nodes[y][TN_RIGHT_ID]
if tree_vals[x_right][TN_MAX_GRAD_ID] > tree_vals[y_right][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[x_right][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[y_right][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, y)
if tmp_max > min_value:
tree_vals[y][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[y][TN_MAX_GRAD_ID] = min_value
# fix x
x_left = tree_nodes[x][TN_LEFT_ID]
if tree_vals[x_left][TN_MAX_GRAD_ID] > tree_vals[y][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[x_left][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[y][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, x)
if tmp_max > min_value:
tree_vals[x][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[x][TN_MAX_GRAD_ID] = min_value
# rotation
tree_nodes[y][TN_LEFT_ID] = tree_nodes[x][TN_RIGHT_ID]
x_right = tree_nodes[x][TN_RIGHT_ID]
tree_nodes[x_right][TN_PARENT_ID] = y
tree_nodes[x][TN_PARENT_ID] = tree_nodes[y][TN_PARENT_ID]
if tree_nodes[y][TN_PARENT_ID] == NIL_ID:
root = x
else:
y_parent = tree_nodes[y][TN_PARENT_ID]
if tree_nodes[y_parent][TN_LEFT_ID] == y:
tree_nodes[y_parent][TN_LEFT_ID] = x
else:
tree_nodes[y_parent][TN_RIGHT_ID] = x
tree_nodes[x][TN_RIGHT_ID] = y
tree_nodes[y][TN_PARENT_ID] = x
return root
@ngjit
def _rb_insert_fixup(tree_vals, tree_nodes, root, z):
# Fix red-black tree after insertion. This may change the root pointer.
# see pseudocode on page 281 in CLRS
z_parent = tree_nodes[z][TN_PARENT_ID]
while tree_nodes[z_parent][TN_COLOR_ID] == RB_RED:
z_parent_parent = tree_nodes[z_parent][TN_PARENT_ID]
n1 = tree_nodes[z][TN_PARENT_ID]
n2 = tree_nodes[z_parent_parent][TN_LEFT_ID]
if n1 == n2:
y = tree_nodes[z_parent_parent][TN_RIGHT_ID]
if tree_nodes[y][TN_COLOR_ID] == RB_RED:
# case 1
tree_nodes[z_parent][TN_COLOR_ID] = RB_BLACK
tree_nodes[y][TN_COLOR_ID] = RB_BLACK
tree_nodes[z_parent_parent][TN_COLOR_ID] = RB_RED
# re assignment for z
z = z_parent_parent
else:
if z == tree_nodes[z_parent][TN_RIGHT_ID]:
# case 2
z = z_parent
# convert case 2 to case 3
root = _left_rotate(tree_vals, tree_nodes, root, z)
# case 3
z_parent = tree_nodes[z][TN_PARENT_ID]
z_parent_parent = tree_nodes[z_parent][TN_PARENT_ID]
tree_nodes[z_parent][TN_COLOR_ID] = RB_BLACK
tree_nodes[z_parent_parent][TN_COLOR_ID] = RB_RED
root = _right_rotate(tree_vals, tree_nodes, root,
z_parent_parent)
else:
# (z->parent == z->parent->parent->right)
y = tree_nodes[z_parent_parent][TN_LEFT_ID]
if tree_nodes[y][TN_COLOR_ID] == RB_RED:
# case 1
tree_nodes[z_parent][TN_COLOR_ID] = RB_BLACK
tree_nodes[y][TN_COLOR_ID] = RB_BLACK
tree_nodes[z_parent_parent][TN_COLOR_ID] = RB_RED
z = z_parent_parent
else:
if z == tree_nodes[z_parent][TN_LEFT_ID]:
# case 2
z = z_parent
# convert case 2 to case 3
root = _right_rotate(tree_vals, tree_nodes, root, z)
# case 3
z_parent = tree_nodes[z][TN_PARENT_ID]
z_parent_parent = tree_nodes[z_parent][TN_PARENT_ID]
tree_nodes[z_parent][TN_COLOR_ID] = RB_BLACK
tree_nodes[z_parent_parent][TN_COLOR_ID] = RB_RED
root = _left_rotate(tree_vals, tree_nodes, root,
z_parent_parent)
z_parent = tree_nodes[z][TN_PARENT_ID]
tree_nodes[root][TN_COLOR_ID] = RB_BLACK
return root
@ngjit
def _insert_into_tree(tree_vals, tree_nodes, root, node_id, value):
# Create node and insert it into the tree
cur_node = root
if _compare(value[TN_KEY_ID], tree_vals[cur_node][TN_KEY_ID]) == -1:
next_node = tree_nodes[cur_node][TN_LEFT_ID]
else:
next_node = tree_nodes[cur_node][TN_RIGHT_ID]
while next_node != NIL_ID:
cur_node = next_node
if _compare(value[TN_KEY_ID], tree_vals[cur_node][TN_KEY_ID]) == -1:
next_node = tree_nodes[cur_node][TN_LEFT_ID]
else:
next_node = tree_nodes[cur_node][TN_RIGHT_ID]
# create a new node
# and place it at the right place
# created node is RED by default
_create_tree_nodes(tree_vals, tree_nodes, node_id, value, color=RB_RED)
next_node = node_id
tree_nodes[next_node][TN_PARENT_ID] = cur_node
if _compare(value[TN_KEY_ID], tree_vals[cur_node][TN_KEY_ID]) == -1:
tree_nodes[cur_node][TN_LEFT_ID] = next_node
else:
tree_nodes[cur_node][TN_RIGHT_ID] = next_node
inserted = next_node
# update augmented maxGradient
tree_vals[next_node][TN_MAX_GRAD_ID] =\
_find_value_min_value(tree_vals, next_node)
while tree_nodes[next_node][TN_PARENT_ID] != NIL_ID:
next_parent = tree_nodes[next_node][TN_PARENT_ID]
if tree_vals[next_parent][TN_MAX_GRAD_ID] <\
tree_vals[next_node][TN_MAX_GRAD_ID]:
tree_vals[next_parent][TN_MAX_GRAD_ID] =\
tree_vals[next_node][TN_MAX_GRAD_ID]
if tree_vals[next_parent][TN_MAX_GRAD_ID] >\
tree_vals[next_node][TN_MAX_GRAD_ID]:
break
next_node = next_parent
# fix rb tree after insertion
root = _rb_insert_fixup(tree_vals, tree_nodes, root, inserted)
return root
@ngjit
def _search_for_node(tree_vals, tree_nodes, root, key):
# Search for a node with a given key.
cur_node = root
while cur_node != NIL_ID and \
_compare(key, tree_vals[cur_node][TN_KEY_ID]) != 0:
if _compare(key, tree_vals[cur_node][TN_KEY_ID]) == -1:
cur_node = tree_nodes[cur_node][TN_LEFT_ID]
else:
cur_node = tree_nodes[cur_node][TN_RIGHT_ID]
return cur_node
# The following is designed for viewshed's algorithm
@ngjit
def _find_max_value_within_key(tree_vals, tree_nodes, root,
max_key, ang, gradient):
key_node = _search_for_node(tree_vals, tree_nodes, root, max_key)
if key_node == NIL_ID:
# there is no point in the structure with key < maxKey */
return SMALLEST_GRAD
cur_node = key_node
max = SMALLEST_GRAD
while tree_nodes[cur_node][TN_PARENT_ID] != NIL_ID:
cur_parent = tree_nodes[cur_node][TN_PARENT_ID]
if cur_node == tree_nodes[cur_parent][TN_RIGHT_ID]:
# its the right node of its parent
cur_parent_left = tree_nodes[cur_parent][TN_LEFT_ID]
tmp_max = _find_max_value(tree_vals[cur_parent_left])
if tmp_max > max:
max = tmp_max
min_value = _find_value_min_value(tree_vals, cur_parent)
if min_value > max:
max = min_value
cur_node = cur_parent
if max > gradient:
return max
# traverse all nodes with smaller distance
max = SMALLEST_GRAD
cur_node = key_node
while cur_node != NIL_ID:
check_me = False
if tree_vals[cur_node][TN_ANG_0] <= ang\
<= tree_vals[cur_node][TN_ANG_2]:
check_me = True
if (not check_me) and tree_vals[cur_node][TN_KEY_ID] > 0:
print('Angles outside angle')
if tree_vals[cur_node][TN_KEY_ID] > max_key:
raise ValueError("current dist too large ")
if check_me and cur_node != key_node:
if ang < tree_vals[cur_node][TN_ANG_1]:
cur_grad = tree_vals[cur_node][TN_GRAD_1] \
+ (tree_vals[cur_node][TN_GRAD_0]
- tree_vals[cur_node][TN_GRAD_1]) \
* (tree_vals[cur_node][TN_ANG_1] - ang) \
/ (tree_vals[cur_node][TN_ANG_1]
- tree_vals[cur_node][TN_ANG_0])
elif ang > tree_vals[cur_node][TN_ANG_1]:
cur_grad = tree_vals[cur_node][TN_GRAD_1] \
+ (tree_vals[cur_node][TN_GRAD_2]
- tree_vals[cur_node][TN_GRAD_1]) \
* (ang - tree_vals[cur_node][TN_ANG_1]) \
/ (tree_vals[cur_node][TN_ANG_2]
- tree_vals[cur_node][TN_ANG_1])
else:
cur_grad = tree_vals[cur_node][TN_GRAD_1]
if cur_grad > max:
max = cur_grad
if max > gradient:
return max
# get next smaller key
if tree_nodes[cur_node][TN_LEFT_ID] != NIL_ID:
cur_node = tree_nodes[cur_node][TN_LEFT_ID]
while tree_nodes[cur_node][TN_RIGHT_ID] != NIL_ID:
cur_node = tree_nodes[cur_node][TN_RIGHT_ID]
else:
# at smallest item in this branch, go back up
last_node = cur_node
cur_node = tree_nodes[cur_node][TN_PARENT_ID]
while cur_node != NIL_ID and \
last_node == tree_nodes[cur_node][TN_LEFT_ID]:
last_node = cur_node
cur_node = tree_nodes[cur_node][TN_PARENT_ID]
return max
@ngjit
def _rb_delete_fixup(tree_vals, tree_nodes, root, x):
# Fix the red-black tree after deletion.
# This may change the root pointer.
while x != root and tree_nodes[x][TN_COLOR_ID] == RB_BLACK:
x_parent = tree_nodes[x][TN_PARENT_ID]
if x == tree_nodes[x_parent][TN_LEFT_ID]:
w = tree_nodes[x_parent][TN_RIGHT_ID]
if tree_nodes[w][TN_COLOR_ID] == RB_RED:
tree_nodes[w][TN_COLOR_ID] = RB_BLACK
tree_nodes[x_parent][TN_COLOR_ID] = RB_RED
root = _left_rotate(tree_vals, tree_nodes, root, x_parent)
w = tree_nodes[x_parent][TN_RIGHT_ID]
if w == NIL_ID:
x = tree_nodes[x][TN_PARENT_ID]
continue
w_left = tree_nodes[w][TN_LEFT_ID]
w_right = tree_nodes[w][TN_RIGHT_ID]
if tree_nodes[w_left][TN_COLOR_ID] == RB_BLACK and \
tree_nodes[w_right][TN_COLOR_ID] == RB_BLACK:
tree_nodes[w][TN_COLOR_ID] = RB_RED
x = tree_nodes[x][TN_PARENT_ID]
else:
if tree_nodes[w_right][TN_COLOR_ID] == RB_BLACK:
tree_nodes[w_left][TN_COLOR_ID] = RB_BLACK
tree_nodes[w][TN_COLOR_ID] = RB_RED
root = _right_rotate(tree_vals, tree_nodes, root, w)
x_parent = tree_nodes[x][TN_PARENT_ID]
w = tree_nodes[x_parent][TN_RIGHT_ID]
x_parent = tree_nodes[x][TN_PARENT_ID]
w_right = tree_nodes[w][TN_RIGHT_ID]
tree_nodes[w][TN_COLOR_ID] = tree_nodes[x_parent][TN_COLOR_ID]
tree_nodes[x_parent][TN_COLOR_ID] = RB_BLACK
tree_nodes[w_right][TN_COLOR_ID] = RB_BLACK
root = _left_rotate(tree_vals, tree_nodes, root, x_parent)
x = root
else:
# x == x.parent.right
x_parent = tree_nodes[x][TN_PARENT_ID]
w = tree_nodes[x_parent][TN_LEFT_ID]
if tree_nodes[w][TN_COLOR_ID] == RB_RED:
tree_nodes[w][TN_COLOR_ID] = RB_BLACK
tree_nodes[x_parent][TN_COLOR_ID] = RB_RED
root = _right_rotate(tree_vals, tree_nodes, root, x_parent)
w = tree_nodes[x_parent][TN_LEFT_ID]
if w == NIL_ID:
x = x_parent
continue
w_left = tree_nodes[w][TN_LEFT_ID]
w_right = tree_nodes[w][TN_RIGHT_ID]
# do we need re-assignment here? No changes has been made for x?
x_parent = tree_nodes[x][TN_PARENT_ID]
if tree_nodes[w_right][TN_COLOR_ID] == RB_BLACK and \
tree_nodes[w_left][TN_COLOR_ID] == RB_BLACK:
tree_nodes[w][TN_COLOR_ID] = RB_RED
x = x_parent
else:
if tree_nodes[w_left][TN_COLOR_ID] == RB_BLACK:
tree_nodes[w_right][TN_COLOR_ID] = RB_BLACK
tree_nodes[w][TN_COLOR_ID] = RB_RED
root = _left_rotate(tree_vals, tree_nodes, root, w)
w = tree_nodes[x_parent][TN_LEFT_ID]
tree_nodes[w][TN_COLOR_ID] = tree_nodes[x_parent][TN_COLOR_ID]
tree_nodes[x_parent][TN_COLOR_ID] = RB_BLACK
w_left = tree_nodes[w][TN_LEFT_ID]
tree_nodes[w_left][TN_COLOR_ID] = RB_BLACK
root = _right_rotate(tree_vals, tree_nodes, root, x_parent)
x = root
tree_nodes[x][TN_COLOR_ID] = RB_BLACK
return root
@ngjit
def _delete_from_tree(tree_vals, tree_nodes, root, key):
# Delete the node out of the tree. This may change the root pointer.
z = _search_for_node(tree_vals, tree_nodes, root, key)
if z == NIL_ID:
# node to delete is not found
raise ValueError("node not found")
# 1-3
if tree_nodes[z][TN_LEFT_ID] == NIL_ID or\
tree_nodes[z][TN_RIGHT_ID] == NIL_ID:
y = z
else:
y = _tree_successor(tree_nodes, z)
if y == NIL_ID:
raise ValueError("successor not found")
deleted = y
# 4-6
if tree_nodes[y][TN_LEFT_ID] != NIL_ID:
x = tree_nodes[y][TN_LEFT_ID]
else:
x = tree_nodes[y][TN_RIGHT_ID]
# 7
tree_nodes[x][TN_PARENT_ID] = tree_nodes[y][TN_PARENT_ID]
# 8-12
if tree_nodes[y][TN_PARENT_ID] == NIL_ID:
root = x
# augmentation to be fixed
to_fix = root
else:
y_parent = tree_nodes[y][TN_PARENT_ID]
if y == tree_nodes[y_parent][TN_LEFT_ID]:
tree_nodes[y_parent][TN_LEFT_ID] = x
else:
tree_nodes[y_parent][TN_RIGHT_ID] = x
# augmentation to be fixed
to_fix = y_parent
# fix augmentation for removing y
cur_node = y
while tree_nodes[cur_node][TN_PARENT_ID] != NIL_ID:
cur_parent = tree_nodes[cur_node][TN_PARENT_ID]
if tree_vals[cur_parent][TN_MAX_GRAD_ID] == \
_find_value_min_value(tree_vals, y):
cur_parent_left = tree_nodes[cur_parent][TN_LEFT_ID]
cur_parent_right = tree_nodes[cur_parent][TN_RIGHT_ID]
left = _find_max_value(tree_vals[cur_parent_left])
right = _find_max_value(tree_vals[cur_parent_right])
if left > right:
tree_vals[cur_parent][TN_MAX_GRAD_ID] = left
else:
tree_vals[cur_parent][TN_MAX_GRAD_ID] = right
min_value = _find_value_min_value(tree_vals, cur_parent)
if min_value > tree_vals[cur_parent][TN_MAX_GRAD_ID]:
tree_vals[cur_parent][TN_MAX_GRAD_ID] = min_value
else:
break
cur_node = cur_parent
# fix augmentation for x
to_fix_left = tree_nodes[to_fix][TN_LEFT_ID]
to_fix_right = tree_nodes[to_fix][TN_RIGHT_ID]
if tree_vals[to_fix_left][TN_MAX_GRAD_ID] >\
tree_vals[to_fix_right][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[to_fix_left][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[to_fix_right][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, to_fix)
if tmp_max > min_value:
tree_vals[to_fix][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[to_fix][TN_MAX_GRAD_ID] = min_value
# 13-15
if y != NIL_ID and y != z:
z_gradient = _find_value_min_value(tree_vals, z)
tree_vals[z][TN_KEY_ID] = tree_vals[y][TN_KEY_ID]
tree_vals[z][TN_GRAD_0] = tree_vals[y][TN_GRAD_0]
tree_vals[z][TN_GRAD_1] = tree_vals[y][TN_GRAD_1]
tree_vals[z][TN_GRAD_2] = tree_vals[y][TN_GRAD_2]
tree_vals[z][TN_ANG_0] = tree_vals[y][TN_ANG_0]
tree_vals[z][TN_ANG_1] = tree_vals[y][TN_ANG_1]
tree_vals[z][TN_ANG_2] = tree_vals[y][TN_ANG_2]
to_fix = z
# fix augmentation
to_fix_left = tree_nodes[to_fix][TN_LEFT_ID]
to_fix_right = tree_nodes[to_fix][TN_RIGHT_ID]
if tree_vals[to_fix_left][TN_MAX_GRAD_ID] > \
tree_vals[to_fix_right][TN_MAX_GRAD_ID]:
tmp_max = tree_vals[to_fix_left][TN_MAX_GRAD_ID]
else:
tmp_max = tree_vals[to_fix_right][TN_MAX_GRAD_ID]
min_value = _find_value_min_value(tree_vals, to_fix)
if tmp_max > min_value:
tree_vals[to_fix][TN_MAX_GRAD_ID] = tmp_max
else:
tree_vals[to_fix][TN_MAX_GRAD_ID] = min_value
while tree_nodes[z][TN_PARENT_ID] != NIL_ID:
z_parent = tree_nodes[z][TN_PARENT_ID]
if tree_vals[z_parent][TN_MAX_GRAD_ID] == z_gradient:
z_parent_left = tree_nodes[z_parent][TN_LEFT_ID]
z_parent_right = tree_nodes[z_parent][TN_RIGHT_ID]
x_parent = tree_nodes[x][TN_PARENT_ID]
x_parent_right = tree_nodes[x_parent][TN_RIGHT_ID]
if _find_value_min_value(tree_vals, z_parent) != z_gradient\
and \
not (tree_vals[z_parent_left][TN_MAX_GRAD_ID] == z_gradient
and
tree_vals[x_parent_right][TN_MAX_GRAD_ID] ==
z_gradient):
left = _find_max_value(tree_vals[z_parent_left])
right = _find_max_value(tree_vals[z_parent_right])
if left > right:
tree_vals[z_parent][TN_MAX_GRAD_ID] = left
else:
tree_vals[z_parent][TN_MAX_GRAD_ID] = right
min_value = _find_value_min_value(tree_vals, z_parent)
if min_value > tree_vals[z_parent][TN_MAX_GRAD_ID]:
tree_vals[z_parent][TN_MAX_GRAD_ID] = min_value
else:
if tree_vals[z][TN_MAX_GRAD_ID] >\
tree_vals[z_parent][TN_MAX_GRAD_ID]:
tree_vals[z_parent][TN_MAX_GRAD_ID] =\
tree_vals[z][TN_MAX_GRAD_ID]
z = z_parent
# 16-17
if tree_nodes[y][TN_COLOR_ID] == RB_BLACK and x != NIL_ID:
root = _rb_delete_fixup(tree_vals, tree_nodes, root, x)
# 18
return root, deleted
def _print_status_node(sn, row, col):
print("row=", row, "col=", col, "dist_to_viewpoint=",
sn[TN_KEY_ID], "grad=", sn[TN_GRAD_0], sn[TN_GRAD_1], sn[TN_GRAD_2],
"ang=", sn[TN_ANG_0], sn[TN_ANG_1], sn[TN_ANG_2])
return
@ngjit
def _max_grad_in_status_struct(tree_vals, tree_nodes, root,
distance, angle, gradient):
# Find the node with max Gradient within the distance (from vp)
# Note: if there is nothing in the status structure,
# it means this cell is VISIBLE
if root == NIL_ID:
return SMALLEST_GRAD
# it is also possible that the status structure is not empty, but
# there are no events with key < dist ---in this case it returns
# SMALLEST_GRAD;
# find max within the max key
return _find_max_value_within_key(tree_vals, tree_nodes, root,
distance, angle, gradient)
@ngjit
def _col_to_east(col, window_west, window_ew_res):
# Column to easting.
# Converts a column relative to a window to an east coordinate.
return window_west + col * window_ew_res
@ngjit
def _row_to_north(row, window_north, window_ns_res):
# Row to northing.
# Converts a row relative to a window to an north coordinate.
return window_north - row * window_ns_res
@ngjit
def _radian(x):
# Convert degree into radian.
return x * PI / 180.0
@ngjit
def _hypot(x, y):
return sqrt(x * x + y * y)
@ngjit
def _g_distance(e1, n1, e2, n2):
# Computes the distance, in meters, from (x1, y1) to (x2, y2)
# assume meter grid
factor = 1.0
return factor * _hypot(e1 - e2, n1 - n2)
@ngjit
def _set_visibility(visibility_grid, i, j, value):
visibility_grid[i][j] = value
return
@ngjit
def _calculate_event_row_col(event_type, event_row, event_col,
viewpoint_row, viewpoint_col):
# Calculate the neighbouring of the given event.
x = 0
y = 0
if event_type == CENTER_EVENT:
raise ValueError("_calculate_event_row_col() must not be called for "
"CENTER events")
if event_row < viewpoint_row and event_col < viewpoint_col:
# first quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 1
x = event_col + 1
else:
# if it is EXITING_EVENT
y = event_row + 1
x = event_col - 1
elif event_col == viewpoint_col and event_row < viewpoint_row:
# between the first and second quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 1
x = event_col + 1
else:
# if it is EXITING_EVENT
y = event_row + 1
x = event_col - 1
elif event_col > viewpoint_col and event_row < viewpoint_row:
# second quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 1
x = event_col + 1
else:
# if it is EXITING_EVENT
y = event_row - 1
x = event_col - 1
elif event_col > viewpoint_col and event_row == viewpoint_row:
# between the second and forth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 1
x = event_col - 1
else:
# if it is EXITING_EVENT
y = event_row - 1
x = event_col - 1
elif event_col > viewpoint_col and event_row > viewpoint_row:
# forth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 1
x = event_col - 1
else:
# if it is EXITING_EVENT
y = event_row - 1
x = event_col + 1
elif event_col == viewpoint_col and event_row > viewpoint_row:
# between the third and fourth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 1
x = event_col - 1
else:
# if it is EXITING_EVENT
y = event_row - 1
x = event_col + 1
elif event_col < viewpoint_col and event_row > viewpoint_row:
# third quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 1
x = event_col - 1
else:
# if it is EXITING_EVENT
y = event_row + 1
x = event_col + 1
elif event_col < viewpoint_col and event_row == viewpoint_row:
# between the first and third quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 1
x = event_col + 1
else:
# if it is EXITING_EVENT
y = event_row + 1
x = event_col + 1
else:
# must be the vp cell itself
assert event_row == viewpoint_row and event_col == viewpoint_col
x = event_col
y = event_row
if abs(x - event_col > 1) or abs(y - event_row > 1):
raise ValueError("_calculate_event_row_col()")
return y, x
@ngjit
def _calc_event_elev(event_type, event_row, event_col, n_rows, n_cols,
viewpoint_row, viewpoint_col, inrast):
# Calculate ENTER and EXIT event elevation (bilinear interpolation)
row1, col1 = _calculate_event_row_col(event_type, event_row, event_col,
viewpoint_row, viewpoint_col)
event_elev = inrast[1][event_col]
if 0 <= row1 < n_rows and 0 <= col1 < n_cols:
elev1 = inrast[row1 - event_row + 1][col1]
elev2 = inrast[row1 - event_row + 1][event_col]
elev3 = inrast[1][col1]
elev4 = inrast[1][event_col]
if np.isnan(elev1) or np.isnan(elev2) or np.isnan(elev3) \
or np.isnan(elev4):
event_elev = inrast[1][event_col]
else:
event_elev = (elev1 + elev2 + elev3 + elev4) / 4.0
return event_elev
@ngjit
def _calc_event_pos(event_type, event_row, event_col,
viewpoint_row, viewpoint_col):
# Calculate the exact position of the given event,
# and store them in x and y.
# Quadrants: 1 2
# 3 4
# ----->x
# |
# |
# |
# V y
x = 0
y = 0
if event_type == CENTER_EVENT:
# FOR CENTER_EVENTS
y = event_row
x = event_col
return y, x
if event_row < viewpoint_row and event_col < viewpoint_col:
# first quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 0.5
x = event_col + 0.5
else:
# if it is EXITING_EVENT
y = event_row + 0.5
x = event_col - 0.5
elif event_row < viewpoint_row and event_col == viewpoint_col:
# between the first and second quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 0.5
x = event_col + 0.5
else:
# if it is EXITING_EVENT
y = event_row + 0.5
x = event_col - 0.5
elif event_row < viewpoint_row and event_col > viewpoint_col:
# second quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 0.5
x = event_col + 0.5
else:
# if it is EXITING_EVENT
y = event_row - 0.5
x = event_col - 0.5
elif event_row == viewpoint_row and event_col > viewpoint_col:
# between the second and the fourth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 0.5
x = event_col - 0.5
else:
# if it is EXITING_EVENT
y = event_row - 0.5
x = event_col - 0.5
elif event_row > viewpoint_row and event_col > viewpoint_col:
# fourth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row + 0.5
x = event_col - 0.5
else:
# if it is EXITING_EVENT
y = event_row - 0.5
x = event_col + 0.5
elif event_row > viewpoint_row and event_col == viewpoint_col:
# between the third and fourth quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 0.5
x = event_col - 0.5
else:
# if it is EXITING_EVENT
y = event_row - 0.5
x = event_col + 0.5
elif event_row > viewpoint_row and event_col < viewpoint_col:
# third quadrant
if event_type == ENTERING_EVENT:
# if it is ENTERING_EVENT
y = event_row - 0.5