use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class CentroidPolygon method calculate.
@Override
public RealLocalizable calculate(final Polygon2D input) {
double area = sizeFunc.calculate(input).get();
double cx = 0;
double cy = 0;
for (int i = 0; i < input.numVertices(); i++) {
RealLocalizable p0 = input.vertex(i);
RealLocalizable p1 = input.vertex((i + 1) % input.numVertices());
double p0_x = p0.getDoublePosition(0);
double p0_y = p0.getDoublePosition(1);
double p1_x = p1.getDoublePosition(0);
double p1_y = p1.getDoublePosition(1);
cx += (p0_x + p1_x) * (p0_x * p1_y - p1_x * p0_y);
cy += (p0_y + p1_y) * (p0_x * p1_y - p1_x * p0_y);
}
return new RealPoint(cx / (area * 6), cy / (area * 6));
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class PolygonFeatureTests method boundingBox.
@Test
public void boundingBox() {
// ground truth verified with matlab
final List<? extends RealLocalizable> received = GeomUtils.vertices(((Polygon2D) ops.run(DefaultBoundingBox.class, contour)));
final RealPoint[] expected = new RealPoint[] { new RealPoint(1, 6), new RealPoint(1, 109), new RealPoint(78, 109), new RealPoint(78, 6) };
assertEquals("Number of polygon points differs.", expected.length, received.size());
for (int i = 0; i < expected.length; i++) {
assertEquals("Polygon point " + i + " differs in x-coordinate.", expected[i].getDoublePosition(0), received.get(i).getDoublePosition(0), EPSILON);
assertEquals("Polygon point " + i + " differs in y-coordinate.", expected[i].getDoublePosition(1), received.get(i).getDoublePosition(1), EPSILON);
}
}
use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.
the class AbstractFeatureTest method getPolygon.
protected static Polygon2D getPolygon() {
final List<RealPoint> vertices = new ArrayList<>();
try {
Files.lines(Paths.get(AbstractFeatureTest.class.getResource("2d_geometric_features_polygon.txt").toURI())).forEach(l -> {
String[] coord = l.split(" ");
RealPoint v = new RealPoint(new double[] { Double.parseDouble(coord[0]), Double.parseDouble(coord[1]) });
vertices.add(v);
});
} catch (IOException | URISyntaxException exc) {
exc.printStackTrace();
}
return new DefaultWritablePolygon2D(vertices);
}
Aggregations