Statistics
| Revision:

gvsig-vectorediting / org.gvsig.vectorediting / trunk / org.gvsig.vectorediting / org.gvsig.vectorediting.lib / org.gvsig.vectorediting.lib.prov / org.gvsig.vectorediting.lib.prov.ellipse / src / main / java / org / gvsig / vectorediting / lib / prov / ellipse / EllipseEditingProvider.java @ 228

History | View | Annotate | Download (11.8 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.ellipse;
26

    
27
import java.util.ArrayList;
28
import java.util.HashMap;
29
import java.util.List;
30
import java.util.Map;
31

    
32
import org.gvsig.fmap.dal.feature.FeatureStore;
33
import org.gvsig.fmap.geom.Geometry;
34
import org.gvsig.fmap.geom.primitive.Curve;
35
import org.gvsig.fmap.geom.primitive.Point;
36
import org.gvsig.tools.dynobject.DynObject;
37
import org.gvsig.tools.service.spi.ProviderServices;
38
import org.gvsig.vectorediting.lib.api.DrawingStatus;
39
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
40
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
41
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
42
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
43
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
44
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
45
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
46
import org.gvsig.vectorediting.lib.spi.DefaultDrawingStatus;
47
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
48
import org.gvsig.vectorediting.lib.spi.EditingProvider;
49
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
50
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
51

    
52
public class EllipseEditingProvider extends AbstractEditingProvider implements
53
EditingProvider {
54

    
55
    protected EditingServiceParameter firstPointAxisA;
56

    
57
    protected EditingServiceParameter secondPointAxisA;
58

    
59
    protected EditingServiceParameter lengthAxisB;
60

    
61
    protected Map<EditingServiceParameter, Object> values;
62

    
63
    protected FeatureStore featureStore;
64

    
65
    public EllipseEditingProvider(ProviderServices providerServices,
66
        DynObject parameters) {
67
        super(providerServices);
68

    
69
        this.featureStore =
70
            (FeatureStore) parameters
71
            .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
72

    
73
        this.firstPointAxisA =
74
            new DefaultEditingServiceParameter("first_point_A_axis",
75
                "first_point_A_axis", EditingServiceParameter.TYPE.POSITION);
76

    
77
        this.secondPointAxisA =
78
            new DefaultEditingServiceParameter("second_point_A_axis",
79
                "second_point_A_axis", EditingServiceParameter.TYPE.POSITION);
80

    
81
        this.lengthAxisB =
82
            new DefaultEditingServiceParameter("length_of_B_axis",
83
                "length_of_B_axis", EditingServiceParameter.TYPE.POSITION,
84
                EditingServiceParameter.TYPE.VALUE);
85
    }
86

    
87
    public EditingServiceParameter next() {
88
        if (values.get(firstPointAxisA) == null) {
89
            return firstPointAxisA;
90
        } else if (values.get(secondPointAxisA) == null) {
91
            return secondPointAxisA;
92
        } else if (values.get(lengthAxisB) == null) {
93
            return lengthAxisB;
94
        }
95
        return null;
96
    }
97

    
98
    public DrawingStatus getDrawingStatus(Point mousePosition)
99
        throws DrawServiceException {
100
        DefaultDrawingStatus drawingStatus = new DefaultDrawingStatus();
101

    
102
        if (values != null) {
103
            EditingProviderServices editingServiceProvider =
104
                (EditingProviderServices) getProviderServices();
105

    
106
            Point firstPointAxis1Value = (Point) values.get(firstPointAxisA);
107
            Point secondPointAxis1Value = (Point) values.get(secondPointAxisA);
108

    
109
            if (firstPointAxis1Value != null) {
110

    
111
                if (secondPointAxis1Value == null) {
112

    
113
                    try {
114
                        int subtype =
115
                            editingServiceProvider.getSubType(featureStore);
116
                        Curve line =
117
                            editingServiceProvider.createLine(
118
                                firstPointAxis1Value, mousePosition, subtype);
119

    
120
                        drawingStatus.addGeometry(line);
121
                        return drawingStatus;
122
                    } catch (Exception e) {
123
                        throw new DrawServiceException(e);
124
                    }
125

    
126
                } else {
127

    
128
                    try {
129
                        int subtype =
130
                            editingServiceProvider.getSubType(featureStore);
131
                        Point center =
132
                            editingServiceProvider.getMidPoint(
133
                                firstPointAxis1Value, secondPointAxis1Value,
134
                                subtype);
135
                        double distance = center.distance(mousePosition);
136

    
137
                        Curve aAxis =
138
                            editingServiceProvider.createLine(
139
                                firstPointAxis1Value, secondPointAxis1Value,
140
                                subtype);
141
                        drawingStatus.addGeometry(aAxis);
142

    
143
                        Double[] lineParams =
144
                            editingServiceProvider.getLineParams(
145
                                firstPointAxis1Value, secondPointAxis1Value);
146
                        Point[] bisector =
147
                            editingServiceProvider.getPerpendicular(
148
                                lineParams[0], lineParams[1], center, subtype);
149
                        Double[] bisectorParams =
150
                            editingServiceProvider.getLineParams(bisector[0],
151
                                bisector[1]);
152

    
153
                        double m = bisectorParams[0]; // Line slope
154
                        double y0 = bisectorParams[1]; // Y-Intercept line
155
                        double mp2 = Math.pow(m, 2); // slope?
156

    
157
                        Curve line;
158
                        if (!Double.isInfinite(m)) {
159
                            double a = 1 + mp2;
160
                            double b = (-2 - (2 * mp2)) * center.getX();
161
                            double c =
162
                                ((1 + mp2) * Math.pow(center.getX(), 2))
163
                                - Math.pow(distance, 2);
164

    
165
                            double x1 =
166
                                (-b + Math.sqrt((Math.pow(b, 2) - (4 * a * c))))
167
                                / (2 * a);
168
                            double y1 = (m * x1) + y0;
169

    
170
                            double x2 =
171
                                (-b - Math.sqrt((Math.pow(b, 2) - (4 * a * c))))
172
                                / (2 * a);
173
                            double y2 = (m * x2) + y0;
174

    
175
                            line =
176
                                editingServiceProvider.createLine(x1, y1, x2,
177
                                    y2, subtype);
178

    
179
                        } else {
180
                            double x1 = center.getX();
181
                            double y1 = center.getY() + distance;
182
                            double y2 = center.getY() - distance;
183

    
184
                            line =
185
                                editingServiceProvider.createLine(x1, y1, x1,
186
                                    y2, subtype);
187
                        }
188

    
189
                        drawingStatus.addGeometry(line);
190
                    } catch (Exception e) {
191
                        throw new DrawServiceException(e);
192
                    }
193
                }
194
            }
195
        }
196
        return drawingStatus;
197
    }
198

    
199
    public void stop() throws StopServiceException {
200
        if (values != null) {
201
            values.clear();
202
        }
203

    
204
    }
205

    
206
    public List<EditingServiceParameter> getParameters() {
207
        List<EditingServiceParameter> list =
208
            new ArrayList<EditingServiceParameter>();
209
        list.add(firstPointAxisA);
210
        list.add(secondPointAxisA);
211
        list.add(lengthAxisB);
212
        return list;
213
    }
214

    
215
    public void setValue(Object value) throws InvalidEntryException {
216
        EditingServiceParameter parameter = this.next();
217
        validateAndInsertValue(parameter, value);
218
    }
219

    
220
    private void validateAndInsertValue(EditingServiceParameter param,
221
        Object value) throws InvalidEntryException {
222

    
223
        if ((param == firstPointAxisA) || (param == secondPointAxisA)) {
224
            if (value instanceof Point) {
225
                values.put(param, value);
226
                return;
227
            }
228

    
229
        } else if (param == lengthAxisB) {
230
            if (value instanceof Point) {
231
                Double length = null;
232

    
233
                try {
234
                    EditingProviderServices editingProviderServices =
235
                        (EditingProviderServices) getProviderServices();
236

    
237
                    Point firstPointAxis1Value =
238
                        (Point) values.get(firstPointAxisA);
239
                    Point firstPointAxis2Value =
240
                        (Point) values.get(secondPointAxisA);
241
                    int subtype =
242
                        editingProviderServices.getSubType(featureStore);
243
                    Point center =
244
                        editingProviderServices
245
                        .getMidPoint(firstPointAxis1Value,
246
                            firstPointAxis2Value, subtype);
247
                    length = ((Point) value).distance(center) * 2;
248

    
249
                } catch (Exception e) {
250
                    throw new InvalidEntryException(e);
251
                }
252

    
253
                if (length != null) {
254
                    values.put(param, length);
255
                    return;
256
                }
257

    
258
            } else if (value instanceof Double) {
259

    
260
                if ((((Double) value) - 0.01) > 0) {
261
                    value = (value);
262
                    values.put(param, value);
263
                    return;
264
                }
265
            }
266
        }
267
    }
268

    
269
    public Geometry finish() throws FinishServiceException {
270
        if (values != null) {
271

    
272
            EditingProviderServices editingProviderService =
273
                (EditingProviderServices) getProviderServices();
274

    
275
            Point firstPointAxis1Value = (Point) values.get(firstPointAxisA);
276
            Point secondPointAxis1Value = (Point) values.get(secondPointAxisA);
277
            double lengthAxis2Value = (Double) values.get(lengthAxisB);
278

    
279
            if ((firstPointAxis1Value != null)
280
                && (secondPointAxis1Value != null) && (lengthAxis2Value > 0)) {
281

    
282
                try {
283
                    int subtype =
284
                        editingProviderService.getSubType(featureStore);
285
                    return editingProviderService.createFilledEllipse(
286
                        firstPointAxis1Value, secondPointAxis1Value,
287
                        lengthAxis2Value / 2, subtype);
288
                } catch (Exception e) {
289
                    throw new FinishServiceException(e);
290
                }
291
            }
292
        }
293
        return null;
294
    }
295

    
296
    public void finishAndStore() throws FinishServiceException {
297
        EditingProviderServices editingProviderServices =
298
            (EditingProviderServices) getProviderServices();
299
        Geometry geometry = finish();
300
        editingProviderServices.insertGeometryIntoFeatureStore(geometry,
301
            featureStore);
302
    }
303

    
304
    public void start() throws StartServiceException {
305
        values = new HashMap<EditingServiceParameter, Object>();
306
    }
307

    
308
    public String getName() {
309
        return EllipseEditingProviderFactory.PROVIDER_NAME;
310
    }
311

    
312
}