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.feature.local.keypoints;
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.feature.local.ScaleSpaceLocation;
039
040/**
041 * The location of a {@link Keypoint}.
042 * 
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 * 
045 */
046public class KeypointLocation extends ScaleSpaceLocation {
047        private static final long serialVersionUID = 1L;
048
049        /**
050         * The dominant orientation of the {@link Keypoint}
051         */
052        public float orientation;
053
054        /**
055         * Construct with zero location, orientation and scale.
056         */
057        public KeypointLocation() {
058        }
059
060        /**
061         * Construct with the given parameters
062         * 
063         * @param x
064         *            x-ordinate of feature
065         * @param y
066         *            y-ordinate of feature
067         * @param scale
068         *            scale of feature
069         * @param orientation
070         *            orientation of feature
071         */
072        public KeypointLocation(float x, float y, float orientation, float scale) {
073                super(x, y, scale);
074                this.orientation = orientation;
075        }
076
077        @Override
078        public void writeBinary(DataOutput out) throws IOException {
079                out.writeFloat(this.x);
080                out.writeFloat(this.y);
081                out.writeFloat(this.scale);
082                out.writeFloat(this.orientation);
083        }
084
085        @Override
086        public void writeASCII(PrintWriter out) throws IOException {
087                // for legacy reasons ascii format writes y, x, scale, ori
088                out.format("%4.2f %4.2f %4.2f %4.3f", y, x, scale, orientation);
089                out.println();
090        }
091
092        @Override
093        public void readBinary(DataInput in) throws IOException {
094                super.readBinary(in);
095                orientation = in.readFloat();
096        }
097
098        @Override
099        public void readASCII(Scanner in) throws IOException {
100                super.readASCII(in);
101                orientation = Float.parseFloat(in.next());
102        }
103
104        @Override
105        public byte[] binaryHeader() {
106                return "".getBytes();
107        }
108
109        @Override
110        public String asciiHeader() {
111                return "";
112        }
113
114        @Override
115        public Float getOrdinate(int dimension) {
116                final float[] pos = { x, y, scale, orientation };
117                return pos[dimension];
118        }
119
120        @Override
121        public int getDimensions() {
122                return 3;
123        }
124}