Skip to content

Commit

Permalink
perf: avoid caching a single multiplication (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
mootw authored Dec 2, 2023
1 parent ad8318b commit df40d8c
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 31 deletions.
19 changes: 5 additions & 14 deletions example/lib/plugins/scalebar_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@ import 'dart:math';
import 'dart:ui';

import 'package:latlong2/latlong.dart';

const double piOver180 = pi / 180.0;

double toDegrees(double radians) {
return radians / piOver180;
}

double toRadians(double degrees) {
return degrees * piOver180;
}
import 'package:vector_math/vector_math_64.dart';

LatLng calculateEndingGlobalCoordinates(
LatLng start, double startBearing, double distance) {
Expand All @@ -25,8 +16,8 @@ LatLng calculateEndingGlobalCoordinates(
const aSquared = a * a;
const bSquared = b * b;
const f = mFlattening;
final phi1 = toRadians(start.latitude);
final alpha1 = toRadians(startBearing);
final phi1 = degrees2Radians * start.latitude;
final alpha1 = degrees2Radians * startBearing;
final cosAlpha1 = cos(alpha1);
final sinAlpha1 = sin(alpha1);
final s = distance;
Expand Down Expand Up @@ -138,7 +129,7 @@ LatLng calculateEndingGlobalCoordinates(

// build result
return LatLng(
clampDouble(toDegrees(phi2), -90, 90),
clampDouble(start.longitude + toDegrees(L), -180, 180),
clampDouble(radians2Degrees * phi2, -90, 90),
clampDouble(start.longitude + (L * radians2Degrees), -180, 180),
);
}
1 change: 1 addition & 0 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies:
shared_preferences: ^2.2.1
url_strategy: ^0.2.0
http: ^1.1.0
vector_math: ^2.1.2

dependency_overrides:
flutter_map_cancellable_tile_provider:
Expand Down
6 changes: 4 additions & 2 deletions lib/src/geo/latlng_bounds.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:math' as math;

import 'package:latlong2/latlong.dart';
import 'package:vector_math/vector_math_64.dart';

/// Data structure representing rectangular bounding box constrained by its
/// northwest and southeast corners
Expand Down Expand Up @@ -98,8 +99,9 @@ class LatLngBounds {
final lambda1 = southWest.longitudeInRad;
final phi2 = northEast.latitudeInRad;

final dLambda = degToRadian(northEast.longitude -
southWest.longitude); // delta lambda = lambda2-lambda1
final dLambda = degrees2Radians *
(northEast.longitude -
southWest.longitude); // delta lambda = lambda2-lambda1

final bx = math.cos(phi2) * math.cos(dLambda);
final by = math.cos(phi2) * math.sin(dLambda);
Expand Down
3 changes: 2 additions & 1 deletion lib/src/gestures/flutter_map_interactive_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter_map/src/map/options/interaction.dart';
import 'package:flutter_map/src/map/options/options.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:latlong2/latlong.dart';
import 'package:vector_math/vector_math_64.dart';

typedef InteractiveViewerBuilder = Widget Function(
BuildContext context,
Expand Down Expand Up @@ -660,7 +661,7 @@ class FlutterMapInteractiveViewerState
final rotationCenter =
_camera.project(_camera.offsetToCrs(_lastFocalLocal));
final vector = oldCenterPt - rotationCenter;
final rotatedVector = vector.rotate(degToRadian(rotationDiff));
final rotatedVector = vector.rotate(degrees2Radians * rotationDiff);
final newCenter = rotationCenter + rotatedVector;

widget.controller.moveAndRotate(
Expand Down
18 changes: 5 additions & 13 deletions lib/src/map/camera/camera.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_map/src/map/options/options.dart';
import 'package:flutter_map/src/misc/bounds.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:latlong2/latlong.dart';
import 'package:vector_math/vector_math_64.dart';

/// Describes the view of a map. This includes the size/zoom/position/crs as
/// well as the minimum/maximum zoom. This class is mostly immutable but has
Expand Down Expand Up @@ -53,9 +54,6 @@ class MapCamera {
/// Lazily calculated field
Point<double>? _pixelOrigin;

/// Lazily calculated field
double? _rotationRad;

/// This is the [LatLngBounds] corresponding to four corners of this camera.
/// This takes rotation in to account.
LatLngBounds get visibleBounds => _bounds ??= LatLngBounds(
Expand Down Expand Up @@ -105,12 +103,10 @@ class MapCamera {
Bounds<double>? pixelBounds,
LatLngBounds? bounds,
Point<double>? pixelOrigin,
double? rotationRad,
}) : _cameraSize = size,
_pixelBounds = pixelBounds,
_bounds = bounds,
_pixelOrigin = pixelOrigin,
_rotationRad = rotationRad;
_pixelOrigin = pixelOrigin;

/// Initializes [MapCamera] from the given [options] and with the
/// [nonRotatedSize] set to [kImpossibleSize].
Expand All @@ -135,7 +131,6 @@ class MapCamera {
nonRotatedSize: nonRotatedSize,
minZoom: minZoom,
maxZoom: maxZoom,
rotationRad: _rotationRad,
);
}

Expand Down Expand Up @@ -171,7 +166,6 @@ class MapCamera {
rotation: rotation,
nonRotatedSize: nonRotatedSize,
size: _cameraSize,
rotationRad: _rotationRad,
);
}

Expand All @@ -189,7 +183,6 @@ class MapCamera {
rotation: rotation,
nonRotatedSize: nonRotatedSize,
size: _cameraSize,
rotationRad: _rotationRad,
);

/// Calculates the size of a bounding box which surrounds a box of size
Expand All @@ -200,7 +193,7 @@ class MapCamera {
) {
if (rotation == 0.0) return nonRotatedSize;

final rotationRad = degToRadian(rotation);
final rotationRad = degrees2Radians * rotation;
final cosAngle = math.cos(rotationRad).abs();
final sinAngle = math.sin(rotationRad).abs();
final width = (nonRotatedSize.x * cosAngle) + (nonRotatedSize.y * sinAngle);
Expand All @@ -210,9 +203,8 @@ class MapCamera {
return Point<double>(width, height);
}

/// The current rotation value in radians. This is calculated and cached when
/// it is first called.
double get rotationRad => _rotationRad ??= degToRadian(rotation);
/// The current rotation value in radians
double get rotationRad => rotation * degrees2Radians;

/// Calculates point value for the given [latLng] using this camera's
/// [crs] and [zoom] (or the provided [zoom]).
Expand Down
3 changes: 2 additions & 1 deletion lib/src/map/controller/internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:flutter_map/src/misc/move_and_rotate_result.dart';
import 'package:flutter_map/src/misc/point_extensions.dart';
import 'package:flutter_map/src/misc/position.dart';
import 'package:latlong2/latlong.dart';
import 'package:vector_math/vector_math_64.dart';

/// This controller is for internal use. All updates to the state should be done
/// by calling methods of this class to ensure consistency.
Expand Down Expand Up @@ -190,7 +191,7 @@ class FlutterMapInternalController extends ValueNotifier<_InternalState> {
camera.unproject(
rotationCenter +
(camera.project(camera.center) - rotationCenter)
.rotate(degToRadian(rotationDiff)),
.rotate(degrees2Radians * rotationDiff),
),
camera.zoom,
offset: Offset.zero,
Expand Down

0 comments on commit df40d8c

Please sign in to comment.