Skip to content

Commit

Permalink
fix elongation()
Browse files Browse the repository at this point in the history
  • Loading branch information
micycle1 committed Feb 9, 2025
1 parent c4001ab commit e401bf8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
* Fixed invalid results given by `PGS_Morphology.rounding()`.
* `PGS_ShapePredicates.elongation()` now correctly measures shape elongation (previously inverted, now returns 1 for highly elongated shapes).

### Removed

Expand Down
17 changes: 9 additions & 8 deletions src/main/java/micycle/pgs/PGS_ShapePredicates.java
Original file line number Diff line number Diff line change
Expand Up @@ -415,11 +415,14 @@ public static double sphericity(final PShape shape) {
}

/**
* Measures the elongation of a shape; the ratio of a shape's bounding box
* length to its width.
* Measures the elongation of a shape as the ratio of the difference between the
* bounding box's length and width to the maximum dimension. A value of 1
* indicates a highly elongated shape, while a value of 0 indicates a square or
* nearly square shape.
*
* @param shape
* @return a value in [0, 1]
* @return a value in the range [0, 1], where 1 represents high elongation and 0
* represents no elongation
*/
public static double elongation(final PShape shape) {
Geometry obb = MinimumDiameter.getMinimumRectangle(fromPShape(shape));
Expand All @@ -429,11 +432,9 @@ public static double elongation(final PShape shape) {
Coordinate c2 = rect.getCoordinates()[2];
double l = c0.distance(c1);
double w = c1.distance(c2);
if (l >= w) {
return w / l;
} else {
return l / w;
}
double max = Math.max(l, w);
double min = Math.min(l, w);
return 1 - (min / max);
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/test/java/micycle/pgs/PGS_ShapePredicatesTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PGS_ShapePredicatesTests {

private static final double EPSILON = 1E-4;

static PShape square, triangle;
static PShape square, triangle, rect;

@BeforeAll
static void initShapes() {
Expand All @@ -29,6 +29,14 @@ static void initShapes() {
square.vertex(10, 10);
square.vertex(0, 10);
square.endShape(PConstants.CLOSE); // close affects rendering only -- does not append another vertex

rect = new PShape(PShape.GEOMETRY); // 10x10 rect
rect.beginShape();
rect.vertex(0, 0);
rect.vertex(10, 0);
rect.vertex(10, 20);
rect.vertex(0, 20);
rect.endShape(PConstants.CLOSE); // close affects rendering only -- does not append another vertex

float[] centroid = new float[] { 0, 0 };
float side_length = 10;
Expand Down Expand Up @@ -87,5 +95,11 @@ void testIsClockwise() {
assertFalse(PGS_ShapePredicates.isClockwise(PGS_Conversion.fromPVector(ccw)));

}

@Test
void testElongation() {
assertEquals(0, PGS_ShapePredicates.elongation(square));
assertEquals(0.5, PGS_ShapePredicates.elongation(rect));
}

}

0 comments on commit e401bf8

Please sign in to comment.