Repository navigation
Expand file tree
/
Copy pathoffsets.dart
More file actions
154 lines (138 loc) · 4.92 KB
/
Copy pathoffsets.dart
File metadata and controls
154 lines (138 loc) · 4.92 KB
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
import 'dart:ui';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/geo/crs.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';
@internal
class OffsetHelper {
OffsetHelper({
required this.camera,
}) : _replicatesWorldLongitude = camera.crs.replicatesWorldLongitude,
_origin = camera.pixelOrigin;
final MapCamera camera;
final Offset _origin;
final bool _replicatesWorldLongitude;
/// Calculate the [Offset] for the [LatLng] point.
Offset getOffset(
LatLng point, {
double shift = 0,
}) {
final crs = camera.crs;
final zoomScale = crs.scale(camera.zoom);
final (x, y) = crs.latLngToXY(point, zoomScale);
return Offset(x - _origin.dx + shift, y - _origin.dy);
}
// TODO not sure if still relevant
/// Calculate the [Offset]s for the list of [LatLng] points.
List<Offset> getOffsets(List<LatLng> points) {
// Critically create as little garbage as possible. This is called on every frame.
final crs = camera.crs;
final zoomScale = crs.scale(camera.zoom);
final ox = -_origin.dx;
final oy = -_origin.dy;
final len = points.length;
// Optimization: monomorphize the Epsg3857-case to avoid the virtual function overhead.
if (crs case final Epsg3857 epsg3857) {
final v = List<Offset>.filled(len, Offset.zero, growable: true);
for (int i = 0; i < len; ++i) {
final (x, y) = epsg3857.latLngToXY(points[i], zoomScale);
v[i] = Offset(x + ox, y + oy);
}
return v;
}
final v = List<Offset>.filled(len, Offset.zero, growable: true);
for (int i = 0; i < len; ++i) {
final (x, y) = crs.latLngToXY(points[i], zoomScale);
v[i] = Offset(x + ox, y + oy);
}
return v;
}
/// Suitable for polylines, filled polygons, and holed polygons
(List<Offset>, double) getOffsetsXY({
required List<Offset> points,
List<List<Offset>>? holePoints,
double shift = 0,
// most of the time will be null, except for polygon holes
double? forcedAddedWorldWidth,
}) {
// Critically create as little garbage as possible. This is called on every frame.
final crs = camera.crs;
final zoomScale = crs.scale(camera.zoom);
// Flatten the points and hole points into a single list, so that the
// loops below can use constant-time indexing. Using a lazy concatenated
// iterable here makes `elementAt` walk from the start on every call,
// which is quadratic in the total number of points.
final List<Offset> realPoints;
if (holePoints == null || holePoints.isEmpty) {
realPoints = points;
} else {
var totalLength = points.length;
for (final hole in holePoints) {
totalLength += hole.length;
}
realPoints = List<Offset>.filled(totalLength, Offset.zero)
..setRange(0, points.length, points);
var start = points.length;
for (final hole in holePoints) {
realPoints.setRange(start, start + hole.length, hole);
start += hole.length;
}
}
final ox = -_origin.dx;
final oy = -_origin.dy;
final len = realPoints.length;
/// Returns additional world width in order to have visible points.
double getAddedWorldWidth() {
if (!_replicatesWorldLongitude) {
return 0;
}
if (forcedAddedWorldWidth != null) {
return forcedAddedWorldWidth;
}
final worldWidth = crs.projection.getWorldWidth();
final List<double> addedWidths = [
0,
worldWidth,
-worldWidth,
];
final halfScreenWidth = camera.size.width / 2;
final p = realPoints[0];
late double result;
late double bestX;
for (int i = 0; i < addedWidths.length; i++) {
final addedWidth = addedWidths[i];
final (x, _) = crs.transform(p.dx + addedWidth, p.dy, zoomScale);
if (i == 0) {
result = addedWidth;
bestX = x;
continue;
}
if ((bestX + ox - halfScreenWidth).abs() >
(x + ox - halfScreenWidth).abs()) {
result = addedWidth;
bestX = x;
}
}
return result;
}
final addedWorldWidth = getAddedWorldWidth();
// Optimization: monomorphize the CrsWithStaticTransformation-case to avoid
// the virtual function overhead.
if (crs case final CrsWithStaticTransformation crs) {
final v = List<Offset>.filled(len, Offset.zero, growable: true);
for (int i = 0; i < len; ++i) {
final p = realPoints[i];
final (x, y) = crs.transform(p.dx + addedWorldWidth, p.dy, zoomScale);
v[i] = Offset(x + ox + shift, y + oy);
}
return (v, addedWorldWidth);
}
final v = List<Offset>.filled(len, Offset.zero, growable: true);
for (int i = 0; i < len; ++i) {
final p = realPoints[i];
final (x, y) = crs.transform(p.dx + addedWorldWidth, p.dy, zoomScale);
v[i] = Offset(x + ox + shift, y + oy);
}
return (v, addedWorldWidth);
}
}