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 / curve / arc / AbstractArc.java @ 47432

History | View | Annotate | Download (14.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.curve.arc;
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.GeometryUtils;
32
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
33
import org.gvsig.fmap.geom.jts.primitive.curve.AbstractCurve;
34
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
35
import org.gvsig.fmap.geom.jts.util.UtilFunctions;
36
import org.gvsig.fmap.geom.operation.GeometryOperationException;
37
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
38
import org.gvsig.fmap.geom.primitive.Arc;
39
import org.gvsig.fmap.geom.primitive.GeneralPathX;
40
import org.gvsig.fmap.geom.primitive.OrientablePrimitive;
41
import org.gvsig.fmap.geom.primitive.Point;
42

    
43

    
44
/**
45
 * @author fdiaz
46
 *
47
 */
48
public abstract class AbstractArc extends AbstractCurve implements Arc {
49

    
50
    /**
51
     *
52
     */
53
    private static final long serialVersionUID = 454301669807892457L;
54

    
55
    /**
56
     * @param subtype
57
     */
58
    protected AbstractArc(int subtype) {
59
        super(Geometry.TYPES.ARC, subtype);
60
    }
61

    
62
    protected Point init;
63

    
64
    /**
65
     * This is the middle point (belongs to the arc), not the center
66
     * of the circle/ellipse
67
     */
68
    protected Point middle;
69
    protected Point end;
70

    
71

    
72
    @Override
73
    public void setPoints(Point initialPoint, Point endPoint) {
74
        String message = "Calling deprecated method setPoints of a arc";
75
        notifyDeprecated(message);
76
        throw new UnsupportedOperationException(message);
77
    }
78

    
79
    @Override
80
    public double getCoordinateAt(int index, int dimension) {
81
        String message = "Calling deprecated method getCoordinateAt of a arc";
82
        notifyDeprecated(message);
83
        throw new UnsupportedOperationException(message);
84
    }
85

    
86
    @Override
87
    public OrientablePrimitive setCoordinateAt(int index, int dimension, double value) {
88
        String message = "Calling deprecated method setCoordinateAt of a arc";
89
        notifyDeprecated(message);
90
        throw new UnsupportedOperationException(message);
91
    }
92

    
93
    @Override
94
    public OrientablePrimitive addVertex(Point point) {
95
        String message = "Calling deprecated method addVertex of a arc";
96
        notifyDeprecated(message);
97
        throw new UnsupportedOperationException(message);
98
    }
99

    
100
    @Override
101
    public OrientablePrimitive addVertex(double x, double y) {
102
        String message = "Calling deprecated method addVertex of a arc";
103
        notifyDeprecated(message);
104
        throw new UnsupportedOperationException(message);
105
    }
106

    
107
    @Override
108
    public OrientablePrimitive addVertex(double x, double y, double z) {
109
        String message = "Calling deprecated method addVertex of a arc";
110
        notifyDeprecated(message);
111
        throw new UnsupportedOperationException(message);
112
    }
113

    
114
    @Override
115
    public void removeVertex(int index) {
116
        String message = "Calling deprecated method removeVertex of a arc";
117
        notifyDeprecated(message);
118
        throw new UnsupportedOperationException(message);
119
    }
120

    
121
    @Override
122
    public Point getVertex(int index) {
123
        String message = "Calling deprecated method getVertex of a arc";
124
        notifyDeprecated(message);
125
        throw new UnsupportedOperationException(message);
126
    }
127

    
128
    @Override
129
    public int getNumVertices() {
130
        String message = "Calling deprecated method getNumVertices of a arc";
131
        notifyDeprecated(message);
132
        throw new UnsupportedOperationException(message);
133
    }
134

    
135
    @Override
136
    public OrientablePrimitive insertVertex(int index, Point p) {
137
        String message = "Calling deprecated method insertVertex of a arc";
138
        notifyDeprecated(message);
139
        throw new UnsupportedOperationException(message);
140
    }
141

    
142
    @Override
143
    public OrientablePrimitive setVertex(int index, Point p) {
144
        String message = "Calling deprecated method setVertex of a arc";
145
        notifyDeprecated(message);
146
        throw new UnsupportedOperationException(message);
147
    }
148

    
149
    @Override
150
    public void setGeneralPath(GeneralPathX generalPathX) {
151
        String message = "Calling deprecated method setGeneralPath of a arc";
152
        notifyDeprecated(message);
153
        throw new UnsupportedOperationException(message);
154
    }
155

    
156
    @Override
157
    public void addMoveToVertex(Point point) {
158
        String message = "Calling deprecated method addMoveToVertex of a arc";
159
        notifyDeprecated(message);
160
        throw new UnsupportedOperationException(message);
161
    }
162

    
163
    @Override
164
    public void closePrimitive() {
165
        String message = "Calling deprecated method closePrimitive of a arc";
166
        notifyDeprecated(message);
167
        throw new UnsupportedOperationException(message);
168
    }
169

    
170
    @Override
171
    public OrientablePrimitive ensureCapacity(int capacity) {
172
        // TODO Auto-generated method stub
173
        return this;
174
    }
175

    
176
    @Override
177
    public void reProject(ICoordTrans ct) {
178
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
179
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
180

    
181
        try {
182
        init.reProject(ct);
183
        middle.reProject(ct);
184
        end.reProject(ct);
185
        this.setProjection(ct.getPDest());
186
        } catch (CoordTransRuntimeException e){
187
            //Si ha fallado la reproyecci?n de alguno de los puntos, ponemos todas las coordenadas a 0
188
            init.setX(0);
189
            init.setY(0);
190
            middle.setX(0);
191
            middle.setY(0);
192
            end.setX(0);
193
            end.setY(0);
194
        }
195
    }
196

    
197
    @Override
198
    public void transform(AffineTransform at) {
199
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
200
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
201

    
202
        init.transform(at);
203
        middle.transform(at);
204
        end.transform(at);
205
    }
206

    
207
    @Override
208
    public int getDimension() {
209
        return init.getDimension();
210
    }
211

    
212
    @Override
213
    public Shape getShape(AffineTransform affineTransform) {
214
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
215
    }
216

    
217
    @Override
218
    public Shape getShape() {
219
        return getShape(null);
220
    }
221

    
222
    @Override
223
    public boolean is3D() {
224
        return ((PointJTS)init).is3D();
225
    }
226

    
227
    /**
228
     * @param point
229
     * @return
230
     */
231
    protected abstract Point fixPoint(Point point);
232

    
233
    @Override
234
    public void setPoints(Point startPoint, Point midPoint, Point endPoint) {
235
        init = fixPoint(startPoint);
236
        middle = fixPoint(midPoint);
237
        end = fixPoint(endPoint);
238
    }
239

    
240
    @Override
241
    public Point getInitPoint() {
242
        return init;
243
    }
244

    
245
    @Override
246
    public Point getEndPoint() {
247
        return end;
248
    }
249

    
250
    @Override
251
    public Point getMiddlePoint() {
252
        return middle;
253
    }
254

    
255
    /**
256
     * Leaves the angle between PI and -PI
257
     * @param angle (radians)
258
     * @return
259
     */
260
    protected double normalizeAngle(double angle) {
261
        return Math.IEEEremainder(angle, 2*Math.PI);
262
    }
263

    
264
    @Override
265
    public void setPointsStartExt(Point center, double radius, double startAngle, double angleExt) {
266
        setPoints(center, radius, startAngle, angleExt);
267
    }
268

    
269
    @Override
270
    public void setPointsStartEnd(Point center, double radius, double startAngle, double endAngle) {
271

    
272
        if (startAngle == endAngle) {
273
            setPointsStartExt(center, radius, startAngle, 0);
274
        } else {
275

    
276
            /*
277
             * Normalize then force clockwise:
278
             */
279
            double norm_start = normalizeAngle(startAngle);
280
            double norm_end = normalizeAngle(endAngle);
281
            double ang_ext;
282
            
283
            // clockwise
284
            // ang_ext must be positive
285
            if (norm_end < 0){
286
                norm_end = 2*Math.PI+norm_end;
287
            }
288
            if (norm_start < 0) {
289
                norm_start = 2*Math.PI+norm_start;
290
            }
291
            ang_ext = norm_end - norm_start;
292
            if (ang_ext < 0) {
293
                ang_ext = 2*Math.PI+ang_ext;
294
                
295
            }
296
            
297
            // finally call other method with ang_ext
298
            setPointsStartExt(center, radius, startAngle, ang_ext);
299

    
300
        }
301
    }
302

    
303
    @Override
304
    public GeneralPathX getGeneralPath() {
305

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

    
310

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

    
316

    
317
    @Override
318
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
319

    
320
        java.awt.geom.Point2D.Double p1 = new java.awt.geom.Point2D.Double(init.getX(), init.getY());
321
        java.awt.geom.Point2D.Double p2 = new java.awt.geom.Point2D.Double(middle.getX(), middle.getY());
322
        java.awt.geom.Point2D.Double p3 = new java.awt.geom.Point2D.Double(end.getX(), end.getY());
323

    
324
        java.awt.geom.Arc2D arco = UtilFunctions.createArc(p1, p2, p3);
325
        if (arco == null) {
326
            LOGGER.info("Did not set arc points (probably aligned points): " + p1.getX() + " " + p1.getY() + " :: "
327
                + p2.getX() + " " + p2.getY() + " :: " + p3.getX() + " " + p3.getY());
328
            throw new IllegalArgumentException("Did not set arc points (probably aligned points).");
329
        }
330

    
331
        return arco.getPathIterator(at, flatness);
332
    }
333

    
334
    @Override
335
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
336
        Point aux = init;
337
        init = end;
338
        end = aux;
339
    }
340

    
341

    
342
    @Override
343
    public double getStartAngle() {
344
        return GeometryUtils.calculateAngle(getCenterPoint(), getInitPoint());
345
//        return getAngle(getCenterPoint(), getInitPoint());
346
    }
347

    
348
    @Override
349
    public double getEndAngle() {
350
        return GeometryUtils.calculateAngle(getCenterPoint(), getEndPoint());
351
    }
352

    
353

    
354
    @Override
355
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
356
        // TODO Auto-generated method stub
357
        Point center = getCenterPoint();
358
        double radius = center.distance(init);
359
        double scale = (radius+distance)/radius;
360
        AffineTransform at = getScaleAffineTransform(center, scale);
361
        Geometry cloned = this.cloneGeometry();
362
        cloned.transform(at);
363
        return cloned;
364
    }
