# HG changeset patch # User Sascha L. Teichmann # Date 1317130654 0 # Node ID 3efc3942b76571998084aeb2fc91a12193ed7bf4 # Parent 8e2fe935ddf1fd0247e0fe223a6396640e1ed2d0 CrossSectionLine: Moved some logic from cross section demo app to this model. flys-backend/trunk@2839 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 8e2fe935ddf1 -r 3efc3942b765 flys-backend/ChangeLog --- a/flys-backend/ChangeLog Mon Sep 26 15:26:44 2011 +0000 +++ b/flys-backend/ChangeLog Tue Sep 27 13:37:34 2011 +0000 @@ -1,3 +1,8 @@ +2011-09-27 Sascha L. Teichmann + + * src/main/java/de/intevation/flys/model/CrossSectionLine.java: + Moved some logic from cross section demo app to this model. + 2011-09-26 Ingo Weinzierl * src/main/java/de/intevation/flys/model/CrossSectionTrack.java: diff -r 8e2fe935ddf1 -r 3efc3942b765 flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java --- a/flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java Mon Sep 26 15:26:44 2011 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java Tue Sep 27 13:37:34 2011 +0000 @@ -4,7 +4,12 @@ import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; +import java.util.Collections; +import java.util.Comparator; + +import java.awt.geom.Point2D; import javax.persistence.Entity; import javax.persistence.Id; @@ -17,17 +22,47 @@ import javax.persistence.OneToMany; import javax.persistence.JoinColumn; +import org.apache.log4j.Logger; + @Entity @Table(name = "cross_section_lines") public class CrossSectionLine implements Serializable { + private static Logger logger = Logger.getLogger(CrossSectionLine.class); + + public static final double EPSILON = 1e-4; + + public static final double TOO_SMALL = 0.2; + public static final double TOO_BIG = 500; + private Integer id; private BigDecimal km; private CrossSection crossSection; private List points; + public static final Comparator COL_POS_CMP = + new Comparator() { + @Override + public int compare(CrossSectionPoint a, CrossSectionPoint b) { + double xa = a.getX().doubleValue(); + double xb = b.getX().doubleValue(); + double d = xa - xb; + if (d < -EPSILON) return -1; + if (d > +EPSILON) return +1; + int diff = a.getColPos() - b.getColPos(); + return diff < 0 ? -1 : diff > 0 ? +1 : 0; + } + }; + + + public static final boolean isValid(double x) { + x = Math.abs(x); + return x > TOO_SMALL && x < TOO_BIG; + } + + public CrossSectionLine() { } @@ -81,5 +116,62 @@ public void setPoints(List points) { this.points = points; } + + + public List fetchCrossSectionLinesPoints() { + List points = new ArrayList(); + + List linePoints = getPoints(); + + if (linePoints.isEmpty()) { + logger.info("No points in selected CrossSectionLine."); + return points; + } + + linePoints = new ArrayList(linePoints); + Collections.sort(linePoints, COL_POS_CMP); + + for (CrossSectionPoint p: linePoints) { + double x = p.getX().doubleValue(); + double y = p.getY().doubleValue(); + if (isValid(x) && isValid(y)) { + points.add(new Point2D.Double(x, y)); + } + } + + return points; + } + + public double [][] fetchCrossSectionProfile() { + return fetchCrossSectionProfile(fetchCrossSectionLinesPoints()); + } + + public static double [][] fetchCrossSectionProfile(List points) { + + int P = points.size(); + + double [] xs = new double[P]; + double [] ys = new double[P]; + + if (P > 0) { + xs[0] = points.get(0).getX(); + ys[0] = points.get(0).getY(); + + for (int i = 1; i < P; i++) { + Point2D p = points.get(i); + double x = p.getX(); + double y = p.getY(); + + if (x <= xs[i-1]) { + x = xs[i-1] + EPSILON; + } + + xs[i] = x; + ys[i] = y; + } + } + + return new double [][] { xs, ys }; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :