ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: sascha@361: package de.intevation.gnv.math; sascha@361: sascha@593: import com.vividsolutions.jts.geom.Coordinate; sascha@593: import com.vividsolutions.jts.geom.Envelope; sascha@593: sascha@361: import java.util.Comparator; sascha@361: sascha@593: import org.apache.log4j.Logger; sascha@361: sascha@361: /** sascha@798: * Point which besides the x, y, z coodinates has an index i, j pair sascha@798: * to model neighborhood. sascha@798: * @author Sascha L. Teichmann sascha@361: */ sascha@361: public class Point2d sascha@361: extends Coordinate sascha@361: { sascha@593: private static Logger log = Logger.getLogger(Point2d.class); sascha@593: sascha@798: /** sascha@798: * Numerical tolerance epsilon: {@value} sascha@798: */ ingo@365: public static final double EPSILON = 1e-3d; sascha@361: sascha@798: /** sascha@798: * Compares two Point2ds by their x coordinates sascha@798: */ sascha@361: public static final Comparator X_COMPARATOR = new Comparator() { sascha@361: public int compare(Object a, Object b) { sascha@361: double xa = ((Coordinate)a).x; sascha@361: double xb = ((Coordinate)b).x; sascha@361: if (xa < xb) return -1; sascha@361: if (xa > xb) return +1; sascha@361: return 0; sascha@361: } sascha@361: }; sascha@361: sascha@798: /** sascha@798: * Compares two Point2ds by their y coordinates. sascha@798: */ sascha@361: public static final Comparator Y_COMPARATOR = new Comparator() { sascha@361: public int compare(Object a, Object b) { sascha@361: double ya = ((Coordinate)a).y; sascha@361: double yb = ((Coordinate)b).y; sascha@361: if (ya < yb) return -1; sascha@361: if (ya > yb) return +1; sascha@361: return 0; sascha@361: } sascha@361: }; sascha@361: sascha@798: /** sascha@798: * The i index of this Point2d. sascha@798: */ sascha@361: public int i; sascha@798: /** sascha@798: * The j index of this Point2d. sascha@798: */ sascha@361: public int j; sascha@361: sascha@798: /** sascha@798: * Default constructor. sascha@798: */ sascha@361: public Point2d() { sascha@361: } sascha@361: sascha@798: /** sascha@798: * Constructor setting x and y coordinate. sascha@798: * @param x The x coordinate. sascha@798: * @param y The y coordinate. sascha@798: */ sascha@593: public Point2d(double x, double y) { sascha@593: super(x, y); sascha@593: } sascha@593: sascha@798: /** sascha@798: * Constructor setting x, y and i, j. sascha@798: * @param x The x coordinate. sascha@798: * @param y The y coordinate. sascha@798: * @param i The i index. sascha@798: * @param j The j index. sascha@798: */ sascha@431: public Point2d(double x, double y, int i, int j) { sascha@431: super(x, y); sascha@431: this.i = i; sascha@431: this.j = j; sascha@431: } sascha@431: sascha@798: /** sascha@798: * Constructor setting x, y, z and i, j. sascha@798: * @param x The x coordinate. sascha@798: * @param y The y coordinate. sascha@798: * @param z The z coordinate. sascha@798: * @param i The i index. sascha@798: * @param j The j index. sascha@798: */ sascha@361: public Point2d(double x, double y, double z, int i, int j) { sascha@361: super(x, y, z); sascha@361: this.i = i; sascha@361: this.j = j; sascha@361: } sascha@361: sascha@798: /** sascha@798: * Calculates the L1 distance to another Point2d. sascha@798: * @param other The other Point2d. sascha@798: * @return The L1 distance. sascha@798: */ sascha@361: public double L1(Point2d other) { sascha@798: return L1Comparator.L1(this, other); sascha@361: } sascha@361: sascha@798: /** sascha@798: * Creates an envelope around this Point2d with sascha@798: * the numerical tolerance of {@link #EPSILON}. sascha@798: * @return The envelope. sascha@798: */ sascha@361: public Envelope envelope() { sascha@361: return envelope(EPSILON); sascha@361: } sascha@361: sascha@798: /** sascha@798: *Creates an envelope around this Point2d with sascha@798: * a given tolerance. sascha@798: * @param epsilon The tolerance in all directions. sascha@798: * @return The envelope. sascha@798: */ sascha@361: public Envelope envelope(double epsilon) { sascha@361: return new Envelope( sascha@361: x-epsilon, x+epsilon, sascha@361: y-epsilon, y+epsilon); sascha@361: } sascha@361: sascha@798: /** sascha@798: * Given this and another Point2d it looks if there is sascha@798: * a gap between the in points in i index direction. sascha@798: * @param other The other Point2d. sascha@798: * @return true if there is is a gap, else false. sascha@798: */ sascha@361: public boolean hasIGap(Point2d other) { sascha@361: return Math.abs(i - other.i) > 1; sascha@361: } sascha@361: sascha@798: /** sascha@798: * Given this and another Point2d it looks if there is sascha@798: * a gap between the in points in j index direction. sascha@798: * @param other The other Point2d. sascha@798: * @return true if there is is a gap, else false. sascha@798: */ sascha@361: public boolean hasJGap(Point2d other) { sascha@361: return Math.abs(j - other.j) > 1; sascha@361: } sascha@593: sascha@798: /** sascha@798: * Given this and another Point2d a new Point2d is sascha@798: * created via {@link #newPoint() }. The x, y coordinate sascha@798: * of the new point is on the line of this and the other sascha@798: * given point at a given scaling point. sascha@798: * @param t The scaling factor. sascha@798: * @param other The other point. sascha@798: * @return The new Point2d. sascha@798: */ sascha@593: public Point2d extrapolate(double t, Point2d other) { sascha@593: if (other == null) { sascha@593: return null; sascha@593: } sascha@593: Point2d p = newPoint(); sascha@593: p.x = t*(other.x - x) + x; sascha@593: p.y = t*(other.y - y) + y; sascha@593: return p; sascha@593: } sascha@593: sascha@798: /** sascha@798: * Creates a new Point2d or an instance of a subclass. sascha@798: * Override this in subclasses. sascha@798: * @return The new Point2d. sascha@798: */ sascha@593: public Point2d newPoint() { sascha@593: return new Point2d(0d, 0d); sascha@593: } sascha@593: sascha@798: /** sascha@798: * Creates a new Point2d or an instance of a subclass sascha@798: * at a given coordinate. sascha@798: * Override this in subclasses. sascha@798: * @param x The x coordinate. sascha@798: * @param y The y coordinate. sascha@798: * @return The new point. sascha@798: */ sascha@593: public Point2d newPoint(double x, double y) { sascha@593: return new Point2d(x, y); sascha@593: } sascha@593: sascha@798: /** sascha@798: * Sets the z value to the inverse distance weighting (IDW) value sascha@798: * of the z values of a set of given points. sascha@798: * @param ps The points from wich the z values are taken sascha@798: * to calculate the IDW. sascha@798: */ sascha@593: public void inverseDistanceWeighting(Point2d [] ps) { sascha@593: sascha@593: double sum = 0d; sascha@593: sascha@593: double [] d = new double[ps.length]; sascha@593: sascha@593: for (int i = 0; i < ps.length; ++i) { sascha@593: Point2d p = ps[i]; sascha@593: if (p != null) { sascha@593: double di = distance(p); sascha@593: if (di < 1e-5d) { z = p.z; return; } sascha@593: di = 1d/di; sascha@593: d[i] = di; sascha@593: sum += di; sascha@593: } sascha@593: } sascha@593: sascha@593: if (sum == 0d) { sascha@593: return; sascha@593: } sascha@593: sascha@593: double v = 0d; sascha@593: sascha@593: for (int i = 0; i < ps.length; ++i) { sascha@593: Point2d p = ps[i]; sascha@593: if (p != null) { sascha@593: v += p.z*d[i]; sascha@593: } sascha@593: } sascha@593: z = v/sum; sascha@593: } sascha@593: sascha@798: /** sascha@798: * Creates a new point via {@link #newPoint() } with the sascha@798: * x,y coordinates of the center of a given set of sascha@798: * coordinates. sascha@798: * @param ps The points from which the x,y coordinates are sascha@798: * taken to calculate the center. sascha@798: * @return The new center point. sascha@798: */ sascha@593: public static Point2d average(Point2d [] ps) { sascha@593: sascha@593: Point2d p = null; sascha@593: int count = 0; sascha@593: sascha@593: for (int i = 0; i < ps.length; ++i) { sascha@593: Point2d t = ps[i]; sascha@593: if (t != null) { sascha@593: ++count; sascha@593: if (p == null) { sascha@593: p = t.newPoint(t.x, t.y); sascha@593: } sascha@593: else { sascha@593: p.x += t.x; sascha@593: p.y += t.y; sascha@593: } sascha@593: } sascha@593: } sascha@593: sascha@593: if (p != null) { sascha@593: double s = 1d/count; sascha@593: p.x *= s; sascha@593: p.y *= s; sascha@593: } sascha@593: sascha@593: return p; sascha@593: } sascha@593: sascha@798: /** sascha@798: * Checks if this Point2d is near to at least one point sascha@798: * out of a given set of points. Near is defined by an sascha@798: * euclidian distance small than {@link #EPSILON}. sascha@798: * @param ps The set of points to be tested. sascha@798: * @return true if this Point2d is near to one of the given sascha@798: * points, else false. sascha@798: */ sascha@593: public boolean near(Point2d [] ps) { sascha@593: sascha@593: for (int i = 0; i < ps.length; ++i) { sascha@593: Point2d p = ps[i]; sascha@593: if (p != null && distance(p) > EPSILON) { sascha@593: return false; sascha@593: } sascha@593: } sascha@593: sascha@593: return true; sascha@593: } sascha@361: } sascha@798: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :