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

Survey 3D #1293

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
GeoTools: Add a polygon complex/simple test in PolygonTools
arthurbenemann committed Nov 25, 2014
commit c2383777f927e651b4f03a8284b67d5cee561ddc
40 changes: 34 additions & 6 deletions Core/src/org/droidplanner/core/helpers/geoTools/PolygonTools.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
package org.droidplanner.core.helpers.geoTools;

import java.util.ArrayList;
import java.util.List;

import org.droidplanner.core.helpers.coordinates.Coord2D;
import org.droidplanner.core.polygon.Polygon;

public class PolygonTools {

public static Polygon offsetPolygon(Polygon polygon) {
ArrayList<LineCoord2D> offsetLines = new ArrayList<LineCoord2D>();
public static Polygon offsetPolygon(Polygon polygon) throws Exception {
if (!PolygonTools.isSimplePolygon(polygon)) {
throw new Exception("Complex Polygon");
}

ArrayList<LineCoord2D> offsetLines = new ArrayList<LineCoord2D>();
for (LineCoord2D line : polygon.getLines()) {
offsetLines.add(LineTools.getParallelLineToTheLeft(line,10));
offsetLines.add(LineTools.getParallelLineToTheLeft(line, 10));
}

ArrayList<Coord2D> path = new ArrayList<Coord2D>();
offsetLines.add(offsetLines.get(0));
offsetLines.add(offsetLines.get(1));
for (int i = 1; i < offsetLines.size(); i++) {
try {
path.add(LineTools.FindLineIntersection(offsetLines.get(i-1), offsetLines.get(i),true));
path.add(LineTools.FindLineIntersection(offsetLines.get(i - 1), offsetLines.get(i),
true));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Polygon polygonwithOffset = new Polygon();
polygonwithOffset.addPoints(path);
return polygonwithOffset;
}

public static boolean isSimplePolygon(Polygon polygon) {
List<LineCoord2D> lines = polygon.getLines();
for (LineCoord2D line1 : lines) {
for (LineCoord2D line2 : lines) {
if (line1.equals(line2)) {
continue;
}
try{
Coord2D intersection = LineTools.FindLineIntersection(line1, line2);
if (intersection.equals(line1.getStart())
|| intersection.equals(line1.getEnd())
|| intersection.equals(line2.getStart())
|| intersection.equals(line2.getEnd())) {
continue;
}
return false;
} catch (Exception e) {
}
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.droidplanner.core.helpers.geoTools;

import org.droidplanner.core.helpers.coordinates.Coord2D;
import org.droidplanner.core.polygon.Polygon;

import junit.framework.TestCase;

public class PolygonToolsTest extends TestCase {

public void testIsSimplePolygon() {
Polygon polygon = new Polygon();

polygon.addPoint(new Coord2D(0, 0));
polygon.addPoint(new Coord2D(0, 1));
polygon.addPoint(new Coord2D(1, 1));
polygon.addPoint(new Coord2D(1, 0));

assertTrue(PolygonTools.isSimplePolygon(polygon));
}

public void testIsComplexPolygon() {
Polygon polygon = new Polygon();

polygon.addPoint(new Coord2D(0, 0));
polygon.addPoint(new Coord2D(1, 1));
polygon.addPoint(new Coord2D(0, 1));
polygon.addPoint(new Coord2D(1, 0));

assertFalse(PolygonTools.isSimplePolygon(polygon));
}
}