Skip to content

Commit

Permalink
migrate most methods that use Bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
mootw committed Dec 5, 2024
1 parent de37184 commit b6d2d95
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 200 deletions.
17 changes: 7 additions & 10 deletions lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ abstract class Crs {
double zoom(double scale) => math.log(scale / 256) / math.ln2;

/// Rescales the bounds to a given zoom value.
Bounds<double>? getProjectedBounds(double zoom);
Rect? getProjectedBounds(double zoom);

/// Returns true if we want the world to be replicated, longitude-wise.
bool get replicatesWorldLongitude => false;
Expand Down Expand Up @@ -118,16 +118,16 @@ abstract class CrsWithStaticTransformation extends Crs {
}

@override
Bounds<double>? getProjectedBounds(double zoom) {
Rect? getProjectedBounds(double zoom) {
if (infinite) return null;

final b = projection.bounds!;
final s = scale(zoom);
final (minx, miny) = _transformation.transform(b.min.x, b.min.y, s);
final (maxx, maxy) = _transformation.transform(b.max.x, b.max.y, s);
return Bounds<double>(
Point<double>(minx, miny),
Point<double>(maxx, maxy),
return Rect.fromPoints(
Offset(minx, miny),
Offset(maxx, maxy),
);
}
}
Expand Down Expand Up @@ -293,7 +293,7 @@ class Proj4Crs extends Crs {

/// Rescales the bounds to a given zoom value.
@override
Bounds<double>? getProjectedBounds(double zoom) {
Rect? getProjectedBounds(double zoom) {
if (infinite) return null;

final b = projection.bounds!;
Expand All @@ -302,10 +302,7 @@ class Proj4Crs extends Crs {
final transformation = _getTransformationByZoom(zoom);
final (minx, miny) = transformation.transform(b.min.x, b.min.y, zoomScale);
final (maxx, maxy) = transformation.transform(b.max.x, b.max.y, zoomScale);
return Bounds<double>(
Point<double>(minx, miny),
Point<double>(maxx, maxy),
);
return Rect.fromPoints(Offset(minx, miny), Offset(maxx, maxy));
}

/// Zoom to Scale function.
Expand Down
33 changes: 16 additions & 17 deletions lib/src/layer/overlay_image_layer/overlay_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ class OverlayImage extends BaseOverlayImage {
final camera = MapCamera.of(context);

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

return Positioned(
left: bounds.topLeft.x,
top: bounds.topLeft.y,
width: bounds.size.x,
height: bounds.size.y,
left: bounds.topLeft.dx,
top: bounds.topLeft.dy,
width: bounds.size.width,
height: bounds.size.height,
child: child,
);
}
Expand Down Expand Up @@ -145,13 +145,12 @@ class RotatedOverlayImage extends BaseOverlayImage {
final pxTopRight = pxTopLeft - pxBottomLeft + pxBottomRight;

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

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

final a = vectorX.dx;
final b = vectorX.dy;
Expand All @@ -161,10 +160,10 @@ class RotatedOverlayImage extends BaseOverlayImage {
final ty = offset.dy;

return Positioned(
left: bounds.topLeft.x,
top: bounds.topLeft.y,
width: bounds.size.x,
height: bounds.size.y,
left: bounds.topLeft.dx,
top: bounds.topLeft.dy,
width: bounds.size.width,
height: bounds.size.height,
child: Transform(
transform: Matrix4(a, b, 0, 0, c, d, 0, 0, 0, 0, 1, 0, tx, ty, 0, 1),
filterQuality: filterQuality,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>>
);

// segment is visible
final projBounds = Bounds(
projection.project(boundsAdjusted.southWest).toPoint(),
projection.project(boundsAdjusted.northEast).toPoint(),
final projBounds = Rect.fromPoints(
projection.project(boundsAdjusted.southWest),
projection.project(boundsAdjusted.northEast),
);

final (xWest, _) = projection.projectXY(const LatLng(0, -180));
Expand Down
19 changes: 10 additions & 9 deletions lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'dart:ui';

import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_bounds/tile_bounds_at_zoom.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
import 'package:flutter_map/src/misc/extensions.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';

Expand Down Expand Up @@ -87,13 +88,13 @@ class DiscreteTileBounds extends TileBounds {
TileBoundsAtZoom _tileBoundsAtZoomImpl(int zoom) {
final zoomDouble = zoom.toDouble();

final Bounds<double> pixelBounds;
final Rect pixelBounds;
if (_latLngBounds == null) {
pixelBounds = crs.getProjectedBounds(zoomDouble)!;
} else {
pixelBounds = Bounds<double>(
crs.latLngToOffset(_latLngBounds!.southWest, zoomDouble).toPoint(),
crs.latLngToOffset(_latLngBounds!.northEast, zoomDouble).toPoint(),
pixelBounds = Rect.fromPoints(
crs.latLngToOffset(_latLngBounds!.southWest, zoomDouble),
crs.latLngToOffset(_latLngBounds!.northEast, zoomDouble),
);
}

Expand Down Expand Up @@ -127,13 +128,13 @@ class WrappedTileBounds extends TileBounds {
WrappedTileBoundsAtZoom _tileBoundsAtZoomImpl(int zoom) {
final zoomDouble = zoom.toDouble();

final Bounds<double> pixelBounds;
final Rect pixelBounds;
if (_latLngBounds == null) {
pixelBounds = crs.getProjectedBounds(zoomDouble)!;
} else {
pixelBounds = Bounds<double>(
crs.latLngToOffset(_latLngBounds!.southWest, zoomDouble).toPoint(),
crs.latLngToOffset(_latLngBounds!.northEast, zoomDouble).toPoint(),
pixelBounds = Rect.fromPoints(
crs.latLngToOffset(_latLngBounds!.southWest, zoomDouble),
crs.latLngToOffset(_latLngBounds!.northEast, zoomDouble),
);
}

Expand Down
19 changes: 9 additions & 10 deletions lib/src/layer/tile_layer/tile_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:math' show Point;
import 'dart:ui';

import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/misc/extensions.dart';
import 'package:meta/meta.dart';

/// A range of tiles, this is normally a [DiscreteTileRange] and sometimes
Expand Down Expand Up @@ -31,11 +30,11 @@ class EmptyTileRange extends TileRange {
const Iterable<TileCoordinates>.empty();
}

Point<int> _floor(Point<double> point) =>
Point<int>(point.x.floor(), point.y.floor());
Point<int> _floor(Offset point) =>
Point<int>(point.dx.floor(), point.dy.floor());

Point<int> _ceil(Point<double> point) =>
Point<int>(point.x.ceil(), point.y.ceil());
Point<int> _ceil(Offset point) =>
Point<int>(point.dx.ceil(), point.dy.ceil());

/// Every [TileRange] is a [DiscreteTileRange] if it's not an [EmptyTileRange].
@immutable
Expand All @@ -50,16 +49,16 @@ class DiscreteTileRange extends TileRange {
factory DiscreteTileRange.fromPixelBounds({
required int zoom,
required int tileDimension,
required Bounds<double> pixelBounds,
required Rect pixelBounds,
}) {
final Bounds<int> bounds;
if (pixelBounds.min == pixelBounds.max) {
final minAndMax = _floor(pixelBounds.min / tileDimension);
if (pixelBounds.isEmpty) {
final minAndMax = _floor(pixelBounds.topLeft / tileDimension.toDouble());
bounds = Bounds<int>(minAndMax, minAndMax);
} else {
bounds = Bounds<int>(
_floor(pixelBounds.min / tileDimension),
_ceil(pixelBounds.max / tileDimension) - const Point(1, 1),
_floor(pixelBounds.topLeft / tileDimension.toDouble()),
_ceil(pixelBounds.bottomRight / tileDimension.toDouble()) - const Point(1, 1),
);
}

Expand Down
8 changes: 5 additions & 3 deletions lib/src/layer/tile_layer/tile_range_calculator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class TileRangeCalculator {
);
}

Bounds<double> _calculatePixelBounds(
Rect _calculatePixelBounds(
MapCamera camera,
LatLng center,
double viewingZoom,
Expand All @@ -51,7 +51,9 @@ class TileRangeCalculator {
final pixelCenter = camera.project(center, tileZoomDouble).floor();
final halfSize = camera.size / (scale * 2);

return Bounds((pixelCenter - halfSize.bottomRight(Offset.zero)).toPoint(),
(pixelCenter + halfSize.bottomRight(Offset.zero)).toPoint());
return Rect.fromPoints(
pixelCenter - halfSize.bottomRight(Offset.zero),
pixelCenter + halfSize.bottomRight(Offset.zero),
);
}
}
2 changes: 1 addition & 1 deletion lib/src/map/camera/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ class MapCamera {
double getScaleZoom(double scale) => crs.zoom(scale * crs.scale(zoom));

/// Calculates the pixel bounds of this camera's [crs].
Bounds? getPixelWorldBounds(double? zoom) =>
Rect? getPixelWorldBounds(double? zoom) =>
crs.getProjectedBounds(zoom ?? this.zoom);

/// Calculates the [Offset] from the [pos] to this camera's [pixelOrigin].
Expand Down
27 changes: 13 additions & 14 deletions lib/src/map/camera/camera_fit.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:math' as math hide Point;
import 'dart:math' show Point;
import 'dart:math' as math;

import 'package:flutter/widgets.dart';
import 'package:flutter_map/flutter_map.dart';
Expand Down Expand Up @@ -144,21 +143,21 @@ class FitBounds extends CameraFit {
// Prevent negative size which results in NaN zoom value later on in the calculation

size = Size(math.max(0, size.width), math.max(0, size.height));
var boundsSize = Bounds(
camera.project(se, camera.zoom).toPoint(),
camera.project(nw, camera.zoom).toPoint(),
var boundsSize = Rect.fromPoints(
camera.project(se, camera.zoom),
camera.project(nw, camera.zoom),
).size;
if (camera.rotation != 0.0) {
final cosAngle = math.cos(camera.rotationRad).abs();
final sinAngle = math.sin(camera.rotationRad).abs();
boundsSize = Point<double>(
(boundsSize.x * cosAngle) + (boundsSize.y * sinAngle),
(boundsSize.y * cosAngle) + (boundsSize.x * sinAngle),
boundsSize = Size(
(boundsSize.width * cosAngle) + (boundsSize.width * sinAngle),
(boundsSize.height * cosAngle) + (boundsSize.height * sinAngle),
);
}

final scale =
math.min(size.width / boundsSize.x, size.height / boundsSize.y);
math.min(size.width / boundsSize.width, size.height / boundsSize.height);

var boundsZoom = camera.getScaleZoom(scale);

Expand Down Expand Up @@ -226,17 +225,17 @@ class FitInsideBounds extends CameraFit {

final cameraSize = camera.nonRotatedSize - paddingTotalXY as Size;

final projectedBoundsSize = Bounds(
camera.project(bounds.southEast, camera.zoom).toPoint(),
camera.project(bounds.northWest, camera.zoom).toPoint(),
final projectedBoundsSize = Rect.fromPoints(
camera.project(bounds.southEast, camera.zoom),
camera.project(bounds.northWest, camera.zoom),
).size;

final scale = _rectInRotRectScale(
angleRad: camera.rotationRad,
smallRectHalfWidth: cameraSize.width / 2.0,
smallRectHalfHeight: cameraSize.height / 2.0,
bigRectHalfWidth: projectedBoundsSize.x / 2.0,
bigRectHalfHeight: projectedBoundsSize.y / 2.0,
bigRectHalfWidth: projectedBoundsSize.width / 2.0,
bigRectHalfHeight: projectedBoundsSize.height / 2.0,
);

var newZoom = camera.getScaleZoom(1.0 / scale);
Expand Down
36 changes: 0 additions & 36 deletions lib/src/misc/bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,6 @@ class Bounds<T extends num> {
(point.y <= max.y);
}

/// Check if an other [Bounds] object is inside of the bounds.
bool containsBounds(Bounds<T> b) {
return (b.min.x >= min.x) &&
(b.max.x <= max.x) &&
(b.min.y >= min.y) &&
(b.max.y <= max.y);
}

/// Calculates the intersection of two Bounds. The return value will be null
/// if there is no intersection. The returned bounds may be zero size
/// (bottomLeft == topRight).
Expand All @@ -103,34 +95,6 @@ class Bounds<T extends num> {
return null;
}

/// Checks if the line between the two coordinates is contained within the
/// [Bounds].
bool aabbContainsLine(double x1, double y1, double x2, double y2) {
// Completely outside.
if ((x1 <= min.x && x2 <= min.x) ||
(y1 <= min.y && y2 <= min.y) ||
(x1 >= max.x && x2 >= max.x) ||
(y1 >= max.y && y2 >= max.y)) {
return false;
}

final m = (y2 - y1) / (x2 - x1);

double y = m * (min.x - x1) + y1;
if (y > min.y && y < max.y) return true;

y = m * (max.x - x1) + y1;
if (y > min.y && y < max.y) return true;

double x = (min.y - y1) / m + x1;
if (x > min.x && x < max.x) return true;

x = (max.y - y1) / m + x1;
if (x > min.x && x < max.x) return true;

return false;
}

@override
String toString() => 'Bounds($min, $max)';
}
32 changes: 16 additions & 16 deletions lib/src/misc/extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,27 +60,27 @@ extension RectExtension on Rect {
/// Checks if the line between the two coordinates is contained within the
/// [Rect].
bool aabbContainsLine(double x1, double y1, double x2, double y2) {
// // Completely outside.
// if ((x1 <= min.x && x2 <= min.x) ||
// (y1 <= min.y && y2 <= min.y) ||
// (x1 >= max.x && x2 >= max.x) ||
// (y1 >= max.y && y2 >= max.y)) {
// return false;
// }
// Completely outside.
if ((x1 <= left && x2 <= left) ||
(y1 <= top && y2 <= top) ||
(x1 >= right && x2 >= right) ||
(y1 >= bottom && y2 >= bottom)) {
return false;
}

// final m = (y2 - y1) / (x2 - x1);
final m = (y2 - y1) / (x2 - x1);

// double y = m * (min.x - x1) + y1;
// if (y > min.y && y < max.y) return true;
double y = m * (left - x1) + y1;
if (y > top && y < bottom) return true;

// y = m * (max.x - x1) + y1;
// if (y > min.y && y < max.y) return true;
y = m * (right - x1) + y1;
if (y > top && y < bottom) return true;

// double x = (min.y - y1) / m + x1;
// if (x > min.x && x < max.x) return true;
double x = (top - y1) / m + x1;
if (x > left && x < right) return true;

// x = (max.y - y1) / m + x1;
// if (x > min.x && x < max.x) return true;
x = (bottom - y1) / m + x1;
if (x > left && x < right) return true;

return false;
}
Expand Down
Loading

0 comments on commit b6d2d95

Please sign in to comment.