Revision 1663

View differences:

org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/Bezier.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7
import org.gvsig.fmap.geom.primitive.Point;
8

  
9
public class Bezier extends ControlCurve {
10

  
11
    public Bezier(final Line line) {
12
        super(line);
13
    }
14

  
15
    // the basis function for a Bezier spline
16
    static float b(final int i, final float t) {
17
        switch (i) {
18
        case 0:
19
            return (1 - t) * (1 - t) * (1 - t);
20
        case 1:
21
            return 3 * t * (1 - t) * (1 - t);
22
        case 2:
23
            return 3 * t * t * (1 - t);
24
        case 3:
25
            return t * t * t;
26
        }
27
        return 0; // we only get here if an invalid i is specified
28
    }
29

  
30
    // evaluate a point on the B spline
31
    Point p(final int i, final float t, int subtype) throws CreateGeometryException {
32
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
33
        float px = 0;
34
        float py = 0;
35
        for (int j = 0; j <= 3; j++) {
36
            px += b(j, t) * m_X[i + j];
37
            py += b(j, t) * m_Y[i + j];
38
        }
39
        return geomManager.createPoint((int) Math.round(px), (int) Math.round(py),subtype);
40
    }
41

  
42
    @Override
43
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
44
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
45
        Line line = geomManager.createLine(subtype);
46

  
47
        Point q = p(0, 0, subtype);
48
        line.addVertex(q);
49
        for (int i = 0; i < m_X.length - 3; i += 3) {
50
            for (int j = 1; j <= iSteps; j++) {
51
                q = p(i, j / (float) iSteps, subtype);
52
                line.addVertex(q);
53
            }
54
        }
55
        return line;
56
    }
57
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingProvider.java
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.smoothline;
25

  
26
import java.util.ArrayList;
27
import java.util.Enumeration;
28
import java.util.HashMap;
29
import java.util.LinkedHashMap;
30
import java.util.List;
31
import java.util.Map;
32

  
33
import org.gvsig.fmap.dal.exception.DataException;
34
import org.gvsig.fmap.dal.feature.EditableFeature;
35
import org.gvsig.fmap.dal.feature.Feature;
36
import org.gvsig.fmap.dal.feature.FeatureSelection;
37
import org.gvsig.fmap.dal.feature.FeatureStore;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryException;
40
import org.gvsig.fmap.geom.GeometryLocator;
41
import org.gvsig.fmap.geom.GeometryManager;
42
import org.gvsig.fmap.geom.aggregate.MultiCurve;
43
import org.gvsig.fmap.geom.aggregate.MultiSurface;
44
import org.gvsig.fmap.geom.exception.CreateGeometryException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationException;
46
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
47
import org.gvsig.fmap.geom.primitive.Arc;
48
import org.gvsig.fmap.geom.primitive.Curve;
49
import org.gvsig.fmap.geom.primitive.Line;
50
import org.gvsig.fmap.geom.primitive.Point;
51
import org.gvsig.fmap.geom.primitive.Polygon;
52
import org.gvsig.fmap.geom.primitive.Spline;
53
import org.gvsig.fmap.geom.primitive.Surface;
54
import org.gvsig.fmap.geom.type.GeometryType;
55
import org.gvsig.tools.ToolsLocator;
56
import org.gvsig.tools.dynobject.DynObject;
57
import org.gvsig.tools.exception.BaseException;
58
import org.gvsig.tools.i18n.I18nManager;
59
import org.gvsig.tools.service.spi.ProviderServices;
60
import org.gvsig.tools.visitor.VisitCanceledException;
61
import org.gvsig.tools.visitor.Visitor;
62
import org.gvsig.vectorediting.lib.api.DrawingStatus;
63
import org.gvsig.vectorediting.lib.api.EditingServiceParameter;
64
import org.gvsig.vectorediting.lib.api.EditingServiceParameter.TYPE;
65
import org.gvsig.vectorediting.lib.api.exceptions.DrawServiceException;
66
import org.gvsig.vectorediting.lib.api.exceptions.FinishServiceException;
67
import org.gvsig.vectorediting.lib.api.exceptions.InvalidEntryException;
68
import org.gvsig.vectorediting.lib.api.exceptions.StartServiceException;
69
import org.gvsig.vectorediting.lib.api.exceptions.StopServiceException;
70
import org.gvsig.vectorediting.lib.spi.AbstractEditingProvider;
71
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceParameter;
72
import org.gvsig.vectorediting.lib.spi.EditingProvider;
73
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
74
import org.gvsig.vectorediting.lib.spi.EditingProviderServices;
75

  
76
/**
77
 * @author fdiaz
78
 *
79
 */
