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

perf: expose isPointInPolygon and make 40% faster (at least in JIT mode) #1907

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
75 changes: 75 additions & 0 deletions benchmark/point_in_polygon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'dart:async';
import 'dart:math' as math;
import 'dart:ui';

import 'package:flutter_map/src/misc/point_in_polygon.dart';
import 'package:logger/logger.dart';

class NoFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) => true;
}

typedef Result = ({
String name,
Duration duration,
});

Future<Result> timedRun(String name, dynamic Function() body) async {
Logger().i('running $name...');
final watch = Stopwatch()..start();
await body();
watch.stop();

return (name: name, duration: watch.elapsed);
}

// NOTE: to have a more prod like comparison, run with:
// $ dart compile exe benchmark/crs.dart && ./benchmark/crs.exe
//
// If you run in JIT mode, the resulting execution times will be a lot more similar.
Future<void> main() async {
Logger.level = Level.all;
Logger.defaultFilter = NoFilter.new;
Logger.defaultPrinter = SimplePrinter.new;

final results = <Result>[];
const N = 3000000;
const points = 1000;

results.add(await timedRun('In circle', () {
final polygon = List.generate(points, (i) {
final angle = math.pi * 2 / points * i;
return Offset(math.cos(angle), math.sin(angle));
});

const point = math.Point(0, 0);

bool yesPlease = true;
for (int i = 0; i < N; ++i) {
yesPlease = yesPlease && pointInPolygon(point, polygon);
}
ignatz marked this conversation as resolved.
Show resolved Hide resolved

assert(yesPlease, 'should be in circle');
return yesPlease;
}));

results.add(await timedRun('Not in circle', () {
final polygon = List.generate(points, (i) {
final angle = math.pi * 2 / points * i;
return Offset(math.cos(angle), math.sin(angle));
});

const point = math.Point(4, 4);

bool noSir = false;
for (int i = 0; i < N; ++i) {
noSir = noSir || pointInPolygon(point, polygon);
}

assert(!noSir, 'should not be in circle');
return noSir;
}));

Logger().i('Results:\n${results.map((r) => r.toString()).join('\n')}');
}
2 changes: 1 addition & 1 deletion lib/src/geo/crs.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:math' as math hide Point;
import 'dart:math' show Point;

import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/misc/bounds.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';
import 'package:proj4dart/proj4dart.dart' as proj4;
Expand Down
24 changes: 2 additions & 22 deletions lib/src/layer/polygon_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,9 @@ base class _PolygonPainter<R extends Object>
}
}

final isInPolygon = _isPointInPolygon(point, projectedCoords);
final isInPolygon = pointInPolygon(point, projectedCoords);
final isInHole = hasHoles &&
projectedHoleCoords
.map((c) => _isPointInPolygon(point, c))
.any((e) => e);
JaffaKetchup marked this conversation as resolved.
Show resolved Hide resolved
projectedHoleCoords.map((c) => pointInPolygon(point, c)).any((e) => e);

// Second check handles case where polygon outline intersects a hole,
// ensuring that the hit matches with the visual representation
Expand Down Expand Up @@ -361,24 +359,6 @@ base class _PolygonPainter<R extends Object>
);
}

/// Checks whether point [p] is within the specified closed [polygon]
///
/// Uses the even-odd algorithm.
static bool _isPointInPolygon(math.Point p, List<Offset> polygon) {
bool isInPolygon = false;

for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
if ((((polygon[i].dy <= p.y) && (p.y < polygon[j].dy)) ||
((polygon[j].dy <= p.y) && (p.y < polygon[i].dy))) &&
(p.x <
(polygon[j].dx - polygon[i].dx) *
(p.y - polygon[i].dy) /
(polygon[j].dy - polygon[i].dy) +
polygon[i].dx)) isInPolygon = !isInPolygon;
}
return isInPolygon;
}

@override
bool shouldRepaint(_PolygonPainter<R> oldDelegate) =>
polygons != oldDelegate.polygons ||
Expand Down
1 change: 1 addition & 0 deletions lib/src/layer/polygon_layer/polygon_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/src/layer/shared/layer_interactivity/internal_hit_detectable.dart';
import 'package:flutter_map/src/layer/shared/line_patterns/pixel_hiker.dart';
import 'package:flutter_map/src/misc/offsets.dart';
import 'package:flutter_map/src/misc/point_in_polygon.dart';
import 'package:flutter_map/src/misc/simplify.dart';
import 'package:latlong2/latlong.dart' hide Path;
import 'package:polylabel/polylabel.dart';
Expand Down
25 changes: 25 additions & 0 deletions lib/src/misc/point_in_polygon.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:math' as math;
import 'dart:ui';

/// Checks whether point [p] is within the specified closed [polygon]
///
/// Uses the even-odd algorithm.
bool pointInPolygon(math.Point p, List<Offset> polygon) {
ignatz marked this conversation as resolved.
Show resolved Hide resolved
final double px = p.x.toDouble();
final double py = p.y.toDouble();

bool isInPolygon = false;
for (int i = 0, j = polygon.length - 1; i < polygon.length; j = i++) {
final double poIx = polygon[i].dx;
final double poIy = polygon[i].dy;

final double poJx = polygon[j].dx;
final double poJy = polygon[j].dy;

if ((((poIy <= py) && (py < poJy)) || ((poJy <= py) && (py < poIy))) &&
(px < (poJx - poIx) * (py - poIy) / (poJy - poIy) + poIx)) {
isInPolygon = !isInPolygon;
}
}
return isInPolygon;
}
38 changes: 38 additions & 0 deletions test/misc/point_in_polygon_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import 'dart:math' as math;

import 'package:flutter_map/src/misc/point_in_polygon.dart';
import 'package:flutter_test/flutter_test.dart';

List<Offset> makeCircle(int points, double radius, double phase) {
return List.generate(points, (i) {
final angle = math.pi * 2 / points * i + phase;
return Offset(radius * math.cos(angle), radius * math.sin(angle));
}, growable: false);
}

void main() {
test('Smoke test for points in and out of polygons', () {
final circle = makeCircle(100, 1, 0);

// Inside points
for (final point in makeCircle(32, 0.8, 0.0001)) {
final p = math.Point(point.dx, point.dy);
expect(pointInPolygon(p, circle), isTrue);
}

// Edge-case: check origin
expect(pointInPolygon(const math.Point(0, 0), circle), isTrue);

// Outside points: small radius
for (final point in makeCircle(32, 1.1, 0.0001)) {
final p = math.Point(point.dx, point.dy);
expect(pointInPolygon(p, circle), isFalse);
}

// Outside points: large radius
for (final point in makeCircle(32, 100000, 0.0001)) {
final p = math.Point(point.dx, point.dy);
expect(pointInPolygon(p, circle), isFalse);
}
});
}
Loading