Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / surface / ellipse / AbstractEllipse.java @ 42464

History | View | Annotate | Download (14.7 KB)

1 42268 fdiaz
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
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., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.surface.ellipse;
24
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28
29 42464 fdiaz
import org.cresques.cts.CoordTransRuntimeException;
30 42268 fdiaz
import org.cresques.cts.ICoordTrans;
31
32 42441 fdiaz
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.GeometryManager;
35 42268 fdiaz
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
36 42283 fdiaz
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
37 42268 fdiaz
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
38
import org.gvsig.fmap.geom.jts.primitive.surface.AbstractSurface;
39 42283 fdiaz
import org.gvsig.fmap.geom.jts.util.JTSUtils;
40 42268 fdiaz
import org.gvsig.fmap.geom.jts.util.UtilFunctions;
41 42281 fdiaz
import org.gvsig.fmap.geom.operation.GeometryOperationException;
42
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
43 42441 fdiaz
import org.gvsig.fmap.geom.primitive.Ellipse;
44 42268 fdiaz
import org.gvsig.fmap.geom.primitive.GeneralPathX;
45
import org.gvsig.fmap.geom.primitive.Line;
46
import org.gvsig.fmap.geom.primitive.Point;
47 42283 fdiaz
import org.gvsig.fmap.geom.primitive.Polygon;
48 42268 fdiaz
import org.gvsig.fmap.geom.primitive.Ring;
49 42283 fdiaz
import org.gvsig.tools.exception.BaseException;
50 42268 fdiaz
51
52
/**
53
 * @author fdiaz
54
 *
55
 */
