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 / circle / AbstractCircle.java @ 47432

History | View | Annotate | Download (12.5 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.circle;
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.UtilFunctions;
38
import org.gvsig.fmap.geom.operation.GeometryOperationException;
39
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
40
import org.gvsig.fmap.geom.primitive.GeneralPathX;
41
import org.gvsig.fmap.geom.primitive.Line;
42
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
43
import org.gvsig.fmap.geom.primitive.Point;
44
import org.gvsig.fmap.geom.primitive.Polygon;
45
import org.gvsig.fmap.geom.primitive.Ring;
46
import org.gvsig.tools.exception.BaseException;
47

    
48

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

    
55
    /**
56
     *
57
     */
58
    private static final long serialVersionUID = -5509291843865895995L;
59

    
60
    protected Point center;
61
    protected double radius;
62

    
63
    /**
64
     * @param type
65
     * @param subtype
66
     */
67
    public AbstractCircle(int type, int subtype) {
68
        super(type, subtype);
69
    }
70

    
71
    /**
72
     * @param type
73
     * @param subtype
74
     */
75
    protected AbstractCircle(int type, int subtype, Point center, double radius) {
76
        this(type, subtype);
77
        this.setCenter(center);
78
        this.setRadius(radius);
79
    }
80

    
81
    @Override
82
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
83
        Circle2D other = new Circle2D((Point) center.force2D(), radius);
84
        other.setProjection(this.getProjection());
85
        return other;
86
    }
87

    
88
    @Override
89
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
90
        Circle2DM other = new Circle2DM((Point) center.force2DM(), radius);
91
        other.setProjection(this.getProjection());
92
        return other;
93
    }
94
    
95
    @Override
96
    public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException {
97
        Circle2DZ other = new Circle2DZ((Point) center.force3D(), radius);
98
        other.setProjection(this.getProjection());
99
        return other;
100
    }
101

    
102
    @Override
103
    public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
104
        Circle2DZM other = new Circle2DZM((Point) center.force3DM(), radius);
105
        other.setProjection(this.getProjection());
106
        return other;
107
    }
108
    
109
    
110
    /**
111
     * @param point
112
     * @return
113
     */
114
    protected abstract Point fixPoint(Point point);
115

    
116
    /**
117
     * @param center the center to set
118
     */
119
    public void setCenter(Point center) {
120
        this.center = fixPoint(center);
121
    }
122

    
123
    public Point getCenter() {
124
        return this.center;
125
    }
126

    
127
    /**
128
     * @param radius the radius to set
129
     */
130
    public void setRadius(double radius) {
131
        this.radius = radius;
132
    }
133

    
134
    public double getRadious() {
135
        return this.radius;
136
    }
137

    
138
    @Override
139
    public int getDimension() {
140
        return this.center.getDimension();
141
    }
142

    
143
    @Override
144
    public boolean isSimple() {
145
        return true;
146
    }
147

    
148
    public void setPoints(Point center, Point radius) {
149
        setCenter(center);
150
        this.radius = ((PointJTS)radius).getJTSCoordinate().distance(((PointJTS)this.center).getJTSCoordinate());
151
    }
152

    
153
    public void setPoints(Point center, double radius) {
154
        setCenter(center);
155
        this.radius = radius;
156
    }
157

    
158
    @Override
159
    public boolean is3D() {
160
        return ((PointJTS)center).is3D();
161
    }
162

    
163
    @Override
164
    public double getCoordinateAt(int index, int dimension) {
165
        String message = "Calling deprecated method setPoints of a circle";
166
        notifyDeprecated(message);
167
        throw new UnsupportedOperationException(message);
168
    }
169

    
170
    @Override
171
    public OrientablePrimitive setCoordinateAt(int index, int dimension, double value) {
172
        String message = "Calling deprecated method setPoints of a circle";
173
        notifyDeprecated(message);
174
        throw new UnsupportedOperationException(message);
175
    }
176

    
177
    @Override
178
    public OrientablePrimitive addVertex(Point point) {
179
        String message = "Calling deprecated method setPoints of a circle";
180
        notifyDeprecated(message);
181
        throw new UnsupportedOperationException(message);
182
    }
183

    
184
    @Override
185
    public OrientablePrimitive addVertex(double x, double y) {
186
        String message = "Calling deprecated method setPoints of a circle";
187
        notifyDeprecated(message);
188
        throw new UnsupportedOperationException(message);
189
    }
190

    
191
    @Override
192
    public OrientablePrimitive addVertex(double x, double y, double z) {
193
        String message = "Calling deprecated method setPoints of a circle";
194
        notifyDeprecated(message);
195
        throw new UnsupportedOperationException(message);
196
    }
197

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

    
205
    @Override
206
    public Point getVertex(int index) {
207
        String message = "Calling deprecated method setPoints of a circle";
208
        notifyDeprecated(message);
209
        throw new UnsupportedOperationException(message);
210
    }
211

    
212
    @Override
213
    public int getNumVertices() {
214
        String message = "Calling deprecated method getNumVertices of a circle";
215
        notifyDeprecated(message);
216
        throw new UnsupportedOperationException(message);
217
    }
