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 / Arc2DZ.java @ 42493

History | View | Annotate | Download (8.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.geom.PathIterator;
26

    
27
import com.vividsolutions.jts.geom.Coordinate;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryException;
31
import org.gvsig.fmap.geom.aggregate.MultiLine;
32
import org.gvsig.fmap.geom.aggregate.MultiPoint;
33
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiLine3D;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint3D;
36
import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon3D;
37
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line3D;
38
import org.gvsig.fmap.geom.jts.primitive.point.Point3D;
39
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
40
import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon3D;
41
import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence;
42
import org.gvsig.fmap.geom.jts.util.JTSUtils;
43
import org.gvsig.fmap.geom.primitive.Line;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.fmap.geom.primitive.Polygon;
46

    
47
/**
48
 * @author fdiaz
49
 *
50
 */
51
public class Arc2DZ extends AbstractArc {
52

    
53
    /**
54
     *
55
     */
56
    private static final long serialVersionUID = -5691008010954470053L;
57
    private double zValue = Double.NaN;
58

    
59
    /**
60
     * @param subtype
61
     */
62
    public Arc2DZ() {
63
        super(Geometry.SUBTYPES.GEOM3D);
64
    }
65

    
66
    /*
67
     * (non-Javadoc)
68
     *
69
     * @see
70
     * org.gvsig.fmap.geom.primitive.Arc#setPoints(org.gvsig.fmap.geom.primitive
71
     * .Point, double, double, double)
72
     */
73
    public void setPoints(Point center, double radius, double startAngle, double angleExt) {
74
        center = fixPoint(center);
75

    
76
        double diameter = radius * 2;
77
        double x;
78
        double y;
79
        double start;
80
        double extent;
81
        double angleOffset;
82
        final double pi2 = Math.PI * 2;
83

    
84
        angleOffset = 0;
85
        if (angleExt < 0) {
86
            angleOffset = pi2 + angleExt;
87
            angleExt = Math.abs(angleExt);
88
        }
89
        x = center.getX() - radius;
90
        y = center.getY() - radius;
91

    
92
        if (angleExt > 0 && (angleExt % pi2) == 0) {
93
            start = 0;
94
            extent = 360;
95
        } else {
96
            angleExt = angleExt % pi2; // Asumimos que aqui angleExt es siempre
97
                                       // positivo
98
            startAngle = Math.abs(startAngle) % pi2;
99
            start = Math.toDegrees(pi2 - startAngle + angleOffset);
100
            extent = -Math.toDegrees(pi2 - angleExt);
101
        }
102

    
103
        double angleStart = Math.toRadians(-start);
104
        double initX = x + (Math.cos(angleStart) * 0.5 + 0.5) * diameter;
105
        double initY = y + (Math.sin(angleStart) * 0.5 + 0.5) * diameter;
106
        init = new Point3D(initX, initY, ((Point3D) center).getZ());
107

    
108
        double angleEnd = Math.toRadians(-start - extent);
109
        double endX = x + (Math.cos(angleEnd) * 0.5 + 0.5) * diameter;
110
        double endY = y + (Math.sin(angleEnd) * 0.5 + 0.5) * diameter;
111
        end = new Point3D(endX, endY, ((Point3D) center).getZ());
112

    
113
        double angleMiddle = Math.toRadians(-start - extent) / 2;
114
        double middleX = x + (Math.cos(angleMiddle) * 0.5 + 0.5) * diameter;
115
        double middleY = y + (Math.sin(angleMiddle) * 0.5 + 0.5) * diameter;
116
        middle = new Point3D(middleX, middleY, ((Point3D) center).getZ());
117
    }
118

    
119
    /*
120
     * (non-Javadoc)
121
     *
122
     * @see org.gvsig.fmap.geom.primitive.Arc#getCenterPoint()
123
     */
124
    public Point getCenterPoint() {
125
        ((PointJTS) init).getJTS();
126
        Point3D center = new Point3D(JTSUtils.getCircumcentre(init, middle, end));
127
        return center;
128
    }
129

    
130
    /*
131
     * (non-Javadoc)
132
     *
133
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
134
     */
135
    public Geometry cloneGeometry() {
136
        Arc2DZ arc2D = new Arc2DZ();
137
        arc2D.setPoints((Point)init.cloneGeometry(), (Point)middle.cloneGeometry(), (Point)end.cloneGeometry());
138
        return arc2D;
139
    }
140

    
141
    /*
142
     * (non-Javadoc)
143
     *
144
     * @see
145
     * org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#fixPoint(org
146
     * .gvsig.fmap.geom.primitive.Point)
147
     */
148
    @Override
149
    protected Point fixPoint(Point point) {
150
        if (point instanceof Point3D) {
151
            Point3D point3D = (Point3D) point;
152
            if (Double.isNaN(zValue) && !(Double.isNaN(point3D.getZ()))) {
153
                zValue = point3D.getZ();
154
                if (init!=null) {
155
                    ((Point3D) init).setZ(zValue);
156
                }
157
                if (middle!=null) {
158
                    ((Point3D) middle).setZ(zValue);
159
                }
160
                if (end!=null) {
161
                    ((Point3D) end).setZ(zValue);
162
                }
163
                return point3D;
164
            }
165
        }
166
        return new Point3D(point.getX(), point.getY(), Double.isNaN(zValue)?0:zValue);
167
    }
168

    
169
    /*
170
     * (non-Javadoc)
171
     *
172
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
173
     */
174
    public com.vividsolutions.jts.geom.Geometry getJTS() {
175
        PathIterator pi = getPathIterator(null);
176
        ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
177

    
178
        double coords[] = new double[6];
179
        while (!pi.isDone()) {
180
            switch (pi.currentSegment(coords)) {
181
            case PathIterator.SEG_MOVETO:
182
                coordinates.add(new Coordinate(coords[0], coords[1], zValue));
183
                break;
184
            case PathIterator.SEG_LINETO:
185
                coordinates.add(new Coordinate(coords[0], coords[1], zValue));
186
                break;
187
            case PathIterator.SEG_QUADTO:
188
                coordinates.add(new Coordinate(coords[0], coords[1], zValue));
189
                coordinates.add(new Coordinate(coords[2], coords[3], zValue));
190
                break;
191
            case PathIterator.SEG_CUBICTO:
192
                coordinates.add(new Coordinate(coords[0], coords[1], zValue));
193
                coordinates.add(new Coordinate(coords[2], coords[3], zValue));
194
                coordinates.add(new Coordinate(coords[4], coords[5], zValue));
195
                break;
196
            case PathIterator.SEG_CLOSE:
197
                coordinates.add(coordinates.get(0));
198
                break;
199
            }
200
            pi.next();
201
        }
202
        return JTSUtils.createJTSLineString(coordinates);
203
    }
204

    
205
    /* (non-Javadoc)
206
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
207
     */
208
    public MultiPoint toPoints() throws GeometryException {
209
        MultiPoint multiPoint = new MultiPoint3D();
210
        Coordinate[] coordinates = getJTS().getCoordinates();
211
        multiPoint.ensureCapacity(coordinates.length);
212
        for (int i = 0; i < coordinates.length; i++) {
213
            multiPoint.addPoint(new Point3D(coordinates[i]));
214
        }
215
        return multiPoint;
216
    }
217

    
218
    /* (non-Javadoc)
219
     * @see org.gvsig.fmap.geom.primitive.Line#toLines()
220
     */
221
    public MultiLine toLines() throws GeometryException {
222
        MultiLine multiLine = new MultiLine3D();
223
        Line line = new Line3D(getJTS().getCoordinates());
224
        multiLine.addPrimitive(line);
225
        return multiLine;
226
    }
227

    
228
    /* (non-Javadoc)
229
     * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
230
     */
231
    public MultiPolygon toPolygons() throws GeometryException {
232
        MultiPolygon multiPolygon = new MultiPolygon3D();
233
        Polygon polygon = new Polygon3D(getJTS().getCoordinates());
234
        multiPolygon.addPrimitive(polygon);
235
        return multiPolygon;
236
    }
237
}