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

History | View | Annotate | Download (13.3 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
import org.cresques.cts.CoordTransRuntimeException;
29
import org.cresques.cts.ICoordTrans;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
32
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
33
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
34
import org.gvsig.fmap.geom.jts.primitive.surface.AbstractSurface;
35
import org.gvsig.fmap.geom.jts.util.JTSUtils;
36
import org.gvsig.fmap.geom.jts.util.UtilFunctions;
37
import org.gvsig.fmap.geom.operation.GeometryOperationException;
38
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
39
import org.gvsig.fmap.geom.primitive.GeneralPathX;
40
import org.gvsig.fmap.geom.primitive.Line;
41
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
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
    @Override
72
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
73
        Ellipse2D other = new Ellipse2D();
74
        other.setPoints((Point)init.force2D(), (Point)end.force2D(), ydist);
75
        other.setProjection(this.getProjection());
76
        return other;
77
    }
78
    
79
    @Override
80
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
81
        Ellipse2DM other = new Ellipse2DM();
82
        other.setProjection(this.getProjection());
83
        other.setPoints((Point)init.force2DM(), (Point)end.force2DM(), ydist);
84
        return other;
85
    }
86
    
87
    @Override
88
    public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException {
89
        Ellipse2DZ other = new Ellipse2DZ();
90
        other.setProjection(this.getProjection());
91
        other.setPoints((Point)init.force3D(), (Point)end.force3D(), ydist);
92
        return other;
93
    }
94

    
95
    @Override
96
    public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
97
        Ellipse2DZM other = new Ellipse2DZM();
98
        other.setProjection(this.getProjection());
99
        other.setPoints((Point)init.force3DM(), (Point)end.force3DM(), ydist);
100
        return other;
101
    }
102

    
103
    
104

    
105
    @Override
106
    public int getNumInteriorRings() {
107
        String message = "Calling deprecated method getInteriorRing of a ellipse";
108
        notifyDeprecated(message);
109
        throw new UnsupportedOperationException(message);
110
    }
111

    
112
    @Override
113
    public Ring getInteriorRing(int index) {
114
        String message = "Calling deprecated method getInteriorRing of a ellipse";
115
        notifyDeprecated(message);
116
        throw new UnsupportedOperationException(message);
117
    }
118

    
119
    @Override
120
    public void addInteriorRing(Ring ring) {
121
        String message = "Calling unsupported method addInteriorRing of a ellipse";
122
        notifyDeprecated(message);
123
        throw new UnsupportedOperationException(message);
124
    }
125

    
126
    @Override
127
    public void addInteriorRing(Line ring) {
128

    
129
        String message = "Calling unsupported method addInteriorRing of a ellipse";
130
        notifyDeprecated(message);
131
        throw new UnsupportedOperationException(message);
132

    
133
    }
134

    
135
    @Override
136
    public void addInteriorRing(Polygon polygon) {
137
        String message = "Calling unsupported method addInteriorRing of a ellipse";
138
        notifyDeprecated(message);
139
        throw new UnsupportedOperationException(message);
140
    }
141

    
142
    @Override
143
    public void removeInteriorRing(int index) {
144
        String message = "Calling unsupported method removeInteriorRing of a ellipse";
145
        notifyDeprecated(message);
146
        throw new UnsupportedOperationException(message);
147
    }
148

    
149
    @Override
150
    public double getCoordinateAt(int index, int dimension) {
151
        String message = "Calling deprecated method getCoordinateAt of a ellipse";
152
        notifyDeprecated(message);
153
        throw new UnsupportedOperationException(message);
154
    }
155

    
156
    @Override
157
    public OrientablePrimitive setCoordinateAt(int index, int dimension, double value) {
158
        String message = "Calling deprecated method setCoordinateAt of a ellipse";
159
        notifyDeprecated(message);
160
        throw new UnsupportedOperationException(message);
161
    }
162

    
163
    @Override
164
    public OrientablePrimitive addVertex(Point point) {
165
        String message = "Calling deprecated method addVertex of a ellipse";
166
        notifyDeprecated(message);
167
        throw new UnsupportedOperationException(message);
168
    }
169

    
170
    @Override
171
    public OrientablePrimitive addVertex(double x, double y) {
172
        String message = "Calling deprecated method addVertex of a ellipse";
173
        notifyDeprecated(message);
174
        throw new UnsupportedOperationException(message);
175
    }
176

    
177
    @Override
178
    public OrientablePrimitive addVertex(double x, double y, double z) {
179
        String message = "Calling deprecated method addVertex of a ellipse";
180
        notifyDeprecated(message);
181
        throw new UnsupportedOperationException(message);
182
    }
183

    
184
    @Override
185
    public void removeVertex(int index) {
186
        String message = "Calling deprecated method removeVertex of a ellipse";
187
        notifyDeprecated(message);
188
        throw new UnsupportedOperationException(message);
189
    }
190

    
191
    @Override
192
    public Point getVertex(int index) {
193
        String message = "Calling deprecated method getVertex of a ellipse";
194
        notifyDeprecated(message);
195
        throw new UnsupportedOperationException(message);
196
    }
197

    
198
    @Override
