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.image.processing.transform;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.IOException;
035import java.io.PrintWriter;
036import java.util.Scanner;
037
038import org.openimaj.citation.annotation.Reference;
039import org.openimaj.citation.annotation.ReferenceType;
040import org.openimaj.io.ReadWriteable;
041
042/**
043 * Parameters defining an affine simulation, in terms of a tilt and rotation.
044 * 
045 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
046 */
047@Reference(
048                type = ReferenceType.Article,
049                author = { "Morel, Jean-Michel", "Yu, Guoshen" },
050                title = "{ASIFT: A New Framework for Fully Affine Invariant Image Comparison}",
051                year = "2009",
052                journal = "SIAM J. Img. Sci.",
053                publisher = "Society for Industrial and Applied Mathematics")
054public class AffineParams implements ReadWriteable {
055        /**
056         * The angle of rotation
057         */
058        public float theta;
059
060        /**
061         * The amount of tilt
062         */
063        public float tilt;
064
065        /**
066         * Construct with the given rotation and tilt.
067         * 
068         * @param theta
069         *            the angle of rotation
070         * @param tilt
071         *            the amount of tilt
072         */
073        public AffineParams(float theta, float tilt) {
074                this.theta = theta;
075                this.tilt = tilt;
076        }
077
078        /**
079         * Construct with zero tilt and rotation
080         */
081        public AffineParams() {
082        }
083
084        @Override
085        public boolean equals(Object po) {
086                if (po instanceof AffineParams) {
087                        final AffineParams p = (AffineParams) po;
088                        return (Math.abs(theta - p.theta) < 0.00001 && Math.abs(tilt - p.tilt) < 0.00001);
089                }
090                return false;
091        }
092
093        @Override
094        public int hashCode() {
095                final int hash = new Float(theta).hashCode() ^ new Float(tilt).hashCode();
096                return hash;
097        }
098
099        @Override
100        public String toString() {
101                return String.format("theta:%f tilt:%f", theta, tilt);
102        }
103
104        @Override
105        public void readBinary(DataInput in) throws IOException {
106                this.theta = in.readFloat();
107                this.tilt = in.readFloat();
108        }
109
110        @Override
111        public void readASCII(Scanner in) throws IOException {
112                this.theta = in.nextFloat();
113                this.tilt = in.nextFloat();
114        }
115
116        @Override
117        public byte[] binaryHeader() {
118                return "".getBytes();
119        }
120
121        @Override
122        public String asciiHeader() {
123                return "";
124        }
125
126        @Override
127        public void writeBinary(DataOutput out) throws IOException {
128                out.writeFloat(this.theta);
129                out.writeFloat(this.tilt);
130        }
131
132        @Override
133        public void writeASCII(PrintWriter out) throws IOException {
134                out.println(this.theta);
135                out.println(this.tilt);
136        }
137}