-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAleSphericalGrid.cs
1048 lines (880 loc) · 34.4 KB
/
AleSphericalGrid.cs
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
/* (c) AleProjects.com, 2018
* v.1.0
*
* MIT License
*
* Convention in variable names:
* Lon, Lat (short) - radians;
* Longitude, Latitude (full) - degrees;
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AleProjects.Spherical.Grid
{
public interface IQuadKeyIdentified : ICartesian
{
long QuadKey { get; set; }
}
/// <summary>
/// Helper class for building and handling of the sphere grid.
/// </summary>
public static class SphereGridHelper
{
// ABSOLUTE_MAX_LEVEL = 30 = ((64 - 1) - 3) / 2
// "64" bits in long type,
// "1" is a sign bit,
// "3" three bits for 0..7 value indexing primary tile which is 1/8 of sphere,
// "2" two bits to represent 0..3 value which is index of sub-tile inside parent tile.
/// <summary>
/// Maximum level of the sphere grid.
/// </summary>
public const int ABSOLUTE_MAX_LEVEL = ((64 - 1) - 3) / 2;
#region unit-vectors
// Unit vectors for the coordinate system.
public static readonly CartesianValue PositiveI = new CartesianValue(1.0, 0.0, 0.0);
public static readonly CartesianValue PositiveJ = new CartesianValue(0.0, 1.0, 0.0);
public static readonly CartesianValue PositiveK = new CartesianValue(0.0, 0.0, 1.0);
public static readonly CartesianValue NegativeI = new CartesianValue(-1.0, 0.0, 0.0);
public static readonly CartesianValue NegativeJ = new CartesianValue(0.0, -1.0, 0.0);
public static readonly CartesianValue NegativeK = new CartesianValue(0.0, 0.0, -1.0);
#endregion
#region primary-tiles
/// <summary>
/// Represents sphere primary tiles (1/8 of sphere, face of octahedron projected in sphere).
/// </summary>
public static readonly CartesianValue[][] PrimaryTiles =
{
// North hemisphere
new CartesianValue[] { PositiveI, PositiveJ, PositiveK },
new CartesianValue[] { PositiveJ, NegativeI, PositiveK },
new CartesianValue[] { NegativeI, NegativeJ, PositiveK },
new CartesianValue[] { NegativeJ, PositiveI, PositiveK },
// South hemisphere
new CartesianValue[] { PositiveJ, PositiveI, NegativeK },
new CartesianValue[] { NegativeI, PositiveJ, NegativeK },
new CartesianValue[] { NegativeJ, NegativeI, NegativeK },
new CartesianValue[] { PositiveI, NegativeJ, NegativeK }
};
#endregion
/// <summary>
/// Finds a primary tile (1/8 part of a sphere, face of octahedron projected on a sphere surface) containing specified location on a sphere.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="location">Location on the sphere.</param>
/// <returns>Index of primary sphere tile.</returns>
public static int FindPrimaryTile<T>(T location)
where T : ICartesian
{
for (int i = 0; i < PrimaryTiles.Length; i++)
if (SphericalExtension._InsideTriangle(location.X, location.Y, location.Z, PrimaryTiles[i][0], PrimaryTiles[i][1], PrimaryTiles[i][2]))
return i;
throw new InvalidOperationException();
}
/// <summary>
/// Finds a primary tile (1/8 part of a sphere, face of octahedron projected on a sphere surface) containing specified location on a sphere.
/// </summary>
/// <param name="latitude">Latitude of the location in degrees.</param>
/// <param name="longitude">Longitude of the locationin degrees.</param>
/// <returns></returns>
public static int FindPrimaryTile(double latitude, double longitude)
{
CartesianValue p = new CartesianValue(latitude, longitude);
return FindPrimaryTile(p);
}
/// <summary>
/// Calculates QuadKey of a tile at specified grid level containing a location with given latitude and longitude.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="location">Location on the sphere.</param>
/// <param name="level">Grid level.</param>
/// <returns>QuadKey which uniquely identifies tile containing given location.</returns>
public static long BuildQuadKey<T>(T location, int level)
where T : ICartesian
{
if (level < 0 || level > ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
double x = location.X;
double y = location.Y;
double z = location.Z;
CartesianValue vertex1 = new CartesianValue();
CartesianValue vertex2 = new CartesianValue();
CartesianValue vertex3 = new CartesianValue();
CartesianValue v1 = new CartesianValue();
CartesianValue v2 = new CartesianValue();
CartesianValue v3 = new CartesianValue();
long result = 0;
for (int i = 0; i < PrimaryTiles.Length; i++)
if (SphericalExtension._InsideTriangle(x, y, z, PrimaryTiles[i][0], PrimaryTiles[i][1], PrimaryTiles[i][2]))
{
vertex1.SetCartesian(PrimaryTiles[i][0].X, PrimaryTiles[i][0].Y, PrimaryTiles[i][0].Z);
vertex2.SetCartesian(PrimaryTiles[i][1].X, PrimaryTiles[i][1].Y, PrimaryTiles[i][1].Z);
vertex3.SetCartesian(PrimaryTiles[i][2].X, PrimaryTiles[i][2].Y, PrimaryTiles[i][2].Z);
result = (long)i << (64 - 1 - 3);
break;
}
for (int i = 0; i < level; i++)
{
v1.SetNormalized(vertex1.X + vertex3.X, vertex1.Y + vertex3.Y, vertex1.Z + vertex3.Z);
v2.SetNormalized(vertex3.X + vertex2.X, vertex3.Y + vertex2.Y, vertex3.Z + vertex2.Z);
v3.SetNormalized(vertex2.X + vertex1.X, vertex2.Y + vertex1.Y, vertex2.Z + vertex1.Z);
if (SphericalExtension._InsideTriangle(x, y, z, vertex1, v3, v1))
{
vertex2 = v3;
vertex3 = v1;
result |= (long)0x0000000000000000 >> (i * 2);
continue;
}
if (SphericalExtension._InsideTriangle(x, y, z, v1, v2, vertex3))
{
vertex1 = v1;
vertex2 = v2;
result |= 0x0400000000000000 >> (i * 2);
continue;
}
if (SphericalExtension._InsideTriangle(x, y, z, v3, vertex2, v2))
{
vertex1 = v3;
vertex3 = v2;
result |= 0x0800000000000000 >> (i * 2);
continue;
}
vertex1 = v1;
vertex2 = v2;
vertex3 = v3;
result |= 0x0c00000000000000 >> (i * 2);
}
return result;
}
/// <summary>
/// Calculates QuadKey of a tile at specified grid level containing a location with given latitude and longitude.
/// </summary>
/// <param name="location">Location on sphere.</param>
/// <param name="level">Grid level.</param>
/// <returns>QuadKey which uniquely identifies tile containing given location.</returns>
public static long BuildQuadKey(double latitude, double longitude, int level)
{
CartesianValue location = new CartesianValue(latitude, longitude, true);
return BuildQuadKey(location, level);
}
/// <summary>
/// Returns bit mask for a Quadkey of a tile at specified grid level.
/// </summary>
/// <param name="level">Grid level.</param>
/// <returns>Bit mask.</returns>
public static long QuadKeyMask(int level)
{
long result = 0x70_00_00_00_00_00_00_00;
long mask = 0x0C_00_00_00_00_00_00_00;
while (level-- > 0)
{
result |= mask;
mask >>= 2;
}
return result;
}
/// <summary>
/// Returns a quadkey for a primary (most top) tile by its index.
/// </summary>
/// <param name="tileNumber">Index of the primary tile.</param>
/// <returns>Quadkey for the primary tile.</returns>
public static long QuadKeyForPrimaryTile(int tileNumber)
{
return (long)(tileNumber & 7) << (64 - 3 - 1);
}
/// <summary>
/// Decomposes QuadKey to triangle indexes at each grid level.
/// </summary>
/// <param name="quadKey">Quadkey to decompose.</param>
/// <returns>Decomposed quadkey.</returns>
#if NETCOREAPP2_1_OR_GREATER
public static void QuadKeyParts(long quadKey, Span<int> keys)
#else
public static void QuadKeyParts(long quadKey, int[] keys)
#endif
{
//= new int[ABSOLUTE_MAX_LEVEL + 1];
for (int i = ABSOLUTE_MAX_LEVEL; i > 0; i--)
{
keys[i] = (int)(quadKey & 3);
quadKey >>= 2;
}
keys[0] = (int)(quadKey & 7);
}
/// <summary>
/// Returns grid level where square of a tile relates to square of a circle in given ratio (approximately).
/// </summary>
/// <param name="angle">Represents the circle radius.</param>
/// <param name="triangleToCircleRatio">Tile to circle ratio.</param>
/// <returns>Grid level.</returns>
public static int LevelForCircleToTriangleRatio(double angle, double circleToTriangleRatio)
{
double circleArea = 2.0 * Math.PI * (1.0 - Math.Cos(angle)); // A = 2pi* r^2 *(1-cos(angle))
double triangleArea = 4.0 * Math.PI / 8.0; // A = 4pi*r^2
int level = 0;
while (circleArea < triangleArea * circleToTriangleRatio && level < ABSOLUTE_MAX_LEVEL)
{
triangleArea /= 4.0;
level++;
}
return level;
}
}
/// <summary>
/// Sphere grid tile. Tiles are triangles uniformly (almost) covering the whole sphere.
/// At first step tile is 1/8 of sphere, on every next level tile is splitted to 4 sub-tiles.
/// </summary>
public class SphereGridTile : IComparable<SphereGridTile>, IEquatable<SphereGridTile>
{
protected SphereGridTile _Parent = null;
/// <summary>
/// Unique identifier of this tile.
/// </summary>
public long QuadKey { get; protected set; }
/// <summary>
/// Sphere grid level of this tile.
/// </summary>
public int Level { get; protected set; }
/// <summary>
/// First vertex of the tile triangle.
/// </summary>
public CartesianValue Vertex1 { get; protected set; }
/// <summary>
/// Second vertex of the tile triangle.
/// </summary>
public CartesianValue Vertex2 { get; protected set; }
/// <summary>
/// Third vertex of the tile triangle.
/// </summary>
public CartesianValue Vertex3 { get; protected set; }
/// <summary>
/// Parent tile at the upper level.
/// </summary>
public SphereGridTile Parent
{
get
{
if (_Parent == null && Level > 0)
{
_Parent = new SphereGridTile(QuadKey, Level - 1);
}
return _Parent;
}
protected set => _Parent = value;
}
/// <summary>
/// Most top (primary) tile containing this tile.
/// </summary>
public SphereGridTile Top
{
get
{
SphereGridTile result = this;
while (result.Parent != null)
result = result.Parent;
return result;
}
}
/// <summary>
/// Index of this tile inside upper parent tile.
/// </summary>
public int LevelKey
{
get
{
long result = QuadKey >> ((SphereGridHelper.ABSOLUTE_MAX_LEVEL - Level) * 2);
return Level == 0 ? (int)(result & 7) : (int)(result & 3);
}
}
/// <summary>
/// QuadKey of the parent tile.
/// </summary>
public long ParentQuadKey
{
get => Level > 0 ? QuadKey & (long)(0x7f_ff_ff_ff_ff_ff_ff_ff & (0xff_ff_ff_ff_ff_ff_ff_ff << ((SphereGridHelper.ABSOLUTE_MAX_LEVEL - Level + 1) * 2))) : 0;
}
/// <summary>
/// Upper value of QuadKey. Allows to select objects inside this tile using filter >= QuadKey and <= QuadKeyUpperValue.
/// </summary>
public long QuadKeyUpperValue
{
//get => QuadKey | (0x4f_ff_ff_ff_ff_ff_ff_ff >> (Level * 2 + 2));
get => QuadKey | (0x7f_ff_ff_ff_ff_ff_ff_ff >> (Level * 2 + 3));
}
/// <summary>
/// Hidden basic constructor.
/// </summary>
protected SphereGridTile()
{
}
/// <summary>
/// Creates a tile at the specified grid level for a given location.
/// </summary>
/// <param name="location">Location to buid tile for.</param>
/// <param name="level">Grid level.</param>
public SphereGridTile(ICartesian location, int level)
{
if (location == null)
throw new ArgumentNullException(nameof(location));
if (level < 0 || level > SphereGridHelper.ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
CartesianValue locationValue = new CartesianValue(location);
int k = SphereGridHelper.FindPrimaryTile(locationValue);
long quadkey = (long)k << (64 - 1 - 3); // 1 - sign bit, 3 - bits for 0..7 values
if (level > 0)
{
SphereGridTile tile = new SphereGridTile()
{
QuadKey = quadkey,
Vertex1 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][0]),
Vertex2 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][1]),
Vertex3 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][2]),
};
SphereGridTile nextTile;
for (int i = 0; i < level; i++)
if ((nextTile = tile.SplitAndFind(locationValue)) != null)
{
nextTile.Parent = tile;
tile = nextTile;
}
QuadKey = tile.QuadKey;
Level = level;
Parent = tile.Parent;
Vertex1 = tile.Vertex1;
Vertex2 = tile.Vertex2;
Vertex3 = tile.Vertex3;
}
else
{
QuadKey = quadkey;
Vertex1 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][0]);
Vertex2 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][1]);
Vertex3 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][2]);
}
}
/// <summary>
/// Creates a tile at the specified grid level using existing QuadKey.
/// </summary>
/// <param name="quadKey">Existing QuadKey.</param>
/// <param name="level">Grid level.</param>
public SphereGridTile(long quadKey, int level)
{
if (quadKey < 0)
throw new ArgumentOutOfRangeException(nameof(quadKey));
if (level < 0 || level > SphereGridHelper.ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
if (level > 0)
{
#if NETCOREAPP2_1_OR_GREATER
Span<int> keys = stackalloc int[SphereGridHelper.ABSOLUTE_MAX_LEVEL + 1];
#else
int[] keys = new int[SphereGridHelper.ABSOLUTE_MAX_LEVEL + 1];
#endif
SphereGridHelper.QuadKeyParts(quadKey, keys);
int k = keys[0];
SphereGridTile tile = new SphereGridTile()
{
Vertex1 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][0]),
Vertex2 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][1]),
Vertex3 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][2]),
QuadKey = quadKey & 0x70_00_00_00_00_00_00_00
};
SphereGridTile nextTile;
SphereGridTile[] tiles = new SphereGridTile[4];
for (int i = 1; i <= level; i++)
{
tile.Split(tiles);
nextTile = tiles[keys[i]];
nextTile.Parent = tile;
tile = nextTile;
}
QuadKey = tile.QuadKey;
Parent = tile.Parent;
Level = level;
Vertex1 = tile.Vertex1;
Vertex2 = tile.Vertex2;
Vertex3 = tile.Vertex3;
}
else
{
int k = (int)(quadKey >> (64 - 1 - 3)); // 1 - sign bit, 3 - bits for 0..7 values
QuadKey = quadKey;
Vertex1 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][0]);
Vertex2 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][1]);
Vertex3 = new CartesianValue(SphereGridHelper.PrimaryTiles[k][2]);
}
}
/// <summary>
/// Splits this tile and finds a sub-tile containing given location.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="location">Location.</param>
/// <returns>Sub-tile containing location.</returns>
public SphereGridTile SplitAndFind<T>(T location)
where T : ICartesian
{
var (x, y, z) = SphericalExtension._Normalized(Vertex1.X + Vertex3.X, Vertex1.Y + Vertex3.Y, Vertex1.Z + Vertex3.Z);
CartesianValue v1 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex3.X + Vertex2.X, Vertex3.Y + Vertex2.Y, Vertex3.Z + Vertex2.Z);
CartesianValue v2 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex2.X + Vertex1.X, Vertex2.Y + Vertex1.Y, Vertex2.Z + Vertex1.Z);
CartesianValue v3 = new CartesianValue(x, y, z);
int level = this.Level + 1;
x = location.X;
y = location.Y;
z = location.Z;
if (SphericalExtension._InsideTriangle(x, y, z, this.Vertex1, v3, v1))
return new SphereGridTile()
{
QuadKey = this.QuadKey | (0x0000000000000000L >> (level * 2)),
Level = level,
Vertex1 = this.Vertex1,
Vertex2 = v3,
Vertex3 = v1,
};
if (SphericalExtension._InsideTriangle(x, y, z, v1, v2, this.Vertex3))
return new SphereGridTile()
{
QuadKey = this.QuadKey | (0x1000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = this.Vertex3,
};
if (SphericalExtension._InsideTriangle(x, y, z, v3, this.Vertex2, v2))
return new SphereGridTile()
{
QuadKey = this.QuadKey | (0x2000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v3,
Vertex2 = this.Vertex2,
Vertex3 = v2,
};
return new SphereGridTile()
{
QuadKey = this.QuadKey | (0x3000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = v3,
};
}
/// <summary>
/// Splits this tile and returns sub-tiles.
/// </summary>
/// <returns>Array of sub-tiles at the next grid level.</returns>
public SphereGridTile[] Split()
{
var (x, y, z) = SphericalExtension._Normalized(Vertex1.X + Vertex3.X, Vertex1.Y + Vertex3.Y, Vertex1.Z + Vertex3.Z);
CartesianValue v1 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex3.X + Vertex2.X, Vertex3.Y + Vertex2.Y, Vertex3.Z + Vertex2.Z);
CartesianValue v2 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex2.X + Vertex1.X, Vertex2.Y + Vertex1.Y, Vertex2.Z + Vertex1.Z);
CartesianValue v3 = new CartesianValue(x, y, z);
int level = this.Level + 1;
return new SphereGridTile[]
{
new SphereGridTile()
{
QuadKey = this.QuadKey | (0x0000000000000000L >> (level * 2)),
Level = level,
Vertex1 = this.Vertex1,
Vertex2 = v3,
Vertex3 = v1
},
new SphereGridTile()
{
QuadKey = this.QuadKey | (0x1000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = this.Vertex3
},
new SphereGridTile()
{
QuadKey = this.QuadKey | (0x2000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v3,
Vertex2 = this.Vertex2,
Vertex3 = v2
},
new SphereGridTile()
{
QuadKey = this.QuadKey | (0x3000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = v3
}
};
}
public void Split(SphereGridTile[] list)
{
var (x, y, z) = SphericalExtension._Normalized(Vertex1.X + Vertex3.X, Vertex1.Y + Vertex3.Y, Vertex1.Z + Vertex3.Z);
CartesianValue v1 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex3.X + Vertex2.X, Vertex3.Y + Vertex2.Y, Vertex3.Z + Vertex2.Z);
CartesianValue v2 = new CartesianValue(x, y, z);
(x, y, z) = SphericalExtension._Normalized(Vertex2.X + Vertex1.X, Vertex2.Y + Vertex1.Y, Vertex2.Z + Vertex1.Z);
CartesianValue v3 = new CartesianValue(x, y, z);
int level = this.Level + 1;
list[0] = new SphereGridTile()
{
QuadKey = this.QuadKey | (0x0000000000000000L >> (level * 2)),
Level = level,
Vertex1 = this.Vertex1,
Vertex2 = v3,
Vertex3 = v1
};
list[1] = new SphereGridTile()
{
QuadKey = this.QuadKey | (0x1000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = this.Vertex3
};
list[2] = new SphereGridTile()
{
QuadKey = this.QuadKey | (0x2000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v3,
Vertex2 = this.Vertex2,
Vertex3 = v2
};
list[3] = new SphereGridTile()
{
QuadKey = this.QuadKey | (0x3000000000000000L >> (level * 2)),
Level = level,
Vertex1 = v1,
Vertex2 = v2,
Vertex3 = v3
};
}
/// <summary>
/// Checks if this tile is covered (fully or partially) by a circle with given center and radius represented as an angle.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="center">Center of the circle.</param>
/// <param name="angle">Angle representing circle radius.</param>
/// <returns>True if the circle covers this tile.</returns>
public bool CoveredByCircle<T>(T center, double angle)
where T : ICartesian
{
double circleCosine = Math.Cos(angle);
double phi;
// any vertex inside circle or circle center inside triangle
if (SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex1.X, Vertex1.Y, Vertex1.Z) > circleCosine ||
SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex2.X, Vertex2.Y, Vertex2.Z) > circleCosine ||
SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex3.X, Vertex3.Y, Vertex3.Z) > circleCosine ||
SphericalExtension._InsideTriangle(center.X, center.Y, center.Z, Vertex1, Vertex2, Vertex3)) return true;
// check side Vertex1-Vertex2
var (vx, vy, vz) = SphericalExtension._CrossProduct(Vertex1.X, Vertex1.Y, Vertex1.Z, Vertex2.X, Vertex2.Y, Vertex2.Z, false);
var (cx, cy, cz) = SphericalExtension._CrossProduct(center.X, center.Y, center.Z, vx, vy, vz, false);
if (SphericalExtension._DotProduct(cx, cy, cz, Vertex1.X, Vertex1.Y, Vertex1.Z) *
SphericalExtension._DotProduct(cx, cy, cz, Vertex2.X, Vertex2.Y, Vertex2.Z) < 0.0)
{
phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
if (angle + phi > Math.PI / 2) return true;
}
// check side Vertex2-Vertex3
(vx, vy, vz) = SphericalExtension._CrossProduct(Vertex2.X, Vertex2.Y, Vertex2.Z, Vertex3.X, Vertex3.Y, Vertex3.Z, false);
(cx, cy, cz) = SphericalExtension._CrossProduct(center.X, center.Y, center.Z, vx, vy, vz, false);
if (SphericalExtension._DotProduct(cx, cy, cz, Vertex2.X, Vertex2.Y, Vertex2.Z) *
SphericalExtension._DotProduct(cx, cy, cz, Vertex3.X, Vertex3.Y, Vertex3.Z) < 0.0)
{
phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
if (angle + phi > Math.PI / 2) return true;
}
// check side Vertex3-Vertex1
(vx, vy, vz) = SphericalExtension._CrossProduct(Vertex3.X, Vertex3.Y, Vertex3.Z, Vertex1.X, Vertex1.Y, Vertex1.Z, false);
(cx, cy, cz) = SphericalExtension._CrossProduct(center.X, center.Y, center.Z, vx, vy, vz, false);
if (SphericalExtension._DotProduct(cx, cy, cz, Vertex3.X, Vertex3.Y, Vertex3.Z) *
SphericalExtension._DotProduct(cx, cy, cz, Vertex1.X, Vertex1.Y, Vertex1.Z) < 0.0)
{
phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
if (angle + phi > Math.PI / 2) return true;
}
return false;
}
/// <summary>
/// Checks if this tile is covered (fully or partially) by a polyline.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="polyline">Vertices of the polyline.</param>
/// <param name="tolerance">Test tolerance in radians.</param>
/// <returns>True if the polyline covers this tile.</returns>
public bool CoveredByPolyline<T>(IEnumerable<T> polyline, double tolerance)
where T : ICartesian
{
T vertex = polyline.First();
foreach (T v in polyline.Skip(1))
{
if (SphericalExtension.SectionsIntersect(Vertex1, Vertex2, vertex, v, tolerance) >= 0 ||
SphericalExtension.SectionsIntersect(Vertex2, Vertex3, vertex, v, tolerance) >= 0 ||
SphericalExtension.SectionsIntersect(Vertex3, Vertex1, vertex, v, tolerance) >= 0)
return true;
vertex = v;
}
return EnclosesPolygon(polyline);
}
/// <summary>
/// Checks if this tile is covered (fully or partially) by a polygon.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="polygon">Vertices of the polygon.</param>
/// <returns>True if the polygon covers this tile.</returns>
public bool CoveredByPolygon<T>(IEnumerable<T> polygon)
where T : ICartesian
{
T vertex = polygon.Last();
foreach (T v in polygon)
{
if (SphericalExtension.SectionsIntersect(Vertex1, Vertex2, vertex, v) >= 0 ||
SphericalExtension.SectionsIntersect(Vertex2, Vertex3, vertex, v) >= 0 ||
SphericalExtension.SectionsIntersect(Vertex3, Vertex1, vertex, v) >= 0)
return true;
vertex = v;
}
return EnclosesPolygon(polygon) ||
Vertex1.InsidePolygon(polygon) ||
Vertex2.InsidePolygon(polygon) ||
Vertex3.InsidePolygon(polygon);
}
/// <summary>
/// Checks if this tile fully encloses a circle with given center and radius represented as an angle.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="center">Center of the circle.</param>
/// <param name="angle">Angle representing circle radius.</param>
/// <returns>True if this tile fully encloses the circle.</returns>
public bool EnclosesCircle<T>(T center, double angle)
where T : ICartesian
{
double circleCosine = Math.Cos(angle);
// any vertex outside circle or circle center outside triangle
if (SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex1.X, Vertex1.Y, Vertex1.Z) > circleCosine ||
SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex2.X, Vertex2.Y, Vertex2.Z) > circleCosine ||
SphericalExtension._Cosine(center.X, center.Y, center.Z, Vertex3.X, Vertex3.Y, Vertex3.Z) > circleCosine ||
!SphericalExtension._InsideTriangle(center.X, center.Y, center.Z, Vertex1, Vertex2, Vertex3))
return false;
// check side Vertex1-Vertex2
var (vx, vy, vz) = SphericalExtension._CrossProduct(Vertex1.X, Vertex1.Y, Vertex1.Z, Vertex2.X, Vertex2.Y, Vertex2.Z, false);
double phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
if (angle + phi > Math.PI / 2) return false;
// check side Vertex2-Vertex3
(vx, vy, vz) = SphericalExtension._CrossProduct(Vertex2.X, Vertex2.Y, Vertex2.Z, Vertex3.X, Vertex3.Y, Vertex3.Z, false);
phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
if (angle + phi > Math.PI / 2) return false;
// check side Vertex3-Vertex1
(vx, vy, vz) = SphericalExtension._CrossProduct(Vertex3.X, Vertex3.Y, Vertex3.Z, Vertex1.X, Vertex1.Y, Vertex1.Z, false);
phi = Math.Acos(SphericalExtension._Cosine(vx, vy, vz, center.X, center.Y, center.Z));
if (phi > Math.PI / 2) phi = Math.PI - phi;
return angle + phi <= Math.PI / 2; // <= because 'false' above
}
/// <summary>
/// Checks if this tile fully encloses a polygon. Can be used for polylines too.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="polygon">Vertices of the polygon or polyline.</param>
/// <returns>True if encloses.</returns>
public bool EnclosesPolygon<T>(IEnumerable<T> polygon)
where T : ICartesian
{
foreach (T vertex in polygon)
if (!SphericalExtension._InsideTriangle(vertex.X, vertex.Y, vertex.Z, Vertex1, Vertex2, Vertex3))
return false;
return true;
}
/// <summary>
/// Joins tiles to upper possible level.
/// </summary>
/// <param name="tiles">List of the tiles to join.</param>
/// <param name="level">Grid level.</param>
/// <param name="sort">Sort if necessary.</param>
protected static void JoinTiles(List<SphereGridTile> tiles, int level, bool sort = true)
{
if (sort)
tiles.Sort();
bool hasJoined = true;
int k = 0;
for (int i = level; i > 0 && hasJoined; i--)
{
hasJoined = false;
for (int j = 0; j < tiles.Count; j++)
if (tiles[j].Level == i &&
j < tiles.Count - 1 &&
tiles[j + 1].Level == i &&
tiles[j + 1].ParentQuadKey == tiles[j].ParentQuadKey)
{
k++;
if (k == 3)
{
j -= 2;
tiles[j] = tiles[j].Parent;
tiles.RemoveRange(j + 1, 3);
k = 0;
hasJoined = true;
}
}
else k = 0;
}
}
/// <summary>
/// Returns a list of tiles covering (fully or partially) a circle with given center and radius represented as an angle.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="center">Center of circle.</param>
/// <param name="angle">Angle representing circle radius.</param>
/// <param name="level">Maximum grid level of the tiles.</param>
/// <param name="join">Join tiles if possible.</param>
/// <returns>List of tiles covering circle.</returns>
public static List<SphereGridTile> CoverCircleByTiles<T>(T center, double angle, int level, bool join)
where T : ICartesian
{
if (level < 0 || level > SphereGridHelper.ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
SphereGridTile tile = new SphereGridTile(center, 0);
SphereGridTile t;
List<SphereGridTile> result = new List<SphereGridTile>() { tile };
int k = tile.LevelKey;
if (!tile.EnclosesCircle(center, angle))
for (int i = 0; i < SphereGridHelper.PrimaryTiles.Length; i++)
if (i != k && (t = new SphereGridTile(SphereGridHelper.QuadKeyForPrimaryTile(i), 0)).CoveredByCircle(center, angle))
result.Add(t);
SphereGridTile[] tmp = new SphereGridTile[4];
int n;
for (int i = 0; i < level; i++)
{
n = result.Count;
for (int j = 0; j < n; j++)
{
result[j].Split(tmp);
if (tmp[0].CoveredByCircle(center, angle)) result.Add(tmp[0]);
if (tmp[1].CoveredByCircle(center, angle)) result.Add(tmp[1]);
if (tmp[2].CoveredByCircle(center, angle)) result.Add(tmp[2]);
if (tmp[3].CoveredByCircle(center, angle)) result.Add(tmp[3]);
}
result.RemoveRange(0, n);
}
if (join && level > 0 && result.Count > 1)
JoinTiles(result, level);
return result;
}
/// <summary>
/// Returns a list of tiles covering (fully or partially) a polyline.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="polyline">Vertices of the polyline.</param>
/// <param name="level">Maximum grid level of tiles.</param>
/// <param name="join">Join tiles if possible.</param>
/// <returns>List of tiles covering the polyline.</returns>
public static List<SphereGridTile> CoverPolylineByTiles<T>(IEnumerable<T> polyline, int level, double tolerance, bool join)
where T : ICartesian
{
if (level < 0 || level > SphereGridHelper.ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
SphereGridTile tile = new SphereGridTile(polyline.First(), 0);
SphereGridTile t;
List<SphereGridTile> result = new List<SphereGridTile>() { tile };
int k = tile.LevelKey;
for (int i = 0; i < SphereGridHelper.PrimaryTiles.Length; i++)
if (i != k && (t = new SphereGridTile(SphereGridHelper.QuadKeyForPrimaryTile(i), 0)).CoveredByPolyline(polyline, tolerance))
result.Add(t);
SphereGridTile[] tmp = new SphereGridTile[4];
int n;
for (int i = 0; i < level; i++)
{
n = result.Count;
for (int j = 0; j < n; j++)
{
result[j].Split(tmp);
if (tmp[0].CoveredByPolyline(polyline, tolerance)) result.Add(tmp[0]);
if (tmp[1].CoveredByPolyline(polyline, tolerance)) result.Add(tmp[1]);
if (tmp[2].CoveredByPolyline(polyline, tolerance)) result.Add(tmp[2]);
if (tmp[3].CoveredByPolyline(polyline, tolerance)) result.Add(tmp[3]);
}
result.RemoveRange(0, n);
}
if (join && level > 0 && result.Count > 1)
JoinTiles(result, level);
return result;
}
/// <summary>
/// Returns a list of tiles covering (fully or partially) a polygon.
/// </summary>
/// <typeparam name="T">Type T must implement the ICartesian interface.</typeparam>
/// <param name="polygon">Vertices of the polygon.</param>
/// <param name="level">Maximum grid level of tiles.</param>
/// <param name="join">Join tiles if possible.</param>
/// <returns>List of tiles covering the polygon.</returns>
public static List<SphereGridTile> CoverPolygonByTiles<T>(IEnumerable<T> polygon, int level, bool join)
where T : ICartesian
{
if (level < 0 || level > SphereGridHelper.ABSOLUTE_MAX_LEVEL)
throw new ArgumentOutOfRangeException(nameof(level));
SphereGridTile tile = new SphereGridTile(polygon.First(), 0);
SphereGridTile t;
List<SphereGridTile> result = new List<SphereGridTile>() { tile };
int k = tile.LevelKey;
if (!tile.EnclosesPolygon(polygon))
for (int i = 0; i < SphereGridHelper.PrimaryTiles.Length; i++)
if (i != k && (t = new SphereGridTile(SphereGridHelper.QuadKeyForPrimaryTile(i), 0)).CoveredByPolygon(polygon))
result.Add(t);
SphereGridTile[] tmp = new SphereGridTile[4];
int n;
for (int i = 0; i < level; i++)
{
n = result.Count;
for (int j = 0; j < n; j++)
{
result[j].Split(tmp);
if (tmp[0].CoveredByPolygon(polygon)) result.Add(tmp[0]);
if (tmp[1].CoveredByPolygon(polygon)) result.Add(tmp[1]);
if (tmp[2].CoveredByPolygon(polygon)) result.Add(tmp[2]);
if (tmp[3].CoveredByPolygon(polygon)) result.Add(tmp[3]);
}
result.RemoveRange(0, n);
}
if (join && level > 0 && result.Count > 1)