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

History | View | Annotate | Download (17.4 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(JTSUtils.getPointAtYAxisInEllipse(axis1Start, axis1End, semiAxis2Length));
268
        try {
269
        axis1Start.reProject(ct);
270
        axis1End.reProject(ct);
271
        aux.reProject(ct);
272
        } catch (CoordTransRuntimeException e) {
273
            //Si ha fallado la reproyecci?n de alguno de los puntos, ponemos todas las coordenadas a 0
274
            axis1Start.setX(0);
275
            axis1Start.setY(0);
276
            axis1End.setX(0);
277
            axis1End.setY(0);
278
            semiAxis2Length = 0;
279
        }
280
        try {
281
            Point2D transformedMiddlePoint = new Point2D(JTSUtils.getMidPoint(axis1Start, axis1End));
282
            semiAxis2Length = transformedMiddlePoint.distance(aux);
283
        } catch (BaseException e) {
284
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
285
        }
286

    
287
    }
288

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

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

    
307
    }
308

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

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

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

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

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

    
342
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
343

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

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

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

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

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

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

    
381

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

    
388

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

    
400
    /* (non-Javadoc)
401
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1Start()
402
     */
403
    public Point getAxis1Start() {
404
        return axis1Start;
405
    }
406

    
407
    /* (non-Javadoc)
408
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis1End()
409
     */
410
    public Point getAxis1End() {
411
        return axis1End;
412
    }
413

    
414
    /* (non-Javadoc)
415
     * @see org.gvsig.fmap.geom.primitive.Ellipse#getAxis2Dist()
416
     */
417
    public double getAxis2Dist() {
418
        return semiAxis2Length;
419
    }
420

    
421
    /*
422
     * (non-Javadoc)
423
     * @see org.gvsig.fmap.geom.primitive.EllipticArc#getAngSt()
424
     */
425
    public double getAngSt(){
426
        return this.angSt;
427
    }
428

    
429
    /*
430
     * (non-Javadoc)
431
     * @see org.gvsig.fmap.geom.primitive.EllipticArc#getAngExt()
432
     */
433
    public double getAngExt(){
434
        return this.angExt;
435
    }
436

    
437
    /* (non-Javadoc)
438
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
439
     */
440
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
441
        //FIXME: throw UnssupportedOperationException or do nothing?
442
//        String message = "Can't flip a ellipse";
443
//        notifyDeprecated(message);
444
//        throw new UnsupportedOperationException(message);
445
    }
446

    
447

    
448
    /* (non-Javadoc)
449
     * @see org.gvsig.fmap.geom.Geometry#offset(double)
450
     */
451
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
452
        EllipticArc cloned = (EllipticArc) this.cloneGeometry();
453

    
454
        Point center = new Point2D((getAxis1Start().getX()+getAxis1End().getX())/2,
455
            (getAxis1Start().getY()+getAxis1End().getY())/2);
456
        double axis1Lenght = getAxis1Start().distance(getAxis1End());
457

    
458
        Point clonedAxis1Start = (Point) getAxis1Start().cloneGeometry();
459
        Point clonedAxis1End = (Point) getAxis1End().cloneGeometry();
460
        double clonedYDist = this.semiAxis2Length+distance;
461

    
462
        clonedAxis1Start.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1Start().getX(), axis1Lenght/2+distance));
463
        clonedAxis1Start.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1Start().getY(), axis1Lenght/2+distance));
464

    
465
        clonedAxis1End.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1End().getX(), axis1Lenght/2+distance));
466
        clonedAxis1End.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1End().getY(), axis1Lenght/2+distance));
467

    
468
        cloned.setPoints(clonedAxis1Start, clonedAxis1End, clonedYDist, this.angSt, this.angExt);
469
        return cloned;
470
    }
471
}