summaryrefslogtreecommitdiff
path: root/misc/openlayers/tests/Geometry/LinearRing.html
blob: cbbba2a0bc36850c540af2b5e85066d4a37512fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<html>
<head>
  <script src="../OLLoader.js"></script>
  <script type="text/javascript">
    var line;
    var components = [new OpenLayers.Geometry.Point(10,10), 
                new OpenLayers.Geometry.Point(0,0)];
        
    function test_LinearRing_constructor (t) {
        t.plan( 6 );

      //null
        ring = new OpenLayers.Geometry.LinearRing();
        t.ok( ring instanceof OpenLayers.Geometry.LinearRing, "new OpenLayers.Geometry.LinearRing returns ring object" );
        t.eq( ring.CLASS_NAME, "OpenLayers.Geometry.LinearRing", "ring.CLASS_NAME is set correctly");
        t.eq( ring.components, [], "ring.components is set correctly");

      //valid components
        ring = new OpenLayers.Geometry.LinearRing(components);
        t.ok( ring instanceof OpenLayers.Geometry.LinearRing, "new OpenLayers.Geometry.LinearRing returns ring object" );
        t.eq( ring.CLASS_NAME, "OpenLayers.Geometry.LinearRing", "ring.CLASS_NAME is set correctly");
        t.eq( ring.components.length, 3, "ring.components.length is set correctly");
    }
    
    function test_LinearRing_addComponent(t) {
        t.plan(13);
        
        var ring = new OpenLayers.Geometry.LinearRing();
 
        var point = new OpenLayers.Geometry.Point(0,0);
        t.ok(ring.addComponent(point),
             "addComponent returns true for 1st point");
        t.eq(ring.components.length, 2, "add first point, correct length");
        t.ok(ring.components[0].equals(point), "point one correct");
        t.ok(ring.components[0] === ring.components[ring.components.length - 1],
             "first and last point are the same");
        
        newPoint = new OpenLayers.Geometry.Point(10,10);
        t.ok(ring.addComponent( newPoint ),
             "addComponent returns true for unique point");
        t.eq(ring.components.length, 3, "correctly adds 3rd point");
        t.ok(ring.components[0].equals(point), "point one correct");
        t.ok(ring.components[1].equals(newPoint), "point one correct");
        t.ok(ring.components[0] === ring.components[ring.components.length - 1],
             "first and last point are the same");
        
        var length = ring.components.length;
        var clone = ring.components[length - 1].clone();
        t.ok(!ring.addComponent(clone),
             "addComponent returns false for adding a duplicate last point");
        t.eq(ring.components.length, length,
             "components remains unchanged after trying to add duplicate point");
        t.ok(ring.addComponent(clone, length - 1),
             "addComponent returns true when adding a duplicate with an index");
        t.eq(ring.components.length, length + 1,
             "components increase in length after adding a duplicate point with index");
        
    }
    
    function test_LinearRing_removeComponent(t) {
        t.plan(10);
        
        var components = [new OpenLayers.Geometry.Point(0,0), 
                    new OpenLayers.Geometry.Point(0,10),
                    new OpenLayers.Geometry.Point(15,15), 
                    new OpenLayers.Geometry.Point(10,0)
                    ];
        var ring = new OpenLayers.Geometry.LinearRing(components);
 
        ring.removeComponent( ring.components[2] );
        t.eq(ring.components.length, 4, "removing from linear ring with 5 points: length ok");
        t.ok(ring.components[0].equals(components[0]), "point one correct");
        t.ok(ring.components[1].equals(components[1]), "point two correct");
        t.ok(ring.components[2].equals(components[3]), "point three correct");
        t.ok(ring.components[0] === ring.components[ring.components.length - 1],
             "first and last point are the same");
 
        var testBounds = new OpenLayers.Bounds(0,0,10,10);
        var ringBounds = ring.getBounds();
        t.ok(ringBounds.equals(testBounds), "bounds correctly recalculated");
        
        ring.removeComponent( ring.components[2] );
        ring.removeComponent( ring.components[1] );
        t.eq(ring.components.length, 3, "cant remove from linear ring with only 3 points. new length ok");
        t.ok(ring.components[0].equals(components[0]), "point one correct");
        t.ok(ring.components[1].equals(components[1]), "point two correct");
        t.ok(ring.components[0] === ring.components[ring.components.length - 1],
             "first and last point are the same");
        
     }
    
    function test_LinearRing_getArea(t) {
        t.plan(1);
        var components = [new OpenLayers.Geometry.Point(0,0), 
                    new OpenLayers.Geometry.Point(0,10),
                    new OpenLayers.Geometry.Point(10,10), 
                    new OpenLayers.Geometry.Point(10,0)
                    ];
        var ring = new OpenLayers.Geometry.LinearRing(components);
        
        t.eq(ring.getArea(), 100, "getArea works lovely");
    }
    
    function test_LinearRing_getLength(t) {
        t.plan(1);
        var components = [
            new OpenLayers.Geometry.Point(0,0), 
            new OpenLayers.Geometry.Point(0,10),
            new OpenLayers.Geometry.Point(10,10), 
            new OpenLayers.Geometry.Point(10,0)
        ];
        var ring = new OpenLayers.Geometry.LinearRing(components);
        t.eq(ring.getLength(), 40, "getLength returns the correct perimiter");
    }

    function test_LinearRing_getCentroid(t) {
        t.plan(2);
        var components = [
            new OpenLayers.Geometry.Point(0,0), 
            new OpenLayers.Geometry.Point(0,10),
            new OpenLayers.Geometry.Point(10,10), 
            new OpenLayers.Geometry.Point(10,0)
        ];
        var ring = new OpenLayers.Geometry.LinearRing(components);
        var centroid = ring.getCentroid();
        t.ok(centroid.x === 5 && centroid.y === 5, "getCentroid returns the correct centroid");
        ring.destroy();

        ring = new OpenLayers.Geometry.LinearRing();
        t.eq(ring.getCentroid(), null, "getCentroid returns null if no components");     
    }

    function test_LinearRing_move(t) {

        var nvert = 4,
            x = new Array(nvert),
            y = new Array(nvert),
            components = new Array(nvert);
            
        t.plan(2 * (nvert + 1));

        for(var i=0; i<nvert; ++i) {
            x[i] = Math.random();
            y[i] = Math.random();
            components[i] = new OpenLayers.Geometry.Point(x[i], y[i]);
        }
        x.push(x[0]);
        y.push(y[0]);

        var ring = new OpenLayers.Geometry.LinearRing(components);

        var dx = Math.random();
        var dy = Math.random();

        ring.move(dx, dy);
        
        for(var j=0; j<nvert + 1; ++j) {
            t.eq(ring.components[j].x, x[j] + dx,
                 "move correctly adjust x coord of " + j + " component");
            t.eq(ring.components[j].y, y[j] + dy,
                 "move correctly adjust y coord of " + j + " component");
        }
    }
    
    function test_LinearRing_rotate(t) {
        t.plan(10);

        var components = [
            new OpenLayers.Geometry.Point(10,10), 
            new OpenLayers.Geometry.Point(11,10),
            new OpenLayers.Geometry.Point(11,11), 
            new OpenLayers.Geometry.Point(10,11)
        ];
        
        var ring = new OpenLayers.Geometry.LinearRing(components);

        // rotate a quarter turn around the origin
        var origin = new OpenLayers.Geometry.Point(0, 0);
        var angle = 90;
        
        ring.rotate(angle, origin);
        
        function withinTolerance(i, j) {
            return Math.abs(i - j) < 1e-9;
        }

        t.ok(withinTolerance(ring.components[0].x , -10),
             "rotate correctly adjusts x of component 0");
        t.ok(withinTolerance(ring.components[0].y, 10),
             "rotate correctly adjusts y of component 0");
        t.ok(withinTolerance(ring.components[1].x, -10),
             "rotate correctly adjusts x of component 1");
        t.ok(withinTolerance(ring.components[1].y, 11),
             "rotate correctly adjusts y of component 1");
        t.ok(withinTolerance(ring.components[2].x, -11),
             "rotate correctly adjusts x of component 2");
        t.ok(withinTolerance(ring.components[2].y, 11),
             "rotate correctly adjusts y of component 2");
        t.ok(withinTolerance(ring.components[3].x, -11),
             "rotate correctly adjusts x of component 3");
        t.ok(withinTolerance(ring.components[3].y, 10),
             "rotate correctly adjusts y of component 3");
        t.ok(withinTolerance(ring.components[4].x, -10),
             "rotate correctly adjusts x of component 4");
        t.ok(withinTolerance(ring.components[4].y, 10),
             "rotate correctly adjusts y of component 4");
    }

    function test_LinearRing_resize(t) {
        t.plan(10);

        var components = [
            new OpenLayers.Geometry.Point(10,10), 
            new OpenLayers.Geometry.Point(11,10),
            new OpenLayers.Geometry.Point(11,11), 
            new OpenLayers.Geometry.Point(10,11)
        ];
        
        var ring = new OpenLayers.Geometry.LinearRing(components);

        // rotate a quarter turn around the origin
        var origin = new OpenLayers.Geometry.Point(0, 0);
        var scale = Math.random();
        
        ring.resize(scale, origin);
        
        function withinTolerance(i, j) {
            return Math.abs(i - j) < 1e-9;
        }

        t.ok(withinTolerance(ring.components[0].x , 10 * scale),
             "resize correctly adjusts x of component 0");
        t.ok(withinTolerance(ring.components[0].y, 10 * scale),
             "resize correctly adjusts y of component 0");
        t.ok(withinTolerance(ring.components[1].x, 11 * scale),
             "resize correctly adjusts x of component 1");
        t.ok(withinTolerance(ring.components[1].y, 10 * scale),
             "resize correctly adjusts y of component 1");
        t.ok(withinTolerance(ring.components[2].x, 11 * scale),
             "resize correctly adjusts x of component 2");
        t.ok(withinTolerance(ring.components[2].y, 11 * scale),
             "resize correctly adjusts y of component 2");
        t.ok(withinTolerance(ring.components[3].x, 10 * scale),
             "resize correctly adjusts x of component 3");
        t.ok(withinTolerance(ring.components[3].y, 11 * scale),
             "resize correctly adjusts y of component 3");
        t.ok(withinTolerance(ring.components[4].x, 10 * scale),
             "resize correctly adjusts x of component 4");
        t.ok(withinTolerance(ring.components[4].y, 10 * scale),
             "resize correctly adjusts y of component 4");
    }

    function test_containsPoint(t) {

        /**
         *  The ring:
         *                      edge 3
         *          (5, 10)  __________ (15, 10)
         *                 /         /
         *        edge 4 /         / edge 2
         *             /         /
         *    (0, 0) /_________/ (10, 0)
         *             edge 1
         */
        var components = [
            new OpenLayers.Geometry.Point(0, 0),
            new OpenLayers.Geometry.Point(10, 0),
            new OpenLayers.Geometry.Point(15, 10),
            new OpenLayers.Geometry.Point(5, 10)
        ];

        var ring = new OpenLayers.Geometry.LinearRing(components);
        
        function p(x, y) {
            return new OpenLayers.Geometry.Point(x, y);
        }
        
        // contains: 1 (touches), true (within), false (outside)
        var cases = [{
            point: p(5, 5), contains: true
        }, {
            point: p(20, 20), contains: false
        }, {
            point: p(15, 15), contains: false
        }, {
            point: p(0, 0), contains: 1 // lower left corner
        }, {
            point: p(10, 0), contains: 1 // lower right corner
        }, {
            point: p(15, 10), contains: 1 // upper right corner
        }, {
            point: p(5, 10), contains: 1 // upper left corner
        }, {
            point: p(5, 0), contains: 1 // on edge 1
        }, {
            point: p(5, -0.1), contains: false // below edge 1
        }, {
            point: p(5, 0.1), contains: true // above edge 1
        }, {
            point: p(12.5, 5), contains: 1 // on edge 2
        }, {
            point: p(12.4, 5), contains: true // left of edge 2
        }, {
            point: p(12.6, 5), contains: false // right of edge 2
        }, {
            point: p(10, 10), contains: 1 // on edge 3
        }, {
            point: p(10, 9.9), contains: true // below edge 3
        }, {
            point: p(10, 10.1), contains: false // above edge 3
        }, {
            point: p(2.5, 5), contains: 1 // on edge 4
        }, {
            point: p(2.4, 5), contains: false // left of edge 4
        }, {
            point: p(2.6, 5), contains: true // right of edge 4
        }];
        
        var len = cases.length;
        t.plan(len);
        var c;
        for (var i=0; i<len; ++i) {
            c = cases[i];
            t.eq(ring.containsPoint(c.point), c.contains, "case " + i + ": " + c.point);
        }
    }
    
    function test_containsPoint_precision(t) {

        /**
         * The test for linear ring containment was sensitive to failure when
         * looking for ray crossings on nearly vertical edges.  With a loss
         * of precision in calculating the x-coordinate for the crossing,
         * the method would erronously determine that the x-coordinate was
         * not within the (very narrow) x-range of the nearly vertical edge.
         * 
         * The test below creates a polygon whose first vertical edge is 
         * nearly horizontal.  The test point lies "far" outside the polygon
         * and we expect the containsPoint method to return false.
         */
        
        t.plan(1);

        var components = [
            new OpenLayers.Geometry.Point(10000020.000001, 1000000),
            new OpenLayers.Geometry.Point(10000020.000002, 1000010), // nearly vertical
            new OpenLayers.Geometry.Point(10000030, 1000010),
            new OpenLayers.Geometry.Point(10000030, 1000000)
        ];

        var ring = new OpenLayers.Geometry.LinearRing(components);
        var point = new OpenLayers.Geometry.Point(10000000, 1000001);

        t.eq(ring.containsPoint(point), false, "false for point outside polygon with nearly vertical edge");

    }

  </script>
</head>
<body>
</body>
</html>