-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboolean-test.js
More file actions
222 lines (196 loc) · 7.34 KB
/
boolean-test.js
File metadata and controls
222 lines (196 loc) · 7.34 KB
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
var utils = require("./utils.js").Utils;
var paper = require("./paperjs/src/load.js");
var colors = require('colors');
var fs = require("fs");
var precision = {
point: 0.01,
area: 20,
bitmap: 20
};
var testModes = {
paths: false,
curves: false,
area: true,
bitmap: true
};
var width = 600, height = 600;
var canvas = new paper.Canvas(width, height);
var ctx = canvas.getContext("2d");
paper.setup(canvas);
var fname = "", fSplit = /(\\|\/)?(.*(\\|\/))*(.*).json/i,
testName, suit, testCount, timeDiff, fn,
casesDone = 0, casesPassed = 0, casesFailed = 0,
i, j, li, lj, cases, tCase, op1, op2, res, resOriginal,
testResult = {}, testRunCount = 0, boolTime = 0, txt,
// Define styles for previewing
style1 = { fillColor: null, strokeColor: new paper.Color(0,0,0) },
style2 = { strokeColor: new paper.Color(1,0,0), fillColor: new paper.Color(1,0,0, 0.4) },
styleBlack = { strokeColor: null, fillColor: new paper.Color(0,0,0) },
styleNull = { strokeColor: null, fillColor: null },
// Export failed cases
failCases = { cases: [] };
// Process args
if(process.argv.length < 3){
console.log("Usage : node boolean-test.js <filename> [options]")
return;
}
var fname = "/" + process.argv[2];
testName = fSplit.exec(fname)[4];
// Load the test suit
utils.log("Loading test suit \"" + testName + "\"...");
utils.timer.start("load");
suit = utils.loadJSONfile(__dirname + fname);
fn = suit.fn;
suit = suit.tests;
timeDiff = utils.timer.end("load", "ms").toFixed(1);
// Find the cumulative test count
testCount = suit.reduce(function(sum, a){ return sum + a.cases.length }, 0);
utils.log(testCount + " tests loaded from test suit \"" + testName + "\"; in " + timeDiff + " ms.");
console.log();
utils.log("Testing paperjs " + fn + " operator.", testName);
// begin the actual test
// TODO: modularize!
utils.progress.setup(testName, testCount);
utils.timer.start("test");
var vectorTest, rasterTest;
for (i = 0, li = suit.length; i < li; i++) {
op1 = utils.getPath(paper, suit[i].op1);
cases = suit[i].cases;
// console.log(suit[i].name);
for (j = 0, lj = cases.length; j < lj; j++) {
// DEBUG:============================================================
// console.log(cases[j].name);
// DEBUG:============================================================
tCase = cases[j];
op2 = utils.getPath(paper, tCase.op2);
resOriginal = utils.getPath(paper, tCase.res);
// Perform the paperjs boolean op
utils.timer.start("bool");
if(fn === "subtract")
res = op2[fn](op1);
else
res = op1[fn](op2);
boolTime += utils.timer.end("bool", "ms");
++casesDone;
vectorTest = compare(res, resOriginal, precision, testResult);
rasterTest = testRasterArea();
if(vectorTest && rasterTest)
++casesPassed;
else{
++casesFailed;
// Save this failed cases
failCases.cases.push({
"name" : suit[i].name + "_" + tCase.name,
"fn" : fn,
"op1" : suit[i].op1,
"op2" : tCase.op2,
"res" : tCase.res,
});
// Save a preview file of the failed case
resOriginal.style = style1;
res.style = style2;
txt = new paper.PointText([10, 20]);
txt.content = (testModes.paths ? "children: " + testResult.child + "(" + testResult.ch1 + ", " + testResult.ch2 + ")" : "") +
(testModes.curves ? ", curves: " + testResult.curves : "") +
(testModes.area ? ", area: " + testResult.area : "");
txt.fillColor = "#000";
paper.view.draw();
fs.writeFileSync(__dirname + "/out/test_" + testName + "_" + suit[i].name + "_" + tCase.name + ".png", canvas.toBuffer());
if(testModes.bitmap)
testRasterArea(__dirname + "/out/test_" + testName + "_" + suit[i].name + "_" + tCase.name + "_v.png", canvas.toBuffer());
txt.remove();
}
// console.log(testResult);
++testRunCount;
op1.remove();
op2.remove();
res.remove();
resOriginal.remove();
utils.progress.update(casesDone, casesPassed, casesFailed);
if (global.gc)
global.gc();
}
}
timeDiff = formatTimeIvl(utils.timer.end("test", "ms"));
utils.progress.close();
// Save the failed cases to disk, if any.
if(failCases.cases.length){
if(testName)
fs.writeFileSync(__dirname + "/out/" + testName + "-fail.json", JSON.stringify(failCases));
}
// Print the test report
console.log();
console.log(" Test Report [" + testName + "]");
console.log("----------------------------------------------------");
console.log(" Tests run - " + testRunCount.toString());
console.log(" Tests passed - " + casesPassed.toString().green + " " + (casesPassed * 100.0/testRunCount).toFixed(1) + "%");
console.log(" Tests failed - " + casesFailed.toString().red + " " + (casesFailed * 100.0/testRunCount).toFixed(1) + "%");
console.log();
console.log(" Elapsed - " + timeDiff);
console.log(" Boolean op. time - " + formatTimeIvl(boolTime));
console.log(" Boolean avg. time - " + formatTimeIvl(boolTime / testRunCount));
console.log();
// --END--
// Compare raster
function testRasterArea(saveFileName) {
if (!testModes.bitmap)
return true;
res.style = resOriginal.style = styleNull;
paper.view.draw();
ctx.antialias = 'none';
resOriginal.style = res.style = styleBlack;
res.blendMode = 'xor';
paper.view.draw();
// Accumulated pixels
var cumul = 0;
// Run the test
if(!saveFileName){
var image = ctx.getImageData(0, 0, width, height),
pixels = image.data, cw = image.width * 4, ch = image.height,
imgi, imgj;
for (imgj = 0; imgj < ch; imgj++) {
var pixrow = imgj * cw;
for (imgi = 0; imgi < cw; imgi += 4) {
if(pixrow[pixrow + imgi])
++cumul;
}
}
} else
// Save the bitmap
fs.writeFileSync(saveFileName, canvas.toBuffer());
// Reset the default values
ctx.antialias = 'default';
res.blendMode = 'normal';
return cumul < precision.bitmap;
}
// Compare two paths
function compare(p1, p2, precision, results) {
var eqChild = false, eqCurves = false, eqArea = false,
ch1 = p1 instanceof paper.CompoundPath ? p1.children.length : 1,
ch2 = p2 instanceof paper.CompoundPath ? p2.children.length : 1,
crv1 = p1.getCurves(), crv2 = p2.getCurves(),
a1 = Math.abs(p1.getArea()) | 0, a2 = Math.abs(p2.getArea()) | 0;
eqChild = ch1 === ch2;
eqCurves = crv1.length === crv2.length;
eqArea = Math.abs(a1 - a2) < precision.area;
if (results){
results.child = eqChild;
results.ch1 = ch1;
results.ch2 = ch2;
results.curves = eqCurves;
results.area = eqArea;
}
return (!testModes.paths || eqChild) && (!testModes.curves || eqCurves) && (!testModes.area || eqArea);
}
function formatTimeIvl(tms) {
var ts = 0, tm = 0;
if (tms > 1000){
ts = (tms / 1000.0) | 0;
tms -= ts * 1000;
}
if (ts > 60){
tm = (ts / 60.0) | 0;
ts -= tm * 60;
}
return tm + " : " + ts + " : " + tms.toFixed(1);
}