365
    
366
    @Override
367
    public Geometry offset(int joinStyle, double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
368
        return offset(distance);
369
    }
370

    
371
    protected AffineTransform getScaleAffineTransform(Point center, Double scale)
372
        throws GeometryOperationNotSupportedException,
373
        GeometryOperationException {
374

    
375
        AffineTransform translate =
376
            AffineTransform
377
                .getTranslateInstance(-center.getX(), -center.getY());
378

    
379
        AffineTransform scaleTransform = AffineTransform.getScaleInstance(scale,scale);
380

    
381
        AffineTransform inverseTranslate =
382
            AffineTransform.getTranslateInstance(center.getX(), center.getY());
383
        AffineTransform at = new AffineTransform(translate);
384

    
385
        at.preConcatenate(scaleTransform);
386
        at.preConcatenate(inverseTranslate);
387
        return at;
388
    }
389

    
390
    @Override
391
    public boolean canBeTransformed(AffineTransform at) {
392
        return false;
393
    }
394

    
395
    @Override
396
    public boolean canBeReprojected(ICoordTrans ct) {
397
        return false;
398
    }
399

    
400
    @Override
401
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
402
        Arc2D other = new Arc2D();
403
        other.setProjection(this.getProjection());
404
        Point clonedInit = (Point)init.force2D();
