Skip to content

Commit

Permalink
Port boolean functions and tests (#91)
Browse files Browse the repository at this point in the history
* in process - early analysis of the work

* linked the meta functions in readme

* CONTRIBUTING.md

* links to the sources

* edited typos

* documentation work

* closes #77, closes #16

* proofread

* improves pub.dev score, closes #79, #78, #75

* Added deleted model, complemented contributing.md

* booleans init

* initial import

* boolean_touches

* valid - wip

* within - init

* rewiring the translated code

* contains etc.

* touches

* valid

* dependencies imported

* Update CONTRIBUTING.md

* clockwise test

* concave test

* intersect test

* pointOnLine test

* contains

* crosses_test - blocked by findIntersections

* boolean_contain

* boolean_within

* rhumb_bearing

* rhumb_bearing\'s test  - incomplete

* rhumb_bearing's test - done, added to lib/bearing

* boolean_contains, boolean_parallel, IP

* working on contain - removed usages of runtimeType

* rewriting pointInLine

* tests reverted to false/true folder style

* restructuring, clean-up

* clockwise and its test, documentation

* concave finalized

* PointInPolygon plus test, dart format

* removed the commented JS lines

* return type added

* disjoint and test - not done, depends on sweepline

* equal implement, test, has dependencies

* moving on

* clean coords init

* cleanCoord implemented, going to new branch

* moving on with equal

* update turf_equality dep

* moving forward

* refactors boolean_equal

* #88 crosses, disjoint, equal, intersects, pip @lukas-h

* cleanup on #88

* progress on overlap

* bool: overlap,parallel,valid,touches & lineOverlap

* issue with test is worked on

* still at porting the overlap test

* done: parallel

* working on valid

* touches done

* valid - got stuck

* progress on within

* moved on with valid, refactored

* valid test

* lineIntersection testet - contains sweepline

* package publishabe, rm git deps, fix equality dep

* update params in equality

* mod. on valid to cover the std. - MutiPs' 'finite'

* RFCs resolved - readied for merge

* return type of getGeom

* linter effect!

* reolved requests

* more requests resolved

* more requests resolved

* test name, touching lineOverlap

* fix conversations in booleanContains

* fixes conversations in crosses and disjoint

* resolves allllllllll outdated conversations, refactor

* use getGeom in booleanTouches

* use getGeom more frequently

* rm linter rule, which is already inherited from dartclub_lint

* cleanup, ready for merge

Co-authored-by: Lukas Himsel <[email protected]>
  • Loading branch information
armantorkzaban and lukas-h authored Sep 11, 2022
1 parent f503b90 commit b17c9bf
Show file tree
Hide file tree
Showing 440 changed files with 16,117 additions and 183 deletions.
38 changes: 37 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,42 @@ To put it simply, be kind to each other.
- Clone the repository: ```git clone [email protected]:dartclub/turf_dart.git```
- Navigate to project's folder in terminal & get its dependencies: ```dart pub get```
- Go through [Implementation Process](#implementation-process)
- Import the library in your code and use it. For example:
```dart
import 'package:turf/helpers.dart';
import 'package:turf/src/line_segment.dart';
Feature<Polygon> poly = Feature<Polygon>(
geometry: Polygon(coordinates: [
[
Position(0, 0),
Position(2, 2),
Position(0, 1),
Position(0, 0),
],
[
Position(0, 0),
Position(1, 1),
Position(0, 1),
Position(0, 0),
],
]),
);
var total = segmentReduce<int>(poly, (previousValue,
currentSegment,
initialValue,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex) {
if (previousValue != null) {
previousValue++;
}
return previousValue;
}, 0, combineNestedGeometries: false);
// total.length == 6
```

## Structure of modules
```
Expand Down Expand Up @@ -56,4 +92,4 @@ In order to add to this very documentation, please develop CONTRIBUTING.md in [d

## GeoJSON Object Model
If you have not read our [README.md](https://github.com/dartclub/turf_dart/blob/main/README.md) this diagram will give you a lot of information. Please consider looking our [notable design decisions](https://github.com/dartclub/turf_dart/blob/main/README.md#notable-design-decisions).
![polymorphism](https://user-images.githubusercontent.com/10634693/159876354-f9da2f37-02b3-4546-b32a-c0f82c372272.png)
![polymorphism](https://user-images.githubusercontent.com/10634693/159876354-f9da2f37-02b3-4546-b32a-c0f82c372272.png)
33 changes: 19 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Most of the implementation is a direct translation from [turf.js](https://github
- Get the [Dart tools](https://dart.dev/tools)
- Install the library with `dart pub add turf`
- Import the library in your code and use it. For example:

```dart
import 'package:turf/helpers.dart';
import 'package:turf/src/line_segment.dart';
Expand All @@ -36,7 +37,7 @@ Feature<Polygon> poly = Feature<Polygon>(
]),
);
main() {
void main() {
var total = segmentReduce<int>(
poly,
(previousValue, currentSegment, initialValue, featureIndex,
Expand Down Expand Up @@ -145,7 +146,7 @@ Any new benchmarks must be named `*_benchmark.dart` and reside in the
- [ ] kinks
- [ ] lineArc
- [ ] lineChunk
- [ ] lineIntersect
- [ ] [lineIntersect](https://github.com/dartclub/turf_dart/blob/main/lib/src/line_intersect.dart)
- [ ] lineOverlap
- [x] [lineSegment](https://github.com/dartclub/turf_dart/blob/main/lib/src/line_segment.dart)
- [ ] lineSlice
Expand Down Expand Up @@ -207,8 +208,6 @@ Any new benchmarks must be named `*_benchmark.dart` and reside in the
- [x] [featureReduce](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/feature.dart)
- [x] [flattenEach](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/flatten.dart)
- [x] [flattenReduce](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/flatten.dart)
- [x] [getCoord](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/coord.dart)
- [x] [getCoords](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/coord.dart)
- [x] [geomEach](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/geom.dart)
- [x] [geomReduce](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/geom.dart)
- [x] [propEach](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/prop.dart)
Expand All @@ -219,19 +218,25 @@ Any new benchmarks must be named `*_benchmark.dart` and reside in the
- [x] [clusterEach](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/cluster.dart)
- [x] [clusterReduce](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/cluster.dart)

### Invariants

- [x] [getCoord](https://github.com/dartclub/turf_dart/blob/main/lib/src/meta/coord.dart)
- [x] [getCoords](https://github.com/dartclub/turf_dart/blob/main/lib/src/invariant.dart)
- [x] [getGeom](https://github.com/dartclub/turf_dart/blob/main/lib/src/invariant.dart)

### Booleans

- [ ] booleanClockwise
- [ ] booleanConcave
- [ ] booleanContains
- [ ] booleanCrosses
- [ ] booleanDisjoint
- [ ] booleanEqual
- [ ] booleanIntersects
- [x] [booleanClockwise](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_clockwise.dart)
- [x] [booleanConcave](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_concave.dart)
- [x] [booleanContains](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_contains.dart)
- [x] [booleanCrosses](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_crosses.dart)
- [x] [booleanDisjoint](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_disjoint.dart)
- [x] [booleanEqual](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_equal.dart)
- [x] [booleanIntersects](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_intersect.dart)
- [ ] booleanOverlap
- [ ] booleanParallel
- [ ] booleanPointInPolygon
- [ ] booleanPointOnLine
- [x] [booleanParallel](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_parallel.dart)
- [x] [booleanPointInPolygon](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_point_in_polygon.dart)
- [x] [booleanPointOnLine](https://github.com/dartclub/turf_dart/blob/main/lib/src/booleans/boolean_point_on_line.dart)
- [ ] booleanWithin

### Unit Conversion
Expand Down
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ include: package:dartclub_lint/dart.yaml

analyzer:
errors:
prefer_generic_function_type_aliases: error
prefer_generic_function_type_aliases: error
2 changes: 1 addition & 1 deletion benchmark/area_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Feature<Polygon> poly = Feature<Polygon>(
]),
);

main() {
void main() {
group('area', () {
benchmark('simple', () {
area(poly);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/explode_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var poly = Polygon(coordinates: [
],
]);

main() {
void main() {
group('explode', () {
benchmark('simple', () {
explode(poly);
Expand Down
2 changes: 1 addition & 1 deletion benchmark/polygon_smooth_benchmark.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:benchmark/benchmark.dart';
import 'package:turf/polygon_smooth.dart';
import 'package:turf/turf.dart';

main() {
void main() {
group("turf-polygon-smooth", () {
var inDir = Directory('./test/examples/polygonSmooth/in');
for (var file in inDir.listSync(recursive: true)) {
Expand Down
2 changes: 1 addition & 1 deletion example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Feature<Polygon> poly = Feature<Polygon>(
]),
);

main() {
void main() {
var total = segmentReduce<int>(
poly,
(previousValue, currentSegment, initialValue, featureIndex,
Expand Down
47 changes: 47 additions & 0 deletions lib/src/booleans/boolean_clockwise.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:turf/src/invariant.dart';

import '../../helpers.dart';

/// Takes a ring and return [true] or [false] whether or not the ring is clockwise
/// or counter-clockwise.
/// Takes a [Feature<LineString>] or[LineString] or a [List<Position>] to be
/// evaluated.
/// example:
/// ```dart
/// var clockwiseRing = LineString(
/// coordinates: [
/// Position.of([0, 0]),
/// Position.of([1, 1]),
/// Position.of([1, 0]),
/// Position.of([0, 0])
/// ],
/// );
/// var counterClockwiseRing = LineString(
/// coordinates: [
/// Position.of([0, 0]),
/// Position.of([1, 0]),
/// Position.of([1, 1]),
/// Position.of([0, 0])
/// ],
/// );
///
/// booleanClockwise(clockwiseRing);
/// //=true
/// booleanClockwise(counterClockwiseRing);
/// //=false
/// ```
bool booleanClockwise(LineString line) {
var ring = getCoords(line) as List<Position>;
num sum = 0;
int i = 1;
Position prev;
Position? cur;

while (i < ring.length) {
prev = cur ?? ring[0];
cur = ring[i];
sum += (cur[0]! - prev[0]!) * (cur[1]! + prev[1]!);
i++;
}
return sum > 0;
}
42 changes: 42 additions & 0 deletions lib/src/booleans/boolean_concave.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:turf/helpers.dart';

/// Takes a [Polygon] and returns [true] or [false] as to whether it is concave
/// or not.
/// example:
/// ```dart
/// var convexPolygon = Polygon(coordinates: [
/// [
/// Position.of([0, 0]),
/// Position.of([0, 1]),
/// Position.of([1, 1]),
/// Position.of([1, 0]),
/// Position.of([0, 0])
/// ]
/// ]);
/// booleanConcave(convexPolygon);
/// //=false
/// ```
bool booleanConcave(Polygon polygon) {
// Taken from https://stackoverflow.com/a/1881201 & https://stackoverflow.com/a/25304159
List<List<Position>> coords = polygon.coordinates;

if (coords[0].length <= 4) {
return false;
}

var sign = false;
var n = coords[0].length - 1;
for (var i = 0; i < n; i++) {
var dx1 = coords[0][(i + 2) % n][0]! - coords[0][(i + 1) % n][0]!;
var dy1 = coords[0][(i + 2) % n][1]! - coords[0][(i + 1) % n][1]!;
var dx2 = coords[0][i][0]! - coords[0][(i + 1) % n][0]!;
var dy2 = coords[0][i][1]! - coords[0][(i + 1) % n][1]!;
var zcrossproduct = dx1 * dy2 - dy1 * dx2;
if (i == 0) {
sign = zcrossproduct > 0;
} else if (sign != zcrossproduct > 0) {
return true;
}
}
return false;
}
Loading

0 comments on commit b17c9bf

Please sign in to comment.