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.");
}
Aggregations