405
        Point clonedMiddle = (Point)middle.force2D();
406
        Point clonedEnd = (Point)end.force2D();
407
        other.setPoints(clonedInit, clonedMiddle, clonedEnd);
408
        return other;
409
    }
410
    
411
    @Override
412
    public Geometry force2DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
413
        Arc2DM other = new Arc2DM();
414
        other.setProjection(this.getProjection());
415
        Point clonedInit = (Point)init.force2DM();
416
        Point clonedMiddle = (Point)middle.force2DM();
417
        Point clonedEnd = (Point)end.force2DM();
418
        other.setPoints(clonedInit, clonedMiddle, clonedEnd);
419
        return other;
420
    }
421
    
422
    @Override
423
    public Geometry force3D() throws GeometryOperationNotSupportedException, GeometryOperationException {
424
        Arc2DZ other = new Arc2DZ();
425
        other.setProjection(this.getProjection());
426
        Point clonedInit = (Point)init.force3D();
427
        Point clonedMiddle = (Point)middle.force3D();
428
        Point clonedEnd = (Point)end.force3D();
429
        other.setPoints(clonedInit, clonedMiddle, clonedEnd);
430
        return other;
431
    }
432

    
433
    @Override
434
    public Geometry force3DM() throws GeometryOperationNotSupportedException, GeometryOperationException {
435
        Arc2DZM other = new Arc2DZM();
436
        other.setProjection(this.getProjection());
437
        Point clonedInit = (Point)init.force3DM();
438
        Point clonedMiddle = (Point)middle.force3DM();
439
        Point clonedEnd = (Point)end.force3DM();
440
        other.setPoints(clonedInit, clonedMiddle, clonedEnd);
441
        return other;
442
    }
443

    
444

    
445
}