[Dive4elements-commits] [PATCH 1 of 3] Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?

Wald Commits scm-commit at wald.intevation.org
Fri Jan 4 15:56:07 CET 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1357310151 -3600
# Node ID 99f9e371371b9c882354938cc313542dcd8c8baf
# Parent  03246a8b3869a9b1b0e4974be3bb6e265f2efaed
Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?

diff -r 03246a8b3869 -r 99f9e371371b flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java	Fri Jan 04 11:17:40 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java	Fri Jan 04 15:35:51 2013 +0100
@@ -4,10 +4,10 @@
 
 import de.intevation.flys.importer.XY;
 
-import de.intevation.flys.utils.EpsilonComparator;
 import de.intevation.flys.importer.parsers.tim.Coordinate;
 
 import de.intevation.flys.utils.DateGuesser;
+import de.intevation.flys.utils.EpsilonComparator;
 
 import java.io.File;
 import java.io.IOException;
@@ -19,8 +19,6 @@
 import java.util.Map;
 import java.util.TreeMap;
 
-import java.util.regex.Pattern;
-
 import org.apache.log4j.Logger;
 
 
@@ -43,18 +41,19 @@
 
 
     /** Anchor to project to. */
-    private class Anchor {
-        private Coordinate coordinate;
+    private static class Anchor extends Coordinate {
+
+        private static final double EPSILON = 1e-5;
+
         private double station;
-        public Anchor(Coordinate anchor, double station) {
-            this.coordinate = anchor;
+
+        public Anchor(double x, double y, double z, double station) {
+            super(x, y, z);
             this.station = station;
         }
-        public double getStation() {
-            return station;
-        }
-        public Coordinate getCoordinate() {
-            return coordinate;
+
+        public boolean sameStation(double station) {
+            return Math.abs(this.station - station) < EPSILON;
         }
     }
 
@@ -159,9 +158,7 @@
      */
     private boolean addPoint(double gkr, double gkh, double height, String idx) {
         // Calculate distance between this and anchor-point.
-        double dx = gkr - anchor.getCoordinate().getX();
-        double dy = gkh - anchor.getCoordinate().getY();
-        double d  = Math.sqrt(dx * dx + dy * dy);
+        double d = anchor.distance(gkr, gkh);
 
         // TODO: Scale to have "x==0" e.g. at axis of river.
         // TODO: Handle "not straight lines."
@@ -220,8 +217,8 @@
         double heightM   = Double.parseDouble(height);
 
         // New (or first) line.
-        if (anchor == null || anchor.getStation() != stationKm) {
-            this.anchor = new Anchor(new Coordinate(gkRightKm, gkHighKm, heightM), stationKm);
+        if (anchor == null || !anchor.sameStation(stationKm)) {
+            anchor = new Anchor(gkRightKm, gkHighKm, heightM, stationKm);
             currentLine = new ArrayList<XY>();
             data.put(stationKm, currentLine);
             currentLine.add(new XY(0d, heightM,0));
diff -r 03246a8b3869 -r 99f9e371371b flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java	Fri Jan 04 11:17:40 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/tim/Coordinate.java	Fri Jan 04 15:35:51 2013 +0100
@@ -31,5 +31,15 @@
     public double getY() {
         return this.y;
     }
+
+    public final double distanceSqr(double ox, double oy) {
+        double dx = x - ox;
+        double dy = y - oy;
+        return dx*dx + dy*dy;
+    }
+
+    public final double distance(double xo, double yo) {
+        return Math.sqrt(distanceSqr(xo, yo));
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list