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