Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/model/CrossSectionLine.java @ 2877:f0a67bc0e777 2.7
merged flys-backend/2.7
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:31 +0200 |
parents | 2f874d14ac68 |
children | 2ae732e2c65c |
comparison
equal
deleted
inserted
replaced
2793:6310b1582f2d | 2877:f0a67bc0e777 |
---|---|
1 package de.intevation.flys.model; | |
2 | |
3 import java.io.Serializable; | |
4 | |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 import java.util.Collections; | |
8 import java.util.Comparator; | |
9 | |
10 import java.awt.geom.Point2D; | |
11 | |
12 import javax.persistence.Entity; | |
13 import javax.persistence.Id; | |
14 import javax.persistence.Table; | |
15 import javax.persistence.GeneratedValue; | |
16 import javax.persistence.Column; | |
17 import javax.persistence.SequenceGenerator; | |
18 import javax.persistence.GenerationType; | |
19 import javax.persistence.OneToOne; | |
20 import javax.persistence.OneToMany; | |
21 import javax.persistence.JoinColumn; | |
22 | |
23 import org.apache.log4j.Logger; | |
24 | |
25 @Entity | |
26 @Table(name = "cross_section_lines") | |
27 public class CrossSectionLine | |
28 implements Serializable | |
29 { | |
30 private static Logger logger = Logger.getLogger(CrossSectionLine.class); | |
31 | |
32 public static final double EPSILON = 1e-4; | |
33 | |
34 public static final double TOO_SMALL = 0.2; | |
35 public static final double TOO_BIG = 500; | |
36 | |
37 private Integer id; | |
38 private Double km; | |
39 private CrossSection crossSection; | |
40 | |
41 private List<CrossSectionPoint> points; | |
42 | |
43 public static final Comparator<CrossSectionPoint> COL_POS_CMP = | |
44 new Comparator<CrossSectionPoint>() { | |
45 @Override | |
46 public int compare(CrossSectionPoint a, CrossSectionPoint b) { | |
47 double xa = a.getX().doubleValue(); | |
48 double xb = b.getX().doubleValue(); | |
49 double d = xa - xb; | |
50 if (d < -EPSILON) return -1; | |
51 if (d > +EPSILON) return +1; | |
52 int diff = a.getColPos() - b.getColPos(); | |
53 return diff < 0 ? -1 : diff > 0 ? +1 : 0; | |
54 } | |
55 }; | |
56 | |
57 | |
58 public static final boolean isValid(double x) { | |
59 x = Math.abs(x); | |
60 return x > TOO_SMALL && x < TOO_BIG; | |
61 } | |
62 | |
63 public static final boolean isValid(Point2D p) { | |
64 return isValid(p.getX()) && isValid(p.getY()); | |
65 } | |
66 | |
67 | |
68 public CrossSectionLine() { | |
69 } | |
70 | |
71 public CrossSectionLine(CrossSection crossSection, Double km) { | |
72 this.crossSection = crossSection; | |
73 this.km = km; | |
74 } | |
75 | |
76 @Id | |
77 @SequenceGenerator( | |
78 name = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ", | |
79 sequenceName = "CROSS_SECTION_LINES_ID_SEQ", | |
80 allocationSize = 1) | |
81 @GeneratedValue( | |
82 strategy = GenerationType.SEQUENCE, | |
83 generator = "SEQUENCE_CROSS_SECTION_LINES_ID_SEQ") | |
84 @Column(name = "id") | |
85 public Integer getId() { | |
86 return id; | |
87 } | |
88 | |
89 public void setId(Integer id) { | |
90 this.id = id; | |
91 } | |
92 | |
93 @Column(name = "km") | |
94 public Double getKm() { | |
95 return km; | |
96 } | |
97 | |
98 public void setKm(Double km) { | |
99 this.km = km; | |
100 } | |
101 | |
102 @OneToOne | |
103 @JoinColumn(name = "cross_section_id") | |
104 public CrossSection getCrossSection() { | |
105 return crossSection; | |
106 } | |
107 | |
108 public void setCrossSection(CrossSection CrossSection) { | |
109 this.crossSection = crossSection; | |
110 } | |
111 | |
112 @OneToMany | |
113 @JoinColumn(name="cross_section_line_id") | |
114 public List<CrossSectionPoint> getPoints() { | |
115 return points; | |
116 } | |
117 | |
118 public void setPoints(List<CrossSectionPoint> points) { | |
119 this.points = points; | |
120 } | |
121 | |
122 | |
123 public List<Point2D> fetchCrossSectionLinesPoints() { | |
124 | |
125 List<CrossSectionPoint> linePoints = | |
126 new ArrayList<CrossSectionPoint>(getPoints()); | |
127 | |
128 Collections.sort(linePoints, COL_POS_CMP); | |
129 | |
130 List<Point2D> points = new ArrayList<Point2D>(linePoints.size()); | |
131 for (CrossSectionPoint p: linePoints) { | |
132 double x = p.getX().doubleValue(); | |
133 double y = p.getY().doubleValue(); | |
134 if (isValid(x) && isValid(y)) { | |
135 points.add(new Point2D.Double(x, y)); | |
136 } | |
137 } | |
138 | |
139 return points; | |
140 } | |
141 | |
142 public double [][] fetchCrossSectionProfile() { | |
143 return fetchCrossSectionProfile(fetchCrossSectionLinesPoints()); | |
144 } | |
145 | |
146 public static double [][] fetchCrossSectionProfile(List<Point2D> points) { | |
147 | |
148 int P = points.size(); | |
149 | |
150 double [] xs = new double[P]; | |
151 double [] ys = new double[P]; | |
152 | |
153 if (P > 0) { | |
154 xs[0] = points.get(0).getX(); | |
155 ys[0] = points.get(0).getY(); | |
156 | |
157 for (int i = 1; i < P; i++) { | |
158 Point2D p = points.get(i); | |
159 double x = p.getX(); | |
160 double y = p.getY(); | |
161 | |
162 if (x <= xs[i-1]) { | |
163 x = xs[i-1] + EPSILON; | |
164 } | |
165 | |
166 xs[i] = x; | |
167 ys[i] = y; | |
168 } | |
169 } | |
170 | |
171 return new double [][] { xs, ys }; | |
172 } | |
173 } | |
174 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |