Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: migrate Point<double> to Offset internally #1996

Merged
merged 54 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
e1a894b
initial commit, started work, going well
mootw Dec 2, 2024
2ab131b
more insane progress, still going well
mootw Dec 2, 2024
c918f08
compiler happy
mootw Dec 2, 2024
bd99ca1
deprecate notices, only really needed because of bounds and tilecoord…
mootw Dec 3, 2024
f3c2132
this definitely touches an externally accessible API:
mootw Dec 3, 2024
d726a5e
dart format
mootw Dec 3, 2024
54127a9
remove stuff
mootw Dec 3, 2024
36d83a4
more stuffffffffff
mootw Dec 3, 2024
ac4f22c
oops
mootw Dec 3, 2024
5e29da1
fix tests
mootw Dec 3, 2024
55641a2
Merge branch 'master' into convert-to-offset
mootw Dec 3, 2024
403c431
use double.negativeInfinity no Offset.negitiveInfinity exists
mootw Dec 3, 2024
1dcb372
rename pointToLatLng to offsetToLatLng
mootw Dec 3, 2024
e443272
remove unused duplicate function
mootw Dec 3, 2024
6948c2e
remove todo
mootw Dec 3, 2024
5beb77a
Update lib/src/map/camera/camera.dart
mootw Dec 3, 2024
ca77334
format change
mootw Dec 3, 2024
af5b36b
replace helper function
mootw Dec 3, 2024
19ab34b
start migration guide
mootw Dec 3, 2024
87f123b
mark as internal
mootw Dec 3, 2024
877bcca
Update lib/src/layer/shared/layer_interactivity/internal_hit_detectab…
mootw Dec 3, 2024
c3d01eb
rename
mootw Dec 5, 2024
a3b61e0
re-phrase things
mootw Dec 5, 2024
11c593d
rotateAroundPoint fix
mootw Dec 5, 2024
c74dc14
Merge branch 'master' into convert-to-offset
mootw Dec 5, 2024
7797c41
convert doublepoint to offset
mootw Dec 5, 2024
f64f135
dart format
mootw Dec 5, 2024
14712ad
fix visible scope lol
mootw Dec 5, 2024
7ab7a91
Update flutter_map.dart
mootw Dec 5, 2024
4399137
Update internal_hit_detectable.dart
mootw Dec 5, 2024
c164196
fix straggling Point
mootw Dec 5, 2024
357bada
convert to use size
mootw Dec 5, 2024
4e720dc
migrate easy Bounds<double> to Rect
mootw Dec 5, 2024
de37184
dart format .
mootw Dec 5, 2024
b6d2d95
migrate most methods that use Bounds
mootw Dec 5, 2024
17d1e07
dart format .
mootw Dec 5, 2024
74e5ee4
Update CHANGELOG.md
mootw Dec 5, 2024
b280465
more complete migration of Rect
mootw Dec 5, 2024
a2d88a1
dart format
mootw Dec 5, 2024
57deeab
convert Bounds to IntegerBounds and made internal
mootw Dec 5, 2024
237d5f6
fix lints
mootw Dec 5, 2024
b2ed4f2
fix test
mootw Dec 5, 2024
0a8134b
fix bug introduced in commit b6d2d9557d3d957d674429224ce6733c242cb858
mootw Dec 5, 2024
5e177e4
fix lint
mootw Dec 5, 2024
72f2fd2
rename method
mootw Dec 5, 2024
8603d71
fix some nits
mootw Dec 5, 2024
d506b44
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
7f82a71
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
209eab7
Update lib/src/geo/crs.dart
mootw Dec 5, 2024
bdc1651
dart format
mootw Dec 5, 2024
fc3e527
rename project method
mootw Dec 5, 2024
2d9a42d
dart format .
mootw Dec 5, 2024
fa244a7
fix unused import
mootw Dec 5, 2024
2a13210
Merge branch 'master' into convert-to-offset
JaffaKetchup Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions benchmark/crs.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';

