File: General.cs

package info (click to toggle)
quickroute-gps 2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 19,576 kB
  • sloc: cs: 74,488; makefile: 72; sh: 43
file content (1413 lines) | stat: -rw-r--r-- 43,992 bytes parent folder | download | duplicates (3)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
using System;
using System.Collections.Generic;
using System.Drawing;
using QuickRoute.Resources;

namespace QuickRoute.BusinessEntities
{
  [Serializable]
  public class PointD
  {
    private double x;
    private double y;

    public PointD()
    {
    }

    public PointD(double x, double y)
    {
      this.x = x;
      this.y = y;
    }

    public double X
    {
      get { return x; }
      set { x = value; }
    }

    public double Y
    {
      get { return y; }
      set { y = value; }
    }

    public override string ToString()
    {
      return X + ", " + Y;
    }

    public static PointD operator -(PointD p0, PointD p1)
    {
      return new PointD(p0.X - p1.X, p0.Y - p1.Y);
    }

    public static PointD operator +(PointD p0, PointD p1)
    {
      return new PointD(p0.X + p1.X, p0.Y + p1.Y);
    }

    public static PointD operator *(double t, PointD p)
    {
      return new PointD(t * p.X, t * p.Y);
    }

    public static PointD operator *(PointD p, double t)
    {
      return new PointD(t * p.X, t * p.Y);
    }

    public static PointD operator /(PointD p, double t)
    {
      return new PointD(p.X / t, p.Y / t);
    }

    public static explicit operator PointF(PointD p)
    {
      return new PointF((float)p.X, (float)p.Y);
    }

    public static explicit operator PointD(Point p)
    {
      return new PointD(p.X, p.Y);
    }

    public static implicit operator PointD(PointF p)
    {
      return new PointD(p.X, p.Y);
    }
  }

  [Serializable]
  public class SizeD
  {
    private double width;
    private double height;

    public SizeD(double width, double height)
    {
      this.width = width;
      this.height = height;
    }

    public double Width
    {
      get { return width; }
      set { width = value; }
    }

    public double Height
    {
      get { return height; }
      set { height = value; }
    }
  }

  [Serializable]
  public class RectangleD
  {
    private double left;
    private double top;
    private double width;
    private double height;

    public RectangleD(double left, double top, double width, double height)
    {
      this.left = left;
      this.top = top;
      this.width = width;
      this.height = height;
    }

    public RectangleD(PointD location, SizeD size)
    {
      left = location.X;
      top = location.Y;
      width = size.Width;
      height = size.Height;
    }

    public double Left
    {
      get { return left; }
      set { left = value; }
    }

    public double Top
    {
      get { return top; }
      set { top = value; }
    }

    public double Right
    {
      get { return left + width; }
    }

    public double Bottom
    {
      get { return top + height; }
    }

    public double Width
    {
      get { return width; }
      set
      {
        if (value < 0)
        {
          left += value;
          width = Math.Abs(value);
        }
        else
        {
          width = value;
        }
      }
    }

    public double Height
    {
      get { return height; }
      set
      {
        if (value < 0)
        {
          top += value;
          height = Math.Abs(value);
        }
        else
        {
          height = value;
        }
      }
    }

    public SizeD Size
    {
      get { return new SizeD(width, height); }
      set
      {
        Width = value.Width;
        Height = value.Height;
      }
    }

    public PointD Center
    {
      get { return new PointD(left + width / 2, top + height / 2); }
    }

    public PointD UpperLeft
    {
      get { return new PointD(left, top); }
    }

    public PointD LowerLeft
    {
      get { return new PointD(left, top + height); }
    }

    public PointD UpperRight
    {
      get { return new PointD(left + width, top); }
    }

    public PointD LowerRight
    {
      get { return new PointD(left + width, top + height); }
    }

    public bool Contains(PointD p)
    {
      return p.X >= left &&
             p.X <= Right &&
             p.Y >= top &&
             p.Y <= Bottom;
    }
  }

  /// <summary>
  /// Class for storing longitude/latitude coordinates.
  /// </summary>
  [Serializable]
  public class LongLat
  {
    private double longitude;
    private double latitude;

    public double Longitude
    {
      get { return longitude; }
      set
      {
        longitude = ((value + 180.0) % 360.0) - 180.0;
      }
    }

    public double Latitude
    {
      get { return latitude; }
      set
      {
        latitude = ((value + 90.0) % 180.0) - 90.0;
      }
    }

    public LongLat()
    {
      longitude = 0.0;
      latitude = 0.0;
    }

    public LongLat(double longitude, double latitude)
    {
      Longitude = longitude;
      Latitude = latitude;
    }

    public static LongLat operator -(LongLat p0, LongLat p1)
    {
      return new LongLat(p0.Longitude - p1.Longitude, p0.Latitude - p1.Latitude);
    }

    public static LongLat operator +(LongLat p0, LongLat p1)
    {
      return new LongLat(p0.Longitude + p1.Longitude, p0.Latitude + p1.Latitude);
    }

    public static LongLat operator *(double t, LongLat p)
    {
      return new LongLat(t * p.Longitude, t * p.Latitude);
    }

    public static LongLat operator *(LongLat p, double t)
    {
      return new LongLat(t * p.Longitude, t * p.Latitude);
    }

    public static LongLat operator /(LongLat p, double t)
    {
      return new LongLat(p.Longitude / t, p.Latitude / t);
    }

    public override int GetHashCode()
    {
      return base.GetHashCode();
    }

    public override bool Equals(object obj)
    {
      return Equals(obj as LongLat);
    }

    public bool Equals(LongLat other)
    {
      if (other == null) return false;
      return (longitude == other.Longitude && latitude == other.Latitude);
    }

    /// <summary>
    /// Gets the 3D coordinates relative to the center of the earth.
    /// </summary>
    /// <returns></returns>
    public GeneralMatrix To3DPoint()
    {
      // use spherical coordinates: rho, phi, theta
      const double rho = 6378200; // earth radius in metres

      double sinPhi = Math.Sin(0.5 * Math.PI + latitude / 180.0 * Math.PI);
      double cosPhi = Math.Cos(0.5 * Math.PI + latitude / 180.0 * Math.PI);
      double sinTheta = Math.Sin(longitude / 180.0 * Math.PI);
      double cosTheta = Math.Cos(longitude / 180.0 * Math.PI);

      GeneralMatrix p = new GeneralMatrix(3, 1);
      p.SetElement(0, 0, rho * sinPhi * cosTheta);
      p.SetElement(1, 0, rho * sinPhi * sinTheta);
      p.SetElement(2, 0, rho * cosPhi);
      return p;
    }

    public override string ToString()
    {
      return ToString(new LongLatFormatter("N", "S", "E", "W", true, 0));
    }

    public string ToString(LongLatFormatter formatter)
    {
      double lon = Math.Abs(longitude);
      double lat = Math.Abs(latitude);
      string secondFormat = "00" + (formatter.SecondDecimalPlaces > 0 ? "." + new string('0', formatter.SecondDecimalPlaces) : "");
      return
        Math.Floor(lat) + " " +
        Math.Floor(60 * (lat - Math.Floor(lat))).ToString("00") + "' " +
        (formatter.SecondsVisible ? Math.Floor((3600 * (lat - Math.Floor(lat))) % 60).ToString(secondFormat) + "\" " : "") +
        (latitude < 0 ? formatter.S : formatter.N) + "   " +
        Math.Floor(lon) + " " +
        Math.Floor(60 * (lon - Math.Floor(lon))).ToString("00") + "' " +
        (formatter.SecondsVisible ? Math.Floor((3600 * (lon - Math.Floor(lon))) % 60).ToString(secondFormat) + "\" " : "") +
        (longitude < 0 ? formatter.W : formatter.E);
    }


    /// <summary>
    /// Applies an ortographic projection to the coordinate.
    /// http://en.wikipedia.org/wiki/Orthographic_projection_%28cartography%29
    /// </summary>
    /// <param name="projectionOrigin">The origin longitude/latitude coordinate of the projection</param>
    /// <returns>A point that gives the distance in meters from the projection origin</returns>
    public PointD Project(LongLat projectionOrigin)
    {
      const double rho = 6378200; // earth radius in metres
      double lambda0 = projectionOrigin.Longitude * Math.PI / 180.0;
      double phi0 = projectionOrigin.Latitude * Math.PI / 180.0;

      double lambda = longitude * Math.PI / 180.0;
      double phi = latitude * Math.PI / 180.0;
      return new PointD(rho * Math.Cos(phi) * Math.Sin(lambda - lambda0),
                        rho * (Math.Cos(phi0) * Math.Sin(phi) - Math.Sin(phi0) * Math.Cos(phi) * Math.Cos(lambda - lambda0)));
    }

    public static LongLat Deproject(PointD coordinate, LongLat projectionOrigin)
    {
      if (LinearAlgebraUtil.DistancePointToPoint(coordinate, new PointD(0, 0)) < 0.0000001)
        return new LongLat(projectionOrigin.Longitude, projectionOrigin.Latitude);
      const double r = 6378200; // earth radius in metres
      var longLat = new LongLat();
      var rho = Math.Sqrt(coordinate.X * coordinate.X + coordinate.Y * coordinate.Y);
      var c = Math.Asin(rho / r);
      var lambda0 = projectionOrigin.Longitude * Math.PI / 180.0;
      var phi1 = projectionOrigin.Latitude * Math.PI / 180.0;
      longLat.Latitude =
        Math.Asin(Math.Cos(c) * Math.Sin(phi1) +
                  (coordinate.Y * Math.Sin(c) * Math.Cos(phi1) / rho)) / Math.PI * 180.0;
      longLat.Longitude = (lambda0 +
                          Math.Atan(coordinate.X * Math.Sin(c) /
                                    (rho * Math.Cos(phi1) * Math.Cos(c) -
                                     coordinate.Y * Math.Sin(phi1) * Math.Sin(c)))) / Math.PI * 180.0;
      return longLat;
    }

  }

  public class LongLatFormatter
  {
    public LongLatFormatter(string n, string s, string e, string w, bool secondsVisible, int secondDecimalPlaces)
    {
      this.N = n;
      this.S = s;
      this.E = e;
      this.W = w;
      this.SecondsVisible = secondsVisible;
      this.SecondDecimalPlaces = secondDecimalPlaces;
    }

    public string N { get; set; }
    public string S { get; set; }
    public string E { get; set; }
    public string W { get; set; }
    public bool SecondsVisible { get; set; }
    public int SecondDecimalPlaces { get; set; }
  }

  /// <summary>
  /// Container class for transformation matrix and a projection origin, i e the data needed to transform from longlat space to pixel space.
  /// </summary>
  public class Transformation
  {
    public GeneralMatrix TransformationMatrix { get; set; }
    public LongLat ProjectionOrigin { get; set; }

    public Transformation()
    {
    }

    public Transformation(LongLatBox longLatBox, Size imageSize)
    {
      // calculate projection origin
      ProjectionOrigin = new LongLat((longLatBox.East + longLatBox.West) / 2,
                                     (longLatBox.North + longLatBox.South) / 2);

      // get image corners from kml file
      var imageCornerLongLats = longLatBox.GetRotatedBoxCornerLongLats();

      // project them on flat surface
      var projectedImageCorners = new Dictionary<Corner, PointD>();
      projectedImageCorners[Corner.NorthWest] = imageCornerLongLats[Corner.NorthWest].Project(ProjectionOrigin);
      projectedImageCorners[Corner.SouthEast] = imageCornerLongLats[Corner.SouthEast].Project(ProjectionOrigin);

      // calculate transformation matrix
      TransformationMatrix = LinearAlgebraUtil.CalculateTransformationMatrix(
        projectedImageCorners[Corner.NorthWest], new PointD(0, 0),
        projectedImageCorners[Corner.SouthEast], new PointD(imageSize.Width - 1, imageSize.Height - 1), null, true);
    }


    public Transformation(GeneralMatrix transformationMatrix, LongLat projectionOrigin)
    {
      TransformationMatrix = transformationMatrix;
      ProjectionOrigin = projectionOrigin;
    }
  }

  public class LongLatBox
  {
    public double North { get; set; }
    public double South { get; set; }
    public double West { get; set; }
    public double East { get; set; }
    public double Rotation { get; set; }

    public Dictionary<Corner, LongLat> GetRotatedBoxCornerLongLats()
    {
      var rotation = -Rotation;

      var corners = new Dictionary<Corner, LongLat>();
      corners[Corner.NorthEast] = new LongLat(East, North);
      corners[Corner.NorthWest] = new LongLat(West, North);
      corners[Corner.SouthWest] = new LongLat(West, South);
      corners[Corner.SouthEast] = new LongLat(East, South);

      var projectionOrigin = new LongLat((East + West) / 2, (North + South) / 2);

      var projectedMapCenter = projectionOrigin.Project(projectionOrigin);

      var projectedCorners = new Dictionary<Corner, PointD>();
      projectedCorners[Corner.NorthEast] = corners[Corner.NorthEast].Project(projectionOrigin);
      projectedCorners[Corner.NorthWest] = corners[Corner.NorthWest].Project(projectionOrigin);
      projectedCorners[Corner.SouthWest] = corners[Corner.SouthWest].Project(projectionOrigin);
      projectedCorners[Corner.SouthEast] = corners[Corner.SouthEast].Project(projectionOrigin);

      var projectedRotatedCorners = new Dictionary<Corner, PointD>();
      projectedRotatedCorners[Corner.NorthEast] = LinearAlgebraUtil.Rotate(projectedCorners[Corner.NorthEast], projectedMapCenter, rotation);
      projectedRotatedCorners[Corner.NorthWest] = LinearAlgebraUtil.Rotate(projectedCorners[Corner.NorthWest], projectedMapCenter, rotation);
      projectedRotatedCorners[Corner.SouthWest] = LinearAlgebraUtil.Rotate(projectedCorners[Corner.SouthWest], projectedMapCenter, rotation);
      projectedRotatedCorners[Corner.SouthEast] = LinearAlgebraUtil.Rotate(projectedCorners[Corner.SouthEast], projectedMapCenter, rotation);

      var rotatedCorners = new Dictionary<Corner, LongLat>();
      rotatedCorners[Corner.NorthWest] = LongLat.Deproject(projectedRotatedCorners[Corner.NorthWest], projectionOrigin);
      rotatedCorners[Corner.NorthEast] = LongLat.Deproject(projectedRotatedCorners[Corner.NorthEast], projectionOrigin);
      rotatedCorners[Corner.SouthWest] = LongLat.Deproject(projectedRotatedCorners[Corner.SouthWest], projectionOrigin);
      rotatedCorners[Corner.SouthEast] = LongLat.Deproject(projectedRotatedCorners[Corner.SouthEast], projectionOrigin);
      return rotatedCorners;
    }
  }

  public enum Corner
  {
    NorthWest,
    NorthEast,
    SouthWest,
    SouthEast
  }


  public class AlphaAdjustmentChange : IComparable<AlphaAdjustmentChange>
  {
    private ParameterizedLocation parameterizedLocation;
    private double alphaAdjustment;

    public AlphaAdjustmentChange(ParameterizedLocation parameterizedLocation, double alphaAdjustment)
    {
      this.parameterizedLocation = parameterizedLocation;
      this.alphaAdjustment = alphaAdjustment;
    }

    public ParameterizedLocation ParameterizedLocation
    {
      get { return parameterizedLocation; }
      set { parameterizedLocation = value; }
    }

    public double AlphaAdjustment
    {
      get { return alphaAdjustment; }
      set { alphaAdjustment = Math.Max(-1, Math.Min(1, value)); }
    }

    #region IComparable<AlphaAdjustmentChange> Members

    public int CompareTo(AlphaAdjustmentChange other)
    {
      return parameterizedLocation.CompareTo(other.ParameterizedLocation);
    }

    #endregion
  }

  public static class ConvertUtil
  {
    public static GeneralMatrix To3x1Matrix(PointD pointD)
    {
      GeneralMatrix m = new GeneralMatrix(3, 1);
      m.SetElement(0, 0, pointD.X);
      m.SetElement(1, 0, pointD.Y);
      m.SetElement(2, 0, 1);
      return m;
    }

    public static GeneralMatrix To3x1Matrix(LongLat longLat)
    {
      GeneralMatrix m = new GeneralMatrix(3, 1);
      m.SetElement(0, 0, longLat.Longitude);
      m.SetElement(1, 0, longLat.Latitude);
      m.SetElement(2, 0, 1);
      return m;
    }

    public static PointF ToPointF(GeneralMatrix _3x1Matrix)
    {
      return new PointF((float)_3x1Matrix.GetElement(0, 0), (float)_3x1Matrix.GetElement(1, 0));
    }

    public static PointD ToPointD(GeneralMatrix _3x1Matrix)
    {
      return new PointD(_3x1Matrix.GetElement(0, 0), _3x1Matrix.GetElement(1, 0));
    }

    /// <summary>
    /// Converts a speed (km/h) to a pace (min/km).
    /// </summary>
    /// <param name="speed">The speed in kilometres per hour.</param>
    /// <returns></returns>
    public static TimeSpan ToPace(double speed)
    {
      return speed == 0 ? new TimeSpan(0) : new TimeSpan((long)(3600.0 / speed * TimeSpan.TicksPerSecond));
    }

    /// <summary>
    /// Converts a pace (min/km) to a speed (km/h).
    /// </summary>
    /// <param name="pace">The pace in minutes per kilometre.</param>
    /// <returns></returns>
    public static double ToSpeed(TimeSpan pace)
    {
      return pace.TotalSeconds == 0 ? 0 : 3600.0 / pace.TotalSeconds;
    }
  }

  public static class LinearAlgebraUtil
  {
    public static double ClosestDistancePointToLine(PointD p, PointD l0, PointD l1, out double lineParameter)
    {
      PointD p1;
      double t;
      double tmpDist;
      double closestDistance;

      // Check endpoints
      closestDistance = DistancePointToPoint(l0, p);
      lineParameter = 0.0;

      tmpDist = DistancePointToPoint(l1, p);
      if (tmpDist < closestDistance)
      {
        closestDistance = tmpDist;
        lineParameter = 1.0;
      }

      // Check normal line
      if (l0.X == l1.X)
      {
        p1 = new PointD(p.X + 1.0, p.Y);
      }
      else if (l0.Y == l1.Y)
      {
        p1 = new PointD(p.X, p.Y + 1.0);
      }
      else
      {
        p1 = new PointD(p.X + 1.0, p.Y - (l1.X - l0.X) / (l1.Y - l0.Y));
      }

      LinesIntersect(l0, l1, p, p1, out t);

      if (t >= 0.0 && t <= 1.0)
      {
        tmpDist = DistancePointToPoint(new PointD(l0.X + t * (l1.X - l0.X), l0.Y + t * (l1.Y - l0.Y)), p);
        if (tmpDist < closestDistance)
        {
          closestDistance = tmpDist;
          lineParameter = t;
        }
      }
      return closestDistance;
    }

    /// <summary>
    /// Distance in longitude/latitude units, not meters
    /// </summary>
    /// <param name="p0"></param>
    /// <param name="p1"></param>
    /// <returns></returns>
    public static double DistancePointToPoint(LongLat p0, LongLat p1)
    {
      return Math.Sqrt((p1.Longitude - p0.Longitude) * (p1.Longitude - p0.Longitude) + (p1.Latitude - p0.Latitude) * (p1.Latitude - p0.Latitude));
    }

    public static double DistancePointToPoint(PointD p0, PointD p1)
    {
      return Math.Sqrt((p1.X - p0.X) * (p1.X - p0.X) + (p1.Y - p0.Y) * (p1.Y - p0.Y));
    }

    public static double DistancePointToPoint(PointF p0, PointF p1)
    {
      return Math.Sqrt((p1.X - p0.X) * (p1.X - p0.X) + (p1.Y - p0.Y) * (p1.Y - p0.Y));
    }

    public static double DistancePointToPoint(GeneralMatrix p0, GeneralMatrix p1)
    {
      if (p0.ColumnDimension != 1 || p1.ColumnDimension != 1 || p0.RowDimension != p1.RowDimension) throw new Exception("The matrices should have one column and equal number of rows.");
      double sum = 0.0;
      for (int i = 0; i < p0.RowDimension; i++)
        sum += (p1.GetElement(i, 0) - p0.GetElement(i, 0)) * (p1.GetElement(i, 0) - p0.GetElement(i, 0));
      return Math.Sqrt(sum);
    }

    public static bool LinesIntersect(PointD l0p0, PointD l0p1, PointD l1p0, PointD l1p1, out double parameter)
    {
      double a;
      double b;
      parameter = 0.0;

      // Check if any line has zero length
      if ((l0p0.X == l0p1.X && l0p0.Y == l0p1.Y) || (l1p0.X == l1p1.X && l1p0.Y == l1p1.Y)) return false;
      // Check if lines are parallell
      if (l0p1.X == l0p0.X && l1p1.X == l1p0.X) return false;
      if (!((l0p1.X == l0p0.X && l1p1.X != l1p0.X) || (l0p1.X != l0p0.X && l1p1.X == l1p0.X)))
      {
        if ((l0p1.Y - l0p0.Y) / (l0p1.X - l0p0.X) == (l1p1.Y - l1p0.Y) / (l1p1.X - l1p0.X)) return false;
      }

      b = ((l0p0.X - l1p0.X) * (l0p1.Y - l0p0.Y) + (l1p0.Y - l0p0.Y) * (l0p1.X - l0p0.X)) / ((l1p1.X - l1p0.X) * (l0p1.Y - l0p0.Y) - (l1p1.Y - l1p0.Y) * (l0p1.X - l0p0.X));
      if (l0p1.X == l0p0.X)
      {
        a = (l1p0.Y - l0p0.Y + b * (l1p1.Y - l1p0.Y)) / (l0p1.Y - l0p0.Y);
      }
      else
      {
        a = (l1p0.X - l0p0.X + b * (l1p1.X - l1p0.X)) / (l0p1.X - l0p0.X);
      }

      parameter = a;
      return (a >= 0.0 && a <= 1.0 && b >= 0.0 && b <= 1.0);
    }

    public static bool LineInfiniteLineIntersect(PointD l0p0, PointD l0p1, PointD l1p, PointD l1d, out double parameter)
    {
      double a;
      double b;
      parameter = 0.0;
      PointD l1p1 = l1p + Normalize(l1d);

      // Check if the first line has zero length
      if ((l0p0.X == l0p1.X && l0p0.Y == l0p1.Y)) return false;
      // Check if lines are parallell
      if (l0p1.X == l0p0.X && l1p1.X == l1p.X) return false;
      if (!((l0p1.X == l0p0.X && l1p1.X != l1p.X) || (l0p1.X != l0p0.X && l1p1.X == l1p.X)))
      {
        if ((l0p1.Y - l0p0.Y) / (l0p1.X - l0p0.X) == (l1p1.Y - l1p.Y) / (l1p1.X - l1p.X)) return false;
      }

      b = ((l0p0.X - l1p.X) * (l0p1.Y - l0p0.Y) + (l1p.Y - l0p0.Y) * (l0p1.X - l0p0.X)) / ((l1p1.X - l1p.X) * (l0p1.Y - l0p0.Y) - (l1p1.Y - l1p.Y) * (l0p1.X - l0p0.X));
      if (l0p1.X == l0p0.X)
      {
        a = (l1p.Y - l0p0.Y + b * (l1p1.Y - l1p.Y)) / (l0p1.Y - l0p0.Y);
      }
      else
      {
        a = (l1p.X - l0p0.X + b * (l1p1.X - l1p.X)) / (l0p1.X - l0p0.X);
      }

      parameter = a;
      return (a >= 0.0 && a <= 1.0);
    }

    public static PointD InfiniteLinesIntersectionPoint(PointD l0p, PointD l0d, PointD l1p, PointD l1d)
    {
      double b;
      PointD l0p1 = l0p + Normalize(l0d);
      PointD l1p1 = l1p + Normalize(l1d);

      // Check if lines are parallell
      if (l0p1.X == l0p.X && l1p1.X == l1p.X) return l0p;
      if (!((l0p1.X == l0p.X && l1p1.X != l1p.X) || (l0p1.X != l0p.X && l1p1.X == l1p.X)))
      {
        if ((l0p1.Y - l0p.Y) / (l0p1.X - l0p.X) == (l1p1.Y - l1p.Y) / (l1p1.X - l1p.X)) return l0p;
      }

      b = ((l0p.X - l1p.X) * (l0p1.Y - l0p.Y) + (l1p.Y - l0p.Y) * (l0p1.X - l0p.X)) / ((l1p1.X - l1p.X) * (l0p1.Y - l0p.Y) - (l1p1.Y - l1p.Y) * (l0p1.X - l0p.X));

      return l1p + b * l1d;
    }

    /// <summary>
    /// Speed in metres per second
    /// </summary>
    /// <param name="longLat0">longitude and latitude for first point</param>
    /// <param name="longLat1">longitude and latitude for second point</param>
    /// <param name="t0">time for first point</param>
    /// <param name="t1">time for first point</param>
    /// <returns></returns>
    public static double CalculateSpeed(LongLat longLat0, LongLat longLat1, DateTime t0, DateTime t1)
    {
      double distance = DistancePointToPointLongLat(longLat0, longLat1);
      TimeSpan timeSpan = t1.Subtract(t0);

      double speed = distance / timeSpan.TotalSeconds;
      return speed;
    }

    /// <summary>
    /// Distance in metres between two points expressed as longitude/latitude
    /// </summary>
    /// <param name="longLat0">longitude and latitude for first point</param>
    /// <param name="longLat1">longitude and latitude for second point</param>
    /// <returns></returns>
    public static double DistancePointToPointLongLat(LongLat longLat0, LongLat longLat1)
    {
      // use spherical coordinates: rho, phi, theta
      const double rho = 6378200; // earth radius in metres

      double sinPhi0 = Math.Sin(0.5 * Math.PI + longLat0.Latitude / 180.0 * Math.PI);
      double cosPhi0 = Math.Cos(0.5 * Math.PI + longLat0.Latitude / 180.0 * Math.PI);
      double sinTheta0 = Math.Sin(longLat0.Longitude / 180.0 * Math.PI);
      double cosTheta0 = Math.Cos(longLat0.Longitude / 180.0 * Math.PI);

      double sinPhi1 = Math.Sin(0.5 * Math.PI + longLat1.Latitude / 180.0 * Math.PI);
      double cosPhi1 = Math.Cos(0.5 * Math.PI + longLat1.Latitude / 180.0 * Math.PI);
      double sinTheta1 = Math.Sin(longLat1.Longitude / 180.0 * Math.PI);
      double cosTheta1 = Math.Cos(longLat1.Longitude / 180.0 * Math.PI);

      var p0 = new GeneralMatrix(3, 1);
      p0.SetElement(0, 0, rho * sinPhi0 * cosTheta0);
      p0.SetElement(1, 0, rho * sinPhi0 * sinTheta0);
      p0.SetElement(2, 0, rho * cosPhi0);

      var p1 = new GeneralMatrix(3, 1);
      p1.SetElement(0, 0, rho * sinPhi1 * cosTheta1);
      p1.SetElement(1, 0, rho * sinPhi1 * sinTheta1);
      p1.SetElement(2, 0, rho * cosPhi1);

      double distance = DistancePointToPoint(p0, p1);
      return distance;
    }

    public static PointD Transform(PointD p, GeneralMatrix transformationMatrix)
    {
      return ConvertUtil.ToPointD(transformationMatrix * ConvertUtil.To3x1Matrix(p));
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="p0">First point on route, in projected (metric) coordinates relative to projection origin</param>
    /// <param name="q0">First point on map, in pixels</param>
    /// <param name="p1">Second point on route, in projected (metric) coordinates relative to projection origin</param>
    /// <param name="q1">Second point on map, in pixels</param>
    /// <param name="fallbackMatrix">Matrix to use if calculation fails due to singular matrix</param>
    /// <param name="useRotation">If true, assumes orthogonal map and calculates scale and rotation. If false, calculates different scale in x and y directions and no rotation.</param>
    /// <returns></returns>
    public static GeneralMatrix CalculateTransformationMatrix(PointD p0, PointD q0, PointD p1, PointD q1, GeneralMatrix fallbackMatrix, bool useRotation)
    {
      try
      {
        if (useRotation)
        {
          // note that we need to mirror y pixel value in x axis
          double angleDifferece = GetAngleR(p1 - p0, new PointD(q1.X, -q1.Y) - new PointD(q0.X, -q0.Y));
          double lengthQ = DistancePointToPoint(q0, q1);
          double lengthP = DistancePointToPoint(p0, p1);
          double scaleFactor = lengthP == 0 ? 0 : lengthQ / lengthP;
          double cos = Math.Cos(angleDifferece);
          double sin = Math.Sin(angleDifferece);

          // translation to origo in metric space
          var a = new GeneralMatrix(3, 3);
          a.SetElement(0, 0, 1);
          a.SetElement(0, 1, 0);
          a.SetElement(0, 2, -p0.X);
          a.SetElement(1, 0, 0);
          a.SetElement(1, 1, 1);
          a.SetElement(1, 2, -p0.Y);
          a.SetElement(2, 0, 0);
          a.SetElement(2, 1, 0);
          a.SetElement(2, 2, 1);

          // rotation
          var b = new GeneralMatrix(3, 3);
          b.SetElement(0, 0, cos);
          b.SetElement(0, 1, -sin);
          b.SetElement(0, 2, 0);
          b.SetElement(1, 0, sin);
          b.SetElement(1, 1, cos);
          b.SetElement(1, 2, 0);
          b.SetElement(2, 0, 0);
          b.SetElement(2, 1, 0);
          b.SetElement(2, 2, 1);

          // scaling, note that we need to mirror y scale around x axis
          var c = new GeneralMatrix(3, 3);
          c.SetElement(0, 0, scaleFactor);
          c.SetElement(0, 1, 0);
          c.SetElement(0, 2, 0);
          c.SetElement(1, 0, 0);
          c.SetElement(1, 1, -scaleFactor);
          c.SetElement(1, 2, 0);
          c.SetElement(2, 0, 0);
          c.SetElement(2, 1, 0);
          c.SetElement(2, 2, 1);

          // translation from origo to pixel space
          var d = new GeneralMatrix(3, 3);
          d.SetElement(0, 0, 1);
          d.SetElement(0, 1, 0);
          d.SetElement(0, 2, q0.X);
          d.SetElement(1, 0, 0);
          d.SetElement(1, 1, 1);
          d.SetElement(1, 2, q0.Y);
          d.SetElement(2, 0, 0);
          d.SetElement(2, 1, 0);
          d.SetElement(2, 2, 1);

          return d * c * b * a;
        }
        else // useRotation == false
        {
          var m1 = new GeneralMatrix(2, 2);
          m1.SetElement(0, 0, p0.X);
          m1.SetElement(0, 1, 1);
          m1.SetElement(1, 0, p1.X);
          m1.SetElement(1, 1, 1);

          var v1 = new GeneralMatrix(2, 1);
          v1.SetElement(0, 0, q0.X);
          v1.SetElement(1, 0, q1.X);
          var t1 = m1.Inverse() * v1;

          var m2 = new GeneralMatrix(2, 2);
          m2.SetElement(0, 0, p0.Y);
          m2.SetElement(0, 1, 1);
          m2.SetElement(1, 0, p1.Y);
          m2.SetElement(1, 1, 1);

          var v2 = new GeneralMatrix(2, 1);
          v2.SetElement(0, 0, q0.Y);
          v2.SetElement(1, 0, q1.Y);
          var t2 = m2.Inverse() * v2;

          var t = new GeneralMatrix(3, 3);
          t.SetElement(0, 0, t1.GetElement(0, 0));
          t.SetElement(0, 1, 0);
          t.SetElement(0, 2, t1.GetElement(1, 0));
          t.SetElement(1, 0, 0);
          t.SetElement(1, 1, t2.GetElement(0, 0));
          t.SetElement(1, 2, t2.GetElement(1, 0));
          t.SetElement(2, 0, 0);
          t.SetElement(2, 1, 0);
          t.SetElement(2, 2, 1);

          return t;
        }
      }
      catch (Exception)
      {
        return (GeneralMatrix)fallbackMatrix.Clone();
      }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="p0">First point on route</param>
    /// <param name="q0">First point on map</param>
    /// <param name="p1">Second point on route</param>
    /// <param name="q1">Second point on map</param>
    /// <param name="p2">Third point on route</param>
    /// <param name="q2">Third point on map</param>
    /// <param name="fallbackMatrix">Matrix to use if calculation fails due to singular matrix</param>
    /// <returns></returns>
    public static GeneralMatrix CalculateTransformationMatrix(PointD p0, PointD q0, PointD p1, PointD q1, PointD p2, PointD q2, GeneralMatrix fallbackMatrix)
    {
      try
      {
        var m = new GeneralMatrix(3, 3);
        m.SetElement(0, 0, p0.X);
        m.SetElement(0, 1, p0.Y);
        m.SetElement(0, 2, 1.0);
        m.SetElement(1, 0, p1.X);
        m.SetElement(1, 1, p1.Y);
        m.SetElement(1, 2, 1.0);
        m.SetElement(2, 0, p2.X);
        m.SetElement(2, 1, p2.Y);
        m.SetElement(2, 2, 1.0);

        var v1 = new GeneralMatrix(3, 1);
        v1.SetElement(0, 0, q0.X);
        v1.SetElement(1, 0, q1.X);
        v1.SetElement(2, 0, q2.X);
        var t1 = m.Inverse() * v1;

        var v2 = new GeneralMatrix(3, 1);
        v2.SetElement(0, 0, q0.Y);
        v2.SetElement(1, 0, q1.Y);
        v2.SetElement(2, 0, q2.Y);
        var t2 = m.Inverse() * v2;

        var v3 = new GeneralMatrix(3, 1);
        v3.SetElement(0, 0, 1.0);
        v3.SetElement(1, 0, 1.0);
        v3.SetElement(2, 0, 1.0);
        var t3 = m.Inverse() * v3;

        var t = new GeneralMatrix(3, 3);
        t.SetElement(0, 0, t1.GetElement(0, 0));
        t.SetElement(0, 1, t1.GetElement(1, 0));
        t.SetElement(0, 2, t1.GetElement(2, 0));
        t.SetElement(1, 0, t2.GetElement(0, 0));
        t.SetElement(1, 1, t2.GetElement(1, 0));
        t.SetElement(1, 2, t2.GetElement(2, 0));
        t.SetElement(2, 0, t3.GetElement(0, 0));
        t.SetElement(2, 1, t3.GetElement(1, 0));
        t.SetElement(2, 2, t3.GetElement(2, 0));

        return t;
      }
      catch (Exception)
      {
        return (GeneralMatrix)fallbackMatrix.Clone();
      }

    }

    public static PointD Normalize(PointD vector)
    {
      if (vector.X == 0.0 && vector.Y == 0.0) return new PointD(0.0, 0.0);
      double length = Math.Sqrt(vector.X * vector.X + vector.Y * vector.Y);
      return new PointD(vector.X / length, vector.Y / length);
    }

    public static PointD GetNormalVector(PointD vector)
    {
      if (vector.X == 0.0 && vector.Y == 0.0) return new PointD(0.0, 0.0);
      PointD normalizedVector = Normalize(vector);
      return new PointD(-normalizedVector.Y, normalizedVector.X);
    }

    /// <summary>
    /// Gets angle in radians (-PI <= a <= PI) of vector relative to x axis.
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static double GetAngleR(PointD v)
    {
      PointD normalizedV = Normalize(v);
      double dp = DotProduct(normalizedV, new PointD(1, 0));
      if (dp > 1.0) dp = 1.0;
      else if (dp < -1.0) dp = -1.0;
      double angle;
      if (v.Y < 0)
        angle = 2 * Math.PI - Math.Acos(dp);
      else
        angle = Math.Acos(dp);
      if (angle > Math.PI) angle -= 2 * Math.PI;
      return angle;
    }

    /// <summary>
    /// Gets angle in radians (-PI <= a <= PI) between two vectors.
    /// </summary>
    /// <param name="v0"></param>
    /// <param name="v1"></param>
    /// <returns></returns>
    public static double GetAngleR(PointD v0, PointD v1)
    {
      double a0 = GetAngleR(v0);
      double a1 = GetAngleR(v1) + 2 * Math.PI;

      double diff = (a1 - a0) % (2 * Math.PI);
      if (diff > Math.PI) diff -= 2 * Math.PI;
      return diff;
    }

    /// <summary>
    /// Gets angle in degrees (-180 <= a <= 180) of vector relative to x axis.
    /// </summary>
    /// <param name="v"></param>
    /// <returns></returns>
    public static double GetAngleD(PointD v)
    {
      return ToDegrees(GetAngleR(v));
    }

    /// <summary>
    /// Gets angle in degrees (-180 <= a <= 180) between two vectors.
    /// </summary>
    /// <param name="v0"></param>
    /// <param name="v1"></param>
    /// <returns></returns>
    public static double GetAngleD(PointD v0, PointD v1)
    {
      return ToDegrees(GetAngleR(v0, v1));
    }

    public static double GetVectorDirectionR(PointD vectorStartPoint, PointD vectorEndPoint)
    {
      return ((GetAngleR(vectorEndPoint - vectorStartPoint) + Math.PI) % (2 * Math.PI)) - Math.PI;
    }

    public static double DotProduct(PointD v0, PointD v1)
    {
      return v0.X * v1.X + v0.Y * v1.Y;
    }

    public static double GetUnitLengthFromTransformationMatrix(GeneralMatrix transformationMatrix)
    {
      double v0 = transformationMatrix.GetElement(0, 0);
      double v1 = transformationMatrix.GetElement(1, 0);
      return Math.Sqrt(v0 * v0 + v1 * v1);
    }

    /// <summary>
    /// Returns an angle in degrees, E180CW style
    /// </summary>
    /// <param name="angleD">Angle in ExxxCW style</param>
    /// <returns></returns>
    public static double AnglifyD(double angleD)
    {
      return AnglifyD(angleD, AngleStyle.E180CCW);
    }

    /// <summary>
    /// Returns an angle in degrees
    /// </summary>
    /// <param name="degreeAngle">Angle in ExxxCW style</param>
    /// <param name="style"></param>
    /// <returns></returns>
    public static double AnglifyD(double degreeAngle, AngleStyle style)
    {
      switch (style)
      {
        case AngleStyle.E180CCW:
          degreeAngle = degreeAngle % 360;
          if (degreeAngle < -180)
          {
            degreeAngle += 360;
          }
          else if (degreeAngle >= 180)
          {
            degreeAngle -= 360;
          }
          break;

        default:
          degreeAngle = AnglifyD(degreeAngle, AngleStyle.E180CCW);
          degreeAngle = 90 - degreeAngle;

          if (degreeAngle < 0)
          {
            degreeAngle += 360;
          }
          break;
      }
      return degreeAngle;
    }

    /// <summary>
    /// Returns an angle in degrees
    /// </summary>
    /// <param name="vector"></param>
    /// <param name="style"></param>
    /// <returns></returns>
    public static double AnglifyD(PointD vector, AngleStyle style)
    {
      return AnglifyD(ToDegrees(GetAngleR(vector)), style);
    }

    /// <summary>
    /// Angles in radians
    /// </summary>
    /// <param name="angleR0"></param>
    /// <param name="angleR1"></param>
    /// <returns></returns>
    public static double CombineAnglesR(double angleR0, double angleR1)
    {
      return CombineAnglesR(angleR0, 0.5, angleR1, 0.5);
    }

    /// <summary>
    /// Angles in radians
    /// </summary>
    /// <param name="angleR0"></param>
    /// <param name="weight0"></param>
    /// <param name="angleR1"></param>
    /// <param name="weight1"></param>
    /// <returns></returns>
    public static double CombineAnglesR(double angleR0, double weight0, double angleR1, double weight1)
    {
      PointD v0 = weight0 * new PointD(Math.Cos(angleR0), Math.Sin(angleR0));
      PointD v1 = weight1 * new PointD(Math.Cos(angleR1), Math.Sin(angleR1));
      return GetAngleR(v0 + v1);
    }

    /// <summary>
    /// Angles in degrees
    /// </summary>
    /// <param name="angleD0"></param>
    /// <param name="angleD1"></param>
    /// <returns></returns>
    public static double CombineAnglesD(double angleD0, double angleD1)
    {
      return CombineAnglesD(angleD0, 0.5, angleD1, 0.5);
    }

    /// <summary>
    /// Angles in radians
    /// </summary>
    /// <param name="angleD0"></param>
    /// <param name="weight0"></param>
    /// <param name="angleD1"></param>
    /// <param name="weight1"></param>
    /// <returns></returns>
    public static double CombineAnglesD(double angleD0, double weight0, double angleD1, double weight1)
    {
      PointD v0 = weight0 * new PointD(Math.Cos(ToRadians(angleD0)), Math.Sin(ToRadians(angleD0)));
      PointD v1 = weight1 * new PointD(Math.Cos(ToRadians(angleD1)), Math.Sin(ToRadians(angleD1)));
      return ToDegrees(GetAngleR(v0 + v1));
    }

    public static double GetAngleDifferenceD(double angle0, double angle1)
    {
      double difference = angle0 - angle1;
      if (difference >= 180) difference = 360 - difference;
      else if (difference < -180) difference = difference + 360;
      return difference;
    }

    public static double ToDegrees(double angleInRadians)
    {
      return angleInRadians * 180 / Math.PI;
    }

    public static double ToRadians(double angleInDegrees)
    {
      return angleInDegrees * Math.PI / 180;
    }

    public static PointD CreateNormalizedVectorFromAngleR(double angleR)
    {
      return new PointD(Math.Cos(angleR), Math.Sin(angleR));
    }

    public static PointD CreateNormalizedVectorFromAngleD(double angleD)
    {
      return CreateNormalizedVectorFromAngleR(ToRadians(angleD));
    }

    public static PointD Rotate(PointD point, PointD rotationCenter, double angleInRadians)
    {
      var rotated =
        new GeneralMatrix(new[] { 1, 0, 0, 0, 1, 0, rotationCenter.X, rotationCenter.Y, 1 }, 3) *
        new GeneralMatrix(new[] { Math.Cos(angleInRadians), -Math.Sin(angleInRadians), 0, Math.Sin(angleInRadians), Math.Cos(angleInRadians), 0, 0, 0, 1 }, 3) *
        new GeneralMatrix(new[] { 1, 0, 0, 0, 1, 0, -rotationCenter.X, -rotationCenter.Y, 1 }, 3) *
        ConvertUtil.To3x1Matrix(point);

      return new PointD(rotated.GetElement(0, 0), rotated.GetElement(1, 0));
    }

  }

  public static class StatisticsUtil
  {
    public static double GetStandardDeviation(List<double> values)
    {
      double n = values.Count;
      if (n == 0) return 0;
      double sum = 0;
      double squareSum = 0;
      foreach (var v in values)
      {
        sum += v;
        squareSum += v * v;
      }
      double squared = squareSum / n - (sum / n) * (sum / n);
      if (squared < 0) squared = 0;
      return Math.Sqrt(squared);
    }

    public static double GetStandardDeviation(List<WeightedItem> items)
    {
      if (items.Count == 0) return 0;
      double sum = 0;
      double squareSum = 0;
      double weightSum = 0;
      foreach (var item in items)
      {
        sum += item.WeightedValue;
        squareSum += item.Weight * item.Value * item.Value;
        weightSum += item.Weight;
      }
      double squared = squareSum / weightSum - (sum / weightSum) * (sum / weightSum);
      if (squared < 0) squared = 0;
      return Math.Sqrt(squared);
    }


    public class WeightedItem
    {
      public WeightedItem(double weight, double value)
      {
        Value = value;
        Weight = weight;
      }

      public double Weight { get; set; }
      public double Value { get; set; }

      public double WeightedValue
      {
        get { return Weight * Value; }
      }
    }

  }

  public static class FileFormatManager
  {
    public static List<string> GetQuickRouteFileExtensions()
    {
      return new List<string>(new[] { ".qrt", ".jpg", ".jpeg" });
    }
  }


  [Serializable]
  public enum QuickRouteFileFormat
  {
    Qrt,
    Xml,
    Jpeg,
    Unknown
  }

  public enum RouteHandleType
  {
    Adjustment = 0,
    MouseHover = 1
  }

  public enum WaypointAttribute
  {
    Pace,
    HeartRate,
    Altitude,
    Speed,
    DirectionDeviationToNextLap,
    ElapsedTime,
    Distance,
    Inclination,
    Direction,
    Longitude,
    Latitude,
    PixelX,
    PixelY,
    MapReadingState,
    MapReadingDuration,
    PreviousMapReadingEnd,
    NextMapReadingStart
  }

  public struct WaypointAttributeString
  {
    private WaypointAttribute? waypointAttribute;
    private readonly string name;
    private readonly string unit;

    public WaypointAttributeString(WaypointAttribute? waypointAttribute)
    {
      this.waypointAttribute = waypointAttribute;
      switch (waypointAttribute)
      {
        case null:
          name = Strings.NoSecondaryColorCoding;
          unit = "";
          break;
        case BusinessEntities.WaypointAttribute.Pace:
          name = Strings.Pace;
          unit = Strings.Unit_Pace;
          break;
        case BusinessEntities.WaypointAttribute.Speed:
          name = Strings.Speed;
          unit = Strings.Unit_Speed;
          break;
        case BusinessEntities.WaypointAttribute.HeartRate:
          name = Strings.HeartRate;
          unit = Strings.Unit_HeartRate;
          break;
        case BusinessEntities.WaypointAttribute.Altitude:
          name = Strings.Altitude;
          unit = Strings.Unit_Altitude;
          break;
        case BusinessEntities.WaypointAttribute.DirectionDeviationToNextLap:
          name = Strings.Direction;
          unit = Strings.Unit_Direction;
          break;
        case BusinessEntities.WaypointAttribute.MapReadingDuration:
          name = Strings.MapReadingDuration;
          unit = Strings.Unit_Time;
          break;
        default:
          name = "";
          unit = "";
          break;
      }
    }

    public WaypointAttribute? WaypointAttribute
    {
      get { return waypointAttribute; }
      set { waypointAttribute = value; }
    }

    public override string ToString()
    {
      return Name;
    }

    public string Name
    {
      get { return name; }
    }

    public string Unit
    {
      get { return unit; }
    }

  }

  public enum MarkerType
  {
    Start,
    Lap,
    Stop,
    MouseHover,
    Handle,
    ActiveHandle,
    MovingActiveHandle
  }

  public enum CutType
  {
    Before,
    After
  }

  public enum AngleStyle
  {
    N360CW,
    E180CCW
  }

}