Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src-test / org / gvsig / fmap / geom / GeometryManagerTest.java @ 26866

History | View | Annotate | Download (9.88 KB)

1
package org.gvsig.fmap.geom;
2

    
3
import junit.framework.TestCase;
4

    
5
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
6
import org.gvsig.fmap.geom.Geometry.TYPES;
7
import org.gvsig.fmap.geom.operation.GeometryOperation;
8
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
9
import org.gvsig.fmap.geom.operation.GeometryOperationException;
10
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
11
import org.gvsig.fmap.geom.operation.ensureOrientation.EnsureOrientation;
12
import org.gvsig.fmap.geom.operation.flip.Flip;
13
import org.gvsig.fmap.geom.operation.isCCW.IsCCW;
14
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
15
import org.gvsig.fmap.geom.operation.towkb.ToWKB;
16
import org.gvsig.fmap.geom.primitive.Arc;
17
import org.gvsig.fmap.geom.primitive.Arc2D;
18
import org.gvsig.fmap.geom.primitive.Point;
19
import org.gvsig.fmap.geom.primitive.Point2D;
20
import org.gvsig.fmap.geom.primitive.Solid2DZ;
21
import org.gvsig.fmap.geom.type.GeometryType;
22
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
23
import org.slf4j.Logger;
24
import org.slf4j.LoggerFactory;
25

    
26
public class GeometryManagerTest extends TestCase {
27

    
28
        final static private Logger logger = LoggerFactory.getLogger("org.gvsig");
29

    
30
        static {
31
                //logger.addAppender(new ConsoleAppender(new SimpleLayout()));
32
        }
33

    
34
        private GeometryManager manager;
35
        
36
        protected void setUp() throws Exception {
37
                super.setUp();
38

    
39
                GeometryLibrary lib = new GeometryLibrary();                
40
                lib.initialize();
41
                lib.postInitialize();
42
                
43
                manager = GeometryLocator.getGeometryManager();
44
        }
45

    
46
        public void testRegister() throws Exception {
47

    
48
                logger.debug("--------- testRegister() START ----------");
49

    
50
                //Point p2d = (Point) factory.createPoint2D(0,0);
51
                Point p2d = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
52
                p2d.setCoordinateAt(0, 0);
53
                p2d.setCoordinateAt(1, 0);
54
                
55
                java.awt.geom.Point2D pt1 = new java.awt.geom.Point2D.Float(0,1);
56
                java.awt.geom.Point2D pt2 = new java.awt.geom.Point2D.Float(1,1);
57
                java.awt.geom.Point2D pt3 = new java.awt.geom.Point2D.Float(1,0);
58
                //Arc2D c2d = (Arc2D) factory.createArc(pt1, pt2, pt3);
59
                Arc2D c2d = (Arc2D) manager.create(TYPES.ARC, SUBTYPES.GEOM2D);
60

    
61
                GeometryType geomType1 = manager.getGeometryType(Point2D.class);
62
                GeometryType geomType2 = manager.getGeometryType(Arc2D.class);
63

    
64
                logger.debug("geomType2=" + geomType2);
65
/*                GeometryType geomType1 = manager.getGeometryType(Point2D.class);//)registerGeometryType(Point2D.class);
66
                GeometryType geomType2 = manager.getGeometryType(Curve2D.class);//registerGeometryType(Curve2D.class);
67
*/
68

    
69
                int op1 = manager.registerGeometryOperation("toJTS", new ToJTS(),geomType1);
70
                logger.debug("op1=" + op1);
71

    
72
                int op2 = -1;
73
                try {
74
                        op2 = manager.registerGeometryOperation("toJTS", ToJTS.class,geomType1);
75
                        logger.debug("op2=" + op2);
76
                } catch (Exception e) {
77
                        logger.error("Error registrando operaci?n: ", e);
78
                }
79

    
80
                int op3 = manager.registerGeometryOperation("toWKB", new ToWKB(),geomType1);
81
                int op4 = manager.registerGeometryOperation("toWKB", new ToWKB(),geomType2);
82

    
83
                logger.debug("op3=" + op3);
84

    
85
                assertTrue("FALLO: op1 != op2", op1 == op2);
86
                assertTrue("FALLO: op1 == op3", op1 != op3);
87
                assertFalse("FALLO: op4 == op", op4 == op1);
88

    
89
                //Point p = (Point) factory.createPoint2D(1, 2);
90
                Point p = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
91
                                                                        
92
                GeometryOperationContext ctx = new GeometryOperationContext();
93
                ctx.setAttribute("dummy", "DummyValue");
94

    
95
                try {
96
                        p.invokeOperation(ToWKB.CODE, ctx);
97
                        p.invokeOperation(ToJTS.CODE, ctx);
98
                        p.invokeOperation(Flip.CODE, ctx);
99
                } catch (Exception e) {
100
                        logger.error("Error, ", e);
101
                }
102

    
103
                Geometry sol = manager.create(TYPES.SOLID, SUBTYPES.GEOM2DZ);
104
                assertNotNull(sol);
105
                //logger.debug(sol);
106

    
107
                logger.debug("--------- testRegister() END ----------");
108
        }
109

    
110
        public void testGeneralPathXOperations() throws InstantiationException, IllegalAccessException{
111
                logger.debug("--------- Test GeneralPathX Operation START ----------");
112

    
113
                //Registering the operation Flip() to all the geometries...
114
                manager.registerGeometryOperation("flip", new Flip());
115

    
116
                //Registering the operation ensureOrientation() to all the geometries...
117
                manager.registerGeometryOperation("ensureOrientation", new EnsureOrientation());
118

    
119
                //Registering the operation isCCW() to all the geometries...
120
                manager.registerGeometryOperation("isCCW", new IsCCW());
121

    
122
                //Building the Points for a curve...
123
//                java.awt.geom.Point2D p1 = new java.awt.geom.Point2D.Double(1,2);
124
//                java.awt.geom.Point2D p2 = new java.awt.geom.Point2D.Double(2,1);
125
//                java.awt.geom.Point2D p3 = new java.awt.geom.Point2D.Double(3,3);
126
                
127
                Point p1 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
128
                p1.setCoordinateAt(0, 1);
129
                p1.setCoordinateAt(1, 2);        
130
                
131
                Point p2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
132
                p2.setCoordinateAt(0, 2);
133
                p2.setCoordinateAt(1, 1);
134
                
135
                Point p3 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
136
                p3.setCoordinateAt(0, 3);
137
                p3.setCoordinateAt(1, 3);                
138
                
139
                //Build a curve to get the operation invoked with the registered operation.
140
                //Geometry curve = (Curve) factory.createArc(p1, p2, p3);
141
                Arc arc = (Arc) manager.create(TYPES.ARC, SUBTYPES.GEOM2D);
142
        arc.setPoints(p1, p2, p3);
143

    
144
                try {
145
                        arc.invokeOperation(Flip.CODE, null);
146
                } catch (GeometryOperationNotSupportedException e) {
147
                        logger.error("Operation doesn't be registered for this geometry. \n", e);
148
                } catch (GeometryOperationException e) {
149
                        logger.error("An error produced while the Operation was running. \n", e);
150
                }
151
                GeometryOperationContext ctx = new GeometryOperationContext();
152
                ctx.setAttribute("bCCW",new Boolean(true));
153
                Boolean aux1 = null;
154
                try {
155
                        aux1 = (Boolean) arc.invokeOperation(EnsureOrientation.CODE, ctx);
156
                } catch (GeometryOperationNotSupportedException e) {
157
                        logger.error("Operation doesn't be registered for this geometry. \n", e);
158
                } catch (GeometryOperationException e) {
159
                        // TODO Auto-generated catch block
160
                        logger.error("An error produced while the Operation was running. \n", e);
161
                }
162
                //True si es exterior, le ha dado la vuelta.
163
                assertTrue(aux1.booleanValue());
164

    
165
                Boolean aux2 = null;
166
                try {
167
                        aux2 = (Boolean) arc.invokeOperation(IsCCW.CODE, null);
168
                } catch (GeometryOperationNotSupportedException e) {
169
                        logger.error("Operation doesn't be registered for this geometry. \n", e);
170
                } catch (GeometryOperationException e) {
171
                        // TODO Auto-generated catch block
172
                        logger.error("An error produced while the Operation was running. \n", e);
173
                }
174
                //True si es CCW.
175
                assertTrue(aux2.booleanValue());
176

    
177
                logger.debug("--------- Test GeneralPathX Operation END ----------");
178
        }
179

    
180
        public void testInvoke() throws InstantiationException, IllegalAccessException {
181
                int size = 100000;
182

    
183
                // Fill the operation context with required params
184
                GeometryOperationContext ctx = new GeometryOperationContext();
185
                GeometryOperationContext var = new GeometryOperationContext();
186
                var.setAttribute("bCCW",new Boolean(true));
187

    
188
                logger.debug("ToJTS.-");
189
                indirectInvoke(ToJTS.CODE, ctx, size);
190
                directInvoke(ToJTS.CODE, ctx, size);
191
                logger.debug("ToWKB.-");
192
                indirectInvoke(ToWKB.CODE, ctx, size);
193
                directInvoke(ToWKB.CODE, ctx, size);
194
                logger.debug("Flip.-");
195
                indirectInvoke(Flip.CODE, ctx, size);
196
                directInvoke(Flip.CODE, ctx, size);
197
                logger.debug("EnsureOrientation.-");
198
                //indirectInvoke(EnsureOrientation.CODE, var, size);
199
                directInvoke(EnsureOrientation.CODE, var, size);
200
                logger.debug("isCCW.-");
201
                //indirectInvoke(IsCCW.CODE, ctx, size);
202
                directInvoke(IsCCW.CODE, ctx, size);
203

    
204
        }
205

    
206
        private Geometry[] createGeometryArray(int size) throws InstantiationException, IllegalAccessException {
207
                Geometry[] geom = new Geometry[size];
208
                for (int i = 0; i < size; i++) {
209
                        //geom[i] = factory.createPoint2D(Math.log(1 - i), Math.log(i + 1));
210
                        geom[i] = manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
211
                }
212
                return geom;
213
        }
214

    
215
        private long directInvoke(int opCode, GeometryOperationContext ctx, int size) throws InstantiationException, IllegalAccessException {
216
                Geometry[] geoms = createGeometryArray(size);
217

    
218
                long t0 = System.currentTimeMillis();
219

    
220
                GeometryOperation geomOp = null;
221
                try {
222
                        geomOp = manager.getGeometryOperation(Point.class, ToJTS.CODE);
223
                } catch (GeometryTypeNotSupportedException gtnse) {
224
                        logger.error("Error:", gtnse);
225
                } catch (GeometryOperationNotSupportedException gonse) {
226
                        logger.error("Error:", gonse);
227
                }
228

    
229
                // Here is the main loop where you call the operation
230
                try {
231
                        for (int i = 0; i < geoms.length; i++) {
232
                                Object result = geomOp.invoke(geoms[i], ctx);
233
                                // if(i%100 == 99) logger.debug(result);
234
                        }
235

    
236
                } catch (GeometryOperationException goe) {
237
                        logger.error("Error:", goe);
238
                }
239
                long t1 = System.currentTimeMillis();
240
                long t = t1 - t0;
241
                logger.debug("Milliseconds (direct): " + t);
242
                return t;
243
        }
244

    
245
        private long indirectInvoke(int opCode, GeometryOperationContext ctx,
246
                        int size) throws InstantiationException, IllegalAccessException {
247
                Geometry[] geoms = createGeometryArray(size);
248

    
249
                long t0 = System.currentTimeMillis();
250

    
251
                // Here is the main loop where you call the operation
252
                try {
253
                        for (int i = 0; i < geoms.length; i++) {
254
                                Object result = ((Geometry) geoms[i]).invokeOperation(opCode,
255
                                                ctx);
256
                                // if(i%100 == 99) logger.debug(result);
257
                        }
258
                } catch (GeometryOperationNotSupportedException gonse) {
259
                        logger.error("Error:", gonse);
260
                } catch (GeometryOperationException goe) {
261
                        logger.error("Error:", goe);
262
                }
263
                long t1 = System.currentTimeMillis();
264
                long t = t1 - t0;
265
                logger.debug("Milliseconds (indirect): " + t);
266
                return t;
267
        }
268

    
269
        public void testRegisterBasicGeometryType() {
270
                GeometryType gt = GeometryLocator.getGeometryManager()
271
                .registerGeometryType(DummyPoint2D.class, "DummyPoint2D", TYPES.ELLIPSE,  SUBTYPES.GEOM2DZ);
272

    
273
                logger.debug(String.valueOf(gt.getType()));
274

    
275
                GeometryLocator.getGeometryManager().unregisterGeometryType(DummyPoint2D.class);
276

    
277
                gt = GeometryLocator.getGeometryManager()
278
                .registerGeometryType(DummyPoint2D.class, "DummyPoint2D", TYPES.POINT, SUBTYPES.GEOM2D);
279

    
280
                logger.debug(String.valueOf(gt.getType()));
281
        }
282
}