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@593: * @author Sascha L. Teichmann (sascha.teichmann@intevation.de) 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: ingo@365: public static final double EPSILON = 1e-3d; sascha@361: 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@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@361: public static class InverseL1Comparator sascha@361: implements Comparator sascha@361: { sascha@361: private Point2d ref; sascha@361: sascha@361: public InverseL1Comparator(Point2d ref) { sascha@361: this.ref = ref; sascha@361: } sascha@361: sascha@361: public int compare(Object a, Object b) { sascha@361: Point2d pa = (Point2d)a; sascha@361: Point2d pb = (Point2d)b; sascha@361: double da = ref.L1(pa); sascha@361: double db = ref.L1(pb); sascha@361: if (da < db) return -1; sascha@361: if (da > db) return +1; sascha@361: return 0; sascha@361: } sascha@361: } // class InverseL1Comparator sascha@361: sascha@361: public int i; sascha@361: public int j; sascha@361: sascha@361: public Point2d() { sascha@361: } sascha@361: sascha@593: public Point2d(double x, double y) { sascha@593: super(x, y); sascha@593: } sascha@593: 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@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@361: sascha@361: public double L1(Point2d other) { sascha@361: return L1(this, other); sascha@361: } sascha@361: sascha@361: public static double L1(Coordinate a, Coordinate b) { sascha@361: return Math.abs(a.x - b.x) + Math.abs(a.y - b.y); sascha@361: } sascha@361: sascha@361: public Envelope envelope() { sascha@361: return envelope(EPSILON); sascha@361: } sascha@361: 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@361: public boolean hasIGap(Point2d other) { sascha@361: return Math.abs(i - other.i) > 1; sascha@361: } sascha@361: sascha@361: public boolean hasJGap(Point2d other) { sascha@361: return Math.abs(j - other.j) > 1; sascha@361: } sascha@593: 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@593: public Point2d newPoint() { sascha@593: return new Point2d(0d, 0d); sascha@593: } sascha@593: sascha@593: public Point2d newPoint(double x, double y) { sascha@593: return new Point2d(x, y); sascha@593: } sascha@593: 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@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@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@361: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: