Revision 575 org.gvsig.vectorediting/trunk/org.gvsig.vectorediting/org.gvsig.vectorediting.lib/org.gvsig.vectorediting.lib.prov/org.gvsig.vectorediting.lib.prov.editvertex/src/main/java/org/gvsig/vectorediting/lib/prov/editvertex/operation/PolygonEditVertexOperation.java

View differences:

PolygonEditVertexOperation.java
22 22
        // TODO Auto-generated constructor stub
23 23
    }
24 24

  
25
    public Geometry insertVertex(Primitive geom, int index)
26
        throws CreateGeometryException {
25
    public Geometry insertVertex(Primitive geom, int index) throws CreateGeometryException {
27 26
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
28
        Polygon result =
29
            geometryManager.createPolygon(geom.getGeometryType().getSubType());
30 27

  
31
        Polygon polygon = (Polygon) geom;
28
        Polygon polygon = (Polygon) geom.cloneGeometry();
32 29
        int numVertices = polygon.getNumVertices();
33
        boolean added = false;
34
        Point lastVertex = polygon.getVertex(numVertices - 1);
35 30
        if (index < 0 || index >= numVertices) {
36
            return geom;
31
            return polygon;
37 32
        }
38 33
        int indexToInsert = index;
39
        if (index == 0 && polygon.getVertex(0).equals(lastVertex)) {
34
        Point antVertex = polygon.getVertex(numVertices - 1);
35
        if (index == 0 && polygon.getVertex(0).equals(antVertex)) {
40 36
            indexToInsert = numVertices - 1;
41 37
        }
42 38

  
43
        if (indexToInsert == 0) {
44
            Point point = polygon.getVertex(0);
45
            if (!point.equals(lastVertex)) {
46
                result.addVertex(geometryManager.createPoint(
47
                    (point.getX() + lastVertex.getX()) / 2,
48
                    (point.getY() + lastVertex.getY()) / 2, geom
49
                        .getGeometryType().getSubType()));
50
                added = true;
51
            }
39
        if (indexToInsert!=0){
40
            antVertex = polygon.getVertex(indexToInsert - 1);
52 41
        }
53
        Point antPoint = null;
54
        for (int i = 0; i < numVertices; i++) {
55
            Point point = polygon.getVertex(i);
56
            if (i == indexToInsert && !added) {
57
                result.addVertex(geometryManager.createPoint(
58
                    (point.getX() + antPoint.getX()) / 2,
59
                    (point.getY() + antPoint.getY()) / 2, geom
60
                        .getGeometryType().getSubType()));
61

  
62
            }
63
            result.addVertex(point);
64
            antPoint = point;
65
        }
66
        return result;
42
        Point point = polygon.getVertex(indexToInsert);
43
        Point newPoint = geometryManager.createPoint((point.getX() + antVertex.getX()) / 2,
44
            (point.getY() + antVertex.getY()) / 2, geom.getGeometryType().getSubType());
45
        polygon.insertVertex(indexToInsert, newPoint);
46
        return polygon;
67 47
    }
68 48

  
69 49
    public Geometry removeVertex(Primitive geom, int index)
70 50
        throws CreateGeometryException {
71
        Polygon polygon = (Polygon) geom;
51
        Polygon polygon = (Polygon) geom.cloneGeometry();
72 52
        int numVertices = polygon.getNumVertices();
73 53
        if (index < 0 || index >= numVertices) {
74
            return geom;
54
            return polygon;
75 55
        }
76
        GeometryManager geometryManager = GeometryLocator.getGeometryManager();
77
        Polygon result =
78
            geometryManager.createPolygon(geom.getGeometryType().getSubType());
79 56

  
80 57
        if (polygon.getVertex(0).equals(polygon.getVertex(numVertices - 1))) {
81 58
            if (numVertices <= 4) {
82
                return geom;
59
                return polygon;
83 60
            }
84 61
            numVertices--;
85 62
        } else {
86 63
            if (numVertices <= 3) {
87
                return geom;
64
                return polygon;
88 65
            }
89 66
        }
67
        polygon.removeVertex(index);
90 68

  
91
        int j = 0;
92
        for (int i = 0; i < numVertices; i++) {
93
            Point point = polygon.getVertex(i);
94
            if (i == index) {
95
                continue;
96
            }
97
            result.addVertex(point);
98
            j++;
99
        }
100
        if (!result.getVertex(0).equals(result.getVertex(j - 1))) {
101
            result.addVertex(result.getVertex(0));
102
        }
103

  
104
        return result;
69
        return polygon;
105 70
    }
106 71

  
107 72
    public Geometry moveVertex(Primitive geom, int index, Point point) {

Also available in: Unified diff