218

    
219
    @Override
220
    public OrientablePrimitive insertVertex(int index, Point p) {
221
        String message = "Calling deprecated method setPoints of a circle";
222
        notifyDeprecated(message);
223
        throw new UnsupportedOperationException(message);
224
    }
225

    
226
    @Override
227
    public OrientablePrimitive setVertex(int index, Point p) {
228
        String message = "Calling deprecated method setPoints of a circle";
229
        notifyDeprecated(message);
230
        throw new UnsupportedOperationException(message);
231
    }
232

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

    
240
    @Override
241
    public void addMoveToVertex(Point point) {
242
        String message = "Calling deprecated method setPoints of a circle";
243
        notifyDeprecated(message);
244
        throw new UnsupportedOperationException(message);
245
    }
246

    
247
    @Override
248
    public void closePrimitive() {
249
        String message = "Calling deprecated method setPoints of a circle";
250
        notifyDeprecated(message);
251
        throw new UnsupportedOperationException(message);
252
    }
253

    
254
    @Override
255
    public OrientablePrimitive ensureCapacity(int capacity) {
256
        String message = "Calling deprecated method ensureCapacity of a circle";
257
        notifyDeprecated(message);
258
        throw new UnsupportedOperationException(message);
259
    }
260

    
261
    @Override
262
    public Shape getShape(AffineTransform affineTransform) {
263
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
264
    }
265

    
266
    @Override
267
    public Shape getShape() {
268
        return getShape(null);
269
    }
270

    
271
    @Override
272
    public PathIterator getPathIterator(AffineTransform at) {
273
        return this.getPathIterator(at, getManager().getFlatness());
274
    }
275

    
276
    @Override
277
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
278

    
279
        java.awt.geom.Point2D.Double center = new java.awt.geom.Point2D.Double(this.center.getX(), this.center.getY());
280
        java.awt.geom.Arc2D arco = UtilFunctions.createCircle(center,  radius);
281

    
282
        return arco.getPathIterator(at, flatness);
283
    }
284

    
285
    @Override
286
    public GeneralPathX getGeneralPath() {
287
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
288
         return gp;
289
     }
290

    
291
    @Override
292
    public int getNumInteriorRings() {
293
        String message = "Calling deprecated method getNumInteriorRings of a circle";
294
        notifyDeprecated(message);
295
        throw new UnsupportedOperationException(message);
296
    }
297

    
298
    @Override
299
    public Ring getInteriorRing(int index) {
300
        String message = "Calling deprecated method getInteriorRing of a circle";
301
        notifyDeprecated(message);
302
        throw new UnsupportedOperationException(message);
303
    }
304

    
305
    @Override
306
    public void addInteriorRing(Ring ring) {
307
        String message = "Calling deprecated method addInteriorRing of a circle";
308
        notifyDeprecated(message);
309
        throw new UnsupportedOperationException(message);
310
    }
311

    
312
    @Override
313
    public void addInteriorRing(Line ring) {
314
        String message = "Calling deprecated method addInteriorRing of a circle";
315
        notifyDeprecated(message);
316
        throw new UnsupportedOperationException(message);
317
    }
318

    
319
    @Override
320
    public void addInteriorRing(Polygon polygon) {
321
        String message = "Calling unsupported method addInteriorRing of a circle";
322
        notifyDeprecated(message);
323
        throw new UnsupportedOperationException(message);
324
    }
325

    
326
    @Override
327
    public void removeInteriorRing(int index) {
328
        String message = "Calling deprecated method removeInteriorRing of a circle";
329
        notifyDeprecated(message);
330
        throw new UnsupportedOperationException(message);
331
    }
332

    
333
    @Override
334
    public void reProject(ICoordTrans ct) {
335
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
336
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
337

    
338
        Point2D aux = new Point2D(center.getX(), center.getY()-radius);
339
        try {
340
            center.reProject(ct);
341
            aux.reProject(ct);
342
            this.setProjection(ct.getPDest());
343
        } catch (CoordTransRuntimeException e){
344
            center.setX(0);
345
            center.setY(0);
346
            radius = 0;
347
            return;
348
        }
349
        try {
350
            radius = center.distance(aux);
351
        } catch (BaseException e) {
352
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
353
        }
354
    }
355

    
356
    @Override
357
    public void transform(AffineTransform at) {
358
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
359
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
360

    
361
        Point2D aux = new Point2D(center.getX(), center.getY()-radius);
362
        center.transform(at);
363
        aux.transform(at);
364
        try {
365
            radius = center.distance(aux);
366
        } catch (BaseException e) {
367
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
368
        }
369

    
370
    }
371

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

    
380

    
381
    @Override
382
    public boolean canBeTransformed(AffineTransform at) {
383
        return false;
384
    }
385

    
386
    @Override
387
    public boolean canBeReprojected(ICoordTrans ct) {
388
        return false;
389
    }
390

    
391
    @Override
392
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
393
        return offset(distance);
394
    }
395
    
396
}