-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
212 lines (188 loc) · 8.92 KB
/
test.js
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
var sample_1 = [
{x: 224.55, y: 250.15}, {x: 226.91, y: 244.19}, {x: 233.31, y: 241.45}, {x: 234.98, y: 236.06},
{x: 244.21, y: 232.76}, {x: 262.59, y: 215.31}, {x: 267.76, y: 213.81}, {x: 273.57, y: 201.84},
{x: 273.12, y: 192.16}, {x: 277.62, y: 189.03}, {x: 280.36, y: 181.41}, {x: 286.51, y: 177.74},
{x: 292.41, y: 159.37}, {x: 296.91, y: 155.64}, {x: 314.95, y: 151.37}, {x: 319.75, y: 145.16},
{x: 330.33, y: 137.57}, {x: 341.48, y: 139.96}, {x: 369.98, y: 137.89}, {x: 387.39, y: 142.51},
{x: 391.28, y: 139.39}, {x: 409.52, y: 141.14}, {x: 414.82, y: 139.75}, {x: 427.72, y: 127.30},
{x: 439.60, y: 119.74}, {x: 474.93, y: 107.87}, {x: 486.51, y: 106.75}, {x: 489.20, y: 109.45},
{x: 493.79, y: 108.63}, {x: 504.74, y: 119.66}, {x: 512.96, y: 122.35}, {x: 518.63, y: 120.89},
{x: 524.09, y: 126.88}, {x: 529.57, y: 127.86}, {x: 534.21, y: 140.93}, {x: 539.27, y: 147.24},
{x: 567.69, y: 148.91}, {x: 575.25, y: 157.26}, {x: 580.62, y: 158.15}, {x: 601.53, y: 156.85},
{x: 617.74, y: 159.86}, {x: 622.00, y: 167.04}, {x: 629.55, y: 194.60}, {x: 638.90, y: 195.61},
{x: 641.26, y: 200.81}, {x: 651.77, y: 204.56}, {x: 671.55, y: 222.55}, {x: 683.68, y: 217.45},
{x: 695.25, y: 219.15}, {x: 700.64, y: 217.98}, {x: 703.12, y: 214.36}, {x: 712.26, y: 215.87},
{x: 721.49, y: 212.81}, {x: 727.81, y: 213.36}, {x: 729.98, y: 208.73}, {x: 735.32, y: 208.20},
{x: 739.94, y: 204.77}, {x: 769.98, y: 208.42}, {x: 779.60, y: 216.87}, {x: 784.20, y: 218.16},
{x: 800.24, y: 214.62}, {x: 810.53, y: 219.73}, {x: 817.19, y: 226.82}, {x: 820.77, y: 236.17},
{x: 827.23, y: 236.16}, {x: 829.89, y: 239.89}, {x: 851.00, y: 248.94}, {x: 859.88, y: 255.49},
{x: 865.21, y: 268.53}, {x: 857.95, y: 280.30}, {x: 865.48, y: 291.45}, {x: 866.81, y: 298.66},
{x: 864.68, y: 302.71}, {x: 867.79, y: 306.17}, {x: 859.87, y: 311.37}, {x: 860.08, y: 314.35},
{x: 858.29, y: 314.94}, {x: 858.10, y: 327.60}, {x: 854.54, y: 335.40}, {x: 860.92, y: 343.00},
{x: 856.43, y: 350.15}, {x: 851.42, y: 352.96}, {x: 849.84, y: 359.59}, {x: 854.56, y: 365.53},
{x: 849.74, y: 370.38}, {x: 844.09, y: 371.89}, {x: 844.75, y: 380.44}, {x: 841.52, y: 383.67},
{x: 839.57, y: 390.40}, {x: 845.59, y: 399.05}, {x: 848.40, y: 407.55}, {x: 843.71, y: 411.30},
{x: 844.09, y: 419.88}, {x: 839.51, y: 432.76}, {x: 841.33, y: 441.04}, {x: 847.62, y: 449.22},
{x: 847.16, y: 458.44}, {x: 851.38, y: 462.79}, {x: 853.97, y: 471.15}, {x: 866.36, y: 480.77}
],
dp_sample_1_expect = [
{x: 224.55, y: 250.15}, {x: 267.76, y: 213.81}, {x: 296.91, y: 155.64},
{x: 330.33, y: 137.57}, {x: 409.52, y: 141.14}, {x: 439.6, y: 119.74},
{x: 486.51, y: 106.75}, {x: 529.57, y: 127.86}, {x: 539.27, y: 147.24},
{x: 617.74, y: 159.86}, {x: 629.55, y: 194.6}, {x: 671.55, y: 222.55},
{x: 727.81, y: 213.36}, {x: 739.94, y: 204.77}, {x: 769.98, y: 208.42},
{x: 784.2, y: 218.16}, {x: 800.24, y: 214.62}, {x: 820.77, y: 236.17},
{x: 859.88, y: 255.49}, {x: 865.21, y: 268.53}, {x: 857.95, y: 280.3},
{x: 867.79, y: 306.17}, {x: 858.29, y: 314.94}, {x: 854.54, y: 335.4},
{x: 860.92, y: 343}, {x: 849.84, y: 359.59}, {x: 854.56, y: 365.53},
{x: 844.09, y: 371.89}, {x: 839.57, y: 390.4}, {x: 848.4, y: 407.55},
{x: 839.51, y: 432.76}, {x: 853.97, y: 471.15}, {x: 866.36, y: 480.77}
],
rd_sample_1_expect = [
{x: 224.55, y: 250.15}, {x: 262.59, y: 215.31}, {x: 286.51, y: 177.74},
{x: 319.75, y: 145.16}, {x: 369.98, y: 137.89}, {x: 414.82, y: 139.75},
{x: 474.93, y: 107.87}, {x: 512.96, y: 122.35}, {x: 567.69, y: 148.91},
{x: 617.74, y: 159.86}, {x: 638.9, y: 195.61}, {x: 671.55, y: 222.55},
{x: 712.26, y: 215.87}, {x: 769.98, y: 208.42}, {x: 810.53, y: 219.73},
{x: 851, y: 248.94}, {x: 865.48, y: 291.45}, {x: 854.54, y: 335.4},
{x: 844.75, y: 380.44}, {x: 839.51, y: 432.76}, {x: 853.97, y: 471.15}, {x: 866.36, y: 480.77}
],
sample_2 = [
{x: 0, y: 0}, {x: 0, y: 0}, {x: 0, y: 0}, {x: 5, y: 6}, {x: 10, y: 10}, {x: 10, y: 10}, {x: 10, y: 10},
{x: 20, y: 19}, {x: 20, y: 19}, {x: 20, y: 19}, {x: 20, y: 19}, {x: 20, y: 19}, {x: 20, y: 19}, {x: 20, y: 19}
],
dp_sample_2_expect = [
{x: 0, y: 0}, {x: 20, y: 19}
],
rd_sample_2_expect = [
{x: 0, y: 0}, {x: 5, y: 6}, {x: 10, y: 10}, {x: 20, y: 19}
],
sample_3 = [
{x: 1, y: 1}, {x: 2, y: 2}, {x: 3, y: 3}, {x: 4, y: 4}, {x: 5, y: 5},
{x: 6, y: 6}, {x: 7, y: 7}, {x: 8, y: 8}, {x: 9, y: 9}, {x: 10, y: 10}
],
dp_sample_3_expect = [
{x: 1, y: 1}, {x: 10, y: 10}
],
rd_sample_3_expect = [
{x: 1, y: 1}, {x: 3, y: 3}, {x: 5, y: 5}, {x: 7, y: 7}, {x: 9, y: 9}, {x: 10, y: 10}
],
sample_4 = [
{x: 1, y: 0}, {x: 2, y: 0}, {x: 3, y: 0}, {x: 2.9, y: 1}, {x: 2.8, y: 2}, {x: 2.9, y: 3}, {x: 3, y: 4},
{x: 3.1, y: 3}, {x: 3.2, y: 2}, {x: 3.1, y: 1}, {x: 3, y: 0}, {x: 4, y: 0}, {x: 5, y: 0}
],
dp_sample_4_expect = [
{ x: 1, y: 0 }, { x: 3, y: 0 }, { x: 3, y: 4 }, { x: 3, y: 0 }, { x: 5, y: 0 }
],
t = require('tape'),
simplify2 = require('./simplify2'),
douglasPeucker = simplify2.douglasPeucker,
radialDistance = simplify2.radialDistance;
// --- douglasPeucker test cases ---
t('douglas-peucker: sample 1', function (t) {
var result = douglasPeucker(sample_1, 5);
t.same(result, dp_sample_1_expect);
t.end();
});
t('douglas-peucker: sample 2', function (t) {
var result = douglasPeucker(sample_2, 5);
t.same(result, dp_sample_2_expect);
t.end();
});
t('douglas-peucker: sample 3', function (t) {
var result = douglasPeucker(sample_3, 2);
t.same(result, dp_sample_3_expect);
t.end();
});
t('douglas-peucker: sample 4', function (t) {
var result = douglasPeucker(sample_4, 1);
t.same(result, dp_sample_4_expect);
t.end();
});
t('douglas-peucker: [] -> []', function (t) {
var result = douglasPeucker([], 1);
t.same(result, []);
t.end();
});
t('douglas-peucker: [p] -> [p]', function (t) {
var result = douglasPeucker([{x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('douglas-peucker: [p, p] -> [p]', function (t) {
var result = douglasPeucker([{x: 1, y: 2}, {x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('douglas-peucker: [p*] -> [p]', function (t) {
var result = douglasPeucker([{x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('douglas-peucker: [p1, p2] -> [p1, p2]', function (t) {
var result = douglasPeucker([{x: 1, y: 2}, {x: 4, y: 3}], 1);
t.same(result, [{x: 1, y: 2}, {x: 4, y: 3}]);
t.end();
});
t('douglas-peucker: [p1, p2*, p3] -> [p1, p2, p3]', function (t) {
var result = douglasPeucker([{x: 1, y: 2}, {x: 3, y: 4}, {x: 3, y: 4}, {x: 3, y: 4}, {x: 3, y: 4}, {x: 5, y: 2}], 1);
t.same(result, [{x: 1, y: 2}, {x: 3, y: 4}, {x: 5, y: 2}]);
t.end();
});
t('douglas-peucker: [p1*, p2*] -> [p1, p2]', function (t) {
var result = douglasPeucker([
{x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2},
{x: 4, y: 3}, {x: 4, y: 3}, {x: 4, y: 3}, {x: 4, y: 3}],
1);
t.same(result, [{x: 1, y: 2}, {x: 4, y: 3}]);
t.end();
});
// --- radialDistance test cases ---
t('radial-distance: sample 1', function (t) {
var result = radialDistance(sample_1, 40);
t.same(result, rd_sample_1_expect);
t.end();
});
t('radial-distance: sample 2', function (t) {
var result = radialDistance(sample_2, 5);
t.same(result, rd_sample_2_expect);
t.end();
});
t('radial-distance: sample 3', function (t) {
var result = radialDistance(sample_3, 2);
t.same(result, rd_sample_3_expect);
t.end();
});
t('radial-distance: [] -> []', function (t) {
var result = radialDistance([], 1);
t.same(result, []);
t.end();
});
t('radial-distance: [p] -> [p]', function (t) {
var result = radialDistance([{x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('radial-distance: [p, p] -> [p]', function (t) {
var result = radialDistance([{x: 1, y: 2}, {x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('radial-distance: [p*] -> [p]', function (t) {
var result = radialDistance([{x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2}], 1);
t.same(result, [{x: 1, y: 2}]);
t.end();
});
t('radial-distance: [p1, p2] -> [p1, p2]', function (t) {
var result = radialDistance([{x: 1, y: 2}, {x: 4, y: 3}], 1);
t.same(result, [{x: 1, y: 2}, {x: 4, y: 3}]);
t.end();
});
t('radial-distance: [p1*, p2*] -> [p1, p2]', function (t) {
var result = radialDistance([
{x: 1, y: 2}, {x: 1, y: 2}, {x: 1, y: 2},
{x: 4, y: 3}, {x: 4, y: 3}, {x: 4, y: 3}, {x: 4, y: 3}],
1);
t.same(result, [{x: 1, y: 2}, {x: 4, y: 3}]);
t.end();
});