Fixed several bugs in and extended MovingLeastSquaresTransform:
[trakem2.git] / mpicbg / trakem2 / transform / MovingLeastSquaresTransform.java
blobbbf48f94be2ca94ceca10e1ec2443971a87baad7
1 /**
2 * License: GPL
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License 2
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 package mpicbg.trakem2.transform;
19 import mpicbg.models.AffineModel2D;
20 import mpicbg.models.AffineModel3D;
21 import mpicbg.models.Point;
22 import mpicbg.models.PointMatch;
23 import mpicbg.models.RigidModel2D;
24 import mpicbg.models.SimilarityModel2D;
25 import mpicbg.models.TranslationModel2D;
27 /**
29 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
30 * @version 0.2b
32 public class MovingLeastSquaresTransform extends mpicbg.models.MovingLeastSquaresTransform implements CoordinateTransform
35 final public void init( final String data ) throws NumberFormatException
37 matches.clear();
39 final String[] fields = data.split( "\\s+" );
40 if ( fields.length > 3 )
42 final int d = Integer.parseInt( fields[ 1 ] );
44 if ( ( fields.length - 3 ) % ( 2 * d + 1 ) == 0 )
46 if ( d == 2 )
48 if ( fields[ 0 ].equals( "translation" ) ) model = new TranslationModel2D();
49 else if ( fields[ 0 ].equals( "rigid" ) ) model = new RigidModel2D();
50 else if ( fields[ 0 ].equals( "similarity" ) ) model = new SimilarityModel2D();
51 else if ( fields[ 0 ].equals( "affine" ) ) model = new AffineModel2D();
52 else throw new NumberFormatException( "Inappropriate parameters for " + this.getClass().getCanonicalName() );
54 else if ( d == 3 )
56 if ( fields[ 0 ].equals( "affine" ) ) model = new AffineModel3D();
57 else throw new NumberFormatException( "Inappropriate parameters for " + this.getClass().getCanonicalName() );
59 else throw new NumberFormatException( "Inappropriate parameters for " + this.getClass().getCanonicalName() );
61 alpha = Float.parseFloat( fields[ 2 ] );
63 int i = 2;
64 while ( i < fields.length - 1 )
66 final float[] p1 = new float[ d ];
67 for ( int k = 0; k < d; ++k )
68 p1[ k ] = Float.parseFloat( fields[ ++i ] );
69 final float[] p2 = new float[ d ];
70 for ( int k = 0; k < d; ++k )
71 p2[ k ] = Float.parseFloat( fields[ ++i ] );
72 final float weight = Float.parseFloat( fields[ ++i ] );
73 final PointMatch m = new PointMatch( new Point( p1 ), new Point( p2 ), weight );
74 matches.add( m );
77 else throw new NumberFormatException( "Inappropriate parameters for " + this.getClass().getCanonicalName() );
79 else throw new NumberFormatException( "Inappropriate parameters for " + this.getClass().getCanonicalName() );
83 public String toDataString()
85 String data = "";
87 if ( TranslationModel2D.class.isInstance( model ) ) data += "translation 2";
88 else if ( RigidModel2D.class.isInstance( model ) ) data += "rigid 2";
89 else if ( SimilarityModel2D.class.isInstance( model ) ) data += "similarity 2";
90 else if ( AffineModel2D.class.isInstance( model ) ) data += "affine 2";
91 else if ( AffineModel3D.class.isInstance( model ) ) data += "affine 3";
92 else data += "unknown";
94 data += " " + alpha;
96 for ( PointMatch m : matches )
98 final float[] p1 = m.getP1().getL();
99 final float[] p2 = m.getP2().getW();
100 for ( int k = 0; k < p1.length; ++k )
101 data += " " + p1[ k ];
102 for ( int k = 0; k < p2.length; ++k )
103 data += " " + p2[ k ];
104 data += " " + m.getWeight();
106 return data;
109 final public String toXML( final String indent )
111 return indent + "<ict_transform class=\"" + this.getClass().getCanonicalName() + "\" data=\"" + toDataString() + "\"/>";
114 @Override
116 * TODO Make this more efficient
118 final public MovingLeastSquaresTransform clone()
120 final MovingLeastSquaresTransform t = new MovingLeastSquaresTransform();
121 t.init( toDataString() );
122 return t;