Fix error in Utils.safeCopy
[trakem2.git] / lenscorrection / PolynomialModel2D.java
blob600ffd505e0be8786639e61ece6a6b7d276b63d8
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 lenscorrection;
19 import java.util.Collection;
21 import mpicbg.models.AbstractAffineModel2D;
22 import mpicbg.models.AbstractModel;
23 import mpicbg.models.IllDefinedDataPointsException;
24 import mpicbg.models.Model;
25 import mpicbg.models.NotEnoughDataPointsException;
26 import mpicbg.models.PointMatch;
28 /**
29 * A wrpper for {@link NonLinearTransform} and the {@link AbstractAffineModel2D}
30 * to which it is regularized.
32 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
33 * @version 0.1a
35 public class PolynomialModel2D extends AbstractModel< PolynomialModel2D >
37 private NonLinearTransform nlt = new NonLinearTransform();
38 private AbstractAffineModel2D< ? > affine = null;
39 private float lambda = 0f;
42 public AbstractAffineModel2D< ? > getAffine(){ return affine; }
43 public void setAffine( final AbstractAffineModel2D< ? > affine ){ this.affine = affine; }
44 public void setAffine( final Class< ? extends AbstractAffineModel2D< ? > > affineClass ) throws Exception
46 affine = affineClass.newInstance();
49 public int getOrder(){ return nlt.getDimension(); }
50 public void setOrder( final int order ){ nlt.setDimension( order ); }
52 public float getLambda(){ return lambda; }
53 public void setLambda( final float lambda ){ this.lambda = lambda; }
56 @Override
57 public PolynomialModel2D copy()
59 final PolynomialModel2D clone = new PolynomialModel2D();
60 clone.nlt = nlt.copy();
61 clone.affine = affine.copy();
62 clone.lambda = lambda;
63 return clone;
66 @Override
67 public < P extends PointMatch >void fit( Collection< P > pointMatches ) throws NotEnoughDataPointsException, IllDefinedDataPointsException
69 if ( pointMatches.size() < getMinNumMatches() )
70 throw new NotEnoughDataPointsException( pointMatches.size() + " data points are not enough to estimate a 2d polynomial of order " + nlt.getDimension() + ", at least " + getMinNumMatches() + " data points required." );
72 affine.fit( pointMatches );
74 final double h1[][] = new double[ pointMatches.size() ][ 2 ];
75 final double h2[][] = new double[ pointMatches.size() ][ 2 ];
77 int i = 0;
78 for ( final P match : pointMatches )
80 final float[] tmp1 = match.getP1().getL().clone();
81 affine.applyInPlace( tmp1 );
83 final float[] tmp2 = match.getP2().getW();
85 h1[ i ] = new double[]{ tmp1[ 0 ], tmp1[ 1 ] };
86 h2[ i ] = new double[]{ tmp2[ 0 ], tmp2[ 1 ] };
88 ++i;
91 nlt.fit( h1, h2, lambda );
94 @Override
95 public int getMinNumMatches()
97 return Math.max( affine.getMinNumMatches(), nlt.getMinNumMatches() );
100 @Override
101 public void set( PolynomialModel2D m )
103 nlt.set( m.nlt );
104 affine = m.affine.copy();
105 lambda = m.lambda;
108 @Override
109 public String toString()
111 return "affine: " + affine.toString() + "; polynomial: " + nlt.toString();
114 @Override
115 public float[] apply( float[] location )
117 final float[] l = location.clone();
118 applyInPlace( l );
119 return l;
122 @Override
123 public void applyInPlace( float[] location )
125 affine.applyInPlace( location );
126 nlt.applyInPlace( location );