root / trunk / libraries / libRaster / src / org / gvsig / raster / datastruct / Extent.java @ 17782
History | View | Annotate | Download (11.2 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
package org.gvsig.raster.datastruct; |
20 |
|
21 |
import java.awt.geom.Point2D; |
22 |
import java.awt.geom.Rectangle2D; |
23 |
import java.text.DecimalFormat; |
24 |
|
25 |
/**
|
26 |
* Clase que getiona el extent de una imagen
|
27 |
*
|
28 |
* @author Luis W.Sevilla (sevilla_lui@gva.es)
|
29 |
*/
|
30 |
public class Extent { |
31 |
Point2D min = null; |
32 |
Point2D max = null; |
33 |
|
34 |
Point2D ul = new Point2D.Double(999999999.0, 999999999.0); |
35 |
Point2D lr = new Point2D.Double(999999999.0, 999999999.0); |
36 |
Point2D ur = new Point2D.Double(999999999.0, 999999999.0); |
37 |
Point2D ll = new Point2D.Double(999999999.0, 999999999.0); |
38 |
|
39 |
/**
|
40 |
* Constructor sin par?metros
|
41 |
*/
|
42 |
public Extent() {
|
43 |
min = new Point2D.Double(999999999.0, 999999999.0); |
44 |
max = new Point2D.Double(-999999999.0, -999999999.0); |
45 |
} |
46 |
|
47 |
/**
|
48 |
* Constructor
|
49 |
* @param pt1 punto que representa la esquina superior izquierda
|
50 |
* @param pt2 punto que representa la esquina inferior derecha
|
51 |
*/
|
52 |
public Extent(Point2D ul, Point2D lr) { |
53 |
this.ul = ul;
|
54 |
this.lr = lr;
|
55 |
newExtent(ul.getX(), ul.getY(), lr.getX(), lr.getY()); |
56 |
} |
57 |
|
58 |
/**
|
59 |
* Constructor
|
60 |
* @param ul punto que representa la esquina superior izquierda
|
61 |
* @param lr punto que representa la esquina inferior derecha
|
62 |
* @param ur punto que representa la esquina superior derecha
|
63 |
* @param ll punto que representa la esquina inferior izquierda
|
64 |
*/
|
65 |
public Extent(Point2D ul, Point2D lr, Point2D ur, Point2D ll) { |
66 |
this.ul = ul;
|
67 |
this.lr = lr;
|
68 |
this.ur = ur;
|
69 |
this.ll = ll;
|
70 |
min = new Point2D.Double( |
71 |
Math.min(Math.min(ul.getX(), lr.getX()), Math.min(ur.getX(), ll.getX())), |
72 |
Math.min(Math.min(ul.getY(), lr.getY()), Math.min(ur.getY(), ll.getY()))); |
73 |
max = new Point2D.Double( |
74 |
Math.max(Math.max(ul.getX(), lr.getX()), Math.max(ur.getX(), ll.getX())), |
75 |
Math.max(Math.max(ul.getY(), lr.getY()), Math.max(ur.getY(), ll.getY()))); |
76 |
} |
77 |
|
78 |
/**
|
79 |
* Contructor
|
80 |
* @param x1 punto que representa la coordenada X de la esquina superior izquierda
|
81 |
* @param y1 punto que representa la coordenada Y de la esquina superior izquierda
|
82 |
* @param x2 punto que representa la coordenada X de la esquina inferior derecha
|
83 |
* @param y2 punto que representa la coordenada Y de la esquina inferior derecha
|
84 |
*/
|
85 |
public Extent(double x1, double y1, double x2, double y2) { |
86 |
ul.setLocation(x1, y1); |
87 |
lr.setLocation(x2, y2); |
88 |
newExtent(x1, y1, x2, y2); |
89 |
} |
90 |
|
91 |
/**
|
92 |
* Constructor
|
93 |
* @param r Rectangulo 2D
|
94 |
*/
|
95 |
public Extent(Rectangle2D r) { |
96 |
ul.setLocation(r.getX(), r.getY() + r.getHeight()); |
97 |
lr.setLocation(r.getX() + r.getWidth(), r.getY()); |
98 |
newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(), |
99 |
r.getY() + r.getHeight()); |
100 |
} |
101 |
|
102 |
/**
|
103 |
* Constructor de copia
|
104 |
* @param ext Objeto Extent
|
105 |
*/
|
106 |
public Extent(Extent ext) {
|
107 |
ul.setLocation(ext.getULX(), ext.getULY()); |
108 |
lr.setLocation(ext.getLRX(), ext.getLRY()); |
109 |
ur.setLocation(ext.getURX(), ext.getURY()); |
110 |
ll.setLocation(ext.getLLX(), ext.getLLY()); |
111 |
min = new Point2D.Double(ext.minX(), ext.minY()); |
112 |
max = new Point2D.Double(ext.maxX(), ext.maxY()); |
113 |
} |
114 |
|
115 |
/**
|
116 |
* Crea un objeto extent identico y lo retorna
|
117 |
* @return Objeto extent
|
118 |
*/
|
119 |
public Object clone() { |
120 |
Extent e = (Extent) clone(); |
121 |
e.min = (Point2D) min.clone();
|
122 |
e.max = (Point2D) max.clone();
|
123 |
|
124 |
e.ul = (Point2D) ul.clone();
|
125 |
e.lr = (Point2D) lr.clone();
|
126 |
e.ur = (Point2D) ur.clone();
|
127 |
e.ll = (Point2D) ll.clone();
|
128 |
return e;
|
129 |
} |
130 |
|
131 |
private void newExtent(double x1, double y1, double x2, double y2) { |
132 |
min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
133 |
max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
134 |
} |
135 |
|
136 |
/**
|
137 |
* Obtiene la coordenada X m?nima
|
138 |
* @return valor de la coordenada X m?nima
|
139 |
*/
|
140 |
public double minX() { |
141 |
return min.getX();
|
142 |
} |
143 |
|
144 |
/**
|
145 |
* Obtiene la coordenada Y m?nima
|
146 |
* @return valor de la coordenada X m?nima
|
147 |
*/
|
148 |
public double minY() { |
149 |
return min.getY();
|
150 |
} |
151 |
|
152 |
/**
|
153 |
* Obtiene la coordenada X m?xima
|
154 |
* @return valor de la coordenada X m?xima
|
155 |
*/
|
156 |
public double maxX() { |
157 |
return max.getX();
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* Obtiene la coordenada Y m?xima
|
162 |
* @return valor de la coordenada Y m?xima
|
163 |
*/
|
164 |
public double maxY() { |
165 |
return max.getY();
|
166 |
} |
167 |
|
168 |
/**
|
169 |
* Obtiene el punto m?nimo
|
170 |
* @return m?nimo
|
171 |
*/
|
172 |
public Point2D getMin() { |
173 |
return min;
|
174 |
} |
175 |
|
176 |
/**
|
177 |
* Obtiene el punto m?ximo
|
178 |
* @return m?ximo
|
179 |
*/
|
180 |
public Point2D getMax() { |
181 |
return max;
|
182 |
} |
183 |
|
184 |
public boolean isAt(Point2D pt) { |
185 |
if (pt.getX() < minX())
|
186 |
return false; |
187 |
|
188 |
if (pt.getX() > maxX())
|
189 |
return false; |
190 |
|
191 |
if (pt.getY() < minY())
|
192 |
return false; |
193 |
|
194 |
if (pt.getY() > maxY())
|
195 |
return false; |
196 |
|
197 |
return true; |
198 |
} |
199 |
|
200 |
public double width() { |
201 |
return Math.abs(maxX() - minX()); |
202 |
} |
203 |
|
204 |
public double height() { |
205 |
return Math.abs(maxY() - minY()); |
206 |
} |
207 |
|
208 |
/**
|
209 |
* Verifica un punto, y modifica el extent si no est? incluido
|
210 |
*/
|
211 |
public void add(Point2D pt) { |
212 |
if (pt == null) { |
213 |
return;
|
214 |
} |
215 |
|
216 |
min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY())); |
217 |
max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY())); |
218 |
} |
219 |
|
220 |
public void add(Extent ext) { |
221 |
if (ext == null) { |
222 |
return;
|
223 |
} |
224 |
|
225 |
min.setLocation(Math.min(ext.minX(), minX()),
|
226 |
Math.min(ext.minY(), minY()));
|
227 |
max.setLocation(Math.max(ext.maxX(), maxX()),
|
228 |
Math.max(ext.maxY(), maxY()));
|
229 |
} |
230 |
|
231 |
/**
|
232 |
* Obtiene la escala
|
233 |
* @param width Ancho
|
234 |
* @param height Alto
|
235 |
* @return
|
236 |
*/
|
237 |
public double[] getScale(int width, int height) { |
238 |
return getScale((double) width, (double) height); |
239 |
} |
240 |
|
241 |
public double[] getScale(double width, double height) { |
242 |
double[] scale = new double[2]; |
243 |
scale[0] = ((float) width) / width(); |
244 |
scale[1] = ((float) height) / height(); |
245 |
|
246 |
return scale;
|
247 |
} |
248 |
|
249 |
public Rectangle2D toRectangle2D() { |
250 |
return new Rectangle2D.Double(minX(), minY(), width(), height()); |
251 |
} |
252 |
|
253 |
public String toString() { |
254 |
DecimalFormat format = new DecimalFormat("####.000"); |
255 |
|
256 |
return "Extent: (" + format.format(minX()) + "," + |
257 |
format.format(minY()) + "), (" + format.format(maxX()) + "," + |
258 |
format.format(maxY()) + ")";
|
259 |
} |
260 |
|
261 |
public interface Has { |
262 |
public Extent getExtent();
|
263 |
} |
264 |
|
265 |
/**
|
266 |
* Obtiene la coordenada X de la esquina superior izquierda. Esta, en condiciones normales
|
267 |
* conincide con el minimo en X pero cuando un raster est? rotado esto puede variar.
|
268 |
* @return ulx
|
269 |
*/
|
270 |
public double getULX() { |
271 |
if(ul.getX() == -999999999.0) |
272 |
return minX();
|
273 |
return ul.getX();
|
274 |
} |
275 |
|
276 |
/**
|
277 |
* Obtiene la coordenada Y de la esquina superior izquierda. Esta, en condiciones normales
|
278 |
* conincide con el maximo en Y pero cuando un raster est? rotado esto puede variar.
|
279 |
* @return uly
|
280 |
*/
|
281 |
public double getULY() { |
282 |
if(ul.getY() == -999999999.0) |
283 |
return maxY();
|
284 |
return ul.getY();
|
285 |
} |
286 |
|
287 |
/**
|
288 |
* Obtiene la coordenada X de la esquina inferior derecha. Esta, en condiciones normales
|
289 |
* conincide con el m?ximo en X pero cuando un raster est? rotado esto puede variar.
|
290 |
* @return lrx
|
291 |
*/
|
292 |
public double getLRX() { |
293 |
if(lr.getX() == -999999999.0) |
294 |
return maxX();
|
295 |
return lr.getX();
|
296 |
} |
297 |
|
298 |
/**
|
299 |
* Obtiene la coordenada Y de la esquina inferior derecha. Esta, en condiciones normales
|
300 |
* conincide con el minimo en Y pero cuando un raster est? rotado esto puede variar.
|
301 |
* @return lry
|
302 |
*/
|
303 |
public double getLRY() { |
304 |
if(lr.getX() == -999999999.0) |
305 |
return minY();
|
306 |
return lr.getY();
|
307 |
} |
308 |
|
309 |
/**
|
310 |
* Obtiene la coordenada X de la esquina superior derecha.
|
311 |
* @return urx
|
312 |
*/
|
313 |
public double getURX() { |
314 |
return ur.getX();
|
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene la coordenada Y de la esquina superior derecha.
|
319 |
* @return uly
|
320 |
*/
|
321 |
public double getURY() { |
322 |
return ur.getY();
|
323 |
} |
324 |
|
325 |
/**
|
326 |
* Obtiene la coordenada X de la esquina inferior izquierda.
|
327 |
* @return lrx
|
328 |
*/
|
329 |
public double getLLX() { |
330 |
return ll.getX();
|
331 |
} |
332 |
|
333 |
/**
|
334 |
* Obtiene la coordenada Y de la esquina inferior izquierda.
|
335 |
* @return lly
|
336 |
*/
|
337 |
public double getLLY() { |
338 |
return ll.getY();
|
339 |
} |
340 |
|
341 |
/**
|
342 |
* Asigna la coordenada X de la esquina superior izquierda al m?nimo X
|
343 |
*/
|
344 |
public void setULXToMin() { |
345 |
ul.setLocation(minX(), ul.getY()); |
346 |
lr.setLocation(maxX(), lr.getY()); |
347 |
} |
348 |
|
349 |
/**
|
350 |
* Asigna la coordenada X de la esquina superior izquierda al m?ximo X
|
351 |
*/
|
352 |
public void setULXToMax() { |
353 |
ul.setLocation(maxX(), ul.getY()); |
354 |
lr.setLocation(minX(), lr.getY()); |
355 |
} |
356 |
|
357 |
/**
|
358 |
* Asigna la coordenada Y de la esquina superior izquierda al m?nimo Y
|
359 |
*/
|
360 |
public void setULYToMin() { |
361 |
ul.setLocation(ul.getX(), minY()); |
362 |
lr.setLocation(lr.getX(), maxY()); |
363 |
} |
364 |
|
365 |
/**
|
366 |
* Asigna la coordenada Y de la esquina superior izquierda al m?ximo Y
|
367 |
*/
|
368 |
public void setULYToMax() { |
369 |
ul.setLocation(ul.getX(), maxY()); |
370 |
lr.setLocation(lr.getX(), minY()); |
371 |
} |
372 |
|
373 |
/**
|
374 |
* Asigna la coordenada X de la esquina inferior derecha al m?nimo X
|
375 |
*/
|
376 |
public void setLRXToMin() { |
377 |
lr.setLocation(minX(), lr.getY()); |
378 |
ul.setLocation(maxX(), ul.getY()); |
379 |
|
380 |
} |
381 |
|
382 |
/**
|
383 |
* Asigna la coordenada X de la esquina inferior derecha al m?ximo X
|
384 |
*/
|
385 |
public void setLRXToMax() { |
386 |
lr.setLocation(maxX(), lr.getY()); |
387 |
ul.setLocation(minX(), ul.getY()); |
388 |
} |
389 |
|
390 |
/**
|
391 |
* Asigna la coordenada Y de la esquina inferior derecha al m?nimo Y
|
392 |
*/
|
393 |
public void setLRYToMin() { |
394 |
lr.setLocation(lr.getX(), minY()); |
395 |
ul.setLocation(ul.getX(), maxX()); |
396 |
} |
397 |
|
398 |
/**
|
399 |
* Asigna la coordenada Y de la esquina inferior derecha al m?ximo Y
|
400 |
*/
|
401 |
public void setLRYToMax() { |
402 |
lr.setLocation(lr.getX(), maxY()); |
403 |
ul.setLocation(ul.getX(), minY()); |
404 |
} |
405 |
} |