Search in sources :

Example 1 with RealLocalizable

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

the class DefaultConvexHull3D method calculate.

@Override
public Mesh calculate(final Mesh input) {
    DefaultMesh output = new DefaultMesh();
    Set<Vertex> vertices = new LinkedHashSet<>();
    for (final RealLocalizable v : input.getVertices()) {
        vertices.add(new Vertex(v.getDoublePosition(0), v.getDoublePosition(1), v.getDoublePosition(2)));
    }
    List<TriangularFacet> facets = new ArrayList<>();
    List<TriangularFacet> facetsWithPointInFront = new ArrayList<>();
    final double epsilon = computeHull(vertices, facets, facetsWithPointInFront);
    for (TriangularFacet f : facets) {
        output.addFace(f);
    }
    output.setEpsilon(epsilon);
    return output;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) RealLocalizable(net.imglib2.RealLocalizable) Vertex(net.imagej.ops.geom.geom3d.mesh.Vertex) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet) DefaultMesh(net.imagej.ops.geom.geom3d.mesh.DefaultMesh) ArrayList(java.util.ArrayList)

Example 2 with RealLocalizable

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

the class DefaultInertiaTensor3DMesh method calculate.

@Override
public RealMatrix calculate(final Mesh input) {
    final RealLocalizable o = centroid.calculate(input);
    BlockRealMatrix tensor = new BlockRealMatrix(3, 3);
    final Iterator<Facet> c = input.getFacets().iterator();
    while (c.hasNext()) {
        final TriangularFacet tf = (TriangularFacet) c.next();
        tensor = tensor.add(tetrahedronInertiaTensor(tf.getVertex(0), tf.getVertex(1), tf.getVertex(2), o));
    }
    return tensor;
}
Also used : RealLocalizable(net.imglib2.RealLocalizable) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet) BlockRealMatrix(org.apache.commons.math3.linear.BlockRealMatrix) Facet(net.imagej.ops.geom.geom3d.mesh.Facet) TriangularFacet(net.imagej.ops.geom.geom3d.mesh.TriangularFacet)

Example 3 with RealLocalizable

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

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

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

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