Skip to content

Commit

Permalink
convert Bounds to IntegerBounds and made internal
Browse files Browse the repository at this point in the history
most methods on integer bounds are not used
  • Loading branch information
mootw committed Dec 5, 2024
1 parent a2d88a1 commit 57deeab
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 105 deletions.
2 changes: 1 addition & 1 deletion lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ class Proj4Crs extends Crs {
/// Inherit from this class if you want to create or implement your own CRS.
@immutable
abstract class Projection {
/// The [Bounds] for the coordinates of this [Projection].
/// The [IntegerBounds] for the coordinates of this [Projection].
final Rect? bounds;

/// Base constructor for the abstract [Projection] class that sets the
Expand Down
12 changes: 6 additions & 6 deletions lib/src/layer/tile_layer/tile_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Point<int> _ceil(Offset point) => Point<int>(point.dx.ceil(), point.dy.ceil());
@immutable
class DiscreteTileRange extends TileRange {
/// Bounds are inclusive
final Bounds<int> _bounds;
final IntegerBounds _bounds;

/// Create a new [DiscreteTileRange] by setting it's values.
const DiscreteTileRange(super.zoom, this._bounds);
Expand All @@ -51,12 +51,12 @@ class DiscreteTileRange extends TileRange {
required int tileDimension,
required Rect pixelBounds,
}) {
final Bounds<int> bounds;
final IntegerBounds bounds;
if (pixelBounds.isEmpty) {
final minAndMax = _floor(pixelBounds.min / tileDimension.toDouble());
bounds = Bounds<int>(minAndMax, minAndMax);
bounds = IntegerBounds(minAndMax, minAndMax);
} else {
bounds = Bounds<int>(
bounds = IntegerBounds(
_floor(pixelBounds.min / tileDimension.toDouble()),
_ceil(pixelBounds.max / tileDimension.toDouble()) - const Point(1, 1),
);
Expand Down Expand Up @@ -95,7 +95,7 @@ class DiscreteTileRange extends TileRange {

return DiscreteTileRange(
zoom,
Bounds<int>(
IntegerBounds(
Point<int>(math.max(min.x, minX), min.y),
Point<int>(math.min(max.x, maxX), max.y),
),
Expand All @@ -110,7 +110,7 @@ class DiscreteTileRange extends TileRange {

return DiscreteTileRange(
zoom,
Bounds<int>(
IntegerBounds(
Point<int>(min.x, math.max(min.y, minY)),
Point<int>(max.x, math.min(max.y, maxY)),
),
Expand Down
6 changes: 3 additions & 3 deletions lib/src/layer/tile_layer/wms_tile_layer_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ class WMSTileLayerOptions {
crs.offsetToLatLng(sePoint.toOffset(), coords.z.toDouble());
final nw = crs.projection.project(nwCoords);
final se = crs.projection.project(seCoords);
final bounds = Bounds(nw.toPoint(), se.toPoint());
final bounds = Rect.fromPoints(nw, se);
final bbox = (_versionNumber >= 1.3 && crs is Epsg4326)
? [bounds.min.y, bounds.min.x, bounds.max.y, bounds.max.x]
: [bounds.min.x, bounds.min.y, bounds.max.x, bounds.max.y];
? [bounds.min.dy, bounds.min.dx, bounds.max.dy, bounds.max.dx]
: [bounds.min.dx, bounds.min.dy, bounds.max.dx, bounds.max.dy];

final buffer = StringBuffer(_encodedBaseUrl);
buffer.write('&width=${retinaMode ? tileDimension * 2 : tileDimension}');
Expand Down
67 changes: 34 additions & 33 deletions lib/src/misc/bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,88 +8,89 @@ import 'package:meta/meta.dart';
/// Rectangular bound delimited by orthogonal lines passing through two
/// points.
@immutable
class Bounds<T extends num> {
/// The edge of the bounds with the minimum x and y coordinate
final Point<T> min;

/// The edge of the bounds with the maximum x and y coordinate
final Point<T> max;

/// Create a [Bounds] instance in a safe way.
factory Bounds(Point<T> a, Point<T> b) {
final T minX;
final T maxX;
@internal
class IntegerBounds {
/// inthe edge of the bounds with the minimum x and y coordinate
final Point<int> min;

/// inthe edge of the bounds with the maximum x and y coordinate
final Point<int> max;

/// Create a [IntegerBounds] instance in a safe way.
factory IntegerBounds(Point<int> a, Point<int> b) {
final int minX;
final int maxX;
if (a.x > b.x) {
minX = b.x;
maxX = a.x;
} else {
minX = a.x;
maxX = b.x;
}
final T minY;
final T maxY;
final int minY;
final int maxY;
if (a.y > b.y) {
minY = b.y;
maxY = a.y;
} else {
minY = a.y;
maxY = b.y;
}
return Bounds.unsafe(Point<T>(minX, minY), Point<T>(maxX, maxY));
return IntegerBounds.unsafe(Point<int>(minX, minY), Point<int>(maxX, maxY));
}

/// Create a [Bounds] instance **without** checking if [min] is actually the
/// Create a [IntegerBounds] instance **without** checking if [min] is actually the
/// minimum and [max] is actually the maximum.
const Bounds.unsafe(this.min, this.max);
const IntegerBounds.unsafe(this.min, this.max);

/// Creates a new [Bounds] obtained by expanding the current ones with a new
/// Creates a new [IntegerBounds] obtained by expanding the current ones with a new
/// point.
Bounds<T> extend(Point<T> point) {
return Bounds.unsafe(
IntegerBounds extend(Point<int> point) {
return IntegerBounds.unsafe(
Point(math.min(point.x, min.x), math.min(point.y, min.y)),
Point(math.max(point.x, max.x), math.max(point.y, max.y)),
);
}

/// This [Bounds] central point.
Offset get center => (min.toOffset() + max.toOffset()) / 2;
/// inthis [IntegerBounds] central point.
Offset get center => (min + max).toOffset() / 2;

/// Bottom-Left corner's point.
Point<T> get bottomLeft => Point(min.x, max.y);
Point<int> get bottomLeft => Point(min.x, max.y);

/// Top-Right corner's point.
Point<T> get topRight => Point(max.x, min.y);
/// intop-Right corner's point.
Point<int> get topRight => Point(max.x, min.y);

/// Top-Left corner's point.
Point<T> get topLeft => min;
/// intop-Left corner's point.
Point<int> get topLeft => min;

/// Bottom-Right corner's point.
Point<T> get bottomRight => max;
Point<int> get bottomRight => max;

/// A point that contains the difference between the point's axis projections.
Point<T> get size {
Point<int> get size {
return max - min;
}

/// Check if a [Point] is inside of the bounds.
bool contains(Point<T> point) {
bool contains(Point<int> point) {
return (point.x >= min.x) &&
(point.x <= max.x) &&
(point.y >= min.y) &&
(point.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
/// Calculates the intersection of two Bounds. inthe return value will be null
/// if there is no intersection. inthe returned bounds may be zero size
/// (bottomLeft == topRight).
Bounds<T>? intersect(Bounds<T> b) {
IntegerBounds? intersect(IntegerBounds b) {
final leftX = math.max(min.x, b.min.x);
final rightX = math.min(max.x, b.max.x);
final topY = math.max(min.y, b.min.y);
final bottomY = math.min(max.y, b.max.y);

if (leftX <= rightX && topY <= bottomY) {
return Bounds.unsafe(Point(leftX, topY), Point(rightX, bottomY));
return IntegerBounds.unsafe(Point(leftX, topY), Point(rightX, bottomY));
}

return null;
Expand Down
Loading

0 comments on commit 57deeab

Please sign in to comment.