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 / ellipticarc / AbstractEllipticArc.java @ 44612

History | View | Annotate | Download (18.1 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.ellipticarc;
24

    
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.Arc2D;
28
import java.awt.geom.PathIterator;
29

    
30
import org.cresques.cts.CoordTransRuntimeException;
31
import org.cresques.cts.ICoordTrans;
32

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

    
52

    
53
/**
54
 * @author fdiaz
55
 *
56
 */
57
public abstract class AbstractEllipticArc extends AbstractSurface {
58

    
59
    /**
60
     *
61
     */
62
    private static final long serialVersionUID = 6163269454714321454L;
63
    protected Point axis1Start;
64
    protected Point axis1End;
65
    protected double semiAxis2Length;
66
    protected double angSt;
67
    protected double angExt;
68

    
69

    
70
    /**
71
     * @param type
72
     * @param subtype
73
     */
74
    protected AbstractEllipticArc(int type, int subtype) {
75
        super(type, subtype);
76
    }
77

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

    
87
    /* (non-Javadoc)
88
     * @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
89
     */
90
    public Ring getInteriorRing(int index) {
91
        String message = "Calling deprecated method getInteriorRing of a ellipticArc";
92
        notifyDeprecated(message);
93
        throw new UnsupportedOperationException(message);
94
    }
95

    
96
    /* (non-Javadoc)
97
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
98
     */
99
    public void addInteriorRing(Ring ring) {
100
        String message = "Calling unsupported method addInteriorRing of a ellipticArc";
101
        notifyDeprecated(message);
102
        throw new UnsupportedOperationException(message);
103
    }
104

    
105
    /* (non-Javadoc)
106
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
107
     */
108
    public void addInteriorRing(Line ring) {
109

    
110
        String message = "Calling unsupported method addInteriorRing of a ellipticArc";
111
        notifyDeprecated(message);
112
        throw new UnsupportedOperationException(message);
113

    
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Polygon)
118
     */
119
    public void addInteriorRing(Polygon polygon) {
120
        String message = "Calling unsupported method addInteriorRing of a ellipticArc";
121
        notifyDeprecated(message);
122
        throw new UnsupportedOperationException(message);
123
    }
124

    
125
    /* (non-Javadoc)
126
     * @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
127
     */
128
    public void removeInteriorRing(int index) {
129
        String message = "Calling unsupported method removeInteriorRing of a ellipticArc";
130
        notifyDeprecated(message);
131
        throw new UnsupportedOperationException(message);
132
    }
133

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

    
143
    /* (non-Javadoc)
144
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setCoordinateAt(int, int, double)
145
     */
146
    public void setCoordinateAt(int index, int dimension, double value) {
147
        String message = "Calling deprecated method setCoordinateAt of a ellipticArc";
148
        notifyDeprecated(message);
149
        throw new UnsupportedOperationException(message);
150
    }
151

    
152
    /* (non-Javadoc)
153
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(org.gvsig.fmap.geom.primitive.Point)
154
     */
155
    public void addVertex(Point point) {
156
        String message = "Calling deprecated method addVertex of a ellipticArc";
157
        notifyDeprecated(message);
158
        throw new UnsupportedOperationException(message);
159
    }
160

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

    
170
    /* (non-Javadoc)
171
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
172
     */
173
    public void addVertex(double x, double y, double z) {
174
        String message = "Calling deprecated method addVertex of a ellipticArc";
175
        notifyDeprecated(message);
176
        throw new UnsupportedOperationException(message);
177
    }
178

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

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

    
197
    /* (non-Javadoc)
198
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getNumVertices()
199
     */
200
    public int getNumVertices() {
201
        String message = "Calling deprecated method getNumVertices of a ellipticArc";
202
        notifyDeprecated(message);
203
        throw new UnsupportedOperationException(message);
204
    }
205

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

    
215
    /* (non-Javadoc)
216
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setVertex(int, org.gvsig.fmap.geom.primitive.Point)
217
     */
218
    public void setVertex(int index, Point p) {
219
        String message = "Calling deprecated method setVertex of a ellipticArc";
220
        notifyDeprecated(message);
221
        throw new UnsupportedOperationException(message);
222
    }
223

    
224
    /* (non-Javadoc)
225
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setGeneralPath(org.gvsig.fmap.geom.primitive.GeneralPathX)
226
     */
227
    public void setGeneralPath(GeneralPathX generalPathX) {
228
        String message = "Calling deprecated method setGeneralPath of a ellipticArc";
229
        notifyDeprecated(message);
230
        throw new UnsupportedOperationException(message);
231
    }
232

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

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

    
251
    /* (non-Javadoc)
252
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#ensureCapacity(int)
253
     */
254
    public void ensureCapacity(int capacity) {
255
        String message = "Calling deprecated method ensureCapacity of a ellipticArc";
256
        notifyDeprecated(message);
257
        throw new UnsupportedOperationException(message);
258
    }
259

    
260
    /* (non-Javadoc)
261
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
262
     */
263
    public void reProject(ICoordTrans ct) {
264
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
265
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
266

    
267
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(axis1Start, axis1End, semiAxis2Length));
268
        try {
269
        axis1Start.reProject(ct);
270
        axis1End.reProject(ct);
271
        aux.reProject(ct);
272
        this.setProjection(ct.getPDest());
273
        } catch (CoordTransRuntimeException e) {
274
            //Si ha fallado la reproyecci?n de alguno de los puntos, ponemos todas las coordenadas a 0
275
            axis1Start.setX(0);
276
            axis1Start.setY(0);
277
            axis1End.setX(0);
278
            axis1End.setY(0);
279
            semiAxis2Length = 0;
280
        }
281
        try {
282
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(axis1Start, axis1End));
283
            semiAxis2Length = transformedMiddlePoint.distance(aux);
284
        } catch (BaseException e) {
285
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
286
        }
287

    
288
    }
289

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

    
297
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(axis1Start, axis1End, semiAxis2Length));
298
        axis1Start.transform(at);
299
        axis1End.transform(at);
300
        aux.transform(at);
301
        try {
302
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(axis1Start, axis1End));
303
            semiAxis2Length = transformedMiddlePoint.distance(aux);
304
        } catch (BaseException e) {
305
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
306
        }
307

    
308
    }
309

    
310
    /* (non-Javadoc)
311
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
312
     */
313
    public int getDimension() {
314
        return axis1Start.getDimension();
315
    }
316

    
317
    /* (non-Javadoc)
318
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
319
     */
320
    public Shape getShape(AffineTransform affineTransform) {
321
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
322
    }
323

    
324
    /* (non-Javadoc)
325
     * @see org.gvsig.fmap.geom.Geometry#getShape()
326
     */
327
    public Shape getShape() {
328
        return getShape(null);
329
    }
330

    
331
    /* (non-Javadoc)
332
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
333
     */
334
    public PathIterator getPathIterator(AffineTransform at) {
335
        return getPathIterator(at, getManager().getFlatness());
336
    }
337

    
338
    /* (non-Javadoc)
339
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
340
     */
341
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
342

    
343
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
344

    
345
        try {
346
            double axis1Lenght = axis1Start.distance(axis1End);
347
            java.awt.geom.Point2D center =
348
                new java.awt.geom.Point2D.Double((axis1Start.getX() + axis1End.getX()) / 2,
349
                    (axis1Start.getY() + axis1End.getY()) / 2);
350
            double x = center.getX() - axis1Lenght / 2;
351
            double y = center.getY() - semiAxis2Length;
352

    
353
            double angle =
354
                UtilFunctions.getAngle(center, new java.awt.geom.Point2D.Double(axis1Start.getX(), axis1Start.getY()));
355

    
356
            Arc2D.Double arc =
357
                new Arc2D.Double(x, y, axis1Lenght, 2 * semiAxis2Length, Math.toDegrees(angSt), Math.toDegrees(angExt),
358
                    Arc2D.CHORD);
359
            AffineTransform mT = AffineTransform.getRotateInstance(angle, center.getX(), center.getY());
360
            return arc.getPathIterator(null, geomManager.getFlatness());
361
        } catch (BaseException e) {
362
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
363
        }
364
    }
365

    
366
    /* (non-Javadoc)
367
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
368
     */