199
    public int getNumVertices() {
200
        String message = "Calling deprecated method getNumVertices of a ellipse";
201
        notifyDeprecated(message);
202
        throw new UnsupportedOperationException(message);
203
    }
204

    
205
    @Override
206
    public OrientablePrimitive insertVertex(int index, Point p) {
207
        String message = "Calling deprecated method insertVertex of a ellipse";
208
        notifyDeprecated(message);
209
        throw new UnsupportedOperationException(message);
210
    }
211

    
212
    @Override
213
    public OrientablePrimitive setVertex(int index, Point p) {
214
        String message = "Calling deprecated method setVertex of a ellipse";
215
        notifyDeprecated(message);
216
        throw new UnsupportedOperationException(message);
217
    }
218

    
219
    @Override
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
    @Override
227
    public void addMoveToVertex(Point point) {
228
        String message = "Calling deprecated method addMoveToVertex of a ellipse";
229
        notifyDeprecated(message);
230
        throw new UnsupportedOperationException(message);
231
    }
232

    
233
    @Override
234
    public void closePrimitive() {
235
        String message = "Calling deprecated method closePrimitive of a ellipse";
236
        notifyDeprecated(message);
237
        throw new UnsupportedOperationException(message);
238
    }
239

    
240
    @Override
241
    public OrientablePrimitive ensureCapacity(int capacity) {
242
        String message = "Calling deprecated method ensureCapacity of a ellipse";
243
        notifyDeprecated(message);
244
        throw new UnsupportedOperationException(message);
245
    }
246

    
247
    @Override
248
    public void reProject(ICoordTrans ct) {
249
        // FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n,
250
        // rotaci?n y escala
251
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
252

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

    
274
    }
275

    
276
    @Override
277
    public void transform(AffineTransform at) {
278
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
279
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
280

    
281
        Point2D aux = new Point2D(this.getProjection(), JTSUtils.getPointAtYAxisInEllipse(init, end, ydist));
282
        init.transform(at);
283
        end.transform(at);
284
        aux.transform(at);
285
        try {
286
            Point2D transformedMiddlePoint = new Point2D(this.getProjection(), JTSUtils.getMidPoint(init, end));
287
            ydist = transformedMiddlePoint.distance(aux);
288
        } catch (BaseException e) {
289
            throw new UnsupportedOperationException("Error calculating the minor semi-axis of the transformed ellipse.", e);
290
        }
291

    
292
    }
293

    
294
    @Override
295
    public int getDimension() {
296
        return init.getDimension();
297
    }
298

    
299
    @Override
300
    public Shape getShape(AffineTransform affineTransform) {
301
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
302
    }
303

    
304
    @Override
305
    public Shape getShape() {
306
        return getShape(null);
307
    }
308

    
309
    @Override
310
    public PathIterator getPathIterator(AffineTransform at) {
311
        return getPathIterator(at, getManager().getFlatness());
312
    }
313

    
314
    @Override
315
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
316

    
317
        java.awt.geom.Point2D.Double p1 = new java.awt.geom.Point2D.Double(init.getX(), init.getY());
318
        java.awt.geom.Point2D.Double p2 = new java.awt.geom.Point2D.Double(end.getX(), end.getY());
319

    
320
        java.awt.Shape ellipse = UtilFunctions.createEllipse(p1, p2, ydist);
321

    
322
        return ellipse.getPathIterator(at, flatness);
323
    }
324

    
325
    @Override
326
    public GeneralPathX getGeneralPath() {
327

    
328
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
329
         return gp;
330
     }
331

    
332
    @Override
333
    public boolean is3D() {
334
        return ((PointJTS)init).is3D();
335
    }
336

    
337

    
338
    /**
339
     * @param point
340
     * @return
341
     */
342
    protected abstract Point fixPoint(Point point);
343

    
344

    
345
    public void setPoints(Point axis1Start, Point axis1End, double axis2Length) {
346
        init = fixPoint(axis1Start);
347
        end = fixPoint(axis1End);
348
        ydist = axis2Length;
349
    }
350

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

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

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

    
363
    @Override
364
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
365
        //FIXME: throw UnssupportedOperationException or do nothing?
366
//        String message = "Can't flip a ellipse";
367
//        notifyDeprecated(message);
368
//        throw new UnsupportedOperationException(message);
369
    }
370

    
371
    protected double getAxis1Angle() throws GeometryOperationNotSupportedException, GeometryOperationException {
372
        double angle = Math.acos((end.getX() - init.getX()) / init.distance(end));
373

    
374
        if (init.getY() > end.getY()) {
375
            angle = -angle;
376
        }
377

    
378
        if (angle < 0) {
379
            angle += (2 * Math.PI);
380
        }
381
        return angle;
382
    }
383

    
384

    
385
    @Override
386
    public boolean canBeTransformed(AffineTransform at) {
387
        return false;
388
    }
389

    
390
    @Override
391
    public boolean canBeReprojected(ICoordTrans ct) {
392
        return false;
393
    }
394

    
395
    @Override
396
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
397
        return offset(distance);
398
    }
399

    
400

    
401
    @Override
402
    public boolean isEmpty() {
403
        return this.getNumVertices()<1;
404
    }
405

    
406
}