80
public class SmoothLineEditingProvider extends AbstractEditingProvider implements
81
    EditingProvider {
82

  
83
    private EditingServiceParameter selection;
84

  
85
    private EditingServiceParameter steps;
86

  
87
    private EditingServiceParameter algorithm;
88

  
89
    private FeatureStore featureStore;
90

  
91
    private Map<EditingServiceParameter, Object> values;
92

  
93
    protected Map<String, String> options;
94

  
95
    public static final String    NATURAL_CUBIC_SPLINES = "natural_cubic_splines";
96
    public static final String    BEZIER_CURVES         = "bezier_curves";
97
    public static final String    BSPLINES              = "b_splines";
98

  
99
    /**
100
     * Default constructor.
101
     *
102
     * @param providerServices
103
     *            available services for this provider
104
     * @param parameters
105
     *            of this provider
106
     */
107
    public SmoothLineEditingProvider(DynObject parameters,
108
        ProviderServices services) {
109
        super(services);
110

  
111
        this.featureStore =
112
            (FeatureStore) parameters
113
                .getDynValue(EditingProviderFactory.FEATURE_STORE_FIELD);
114

  
115
        this.selection =
116
            new DefaultEditingServiceParameter("selection", "selection",
117
                TYPE.SELECTION);
118

  
119
        this.steps =
120
            new DefaultEditingServiceParameter("steps", "intermediate_steps_1_9",
121
                TYPE.VALUE);
122

  
123
        I18nManager i18nManager = ToolsLocator.getI18nManager();
124
//        "Natural cubic splines","Bezier curves", "B splines"
125
        options = new LinkedHashMap<String, String>();
126
        options.put(i18nManager.getTranslation("key_natural_cubic_splines"), NATURAL_CUBIC_SPLINES);
127
        options.put(i18nManager.getTranslation("key_bezier_curves"),BEZIER_CURVES);
128
        options.put(i18nManager.getTranslation("key_b_splines"),BSPLINES);
129

  
130

  
131
        EditingProviderServices editingProviderServices =
132
            (EditingProviderServices) getProviderServices();
133

  
134
        String consoleMsg =
135
            editingProviderServices.makeConsoleMessage(null, options);
136

  
137
        this.algorithm =
138
            new DefaultEditingServiceParameter("algorithm", consoleMsg, options,
139
                EditingServiceParameter.TYPE.OPTION);
140
}
141

  
142
    public EditingServiceParameter next() {
143
        if (values.get(selection) == null) {
144
            return selection;
145
        } else if (values.get(steps) == null) {
146
            return steps;
147
        } else if (values.get(algorithm) == null) {
148
            return algorithm;
149
        }
150
        return null;
151
    }
152

  
153
    public DrawingStatus getDrawingStatus(Point mousePosition)
154
        throws DrawServiceException {
155
        return null;
156
    }
157

  
158
    public void stop() throws StopServiceException {
159
        if (values != null) {
160
            values.clear();
161
        }
162
    }
163

  
164
    public List<EditingServiceParameter> getParameters() {
165
        List<EditingServiceParameter> parameters =
166
            new ArrayList<EditingServiceParameter>();
167
        parameters.add(selection);
168
        parameters.add(steps);
169
        parameters.add(algorithm);
170
        return parameters;
171
    }
172

  
173
    public void setValue(Object value) throws InvalidEntryException {
174
        EditingServiceParameter parameter = next();
175
        validateAndInsertValue(parameter, value);
176
    }
177

  
178
    private void validateAndInsertValue(EditingServiceParameter parameter,
179
        Object value) throws InvalidEntryException {
180

  
181
        if (parameter == selection) {
182

  
183
            FeatureSelection featureSelection = (FeatureSelection) value;
184

  
185
            if (featureSelection.getSelectedCount() > 0) {
186
                try {
187
                    featureSelection.accept(new Visitor() {
188

  
189
                        public void visit(Object obj)
190
                            throws VisitCanceledException, BaseException {
191

  
192
                            Feature feature = (Feature) obj;
193
                            Geometry geometry = feature.getDefaultGeometry();
194
                            GeometryType geometryType =
195
                                geometry.getGeometryType();
196

  
197
                            if (!geometryType.isTypeOf(Geometry.TYPES.CURVE)
198
                                && !geometryType.isTypeOf(Geometry.TYPES.MULTICURVE)) {
199

  
200
                                throw new InvalidEntryException(null);
201
                            }
202
                        }
203
                    });
204

  
205
                    values.put(selection, value);
206
                    return;
207
                } catch (BaseException e) {
208
                    throw new InvalidEntryException(e);
209
                }
210
            }
211
        } else if (parameter == steps) {
212
            if (value instanceof Double) {
213
                Integer stepsValue = ((Double) value).intValue();
214
                if (stepsValue > 0 && stepsValue < 10 ) {
215
                    values.put(steps, stepsValue);
216
                    return;
217
                }
218
            }
219
        }  else if (parameter == algorithm) {
220

  
221
            if (value instanceof String) {
222

  
223
                I18nManager i18nManager = ToolsLocator.getI18nManager();
224
                String option = (String) value;
225

  
226
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_natural_cubic_splines"))){
227
                    values.put(parameter, "key_natural_cubic_splines");
228
                    return;
229
                }
230
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_bezier_curves"))){
231
                    values.put(parameter, "key_bezier_curves");
232
                    return;
233
                }
234
                if (option.equalsIgnoreCase(i18nManager.getTranslation("key_b_splines"))){
235
                    values.put(parameter, "key_b_splines");
236
                    return;
237
                }
238
            }
239
            throw new InvalidEntryException(null);
240

  
241
        }
242
        throw new InvalidEntryException(null);
243
    }
244

  
245
    public Geometry finish() throws FinishServiceException {
246
        return null;
247
    }
248

  
249
    public void finishAndStore() throws FinishServiceException {
250

  
251
        if (values != null) {
252

  
253
            FeatureSelection featureSelection =
254
                (FeatureSelection) values.get(selection);
255
            ToolsLocator.getDisposableManager().bind(featureSelection);
256

  
257

  
258
            final Integer stepsValue = (Integer) values.get(steps);
259

  
260
            final String algorithmValue = (String)values.get(algorithm);
261

  
262
            final EditingProviderServices editingProviderServices =
263
                (EditingProviderServices) getProviderServices();
264

  
265
            try {
266
                final int subtype =
267
                    editingProviderServices.getSubType(featureStore);
268

  
269
                featureSelection.accept(new Visitor() {
270

  
271
                    public void visit(Object obj)
272
                        throws VisitCanceledException, BaseException {
273

  
274
                        Feature feature = (Feature) obj;
275
                        Geometry geometry = feature.getDefaultGeometry();
276
                        GeometryType geoType = geometry.getGeometryType();
277

  
278
                        if (geoType.isTypeOf(AGGREGATE)) {
279

  
280
                            MultiCurve multiCurveToSmooth =
281
                                (MultiCurve) geometry;
282

  
283
                            MultiCurve multiCurveSmoothed =
284
                                GeometryLocator.getGeometryManager()
285
                                    .createMultiCurve(subtype);
286

  
287
                            for (int i = 0; i < multiCurveToSmooth
288
                                .getPrimitivesNumber(); i++) {
289

  
290
                                Curve curveToSmooth =
291
                                    (Curve) multiCurveToSmooth
292
                                        .getPrimitiveAt(i);
293
                                curveToSmooth =
294
                                    smoothLine(curveToSmooth, stepsValue, algorithmValue);
295
                                multiCurveSmoothed.addCurve(curveToSmooth);
296
                                geometry = multiCurveSmoothed;
297
                            }
298
                        } else {
299
                            Curve curveToSmooth = (Curve) geometry;
300
                            curveToSmooth =
301
                                smoothLine(curveToSmooth, stepsValue, algorithmValue);
302
                            geometry = curveToSmooth;
303
                        }
304

  
305
                        EditableFeature eFeature = feature.getEditable();
306
                        eFeature.setDefaultGeometry(geometry);
307
                        editingProviderServices.updateFeatureInFeatureStore(
308
                            eFeature, featureStore);
309
                    }
310
                });
311

  
312
                featureStore.getFeatureSelection().deselectAll();
313
                featureSelection.dispose();
314

  
315
            } catch (BaseException e) {
316
                throw new FinishServiceException(e);
317
            }
318
        }
319
    }
320

  
321
    private Line smoothLine(Curve curveToSmooth, Integer steps, String algorithm) throws DataException, CreateGeometryException{
322

  
323
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
324
        EditingProviderServices editingProviderServices =
325
            (EditingProviderServices) getProviderServices();
326

  
327
        int subtype = editingProviderServices.getSubType(featureStore);
328
        Line lineToSmooth = null;
329

  
330
        if (curveToSmooth instanceof Arc || curveToSmooth instanceof Spline) {
331
            lineToSmooth = geometryManager.createLine(curveToSmooth.getGeometryType().getSubType());
332
            lineToSmooth.setGeneralPath(curveToSmooth.getGeneralPath());
333

  
334
        } else {
335
            lineToSmooth = (Line)curveToSmooth;
336
        }
337

  
338
        ControlCurve operation;
339
        if (algorithm.equalsIgnoreCase("key_natural_cubic_splines")){
340
            operation = new NatCubic(lineToSmooth);
341
        } else if (algorithm.equalsIgnoreCase("key_bezier_curves")){
342
            operation = new Bezier(lineToSmooth);
343
        } else if (algorithm.equalsIgnoreCase("key_b_splines")){
344
            operation = new BSpline(lineToSmooth);
345
        } else {
346
            return null;
347
        }
348
        return operation.getSmoothedLine(steps, subtype);
349

  
350
    }
351

  
352

  
353
    private Line simplifyLine(Curve curveToSimplify, Double toleranceValue)
354
        throws DataException, CreateGeometryException,
355
        GeometryOperationNotSupportedException, GeometryOperationException {
356

  
357
        EditingProviderServices editingProviderServices =
358
            (EditingProviderServices) getProviderServices();
359

  
360
        int subtype = editingProviderServices.getSubType(featureStore);
361

  
362
        Line simplifiedLine =
363
            (Line) GeometryLocator.getGeometryManager().createLine(subtype);
364

  
365
        // Add first vertex
366
        simplifiedLine.addVertex(curveToSimplify.getVertex(0));
367

  
368
        // Iterate over vertices
369
        for (int i = 0; i < curveToSimplify.getNumVertices() - 2; i++) {
370

  
371
            Point vertex = curveToSimplify.getVertex(i);
372
            Point nextVertex = curveToSimplify.getVertex(i + 1);
373
            Point nextNextVertex = curveToSimplify.getVertex(i + 2);
374

  
375
            // Creates line between i vertex and i+2 vertex
376
            Line tmpLine =
377
                editingProviderServices.createLine(vertex, nextNextVertex,
378
                    subtype);
379

  
380
            // If distance between line and i+1 vertex is bigger than tolerance
381
            // add point to simplifiedLine, else skip vertex
382
            if (tmpLine.distance(nextVertex) > toleranceValue) {
383
                simplifiedLine.addVertex(nextVertex);
384
            } else {
385
                i++;
386
            }
387
        }
388

  
389
        // Add last vertex
390
        simplifiedLine.addVertex(curveToSimplify.getVertex(curveToSimplify
391
            .getNumVertices() - 1));
392

  
393
        return simplifiedLine;
394
    }
395

  
396
    public void start() throws StartServiceException, InvalidEntryException {
397
        values = new HashMap<EditingServiceParameter, Object>();
398

  
399
        FeatureSelection selected = null;
400
        if (featureStore != null) {
401
            try {
402
                selected = featureStore.getFeatureSelection();
403
            } catch (DataException e) {
404
                throw new StartServiceException(e);
405
            }
406
            if (selected.getSelectedCount() > 0) {
407
                try {
408
                    setValue(selected);
409
                } catch (InvalidEntryException e) {
410
                    throw new InvalidEntryException(e);
411
                }
412
            }
413
        }
414
    }
415

  
416
    public String getName() {
417
        return SmoothLineEditingProviderFactory.PROVIDER_NAME;
418
    }
419

  
420
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/ControlCurve.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.exception.CreateGeometryException;
4
import org.gvsig.fmap.geom.primitive.Line;
5

  
6
public abstract class ControlCurve {
7

  
8
    protected double[] m_X;
9
    protected double[] m_Y;
10
    protected Line line;
11

  
12
    public ControlCurve(final Line line) {
13

  
14
       m_X = new double[line.getNumVertices()];
15
       m_Y = new double[line.getNumVertices()];
16
       for (int i = 0; i < line.getNumVertices(); i++) {
17
          m_X[i] = line.getVertex(i).getX();
18
          m_Y[i] = line.getVertex(i).getY();
19
       }
20
       this.line = line;
21

  
22
    }
23

  
24
    public abstract Line getSmoothedLine(int iSteps, int subtype) throws CreateGeometryException;
25

  
26
 }
27

  
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/Cubic.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3

  
4
public class Cubic {
5

  
6
    double a, b, c, d; /* a + b*u + c*u^2 +d*u^3 */
7

  
8

  
9
    public Cubic(final double a,
10
                 final double b,
11
                 final double c,
12
                 final double d) {
13
       this.a = a;
14
       this.b = b;
15
       this.c = c;
16
       this.d = d;
17
    }
18

  
19
    /** evaluate cubic */
20
    public double eval(final double u) {
21
       return (((d * u) + c) * u + b) * u + a;
22
    }
23
 }
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/NatCubic.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7

  
8
public class NatCubic extends ControlCurve {
9

  
10
    /*
11
     * calculates the natural cubic spline that interpolates
12
     * y[0], y[1], ... y[n]
13
     * The first segment is returned as
14
     * C[0].a + C[0].b*u + C[0].c*u^2 + C[0].d*u^3 0<=u <1
15
     * the other segments are in C[1], C[2], ... C[n-1]
16
     */
17

  
18
    public NatCubic(final Line line) {
19
        super(line);
20
    }
21

  
22
    Cubic[] calcNaturalCubic(final int n, final double[] x) {
23
        final double[] gamma = new double[n + 1];
24
        final double[] delta = new double[n + 1];
25
        final double[] D = new double[n + 1];
26
        int i;
27
        /*
28
         * We solve the equation
29
         * [2 1 ] [D[0]] [3(x[1] - x[0]) ]
30
         * |1 4 1 | |D[1]| |3(x[2] - x[0]) |
31
         * | 1 4 1 | | . | = | . |
32
         * | ..... | | . | | . |
33
         * | 1 4 1| | . | |3(x[n] - x[n-2])|
34
         * [ 1 2] [D[n]] [3(x[n] - x[n-1])]
35
         *
36
         * by using row operations to convert the matrix to upper triangular
37
         * and then back sustitution. The D[i] are the derivatives at the knots.
38
         */
39

  
40
        gamma[0] = 1.0f / 2.0f;
41
        for (i = 1; i < n; i++) {
42
            gamma[i] = 1 / (4 - gamma[i - 1]);
43
        }
44
        gamma[n] = 1 / (2 - gamma[n - 1]);
45

  
46
        delta[0] = 3 * (x[1] - x[0]) * gamma[0];
47
        for (i = 1; i < n; i++) {
48
            delta[i] = (3 * (x[i + 1] - x[i - 1]) - delta[i - 1]) * gamma[i];
49
        }
50
        delta[n] = (3 * (x[n] - x[n - 1]) - delta[n - 1]) * gamma[n];
51

  
52
        D[n] = delta[n];
53
        for (i = n - 1; i >= 0; i--) {
54
            D[i] = delta[i] - gamma[i] * D[i + 1];
55
        }
56

  
57
        /* now compute the coefficients of the cubics */
58
        final Cubic[] C = new Cubic[n];
59
        for (i = 0; i < n; i++) {
60
            C[i] =
61
                new Cubic(x[i], D[i], 3 * (x[i + 1] - x[i]) - 2 * D[i]
62
                    - D[i + 1], 2 * (x[i] - x[i + 1]) + D[i] + D[i + 1]);
63
        }
64
        return C;
65
    }
66

  
67
    @Override
68
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
69
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
70

  
71
        if (m_X.length >= 2) {
72
            final Cubic[] X = calcNaturalCubic(m_X.length - 1, m_X);
73
            final Cubic[] Y = calcNaturalCubic(m_Y.length - 1, m_Y);
74

  
75
            Line line = geomManager.createLine(subtype);
76
            /*
77
             * very crude technique - just break each segment up into steps
78
             * lines
79
             */
80
            line.addVertex((int) Math.round(X[0].eval(0)),
81
                (int) Math.round(Y[0].eval(0)));
82

  
83
            for (int i = 0; i < X.length; i++) {
84
                for (int j = 1; j <= iSteps; j++) {
85
                    final float u = j / (float) iSteps;
86
                    line.addVertex(X[i].eval(u), Y[i].eval(u));
87
                }
88
            }
89

  
90
            return line;
91

  
92
        } else {
93
            return this.line;
94
        }
95
    }
96
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingProviderFactory.java
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.smoothline;
25

  
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.dynobject.DynClass;
29
import org.gvsig.tools.dynobject.DynObject;
30
import org.gvsig.tools.service.spi.AbstractProviderFactory;
31
import org.gvsig.tools.service.spi.Provider;
32
import org.gvsig.tools.service.spi.ProviderServices;
33
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
34
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceinfo;
35
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
36

  
37
/**
38
 * @author fdiaz
39
 *
40
 */