369
    public GeneralPathX getGeneralPath() {
370

    
371
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
372
         return gp;
373
     }
374

    
375
    /* (non-Javadoc)
376
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
377
     */
378
    public boolean is3D() {
379
        return ((PointJTS)axis1Start).is3D();
380
    }
381

    
382

    
383
    /**
384
     * @param initialPoint
385
     * @return
386
     */
387
    protected abstract Point fixPoint(Point point);
388

    
389

    
390
    /* (non-Javadoc)
391
     * @see org.gvsig.fmap.geom.primitive.Ellipse#setPoints(org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point, double)
392
     */
393
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length, double angSt, double angExt) {
394
        this.axis1Start = fixPoint(axis1Start);
395
        this.axis1End = fixPoint(axis1End);
396
        this.semiAxis2Length = axis2Length;
397
        this.angSt = angSt;
398
        this.angExt = angExt;
399
    }
400

    
401
    @Override
402
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
403
        EllipticArc2D other = new EllipticArc2D();
404
        Point axis1Start = (Point) this.axis1Start.force2D();
405
        Point axis1End = (Point) this.axis1End.force2D();
406
        other.setPoints(axis1Start, axis1End, semiAxis2Length, angSt, angExt);
407
        other.setProjection(this.getProjection());
408
        return other;
409
    }
410

    
411

    
412
    /* (non-Javadoc)
413
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1Start()
414
     */
415
    public Point getAxis1Start() {
416
        return axis1Start;
417
    }
418

    
419
    /* (non-Javadoc)
420
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1End()
421
     */
422
    public Point getAxis1End() {
423
        return axis1End;
424
    }
425

    
426
    /* (non-Javadoc)
427
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis2Dist()
428
     */
429
    public double getAxis2Dist() {
430
        return semiAxis2Length;
431
    }
432

    
433
    /*
434
     * (non-Javadoc)
435
     * @see org.gvsig.fmap.geom.primitive.EllipticArc#getAngSt()
436
     */
437
    public double getAngSt(){
438
        return this.angSt;
439
    }
440

    
441
    /*
442
     * (non-Javadoc)
443
     * @see org.gvsig.fmap.geom.primitive.EllipticArc#getAngExt()
444
     */
445
    public double getAngExt(){
446
        return this.angExt;
447
    }
448

    
449
    /* (non-Javadoc)
450
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
451
     */
452
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
453
        //FIXME: throw UnssupportedOperationException or do nothing?
454
//        String message = "Can't flip a ellipse";
455
//        notifyDeprecated(message);
456
//        throw new UnsupportedOperationException(message);
457
    }
458

    
459

    
460
    /* (non-Javadoc)
461
     * @see org.gvsig.fmap.geom.Geometry#offset(double)
462
     */
463
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
464
        EllipticArc cloned = (EllipticArc) this.cloneGeometry();
465

    
466
        Point center = new Point2D((getAxis1Start().getX()+getAxis1End().getX())/2,
467
            (getAxis1Start().getY()+getAxis1End().getY())/2);
468
        double axis1Lenght = getAxis1Start().distance(getAxis1End());
469

    
470
        Point clonedAxis1Start = (Point) getAxis1Start().cloneGeometry();
471
        Point clonedAxis1End = (Point) getAxis1End().cloneGeometry();
472
        double clonedYDist = this.semiAxis2Length+distance;
473

    
474
        clonedAxis1Start.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1Start().getX(), axis1Lenght/2+distance));
475
        clonedAxis1Start.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1Start().getY(), axis1Lenght/2+distance));
476

    
477
        clonedAxis1End.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1End().getX(), axis1Lenght/2+distance));
478
        clonedAxis1End.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1End().getY(), axis1Lenght/2+distance));
479

    
480
        cloned.setPoints(clonedAxis1Start, clonedAxis1End, clonedYDist, this.angSt, this.angExt);
481
        return cloned;
482
    }
483

    
484

    
485
    @Override
486
    public boolean canBeTransformed(AffineTransform at) {
487
        return false;
488
    }
489

    
490
    @Override
491
    public boolean canBeReprojected(ICoordTrans ct) {
492
        return false;
493
    }
494

    
495
    
496
}