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.point; 031 032import java.io.DataInput; 033import java.io.DataOutput; 034import java.io.IOException; 035import java.io.PrintWriter; 036import java.util.Scanner; 037 038import Jama.Matrix; 039 040/** 041 * Abstract base for {@link Point2d} implementations that retains the underlying 042 * precision. 043 * 044 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 045 * 046 */ 047public abstract class AbstractPoint2d implements Point2d { 048 @Override 049 public int getDimensions() { 050 return 2; 051 } 052 053 @Override 054 public boolean equals(Object o) { 055 if (!(o instanceof Point2d)) 056 return false; 057 final Point2d p = (Point2d) o; 058 return p.getOrdinate(0).equals(getOrdinate(0)) && p.getOrdinate(1).equals(getOrdinate(1)); 059 } 060 061 @Override 062 public int hashCode() { 063 return toString().hashCode(); 064 } 065 066 @Override 067 public void readASCII(Scanner in) throws IOException { 068 setOrdinate(0, in.nextDouble()); 069 setOrdinate(1, in.nextDouble()); 070 } 071 072 @Override 073 public String asciiHeader() { 074 return "Point2d"; 075 } 076 077 @Override 078 public void readBinary(DataInput in) throws IOException { 079 setOrdinate(0, in.readDouble()); 080 setOrdinate(1, in.readDouble()); 081 } 082 083 @Override 084 public byte[] binaryHeader() { 085 return "PT2DD".getBytes(); 086 } 087 088 @Override 089 public void writeASCII(PrintWriter out) throws IOException { 090 out.format("%f %f", getOrdinate(0).doubleValue(), getOrdinate(1).doubleValue()); 091 } 092 093 @Override 094 public void writeBinary(DataOutput out) throws IOException { 095 out.writeDouble(getOrdinate(0).doubleValue()); 096 out.writeDouble(getOrdinate(1).doubleValue()); 097 } 098 099 @Override 100 public float getX() { 101 return getOrdinate(0).floatValue(); 102 } 103 104 @Override 105 public void setX(float x) { 106 setOrdinate(0, x); 107 } 108 109 @Override 110 public float getY() { 111 return getOrdinate(1).floatValue(); 112 } 113 114 @Override 115 public void setY(float y) { 116 setOrdinate(1, y); 117 } 118 119 @Override 120 public void copyFrom(Point2d p) { 121 setOrdinate(0, p.getOrdinate(0)); 122 setOrdinate(1, p.getOrdinate(1)); 123 } 124 125 @Override 126 public void translate(float x, float y) { 127 setOrdinate(0, getOrdinate(0).doubleValue() + x); 128 setOrdinate(0, getOrdinate(1).doubleValue() + y); 129 } 130 131 @Override 132 public Point2d transform(Matrix transform) { 133 double xt, yt, zt; 134 if (transform.getRowDimension() == 3) { 135 xt = transform.get(0, 0) * getX() + transform.get(0, 1) * getY() 136 + transform.get(0, 2); 137 yt = transform.get(1, 0) * getX() + transform.get(1, 1) * getY() 138 + transform.get(1, 2); 139 zt = transform.get(2, 0) * getX() + transform.get(2, 1) * getY() 140 + transform.get(2, 2); 141 142 xt /= zt; 143 yt /= zt; 144 } else if (transform.getRowDimension() == 2 && transform.getColumnDimension() == 2) { 145 xt = transform.get(0, 0) * getX() + transform.get(0, 1) * getY(); 146 yt = transform.get(1, 0) * getX() + transform.get(1, 1) * getY(); 147 } else if (transform.getRowDimension() == 2 && transform.getColumnDimension() == 3) { 148 xt = transform.get(0, 0) * getX() + transform.get(0, 1) * getY() + transform.get(0, 2); 149 yt = transform.get(1, 0) * getX() + transform.get(1, 1) * getY() + transform.get(1, 2); 150 } else { 151 throw new IllegalArgumentException("Transform matrix has unexpected size"); 152 } 153 154 final Point2d cpy = copy(); 155 cpy.setOrdinate(0, xt); 156 cpy.setOrdinate(1, yt); 157 return cpy; 158 } 159 160 @Override 161 public Point2d minus(Point2d a) { 162 final Point2d cpy = copy(); 163 cpy.setOrdinate(0, getOrdinate(0).doubleValue() - a.getOrdinate(0).doubleValue()); 164 cpy.setOrdinate(1, getOrdinate(1).doubleValue() - a.getOrdinate(1).doubleValue()); 165 return cpy; 166 } 167 168 @Override 169 public void translate(Point2d v) { 170 setOrdinate(0, getOrdinate(0).doubleValue() + v.getOrdinate(0).doubleValue()); 171 setOrdinate(1, getOrdinate(1).doubleValue() + v.getOrdinate(1).doubleValue()); 172 } 173}