use of mpicbg.models.MovingLeastSquaresTransform in project TrakEM2 by trakem2.
the class Compare method transferVectorStrings.
/**
* Transform all points of all VectorString3D in vs using a Moving Least Squares Transform defined by the pairing of points in source to those in target.
* In short, bring source into target.
*/
public static List<VectorString3D> transferVectorStrings(final List<VectorString3D> vs, final List<Tuple3d> source, final List<Tuple3d> target, final Class<AffineModel3D> model_class) throws Exception {
if (source.size() != target.size()) {
Utils.log2("Could not generate a MovingLeastSquaresTransform: different number of source and target points.");
return null;
}
if (source.size() < 1 || target.size() < 1) {
Utils.log2("Cannot transform with less than one point correspondence!");
return null;
}
// 1 - Create the MovingLeastSquaresTransform from the point matches
final ArrayList<PointMatch> pm = new ArrayList<PointMatch>();
for (final Iterator<Tuple3d> si = source.iterator(), ti = target.iterator(); si.hasNext(); ) {
final Tuple3d sp = si.next();
final Tuple3d tp = ti.next();
pm.add(new PointMatch(new mpicbg.models.Point(new double[] { sp.x, sp.y, sp.z }), new mpicbg.models.Point(new double[] { tp.x, tp.y, tp.z }), 1));
}
final MovingLeastSquaresTransform mls = new MovingLeastSquaresTransform();
mls.setModel(model_class);
mls.setMatches(pm);
final double[] point = new double[3];
// 1.1 - Test: transfer source points
/*
for (final Iterator<Tuple3d> si = source.iterator(), ti = target.iterator(); si.hasNext(); ) {
Tuple3d sp = si.next();
point[0] = (double) sp.x;
point[1] = (double) sp.y;
point[2] = (double) sp.z;
mls.applyInPlace(point);
Tuple3d tp = ti.next();
Utils.log2("== target: " + (double)tp.x + ", " + (double)tp.y + ", " + (double)tp.z +
"\n o source: " + (double)sp.x + ", " + (double)sp.y + ", " + (double)sp.z +
"\n source: " + point[0] + ", " + point[1] + ", " + point[2]);
}
*/
// 2 - Transfer each VectorString3D in vs with mls
final List<VectorString3D> vt = new ArrayList<VectorString3D>();
for (final VectorString3D vi : vs) {
// The points of the VectorString3D:
final double[] x = vi.getPoints(0);
final double[] y = vi.getPoints(1);
final double[] z = vi.getPoints(2);
// Empty arrays to fill with the points to transfer:
final double[] tx = new double[x.length];
final double[] ty = new double[x.length];
final double[] tz = new double[x.length];
// Transfer point by point:
for (int i = 0; i < x.length; i++) {
point[0] = x[i];
point[1] = y[i];
point[2] = z[i];
mls.applyInPlace(point);
tx[i] = point[0];
ty[i] = point[1];
tz[i] = point[2];
}
try {
vt.add(new VectorString3D(tx, ty, tz, vi.isClosed()));
} catch (final Exception e) {
}
}
return vt;
}
Aggregations