Search in sources :

Example 16 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class DefaultMinimumFeret method calculate.

@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
    final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
    double distance = Double.POSITIVE_INFINITY;
    RealLocalizable p0 = points.get(0);
    RealLocalizable p1 = points.get(0);
    double tmpDist = 0;
    RealLocalizable tmpP0 = p0;
    RealLocalizable tmpP1 = p1;
    for (int i = 0; i < points.size() - 2; i++) {
        final RealLocalizable lineStart = points.get(i);
        final RealLocalizable lineEnd = points.get(i + 1);
        tmpDist = 0;
        final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
        for (int j = 0; j < points.size(); j++) {
            if (j != i && j != i + 1) {
                final RealLocalizable ttmpP0 = points.get(j);
                final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                if (tmp > tmpDist) {
                    tmpDist = tmp;
                    final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                    tmpP0 = new RealPoint(vp.getX(), vp.getY());
                    tmpP1 = ttmpP0;
                }
            }
        }
        if (tmpDist < distance) {
            distance = tmpDist;
            p0 = tmpP0;
            p1 = tmpP1;
        }
    }
    final RealLocalizable lineStart = points.get(points.size() - 1);
    final RealLocalizable lineEnd = points.get(0);
    final Line l = new Line(new Vector2D(lineStart.getDoublePosition(0), lineStart.getDoublePosition(1)), new Vector2D(lineEnd.getDoublePosition(0), lineEnd.getDoublePosition(1)), 10e-12);
    tmpDist = 0;
    for (int j = 0; j < points.size(); j++) {
        if (j != points.size() - 1 && j != 0 + 1) {
            final RealLocalizable ttmpP0 = points.get(j);
            final double tmp = l.distance(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
            if (tmp > tmpDist) {
                tmpDist = tmp;
                final Vector2D vp = (Vector2D) l.project(new Vector2D(ttmpP0.getDoublePosition(0), ttmpP0.getDoublePosition(1)));
                tmpP0 = new RealPoint(vp.getX(), vp.getY());
                tmpP1 = ttmpP0;
            }
        }
    }
    if (tmpDist < distance) {
        distance = tmpDist;
        p0 = tmpP0;
        p1 = tmpP1;
    }
    return new ValuePair<>(p0, p1);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) Line(org.apache.commons.math3.geometry.euclidean.twod.Line) Vector2D(org.apache.commons.math3.geometry.euclidean.twod.Vector2D) RealPoint(net.imglib2.RealPoint) ValuePair(net.imglib2.util.ValuePair) RealPoint(net.imglib2.RealPoint)

Example 17 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class CentroidII method calculate.

@Override
public RealLocalizable calculate(final IterableInterval<?> input) {
    int numDimensions = input.numDimensions();
    double[] output = new double[numDimensions];
    Cursor<?> c = input.localizingCursor();
    double[] pos = new double[numDimensions];
    while (c.hasNext()) {
        c.fwd();
        c.localize(pos);
        for (int i = 0; i < output.length; i++) {
            output[i] += pos[i];
        }
    }
    for (int i = 0; i < output.length; i++) {
        output[i] = output[i] / input.size();
    }
    return new RealPoint(output);
}
Also used : RealPoint(net.imglib2.RealPoint) RealPoint(net.imglib2.RealPoint)

Example 18 with RealPoint

use of net.imglib2.RealPoint in project imagej-ops by imagej.

the class CentroidMesh method calculate.

@Override
public RealLocalizable calculate(final Mesh input) {
    double c_x = 0;
    double c_y = 0;
    double c_z = 0;
    for (int i = 0; i < input.getFacets().size(); i++) {
        TriangularFacet f = (TriangularFacet) input.getFacets().get(i);
        Vector3D normal = f.getNormal();
        Vector3D a = f.getP0();
        Vector3D b = f.getP1();
        Vector3D c = f.getP2();
        c_x += (1 / 24d) * normal.getX() * (Math.pow((a.getX() + b.getX()), 2) + Math.pow((b.getX() + c.getX()), 2) + Math.pow((c.getX() + a.getX()), 2));
        c_y += (1 / 24d) * normal.getY() * (Math.pow((a.getY() + b.getY()), 2) + Math.pow((b.getY() + c.getY()), 2) + Math.pow((c.getY() + a.getY()), 2));
        c_z += (1 / 24d) * normal.getZ() * (Math.pow((a.getZ() + b.getZ()), 2) + Math.pow((b.getZ() + c.getZ()), 2) + Math.pow((c.getZ() + a.getZ()), 2));
    }
    double d = 1 / (2 * sizeFunc.calculate(input).get());
    c_x *= d;
    c_y *= d;
    c_z *= d;
    return new RealPoint(-c_x, -c_y, -c_z);
}
Also used : TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet) Vector3D(org.apache.commons.math3.geometry.euclidean.threed.Vector3D) RealPoint(net.imglib2.RealPoint) RealPoint(net.imglib2.RealPoint)

Example 19 with RealPoint

use of net.imglib2.RealPoint 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));
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) RealPoint(net.imglib2.RealPoint) RealPoint(net.imglib2.RealPoint)

Example 20 with RealPoint

use of net.imglib2.RealPoint 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);
    }
}
Also used : RealPoint(net.imglib2.RealPoint) Polygon2D(net.imglib2.roi.geom.real.Polygon2D) RealPoint(net.imglib2.RealPoint) DefaultBoundingBox(net.imagej.ops.geom.geom2d.DefaultBoundingBox) Test(org.junit.Test) AbstractFeatureTest(net.imagej.ops.features.AbstractFeatureTest)

Aggregations

RealPoint (net.imglib2.RealPoint)21 ArrayList (java.util.ArrayList)9 RealLocalizable (net.imglib2.RealLocalizable)6 DefaultWritablePolygon2D (net.imglib2.roi.geom.real.DefaultWritablePolygon2D)5 Test (org.junit.Test)5 AbstractFeatureTest (net.imagej.ops.features.AbstractFeatureTest)4 Polygon2D (net.imglib2.roi.geom.real.Polygon2D)3 TriangularFacet (net.imagej.ops.geom.geom3d.mesh.TriangularFacet)2 DefaultWritablePolyline (net.imglib2.roi.geom.real.DefaultWritablePolyline)2 Vector3D (org.apache.commons.math3.geometry.euclidean.threed.Vector3D)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 AbstractOpTest (net.imagej.ops.AbstractOpTest)1 DefaultBoundingBox (net.imagej.ops.geom.geom2d.DefaultBoundingBox)1 DefaultConvexHull2D (net.imagej.ops.geom.geom2d.DefaultConvexHull2D)1 DefaultSmallestEnclosingRectangle (net.imagej.ops.geom.geom2d.DefaultSmallestEnclosingRectangle)1 DefaultMesh (net.imagej.ops.geom.geom3d.mesh.DefaultMesh)1 Facet (net.imagej.ops.geom.geom3d.mesh.Facet)1 FinalInterval (net.imglib2.FinalInterval)1 Interval (net.imglib2.Interval)1