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

History | View | Annotate | Download (14.6 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.OrientablePrimitive;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.fmap.geom.primitive.Polygon;
50
import org.gvsig.fmap.geom.primitive.Ring;
51
import org.gvsig.tools.exception.BaseException;
52

    
53

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

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

    
70

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

    
79
    @Override
80
    public int getNumInteriorRings() {
81
        String message = "Calling deprecated method getInteriorRing of a ellipticArc";
82
        notifyDeprecated(message);
83
        throw new UnsupportedOperationException(message);
84
    }
85

    
86
    @Override
87
    public Ring getInteriorRing(int index) {
88
        String message = "Calling deprecated method getInteriorRing of a ellipticArc";
89
        notifyDeprecated(message);
90
        throw new UnsupportedOperationException(message);
91
    }
92

    
93
    @Override
94
    public void addInteriorRing(Ring ring) {
95
        String message = "Calling unsupported method addInteriorRing of a ellipticArc";
96
        notifyDeprecated(message);
97
        throw new UnsupportedOperationException(message);
98
    }
99

    
100
    @Override
101
    public void addInteriorRing(Line ring) {
102

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

    
107
    }
108

    
109
    @Override
110
    public void addInteriorRing(Polygon polygon) {
111
        String message = "Calling unsupported method addInteriorRing of a ellipticArc";
112
        notifyDeprecated(message);
113
        throw new UnsupportedOperationException(message);
114
    }
115

    
116
    @Override
117
    public void removeInteriorRing(int index) {
118
        String message = "Calling unsupported method removeInteriorRing of a ellipticArc";
119
        notifyDeprecated(message);
120
        throw new UnsupportedOperationException(message);
121
    }
122

    
123
    @Override
124
    public double getCoordinateAt(int index, int dimension) {
125
        String message = "Calling deprecated method getCoordinateAt of a ellipticArc";
126
        notifyDeprecated(message);
127
        throw new UnsupportedOperationException(message);
128
    }
129

    
130
    @Override
131
    public OrientablePrimitive setCoordinateAt(int index, int dimension, double value) {
132
        String message = "Calling deprecated method setCoordinateAt of a ellipticArc";
133
        notifyDeprecated(message);
134
        throw new UnsupportedOperationException(message);
135
    }
136

    
137
    @Override
138
    public OrientablePrimitive addVertex(Point point) {
139
        String message = "Calling deprecated method addVertex of a ellipticArc";
140
        notifyDeprecated(message);
141
        throw new UnsupportedOperationException(message);
142
    }
143

    
144
    @Override
145
    public OrientablePrimitive addVertex(double x, double y) {
146
        String message = "Calling deprecated method addVertex of a ellipticArc";
147
        notifyDeprecated(message);
148
        throw new UnsupportedOperationException(message);
149
    }
150

    
151
    @Override
152
    public OrientablePrimitive addVertex(double x, double y, double z) {
153
        String message = "Calling deprecated method addVertex of a ellipticArc";
154
        notifyDeprecated(message);
155
        throw new UnsupportedOperationException(message);
156
    }
157

    
158
    @Override
159
    public void removeVertex(int index) {
160
        String message = "Calling deprecated method removeVertex of a ellipticArc";
161
        notifyDeprecated(message);
162
        throw new UnsupportedOperationException(message);
163
    }
164

    
165
    @Override
166
    public Point getVertex(int index) {
167
        String message = "Calling deprecated method getVertex of a ellipticArc";
168
        notifyDeprecated(message);
169
        throw new UnsupportedOperationException(message);
170
    }
171

    
172
    @Override
173
    public int getNumVertices() {
174
        String message = "Calling deprecated method getNumVertices of a ellipticArc";
175
        notifyDeprecated(message);
176
        throw new UnsupportedOperationException(message);
177
    }
178

    
179
    @Override
180
    public OrientablePrimitive insertVertex(int index, Point p) {
181
        String message = "Calling deprecated method insertVertex of a ellipticArc";
182
        notifyDeprecated(message);
183
        throw new UnsupportedOperationException(message);
184
    }
185

    
186
    @Override
187
    public OrientablePrimitive setVertex(int index, Point p) {
188
        String message = "Calling deprecated method setVertex of a ellipticArc";
189
        notifyDeprecated(message);
190
        throw new UnsupportedOperationException(message);
191
    }
192

    
193
    @Override
194
    public void setGeneralPath(GeneralPathX generalPathX) {
195
        String message = "Calling deprecated method setGeneralPath of a ellipticArc";
196
        notifyDeprecated(message);
197
        throw new UnsupportedOperationException(message);
198
    }
199

    
200
    @Override
201
    public void addMoveToVertex(Point point) {
202
        String message = "Calling deprecated method addMoveToVertex of a ellipticArc";
203
        notifyDeprecated(message);
204
        throw new UnsupportedOperationException(message);
205
    }
206

    
207
    @Override
208
    public void closePrimitive() {
209
        String message = "Calling deprecated method closePrimitive of a ellipticArc";
210
        notifyDeprecated(message);
211
        throw new UnsupportedOperationException(message);
212
    }
213

    
214
    @Override
215
    public OrientablePrimitive ensureCapacity(int capacity) {
216
        String message = "Calling deprecated method ensureCapacity of a ellipticArc";
217
        notifyDeprecated(message);
218
        throw new UnsupportedOperationException(message);
219
    }
220

    
221
    @Override
222
    public void reProject(ICoordTrans ct) {
223
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
224
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
225

    
226
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(axis1Start, axis1End, semiAxis2Length));
227
        try {
228
        axis1Start.reProject(ct);
229
        axis1End.reProject(ct);
230
        aux.reProject(ct);
231
        this.setProjection(ct.getPDest());
232
        } catch (CoordTransRuntimeException e) {
233
            //Si ha fallado la reproyecci?n de alguno de los puntos, ponemos todas las coordenadas a 0
234
            axis1Start.setX(0);
235
            axis1Start.setY(0);
236
            axis1End.setX(0);
237
            axis1End.setY(0);
238
            semiAxis2Length = 0;
239
        }
240
        try {
241
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(axis1Start, axis1End));
242
            semiAxis2Length = transformedMiddlePoint.distance(aux);
243
        } catch (BaseException e) {
244
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
245
        }
