comparison flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java @ 2386:cb7ebcadb214 2.6

merged flys-backend/2.6
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:23 +0200
parents e237a08acf6e
children 2f874d14ac68
comparison
equal deleted inserted replaced
2333:66946e278e66 2386:cb7ebcadb214
1 package de.intevation.flys.model;
2
3 import java.io.Serializable;
4
5 import java.math.BigDecimal;
6
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.Collections;
10 import java.util.Comparator;
11
12 import java.awt.geom.Point2D;
13
14 import javax.persistence.Entity;
15 import javax.persistence.Id;
16 import javax.persistence.Table;
17 import javax.persistence.GeneratedValue;
18 import javax.persistence.Column;
19 import javax.persistence.SequenceGenerator;
20 import javax.persistence.GenerationType;
21 import javax.persistence.OneToOne;
22 import javax.persistence.OneToMany;
23 import javax.persistence.JoinColumn;
24
25 import org.apache.log4j.Logger;
26
27 @Entity
28 @Table(name = "cross_section_lines")
29 public class CrossSectionLine
30 implements Serializable
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
39 private Integer id;
40 private BigDecimal km;
41 private CrossSection crossSection;
42
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
65 public static final boolean isValid(Point2D p) {
66 return isValid(p.getX()) && isValid(p.getY());
67 }
68
69
70 public CrossSectionLine() {
71 }
72
73 public CrossSectionLine(CrossSection crossSection, BigDecimal km) {
74 this.crossSection = crossSection;
75 this.km = km;
76 }
77
78 @Id
79 @SequenceGenerator(
80 name = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ",
81 sequenceName = "CROSS_SECTION_LINES_ID_SEQ",
82 allocationSize = 1)
83 @GeneratedValue(
84 strategy = GenerationType.SEQUENCE,
85 generator = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ")
86 @Column(name = "id")
87 public Integer getId() {
88 return id;
89 }
90
91 public void setId(Integer id) {
92 this.id = id;
93 }
94
95 @Column(name = "km")
96 public BigDecimal getKm() {
97 return km;
98 }
99
100 public void setKm(BigDecimal km) {
101 this.km = km;
102 }
103
104 @OneToOne
105 @JoinColumn(name = "cross_section_id")
106 public CrossSection getCrossSection() {
107 return crossSection;
108 }
109
110 public void setCrossSection(CrossSection CrossSection) {
111 this.crossSection = crossSection;
112 }
113
114 @OneToMany
115 @JoinColumn(name="cross_section_line_id")
116 public List<CrossSectionPoint> getPoints() {
117 return points;
118 }
119
120 public void setPoints(List<CrossSectionPoint> points) {
121 this.points = points;
122 }
123
124
125 public List<Point2D> fetchCrossSectionLinesPoints() {
126
127 List<CrossSectionPoint> linePoints =
128 new ArrayList<CrossSectionPoint>(getPoints());
129
130 Collections.sort(linePoints, COL_POS_CMP);
131
132 List<Point2D> points = new ArrayList<Point2D>(linePoints.size());
133 for (CrossSectionPoint p: linePoints) {
134 double x = p.getX().doubleValue();
135 double y = p.getY().doubleValue();
136 if (isValid(x) && isValid(y)) {
137 points.add(new Point2D.Double(x, y));
138 }
139 }
140
141 return points;
142 }
143
144 public double [][] fetchCrossSectionProfile() {
145 return fetchCrossSectionProfile(fetchCrossSectionLinesPoints());
146 }
147
148 public static double [][] fetchCrossSectionProfile(List<Point2D> points) {
149
150 int P = points.size();
151
152 double [] xs = new double[P];
153 double [] ys = new double[P];
154
155 if (P > 0) {
156 xs[0] = points.get(0).getX();
157 ys[0] = points.get(0).getY();
158
159 for (int i = 1; i < P; i++) {
160 Point2D p = points.get(i);
161 double x = p.getX();
162 double y = p.getY();
163
164 if (x <= xs[i-1]) {
165 x = xs[i-1] + EPSILON;
166 }
167
168 xs[i] = x;
169 ys[i] = y;
170 }
171 }
172
173 return new double [][] { xs, ys };
174 }
175 }
176 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org