comparison flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java @ 2342:3efc3942b765

CrossSectionLine: Moved some logic from cross section demo app to this model. flys-backend/trunk@2839 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 27 Sep 2011 13:37:34 +0000
parents 22858e7cca79
children c5d83366d0b1
comparison
equal deleted inserted replaced
2341:8e2fe935ddf1 2342:3efc3942b765
2 2
3 import java.io.Serializable; 3 import java.io.Serializable;
4 4
5 import java.math.BigDecimal; 5 import java.math.BigDecimal;
6 6
7 import java.util.ArrayList;
7 import java.util.List; 8 import java.util.List;
9 import java.util.Collections;
10 import java.util.Comparator;
11
12 import java.awt.geom.Point2D;
8 13
9 import javax.persistence.Entity; 14 import javax.persistence.Entity;
10 import javax.persistence.Id; 15 import javax.persistence.Id;
11 import javax.persistence.Table; 16 import javax.persistence.Table;
12 import javax.persistence.GeneratedValue; 17 import javax.persistence.GeneratedValue;
15 import javax.persistence.GenerationType; 20 import javax.persistence.GenerationType;
16 import javax.persistence.OneToOne; 21 import javax.persistence.OneToOne;
17 import javax.persistence.OneToMany; 22 import javax.persistence.OneToMany;
18 import javax.persistence.JoinColumn; 23 import javax.persistence.JoinColumn;
19 24
25 import org.apache.log4j.Logger;
26
20 @Entity 27 @Entity
21 @Table(name = "cross_section_lines") 28 @Table(name = "cross_section_lines")
22 public class CrossSectionLine 29 public class CrossSectionLine
23 implements Serializable 30 implements Serializable
24 { 31 {
32 private static Logger logger = Logger.getLogger(CrossSectionLine.class);
33
34 public static final double EPSILON = 1e-4;
35
36 public static final double TOO_SMALL = 0.2;
37 public static final double TOO_BIG = 500;
38
25 private Integer id; 39 private Integer id;
26 private BigDecimal km; 40 private BigDecimal km;
27 private CrossSection crossSection; 41 private CrossSection crossSection;
28 42
29 private List<CrossSectionPoint> points; 43 private List<CrossSectionPoint> points;
44
45 public static final Comparator<CrossSectionPoint> COL_POS_CMP =
46 new Comparator<CrossSectionPoint>() {
47 @Override
48 public int compare(CrossSectionPoint a, CrossSectionPoint b) {
49 double xa = a.getX().doubleValue();
50 double xb = b.getX().doubleValue();
51 double d = xa - xb;
52 if (d < -EPSILON) return -1;
53 if (d > +EPSILON) return +1;
54 int diff = a.getColPos() - b.getColPos();
55 return diff < 0 ? -1 : diff > 0 ? +1 : 0;
56 }
57 };
58
59
60 public static final boolean isValid(double x) {
61 x = Math.abs(x);
62 return x > TOO_SMALL && x < TOO_BIG;
63 }
64
30 65
31 public CrossSectionLine() { 66 public CrossSectionLine() {
32 } 67 }
33 68
34 public CrossSectionLine(CrossSection crossSection, BigDecimal km) { 69 public CrossSectionLine(CrossSection crossSection, BigDecimal km) {
79 } 114 }
80 115
81 public void setPoints(List<CrossSectionPoint> points) { 116 public void setPoints(List<CrossSectionPoint> points) {
82 this.points = points; 117 this.points = points;
83 } 118 }
119
120
121 public List<Point2D> fetchCrossSectionLinesPoints() {
122 List<Point2D> points = new ArrayList<Point2D>();
123
124 List<CrossSectionPoint> linePoints = getPoints();
125
126 if (linePoints.isEmpty()) {
127 logger.info("No points in selected CrossSectionLine.");
128 return points;
129 }
130
131 linePoints = new ArrayList(linePoints);
132 Collections.sort(linePoints, COL_POS_CMP);
133
134 for (CrossSectionPoint p: linePoints) {
135 double x = p.getX().doubleValue();
136 double y = p.getY().doubleValue();
137 if (isValid(x) && isValid(y)) {
138 points.add(new Point2D.Double(x, y));
139 }
140 }
141
142 return points;
143 }
144
145 public double [][] fetchCrossSectionProfile() {
146 return fetchCrossSectionProfile(fetchCrossSectionLinesPoints());
147 }
148
149 public static double [][] fetchCrossSectionProfile(List<Point2D> points) {
150
151 int P = points.size();
152
153 double [] xs = new double[P];
154 double [] ys = new double[P];
155
156 if (P > 0) {
157 xs[0] = points.get(0).getX();
158 ys[0] = points.get(0).getY();
159
160 for (int i = 1; i < P; i++) {
161 Point2D p = points.get(i);
162 double x = p.getX();
163 double y = p.getY();
164
165 if (x <= xs[i-1]) {
166 x = xs[i-1] + EPSILON;
167 }
168
169 xs[i] = x;
170 ys[i] = y;
171 }
172 }
173
174 return new double [][] { xs, ys };
175 }
84 } 176 }
85 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 177 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org