-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathosmaptransform.pas
1575 lines (1325 loc) · 41.5 KB
/
osmaptransform.pas
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
(*
OsMap components for offline rendering and routing functionalities
based on OpenStreetMap data
Copyright (C) 2019 Sergey Bodrov
This source is ported from libosmscout library
Copyright (C) 2011 Tim Teulings
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
(*
util\Transformation
OptimizeMethod -> TTransOptimizeMethod
OutputConstraint -> TTransOutputConstraint
TransPoint -> TTransPoint
TransPolygon
CoordBuffer -> TTransBuffer
TransBuffer -> TTransBuffer
*)
unit OsMapTransform;
{$ifdef FPC}
{$mode objfpc}{$H+}
{$endif}
{//$define DEBUG_POLYLABEL}
interface
uses
Classes, SysUtils, OsMapGeometry, OsMapTypes, OsMapProjection;
type
TTransOptimizeMethod = (tomNone, tomFast, tomQuality);
TTransOutputConstraint = (tocNoConstraint, tocSimple);
{ TTransPoint }
TTransPoint = object
public
IsVisible: Boolean; // point visible on screen area
IsDraw: Boolean; // point visible or connected to visible
XLon: TReal;
YLat: TReal;
Index: Integer; // original index, for copy of point
function IsEqual(AOther: TTransPoint): Boolean; inline;
function AsCoord(): TGeoPoint; inline;
function AsVertex2D(): TVertex2D; inline;
end;
TTransPointArray = array of TTransPoint;
{ TTransPointList }
TTransPointList = object
private
FCount: Integer;
function GetCapacity: Integer;
function GetCount: Integer;
procedure SetCapacity(AValue: Integer);
public
Items: array of TTransPoint;
procedure Clear();
procedure Delete(AStart, ACount: Integer);
property Count: Integer read GetCount;
property Capacity: Integer read GetCapacity write SetCapacity;
end;
{ TPolyCell }
TPolyCell = object
public
CellCenter: TVertex2D; // cell center
HalfSize: TReal; // half the cell size
Dist: TReal; // distance from cell center to polygon
MaxDist: TReal; // max distance to polygon within a cell
procedure Init(AX, AY: TReal; AHalfSize: TReal; const APoly: TVertex2DArray);
// set as polygon centroid
procedure InitCentroid(const APoly: TVertex2DArray);
end;
{ TPolyCellQueue }
TPolyCellQueue = object
private
FItems: array of TPolyCell;
FCount: Integer;
public
procedure Init();
procedure Push(const AItem: TPolyCell);
function PullMaxDist(out AItem: TPolyCell): Boolean;
property Count: Integer read FCount;
end;
{ TTransPolygon }
TTransPolygon = object
private
//FPointsSize: Integer;
FLength: Integer;
FStart: Integer;
FEnd: Integer;
procedure ErasePointsFromArray(var APoints: TTransPointArray; AFrom, ATo: Integer);
function AddPointToArray(var APoints: TTransPointArray; const AItem: TTransPoint): Integer;
procedure TransformGeoToPixel(const AProjection: TProjection;
const ANodes: TGeoPointArray);
procedure DropSimilarPoints(AOptimizeErrorTolerance: TReal);
{ Drop every point that is (more or less) on direct line between two points A and B }
procedure DropRedundantPointsFast(AOptimizeErrorTolerance: TReal);
procedure DropRedundantPointsDouglasPeucker(AOptimizeErrorTolerance: TReal; AIsArea: Boolean);
procedure DropEqualPoints();
{ Returns true, if the lines defined by the given coordinates intersect. }
function IsLinesIntersect(const A1, A2, B1, B2: TTransPoint): Boolean;
{ Helper for FindPathIntersections
Compute bounding box of path segment from node index
AFfrom (inclusive) to node index ATo (exclusive) }
procedure GetSegmentBoundingBox(const APoints: TTransPointArray; AFrom, ATo: Integer; out ABoundingBox: TGeoBox);
{ Helper for FindPathIntersections
Compute bounding boxes for path segments with length 1000 nodes,
up to node index bound (exclusive) }
procedure ComputeSegmentBoxes(const APoints: TTransPointArray; var ASegmentBoxes: TSegmentGeoBoxArray; ABound: Integer; ASegmentSize: Integer = 1000);
{ Find next intersection on way (with itself) from node index AStart1.
Return true if some intersection was found (way is not simple),
AStart1i and AStart2 indexes are setup to start possition of intesections lines. }
function FindIntersection(const APoints: TTransPointArray; var AStart1, AStart2: Integer): Boolean;
{ set IsDraw to false for redundant nodes }
procedure EnsureSimple(AIsArea: Boolean);
{ Extend Points array length if needed, return False if ALength < AMinLength }
function CheckCapacity(ALength, AMinLength: Integer): Boolean;
{ Calculate start and end index for points array of given ALength }
procedure CalculateStartEnd(ALength: Integer);
public
{ transformed points, in pixel XY }
Points: TTransPointArray;
BatchTransformer: TBatchTransformer;
function IsEmpty(): Boolean;
procedure TransformArea(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ANodes: TGeoPointArray;
AOptimizeErrorTolerance: TReal;
AConstraint: TTransOutputConstraint = tocNoConstraint);
procedure TransformWay(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ANodes: TGeoPointArray;
AOptimizeErrorTolerance: TReal;
AConstraint: TTransOutputConstraint = tocNoConstraint);
procedure TransformBoundingBox(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ABoundingBox: TGeoBox;
AOptimizeErrorTolerance: TReal;
AConstraint: TTransOutputConstraint = tocNoConstraint);
function GetBoundingBox(out AMinX, AMinY, AMaxX, AMaxY: TReal): Boolean;
property PointsCount: Integer read FLength;
property StartIndex: Integer read FStart;
property EndIndex: Integer read FEnd;
end;
TDebugPolyCellEvent = procedure(const ACell: TPolyCell; ANum: Integer) of object;
{ TTransBuffer }
{ List of all screen points for every visible area and way vertex }
{ TODO : replace by TMapTransform }
TTransBuffer = object
private
FCount: Integer; // used points
FPolyCellQueue: TPolyCellQueue;
// debug
FOnDebugPolyCell: TDebugPolyCellEvent;
public
TransPolygon: TTransPolygon;
Buffer: array of TVertex2D;
procedure Init();
procedure Reset();
procedure Assign(AOther: TTransBuffer);
function PushCoord(X, Y: TReal): Integer;
procedure TransformArea(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ANodes: TGeoPointArray;
out AStart, AEnd: Integer;
AOptimizeErrorTolerance: TReal);
function TransformWay(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ANodes: TGeoPointArray;
out AStart, AEnd: Integer;
AOptimizeErrorTolerance: TReal): Boolean;
{ Generate parallel way to way stored in this buffer on range orgStart, orgEnd (inclusive)
Result is stored after the last valid point. Generated way offsets are returned
in start and end. }
function GenerateParallelWay(AOrgStart, AOrgEnd: Integer;
AOffset: TReal;
out AStart, AEnd: Integer): Boolean;
{ A fast algorithm for finding polygon pole of inaccessibility, the most
distant internal point from the polygon outline (not to be confused with
centroid). Useful for optimal placement of a text label on a polygon.
https://github.com/mapbox/polylabel }
function Polylabel(AStart, AEnd: Integer; APrecision: TReal = 1.0): TVertex2D;
procedure FillVertex2DArray(out APolyArr: TVertex2DArray; AStart, AEnd: Integer);
property Count: Integer read FCount;
property OnDebugPolyCell: TDebugPolyCellEvent read FOnDebugPolyCell write FOnDebugPolyCell;
end;
type
{ TTransLineSegment }
TTransLineSegment = object
Ref: TTransPoint;
XDelta: TReal;
YDelta: TReal;
InverseLength: TReal;
procedure Init(const A, B: TTransPoint);
function IsValid(): Boolean;
function CalculateDistanceSquared(const p: TTransPoint): TReal;
end;
implementation
uses Math;
const
SQRT2 = 1.41421356237; // sqrt(2);
function fabs(a: TReal): TReal; inline;
begin
Result := abs(a);
end;
{ Calculates the distance between a point p and a line defined by the points a and b.
p - The point in distance to a line
a - One point defining the line
b - Another point defining the line }
function CalculateDistancePointToLineSegment(const p, a, b: TTransPoint): TReal;
var
xdelta, ydelta, u, cx, cy, dx, dy: TReal;
begin
xdelta := b.XLon - a.XLon;
ydelta := b.YLat - a.YLat;
if (xdelta = 0) and (ydelta = 0) then
begin
Result := Infinity;
Exit;
end;
u := ((p.XLon - a.XLon) * xdelta + (p.YLat - a.YLat) * ydelta) / (xdelta * xdelta + ydelta * ydelta);
if (u < 0) then
begin
cx := a.XLon;
cy := a.YLat;
end
else if (u > 1) then
begin
cx := b.XLon;
cy := b.YLat;
end
else
begin
cx := a.XLon + (u * xdelta);
cy := a.YLat + (u * ydelta);
end;
dx := cx - p.XLon;
dy := cy - p.YLat;
Result := sqrt((dx * dx) + (dy * dy));
end;
function CalculateDistancePointToPoint(const a, b: TTransPoint): TReal;
var
dx, dy: TReal;
begin
dx := b.XLon - a.XLon;
dy := b.YLat - a.YLat;
Result := sqrt(dx*dx + dy*dy);
end;
// get squared distance from a point (P) to a segment (A, B)
function CalculateSquaredDistancePointToLineSegment(const P, A, B: TVertex2D): TReal;
var
X, Y, DX, DY, T: TReal;
begin
X := A.X;
Y := A.Y;
DX := B.X - X;
DY := B.Y - Y;
if (DX <> 0) or (DY <> 0) then
begin
T := ((P.X - X) * DX + (P.Y - Y) * DY) / (DX * DX + DY * DY);
if (T > 1) then
begin
X := B.X;
Y := B.Y;
end
else if (T > 0) then
begin
X := X + (DX * T);
Y := Y + (DY * T);
end;
end;
DX := P.X - X;
DY := P.Y - Y;
Result := (DX * DX) + (DY * DY);
end;
procedure SimplifyPolyLineDouglasPeucker(var APoints: TTransPointArray;
ABeginIndex, AEndIndex, AEndValueIndex: Integer;
AOptimizeErrorToleranceSquared: TReal);
var
LineSegment: TTransLineSegment;
distanceSquared, maxDistanceSquared: TReal;
i, maxDistanceIndex: Integer;
begin
LineSegment.Init(APoints[ABeginIndex], APoints[AEndValueIndex]);
maxDistanceSquared := 0;
maxDistanceIndex := ABeginIndex;
for i := ABeginIndex+1 to AEndIndex do
begin
if APoints[i].IsDraw then
begin
distanceSquared := LineSegment.CalculateDistanceSquared(APoints[i]);
if (distanceSquared > maxDistanceSquared) then
begin
maxDistanceSquared := distanceSquared;
maxDistanceIndex := i;
end;
end;
end;
if (maxDistanceSquared <= AOptimizeErrorToleranceSquared) then
begin
//we don't need to draw any extra points
for i := ABeginIndex+1 to AEndIndex do
APoints[i].IsDraw := False;
Exit;
end;
//we need to split this line in two pieces
SimplifyPolyLineDouglasPeucker(APoints,
ABeginIndex,
maxDistanceIndex,
maxDistanceIndex,
AOptimizeErrorToleranceSquared);
SimplifyPolyLineDouglasPeucker(APoints,
maxDistanceIndex,
AEndIndex,
AEndValueIndex,
AOptimizeErrorToleranceSquared);
end;
{ TTransPoint }
function TTransPoint.IsEqual(AOther: TTransPoint): Boolean;
begin
Result := (IsDraw = AOther.IsDraw) and (XLon = AOther.XLon) and (YLat = AOther.YLat);
end;
function TTransPoint.AsCoord(): TGeoPoint;
begin
Result.Lat := YLat;
Result.Lon := XLon;
end;
function TTransPoint.AsVertex2D(): TVertex2D;
begin
Result.X := XLon;
Result.Y := YLat;
end;
{ TTransPointList }
function TTransPointList.GetCapacity: Integer;
begin
Result := Length(Items);
end;
function TTransPointList.GetCount: Integer;
begin
Result := FCount;
end;
procedure TTransPointList.SetCapacity(AValue: Integer);
begin
SetLength(Items, AValue);
if FCount > AValue then
FCount := AValue;
end;
procedure TTransPointList.Clear();
begin
FCount := 0;
end;
procedure TTransPointList.Delete(AStart, ACount: Integer);
var
i, ii, n: Integer;
begin
if ACount > (FCount - AStart) then
ACount := (FCount - AStart);
if ACount <= 0 then
Exit;
i := AStart;
ii := i + ACount;
n := 0;
while (n < ACount) do
begin
Items[i] := Items[ii];
Inc(i);
Inc(ii);
Inc(n);
end;
FCount := FCount - ACount;
end;
{ TTransPolygon }
procedure TTransPolygon.ErasePointsFromArray(var APoints: TTransPointArray;
AFrom, ATo: Integer);
var
i, ii, n, iCount: Integer;
begin
iCount := Length(APoints);
if ATo >= iCount then
ATo := iCount-1;
if (ATo < 0) or (ATo < AFrom) then
Exit;
n := ATo - AFrom + 1; // move count
i := AFrom;
ii := AFrom + n;
while n > 0 do
begin
APoints[i] := APoints[ii];
Inc(i);
Inc(ii);
Dec(n);
end;
SetLength(APoints, iCount - (ATo - AFrom + 1));
end;
function TTransPolygon.AddPointToArray(var APoints: TTransPointArray;
const AItem: TTransPoint): Integer;
begin
Result := Length(APoints);
SetLength(APoints, Result + 1);
APoints[Result] := AItem;
end;
procedure TTransPolygon.TransformGeoToPixel(const AProjection: TProjection;
const ANodes: TGeoPointArray);
var
i: Integer;
begin
{$ifdef OSMSCOUT_HAVE_SSE2}
BatchTransformer.Projection := AProjection;
{$endif}
if Length(ANodes) > 0 then
begin
FStart := 0;
FLength := Length(ANodes);
FEnd := FLength-1;
SetLength(Points, FLength);
for i := FStart to FEnd do
begin
{$ifdef OSMSCOUT_HAVE_SSE2}
BatchTransformer.GeoToPixel(ANodes[i].Lon, ANodes[i].Lat,
Points[i].XLon, Points[i].YLat);
{$else}
AProjection.GeoToPixel(ANodes[i], Points[i].XLon, Points[i].YLat);
{$endif}
Points[i].IsDraw := True;
end;
end
else
begin
FStart := 0;
FEnd := 0;
FLength := 0;
SetLength(Points, FLength);
end;
end;
procedure TTransPolygon.DropSimilarPoints(AOptimizeErrorTolerance: TReal);
var
i, j: Integer;
begin
for i := 0 to FLength - 1 do
begin
if Points[i].IsDraw then
begin
j := i+1;
while (j < FLength) do
begin
if Points[j].IsDraw then
begin
if ((fabs(Points[j].XLon - Points[i].XLon) <= AOptimizeErrorTolerance) and
(fabs(Points[j].YLat - Points[i].YLat) <= AOptimizeErrorTolerance))
then
Points[j].IsDraw := False
else
Break;
end;
Inc(j);
end;
end;
end;
end;
procedure TTransPolygon.DropRedundantPointsFast(AOptimizeErrorTolerance: TReal);
var
prev, cur, next: Integer;
distance: TReal;
begin
// Drop every point that is (more or less) on direct line between two points A and B
prev := 0;
while (prev < FLength) do
begin
while (prev < FLength) and (not Points[prev].IsDraw) do
Inc(prev);
if (prev >= FLength) then
Break;
cur := prev + 1;
while (cur < FLength) and (not Points[cur].IsDraw) do
Inc(cur);
if (cur >= FLength) then
Break;
next := cur + 1;
while (next < FLength) and (not Points[next].IsDraw) do
Inc(next);
if (next >= FLength) then
Break;
distance := CalculateDistancePointToLineSegment(Points[cur],
Points[prev],
Points[next]);
if (distance <= AOptimizeErrorTolerance) then
begin
Points[cur].IsDraw := False;
prev := next;
end
else
Inc(prev);
end;
end;
procedure TTransPolygon.DropRedundantPointsDouglasPeucker(
AOptimizeErrorTolerance: TReal; AIsArea: Boolean);
var
optimizeErrorToleranceSquared: TReal;
i, iBeg, iEnd, maxDistIndex: Integer;
maxDist, dist: TReal;
begin
// An implementation of Douglas-Peuker algorithm http://softsurfer.com/Archive/algorithm_0205/algorithm_0205.htm
optimizeErrorToleranceSquared := AOptimizeErrorTolerance * AOptimizeErrorTolerance;
iBeg := 0;
while (iBeg < FLength) and (not Points[iBeg].IsDraw) do
Inc(iBeg);
if (iBeg >= FLength) then
Exit; //we found no single point that is drawn.
//if this polyon is an area, we start by finding the largest distance from begin point to all other points
if (AIsArea) then
begin
maxDist := 0.0;
maxDistIndex := iBeg;
for i := iBeg to FLength-1 do
begin
if Points[i].IsDraw then
begin
dist := CalculateDistancePointToPoint(Points[iBeg], Points[i]);
if (dist > maxDist) then
begin
maxDist := dist;
maxDistIndex := i;
end;
end;
end;
if (maxDistIndex = iBeg) then
Exit; //we only found 1 point to draw
SimplifyPolyLineDouglasPeucker(Points,
iBeg,
maxDistIndex,
maxDistIndex,
optimizeErrorToleranceSquared);
SimplifyPolyLineDouglasPeucker(Points,
maxDistIndex,
FLength,
iBeg,
optimizeErrorToleranceSquared);
end
else
begin
//not an area but polyline
//find last drawable point;
iEnd := FLength-1;
while (iEnd > iBeg) and (not Points[iEnd].IsDraw) do
Dec(iEnd);
if (iEnd <= iBeg) then
Exit; //we only found 1 drawable point;
SimplifyPolyLineDouglasPeucker(Points,
iBeg,
iEnd,
iEnd,
optimizeErrorToleranceSquared);
end;
end;
procedure TTransPolygon.DropEqualPoints();
var
iCurrent, iNext: Integer;
begin
iCurrent := 0;
while (iCurrent < FLength) do
begin
if (not Points[iCurrent].IsDraw) then
begin
Inc(iCurrent);
Continue;
end;
iNext := iCurrent + 1;
while (iNext < FLength) and (not Points[iNext].IsDraw) do
Inc(iNext);
if (iNext >= FLength) then
Exit;
if (Points[iCurrent].XLon = Points[iNext].XLon)
and (Points[iCurrent].YLat = Points[iNext].YLat)
then
Points[iNext].IsDraw := False;
iCurrent := iNext;
end;
end;
function TTransPolygon.IsLinesIntersect(const A1, A2, B1, B2: TTransPoint): Boolean;
var
denr, ua_numr, ub_numr, ua, ub: TReal;
aBox, bBox: TGeoBox;
begin
if A1.IsEqual(B1)
or A1.IsEqual(B2)
or A2.IsEqual(B1)
or A2.IsEqual(B2)
then
Exit(True);
if (A1.IsEqual(A2) and B1.IsEqual(B2)) then
begin
// two different zero size vectors can't intersects
Exit(False);
end;
// denominator, see GetLineIntersection for more details
denr := (B2.YLat - B1.YLat) * (A2.XLon - A1.XLon)
- (B2.XLon - B1.XLon) * (A2.YLat - A1.YLat);
ua_numr := (B2.XLon - B1.XLon) * (A1.YLat - B1.YLat)
- (B2.YLat - B1.YLat) * (A1.XLon - B1.XLon);
ub_numr := (A2.XLon - A1.XLon) * (A1.YLat - B1.YLat)
- (A2.YLat - A1.YLat) * (A1.XLon - B1.XLon);
if (denr = 0.0) then
begin
// parallels
if (ua_numr = 0.0) and (ub_numr = 0.0) then
begin
// two lines are on one straight line, check the bounds
aBox.SetValue(GeoCoord(A1.YLat, A1.XLon),
GeoCoord(A2.YLat, A2.XLon));
bBox.SetValue(GeoCoord(B1.YLat, B1.XLon),
GeoCoord(B2.YLat, B2.XLon));
Result := bBox.IsIncludes(A1.AsCoord(), False)
or bBox.IsIncludes(A2.AsCoord(), False)
or aBox.IsIncludes(B1.AsCoord(), False)
or aBox.IsIncludes(B2.AsCoord(), False);
end
else
Result := False;
Exit;
end;
ua := ua_numr / denr;
ub := ub_numr / denr;
Result := (ua >= 0.0) and (ua <= 1.0) and (ub >= 0.0) and (ub <= 1.0);
end;
procedure TTransPolygon.GetSegmentBoundingBox(const APoints: TTransPointArray;
AFrom, ATo: Integer; out ABoundingBox: TGeoBox);
var
i, iCount: Integer;
MinLon, MinLat, MaxLon, MaxLat: TReal;
begin
iCount := Length(APoints);
if (iCount = 0) or (AFrom >= ATo) then
ABoundingBox.Invalidate();
MinLon := APoints[AFrom mod iCount].XLon;
MinLat := APoints[AFrom mod iCount].YLat;
MaxLon := MinLon;
MaxLat := MinLat;
for i := AFrom to ATo-1 do
begin
MinLon := min(MinLon, APoints[i mod iCount].XLon);
MinLat := min(MinLat, APoints[i mod iCount].YLat);
MaxLon := max(MaxLon, APoints[i mod iCount].XLon);
MaxLat := max(MaxLat, APoints[i mod iCount].YLat);
end;
ABoundingBox.SetValue(GeoCoord(MinLat, MinLon),
GeoCoord(MaxLat, MaxLon));
end;
procedure TTransPolygon.ComputeSegmentBoxes(const APoints: TTransPointArray;
var ASegmentBoxes: TSegmentGeoBoxArray; ABound: Integer; ASegmentSize: Integer);
var
i, n: Integer;
box: TSegmentGeoBox;
begin
Assert(ASegmentSize > 0);
i := 0;
while i < ABound do
begin
box.FromIndex := i;
box.ToIndex := min(i + ASegmentSize, ABound);
GetSegmentBoundingBox(APoints, box.FromIndex, box.ToIndex, box.BBox);
{ TODO : optimize }
n := Length(ASegmentBoxes);
SetLength(ASegmentBoxes, n+1);
ASegmentBoxes[n] := box;
Inc(i, ASegmentSize);
end;
end;
function TTransPolygon.FindIntersection(const APoints: TTransPointArray;
var AStart1, AStart2: Integer): Boolean;
var
LineBox: TGeoBox;
SegmentBoxes: TSegmentGeoBoxArray;
i, j, iCount: Integer;
i1, i2: TTransPoint;
begin
// compute b-boxes for path, each 1000 point-long segment
SetLength(SegmentBoxes, 0);
iCount := Length(APoints);
ComputeSegmentBoxes(APoints, SegmentBoxes, iCount, 1000);
for i := AStart1 to iCount-1 do
begin
i1 := APoints[i];
i2 := APoints[i+1];
LineBox.SetValue(GeoCoord(i1.YLat, i1.XLon),
GeoCoord(i2.YLat, i2.XLon));
j := i+1;
while j < iCount do
begin
if (not SegmentBoxes[j div 1000].BBox.IsIntersects(LineBox, False))
and (not SegmentBoxes[(j+1) div 1000].BBox.IsIntersects(LineBox, False))
then
begin
// round up
j := j + max(0, 998-(j mod 1000));
Continue;
end;
if (IsLinesIntersect(APoints[i], APoints[i+1], APoints[j], APoints[j+1]))
then
begin
if (APoints[i].IsEqual(APoints[j]) or APoints[i].IsEqual(APoints[j+1])
or APoints[i+1].IsEqual(APoints[j]) or APoints[i+1].IsEqual(APoints[j+1]))
then
Continue; // ignore sibling edges
AStart1 := i;
AStart2 := j;
Result := True;
Exit;
end;
Inc(j);
end;
end;
Result := False;
end;
procedure TTransPolygon.EnsureSimple(AIsArea: Boolean);
var
optimised: TTransPointArray;
i, j, n: Integer;
IsModified, IsSimple, IsUpdated: Boolean;
begin
// copy points to vector of TransPointRef for easy manipulation
SetLength(optimised, FLength+1);
n := 0;
for i := 0 to FLength-1 do
begin
if Points[i].IsDraw then
begin
optimised[n] := Points[i];
optimised[n].Index := i;
Inc(n);
end;
end;
if n <= 3 then
Exit;
if AIsArea then
begin
optimised[n] := optimised[0];
Inc(n);
end;
SetLength(optimised, n);
IsModified := False;
IsSimple := False;
while (not IsSimple) do
begin
IsSimple := True;
i := 0;
j := 0;
IsUpdated := True;
// following while is just performance optimisation,
// we don't start searching intersections from start again
while IsUpdated do
begin
if FindIntersection(optimised, i, j) then
begin
IsSimple := False;
IsModified := True;
if AIsArea and (j-i > i + Length(optimised)-j) then
begin
{ TODO : optimize, replace erase }
ErasePointsFromArray(optimised, j+1, MaxInt);
ErasePointsFromArray(optimised, 0, i);
AddPointToArray(optimised, optimised[0]);
i := 0;
end
else
begin
ErasePointsFromArray(optimised, i+1, j+1);
end;
end
else
begin
IsUpdated := False;
end
end;
end;
if (IsModified) then
begin
// setup draw property for points remaining in optimised vector
for i := 0 to FLength-1 do
Points[i].IsDraw := False;
for i := 0 to Length(optimised)-1 do
Points[optimised[i].Index].IsDraw := True;
end;
end;
function TTransPolygon.CheckCapacity(ALength, AMinLength: Integer): Boolean;
begin
Result := (ALength >= AMinLength);
if Result and (Length(Points) < ALength) then
SetLength(Points, ALength)
else
FLength := 0;
end;
procedure TTransPolygon.CalculateStartEnd(ALength: Integer);
var
i: Integer;
begin
FLength := 0;
FStart := ALength;
FEnd := 0;
// Calculate start, end and length
for i := 0 to ALength-1 do
begin
if (Points[i].IsDraw) then
begin
Inc(FLength);
if (i < FStart) then
FStart := i;
FEnd := i;
end;
end;
end;
function TTransPolygon.IsEmpty(): Boolean;
begin
Result := (FLength = 0);
end;
procedure TTransPolygon.TransformArea(const AProjection: TProjection;
AOptimize: TTransOptimizeMethod;
const ANodes: TGeoPointArray;
AOptimizeErrorTolerance: TReal;
AConstraint: TTransOutputConstraint);
begin
if not CheckCapacity(Length(ANodes), 2) then
Exit;
TransformGeoToPixel(AProjection, ANodes);
if (AOptimize <> tomNone) then
begin
if (AOptimize = tomFast) then
begin
DropSimilarPoints(AOptimizeErrorTolerance);
DropRedundantPointsFast(AOptimizeErrorTolerance);
end
else
DropRedundantPointsDouglasPeucker(AOptimizeErrorTolerance, True);
DropEqualPoints();
if (AConstraint = tocSimple) then
EnsureSimple(True);