View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.processing.transform;
31  
32  import java.io.DataInput;
33  import java.io.DataOutput;
34  import java.io.IOException;
35  import java.io.PrintWriter;
36  import java.util.Scanner;
37  
38  import org.openimaj.citation.annotation.Reference;
39  import org.openimaj.citation.annotation.ReferenceType;
40  import org.openimaj.io.ReadWriteable;
41  
42  /**
43   * Parameters defining an affine simulation, in terms of a tilt and rotation.
44   * 
45   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
46   */
47  @Reference(
48  		type = ReferenceType.Article,
49  		author = { "Morel, Jean-Michel", "Yu, Guoshen" },
50  		title = "{ASIFT: A New Framework for Fully Affine Invariant Image Comparison}",
51  		year = "2009",
52  		journal = "SIAM J. Img. Sci.",
53  		publisher = "Society for Industrial and Applied Mathematics")
54  public class AffineParams implements ReadWriteable {
55  	/**
56  	 * The angle of rotation
57  	 */
58  	public float theta;
59  
60  	/**
61  	 * The amount of tilt
62  	 */
63  	public float tilt;
64  
65  	/**
66  	 * Construct with the given rotation and tilt.
67  	 * 
68  	 * @param theta
69  	 *            the angle of rotation
70  	 * @param tilt
71  	 *            the amount of tilt
72  	 */
73  	public AffineParams(float theta, float tilt) {
74  		this.theta = theta;
75  		this.tilt = tilt;
76  	}
77  
78  	/**
79  	 * Construct with zero tilt and rotation
80  	 */
81  	public AffineParams() {
82  	}
83  
84  	@Override
85  	public boolean equals(Object po) {
86  		if (po instanceof AffineParams) {
87  			final AffineParams p = (AffineParams) po;
88  			return (Math.abs(theta - p.theta) < 0.00001 && Math.abs(tilt - p.tilt) < 0.00001);
89  		}
90  		return false;
91  	}
92  
93  	@Override
94  	public int hashCode() {
95  		final int hash = new Float(theta).hashCode() ^ new Float(tilt).hashCode();
96  		return hash;
97  	}
98  
99  	@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 }