Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / shared / Extent.java @ 10740

History | View | Annotate | Download (5.92 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.shared;
20

    
21
import java.awt.geom.Point2D;
22
import java.awt.geom.Rectangle2D;
23

    
24
import java.text.DecimalFormat;
25

    
26
/**
27
 *        Clase que getiona el extent de una imagen
28
 *        
29
 *  @author Luis W.Sevilla (sevilla_lui@gva.es)
30
 */
31
public class Extent {
32
    Point2D min = null;
33
    Point2D max = null;
34

    
35
    /**
36
     * Constructor sin par?metros
37
     */
38
    public Extent() {
39
        min = new Point2D.Double(999999999.0, 999999999.0);
40
        max = new Point2D.Double(-999999999.0, -999999999.0);
41
    }
42

    
43
    /**
44
     * Constructor 
45
     * @param pt1        punto que representa la esquina superior izquierda
46
     * @param pt2        punto que representa la esquina inferior derecha
47
     */
48
    public Extent(Point2D pt1, Point2D pt2) {
49
        newExtent(pt1.getX(), pt1.getY(), pt2.getX(), pt2.getY());
50
    }
51

    
52
    /**
53
     * Contructor
54
     * @param x1 punto que representa la coordenada X de la esquina superior izquierda
55
     * @param y1 punto que representa la coordenada Y de la esquina superior izquierda
56
     * @param x2 punto que representa la coordenada X de la esquina inferior derecha
57
     * @param y2 punto que representa la coordenada Y de la esquina inferior derecha
58
     */
59
    public Extent(double x1, double y1, double x2, double y2) {
60
        newExtent(x1, y1, x2, y2);
61
    }
62

    
63
    /**
64
     * Constructor
65
     * @param r        Rectangulo 2D
66
     */
67
    public Extent(Rectangle2D r) {
68
        newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(),
69
                  r.getY() + r.getHeight());
70
    }
71

    
72
    /**
73
     * Constructor de copia
74
     * @param ext        Objeto Extent
75
     */
76
    public Extent(Extent ext) {
77
        newExtent(ext.minX(), ext.minY(), ext.maxX(), ext.maxY());
78
    }
79

    
80
    /**
81
     * Crea un objeto extent identico y lo retorna
82
     * @return Objeto extent
83
     */
84
    public Object clone() {
85
        Extent e = (Extent) clone();
86
        e.min = (Point2D) min.clone();
87
        e.max = (Point2D) max.clone();
88

    
89
        return e;
90
    }
91

    
92
    private void newExtent(double x1, double y1, double x2, double y2) {
93
        double[] e = { x1, y1, x2, y2 };
94
        min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2));
95
        max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2));
96
    }
97

    
98
    /**
99
     * Obtiene la coordenada X m?nima
100
     * @return valor de la coordenada X m?nima
101
     */
102
    public double minX() {
103
        return min.getX();
104
    }
105

    
106
    /**
107
     * Obtiene la coordenada Y m?nima
108
     * @return valor de la coordenada X m?nima
109
     */
110
    public double minY() {
111
        return min.getY();
112
    }
113

    
114
    /**
115
     * Obtiene la coordenada X m?xima
116
     * @return valor de la coordenada X m?xima
117
     */
118
    public double maxX() {
119
        return max.getX();
120
    }
121

    
122
    /**
123
     * Obtiene la coordenada Y m?xima
124
     * @return valor de la coordenada Y m?xima
125
     */
126
    public double maxY() {
127
        return max.getY();
128
    }
129
    
130
    /**
131
     * Obtiene el punto m?nimo
132
     * @return m?nimo
133
     */
134
    public Point2D getMin() {
135
        return min;
136
    }
137

    
138
    /**
139
     * Obtiene el punto m?ximo
140
     * @return m?ximo
141
     */
142
    public Point2D getMax() {
143
        return max;
144
    }
145

    
146
    public boolean isAt(Point2D pt) {
147
        if (pt.getX() < minX()) {
148
            return false;
149
        }
150

    
151
        if (pt.getX() > maxX()) {
152
            return false;
153
        }
154

    
155
        if (pt.getY() < minY()) {
156
            return false;
157
        }
158

    
159
        if (pt.getY() > maxY()) {
160
            return false;
161
        }
162

    
163
        return true;
164
    }
165

    
166
    public double width() {
167
        return Math.abs(maxX() - minX());
168
    }
169

    
170
    public double height() {
171
        return Math.abs(maxY() - minY());
172
    }
173

    
174
    /**
175
     * Verifica un punto, y modifica el extent si no est? incluido
176
     */
177
    public void add(Point2D pt) {
178
        if (pt == null) {
179
            return;
180
        }
181

    
182
        min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY()));
183
        max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY()));
184
    }
185

    
186
    public void add(Extent ext) {
187
        if (ext == null) {
188
            return;
189
        }
190

    
191
        min.setLocation(Math.min(ext.minX(), minX()),
192
                        Math.min(ext.minY(), minY()));
193
        max.setLocation(Math.max(ext.maxX(), maxX()),
194
                        Math.max(ext.maxY(), maxY()));
195
    }
196

    
197
    /**
198
     * Obtiene la escala
199
     * @param width        Ancho
200
     * @param height        Alto
201
     * @return
202
     */
203
    public double[] getScale(int width, int height) {
204
        return getScale((double) width, (double) height);
205
    }
206

    
207
    public double[] getScale(double width, double height) {
208
        double[] scale = new double[2];
209
        scale[0] = ((float) width) / width();
210
        scale[1] = ((float) height) / height();
211

    
212
        return scale;
213
    }
214

    
215
    public Rectangle2D toRectangle2D() {
216
        return new Rectangle2D.Double(minX(), minY(), width(), height());
217
    }
218

    
219
    public String toString() {
220
        DecimalFormat format = new DecimalFormat("####.000");
221

    
222
        return "Extent: (" + format.format(minX()) + "," +
223
               format.format(minY()) + "), (" + format.format(maxX()) + "," +
224
               format.format(maxY()) + ")";
225
    }
226

    
227
    public interface Has {
228
        public Extent getExtent();
229
    }
230
}