56 42304 fdiaz
public abstract class AbstractEllipse extends AbstractSurface {
57 42268 fdiaz
58
    /**
59
     *
60
     */
61
    private static final long serialVersionUID = -4218931408628745830L;
62
63
    protected Point init;
64
    protected Point end;
65
    protected double ydist;
66
67
    /**
68
     * @param type
69
     * @param subtype
70
     */
71 42304 fdiaz
    protected AbstractEllipse(int type, int subtype) {
72
        super(type, subtype);
73 42268 fdiaz
    }
74
75
    /* (non-Javadoc)
76
     * @see org.gvsig.fmap.geom.primitive.Surface#getNumInteriorRings()
77
     */
78
    public int getNumInteriorRings() {
79 42283 fdiaz
        String message = "Calling deprecated method getInteriorRing of a ellipse";
80
        notifyDeprecated(message);
81
        throw new UnsupportedOperationException(message);
82 42268 fdiaz
    }
83
84
    /* (non-Javadoc)
85
     * @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
86
     */
87
    public Ring getInteriorRing(int index) {
88 42283 fdiaz
        String message = "Calling deprecated method getInteriorRing of a ellipse";
89
        notifyDeprecated(message);
90
        throw new UnsupportedOperationException(message);
91 42268 fdiaz
    }
92
93
    /* (non-Javadoc)
94
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
95
     */
96
    public void addInteriorRing(Ring ring) {
97 42283 fdiaz
        String message = "Calling unsupported method addInteriorRing of a ellipse";
98
        notifyDeprecated(message);
99
        throw new UnsupportedOperationException(message);
100 42268 fdiaz
    }
101
102
    /* (non-Javadoc)
103
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
104
     */
105
    public void addInteriorRing(Line ring) {
106
107 42283 fdiaz
        String message = "Calling unsupported method addInteriorRing of a ellipse";
108
        notifyDeprecated(message);
109
        throw new UnsupportedOperationException(message);
110
111 42268 fdiaz
    }
112
113
    /* (non-Javadoc)
114 42283 fdiaz
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Polygon)
115
     */
116
    public void addInteriorRing(Polygon polygon) {
117
        String message = "Calling unsupported method addInteriorRing of a ellipse";
118
        notifyDeprecated(message);
119
        throw new UnsupportedOperationException(message);
120
    }
121
122
    /* (non-Javadoc)
123 42268 fdiaz
     * @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
124
     */
125
    public void removeInteriorRing(int index) {
126 42283 fdiaz
        String message = "Calling unsupported method removeInteriorRing of a ellipse";
127
        notifyDeprecated(message);
128
        throw new UnsupportedOperationException(message);
129 42268 fdiaz
    }
130
131
    /* (non-Javadoc)
132
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
133
     */
134
    public double getCoordinateAt(int index, int dimension) {
135
        String message = "Calling deprecated method getCoordinateAt of a ellipse";
136
        notifyDeprecated(message);
137
        throw new UnsupportedOperationException(message);
138
    }
139
140
    /* (non-Javadoc)
141
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setCoordinateAt(int, int, double)
142
     */
143
    public void setCoordinateAt(int index, int dimension, double value) {
144
        String message = "Calling deprecated method setCoordinateAt of a ellipse";
145
        notifyDeprecated(message);
146
        throw new UnsupportedOperationException(message);
147
    }
148
149
    /* (non-Javadoc)
150
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(org.gvsig.fmap.geom.primitive.Point)
151
     */
152
    public void addVertex(Point point) {
153
        String message = "Calling deprecated method addVertex of a ellipse";
154
        notifyDeprecated(message);
155
        throw new UnsupportedOperationException(message);
156
    }
157
158
    /* (non-Javadoc)
159
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
160
     */
161
    public void addVertex(double x, double y) {
162
        String message = "Calling deprecated method addVertex of a ellipse";
163
        notifyDeprecated(message);
164
        throw new UnsupportedOperationException(message);
165
    }
166
167
    /* (non-Javadoc)
168
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
169
     */
170
    public void addVertex(double x, double y, double z) {
171
        String message = "Calling deprecated method addVertex of a ellipse";
172
        notifyDeprecated(message);
173
        throw new UnsupportedOperationException(message);
174
    }
175
176
    /* (non-Javadoc)
177
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#removeVertex(int)
178
     */
179
    public void removeVertex(int index) {
180
        String message = "Calling deprecated method removeVertex of a ellipse";
181
        notifyDeprecated(message);
182
        throw new UnsupportedOperationException(message);
183
    }
184
185
    /* (non-Javadoc)
186
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
187
     */
188
    public Point getVertex(int index) {
189
        String message = "Calling deprecated method getVertex of a ellipse";
190
        notifyDeprecated(message);
191
        throw new UnsupportedOperationException(message);
192
    }
193
194
    /* (non-Javadoc)
195
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getNumVertices()
196
     */
197
    public int getNumVertices() {
198
        String message = "Calling deprecated method getNumVertices of a ellipse";
199
        notifyDeprecated(message);
200
        throw new UnsupportedOperationException(message);
201
    }
202
203
    /* (non-Javadoc)
204
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#insertVertex(int, org.gvsig.fmap.geom.primitive.Point)
205
     */
206
    public void insertVertex(int index, Point p) {
207
        String message = "Calling deprecated method insertVertex of a ellipse";
208
        notifyDeprecated(message);
209
        throw new UnsupportedOperationException(message);
210
    }
211
212
    /* (non-Javadoc)
213
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setVertex(int, org.gvsig.fmap.geom.primitive.Point)
214
     */
215
    public void setVertex(int index, Point p) {
216
        String message = "Calling deprecated method setVertex of a ellipse";
217
        notifyDeprecated(message);
218
        throw new UnsupportedOperationException(message);
219
    }
220
221
    /* (non-Javadoc)
222
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setGeneralPath(org.gvsig.fmap.geom.primitive.GeneralPathX)
223
     */
224
    public void setGeneralPath(GeneralPathX generalPathX) {
225
        String message = "Calling deprecated method setGeneralPath of a ellipse";
226
        notifyDeprecated(message);
227
        throw new UnsupportedOperationException(message);
228
    }
229
230
    /* (non-Javadoc)
231
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addMoveToVertex(org.gvsig.fmap.geom.primitive.Point)
232
     */
233
    public void addMoveToVertex(Point point) {
234
        String message = "Calling deprecated method addMoveToVertex of a ellipse";
235
        notifyDeprecated(message);
236
        throw new UnsupportedOperationException(message);
237
    }
238
239
    /* (non-Javadoc)
240
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#closePrimitive()
241
     */
242
    public void closePrimitive() {
243 42283 fdiaz
        String message = "Calling deprecated method closePrimitive of a ellipse";
244
        notifyDeprecated(message);
245
        throw new UnsupportedOperationException(message);
246 42268 fdiaz
    }
247
248
    /* (non-Javadoc)
249
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#ensureCapacity(int)
250
     */
251
    public void ensureCapacity(int capacity) {
252 42283 fdiaz
        String message = "Calling deprecated method ensureCapacity of a ellipse";
253
        notifyDeprecated(message);
254
        throw new UnsupportedOperationException(message);
255 42268 fdiaz
    }
256
257
    /* (non-Javadoc)
258
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
259
     */
260
    public void reProject(ICoordTrans ct) {
261 42464 fdiaz
        // FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n,
262
        // rotaci?n y escala
263 42283 fdiaz
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
264 42268 fdiaz
265 42283 fdiaz
        Point2D aux = new Point2D(JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
266
        try {
267 42464 fdiaz
            init.reProject(ct);
268
            end.reProject(ct);
269
            aux.reProject(ct);
270
        } catch (CoordTransRuntimeException e) {
271
            //Si no se ha podido reproyectar alguno de los puntos, les asignamos 0 a todas las coordenadas
272
            init.setX(0);
273
            init.setY(0);
274
            end.setX(0);
275
            end.setY(0);
276
            ydist = 0;
277
        }
278
        try {
279 42283 fdiaz
            Point2D transformedMiddlePoint = new Point2D(JTSUtils.getMidPoint(init, end));
280
            ydist = transformedMiddlePoint.distance(aux);
281
        } catch (BaseException e) {
282
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
283
        }
284
285 42268 fdiaz
    }
286
287
    /* (non-Javadoc)
288
     * @see org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
289
     */
290
    public void transform(AffineTransform at) {
291 42283 fdiaz
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
292
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
293 42268 fdiaz
294 42283 fdiaz
        Point2D aux = new Point2D(JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
295
        init.transform(at);
296
        end.transform(at);
297
        aux.transform(at);
298
        try {
299
            Point2D transformedMiddlePoint = new Point2D(JTSUtils.getMidPoint(init, end));
300
            ydist = transformedMiddlePoint.distance(aux);
301
        } catch (BaseException e) {
302 42304 fdiaz
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
303 42283 fdiaz
        }
304
305 42268 fdiaz
    }
306
307
    /* (non-Javadoc)
308
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
309
     */
310
    public int getDimension() {
311
        return init.getDimension();
312
    }
313
314
    /* (non-Javadoc)
315
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
316
     */
317
    public Shape getShape(AffineTransform affineTransform) {
318
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
319
    }
320
321
    /* (non-Javadoc)
322
     * @see org.gvsig.fmap.geom.Geometry#getShape()
323
     */
324
    public Shape getShape() {
325
        return getShape(null);
326
    }
327
328
    /* (non-Javadoc)
329
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
330
     */
331
    public PathIterator getPathIterator(AffineTransform at) {
332
        return getPathIterator(at, getManager().getFlatness());
333
    }
334
335
    /* (non-Javadoc)
336
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
337
     */
338
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
339
340
        java.awt.geom.Point2D.Double p1 = new java.awt.geom.Point2D.Double(init.getX(), init.getY());
341
        java.awt.geom.Point2D.Double p2 = new java.awt.geom.Point2D.Double(end.getX(), end.getY());
342
343
        java.awt.Shape ellipse = UtilFunctions.createEllipse(p1, p2, ydist);
344
345
        return ellipse.getPathIterator(at, flatness);
346
    }
347
348
    /* (non-Javadoc)
349
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
350
     */
351
    public GeneralPathX getGeneralPath() {
352
353
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
354
         return gp;
355
     }
356
357
    /* (non-Javadoc)
358
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
359
     */
360
    public boolean is3D() {
361
        return ((PointJTS)init).is3D();
362
    }
363
364
365
    /**
366
     * @param initialPoint
367
     * @return
368
     */
369
    protected abstract Point fixPoint(Point point);
370
371
372
    /* (non-Javadoc)
373
     * @see org.gvsig.fmap.geom.primitive.Ellipse#setPoints(org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point, double)
374
     */
375
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length) {
376
        init = fixPoint(axis1Start);
377
        end = fixPoint(axis1End);
378
        ydist = axis2Length;
379
    }
380
381
    /* (non-Javadoc)
382
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1Start()
383
     */
384
    public Point getAxis1Start() {
385
        return init;
386
    }
387
388
    /* (non-Javadoc)
389
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1End()
390
     */
391
    public Point getAxis1End() {
392
        return end;
393
    }
394
395
    /* (non-Javadoc)
396
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis2Dist()
397
     */
398
    public double getAxis2Dist() {
399
        return ydist;
400
    }
401
402 42281 fdiaz
    /* (non-Javadoc)
403
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
404
     */
405
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
406
        //FIXME: throw UnssupportedOperationException or do nothing?
407
//        String message = "Can't flip a ellipse";
408
//        notifyDeprecated(message);
409
//        throw new UnsupportedOperationException(message);
410
    }
411
412 42385 fdiaz
    protected double getAxis1Angle() throws GeometryOperationNotSupportedException, GeometryOperationException {
413
        double angle = Math.acos((end.getX() - init.getX()) / init.distance(end));
414
415
        if (init.getY() > end.getY()) {
416
            angle = -angle;
417
        }
418
419
        if (angle < 0) {
420
            angle += (2 * Math.PI);
421
        }
422
        return angle;
423
    }
424
425 42268 fdiaz
}