Search in sources :

Example 21 with RealLocalizable

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

the class DefaultConvexHull2D method calculate.

@Override
public Polygon2D calculate(final Polygon2D input) {
    // create a copy of points because se will get resorted, etc.
    List<? extends RealLocalizable> RealPoints = new ArrayList<>(GeomUtils.vertices(input));
    // Sort RealPoints of P by x-coordinate (in case of a tie, sort by
    // y-coordinate).
    Collections.sort(RealPoints, new Comparator<RealLocalizable>() {

        @Override
        public int compare(final RealLocalizable o1, final RealLocalizable o2) {
            final Double o1x = new Double(o1.getDoublePosition(0));
            final Double o2x = new Double(o2.getDoublePosition(0));
            final int result = o1x.compareTo(o2x);
            if (result == 0) {
                return new Double(o1.getDoublePosition(1)).compareTo(new Double(o2.getDoublePosition(1)));
            }
            return result;
        }
    });
    // Initialize U and L as empty lists.
    // lists will hold vertices of upper and lower hulls
    // respectively.
    final List<RealLocalizable> U = new ArrayList<>();
    final List<RealLocalizable> L = new ArrayList<>();
    // build lower hull
    for (final RealLocalizable p : RealPoints) {
        // turn: remove last RealPoint from L
        while (L.size() >= 2 && ccw(L.get(L.size() - 2), L.get(L.size() - 1), p) <= 0) {
            L.remove(L.size() - 1);
        }
        L.add(p);
    }
    // build upper hull
    Collections.reverse(RealPoints);
    for (final RealLocalizable p : RealPoints) {
        // turn: remove last RealPoint from U
        while (U.size() >= 2 && ccw(U.get(U.size() - 2), U.get(U.size() - 1), p) <= 0) {
            U.remove(U.size() - 1);
        }
        U.add(p);
    }
    // Remove last RealPoint of each list (it's same as first
    // RealPoint
    // of or list).
    L.remove(L.size() - 1);
    U.remove(U.size() - 1);
    // concatenate L and U
    L.addAll(U);
    return new DefaultWritablePolygon2D(L);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) ArrayList(java.util.ArrayList) DefaultWritablePolygon2D(net.imglib2.roi.geom.real.DefaultWritablePolygon2D)

Example 22 with RealLocalizable

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

the class DefaultElongation method compute.

@Override
public void compute(final Polygon2D input, final DoubleType output) {
    final List<? extends RealLocalizable> minBB = GeomUtils.vertices(minimumBoundingBoxFunc.calculate(input));
    final RealLocalizable p1 = minBB.get(0);
    final RealLocalizable p2 = minBB.get(1);
    final RealLocalizable p3 = minBB.get(2);
    double width = Math.sqrt(Math.pow(p1.getDoublePosition(0) - p2.getDoublePosition(0), 2) + Math.pow(p1.getDoublePosition(1) - p2.getDoublePosition(1), 2));
    double length = Math.sqrt(Math.pow(p2.getDoublePosition(0) - p3.getDoublePosition(0), 2) + Math.pow(p2.getDoublePosition(1) - p3.getDoublePosition(1), 2));
    if (width > length) {
        final double tmp = width;
        width = length;
        length = tmp;
    }
    output.set(1d - (width / length));
}
Also used : RealLocalizable(net.imglib2.RealLocalizable)

Example 23 with RealLocalizable

use of net.imglib2.RealLocalizable 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 24 with RealLocalizable

use of net.imglib2.RealLocalizable 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 25 with RealLocalizable

use of net.imglib2.RealLocalizable 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)

Aggregations

RealLocalizable (net.imglib2.RealLocalizable)21 RealPoint (net.imglib2.RealPoint)15 Test (org.junit.Test)9 LinkedHashSet (java.util.LinkedHashSet)6 DefaultMesh (net.imagej.ops.geom.geom3d.mesh.DefaultMesh)6 Vertex (net.imagej.ops.geom.geom3d.mesh.Vertex)6 Polygon2D (net.imglib2.roi.geom.real.Polygon2D)6 ArrayList (java.util.ArrayList)5 AbstractFeatureTest (net.imagej.ops.features.AbstractFeatureTest)5 AbstractOpTest (net.imagej.ops.AbstractOpTest)4 DefaultConvexHull3D (net.imagej.ops.geom.geom3d.DefaultConvexHull3D)4 TriangularFacet (net.imagej.ops.geom.geom3d.mesh.TriangularFacet)4 DefaultWritablePolygon2D (net.imglib2.roi.geom.real.DefaultWritablePolygon2D)4 ValuePair (net.imglib2.util.ValuePair)3 Facet (net.imagej.ops.geom.geom3d.mesh.Facet)2 Vector3D (org.apache.commons.math3.geometry.euclidean.threed.Vector3D)2 Random (java.util.Random)1 DefaultBoundingBox (net.imagej.ops.geom.geom2d.DefaultBoundingBox)1 DefaultContour (net.imagej.ops.geom.geom2d.DefaultContour)1 DefaultConvexHull2D (net.imagej.ops.geom.geom2d.DefaultConvexHull2D)1