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 @ 44099

History | View | Annotate | Download (14.9 KB)

1
/* 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
import org.cresques.cts.CoordTransRuntimeException;
30
import org.cresques.cts.ICoordTrans;
31

    
32
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
33
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
34
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
35
import org.gvsig.fmap.geom.jts.primitive.surface.AbstractSurface;
36
import org.gvsig.fmap.geom.jts.util.JTSUtils;
37
import org.gvsig.fmap.geom.jts.util.UtilFunctions;
38
import org.gvsig.fmap.geom.operation.GeometryOperationException;
39
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
40
import org.gvsig.fmap.geom.primitive.GeneralPathX;
41
import org.gvsig.fmap.geom.primitive.Line;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.primitive.Polygon;
44
import org.gvsig.fmap.geom.primitive.Ring;
45
import org.gvsig.tools.exception.BaseException;
46

    
47

    
48
/**
49
 * @author fdiaz
50
 *
51
 */
52
public abstract class AbstractEllipse extends AbstractSurface {
53

    
54
    /**
55
     *
56
     */
57
    private static final long serialVersionUID = -4218931408628745830L;
58

    
59
    protected Point init;
60
    protected Point end;
61
    protected double ydist;
62

    
63
    /**
64
     * @param type
65
     * @param subtype
66
     */
67
    protected AbstractEllipse(int type, int subtype) {
68
        super(type, subtype);
69
    }
70

    
71
    /* (non-Javadoc)
72
     * @see org.gvsig.fmap.geom.primitive.Surface#getNumInteriorRings()
73
     */
74
    public int getNumInteriorRings() {
75
        String message = "Calling deprecated method getInteriorRing of a ellipse";
76
        notifyDeprecated(message);
77
        throw new UnsupportedOperationException(message);
78
    }
79

    
80
    /* (non-Javadoc)
81
     * @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
82
     */
83
    public Ring getInteriorRing(int index) {
84
        String message = "Calling deprecated method getInteriorRing of a ellipse";
85
        notifyDeprecated(message);
86
        throw new UnsupportedOperationException(message);
87
    }
88

    
89
    /* (non-Javadoc)
90
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
91
     */
92
    public void addInteriorRing(Ring ring) {
93
        String message = "Calling unsupported method addInteriorRing of a ellipse";
94
        notifyDeprecated(message);
95
        throw new UnsupportedOperationException(message);
96
    }
97

    
98
    /* (non-Javadoc)
99
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
100
     */
101
    public void addInteriorRing(Line ring) {
102

    
103
        String message = "Calling unsupported method addInteriorRing of a ellipse";
104
        notifyDeprecated(message);
105
        throw new UnsupportedOperationException(message);
106

    
107
    }
108

    
109
    /* (non-Javadoc)
110
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Polygon)
111
     */
112
    public void addInteriorRing(Polygon polygon) {
113
        String message = "Calling unsupported method addInteriorRing of a ellipse";
114
        notifyDeprecated(message);
115
        throw new UnsupportedOperationException(message);
116
    }
117

    
118
    /* (non-Javadoc)
119
     * @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
120
     */
121
    public void removeInteriorRing(int index) {
122
        String message = "Calling unsupported method removeInteriorRing of a ellipse";
123
        notifyDeprecated(message);
124
        throw new UnsupportedOperationException(message);
125
    }
126

    
127
    /* (non-Javadoc)
128
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
129
     */
130
    public double getCoordinateAt(int index, int dimension) {
131
        String message = "Calling deprecated method getCoordinateAt of a ellipse";
132
        notifyDeprecated(message);
133
        throw new UnsupportedOperationException(message);
134
    }
135

    
136
    /* (non-Javadoc)
137
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setCoordinateAt(int, int, double)
138
     */
139
    public void setCoordinateAt(int index, int dimension, double value) {
140
        String message = "Calling deprecated method setCoordinateAt of a ellipse";
141
        notifyDeprecated(message);
142
        throw new UnsupportedOperationException(message);
143
    }
144

    
145
    /* (non-Javadoc)
146
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(org.gvsig.fmap.geom.primitive.Point)
147
     */
148
    public void addVertex(Point point) {
149
        String message = "Calling deprecated method addVertex of a ellipse";
150
        notifyDeprecated(message);
151
        throw new UnsupportedOperationException(message);
152
    }
153

    
154
    /* (non-Javadoc)
155
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
156
     */
157
    public void addVertex(double x, double y) {
158
        String message = "Calling deprecated method addVertex of a ellipse";
159
        notifyDeprecated(message);
160
        throw new UnsupportedOperationException(message);
161
    }
162

    
163
    /* (non-Javadoc)
164
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
165
     */
166
    public void addVertex(double x, double y, double z) {
167
        String message = "Calling deprecated method addVertex of a ellipse";
168
        notifyDeprecated(message);
169
        throw new UnsupportedOperationException(message);
170
    }
171

    
172
    /* (non-Javadoc)
173
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#removeVertex(int)
174
     */
175
    public void removeVertex(int index) {
176
        String message = "Calling deprecated method removeVertex of a ellipse";
177
        notifyDeprecated(message);
178
        throw new UnsupportedOperationException(message);
179
    }
180

    
181
    /* (non-Javadoc)
182
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
183
     */
184
    public Point getVertex(int index) {
185
        String message = "Calling deprecated method getVertex of a ellipse";
186
        notifyDeprecated(message);
187
        throw new UnsupportedOperationException(message);
188
    }
189

    
190
    /* (non-Javadoc)
191
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getNumVertices()
192
     */
193
    public int getNumVertices() {
194
        String message = "Calling deprecated method getNumVertices of a ellipse";
195
        notifyDeprecated(message);
196
        throw new UnsupportedOperationException(message);
197
    }
198

    
199
    /* (non-Javadoc)
200
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#insertVertex(int, org.gvsig.fmap.geom.primitive.Point)
201
     */
202
    public void insertVertex(int index, Point p) {
203
        String message = "Calling deprecated method insertVertex of a ellipse";
204
        notifyDeprecated(message);
205
        throw new UnsupportedOperationException(message);
206
    }
207

    
208
    /* (non-Javadoc)
209
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setVertex(int, org.gvsig.fmap.geom.primitive.Point)
210
     */
211
    public void setVertex(int index, Point p) {
212
        String message = "Calling deprecated method setVertex of a ellipse";
213
        notifyDeprecated(message);
214
        throw new UnsupportedOperationException(message);
215
    }
216

    
217
    /* (non-Javadoc)
218
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setGeneralPath(org.gvsig.fmap.geom.primitive.GeneralPathX)
219
     */
220
    public void setGeneralPath(GeneralPathX generalPathX) {
221
        String message = "Calling deprecated method setGeneralPath of a ellipse";
222
        notifyDeprecated(message);
223
        throw new UnsupportedOperationException(message);
224
    }
225

    
226
    /* (non-Javadoc)
227
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addMoveToVertex(org.gvsig.fmap.geom.primitive.Point)
228
     */
229
    public void addMoveToVertex(Point point) {
230
        String message = "Calling deprecated method addMoveToVertex of a ellipse";
231
        notifyDeprecated(message);
232
        throw new UnsupportedOperationException(message);
233
    }
234

    
235
    /* (non-Javadoc)
236
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#closePrimitive()
237
     */
238
    public void closePrimitive() {
239
        String message = "Calling deprecated method closePrimitive of a ellipse";
240
        notifyDeprecated(message);
241
        throw new UnsupportedOperationException(message);
242
    }
243

    
244
    /* (non-Javadoc)
245
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#ensureCapacity(int)
246
     */
247
    public void ensureCapacity(int capacity) {
248
        String message = "Calling deprecated method ensureCapacity of a ellipse";
249
        notifyDeprecated(message);
250
        throw new UnsupportedOperationException(message);
251
    }
252

    
253
    /* (non-Javadoc)
254
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
255
     */
256
    public void reProject(ICoordTrans ct) {
257
        // FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n,
258
        // rotaci?n y escala
259
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
260

    
261
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
262
        try {
263
            init.reProject(ct);
264
            end.reProject(ct);
265
            aux.reProject(ct);
266
            this.setProjection(ct.getPDest());
267
        } catch (CoordTransRuntimeException e) {
268
            //Si no se ha podido reproyectar alguno de los puntos, les asignamos 0 a todas las coordenadas
269
            init.setX(0);
270
            init.setY(0);
271
            end.setX(0);
272
            end.setY(0);
273
            ydist = 0;
274
        }
275
        try {
276
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(init, end));
277
            ydist = transformedMiddlePoint.distance(aux);
278
        } catch (BaseException e) {
279
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
280
        }
281

    
282
    }