import 'package:flutter_map/src/geo/crs.dart';
import 'package:latlong2/latlong.dart';
Expand Down Expand Up @@ -55,8 +55,8 @@ Future<void> main() async {
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final p = crs.latLngToPoint(latlng, 1);
x += p.x;
y += p.y;
x += p.dx;
y += p.dy;
}
return x + y;
}));
Expand Down Expand Up @@ -85,8 +85,8 @@ Future<void> main() async {
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final point = crs.latLngToPoint(latlng, 1);
x += point.x;
y += point.y;
x += point.dx;
y += point.dy;
}
return x + y;
}));
Expand All @@ -95,7 +95,7 @@ Future<void> main() async {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = crs.pointToLatLng(math.Point<double>(x, y), 1);
final latlng = crs.pointToLatLng(Offset(x, y), 1);
x += latlng.longitude;
y += latlng.latitude;
}
Expand Down
4 changes: 2 additions & 2 deletions benchmark/point_in_polygon.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Future<void> main() async {
final circle = makeCircle(1000, 1, 0);

results.add(await timedRun('In circle', () {
const point = math.Point(0, 0);
const point = Offset.zero;

bool yesPlease = true;
for (int i = 0; i < N; ++i) {
Expand All @@ -61,7 +61,7 @@ Future<void> main() async {
}));

results.add(await timedRun('Not in circle', () {
const point = math.Point(4, 4);
const point = Offset(4, 4);

bool noSir = false;
for (int i = 0; i < N; ++i) {
Expand Down
12 changes: 5 additions & 7 deletions example/lib/pages/latlng_to_screen_point.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:flutter_map/flutter_map.dart';
Expand All @@ -23,7 +21,7 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
final mapController = MapController();

LatLng? tappedCoords;
Point<double>? tappedPoint;
Offset? tappedPoint;

@override
void initState() {
Expand Down Expand Up @@ -52,8 +50,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
),
onTap: (_, latLng) {
final point = mapController.camera
.latLngToScreenPoint(tappedCoords = latLng);
setState(() => tappedPoint = Point(point.x, point.y));
.latLngToScreenOffset(tappedCoords = latLng);
setState(() => tappedPoint = Offset(point.dx, point.dy));
},
),
children: [
Expand All @@ -77,8 +75,8 @@ class _LatLngToScreenPointPageState extends State<LatLngToScreenPointPage> {
),
if (tappedPoint != null)
Positioned(
left: tappedPoint!.x - 60 / 2,
top: tappedPoint!.y - 60 / 2,
left: tappedPoint!.dx - 60 / 2,
top: tappedPoint!.dy - 60 / 2,
child: const IgnorePointer(
child: Icon(
Icons.center_focus_strong_outlined,
Expand Down
4 changes: 1 addition & 3 deletions example/lib/pages/screen_point_to_latlng.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map_example/misc/tile_providers.dart';
Expand Down Expand Up @@ -96,7 +94,7 @@ class PointToLatlngPage extends State<ScreenPointToLatLngPage> {
}

void updatePoint(BuildContext context) => setState(() => latLng =
mapController.camera.pointToLatLng(Point(_getPointX(context), pointY)));
mapController.camera.pointToLatLng(Offset(_getPointX(context), pointY)));

double _getPointX(BuildContext context) =>
MediaQuery.sizeOf(context).width / 2;
Expand Down
23 changes: 12 additions & 11 deletions lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:math' as math hide Point;
import 'dart:math' show Point;
import 'dart:ui';

import 'package:flutter_map/src/misc/bounds.dart';
import 'package:latlong2/latlong.dart';
Expand Down Expand Up @@ -51,13 +52,13 @@ abstract class Crs {
(double, double) latLngToXY(LatLng latlng, double scale);

/// Similar to [latLngToXY] but converts the XY coordinates to a [Point].
mootw marked this conversation as resolved.
Show resolved Hide resolved
Point<double> latLngToPoint(LatLng latlng, double zoom) {
Offset latLngToPoint(LatLng latlng, double zoom) {
final (x, y) = latLngToXY(latlng, scale(zoom));
return Point<double>(x, y);
return Offset(x, y);
}
mootw marked this conversation as resolved.
Show resolved Hide resolved

/// Converts a map point to the sphere coordinate (at a certain zoom).
LatLng pointToLatLng(Point point, double zoom);
LatLng pointToLatLng(Offset point, double zoom);
mootw marked this conversation as resolved.
Show resolved Hide resolved

/// Zoom to Scale function.
double scale(double zoom) => 256.0 * math.pow(2, zoom);
Expand Down Expand Up @@ -107,10 +108,10 @@ abstract class CrsWithStaticTransformation extends Crs {
}

@override
LatLng pointToLatLng(Point point, double zoom) {
LatLng pointToLatLng(Offset point, double zoom) {
final (x, y) = _transformation.untransform(
point.x.toDouble(),
point.y.toDouble(),
point.dx,
point.dy,
scale(zoom),
);
return projection.unprojectXY(x, y);
Expand Down Expand Up @@ -170,13 +171,13 @@ class Epsg3857 extends CrsWithStaticTransformation {
);

@override
Point<double> latLngToPoint(LatLng latlng, double zoom) {
Offset latLngToPoint(LatLng latlng, double zoom) {
final (x, y) = _transformation.transform(
SphericalMercator.projectLng(latlng.longitude),
SphericalMercator.projectLat(latlng.latitude),
scale(zoom),
);
return Point<double>(x, y);
return Offset(x, y);
}

@override
Expand Down Expand Up @@ -281,10 +282,10 @@ class Proj4Crs extends Crs {

/// Converts a map point to the sphere coordinate (at a certain zoom).
@override
LatLng pointToLatLng(Point point, double zoom) {
LatLng pointToLatLng(Offset point, double zoom) {
final (x, y) = _getTransformationByZoom(zoom).untransform(
point.x.toDouble(),
point.y.toDouble(),
point.dx,
point.dy,
scale(zoom),
);
return projection.unprojectXY(x, y);
Expand Down
25 changes: 13 additions & 12 deletions lib/src/gestures/map_interactive_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
.clamp(minZoom, maxZoom);
// Calculate offset of mouse cursor from viewport center
final newCenter = _camera.focusedZoomCenter(
pointerSignal.localPosition.toPoint(),
pointerSignal.localPosition,
newZoom,
);
widget.controller.moveRaw(
Expand Down Expand Up @@ -636,7 +636,7 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
final zoomDifference = oldFocalPt - newFocalPt;
final moveDifference = _rotateOffset(_focalStartLocal - _lastFocalLocal);

final newCenterPt = oldCenterPt + zoomDifference + moveDifference.toPoint();
final newCenterPt = oldCenterPt + zoomDifference + moveDifference;
return _camera.unproject(newCenterPt, zoomAfterPinchZoom);
}

Expand Down Expand Up @@ -728,9 +728,9 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
}

final direction = details.velocity.pixelsPerSecond / magnitude;
final distance =
(Offset.zero & Size(_camera.nonRotatedSize.x, _camera.nonRotatedSize.y))
.shortestSide;
final distance = (Offset.zero &
Size(_camera.nonRotatedSize.dx, _camera.nonRotatedSize.dy))
.shortestSide;

final flingOffset = _focalStartLocal - _lastFocalLocal;
_flingAnimation = Tween<Offset>(
Expand Down Expand Up @@ -803,7 +803,7 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
if (InteractiveFlag.hasDoubleTapZoom(_interactionOptions.flags)) {
final newZoom = _getZoomForScale(_camera.zoom, 2);
final newCenter = _camera.focusedZoomCenter(
tapPosition.relative!.toPoint(),
tapPosition.relative!,
newZoom,
);
_startDoubleTapAnimation(newZoom, newCenter);
Expand Down Expand Up @@ -882,19 +882,20 @@ class MapInteractiveViewerState extends State<MapInteractiveViewer>
}

final newCenterPoint = _camera.project(_mapCenterStart) +
_flingAnimation.value.toPoint().rotate(_camera.rotationRad);
_flingAnimation.value.rotate(_camera.rotationRad);
final LatLng newCenter;
if (!_camera.crs.replicatesWorldLongitude) {
newCenter = _camera.unproject(newCenterPoint);
} else {
final math.Point<double> bestCenterPoint;
final Offset bestCenterPoint;
final double worldSize = _camera.crs.scale(_camera.zoom);
if (newCenterPoint.x > worldSize) {
// TODO there has to be an easier way to write this code...
mootw marked this conversation as resolved.
Show resolved Hide resolved
if (newCenterPoint.dx > worldSize) {
bestCenterPoint =
math.Point(newCenterPoint.x - worldSize, newCenterPoint.y);
} else if (newCenterPoint.x < 0) {
Offset(newCenterPoint.dx - worldSize, newCenterPoint.dy);
} else if (newCenterPoint.dx < 0) {
bestCenterPoint =
math.Point(newCenterPoint.x + worldSize, newCenterPoint.y);
Offset(newCenterPoint.dx + worldSize, newCenterPoint.dy);
} else {
bestCenterPoint = newCenterPoint;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/circle_layer/circle_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CircleLayer<R extends Object> extends StatelessWidget {
camera: camera,
hitNotifier: hitNotifier,
),
size: Size(camera.size.x, camera.size.y),
size: Size(camera.size.dx, camera.size.dy),
isComplex: true,
),
);
Expand Down
4 changes: 2 additions & 2 deletions lib/src/layer/circle_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ base class CirclePainter<R extends Object>
@override
bool elementHitTest(
CircleMarker<R> element, {
required Point<double> point,
required Offset point,
required LatLng coordinate,
}) {
final circle = element; // Should be optimized out by compiler, avoids lint
Expand All @@ -33,7 +33,7 @@ base class CirclePainter<R extends Object>
.distance
: circle.radius;

return pow(point.x - center.dx, 2) + pow(point.y - center.dy, 2) <=
return pow(point.dx - center.dx, 2) + pow(point.dy - center.dy, 2) <=
radius * radius;
}

Expand Down
12 changes: 7 additions & 5 deletions lib/src/layer/marker_layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ class MarkerLayer extends StatelessWidget {
// Cull if out of bounds
if (!map.pixelBounds.containsPartialBounds(
Bounds(
Point(pxPoint.x + left, pxPoint.y - bottom),
Point(pxPoint.x - right, pxPoint.y + top),
Point(pxPoint.dx + left, pxPoint.dy - bottom),
Point(pxPoint.dx - right, pxPoint.dy + top),
),
)) continue;
)) {
continue;
}

// Apply map camera to marker position
final pos = pxPoint - map.pixelOrigin;
Expand All @@ -72,8 +74,8 @@ class MarkerLayer extends StatelessWidget {
key: m.key,
width: m.width,
height: m.height,
left: pos.x - right,
top: pos.y - bottom,
left: pos.dx - right,
top: pos.dy - bottom,
child: (m.rotate ?? rotate)
? Transform.rotate(
angle: -map.rotationRad,
Expand Down
26 changes: 13 additions & 13 deletions lib/src/layer/overlay_image_layer/overlay_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ class OverlayImage extends BaseOverlayImage {

// northWest is not necessarily upperLeft depending on projection
final bounds = Bounds<double>(
camera.project(this.bounds.northWest) - camera.pixelOrigin,
camera.project(this.bounds.southEast) - camera.pixelOrigin,
(camera.project(this.bounds.northWest) - camera.pixelOrigin).toPoint(),
(camera.project(this.bounds.southEast) - camera.pixelOrigin).toPoint(),
);

return Positioned(
Expand Down Expand Up @@ -145,20 +145,20 @@ class RotatedOverlayImage extends BaseOverlayImage {
final pxTopRight = pxTopLeft - pxBottomLeft + pxBottomRight;

/// update/enlarge bounds so the new corner points fit within
final bounds = Bounds<double>(pxTopLeft, pxBottomRight)
.extend(pxTopRight)
.extend(pxBottomLeft);
final bounds = Bounds<double>(pxTopLeft.toPoint(), pxBottomRight.toPoint())
.extend(pxTopRight.toPoint())
.extend(pxBottomLeft.toPoint());

final vectorX = (pxTopRight - pxTopLeft) / bounds.size.x;
final vectorY = (pxBottomLeft - pxTopLeft) / bounds.size.y;
final offset = pxTopLeft - bounds.topLeft;

final a = vectorX.x;
final b = vectorX.y;
final c = vectorY.x;
final d = vectorY.y;
final tx = offset.x;
final ty = offset.y;
final offset = pxTopLeft - bounds.topLeft.toOffset();

final a = vectorX.dx;
final b = vectorX.dy;
final c = vectorY.dx;
final d = vectorY.dy;
final tx = offset.dx;
final ty = offset.dy;

return Positioned(
left: bounds.topLeft.x,
Expand Down
10 changes: 5 additions & 5 deletions lib/src/layer/polygon_layer/label.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
part of 'polygon_layer.dart';

void Function(Canvas canvas)? _buildLabelTextPainter({
required math.Point<double> mapSize,
required Offset mapSize,
required Offset placementPoint,
required ({Offset min, Offset max}) bounds,
required TextPainter textPainter,
Expand All @@ -18,10 +18,10 @@ void Function(Canvas canvas)? _buildLabelTextPainter({
// Currently this is only enabled when the map isn't rotated, since the placementOffset
// is relative to the MobileLayerTransformer rather than in actual screen coordinates.
if (rotationRad == 0) {
if (dx + width / 2 < 0 || dx - width / 2 > mapSize.x) {
if (dx + width / 2 < 0 || dx - width / 2 > mapSize.dx) {
return null;
}
if (dy + height / 2 < 0 || dy - height / 2 > mapSize.y) {
if (dy + height / 2 < 0 || dy - height / 2 > mapSize.dy) {
return null;
}
}
Expand Down Expand Up @@ -77,8 +77,8 @@ LatLng _computeCentroid(List<LatLng> points) {
LatLng _computePolylabel(List<LatLng> points) {
final labelPosition = polylabel(
[
List<math.Point>.generate(points.length,
(i) => math.Point(points[i].longitude, points[i].latitude)),
List<Point<double>>.generate(points.length,
(i) => Point<double>(points[i].longitude, points[i].latitude)),
],
// "precision" is a bit of a misnomer. It's a threshold for when to stop
// dividing-and-conquering the polygon in the hopes of finding a better
Expand Down
4 changes: 2 additions & 2 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ base class _PolygonPainter<R extends Object>
@override
bool elementHitTest(
_ProjectedPolygon<R> projectedPolygon, {
required math.Point<double> point,
required Offset point,
required LatLng coordinate,
}) {
// TODO: We should check the bounding box here, for efficiency
Expand Down Expand Up @@ -193,7 +193,7 @@ base class _PolygonPainter<R extends Object>
lastHash = null;
}

final origin = (camera.project(camera.center) - camera.size / 2).toOffset();
final origin = camera.project(camera.center) - camera.size / 2;

// Main loop constructing batched fill and border paths from given polygons.
for (int i = 0; i <= polygons.length - 1; i++) {
Expand Down
Loading
Loading