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 / Circle2Tg1PEditingProvider.java @ 2204
History | View | Annotate | Download (21.2 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 |
package org.gvsig.vectorediting.lib.prov.circlecr; |
25 |
|
26 |
import java.awt.geom.Point2D; |
27 |
import java.util.ArrayList; |
28 |
import java.util.HashMap; |
29 |
import java.util.List; |
30 |
import java.util.Map; |
31 |
import java.util.Objects; |
32 |
import org.gvsig.euclidean.EuclideanLine2D; |
33 |
import org.gvsig.euclidean.EuclideanManager; |
34 |
|
35 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
36 |
import org.gvsig.fmap.geom.Geometry; |
37 |
import org.gvsig.fmap.geom.GeometryLocator; |
38 |
import org.gvsig.fmap.geom.GeometryManager; |
39 |
import org.gvsig.fmap.geom.aggregate.MultiSurface; |
40 |
import org.gvsig.fmap.geom.primitive.Circle; |
41 |
import org.gvsig.fmap.geom.primitive.Curve; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.fmap.geom.type.GeometryType; |
44 |
import org.gvsig.fmap.mapcontext.rendering.symbols.ISymbol; |
45 |
import org.gvsig.tools.dynobject.DynObject; |
46 |
import org.gvsig.tools.service.spi.ProviderServices; |
47 |
import org.gvsig.tools.util.ToolsUtilLocator; |
48 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter; |
49 |
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE; |
50 |
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException; |
51 |
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException; |
52 |
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException; |
53 |
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException; |
54 |
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider; |
55 |
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus; |
56 |
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter; |
57 |
import org.gvsig.vectorediting.lib.spi.EditingProvider; |
58 |
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory; |
59 |
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator; |
60 |
import org.gvsig.vectorediting.lib.spi.EditingProviderManager; |
61 |
import org.gvsig.vectorediting.lib.spi.EditingProviderServices; |
62 |
|
63 |
public class Circle2Tg1PEditingProvider extends AbstractEditingProvider implements |
64 |
EditingProvider { |
65 |
|
66 |
protected EditingServiceParameter firstPoint;
|
67 |
|
68 |
protected EditingServiceParameter secondPoint;
|
69 |
|
70 |
protected EditingServiceParameter thirdPoint;
|
71 |
|
72 |
protected EditingServiceParameter fourthPoint;
|
73 |
|
74 |
protected EditingServiceParameter fifthPoint;
|
75 |
|
76 |
protected Map<EditingServiceParameter, Object> values; |
77 |
|
78 |
protected FeatureStore featureStore;
|
79 |
|
80 |
public Circle2Tg1PEditingProvider(ProviderServices providerServices,
|
81 |
DynObject parameters) { |
82 |
super(providerServices);
|
83 |
|
84 |
this.featureStore
|
85 |
= (FeatureStore) parameters |
86 |
.getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD); |
87 |
|
88 |
this.firstPoint
|
89 |
= new DefaultEditingServiceParameter("first_point_of_the_first_line", "first_point_of_the_first_line", |
90 |
TYPE.POSITION); |
91 |
|
92 |
this.secondPoint
|
93 |
= new DefaultEditingServiceParameter("second_point_of_the_first_line", "second_point_of_the_first_line", |
94 |
TYPE.POSITION); |
95 |
|
96 |
this.thirdPoint
|
97 |
= new DefaultEditingServiceParameter("first_point_of_the_second_line", "first_point_of_the_second_line", |
98 |
TYPE.POSITION); |
99 |
|
100 |
this.fourthPoint
|
101 |
= new DefaultEditingServiceParameter("second_point_of_the_second_line", "second_point_of_the_second_line", |
102 |
TYPE.POSITION); |
103 |
|
104 |
this.fifthPoint
|
105 |
= new DefaultEditingServiceParameter("point_near_center", "point_near_center", |
106 |
TYPE.POSITION); |
107 |
} |
108 |
|
109 |
public DefaultDrawingStatus getDrawingStatus(Point mousePosition) |
110 |
throws DrawServiceException {
|
111 |
DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
|
112 |
EditingProviderManager editingProviderManager |
113 |
= EditingProviderLocator.getProviderManager(); |
114 |
ISymbol polygonSymbolEditing = editingProviderManager.getSymbol("polygon-symbol-editing");
|
115 |
ISymbol auxiliaryLineSymbolEditing = editingProviderManager.getSymbol("auxiliary-line-symbol-editing");
|
116 |
ISymbol auxiliaryPointSymbolEditing = editingProviderManager.getSymbol("auxiliary-point-symbol-editing");
|
117 |
|
118 |
EuclideanManager euclideanManager = ToolsUtilLocator.getEuclideanManager(); |
119 |
GeometryManager geomManager = GeometryLocator.getGeometryManager(); |
120 |
|
121 |
EditingProviderServices editingProviderServices |
122 |
= (EditingProviderServices) getProviderServices(); |
123 |
|
124 |
if (values != null) { |
125 |
|
126 |
Point firstPointValue = (Point) values.get(firstPoint); |
127 |
Point secondPointValue = (Point) values.get(secondPoint); |
128 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
129 |
Point fourthPointValue = (Point) values.get(fourthPoint); |
130 |
Point fifthPointValue = (Point) values.get(fifthPoint); |
131 |
|
132 |
|
133 |
if (firstPointValue != null && secondPointValue == null) { |
134 |
|
135 |
try {
|
136 |
|
137 |
int subtype = editingProviderServices.getSubType(featureStore);
|
138 |
|
139 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
140 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
141 |
|
142 |
Curve line |
143 |
= editingProviderServices.createLine(firstPointValue, |
144 |
mousePosition, subtype); |
145 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
146 |
|
147 |
} catch (Exception e) { |
148 |
throw new DrawServiceException(e); |
149 |
} |
150 |
|
151 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue == null) { |
152 |
|
153 |
try {
|
154 |
|
155 |
int subtype = editingProviderServices.getSubType(featureStore);
|
156 |
|
157 |
Curve line1 |
158 |
= editingProviderServices.createLine(firstPointValue, |
159 |
secondPointValue, subtype); |
160 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
161 |
|
162 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
163 |
|
164 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
165 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
166 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
167 |
|
168 |
} catch (Exception e) { |
169 |
throw new DrawServiceException(e); |
170 |
} |
171 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue != null && fourthPointValue == null) { |
172 |
|
173 |
try {
|
174 |
|
175 |
int subtype = editingProviderServices.getSubType(featureStore);
|
176 |
|
177 |
Curve line1 |
178 |
= editingProviderServices.createLine(firstPointValue, |
179 |
secondPointValue, subtype); |
180 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
181 |
|
182 |
Curve line |
183 |
= editingProviderServices.createLine(thirdPointValue, |
184 |
mousePosition, subtype); |
185 |
drawingStatus.addStatus(line, auxiliaryLineSymbolEditing, "");
|
186 |
|
187 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
188 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
189 |
drawingStatus.addStatus(thirdPointValue, auxiliaryPointSymbolEditing, "");
|
190 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
191 |
|
192 |
} catch (Exception e) { |
193 |
throw new DrawServiceException(e); |
194 |
} |
195 |
} else if (firstPointValue != null && secondPointValue != null && thirdPointValue != null && fourthPointValue != null && fifthPointValue == null) { |
196 |
|
197 |
try {
|
198 |
|
199 |
int subtype = editingProviderServices.getSubType(featureStore);
|
200 |
|
201 |
//Draw tangents
|
202 |
Curve line1 |
203 |
= editingProviderServices.createLine(firstPointValue, |
204 |
secondPointValue, subtype); |
205 |
drawingStatus.addStatus(line1, auxiliaryLineSymbolEditing, "");
|
206 |
|
207 |
Curve line2 |
208 |
= editingProviderServices.createLine(thirdPointValue, |
209 |
fourthPointValue, subtype); |
210 |
drawingStatus.addStatus(line2, auxiliaryLineSymbolEditing, "");
|
211 |
|
212 |
//Draw circle
|
213 |
Circle circle |
214 |
= editingProviderServices.createCircle( |
215 |
firstPointValue, |
216 |
secondPointValue, |
217 |
thirdPointValue, |
218 |
fourthPointValue, |
219 |
mousePosition, |
220 |
subtype); |
221 |
|
222 |
drawingStatus.addStatus(circle, polygonSymbolEditing, "");
|
223 |
Point center = circle.getCenter();
|
224 |
|
225 |
//Draw the first four points
|
226 |
drawingStatus.addStatus(firstPointValue, auxiliaryPointSymbolEditing, "");
|
227 |
drawingStatus.addStatus(secondPointValue, auxiliaryPointSymbolEditing, "");
|
228 |
drawingStatus.addStatus(thirdPointValue, auxiliaryPointSymbolEditing, "");
|
229 |
drawingStatus.addStatus(fourthPointValue, auxiliaryPointSymbolEditing, "");
|
230 |
|
231 |
EuclideanLine2D tangent1 = euclideanManager.createLine2D(firstPointValue.getX(), firstPointValue.getY(), secondPointValue.getX(), secondPointValue.getY()); |
232 |
EuclideanLine2D tangent2 = euclideanManager.createLine2D(thirdPointValue.getX(), thirdPointValue.getY(), fourthPointValue.getX(), fourthPointValue.getY()); |
233 |
|
234 |
if (tangent1.isParallel(tangent2)) {
|
235 |
if (Objects.equals(tangent1.getYIntercept(), tangent2.getYIntercept())) { //Same lines |
236 |
//Draw line between mouse position and tangent1
|
237 |
EuclideanLine2D perp = tangent1.getPerpendicular(mousePosition.getX(), mousePosition.getY()); |
238 |
Point2D intersection = tangent1.getIntersection(perp);
|
239 |
drawingStatus.addStatus( |
240 |
editingProviderServices.createLine( |
241 |
mousePosition.getX(), |
242 |
mousePosition.getY(), |
243 |
intersection.getX(), |
244 |
intersection.getY(), |
245 |
subtype |
246 |
), auxiliaryLineSymbolEditing, "");
|
247 |
} |
248 |
} else {
|
249 |
//Draw line between center and intersection tangents (bisector passing through the center)
|
250 |
|
251 |
Point2D intersectionTangents = tangent1.getIntersection(tangent2);
|
252 |
|
253 |
drawingStatus.addStatus( |
254 |
editingProviderServices.createLine( |
255 |
intersectionTangents.getX(), |
256 |
intersectionTangents.getY(), |
257 |
center.getX(), |
258 |
center.getY(), |
259 |
subtype |
260 |
), |
261 |
auxiliaryLineSymbolEditing, |
262 |
"");
|
263 |
|
264 |
drawingStatus.addStatus( |
265 |
geomManager.createPoint( |
266 |
intersectionTangents.getX(), |
267 |
intersectionTangents.getY(), |
268 |
subtype |
269 |
), |
270 |
auxiliaryPointSymbolEditing, "");
|
271 |
|
272 |
//Draw perpendicular bisectors passing through the center
|
273 |
|
274 |
EuclideanLine2D[] bisectors = tangent1.getBisectors(tangent2);
|
275 |
|
276 |
double distance0 = bisectors[0].getDistance(mousePosition.getX(), mousePosition.getY()); |
277 |
double distance1 = bisectors[1].getDistance(mousePosition.getX(), mousePosition.getY()); |
278 |
|
279 |
EuclideanLine2D bisector = bisectors[0];
|
280 |
if (distance0 > distance1) {
|
281 |
bisector = bisectors[1];
|
282 |
} |
283 |
|
284 |
EuclideanLine2D perp = bisector.getPerpendicular(mousePosition.getX(), mousePosition.getY()); |
285 |
Point2D intersection;
|
286 |
if(Double.isInfinite(perp.getSlope())){ |
287 |
intersection = new Point2D.Double( |
288 |
mousePosition.getX(), |
289 |
center.getY()); |
290 |
} else {
|
291 |
intersection = bisector.getIntersection(perp); |
292 |
} |
293 |
|
294 |
drawingStatus.addStatus( |
295 |
editingProviderServices.createLine( |
296 |
intersection.getX(), |
297 |
intersection.getY(), |
298 |
center.getX(), |
299 |
center.getY(), |
300 |
subtype |
301 |
), |
302 |
auxiliaryLineSymbolEditing, |
303 |
"");
|
304 |
} |
305 |
|
306 |
//Draw line between mouse position and center
|
307 |
drawingStatus.addStatus( |
308 |
editingProviderServices.createLine( |
309 |
mousePosition, |
310 |
center, |
311 |
subtype), |
312 |
auxiliaryLineSymbolEditing, "");
|
313 |
|
314 |
//Draw normal lines (perpendicular to the tangents passing through the center)
|
315 |
EuclideanLine2D perpA = tangent1.getPerpendicular(center.getX(), center.getY()); |
316 |
|
317 |
Point intersectionA;
|
318 |
if (tangent1.getSlope() == 0.0) { |
319 |
intersectionA = editingProviderServices.createPoint(center.getX(), tangent1.getYIntercept(), subtype); |
320 |
} else {
|
321 |
Point2D intersection = tangent1.getIntersection(perpA);
|
322 |
intersectionA = editingProviderServices.createPoint(intersection.getX(), intersection.getY(), subtype); |
323 |
} |
324 |
drawingStatus.addStatus(intersectionA, auxiliaryPointSymbolEditing, "");
|
325 |
drawingStatus.addStatus(editingProviderServices.createLine(center, intersectionA, subtype), auxiliaryLineSymbolEditing, "");
|
326 |
|
327 |
|
328 |
EuclideanLine2D perpB = tangent2.getPerpendicular(center.getX(), center.getY()); |
329 |
|
330 |
Point intersectionB;
|
331 |
if (tangent2.getSlope() == 0.0) { |
332 |
intersectionB = editingProviderServices.createPoint(center.getX(), tangent2.getYIntercept(), subtype); |
333 |
} else {
|
334 |
Point2D intersection = tangent2.getIntersection(perpB);
|
335 |
intersectionB = editingProviderServices.createPoint(intersection.getX(), intersection.getY(), subtype); |
336 |
} |
337 |
drawingStatus.addStatus(intersectionB, auxiliaryPointSymbolEditing, "");
|
338 |
drawingStatus.addStatus(editingProviderServices.createLine(center, intersectionB, subtype), auxiliaryLineSymbolEditing, "");
|
339 |
|
340 |
//Draw point in mouse position
|
341 |
drawingStatus.addStatus(mousePosition, auxiliaryPointSymbolEditing, "");
|
342 |
|
343 |
} catch (Exception e) { |
344 |
throw new DrawServiceException(e); |
345 |
} |
346 |
} |
347 |
} |
348 |
return drawingStatus;
|
349 |
} |
350 |
|
351 |
public EditingServiceParameter next() {
|
352 |
if (values != null) { |
353 |
if (values.get(firstPoint) == null) { |
354 |
return firstPoint;
|
355 |
} else if (values.get(secondPoint) == null) { |
356 |
return secondPoint;
|
357 |
} else if (values.get(thirdPoint) == null) { |
358 |
return thirdPoint;
|
359 |
} else if (values.get(fourthPoint) == null) { |
360 |
return fourthPoint;
|
361 |
} else if (values.get(fifthPoint) == null) { |
362 |
return fifthPoint;
|
363 |
} |
364 |
} |
365 |
return null; |
366 |
} |
367 |
|
368 |
public void stop() { |
369 |
values.clear(); |
370 |
} |
371 |
|
372 |
private void validateAndInsertValue(EditingServiceParameter param, |
373 |
Object value) throws InvalidEntryException { |
374 |
if (param == firstPoint) {
|
375 |
if (value instanceof Point) { |
376 |
values.put(param, value); |
377 |
return;
|
378 |
} |
379 |
} else if (param == secondPoint) { |
380 |
if (value instanceof Point) { |
381 |
values.put(param, value); |
382 |
return;
|
383 |
} |
384 |
} else if (param == thirdPoint) { |
385 |
if (value instanceof Point) { |
386 |
values.put(param, value); |
387 |
return;
|
388 |
} |
389 |
} else if (param == fourthPoint) { |
390 |
if (value instanceof Point) { |
391 |
values.put(param, value); |
392 |
return;
|
393 |
} |
394 |
} else if (param == fifthPoint) { |
395 |
if (value instanceof Point) { |
396 |
values.put(param, value); |
397 |
return;
|
398 |
} |
399 |
} |
400 |
throw new InvalidEntryException(null); |
401 |
} |
402 |
|
403 |
@Override
|
404 |
public List<EditingServiceParameter> getParameters() { |
405 |
List<EditingServiceParameter> list
|
406 |
= new ArrayList<>(); |
407 |
list.add(firstPoint); |
408 |
list.add(secondPoint); |
409 |
list.add(thirdPoint); |
410 |
list.add(fourthPoint); |
411 |
list.add(fifthPoint); |
412 |
return list;
|
413 |
} |
414 |
|
415 |
@Override
|
416 |
public void setValue(EditingServiceParameter parameter, Object value) throws InvalidEntryException { |
417 |
validateAndInsertValue(parameter, value); |
418 |
} |
419 |
|
420 |
@Override
|
421 |
public void setValue(Object value) throws InvalidEntryException { |
422 |
EditingServiceParameter param = next(); |
423 |
validateAndInsertValue(param, value); |
424 |
} |
425 |
|
426 |
@Override
|
427 |
public void finishAndStore() throws FinishServiceException { |
428 |
EditingProviderServices editingProviderServices |
429 |
= (EditingProviderServices) getProviderServices(); |
430 |
Geometry geometry = finish(); |
431 |
editingProviderServices.insertGeometryIntoFeatureStore(geometry, |
432 |
featureStore); |
433 |
} |
434 |
|
435 |
public Geometry finish() throws FinishServiceException { |
436 |
|
437 |
final GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
438 |
|
439 |
Point firstPointValue = (Point) values.get(firstPoint); |
440 |
Point secondPointValue = (Point) values.get(secondPoint); |
441 |
Point thirdPointValue = (Point) values.get(thirdPoint); |
442 |
Point fourthPointValue = (Point) values.get(fourthPoint); |
443 |
Point fifthPointValue = (Point) values.get(fifthPoint); |
444 |
|
445 |
EditingProviderServices editingProviderServices |
446 |
= (EditingProviderServices) getProviderServices(); |
447 |
|
448 |
try {
|
449 |
int subtype = editingProviderServices.getSubType(featureStore);
|
450 |
GeometryType storeGeomType |
451 |
= editingProviderServices.getGeomType(featureStore); |
452 |
Geometry geometry |
453 |
= editingProviderServices.createCircle(firstPointValue, secondPointValue, thirdPointValue, fourthPointValue, fifthPointValue, subtype); |
454 |
|
455 |
if (storeGeomType.isTypeOf(MULTISURFACE)) {
|
456 |
MultiSurface multisurface; |
457 |
multisurface |
458 |
= geomManager.createMultiSurface( |
459 |
storeGeomType.getSubType()); |
460 |
multisurface.addSurface((Circle) geometry); |
461 |
return multisurface;
|
462 |
} |
463 |
|
464 |
return geometry;
|
465 |
} catch (Exception e) { |
466 |
throw new FinishServiceException(e); |
467 |
} |
468 |
} |
469 |
|
470 |
@Override
|
471 |
public void start() throws StartServiceException { |
472 |
this.values = new HashMap<>(); |
473 |
} |
474 |
|
475 |
@Override
|
476 |
public String getName() { |
477 |
return Circle2Tg1PEditingProviderFactory.PROVIDER_NAME;
|
478 |
} |
479 |
|
480 |
} |