kopia lustrzana https://github.com/onthegomap/planetiler
fix is convex negated (#241)
rodzic
27cf19cca4
commit
f93e5221f8
|
@ -403,11 +403,20 @@ public class GeoUtils {
|
||||||
return JTS_FACTORY.createPolygon(exteriorRing, rings.toArray(LinearRing[]::new));
|
return JTS_FACTORY.createPolygon(exteriorRing, rings.toArray(LinearRing[]::new));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns {@code false} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
|
||||||
|
* {@code ring}, ignoring repeated and collinear points.
|
||||||
|
*/
|
||||||
|
public static boolean isConvex(LinearRing ring) {
|
||||||
|
return !isConcave(ring);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns {@code true} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
|
* Returns {@code true} if the signed area of the triangle formed by 3 sequential points changes sign anywhere along
|
||||||
* {@code ring}, ignoring repeated and collinear points.
|
* {@code ring}, ignoring repeated and collinear points.
|
||||||
*/
|
*/
|
||||||
public static boolean isConvex(LinearRing ring) {
|
public static boolean isConcave(LinearRing ring) {
|
||||||
CoordinateSequence seq = ring.getCoordinateSequence();
|
CoordinateSequence seq = ring.getCoordinateSequence();
|
||||||
if (seq.size() <= 3) {
|
if (seq.size() <= 3) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -117,7 +117,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsConvexTriangle() {
|
void testIsConvexTriangle() {
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
0, 1,
|
0, 1,
|
||||||
|
@ -127,7 +127,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testIsConvexRectangle() {
|
void testIsConvexRectangle() {
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -138,21 +138,21 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBarelyConvexRectangle() {
|
void testBarelyConvexRectangle() {
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
0.5, 0.5,
|
0.5, 0.5,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
0.4, 0.4,
|
0.4, 0.4,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -163,7 +163,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testConcaveRectangleDoublePoints() {
|
void testConcaveRectangleDoublePoints() {
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
|
@ -171,7 +171,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
|
@ -179,7 +179,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -187,7 +187,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -195,7 +195,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -207,7 +207,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBarelyConcaveRectangle() {
|
void testBarelyConcaveRectangle() {
|
||||||
assertConvex(false, newLinearRing(
|
assertConcave(false, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -218,7 +218,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test5PointsConcave() {
|
void test5PointsConcave() {
|
||||||
assertConvex(false, newLinearRing(
|
assertConcave(false, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
0.5, 0.1,
|
0.5, 0.1,
|
||||||
1, 0,
|
1, 0,
|
||||||
|
@ -226,7 +226,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(false, newLinearRing(
|
assertConcave(false, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
0.9, 0.5,
|
0.9, 0.5,
|
||||||
|
@ -234,7 +234,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(false, newLinearRing(
|
assertConcave(false, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -242,7 +242,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(false, newLinearRing(
|
assertConcave(false, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -254,7 +254,7 @@ class GeoUtilsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void test5PointsColinear() {
|
void test5PointsColinear() {
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
0.5, 0,
|
0.5, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
|
@ -262,7 +262,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 0.5,
|
1, 0.5,
|
||||||
|
@ -270,7 +270,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -278,7 +278,7 @@ class GeoUtilsTest {
|
||||||
0, 1,
|
0, 1,
|
||||||
0, 0
|
0, 0
|
||||||
));
|
));
|
||||||
assertConvex(true, newLinearRing(
|
assertConcave(true, newLinearRing(
|
||||||
0, 0,
|
0, 0,
|
||||||
1, 0,
|
1, 0,
|
||||||
1, 1,
|
1, 1,
|
||||||
|
@ -288,14 +288,17 @@ class GeoUtilsTest {
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void assertConvex(boolean isConvex, LinearRing ring) {
|
private static void assertConcave(boolean isConcave, LinearRing ring) {
|
||||||
for (double rotation : new double[]{0, 90, 180, 270}) {
|
for (double rotation : new double[]{0, 90, 180, 270}) {
|
||||||
LinearRing rotated = (LinearRing) AffineTransformation.rotationInstance(Math.toRadians(rotation)).transform(ring);
|
LinearRing rotated = (LinearRing) AffineTransformation.rotationInstance(Math.toRadians(rotation)).transform(ring);
|
||||||
for (boolean flip : new boolean[]{false, true}) {
|
for (boolean flip : new boolean[]{false, true}) {
|
||||||
LinearRing flipped = flip ? (LinearRing) AffineTransformation.scaleInstance(-1, 1).transform(rotated) : rotated;
|
LinearRing flipped = flip ? (LinearRing) AffineTransformation.scaleInstance(-1, 1).transform(rotated) : rotated;
|
||||||
for (boolean reverse : new boolean[]{false, true}) {
|
for (boolean reverse : new boolean[]{false, true}) {
|
||||||
LinearRing reversed = reverse ? flipped.reverse() : flipped;
|
LinearRing reversed = reverse ? flipped.reverse() : flipped;
|
||||||
assertEquals(isConvex, isConvex(reversed), "rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
|
assertEquals(isConcave, isConcave(reversed),
|
||||||
|
"rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
|
||||||
|
assertEquals(!isConcave, isConvex(reversed),
|
||||||
|
"rotation=" + rotation + " flip=" + flip + " reverse=" + reverse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Ładowanie…
Reference in New Issue