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

History | View | Annotate | Download (12.2 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
import org.gvsig.fmap.geom.Geometry;
32

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

    
49

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

    
56
    /**
57
     *
58
     */
59
    private static final long serialVersionUID = -4218931408628745830L;
60

    
61
    protected Point init;
62
    protected Point end;
63
    protected double ydist;
64

    
65
    /**
66
     * @param type
67
     * @param subtype
68
     */
69
    protected AbstractEllipse(int type, int subtype) {
70
        super(type, subtype);
71
    }
72
    
73
    @Override
74
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
75
        Ellipse2D other = new Ellipse2D();
76
        other.setPoints((Point)init.force2D(), (Point)end.force2D(), ydist);
77
        other.setProjection(this.getProjection());
78
        return other;
79
    }
80

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

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

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

    
102
    @Override
103
    public void addInteriorRing(Line ring) {
104

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

    
109
    }
110

    
111
    @Override
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
    @Override
119
    public void removeInteriorRing(int index) {
120
        String message = "Calling unsupported method removeInteriorRing of a ellipse";
121
        notifyDeprecated(message);
122
        throw new UnsupportedOperationException(message);
123
    }
124

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
229
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
230
        try {
231
            init.reProject(ct);
232
            end.reProject(ct);
233
            aux.reProject(ct);
234
            this.setProjection(ct.getPDest());
235
        } catch (CoordTransRuntimeException e) {
236
            //Si no se ha podido reproyectar alguno de los puntos, les asignamos 0 a todas las coordenadas
237
            init.setX(0);
238
            init.setY(0);
239
            end.setX(0);
240
            end.setY(0);
241
            ydist = 0;
242
        }
243
        try {
244
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(init, end));
245
            ydist = transformedMiddlePoint.distance(aux);
246
        } catch (BaseException e) {
247
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
248
        }
249

    
250
    }
251

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

    
257
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
258
        init.transform(at);
259
        end.transform(at);
260
        aux.transform(at);
261
        try {
262
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(init, end));
263
            ydist = transformedMiddlePoint.distance(aux);
264
        } catch (BaseException e) {
265
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
266
        }
267

    
268
    }
269

    
270
    @Override
271
    public int getDimension() {
272
        return init.getDimension();
273
    }
274

    
275
    @Override
276
    public Shape getShape(AffineTransform affineTransform) {
277
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
278
    }
279

    
280
    @Override
281
    public Shape getShape() {
282
        return getShape(null);
283
    }
284

    
285
    @Override
286
    public PathIterator getPathIterator(AffineTransform at) {
287
        return getPathIterator(at, getManager().getFlatness());
288
    }
289

    
290
    @Override
291
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
292

    
293
        java.awt.geom.Point2D.Double p1 = new java.awt.geom.Point2D.Double(init.getX(), init.getY());
294
        java.awt.geom.Point2D.Double p2 = new java.awt.geom.Point2D.Double(end.getX(), end.getY());
295

    
296
        java.awt.Shape ellipse = UtilFunctions.createEllipse(p1, p2, ydist);
297

    
298
        return ellipse.getPathIterator(at, flatness);
299
    }
300

    
301
    @Override
302
    public GeneralPathX getGeneralPath() {
303

    
304
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
305
         return gp;
306
     }
307

    
308
    @Override
309
    public boolean is3D() {
310
        return ((PointJTS)init).is3D();
311
    }
312

    
313

    
314
    /**
315
     * @param point
316
     * @return
317
     */
318
    protected abstract Point fixPoint(Point point);
319

    
320

    
321
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length) {
322
        init = fixPoint(axis1Start);
323
        end = fixPoint(axis1End);
324
        ydist = axis2Length;
325
    }
326

    
327
    public Point getAxis1Start() {
328
        return init;
329
    }
330

    
331
    public Point getAxis1End() {
332
        return end;
333
    }
334

    
335
    public double getAxis2Dist() {
336
        return ydist;
337
    }
338

    
339
    @Override
340
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
341
        //FIXME: throw UnssupportedOperationException or do nothing?
342
//        String message = "Can't flip a ellipse";
343
//        notifyDeprecated(message);
344
//        throw new UnsupportedOperationException(message);
345
    }
346

    
347
    protected double getAxis1Angle() throws GeometryOperationNotSupportedException, GeometryOperationException {
348
        double angle = Math.acos((end.getX() - init.getX()) / init.distance(end));
349

    
350
        if (init.getY() > end.getY()) {
351
            angle = -angle;
352
        }
353

    
354
        if (angle < 0) {
355
            angle += (2 * Math.PI);
356
        }
357
        return angle;
358
    }
359

    
360

    
361
    @Override
362
    public boolean canBeTransformed(AffineTransform at) {
363
        return false;
364
    }
365

    
366
    @Override
367
    public boolean canBeReprojected(ICoordTrans ct) {
368
        return false;
369
    }
370

    
371
    @Override
372
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
373
        return offset(distance);
374
    }
375
    
376
}