Search in sources :

Example 6 with NoninvertibleModelException

use of mpicbg.models.NoninvertibleModelException in project TrakEM2 by trakem2.

the class TransformMesh method applyInverseInPlace.

/**
 * Catch non-invertible locations outside of the meshes boundaries and
 * transfer them with the affine defined by the `closest' affine (the affine
 * whose summed up control points distances to location are smallest).
 */
@Override
public void applyInverseInPlace(final double[] location) throws NoninvertibleModelException {
    assert location.length == 2 : "2d transform meshs can be applied to 2d points only.";
    final Set<AffineModel2D> s = av.keySet();
    for (final AffineModel2D ai : s) {
        final ArrayList<PointMatch> pm = av.get(ai);
        if (isInConvexTargetPolygon(pm, location)) {
            ai.applyInverseInPlace(location);
            return;
        }
    }
    /* not in the mesh, find the closest affine */
    double dMin = Double.MAX_VALUE;
    AffineModel2D closestAffine = new AffineModel2D();
    final double x = location[0];
    final double y = location[1];
    for (final AffineModel2D ai : s) {
        final ArrayList<PointMatch> pm = av.get(ai);
        double d = 0;
        for (final PointMatch p : pm) {
            final double[] w = p.getP2().getW();
            final double dx = w[0] - x;
            final double dy = w[1] - y;
            d += Math.sqrt(dx * dx + dy * dy);
        }
        if (d < dMin) {
            dMin = d;
            closestAffine = ai;
        }
    }
    closestAffine.applyInverseInPlace(location);
    throw new NoninvertibleModelException("Mesh external location ( " + x + ", " + y + " ) transferred to ( " + location[0] + ", " + location[1] + " ) by closest affine.");
}
Also used : PointMatch(mpicbg.models.PointMatch) NoninvertibleModelException(mpicbg.models.NoninvertibleModelException) AffineModel2D(mpicbg.models.AffineModel2D)

Aggregations

NoninvertibleModelException (mpicbg.models.NoninvertibleModelException)6 PointMatch (mpicbg.models.PointMatch)2 ImageProcessor (ij.process.ImageProcessor)1 AffineTransform (java.awt.geom.AffineTransform)1 Area (java.awt.geom.Area)1 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)1 PathIterator (java.awt.geom.PathIterator)1 Point2D (java.awt.geom.Point2D)1 AffineModel2D (mpicbg.models.AffineModel2D)1 CoordinateTransformMesh (mpicbg.models.CoordinateTransformMesh)1 IllDefinedDataPointsException (mpicbg.models.IllDefinedDataPointsException)1 NotEnoughDataPointsException (mpicbg.models.NotEnoughDataPointsException)1 Point (mpicbg.models.Point)1 SimilarityModel2D (mpicbg.models.SimilarityModel2D)1 CoordinateTransform (mpicbg.trakem2.transform.CoordinateTransform)1 CoordinateTransformList (mpicbg.trakem2.transform.CoordinateTransformList)1 TransformMesh (mpicbg.trakem2.transform.TransformMesh)1 TransformMeshMappingWithMasks (mpicbg.trakem2.transform.TransformMeshMappingWithMasks)1