001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.math.geometry.transforms; 031 032import java.util.List; 033 034import org.openimaj.math.geometry.point.Point2d; 035import org.openimaj.math.model.EstimatableModel; 036import org.openimaj.util.function.Predicate; 037import org.openimaj.util.pair.IndependentPair; 038 039import Jama.Matrix; 040 041/** 042 * Concrete implementation of a model of an Affine transform. Capable of 043 * least-squares estimate of model parameters using the SVD. 044 * 045 * @author Jonathon Hare 046 * 047 */ 048public class AffineTransformModel implements EstimatableModel<Point2d, Point2d>, MatrixTransformProvider { 049 protected Predicate<AffineTransformModel> modelCheck; 050 protected Matrix transform; 051 052 /** 053 * Create an {@link AffineTransformModel} 054 */ 055 public AffineTransformModel() 056 { 057 transform = new Matrix(3, 3); 058 059 transform.set(2, 0, 0); 060 transform.set(2, 1, 0); 061 transform.set(2, 2, 1); 062 } 063 064 /** 065 * Create an {@link AffineTransformModel}. The given {@link Predicate} is 066 * used by the {@link #estimate(List)} method to test whether the estimated 067 * affine transform is sensible. 068 * 069 * @param mc 070 * the test function for sensible affine transforms 071 */ 072 public AffineTransformModel(Predicate<AffineTransformModel> mc) 073 { 074 this.modelCheck = mc; 075 transform = new Matrix(3, 3); 076 077 transform.set(2, 0, 0); 078 transform.set(2, 1, 0); 079 transform.set(2, 2, 1); 080 } 081 082 @Override 083 public AffineTransformModel clone() { 084 final AffineTransformModel atm = new AffineTransformModel(); 085 atm.modelCheck = modelCheck; 086 atm.transform = transform.copy(); 087 return atm; 088 } 089 090 @Override 091 public Matrix getTransform() { 092 return transform; 093 } 094 095 /* 096 * SVD least-squares estimation of affine transform matrix for a set of 2d 097 * points 098 */ 099 @Override 100 public boolean estimate(List<? extends IndependentPair<Point2d, Point2d>> data) { 101 if (data.size() < numItemsToEstimate()) 102 return false; 103 104 this.transform = TransformUtilities.affineMatrix(data); 105 106 if (modelCheck == null) 107 return true; 108 109 return modelCheck.test(this); 110 } 111 112 @Override 113 public Point2d predict(Point2d p) { 114 return p.transform(transform); 115 } 116 117 @Override 118 public int numItemsToEstimate() { 119 return 3; 120 } 121 122 @Override 123 public String toString() { 124 String str = ""; 125 final double[][] mat = transform.getArray(); 126 for (final double[] r : mat) { 127 for (final double v : r) { 128 str += " " + v; 129 } 130 str += "\n"; 131 } 132 return str; 133 } 134}