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
.NotEnoughDataPointsException
;
25 import mpicbg
.models
.PointMatch
;
28 * A wrpper for {@link NonLinearTransform} and the {@link AbstractAffineModel2D}
29 * to which it is regularized.
31 * @author Stephan Saalfeld <saalfeld@mpi-cbg.de>
34 public class PolynomialModel2D
extends AbstractModel
< PolynomialModel2D
>
36 private NonLinearTransform nlt
= new NonLinearTransform();
37 private AbstractAffineModel2D
< ?
> affine
= null;
38 private float lambda
= 0f
;
41 public AbstractAffineModel2D
< ?
> getAffine(){ return affine
; }
42 public void setAffine( final AbstractAffineModel2D
< ?
> affine
){ this.affine
= affine
; }
43 public void setAffine( final Class
< ?
extends AbstractAffineModel2D
< ?
> > affineClass
) throws Exception
45 affine
= affineClass
.newInstance();
48 public int getOrder(){ return nlt
.getDimension(); }
49 public void setOrder( final int order
){ nlt
.setDimension( order
); }
51 public float getLambda(){ return lambda
; }
52 public void setLambda( final float lambda
){ this.lambda
= lambda
; }
56 public PolynomialModel2D
copy()
58 final PolynomialModel2D clone
= new PolynomialModel2D();
59 clone
.nlt
= nlt
.copy();
60 clone
.affine
= affine
.copy();
61 clone
.lambda
= lambda
;
66 public < P
extends PointMatch
>void fit( Collection
< P
> pointMatches
) throws NotEnoughDataPointsException
, IllDefinedDataPointsException
68 if ( pointMatches
.size() < getMinNumMatches() )
69 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." );
71 affine
.fit( pointMatches
);
73 final double h1
[][] = new double[ pointMatches
.size() ][ 2 ];
74 final double h2
[][] = new double[ pointMatches
.size() ][ 2 ];
77 for ( final P match
: pointMatches
)
79 final float[] tmp1
= match
.getP1().getL().clone();
80 affine
.applyInPlace( tmp1
);
82 final float[] tmp2
= match
.getP2().getW();
84 h1
[ i
] = new double[]{ tmp1
[ 0 ], tmp1
[ 1 ] };
85 h2
[ i
] = new double[]{ tmp2
[ 0 ], tmp2
[ 1 ] };
90 nlt
.fit( h1
, h2
, lambda
);
94 public int getMinNumMatches()
96 return Math
.max( affine
.getMinNumMatches(), nlt
.getMinNumMatches() );
100 public void set( PolynomialModel2D m
)
103 affine
= m
.affine
.copy();
108 public String
toString()
110 return "affine: " + affine
.toString() + "; polynomial: " + nlt
.toString();
114 public float[] apply( float[] location
)
116 final float[] l
= location
.clone();
122 public void applyInPlace( float[] location
)
124 affine
.applyInPlace( location
);
125 nlt
.applyInPlace( location
);