Search in sources :

Example 1 with RealPoint

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

the class DefaultVoxelization3D method calculate.

@Override
public RandomAccessibleInterval<BitType> calculate(Mesh input) {
    Img<BitType> outImg = ops.create().img(new FinalInterval(width, height, depth), new BitType());
    DefaultMesh dMesh = (DefaultMesh) input;
    Set<RealLocalizable> verts = dMesh.getVertices();
    RealPoint minPoint = new RealPoint(verts.iterator().next());
    RealPoint maxPoint = new RealPoint(verts.iterator().next());
    for (RealLocalizable v : verts) {
        if (v.getDoublePosition(0) < minPoint.getDoublePosition(0))
            minPoint.setPosition(v.getDoublePosition(0), 0);
        if (v.getDoublePosition(1) < minPoint.getDoublePosition(1))
            minPoint.setPosition(v.getDoublePosition(1), 1);
        if (v.getDoublePosition(2) < minPoint.getDoublePosition(2))
            minPoint.setPosition(v.getDoublePosition(2), 2);
        if (v.getDoublePosition(0) > maxPoint.getDoublePosition(0))
            maxPoint.setPosition(v.getDoublePosition(0), 0);
        if (v.getDoublePosition(1) > maxPoint.getDoublePosition(1))
            maxPoint.setPosition(v.getDoublePosition(1), 1);
        if (v.getDoublePosition(2) > maxPoint.getDoublePosition(2))
            maxPoint.setPosition(v.getDoublePosition(2), 2);
    }
    RealPoint dimPoint = new RealPoint((maxPoint.getDoublePosition(0) - minPoint.getDoublePosition(0)), (maxPoint.getDoublePosition(1) - minPoint.getDoublePosition(1)), (maxPoint.getDoublePosition(2) - minPoint.getDoublePosition(2)));
    double[] stepSizes = new double[3];
    stepSizes[0] = dimPoint.getDoublePosition(0) / width;
    stepSizes[1] = dimPoint.getDoublePosition(1) / height;
    stepSizes[2] = dimPoint.getDoublePosition(2) / depth;
    double[] voxelHalfsize = new double[3];
    for (int k = 0; k < stepSizes.length; k++) voxelHalfsize[k] = stepSizes[k] / 2.0;
    for (Facet f : dMesh.getFacets()) {
        TriangularFacet tri = (TriangularFacet) f;
        Vector3D v1 = tri.getP0();
        Vector3D v2 = tri.getP1();
        Vector3D v3 = tri.getP2();
        double[] minSubBoundary = new double[] { Math.min(Math.min(v1.getX(), v2.getX()), v3.getX()) - minPoint.getDoublePosition(0), Math.min(Math.min(v1.getY(), v2.getY()), v3.getY()) - minPoint.getDoublePosition(1), Math.min(Math.min(v1.getZ(), v2.getZ()), v3.getZ()) - minPoint.getDoublePosition(2) };
        double[] maxSubBoundary = new double[] { Math.max(Math.max(v1.getX(), v2.getX()), v3.getX()) - minPoint.getDoublePosition(0), Math.max(Math.max(v1.getY(), v2.getY()), v3.getY()) - minPoint.getDoublePosition(1), Math.max(Math.max(v1.getZ(), v2.getZ()), v3.getZ()) - minPoint.getDoublePosition(2) };
        // Should use the
        RandomAccess<BitType> ra = outImg.randomAccess();
        // interval
        // implementation
        // for speed
        long[] indices = new long[3];
        for (indices[0] = (long) Math.floor(minSubBoundary[0] / stepSizes[0]); indices[0] < Math.floor(maxSubBoundary[0] / stepSizes[0]); indices[0]++) {
            for (indices[1] = (long) Math.floor(minSubBoundary[1] / stepSizes[1]); indices[1] < Math.floor(maxSubBoundary[1] / stepSizes[1]); indices[1]++) {
                for (indices[2] = (long) Math.floor(minSubBoundary[2] / stepSizes[2]); indices[2] < Math.floor(maxSubBoundary[2] / stepSizes[2]); indices[2]++) {
                    ra.setPosition(indices);
                    if (// Don't check if voxel is already
                    !ra.get().get()) // filled
                    {
                        double[] voxelCenter = new double[3];
                        for (int k = 0; k < 3; k++) voxelCenter[k] = indices[k] * stepSizes[k] + voxelHalfsize[k];
                        if (triBoxOverlap(voxelCenter, voxelHalfsize, v1, v2, v3) == 1) {
                            ra.get().set(true);
                        }
                    }
                }
            }
        }
    }
    return outImg;
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) RealPoint(net.imglib2.RealPoint) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet) DefaultMesh(net.imagej.ops.geom.geom3d.mesh.DefaultMesh) BitType(net.imglib2.type.logic.BitType) Vector3D(org.apache.commons.math3.geometry.euclidean.threed.Vector3D) RealPoint(net.imglib2.RealPoint) FinalInterval(net.imglib2.FinalInterval) Facet(net.imagej.ops.geom.geom3d.mesh.Facet) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet)

Example 2 with RealPoint

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

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

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

the class DefaultDetectJunctions method averagePoints.

private RealPoint averagePoints(ArrayList<RealPoint> list) {
    double[] pos = { 0, 0 };
    for (RealPoint p : list) {
        pos[0] += p.getDoublePosition(0);
        pos[1] += p.getDoublePosition(1);
    }
    pos[0] /= list.size();
    pos[1] /= list.size();
    return new RealPoint(pos);
}
Also used : RealPoint(net.imglib2.RealPoint)

Example 5 with RealPoint

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

the class DefaultCenterOfGravity method calculate.

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

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