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 / Arc2D.java @ 42283
History | View | Annotate | Download (7.39 KB)
1 | 42267 | fdiaz | /* 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 | 42269 | fdiaz | 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.MultiLine2D; |
||
35 | import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D; |
||
36 | import org.gvsig.fmap.geom.jts.aggregate.MultiPolygon2D; |
||
37 | import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2D; |
||
38 | 42267 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.point.Point2D; |
39 | import org.gvsig.fmap.geom.jts.primitive.point.PointJTS; |
||
40 | 42269 | fdiaz | import org.gvsig.fmap.geom.jts.primitive.surface.polygon.Polygon2D; |
41 | 42267 | fdiaz | import org.gvsig.fmap.geom.jts.util.ArrayListCoordinateSequence; |
42 | import org.gvsig.fmap.geom.jts.util.JTSUtils; |
||
43 | 42269 | fdiaz | import org.gvsig.fmap.geom.primitive.Line; |
44 | 42267 | fdiaz | import org.gvsig.fmap.geom.primitive.Point; |
45 | 42269 | fdiaz | import org.gvsig.fmap.geom.primitive.Polygon; |
46 | 42267 | fdiaz | |
47 | |||
48 | |||
49 | /**
|
||
50 | * @author fdiaz
|
||
51 | *
|
||
52 | */
|
||
53 | public class Arc2D extends AbstractArc { |
||
54 | |||
55 | /**
|
||
56 | *
|
||
57 | */
|
||
58 | private static final long serialVersionUID = 3414562338763885954L; |
||
59 | |||
60 | /**
|
||
61 | * @param subtype
|
||
62 | */
|
||
63 | 42283 | fdiaz | public Arc2D() { |
64 | 42267 | fdiaz | super(Geometry.SUBTYPES.GEOM2D);
|
65 | } |
||
66 | |||
67 | /* (non-Javadoc)
|
||
68 | * @see org.gvsig.fmap.geom.primitive.Arc#setPoints(org.gvsig.fmap.geom.primitive.Point, double, double, double)
|
||
69 | */
|
||
70 | public void setPoints(Point center, double radius, double startAngle, double angleExt) { |
||
71 | |||
72 | 42271 | fdiaz | double diameter = radius*2; |
73 | 42267 | fdiaz | double x;
|
74 | double y;
|
||
75 | double start;
|
||
76 | double extent;
|
||
77 | double angleOffset;
|
||
78 | final double pi2 = Math.PI*2; |
||
79 | |||
80 | angleOffset = 0;
|
||
81 | if( angleExt<0 ) { |
||
82 | angleOffset = pi2 + angleExt; |
||
83 | angleExt = Math.abs(angleExt);
|
||
84 | } |
||
85 | x = center.getX() - radius; |
||
86 | y = center.getY() - radius; |
||
87 | |||
88 | if( angleExt > 0 && (angleExt % pi2) == 0 ) { |
||
89 | start = 0;
|
||
90 | extent = 360;
|
||
91 | } else {
|
||
92 | angleExt = angleExt % pi2; //Asumimos que aqui angleExt es siempre positivo
|
||
93 | startAngle = Math.abs(startAngle) % pi2;
|
||
94 | start = Math.toDegrees( pi2 - startAngle + angleOffset) ;
|
||
95 | extent = - Math.toDegrees(pi2 - angleExt);
|
||
96 | } |
||
97 | |||
98 | |||
99 | double angleStart = Math.toRadians(-start); |
||
100 | 42271 | fdiaz | double initX = x + (Math.cos(angleStart) * 0.5 + 0.5) * diameter; |
101 | double initY = y + (Math.sin(angleStart) * 0.5 + 0.5) * diameter; |
||
102 | 42267 | fdiaz | init = new Point2D(initX, initY); |
103 | |||
104 | double angleEnd = Math.toRadians(-start - extent); |
||
105 | 42271 | fdiaz | double endX = x + (Math.cos(angleEnd) * 0.5 + 0.5) * diameter; |
106 | double endY = y + (Math.sin(angleEnd) * 0.5 + 0.5) * diameter; |
||
107 | 42267 | fdiaz | end = new Point2D(endX, endY); |
108 | |||
109 | double angleMiddle = Math.toRadians(-start - extent)/2; |
||
110 | 42271 | fdiaz | double middleX = x + (Math.cos(angleMiddle) * 0.5 + 0.5) * diameter; |
111 | double middleY = y + (Math.sin(angleMiddle) * 0.5 + 0.5) * diameter; |
||
112 | 42283 | fdiaz | middle = new Point2D(middleX, middleY); |
113 | 42267 | fdiaz | } |
114 | |||
115 | /* (non-Javadoc)
|
||
116 | * @see org.gvsig.fmap.geom.primitive.Arc#getCenterPoint()
|
||
117 | */
|
||
118 | public Point getCenterPoint() { |
||
119 | ((PointJTS)init).getJTS(); |
||
120 | Point2D center = new Point2D(JTSUtils.getCircumcentre(init, middle, end)); |
||
121 | return center;
|
||
122 | } |
||
123 | |||
124 | /* (non-Javadoc)
|
||
125 | * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
||
126 | */
|
||
127 | public Geometry cloneGeometry() {
|
||
128 | Arc2D arc2D = new Arc2D(); |
||
129 | 42283 | fdiaz | arc2D.setPoints((Point)init.cloneGeometry(), (Point)middle.cloneGeometry(), (Point)end.cloneGeometry()); |
130 | 42267 | fdiaz | return arc2D;
|
131 | } |
||
132 | |||
133 | /*
|
||
134 | * (non-Javadoc)
|
||
135 | *
|
||
136 | * @see
|
||
137 | * org.gvsig.fmap.geom.jts.primitive.curve.line.AbstractLine#fixPoint(org
|
||
138 | * .gvsig.fmap.geom.primitive.Point)
|
||
139 | */
|
||
140 | @Override
|
||
141 | protected Point fixPoint(Point point) { |
||
142 | if (point instanceof Point2D) { |
||
143 | return point;
|
||
144 | } else {
|
||
145 | return new Point2D(point.getX(), point.getY()); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /* (non-Javadoc)
|
||
150 | * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
|
||
151 | */
|
||
152 | public com.vividsolutions.jts.geom.Geometry getJTS() {
|
||
153 | PathIterator pi = getPathIterator(null); |
||
154 | ArrayListCoordinateSequence coordinates = new ArrayListCoordinateSequence();
|
||
155 | |||
156 | double coords[] = new double[6]; |
||
157 | while (!pi.isDone()) {
|
||
158 | switch (pi.currentSegment(coords)) {
|
||
159 | case PathIterator.SEG_MOVETO: |
||
160 | coordinates.add(new Coordinate(coords[0], coords[1])); |
||
161 | break;
|
||
162 | case PathIterator.SEG_LINETO: |
||
163 | coordinates.add(new Coordinate(coords[0], coords[1])); |
||
164 | break;
|
||
165 | case PathIterator.SEG_QUADTO: |
||
166 | coordinates.add(new Coordinate(coords[0], coords[1])); |
||
167 | coordinates.add(new Coordinate(coords[2], coords[3])); |
||
168 | break;
|
||
169 | case PathIterator.SEG_CUBICTO: |
||
170 | coordinates.add(new Coordinate(coords[0], coords[1])); |
||
171 | coordinates.add(new Coordinate(coords[2], coords[3])); |
||
172 | coordinates.add(new Coordinate(coords[4], coords[5])); |
||
173 | break;
|
||
174 | case PathIterator.SEG_CLOSE: |
||
175 | coordinates.add(coordinates.get(0));
|
||
176 | break;
|
||
177 | } |
||
178 | pi.next(); |
||
179 | } |
||
180 | 42268 | fdiaz | return JTSUtils.createJTSLineString(coordinates);
|
181 | 42267 | fdiaz | } |
182 | |||
183 | 42269 | fdiaz | /* (non-Javadoc)
|
184 | * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
||
185 | */
|
||
186 | public MultiPoint toPoints() throws GeometryException { |
||
187 | MultiPoint multiPoint = new MultiPoint2D();
|
||
188 | Coordinate[] coordinates = getJTS().getCoordinates();
|
||
189 | 42271 | fdiaz | multiPoint.ensureCapacity(coordinates.length); |
190 | 42269 | fdiaz | for (int i = 0; i < coordinates.length; i++) { |
191 | multiPoint.addPoint(new Point2D(coordinates[i])); |
||
192 | } |
||
193 | return multiPoint;
|
||
194 | } |
||
195 | 42267 | fdiaz | |
196 | 42269 | fdiaz | /* (non-Javadoc)
|
197 | * @see org.gvsig.fmap.geom.primitive.Line#toLines()
|
||
198 | */
|
||
199 | public MultiLine toLines() throws GeometryException { |
||
200 | MultiLine multiLine = new MultiLine2D();
|
||
201 | Line line = new Line2D(getJTS().getCoordinates()); |
||
202 | multiLine.addPrimitive(line); |
||
203 | return multiLine;
|
||
204 | } |
||
205 | |||
206 | /* (non-Javadoc)
|
||
207 | * @see org.gvsig.fmap.geom.primitive.Line#toPolygons()
|
||
208 | */
|
||
209 | public MultiPolygon toPolygons() throws GeometryException { |
||
210 | MultiPolygon multiPolygon = new MultiPolygon2D();
|
||
211 | Polygon polygon = new Polygon2D(getJTS().getCoordinates()); |
||
212 | multiPolygon.addPrimitive(polygon); |
||
213 | return multiPolygon;
|
||
214 | } |
||
215 | |||
216 | |||
217 | 42267 | fdiaz | } |