Skip to content

Commit

Permalink
Minor formatting improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
JaffaKetchup committed Jan 16, 2024
1 parent e7e1b82 commit 23eae12
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 58 deletions.
14 changes: 7 additions & 7 deletions lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,6 @@ abstract class CrsWithStaticTransformation extends Crs {
@override
final Projection projection;

@override
(double, double) transform(double x, double y, double scale) =>
_transformation.transform(x, y, scale);
@override
(double, double) untransform(double x, double y, double scale) =>
_transformation.untransform(x, y, scale);

const CrsWithStaticTransformation._({
required _Transformation transformation,
required this.projection,
Expand All @@ -82,6 +75,13 @@ abstract class CrsWithStaticTransformation extends Crs {
super.wrapLat,
}) : _transformation = transformation;

@override
(double, double) transform(double x, double y, double scale) =>
_transformation.transform(x, y, scale);
@override
(double, double) untransform(double x, double y, double scale) =>
_transformation.untransform(x, y, scale);

@override
(double, double) latLngToXY(LatLng latlng, double scale) {
final (x, y) = projection.projectXY(latlng);
Expand Down
41 changes: 27 additions & 14 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,36 @@ class _ProjectedPolygon {

_ProjectedPolygon.fromPolygon(Projection projection, Polygon polygon)
: this._(
polygon: polygon,
points: List<DoublePoint>.generate(polygon.points.length, (j) {
polygon: polygon,
points: List<DoublePoint>.generate(
polygon.points.length,
(j) {
final (x, y) = projection.projectXY(polygon.points[j]);
return DoublePoint(x, y);
}, growable: false),
holePoints: () {
final holes = polygon.holePointsList;
if (holes == null) return null;

return List<List<DoublePoint>>.generate(holes.length, (j) {
},
growable: false,
),
holePoints: () {
final holes = polygon.holePointsList;
if (holes == null) return null;

return List<List<DoublePoint>>.generate(
holes.length,
(j) {
final points = holes[j];
return List<DoublePoint>.generate(points.length, (k) {
final (x, y) = projection.projectXY(points[k]);
return DoublePoint(x, y);
}, growable: false);
}, growable: false);
}());
return List<DoublePoint>.generate(
points.length,
(k) {
final (x, y) = projection.projectXY(points[k]);
return DoublePoint(x, y);
},
growable: false,
);
},
growable: false,
);
}(),
);
}

class _PolygonPainter extends CustomPainter {
Expand Down
44 changes: 26 additions & 18 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PolygonLayer extends StatefulWidget {
}

class _PolygonLayerState extends State<PolygonLayer> {
List<_ProjectedPolygon>? _polygons;
List<_ProjectedPolygon>? _cachedProjectedPolygons;
final _cachedSimplifiedPolygons = <int, List<_ProjectedPolygon>>{};

@override
Expand All @@ -81,30 +81,37 @@ class _PolygonLayerState extends State<PolygonLayer> {
}

_cachedSimplifiedPolygons.clear();
_polygons = null;
_cachedProjectedPolygons = null;
}

@override
Widget build(BuildContext context) {
final camera = MapCamera.of(context);
final zoom = camera.zoom.floor();

final projectedPolygons = _polygons ??=
List<_ProjectedPolygon>.generate(widget.polygons.length, (i) {
final polygon = widget.polygons[i];
return _ProjectedPolygon.fromPolygon(camera.crs.projection, polygon);
}, growable: false);
final projected = _cachedProjectedPolygons ??= List.generate(
widget.polygons.length,
(i) => _ProjectedPolygon.fromPolygon(
camera.crs.projection,
widget.polygons[i],
),
growable: false,
);

final zoom = camera.zoom.floor();
final simplified = widget.simplificationTolerance == 0
? projectedPolygons
? projected
: _cachedSimplifiedPolygons[zoom] ??= _computeZoomLevelSimplification(
projectedPolygons, widget.simplificationTolerance, zoom);
projected,
widget.simplificationTolerance,
zoom,
);

final culled = !widget.polygonCulling
? simplified
: simplified
.where((p) =>
p.polygon.boundingBox.isOverlapping(camera.visibleBounds))
.where(
(p) => p.polygon.boundingBox.isOverlapping(camera.visibleBounds),
)
.toList();

return MobileLayerTransformer(
Expand Down Expand Up @@ -140,14 +147,15 @@ class _PolygonLayerState extends State<PolygonLayer> {
),
holePoints: holes == null
? null
: List<List<DoublePoint>>.generate(holes.length, (j) {
final hole = holes[j];
return simplifyPoints(
hole,
: List<List<DoublePoint>>.generate(
holes.length,
(j) => simplifyPoints(
holes[j],
tolerance / math.pow(2, zoom),
highestQuality: true,
);
}),
),
growable: false,
),
);
},
growable: false,
Expand Down
24 changes: 13 additions & 11 deletions lib/src/layer/polyline_layer/painter.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
part of 'polyline_layer.dart';

class PolylinePainter<R extends Object> extends CustomPainter {
class _PolylinePainter<R extends Object> extends CustomPainter {
final List<Polyline<R>> polylines;
final MapCamera camera;
final LayerHitNotifier<R>? hitNotifier;
final double minimumHitbox;

final _hits = <R>[]; // Avoids repetitive memory reallocation

PolylinePainter({
_PolylinePainter({
required this.polylines,
required this.camera,
required this.hitNotifier,
Expand Down Expand Up @@ -64,14 +64,16 @@ class PolylinePainter<R extends Object> extends CustomPainter {
final o1 = offsets[i];
final o2 = offsets[i + 1];

final distance = math.sqrt(getSqSegDist(
position.dx,
position.dy,
o1.dx,
o1.dy,
o2.dx,
o2.dy,
));
final distance = math.sqrt(
getSqSegDist(
position.dx,
position.dy,
o1.dx,
o1.dy,
o2.dx,
o2.dy,
),
);

if (distance < hittableDistance) {
_hits.add(polyline.hitValue!);
Expand Down Expand Up @@ -279,7 +281,7 @@ class PolylinePainter<R extends Object> extends CustomPainter {
}

@override
bool shouldRepaint(PolylinePainter<R> oldDelegate) => false;
bool shouldRepaint(_PolylinePainter<R> oldDelegate) => false;
}

const _distance = Distance();
2 changes: 1 addition & 1 deletion lib/src/layer/polyline_layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class _PolylineLayerState<R extends Object> extends State<PolylineLayer<R>> {

return MobileLayerTransformer(
child: CustomPaint(
painter: PolylinePainter(
painter: _PolylinePainter(
polylines: culled,
camera: camera,
hitNotifier: widget.hitNotifier,
Expand Down
5 changes: 4 additions & 1 deletion lib/src/misc/offsets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ List<Offset> getOffsets(MapCamera camera, Offset origin, List<LatLng> points) {
}

List<Offset> getOffsetsXY(
MapCamera camera, Offset origin, List<DoublePoint> points) {
MapCamera camera,
Offset origin,
List<DoublePoint> points,
) {
// Critically create as little garbage as possible. This is called on every frame.
final crs = camera.crs;
final zoomScale = crs.scale(camera.zoom);
Expand Down
14 changes: 8 additions & 6 deletions lib/src/misc/simplify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ List<DoublePoint> simplifyDouglasPeucker(
return simplified;
}

/// high quality simplification uses the Ramer-Douglas-Peucker algorithm
/// otherwise it just merges close points
List<LatLng> simplify(
List<LatLng> points,
double tolerance, {
Expand All @@ -127,19 +125,23 @@ List<LatLng> simplify(
// Don't simplify anything less than a square
if (points.length <= 4) return points;

List<DoublePoint> nextPoints = List<DoublePoint>.generate(points.length,
(i) => DoublePoint(points[i].longitude, points[i].latitude));
List<DoublePoint> nextPoints = List<DoublePoint>.generate(
points.length,
(i) => DoublePoint(points[i].longitude, points[i].latitude),
);
final double sqTolerance = tolerance * tolerance;
nextPoints =
highestQuality ? nextPoints : simplifyRadialDist(nextPoints, sqTolerance);
nextPoints = simplifyDouglasPeucker(nextPoints, sqTolerance);

return List<LatLng>.generate(
nextPoints.length, (i) => LatLng(nextPoints[i].y, nextPoints[i].x));
nextPoints.length,
(i) => LatLng(nextPoints[i].y, nextPoints[i].x),
);
}

List<DoublePoint> simplifyPoints(
List<DoublePoint> points,
final List<DoublePoint> points,
double tolerance, {
bool highestQuality = false,
}) {
Expand Down

0 comments on commit 23eae12

Please sign in to comment.