Search in sources :

Example 1 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class DefaultMinorMajorAxis method calculate.

@Override
public Pair<DoubleType, DoubleType> calculate(final Polygon2D input) {
    List<RealLocalizable> points = new ArrayList<>(GeomUtils.vertices(input));
    // Sort RealLocalizables of P by x-coordinate (in case of a tie,
    // sort by
    // y-coordinate). Sorting is counter clockwise.
    Collections.sort(points, 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 = o2x.compareTo(o1x);
            if (result == 0) {
                return new Double(o2.getDoublePosition(1)).compareTo(new Double(o1.getDoublePosition(1)));
            }
            return result;
        }
    });
    points.add(points.get(0));
    // calculate minor and major axis
    double[] minorMajorAxis = getMinorMajorAxis(input, points);
    return new ValuePair<>(new DoubleType(minorMajorAxis[0]), new DoubleType(minorMajorAxis[1]));
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) DoubleType(net.imglib2.type.numeric.real.DoubleType) ValuePair(net.imglib2.util.ValuePair) ArrayList(java.util.ArrayList)

Example 2 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class DefaultSmallestEnclosingRectangle method rotate.

/**
 * Rotates the given Polygon2D consisting of a list of RealPoints by the
 * given angle about the given center.
 *
 * @param inPoly A Polygon2D consisting of a list of RealPoint RealPoints
 * @param angle the rotation angle
 * @param center the rotation center
 * @return a rotated polygon
 */
private Polygon2D rotate(final Polygon2D inPoly, final double angle, final RealLocalizable center) {
    List<RealLocalizable> out = new ArrayList<>();
    for (RealLocalizable RealPoint : GeomUtils.vertices(inPoly)) {
        // double angleInRadians = Math.toRadians(angleInDegrees);
        double cosTheta = Math.cos(angle);
        double sinTheta = Math.sin(angle);
        double x = cosTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) - sinTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(0);
        double y = sinTheta * (RealPoint.getDoublePosition(0) - center.getDoublePosition(0)) + cosTheta * (RealPoint.getDoublePosition(1) - center.getDoublePosition(1)) + center.getDoublePosition(1);
        out.add(new RealPoint(x, y));
    }
    return new DefaultWritablePolygon2D(out);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) RealPoint(net.imglib2.RealPoint) ArrayList(java.util.ArrayList) DefaultWritablePolygon2D(net.imglib2.roi.geom.real.DefaultWritablePolygon2D)

Example 3 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class DefaultBoundingBox method calculate.

@Override
public Polygon2D calculate(final Polygon2D input) {
    double min_x = Double.POSITIVE_INFINITY;
    double max_x = Double.NEGATIVE_INFINITY;
    double min_y = Double.POSITIVE_INFINITY;
    double max_y = Double.NEGATIVE_INFINITY;
    for (final RealLocalizable rl : GeomUtils.vertices(input)) {
        if (rl.getDoublePosition(0) < min_x) {
            min_x = rl.getDoublePosition(0);
        }
        if (rl.getDoublePosition(0) > max_x) {
            max_x = rl.getDoublePosition(0);
        }
        if (rl.getDoublePosition(1) < min_y) {
            min_y = rl.getDoublePosition(1);
        }
        if (rl.getDoublePosition(1) > max_y) {
            max_y = rl.getDoublePosition(1);
        }
    }
    final List<RealLocalizable> bounds = new ArrayList<>();
    bounds.add(new RealPoint(min_x, min_y));
    bounds.add(new RealPoint(min_x, max_y));
    bounds.add(new RealPoint(max_x, max_y));
    bounds.add(new RealPoint(max_x, min_y));
    return new DefaultWritablePolygon2D(bounds);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) RealPoint(net.imglib2.RealPoint) ArrayList(java.util.ArrayList) DefaultWritablePolygon2D(net.imglib2.roi.geom.real.DefaultWritablePolygon2D)

Example 4 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class DefaultFeretsDiameterForAngle method compute.

@Override
public void compute(Polygon2D input, DoubleType output) {
    final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
    final double angleRad = -angle * Math.PI / 180.0;
    double minX = Double.POSITIVE_INFINITY;
    double maxX = Double.NEGATIVE_INFINITY;
    for (RealLocalizable p : points) {
        final double tmpX = p.getDoublePosition(0) * Math.cos(angleRad) - p.getDoublePosition(1) * Math.sin(angleRad);
        minX = tmpX < minX ? tmpX : minX;
        maxX = tmpX > maxX ? tmpX : maxX;
    }
    output.set(Math.abs(maxX - minX));
}
Also used : RealLocalizable(net.imglib2.RealLocalizable)

Example 5 with Polygon2D

use of net.imglib2.roi.geom.real.Polygon2D in project imagej-ops by imagej.

the class DefaultMaximumFeret method calculate.

@Override
public Pair<RealLocalizable, RealLocalizable> calculate(Polygon2D input) {
    final List<? extends RealLocalizable> points = GeomUtils.vertices(function.calculate(input));
    double distance = Double.NEGATIVE_INFINITY;
    RealLocalizable p0 = points.get(0);
    RealLocalizable p1 = points.get(0);
    for (int i = 0; i < points.size(); i++) {
        for (int j = i + 2; j < points.size(); j++) {
            final RealLocalizable tmpP0 = points.get(i);
            final RealLocalizable tmpP1 = points.get(j);
            final double tmp = Math.sqrt(Math.pow(tmpP0.getDoublePosition(0) - tmpP1.getDoublePosition(0), 2) + Math.pow(tmpP0.getDoublePosition(1) - tmpP1.getDoublePosition(1), 2));
            if (tmp > distance) {
                distance = tmp;
                p0 = tmpP0;
                p1 = tmpP1;
            }
        }
    }
    return new ValuePair<>(p0, p1);
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) ValuePair(net.imglib2.util.ValuePair)

Aggregations

RealPoint (net.imglib2.RealPoint)12 RealLocalizable (net.imglib2.RealLocalizable)11 ArrayList (java.util.ArrayList)6 DefaultWritablePolygon2D (net.imglib2.roi.geom.real.DefaultWritablePolygon2D)6 Polygon2D (net.imglib2.roi.geom.real.Polygon2D)6 AbstractFeatureTest (net.imagej.ops.features.AbstractFeatureTest)5 Test (org.junit.Test)5 ValuePair (net.imglib2.util.ValuePair)3 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)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 DefaultSmallestEnclosingRectangle (net.imagej.ops.geom.geom2d.DefaultSmallestEnclosingRectangle)1 LabelRegionToPolygonConverter (net.imagej.ops.geom.geom2d.LabelRegionToPolygonConverter)1 DoubleType (net.imglib2.type.numeric.real.DoubleType)1 Line (org.apache.commons.math3.geometry.euclidean.twod.Line)1 Vector2D (org.apache.commons.math3.geometry.euclidean.twod.Vector2D)1