41
public class SmoothLineEditingProviderFactory extends AbstractProviderFactory
42
    implements EditingProviderFactory {
43

  
44
    public static final String PROVIDER_NAME = "modify-smooth-line";
45

  
46
    private final static String PROVIDER_DESCRIPTION =
47
        "Smooth lines";
48

  
49
    public void initialize() {
50
    }
51

  
52
    public EditingServiceInfo getServiceInfo() {
53
        EditingServiceInfo serviceInfo =
54
            new DefaultEditingServiceinfo(PROVIDER_NAME, "", false, null,
55
                new int[] { 
56
                    Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE, 
57
                    Geometry.TYPES.CURVE, Geometry.TYPES.MULTICURVE 
58
                });
59

  
60
        return serviceInfo;
61
    }
62

  
63
    @Override
64
    protected Provider doCreate(DynObject parameters, ProviderServices services) {
65
        return new SmoothLineEditingProvider(parameters, services);
66
    }
67

  
68
    @Override
69
    public DynObject createParameters() {
70
        DynObject dynobject = super.createParameters();
71
        dynobject.setDynValue(PROVIDER_NAME_FIELD, PROVIDER_NAME);
72
        return dynobject;
73
    }
74

  
75
    @Override
76
    protected DynClass createParametersDynClass() {
77
        DynClass dynclass =
78
            ToolsLocator.getDynObjectManager().createDynClass(PROVIDER_NAME,
79
                PROVIDER_DESCRIPTION);
80

  
81
        dynclass.addDynFieldString(PROVIDER_NAME_FIELD);
82
        dynclass.addDynFieldObject(FEATURE_STORE_FIELD);
83
        dynclass.addDynFieldObject(MAPCONTEXT_FIELD);
84

  
85
        return dynclass;
86
    }
87

  
88
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/SmoothLineEditingLibrary.java
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.smoothline;
26

  
27
import org.gvsig.tools.ToolsLocator;
28
import org.gvsig.tools.i18n.I18nManager;
29
import org.gvsig.tools.library.AbstractLibrary;
30
import org.gvsig.tools.library.Library;
31
import org.gvsig.tools.library.LibraryException;
32
import org.gvsig.vectorediting.lib.api.EditingLibrary;
33
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
34
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
35

  
36

  
37
/**
38
 * @author fdiaz
39
 *
40
 */
41
public class SmoothLineEditingLibrary extends AbstractLibrary implements Library {
42

  
43
    @Override
44
    public void doRegistration() {
45
        registerAsServiceOf(EditingLibrary.class);
46
    }
47

  
48
    @Override
49
    protected void doInitialize() throws LibraryException {
50
    }
51

  
52
    @Override
53
    protected void doPostInitialize() throws LibraryException {
54
        EditingProviderManager manager =
55
            EditingProviderLocator.getProviderManager();
56

  
57
        manager.addProviderFactory(new SmoothLineEditingProviderFactory());
58

  
59
        manager.registerIcon("vectorediting-tools", "modify-smooth-line", this
60
            .getClass().getClassLoader(), this.getClass().getName());
61

  
62
        registerTranslations();
63
    }
64

  
65
    private void registerTranslations() {
66
        I18nManager manager = ToolsLocator.getI18nManager();
67
        manager.addResourceFamily("i18n.text",
68
            this.getClass().getClassLoader(), "smooth-line-editing");
69
    }
70

  
71
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/java/org/gvsig/vectorediting/lib/prov/smoothline/BSpline.java
1
package org.gvsig.vectorediting.lib.prov.smoothline;
2

  
3
import org.gvsig.fmap.geom.GeometryLocator;
4
import org.gvsig.fmap.geom.GeometryManager;
5
import org.gvsig.fmap.geom.exception.CreateGeometryException;
6
import org.gvsig.fmap.geom.primitive.Line;
7
import org.gvsig.fmap.geom.primitive.Point;
8

  
9

  
10
public class BSpline extends ControlCurve {
11

  
12
    public BSpline(final Line line) {
13
        super(line);
14
    }
15

  
16
    // the basis function for a cubic B spline
17
    float b(final int i, float t) {
18
        switch (i) {
19
        case -2:
20
            return (((-t + 3) * t - 3) * t + 1) / 6;
21
        case -1:
22
            return (((3 * t - 6) * t) * t + 4) / 6;
23
        case 0:
24
            return (((-3 * t + 3) * t + 3) * t + 1) / 6;
25
        case 1:
26
            return (t * t * t) / 6;
27
        }
28
        return 0; // we only get here if an invalid i is specified
29
    }
30

  
31
    // evaluate a point on the B spline
32
    private Point p(final int i, final float t, int subtype) throws CreateGeometryException {
33
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
34
        float px = 0;
35
        float py = 0;
36
        for (int j = -2; j <= 1; j++) {
37
            px += b(j, t) * m_X[i + j];
38
            py += b(j, t) * m_Y[i + j];
39
        }
40
        return geomManager.createPoint((int) Math.round(px), (int) Math.round(py), subtype);
41
    }
42

  
43
    @Override
44
    public Line getSmoothedLine(final int iSteps, int subtype) throws CreateGeometryException {
45
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
46
        Line line = geomManager.createLine(subtype);
47

  
48
        Point q = p(2, 0, subtype);
49
        line.addVertex(q.getX(), q.getY());
50
        for (int i = 2; i < m_X.length - 1; i++) {
51
            for (int j = 1; j <= iSteps; j++) {
52
                q = p(i, j / (float) iSteps, subtype);
53
                line.addVertex(q);
54
            }
55
        }
56
        return line;
57
    }
58
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.vectorediting.lib.prov.smoothline.SmoothLineEditingLibrary
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.smoothline/pom.xml
1
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2
  <modelVersion>4.0.0</modelVersion>
3
  <parent>
4
    <groupId>org.gvsig</groupId>
5
    <artifactId>org.gvsig.vectorediting.lib.prov</artifactId>
6
    <version>1.0.92</version>
7
  </parent>
8
  <artifactId>org.gvsig.vectorediting.lib.prov.smoothline</artifactId>
9
  <name>org.gvsig.vectorediting.lib.prov.smoothline</name>
10
  <description>Editing provider to smooth lines</description>
11
</project>
0 12

  
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/resources/META-INF/services/org.gvsig.tools.library.Library
1
org.gvsig.vectorediting.lib.prov.trimline.TrimLineEditingLibrary
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/TrimLineEditingProviderFactory.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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.trimline;
26

  
27
import org.gvsig.fmap.geom.Geometry;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.dynobject.DynClass;
30
import org.gvsig.tools.dynobject.DynObject;
31
import org.gvsig.tools.service.spi.AbstractProviderFactory;
32
import org.gvsig.tools.service.spi.Provider;
33
import org.gvsig.tools.service.spi.ProviderServices;
34
import org.gvsig.vectorediting.lib.api.EditingServiceInfo;
35
import org.gvsig.vectorediting.lib.spi.DefaultEditingServiceinfo;
36
import org.gvsig.vectorediting.lib.spi.EditingProviderFactory;
37

  
38
/**
39
 * @author llmarques
40
 *
41
 */
42
public class TrimLineEditingProviderFactory extends AbstractProviderFactory
43
    implements EditingProviderFactory {
44

  
45
    public static final String PROVIDER_NAME = "modify-trim-line";
46

  
47
    private final static String PROVIDER_DESCRIPTION =
48
        "Trim lines by cutting edges";
49

  
50
    public void initialize() {
51
    }
52

  
53
    public EditingServiceInfo getServiceInfo() {
54
        EditingServiceInfo serviceInfo =
55
            new DefaultEditingServiceinfo(PROVIDER_NAME, PROVIDER_DESCRIPTION,
56
                false, null, new int[] { 
57
                    Geometry.TYPES.LINE, Geometry.TYPES.MULTILINE, 
58
                    Geometry.TYPES.CURVE, Geometry.TYPES.MULTICURVE 
59
                });
60

  
61
        return serviceInfo;
62
    }
63

  
64
    @Override
65
    protected Provider doCreate(DynObject parameters, ProviderServices services) {
66
        return new TrimLineEditingProvider(services, parameters);
67
    }
68
    
69
    @Override
70
    public DynObject createParameters() {
71
        DynObject dynobject = super.createParameters();
72
        dynobject.setDynValue(PROVIDER_NAME_FIELD, PROVIDER_NAME);
73
        return dynobject;
74
    }
75

  
76
    @Override
77
    protected DynClass createParametersDynClass() {
78
        DynClass dynclass =
79
            ToolsLocator.getDynObjectManager().createDynClass(PROVIDER_NAME,
80
                PROVIDER_DESCRIPTION);
81

  
82
        dynclass.addDynFieldString(PROVIDER_NAME_FIELD);
83
        dynclass.addDynFieldObject(FEATURE_STORE_FIELD);
84
        dynclass.addDynFieldObject(MAPCONTEXT_FIELD);
85

  
86
        return dynclass;
87
    }
88

  
89
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/TrimLineEditingLibrary.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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.trimline;
26

  
27
import org.gvsig.fmap.geom.Geometry.TYPES;
28
import org.gvsig.tools.ToolsLocator;
29
import org.gvsig.tools.i18n.I18nManager;
30
import org.gvsig.tools.library.AbstractLibrary;
31
import org.gvsig.tools.library.Library;
32
import org.gvsig.tools.library.LibraryException;
33
import org.gvsig.vectorediting.lib.prov.trimline.operation.ArcTrimLineOperation;
34
import org.gvsig.vectorediting.lib.prov.trimline.operation.CurveTrimLineOperation;
35
import org.gvsig.vectorediting.lib.prov.trimline.operation.TrimLineOperationUtils;
36
import org.gvsig.vectorediting.lib.spi.EditingProviderLocator;
37
import org.gvsig.vectorediting.lib.spi.EditingProviderManager;
38

  
39
/**
40
 * @author llmarques
41
 *
42
 */
43
public class TrimLineEditingLibrary extends AbstractLibrary implements Library {
44

  
45
    @Override
46
    protected void doInitialize() throws LibraryException {
47
    }
48

  
49
    @Override
50
    protected void doPostInitialize() throws LibraryException {
51
        EditingProviderManager manager =
52
            EditingProviderLocator.getProviderManager();
53

  
54
        manager.addProviderFactory(new TrimLineEditingProviderFactory());
55

  
56
        manager.registerIcon("vectorediting-tools", "modify-trim-line", this
57
            .getClass().getClassLoader(), this.getClass().getName());
58
        
59
        TrimLineOperationUtils.register(new ArcTrimLineOperation(), TYPES.ARC);
60
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.SPLINE);
61
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.LINE);
62
        TrimLineOperationUtils.register(new CurveTrimLineOperation(), TYPES.CURVE);
63

  
64
        registerTranslations();
65
    }
66

  
67
    private void registerTranslations() {
68
        I18nManager manager = ToolsLocator.getI18nManager();
69
        manager.addResourceFamily("i18n/text",
70
            this.getClass().getClassLoader(), "modify-trim-line");
71
    }
72

  
73
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/operation/TrimLineOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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.trimline.operation;
26

  
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.FeatureSelection;
29
import org.gvsig.fmap.geom.aggregate.MultiCurve;
30
import org.gvsig.fmap.geom.exception.CreateGeometryException;
31
import org.gvsig.fmap.geom.operation.GeometryOperationException;
32
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
33
import org.gvsig.fmap.geom.primitive.Curve;
34
import org.gvsig.fmap.geom.primitive.Point;
35
import org.gvsig.tools.locator.LocatorException;
36

  
37
/**
38
 * Represents an operation to trim curve.
39
 * 
40
 * @author llmarques
41
 *
42
 */
43
public interface TrimLineOperation {
44

  
45
    /**
46
     * Trim line, taking as cutting edges the boundary objects received as
47
     * parameters.
48
     * 
49
     * Inserted point is the point inserted by user. It is used to determinate
50
     * what segment of curve it must trimmed.
51
     * 
52
     * If curveToBeExtended does not intersect with any geometry, the geometry
53
     * will not be modified.
54
     * 
55
     * @param curveToTrim
56
     *            Curve to trim
57
     * @param insertedPoint
58
     *            Point inserted by user to determinate what side of curve must
59
     *            be extended.
60
     * @param boundaryObjects
61
     *            If curveToBeExtended intersects with some boundary object, it
62
     *            will
63
     *            be extend to it. If there are several boundary objects that
64
     *            intersects with curveToBeExtended, it will be extended to
65
     *            nearest
66
     *            point.
67
     * @return Trimmed line
68
     */
69
    public MultiCurve trimLine(Curve curveToTrim, Point insertedPoint,
70
        FeatureSelection boundaryObjects)
71
        throws GeometryOperationNotSupportedException,
72
        GeometryOperationException, DataException, CreateGeometryException,
73
        LocatorException;
74

  
75
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/operation/TrimLineOperationUtils.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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.trimline.operation;
26

  
27
import java.util.HashMap;
28
import java.util.Map;
29

  
30
import org.gvsig.fmap.geom.Geometry.TYPES;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.exception.CreateGeometryException;
33
import org.gvsig.fmap.geom.operation.GeometryOperationException;
34
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
35
import org.gvsig.fmap.geom.primitive.Arc;
36
import org.gvsig.fmap.geom.primitive.Curve;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.fmap.geom.primitive.Primitive;
39
import org.gvsig.tools.locator.LocatorException;
40

  
41
/**
42
 * @author llmarques
43
 *
44
 */
45
public class TrimLineOperationUtils {
46

  
47
    /**
48
     * Use it to indicate start side should be trimmed
49
     */
50
    public static final String START_SIDE = "start";
51

  
52
    /**
53
     * Use it to indicate end side should be trimmed
54
     */
55
    public static final String END_SIDE = "end";
56

  
57
    private static Map<Integer, TrimLineOperation> operations =
58
        new HashMap<Integer, TrimLineOperation>();
59

  
60
    public TrimLineOperationUtils() {
61
    }
62

  
63
    public static void register(TrimLineOperation operation, int geometryType) {
64
        operations.put(geometryType, operation);
65
    }
66

  
67
    public static TrimLineOperation getOperation(Primitive geom) {
68
        Integer type = geom.getGeometryType().getType();
69

  
70
        TrimLineOperation operation = operations.get(type);
71

  
72
        return operation;
73
    }
74

  
75
    // FIXME: remove this method when geometry library has this utility method
76
    public static double getAngle(Point start, Point end)
77
        throws GeometryOperationNotSupportedException,
78
        GeometryOperationException {
79
        double angle = Math.acos((end.getX() - start.getX()) / start.distance(end));
80

  
81
        if (start.getY() > end.getY()) {
82
            angle = -angle;
83
        }
84

  
85
        if (angle < 0) {
86
            angle += (2 * Math.PI);
87
        }
88

  
89
        return angle;
90
    }
91
    
92
    public static Arc createArc(Point center, double radius, Point initPoint,
93
        Point endPoint, int subtype) throws CreateGeometryException,
94
        LocatorException, GeometryOperationNotSupportedException,
95
        GeometryOperationException {
96

  
97
        Arc arc =
98
            (Arc) GeometryLocator.getGeometryManager().create(TYPES.ARC,
99
                subtype);
100

  
101
        double startAngle = TrimLineOperationUtils.getAngle(center, initPoint);
102
        double endAngle = TrimLineOperationUtils.getAngle(center, endPoint);
103

  
104
        arc.setPointsStartEnd(center, radius, startAngle, endAngle);
105
        return arc;
106
    }
107
    
108
    public static boolean intersects(Curve curve, Point projectedPoint)
109
        throws GeometryOperationNotSupportedException,
110
        GeometryOperationException {
111

  
112
        double tolerance = 1;
113

  
114
        return curve.buffer(tolerance).intersects(projectedPoint);
115
    }
116
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/operation/CurveTrimLineOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 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.trimline.operation;
26

  
27
import org.gvsig.fmap.dal.exception.DataException;
28
import org.gvsig.fmap.dal.feature.Feature;
29
import org.gvsig.fmap.dal.feature.FeatureSelection;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryLocator;
32
import org.gvsig.fmap.geom.GeometryManager;
33
import org.gvsig.fmap.geom.aggregate.MultiCurve;
34
import org.gvsig.fmap.geom.exception.CreateGeometryException;
35
import org.gvsig.fmap.geom.operation.GeometryOperationException;
36
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
37
import org.gvsig.fmap.geom.primitive.Curve;
38
import org.gvsig.fmap.geom.primitive.Line;
39
import org.gvsig.fmap.geom.primitive.Point;
40
import org.gvsig.tools.dispose.DisposableIterator;
41
import org.gvsig.tools.locator.LocatorException;
42

  
43
/**
44
 * Operation to trim curves.
45
 * 
46
 * @author llmarques
47
 */
48
public class CurveTrimLineOperation implements TrimLineOperation {
49

  
50
    /*
51
     * Strategy:
52
     * 
53
     * 1. Get arc segments between curve and boundary objects.
54
     * 2. Determinate what segment of curve must be deleted
55
     * 3. Rebuild curve taking into account the segment that must be deleted
56
     */
57
    public MultiCurve trimLine(Curve curveToTrim, Point insertedPoint,
58
        FeatureSelection boundaryObjects)
59
        throws GeometryOperationNotSupportedException,
60
        GeometryOperationException, DataException, CreateGeometryException,
61
        LocatorException {
62

  
63
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
64
        int subType = curveToTrim.getGeometryType().getSubType();
65

  
66
        DisposableIterator it = boundaryObjects.fastIterator();
67
        MultiCurve segmentedLine = geometryManager.createMultiCurve(subType);
68
        segmentedLine.addCurve(curveToTrim);
69

  
70
        while (it.hasNext()) {
71
            Feature feature = (Feature) it.next();
72
            Geometry geometry = feature.getDefaultGeometry();
73

  
74
            Geometry tmpDifference = segmentedLine.difference(geometry);
75
            if (tmpDifference instanceof MultiCurve) {
76
                segmentedLine = (MultiCurve) tmpDifference;
77
            } else if(tmpDifference instanceof Curve) {
78
                // Sometimes line to split and splitter are so close ( distance
79
                // between geometries < 0.00000000001 ). In this cases, we try
80
                // to do an enhance difference.
81
                // If the result is instance of Curve, it is considered that
82
                // lines don't intersect.
83
                // If the result is instance of Multicurve, lines are so close
84
                // and it is considered that lines intersect.
85
                Geometry enchancedGeometry = enhancedDifference(geometry, (Curve) tmpDifference);
86
                if(enchancedGeometry instanceof MultiCurve){
87
                    segmentedLine = (MultiCurve) enchancedGeometry;
88
                } 
89
            }
90
        }
91

  
92
        if (segmentedLine.getPrimitivesNumber() == 1) {
93
            return segmentedLine;
94
        }
95

  
96
        int index = 0;
97
        Curve curve;
98
        double minDistance = Double.POSITIVE_INFINITY;
99
        for (int i = 0; i < segmentedLine.getPrimitivesNumber(); i++) {
100
            curve = segmentedLine.getCurveAt(i);
101
            double distance = curve.distance(insertedPoint);
102
            if (distance < minDistance) {
103
                index = i;
104
                minDistance = distance;
105
            }
106
        }
107

  
108
        MultiCurve trimmedCurve = geometryManager.createMultiCurve(subType);
109

  
110
        Curve tmpCurve = null;
111
        for (int i = 0; i < segmentedLine.getPrimitivesNumber(); i++) {
112

  
113
            if (i == index) {
114
                if (tmpCurve != null) {
115
                    trimmedCurve.addCurve(tmpCurve);
116
                    tmpCurve = null;
117
                }
118
                continue;
119
            }
120

  
121
            if (tmpCurve != null) {
122
                tmpCurve = union(tmpCurve, segmentedLine.getCurveAt(i));
123
            } else {
124
                tmpCurve = segmentedLine.getCurveAt(i);
125
            }
126
        }
127
        if (tmpCurve != null) {
128
            trimmedCurve.addCurve(tmpCurve);
129
        }
130

  
131
        return trimmedCurve;
132
    }
133

  
134
    private Curve union(Curve firstCurve, Curve secondCurve)
135
        throws CreateGeometryException, LocatorException,
136
        GeometryOperationNotSupportedException, GeometryOperationException {
137

  
138
        int subtype1 = firstCurve.getGeometryType().getSubType();
139
        int subtype2 = secondCurve.getGeometryType().getSubType();
140

  
141
        if (subtype1 == subtype2) {
142
            Curve union =
143
                GeometryLocator.getGeometryManager().createLine(subtype1);
144

  
145
            for (int i = 0; i < firstCurve.getNumVertices() - 1; i++) {
146
                union.addVertex(firstCurve.getVertex(i));
147
            }
148

  
149
            Point lastVertex =
150
                firstCurve.getVertex(firstCurve.getNumVertices() - 2);
151
            for (int i = 0; i < secondCurve.getNumVertices(); i++) {
152

  
153
                if (i == 0) {
154
                    Line tmpLine =
155
                        GeometryLocator.getGeometryManager().createLine(
156
                            subtype1);
157
                    tmpLine.setPoints(lastVertex, secondCurve.getVertex(1));
158
                    if (TrimLineOperationUtils.intersects(tmpLine,
159
                        secondCurve.getVertex(0))) {
160
                        continue;
161
                    }
162
                }
163
                union.addVertex(secondCurve.getVertex(i));
164
            }
165

  
166
            return union;
167
        }
168
        return null;
169
    }
170
    
171
    private Geometry enhancedDifference(Geometry splitter, Curve curve)
172
        throws GeometryOperationNotSupportedException,
173
        GeometryOperationException {
174
        Geometry snapTo = curve.snapTo(splitter, 0.1);
175
        return snapTo.difference(splitter);
176
    }
177
}
org.gvsig.vectorediting/tags/org.gvsig.vectorediting-1.0.92/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.trimline/src/main/java/org/gvsig/vectorediting/lib/prov/trimline/operation/ArcTrimLineOperation.java
1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright ? 2007-2015 gvSIG Association
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff