gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.circlecr / src / main / java / org / gvsig / vectorediting / lib / prov / circlecr / Circumference2Tg1PEditingProvider.java @ 2109
History | View | Annotate | Download (17 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2014 gvSIG Association
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.vectorediting.lib.prov.circlecr; |
26 |
|
27 |
import java.awt.geom.Point2D; |
28 |
import java.util.Objects; |
29 |
import org.gvsig.euclidean.EuclideanLine2D; |
30 |
import org.gvsig.euclidean.EuclideanManager; |
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.GeometryLocator; |
33 |
import org.gvsig.fmap.geom.GeometryManager; |
34 |
import org.gvsig.fmap.geom.aggregate.MultiCurve; |
35 |
import org.gvsig.fmap.geom.primitive.Circle; |
36 |
import org.gvsig.fmap.geom.primitive.Circumference; |
37 |
import org.gvsig.fmap.geom.primitive.Curve; |
38 |
import org.gvsig.fmap.geom.primitive.Point; |
39 |
import org.gvsig.fmap.geom.type.GeometryType; |
40 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
41 |
import org.gvsig.tools.dynobject.DynObject; |
42 |
import org.gvsig.tools.service.spi.ProviderServices; |
43 |
import org.gvsig.tools.util.ToolsUtilLocator; |
44 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
45 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
46 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
47 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
48 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
49 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
50 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
51 |
|
52 |
public class Circumference2Tg1PEditingProvider extends Circle2Tg1PEditingProvider |
53 |
implements EditingProvider {
|
54 |
|
55 |
public Circumference2Tg1PEditingProvider(ProviderServices providerServices,
|
56 |
DynObject parameters) { |
57 |
super(providerServices, parameters);
|
58 |
} |
59 |
|
60 |
public DefaultDrawingStatus getDrawingStatus(Point mousePosition) |
61 |
throws DrawServiceException {
|
62 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
63 |
EditingProviderManager editingProviderManager = |
64 |
EditingProviderLocator.getProviderManager(); |
65 |
// ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
66 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
67 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
68 |
|
69 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
70 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
71 |
|
72 |
EditingProviderServices editingProviderServices |
73 |
= (EditingProviderServices) getProviderServices(); |
74 |
|
75 |
if (values != null) { |
76 |
|
77 |
Point firstPointValue = (Point) values.get(firstPoint); |
78 |
Point secondPointValue = (Point) values.get(secondPoint); |
79 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
80 |
Point fourthPointValue = (Point) values.get(fourthPoint); |
81 |
Point fifthPointValue = (Point) values.get(fifthPoint); |
82 |
|
83 |
|
84 |
if (firstPointValue != null && secondPointValue == null) { |
85 |
|
86 |
try {
|
87 |
|
88 |
int subtype = editingProviderServices.getSubType(featureStore);
|
89 |
|
90 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
91 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
92 |
|
93 |
Curve line = |
94 |
editingProviderServices.createLine(firstPointValue, |
95 |
mousePosition, subtype); |
96 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
97 |
|
98 |
} catch (Exception e) { |
99 |
throw new DrawServiceException(e); |
100 |
} |
101 |
|
102 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue == null) { |
103 |
|
104 |
try {
|
105 |
|
106 |
int subtype = editingProviderServices.getSubType(featureStore);
|
107 |
|
108 |
Curve line1 = |
109 |
editingProviderServices.createLine(firstPointValue, |
110 |
secondPointValue, subtype); |
111 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
112 |
|
113 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
114 |
|
115 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
116 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
117 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
118 |
|
119 |
} catch (Exception e) { |
120 |
throw new DrawServiceException(e); |
121 |
} |
122 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue != null && fourthPointValue == null) { |
123 |
|
124 |
try {
|
125 |
|
126 |
int subtype = editingProviderServices.getSubType(featureStore);
|
127 |
|
128 |
Curve line1 = |
129 |
editingProviderServices.createLine(firstPointValue, |
130 |
secondPointValue, subtype); |
131 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
132 |
|
133 |
Curve line = |
134 |
editingProviderServices.createLine(thirdPointValue, |
135 |
mousePosition, subtype); |
136 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
137 |
|
138 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
139 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
140 |
drawingStatus.addStatus(thirdPointValue, auxiliaryPointSymbolEditing, "");
|
141 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
142 |
|
143 |
} catch (Exception e) { |
144 |
throw new DrawServiceException(e); |
145 |
} |
146 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue != null && fourthPointValue != null && fifthPointValue == null) { |
147 |
|
148 |
try {
|
149 |
|
150 |
int subtype = editingProviderServices.getSubType(featureStore);
|
151 |
|
152 |
//Draw tangents
|
153 |
Curve line1 |
154 |
= editingProviderServices.createLine(firstPointValue, |
155 |
secondPointValue, subtype); |
156 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
157 |
|
158 |
Curve line2 = |
159 |
editingProviderServices.createLine(thirdPointValue, |
160 |
fourthPointValue, subtype); |
161 |
drawingStatus.addStatus(line2, auxiliaryLineSymbolEditing, "");
|
162 |
|
163 |
//Draw circle
|
164 |
Circle circle |
165 |
= editingProviderServices.createCircle( |
166 |
firstPointValue, |
167 |
secondPointValue, |
168 |
thirdPointValue, |
169 |
fourthPointValue, |
170 |
mousePosition, |
171 |
subtype); |
172 |
|
173 |
Circumference circumference = (Circumference) geomManager.create(CIRCUMFERENCE, subtype); |
174 |
circumference.setPoints(circle.getCenter(), circle.getRadious()); |
175 |
|
176 |
drawingStatus.addStatus(circle, auxiliaryLineSymbolEditing, "");
|
177 |
Point center = circle.getCenter();
|
178 |
|
179 |
//Draw the first four points
|
180 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
181 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
182 |
drawingStatus.addStatus(thirdPointValue, auxiliaryPointSymbolEditing, "");
|
183 |
drawingStatus.addStatus(fourthPointValue, auxiliaryPointSymbolEditing, "");
|
184 |
|
185 |
EuclideanLine2D tangent1 = euclideanManager.createLine2D(firstPointValue.getX(), firstPointValue.getY(), secondPointValue.getX(), secondPointValue.getY()); |
186 |
EuclideanLine2D tangent2 = euclideanManager.createLine2D(thirdPointValue.getX(), thirdPointValue.getY(), fourthPointValue.getX(), fourthPointValue.getY()); |
187 |
|
188 |
if (tangent1.isParallel(tangent2)) {
|
189 |
if (Objects.equals(tangent1.getYIntercept(), tangent2.getYIntercept())) { //Same lines |
190 |
//Draw line between mouse position and tangent1
|
191 |
EuclideanLine2D perp = tangent1.getPerpendicular(mousePosition.getX(), mousePosition.getY()); |
192 |
Point2D intersection = tangent1.getIntersection(perp);
|
193 |
drawingStatus.addStatus( |
194 |
editingProviderServices.createLine( |
195 |
mousePosition.getX(), |
196 |
mousePosition.getY(), |
197 |
intersection.getX(), |
198 |
intersection.getY(), |
199 |
subtype |
200 |
), auxiliaryLineSymbolEditing, "");
|
201 |
} |
202 |
} else {
|
203 |
//Draw line between center and intersection tangents (bisector passing through the center)
|
204 |
|
205 |
Point2D intersectionTangents = tangent1.getIntersection(tangent2);
|
206 |
|
207 |
drawingStatus.addStatus( |
208 |
editingProviderServices.createLine( |
209 |
intersectionTangents.getX(), |
210 |
intersectionTangents.getY(), |
211 |
center.getX(), |
212 |
center.getY(), |
213 |
subtype |
214 |
), |
215 |
auxiliaryLineSymbolEditing, |
216 |
"");
|
217 |
|
218 |
drawingStatus.addStatus( |
219 |
geomManager.createPoint( |
220 |
intersectionTangents.getX(), |
221 |
intersectionTangents.getY(), |
222 |
subtype |
223 |
), |
224 |
auxiliaryPointSymbolEditing, "");
|
225 |
|
226 |
//Draw perpendicular bisectors passing through the center
|
227 |
|
228 |
EuclideanLine2D[] bisectors = tangent1.getBisectors(tangent2);
|
229 |
|
230 |
double distance0 = bisectors[0].getDistance(mousePosition.getX(), mousePosition.getY()); |
231 |
double distance1 = bisectors[1].getDistance(mousePosition.getX(), mousePosition.getY()); |
232 |
|
233 |
EuclideanLine2D bisector = bisectors[0];
|
234 |
if (distance0 > distance1) {
|
235 |
bisector = bisectors[1];
|
236 |
} |
237 |
|
238 |
EuclideanLine2D perp = bisector.getPerpendicular(mousePosition.getX(), mousePosition.getY()); |
239 |
Point2D intersection;
|
240 |
if(Double.isInfinite(perp.getSlope())){ |
241 |
intersection = new Point2D.Double( |
242 |
mousePosition.getX(), |
243 |
center.getY()); |
244 |
} else {
|
245 |
intersection = bisector.getIntersection(perp); |
246 |
} |
247 |
|
248 |
drawingStatus.addStatus( |
249 |
editingProviderServices.createLine( |
250 |
intersection.getX(), |
251 |
intersection.getY(), |
252 |
center.getX(), |
253 |
center.getY(), |
254 |
subtype |
255 |
), |
256 |
auxiliaryLineSymbolEditing, |
257 |
"");
|
258 |
} |
259 |
|
260 |
//Draw line between mouse position and center
|
261 |
drawingStatus.addStatus( |
262 |
editingProviderServices.createLine( |
263 |
mousePosition, |
264 |
center, |
265 |
subtype), |
266 |
auxiliaryLineSymbolEditing, "");
|
267 |
|
268 |
//Draw normal lines (perpendicular to the tangents passing through the center)
|
269 |
EuclideanLine2D perpA = tangent1.getPerpendicular(center.getX(), center.getY()); |
270 |
|
271 |
Point intersectionA;
|
272 |
if (tangent1.getSlope() == 0.0) { |
273 |
intersectionA = editingProviderServices.createPoint(center.getX(), tangent1.getYIntercept(), subtype); |
274 |
} else {
|
275 |
Point2D intersection = tangent1.getIntersection(perpA);
|
276 |
intersectionA = editingProviderServices.createPoint(intersection.getX(), intersection.getY(), subtype); |
277 |
} |
278 |
drawingStatus.addStatus(intersectionA, auxiliaryPointSymbolEditing, "");
|
279 |
drawingStatus.addStatus(editingProviderServices.createLine(center, intersectionA, subtype), auxiliaryLineSymbolEditing, "");
|
280 |
|
281 |
|
282 |
EuclideanLine2D perpB = tangent2.getPerpendicular(center.getX(), center.getY()); |
283 |
|
284 |
Point intersectionB;
|
285 |
if (tangent2.getSlope() == 0.0) { |
286 |
intersectionB = editingProviderServices.createPoint(center.getX(), tangent2.getYIntercept(), subtype); |
287 |
} else {
|
288 |
Point2D intersection = tangent2.getIntersection(perpB);
|
289 |
intersectionB = editingProviderServices.createPoint(intersection.getX(), intersection.getY(), subtype); |
290 |
} |
291 |
drawingStatus.addStatus(intersectionB, auxiliaryPointSymbolEditing, "");
|
292 |
drawingStatus.addStatus(editingProviderServices.createLine(center, intersectionB, subtype), auxiliaryLineSymbolEditing, "");
|
293 |
|
294 |
//Draw point in mouse position
|
295 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
296 |
|
297 |
} catch (Exception e) { |
298 |
throw new DrawServiceException(e); |
299 |
} |
300 |
} |
301 |
} |
302 |
return drawingStatus;
|
303 |
} |
304 |
|
305 |
public Geometry finish() throws FinishServiceException { |
306 |
|
307 |
final GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
308 |
|
309 |
Point firstPointValue = (Point) values.get(firstPoint); |
310 |
Point secondPointValue = (Point) values.get(secondPoint); |
311 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
312 |
Point fourthPointValue = (Point) values.get(fourthPoint); |
313 |
Point fifthPointValue = (Point) values.get(fifthPoint); |
314 |
|
315 |
EditingProviderServices editingProviderServices = |
316 |
(EditingProviderServices) getProviderServices(); |
317 |
|
318 |
try {
|
319 |
int subtype = editingProviderServices.getSubType(featureStore);
|
320 |
GeometryType storeGeomType = |
321 |
editingProviderServices.getGeomType(featureStore); |
322 |
Circle circle = |
323 |
editingProviderServices.createCircle(firstPointValue, secondPointValue, thirdPointValue, fourthPointValue, fifthPointValue, subtype); |
324 |
Circumference circumference = (Circumference) geomManager.create(CIRCUMFERENCE, subtype); |
325 |
circumference.setPoints(circle.getCenter(), circle.getRadious()); |
326 |
|
327 |
if (storeGeomType.isTypeOf(MULTICURVE)) {
|
328 |
MultiCurve multicurve; |
329 |
multicurve = |
330 |
geomManager.createMultiCurve( |
331 |
storeGeomType.getSubType()); |
332 |
multicurve.addCurve((Circumference) circumference); |
333 |
return multicurve;
|
334 |
} |
335 |
|
336 |
return circumference;
|
337 |
} catch (Exception e) { |
338 |
throw new FinishServiceException(e); |
339 |
} |
340 |
} |
341 |
|
342 |
@Override
|
343 |
public String getName() { |
344 |
return Circumference2Tg1PEditingProviderFactory.PROVIDER_NAME;
|
345 |
} |
346 |
|
347 |
} |