246

    
247
    }
248

    
249
    @Override
250
    public void transform(AffineTransform at) {
251
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
252
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
253

    
254
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(axis1Start, axis1End, semiAxis2Length));
255
        axis1Start.transform(at);
256
        axis1End.transform(at);
257
        aux.transform(at);
258
        try {
259
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(axis1Start, axis1End));
260
            semiAxis2Length = transformedMiddlePoint.distance(aux);
261
        } catch (BaseException e) {
262
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
263
        }
264

    
265
    }
266

    
267
    @Override
268
    public int getDimension() {
269
        return axis1Start.getDimension();
270
    }
271

    
272
    @Override
273
    public Shape getShape(AffineTransform affineTransform) {
274
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
275
    }
276

    
277
    @Override
278
    public Shape getShape() {
279
        return getShape(null);
280
    }
281

    
282
    @Override
283
    public PathIterator getPathIterator(AffineTransform at) {
284
        return getPathIterator(at, getManager().getFlatness());
285
    }
286

    
287
    @Override
288
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
289

    
290
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
291

    
292
        try {
293
            double axis1Lenght = axis1Start.distance(axis1End);
294
            java.awt.geom.Point2D center =
295
                new java.awt.geom.Point2D.Double((axis1Start.getX() + axis1End.getX()) / 2,
296
                    (axis1Start.getY() + axis1End.getY()) / 2);
297
            double x = center.getX() - axis1Lenght / 2;
298
            double y = center.getY() - semiAxis2Length;
299

    
300
            double angle =
301
                UtilFunctions.getAngle(center, new java.awt.geom.Point2D.Double(axis1Start.getX(), axis1Start.getY()));
302

    
303
            Arc2D.Double arc =
304
                new Arc2D.Double(x, y, axis1Lenght, 2 * semiAxis2Length, Math.toDegrees(angSt), Math.toDegrees(angExt),
305
                    Arc2D.CHORD);
306
            AffineTransform mT = AffineTransform.getRotateInstance(angle, center.getX(), center.getY());
307
            return arc.getPathIterator(null, geomManager.getFlatness());
308
        } catch (BaseException e) {
309
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
310
        }
311
    }
312

    
313
    @Override
314
    public GeneralPathX getGeneralPath() {
315

    
316
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
317
         return gp;
318
     }
319

    
320
    @Override
321
    public boolean is3D() {
322
        return ((PointJTS)axis1Start).is3D();
323
    }
324

    
325

    
326
    /**
327
     * @param point
328
     * @return
329
     */
330
    protected abstract Point fixPoint(Point point);
331

    
332

    
333
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length, double angSt, double angExt) {
334
        this.axis1Start = fixPoint(axis1Start);
335
        this.axis1End = fixPoint(axis1End);
336
        this.semiAxis2Length = axis2Length;
337
        this.angSt = angSt;
338
        this.angExt = angExt;
339
    }
340

    
341
    @Override
342
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
343
        EllipticArc2D other = new EllipticArc2D();
344
        Point theAxis1Start = (Point) this.axis1Start.force2D();
345
        Point theAxis1End = (Point) this.axis1End.force2D();
346
        other.setPoints(theAxis1Start, theAxis1End, semiAxis2Length, angSt, angExt);
347
        other.setProjection(this.getProjection());
348
        return other;
349
    }
350

    
351
    public Point getAxis1Start() {
352
        return axis1Start;
353
    }
354

    
355
    public Point getAxis1End() {
356
        return axis1End;
357
    }
358

    
359
    public double getAxis2Dist() {
360
        return semiAxis2Length;
361
    }
362

    
363
    public double getAngSt(){
364
        return this.angSt;
365
    }
366

    
367
    public double getAngExt(){
368
        return this.angExt;
369
    }
370

    
371
    @Override
372
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
373
        //FIXME: throw UnssupportedOperationException or do nothing?
374
//        String message = "Can't flip a ellipse";
375
//        notifyDeprecated(message);
376
//        throw new UnsupportedOperationException(message);
377
    }
378

    
379
    @Override
380
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
381
        EllipticArc cloned = (EllipticArc) this.cloneGeometry();
382

    
383
        Point center = new Point2D((getAxis1Start().getX()+getAxis1End().getX())/2,
384
            (getAxis1Start().getY()+getAxis1End().getY())/2);
385
        double axis1Lenght = getAxis1Start().distance(getAxis1End());
386

    
387
        Point clonedAxis1Start = (Point) getAxis1Start().cloneGeometry();
388
        Point clonedAxis1End = (Point) getAxis1End().cloneGeometry();
389
        double clonedYDist = this.semiAxis2Length+distance;
390

    
391
        clonedAxis1Start.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1Start().getX(), axis1Lenght/2+distance));
392
        clonedAxis1Start.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1Start().getY(), axis1Lenght/2+distance));
393

    
394
        clonedAxis1End.setX(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getX(), getAxis1End().getX(), axis1Lenght/2+distance));
395
        clonedAxis1End.setY(JTSUtils.straightLineThroughTwoPointsEquation(0, axis1Lenght/2, center.getY(), getAxis1End().getY(), axis1Lenght/2+distance));
396

    
397
        cloned.setPoints(clonedAxis1Start, clonedAxis1End, clonedYDist, this.angSt, this.angExt);
398
        return cloned;
399
    }
400

    
401

    
402
    @Override
403
    public boolean canBeTransformed(AffineTransform at) {
404
        return false;
405
    }
406

    
407
    @Override
408
    public boolean canBeReprojected(ICoordTrans ct) {
409
        return false;
410
    }
411

    
412
    
413
}