283

    
284
    /* (non-Javadoc)
285
     * @see org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
286
     */
287
    public void transform(AffineTransform at) {
288
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
289
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
290

    
291
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
292
        init.transform(at);
293
        end.transform(at);
294
        aux.transform(at);
295
        try {
296
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(init, end));
297
            ydist = transformedMiddlePoint.distance(aux);
298
        } catch (BaseException e) {
299
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
300
        }
301

    
302
    }
303

    
304
    /* (non-Javadoc)
305
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
306
     */
307
    public int getDimension() {
308
        return init.getDimension();
309
    }
310

    
311
    /* (non-Javadoc)
312
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
313
     */
314
    public Shape getShape(AffineTransform affineTransform) {
315
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
316
    }
317

    
318
    /* (non-Javadoc)
319
     * @see org.gvsig.fmap.geom.Geometry#getShape()
320
     */
321
    public Shape getShape() {
322
        return getShape(null);
323
    }
324

    
325
    /* (non-Javadoc)
326
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
327
     */
328
    public PathIterator getPathIterator(AffineTransform at) {
329
        return getPathIterator(at, getManager().getFlatness());
330
    }
331

    
332
    /* (non-Javadoc)
333
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
334
     */
335
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
336

    
337
        java.awt.geom.Point2D.Double p1 = new java.awt.geom.Point2D.Double(init.getX(), init.getY());
338
        java.awt.geom.Point2D.Double p2 = new java.awt.geom.Point2D.Double(end.getX(), end.getY());
339

    
340
        java.awt.Shape ellipse = UtilFunctions.createEllipse(p1, p2, ydist);
341

    
342
        return ellipse.getPathIterator(at, flatness);
343
    }
344

    
345
    /* (non-Javadoc)
346
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
347
     */
348
    public GeneralPathX getGeneralPath() {
349

    
350
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
351
         return gp;
352
     }
353

    
354
    /* (non-Javadoc)
355
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
356
     */
357
    public boolean is3D() {
358
        return ((PointJTS)init).is3D();
359
    }
360

    
361

    
362
    /**
363
     * @param initialPoint
364
     * @return
365
     */
366
    protected abstract Point fixPoint(Point point);
367

    
368

    
369
    /* (non-Javadoc)
370
     * @see org.gvsig.fmap.geom.primitive.Ellipse#setPoints(org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point, double)
371
     */
372
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length) {
373
        init = fixPoint(axis1Start);
374
        end = fixPoint(axis1End);
375
        ydist = axis2Length;
376
    }
377

    
378
    /* (non-Javadoc)
379
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1Start()
380
     */
381
    public Point getAxis1Start() {
382
        return init;
383
    }
384

    
385
    /* (non-Javadoc)
386
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1End()
387
     */
388
    public Point getAxis1End() {
389
        return end;
390
    }
391

    
392
    /* (non-Javadoc)
393
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis2Dist()
394
     */
395
    public double getAxis2Dist() {
396
        return ydist;
397
    }
398

    
399
    /* (non-Javadoc)
400
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
401
     */
402
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
403
        //FIXME: throw UnssupportedOperationException or do nothing?
404
//        String message = "Can't flip a ellipse";
405
//        notifyDeprecated(message);
406
//        throw new UnsupportedOperationException(message);
407
    }
408

    
409
    protected double getAxis1Angle() throws GeometryOperationNotSupportedException, GeometryOperationException {
410
        double angle = Math.acos((end.getX() - init.getX()) / init.distance(end));
411

    
412
        if (init.getY() > end.getY()) {
413
            angle = -angle;
414
        }
415

    
416
        if (angle < 0) {
417
            angle += (2 * Math.PI);
418
        }
419
        return angle;
420
    }
421

    
422

    
423
    @Override
424
    public boolean canBeTransformed(AffineTransform at) {
425
        return false;
426
    }
427

    
428
    @Override
429
    public boolean canBeReprojected(ICoordTrans ct) {
430
        return false;
431
    }
432

    
433
}